2021-10-17 20:01:45 +02:00
:PROPERTIES:
:ID: 986ca7a5-d225-49bb-9e35-f2dffafe8aee
:END:
#+title : Org Mode
#+filetags : emacs-load
2022-07-31 11:03:59 +02:00
# SPDX-FileCopyrightText: 2022 Richard Brežák <richard@brezak.sk>
#
# SPDX-License-Identifier: LGPL-3.0-or-later
2021-10-17 20:01:45 +02:00
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.
#+BEGIN_SRC emacs-lisp :results none
(add-hook 'org-mode-hook 'visual-line-mode)
#+END_SRC
2023-01-15 01:16:04 +01:00
Enable /"fake"/ indentation in =org-mode= , in other words, add indentation using overlays, but on disk the buffer is not indented.
2021-10-17 20:01:45 +02:00
#+BEGIN_SRC emacs-lisp :results none
(add-hook 'org-mode-hook 'org-indent-mode)
#+END_SRC
Increase the size of headings, in my personal opinion this makes the headings stand out a bit more and therefore easier to read.
#+BEGIN_SRC emacs-lisp :results none
(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)))))
#+END_SRC
2022-02-20 15:26:48 +01:00
Disable element cache for now, it freaks out all the damn time. God forbid I make a tiny syntax error...
#+begin_src emacs-lisp
(setq org-element-use-cache nil)
#+end_src
2022-06-07 17:08:31 +02:00
#+begin_src emacs-lisp
(setf org-blank-before-new-entry '((heading . t) (plain-list-item . nil)))
#+end_src
2022-01-26 20:39:11 +01:00
* Org Mark Ring
To go back to the previous mark, very useful with [[id:18476d68-cccb-48f4-aa77-caefe213d8bd ][Org Roam ]].
#+BEGIN_SRC emacs-lisp :results none
(general-def org-mode-map "C-c b" 'org-mark-ring-goto)
#+END_SRC
2021-10-17 20:01:45 +02:00
* Babel
Enable =tangle on save= , big thanks to Diego Zamboni for his amazing booklet about /[[https:/ /leanpub.com/lit-config/read][Literate Configuration]]/ .
#+BEGIN_SRC emacs-lisp :results none
2023-01-15 01:16:04 +01:00
(add-hook 'org-mode-hook
(lambda () (add-hook 'after-save-hook #'org-babel-tangle :append :local)))
2021-10-17 20:01:45 +02:00
#+END_SRC
After executing a source code block with =org-babel= , redisplay inline images, this speeds up the REPL-like workflow a lot.
#+BEGIN_SRC emacs-lisp :results none
(add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)
#+END_SRC
Enable additional babel languages.
#+BEGIN_SRC emacs-lisp :results none
(org-babel-do-load-languages
'org-babel-load-languages
(cl-map 'list (lambda (lang) `(,lang . t))
2022-10-07 22:05:43 +02:00
'(python R shell dot latex plantuml)))
2021-10-17 20:01:45 +02:00
#+END_SRC
* Latex
2022-12-11 20:37:09 +01:00
For previews, create SVGs and not PNGs or something, use the ~dvisvgm-lua~ command.
2021-10-17 20:01:45 +02:00
#+BEGIN_SRC emacs-lisp :results none
(setq org-preview-latex-default-process 'dvisvgm)
#+END_SRC
2022-12-11 20:37:09 +01:00
Actually define ~dvisvgm-lua~ .
#+begin_src emacs-lisp :results none
(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")))
#+end_src
2021-10-17 20:01:45 +02:00
2023-01-15 01:16:04 +01:00
Adjust size of LaTeX previews.
2022-12-11 20:37:09 +01:00
2023-01-09 13:06:55 +01:00
#+BEGIN_SRC emacs-lisp :results none
(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 0.8))))
2021-10-17 20:01:45 +02:00
#+END_SRC
2022-10-27 13:24:57 +02:00
To support non-breakable whitespace, create a new ~org-entity~ .
#+begin_src emacs-lisp
(add-to-list 'org-entities
'("space" "~" nil " " " " " " " "))
#+end_src
2022-10-29 21:43:35 +02:00
2022-12-09 11:27:16 +01:00
Enable fontification for inline LaTeX blocks which convieniently also makes in fixed-width.
#+begin_src emacs-lisp
(setq org-highlight-latex-and-related '(native script entities))
#+end_src
2022-10-29 21:43:35 +02:00
** 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.
#+begin_src emacs-lisp
2022-12-11 20:37:09 +01:00
;;; -*- lexical-binding: t; -* -
2022-10-29 21:43:35 +02:00
(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)
2022-12-11 20:37:09 +01:00
(put-text-property beg end 'display nil)
(put-text-property beg end 'line-height nil))
2022-12-09 11:27:16 +01:00
(font-lock-fontify-region beg end)))
2022-10-29 21:43:35 +02:00
(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))
2023-01-09 13:06:55 +01:00
(image-spec (list 'image :type imagetype :file image :relief 0 :ascent 'center))
2022-10-29 21:43:35 +02:00
(ov (make-overlay beg end)))
(overlay-put ov 'org-overlay-type 'org-latex-overlay)
2022-12-11 20:37:09 +01:00
(overlay-put ov
'modification-hooks
(list (lambda (&rest _args)
(org-clear-latex-preview beg end))))
2022-10-29 21:43:35 +02:00
(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)))
2022-12-09 11:27:16 +01:00
;; loop through and check for empty lines, those will break rendering
2022-10-29 21:43:35 +02:00
(when (= (line-beginning-position) (line-end-position))
(goto-char (line-beginning-position))
(insert "%")
(forward-char -1))
2023-01-09 13:06:55 +01:00
(when (> (* dy 2) (- image-height y))
(setq dy (- image-height y)))
2022-10-29 21:43:35 +02:00
(with-silent-modifications
2022-12-09 11:27:16 +01:00
;; place the image property on the current line
2022-10-29 21:43:35 +02:00
(put-text-property (line-beginning-position) (line-end-position)
'display
(list
(list 'slice
0 y 1.0 dy)
2023-01-09 13:06:55 +01:00
image-spec))
2022-12-11 20:40:47 +01:00
;; ;; remove any fontification face so the images don't get colored
;; (put-text-property (line-beginning-position) (line-end-position)
;; 'face
2023-01-09 13:06:55 +01:00
;; nil)
)
2022-10-29 21:43:35 +02:00
(forward-line 1)
(setq y (+ y dy))))
(if (not (> (point) endm))
2022-12-11 20:37:09 +01:00
(let ((ov (make-overlay (- (point) 1) endm)))
2022-10-29 21:43:35 +02:00
(overlay-put ov 'org-overlay-type 'org-latex-overlay)
(overlay-put ov 'evaporate t)
(overlay-put ov 'invisible t))))
(set-marker endm nil))
2022-11-10 22:41:26 +01:00
(with-silent-modifications
(put-text-property beg end
'display
2023-01-09 13:06:55 +01:00
(list image-spec))))
2022-12-11 20:43:41 +01:00
(font-lock-fontify-region beg end)
2022-10-29 21:43:35 +02:00
(with-silent-modifications
2022-12-11 20:43:41 +01:00
(put-text-property beg end 'line-height t))))
2022-10-29 21:43:35 +02:00
#+end_src