dotfiles/emacs-lisp/org_mode.org
Magic_RB 580fec0979
Update flake.lock
Signed-off-by: Magic_RB <magic_rb@redalder.org>
2023-09-28 14:42:44 +02:00

7.7 KiB

Org Mode

#

I used to respect the 80 column limit, but why waste all the space when it can be dynamic. In this way all of the available screen space is utilized.

  (add-hook 'org-mode-hook 'visual-line-mode)

Enable "fake" indentation in org-mode, in other words, add indentation using overlays, but on disk the buffer is not indented.

  (add-hook 'org-mode-hook 'org-indent-mode)

Increase the size of headings, in my personal opinion this makes the headings stand out a bit more and therefore easier to read.

  (custom-set-faces
   '(org-level-1 ((t (:inherit outline-1 :height 1.25))))
   '(org-level-2 ((t (:inherit outline-2 :height 1.2))))
   '(org-level-3 ((t (:inherit outline-3 :height 1.15))))
   '(org-level-4 ((t (:inherit outline-4 :height 1.10))))
   '(org-level-5 ((t (:inherit outline-5 :height 1.05)))))

Disable element cache for now, it freaks out all the damn time. God forbid I make a tiny syntax error…

  (setq org-element-use-cache nil)
  (setf org-blank-before-new-entry '((heading . t) (plain-list-item . nil)))
  (setq org-src-window-setup 'current-window)

Org Mark Ring

To go back to the previous mark, very useful with Org Roam.

  (general-def org-mode-map "C-c b" 'org-mark-ring-goto)

Babel

Enable tangle on save, big thanks to Diego Zamboni for his amazing booklet about Literate Configuration.

   (add-hook 'org-mode-hook
             (lambda () (add-hook 'after-save-hook #'org-babel-tangle :append :local)))

After executing a source code block with org-babel, redisplay inline images, this speeds up the REPL-like workflow a lot.

  (add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)

Enable additional babel languages.

  (org-babel-do-load-languages
   'org-babel-load-languages
   (cl-map 'list (lambda (lang) `(,lang . t))
           '(python R shell dot latex plantuml)))

Latex

For previews, create SVGs and not PNGs or something, use the dvisvgm-lua command.

  (setq org-preview-latex-default-process 'dvisvgm)

Actually define dvisvgm-lua.

  (add-to-list
   'org-preview-latex-process-alist
   '(dvisvgm-lua
     :programs ("dvilualatex" "dvisvgm")
     :description "dvi > svg"
     :message "you need to install the programs: latex and dvisvgm."
     :image-input-type "dvi"
     :image-output-type "svg"
     :image-size-adjust (1.7 . 1.5)
     :latex-compiler ("dvilualatex -interaction nonstopmode -output-directory %o %f")
     :image-converter ("dvisvgm %f -n -b min -c %S -o %O")))

Adjust size of LaTeX previews.

  (pcase (system-name)
    ("heater" (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.75)))
    ("omen" (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.0))))

To support non-breakable whitespace, create a new org-entity.

  (add-to-list 'org-entities
        '("space" "~" nil "&nbsp;" " " " " " "))

Enable fontification for inline LaTeX blocks which convieniently also makes in fixed-width.

  (setq org-highlight-latex-and-related '(native entities))

Org Mode by default considers .aux files as log files and deletes them. Which among other things breaks tikzmark and babel

  (setq org-latex-logfiles-extensions (remove "aux" org-latex-logfiles-extensions))

Sliced Previews

Normally a LaTeX preview is just one huge image which makes Emacs really jumpy and makes writing prose a really unpleasant experience. With these two functions, that's fixed. They work using mainly text properties (overlays are still involved but only one per preview, max two) so it should be fast still.

  ;;; -*- lexical-binding: t; -*-

  (plist-put org-format-latex-options :background "Transparent")

  (defadvice org-clear-latex-preview (after org-prop-img--org-clear-later-preview (beg end) activate)
    (save-excursion
      (goto-char beg)
      (with-silent-modifications
        (put-text-property beg end 'read-only nil)
        (put-text-property beg end 'display nil)
        (put-text-property beg end 'line-height nil))
      (font-lock-fontify-region beg end)))

  (defun org--make-preview-overlay (beg end image &optional imagetype)
    "Build an overlay between BEG and END using IMAGE file.
    Argument IMAGETYPE is the extension of the displayed image,
    as a string.  It defaults to \"png\"."
    (let* ((imagetype (or (intern imagetype) 'png))
           (image-spec (list 'image :type imagetype :file image :ascent 'center))
           (ov (make-overlay beg end)))
      (overlay-put ov 'org-overlay-type 'org-latex-overlay)
      (overlay-put ov
                   'modification-hooks
                   (list (lambda (ov after &rest args)
                           (when (not after)
                             (org-clear-latex-preview (overlay-start ov) (overlay-end ov))))))
      (if (> (count-lines beg end) 1)
          (let ((image-height (cdr (image-size image-spec t)))

                (y 0)
                (endm (make-marker)))
            (set-marker endm end)
            (save-excursion
              (goto-char beg)
              (while (and (<= (point) endm) (< y image-height))
                (let* ((dy (line-pixel-height)))

                  ;; loop through and check for empty lines, those will break rendering
                  (when (= (line-beginning-position) (line-end-position))
                    (goto-char (line-beginning-position))
                    (insert "%")
                    (forward-char -1))

                  (when (> (* dy 2) (- image-height y))
                    (setq dy (- image-height y)))

                  (with-silent-modifications
                    ;; place the image property on the current line
                    (put-text-property
                     (line-beginning-position) (line-end-position)
                     'display
                     (list
                      (list 'slice
                            0 y 1.0 dy)
                      image-spec))
                    ;; ;; remove any fontification face so the images don't get colored
                    ;; (put-text-property (line-beginning-position) (line-end-position)
                    ;;                    'face
                    ;;                    nil)
                    )

                  (forward-line 1)
                  (setq y (+ y dy))))
              (if (not (> (point) endm))
                  (let ((ov (make-overlay (- (point) 1) endm)))

                    (overlay-put ov 'org-overlay-type 'org-latex-overlay)
                    (overlay-put ov 'evaporate t)
                    (overlay-put ov 'invisible t))))
            (set-marker endm nil))
        (with-silent-modifications
          (put-text-property beg end
                             'display
                             (list image-spec))))
      (font-lock-fontify-region beg end)
      (with-silent-modifications
        (put-text-property beg end 'line-height t))))