Skip to content

Latest commit

 

History

History
2653 lines (2565 loc) · 102 KB

config.org

File metadata and controls

2653 lines (2565 loc) · 102 KB

Literate doom-emacs config

Table of Contents

About

This is a literate config file for doom-emacs by hlinssner. This is meant to be used with the develop branch. Much of the basic setup is pulled from the emacs literate starter by gilbertw1. The companion packages page describes the basic package setup, while the init.el is described on the project homepage.

About that TOC..

So as mentioned here there is trouble when org-export tries to create files while keeping the org-toc tag.

It’s not really a problem since org-export actually generates a TOC for the files anyway.

Personal Information

Let’s set some variables with basic user information.

(setq user-full-name "Rohit Goswami (HaoZeke)"
      user-mail-address "rohit.goswami@aol.com")

Doom Emacs Stuff

Visual tweaks

Font Face

Honestly the basic font setting is simply not pretty enough.

(setq doom-font (font-spec :family "Intel One Mono" :size 18))
;; (setq doom-font (font-spec :family "Attribute Mono" :size 16))
(unless (find-font doom-font)
  (setq doom-font (font-spec :family "Cascadia Code PL" :size 14)))

Unicode Fonts

This should be implemented as fallback font once this is merged.

(setq doom-unicode-font (font-spec :family "Symbola" :size 20))

Helm and Childframes

I prefer the regular diminished font size, even for the childframes.

;; Undo the helm text enlargement in childframes
(setq +helm-posframe-text-scale 0)

Saner Dashboard

I have trouble at work with the current DOOM dashboard. This one is from the doom-emacs lead developer, as found on Discord.

(setq +doom-dashboard-banner-file (expand-file-name "banner.png" doom-private-dir))

Bugfixes

Most of these should be removed when doom fixes them.

(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
                         ("nongnu" . "https://elpa.nongnu.org/nongnu/")
                         ("elpa" . "https://elpa.gnu.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")
                         ))

Helm changes

This makes `helm` behave more like `ivy` while working with directories.

(after! helm
  ;; I want backspace to go up a level, like ivy
  (add-hook! 'helm-find-files-after-init-hook
    (map! :map helm-find-files-map
          "<DEL>" #'helm-find-files-up-one-level)))

Disable eager functions

Kill spellcheck

This is really really really excruciatingly slow for LaTeX mode and maybe even for other random buffers.

(setq-hook! 'LaTeX-mode-hook +spellcheck-immediately nil)

Snippets

I am not sure if these need to be initialized.

; AndreaCrotti
(use-package! yasnippet-snippets
  :after yasnippet)

Kill over-eager literate after-save-hook

This is a little too frequent for working with a git repo. Henrik mentioned an async version of the re-compliation might be in the works, until then however I will probably only manually trigger the re-compliation.

(after! org
  (remove-hook 'after-save-hook #'+literate|recompile-maybe))

Kill Orgmode template

These conflict with my other templates.

(set-file-template! "\\.org$" :ignore t)

Variables

(setq
   org_notes (concat (getenv "HOME") "/Git/Gitlab/Mine/Notes/")
   zot_bib (concat (getenv "HOME") "/Insync/r95g10@gmail.com/Google Drive/zotLib.bib")
   org-directory org_notes
   deft-directory org_notes
   org-roam-directory org_notes
   )

Theme

Sometimes I do tire of the defaults.

;; Also like doom-city-lights, doom-oceanic-next, doom-tomorrow-night, doom-wilmersdorf, leuven, doom-monokai-pro, doom-solarized-dark-high-contrast
;; Light ones like solarized
(setq doom-theme 'doom-tomorrow-night)

Do not Format on Save

Formatting with styler takes forever.

(setq +format-on-save-enabled-modes '(not emacs-lisp-mode ; works well enough without it
                                          sql-mode        ; sqlformat is broken
                                          tex-mode        ; latexindent is broken
                                          latex-mode      ; latexindent is broken
                                          bibtex-mode     ; is broken
                                          ess-r-mode      ; styler takes forever
                                          web-mode      ; dunno who this is for
                                          ))

Keybindings

These are eventually going to with general anyway. So it’s better to load that for now and carry on. Later this block can be prevented from being tangled.

General

  • [X] Remove once merged upstream.

This does very little other than load it and remind it that SPC is the leader for the other bindings to work.

(use-package! general)
;; Creating a constant for making future changes simpler
(defconst my-leader "SPC")
;; Tell general all about it
(general-create-definer my-leader-def
  :prefix my-leader)
  ;; :prefix my-leader)
;; (general-create-definer my-local-leader-def
;;   ;; :prefix my-local-leader
;;   :prefix "SPC m")

Evil Setup and Error Handling

Actually this might not be ported over so I’m just going to put this elsewhere.

;; I like short names
(general-evil-setup t)
;; Stop telling me things begin with non-prefix keys
(general-auto-unbind-keys)

Spacemacs Equivalency

Kill buffer

Also it’s inconvenient to have a key chord requiring two hands to close a buffer.

; Compatibility, delete when fully migrated
(defconst my-leader "SPC")
; Bind a new key chord
(map!
 (:leader
   (:prefix "b"
     :desc "Kill buffer" "d" #'kill-this-buffer)
   (:prefix ("k" . "kill")
     :desc "Save and kill" "e" 'save-buffers-kill-terminal
     :desc "Kill buffer" "b" 'my-kill-this-buffer
     :desc "Delete frame" "f" 'delete-frame
   (:prefix ("o" . "Other")
     :desc "Frames" "f" 'delete-other-frames
     :desc "Windows" "w" 'delete-other-windows
     )
   )
   ))
general.el

The binding syntax of the future, TODAY!

;; ** Global Keybindings
;; Normal mode?
(nmap
 :prefix my-leader
 "b d" #'kill-this-buffer
  ;; kill things
  "k" '(:ignore t :which-key "kill")
  "k e" 'save-buffers-kill-terminal
  "k b" 'my-kill-this-buffer
  "k f" 'delete-frame
  "k o f" 'delete-other-frames
  "k o w" 'delete-other-windows
 "a" 'helm-mini)
;; (my-leader-def 'normal 'override
;;   "a" 'org-agenda)

Global Maps

Multiple Cursors

These need practice. Many of these are already in the default configuration, but they are redefined here for mnemonic usage. Also to add the which-key hints.

(nmap
  :prefix "gz"
  :keymaps 'global
  "r" '(mc/edit-lines :wk "Span region")
  "z" '(+evil/mc-make-cursor-here :wk "Place frozen cursor")
  )

Move around with Links

Useful for navigating files and what not.

(map! :leader
      :desc "Follow thing"  "RET" 'org-open-at-point)

Replace Stuff

There are way too many of these to keep using helm.

(map! :leader
      (:prefix ("r" . "Replace")
      :desc "String" "s" 'replace-string
      :desc "Query" "q" 'query-replace
      (:prefix ("r" . "Regexp")
        :desc "String" "s" 'replace-regexp
        :desc "Query" "q" 'query-replace-regexp
        )
      )
      )

Insert Unicode

This should hopefully propogate across all modes.

(map! :leader
      (:prefix ("i" . "Insert")
       :desc "Unicode" "u" 'insert-char
       :desc "Snippet" "s" 'yas-insert-snippet
       :desc "From Clipboard" "y" '+default/yank-pop
       :desc "From Evil Registers" "r" 'counsel-evil-registers
      )
)

Wrap Words

  • [ ] Load conditionally

This is for working with the various options enabled by +smartparens.

(map! :leader
      (:prefix ("i" . "Insert")
        (:prefix ("w" . "Wrap")
          :desc "Backticks" "`" . 'sp-wrap-backtick
          :desc "Tildes" "~" . 'sp-wrap-tilde
          )))

Lookup

These were bound to really weird things.

(nmap
  :prefix my-leader
  ;; look things up
  "l" '(:ignore t :wk "lookup")
  "l o" '(+lookup/online-select :wk "Online")
  "l f" '(+lookup/file :wk "File")
  )

No ESC

The escape key for exiting things seems very painful.

(general-define-key
 :keymaps '(insert visual normal)
 "S-SPC" 'evil-force-normal-state)

Markdown Improvements

Local leader is already bound to `m` and there are few bindings, this just adds more.

(map! :localleader
      :map markdown-mode-map
      :prefix ("i" . "Insert")
      :desc "Blockquote"    "q" 'markdown-insert-blockquote
      :desc "Bold"          "b" 'markdown-insert-bold
      :desc "Code"          "c" 'markdown-insert-code
      :desc "Emphasis"      "e" 'markdown-insert-italic
      :desc "Footnote"      "f" 'markdown-insert-footnote
      :desc "Code Block"    "s" 'markdown-insert-gfm-code-block
      :desc "Image"         "i" 'markdown-insert-image
      :desc "Link"          "l" 'markdown-insert-link
      :desc "List Item"     "n" 'markdown-insert-list-item
      :desc "Pre"           "p" 'markdown-insert-pre
      (:prefix ("h" . "Headings")
        :desc "One"   "1" 'markdown-insert-atx-1
        :desc "Two"   "2" 'markdown-insert-atx-2
        :desc "Three" "3" 'markdown-insert-atx-3
        :desc "Four"  "4" 'markdown-insert-atx-4
        :desc "Five"  "5" 'markdown-insert-atx-5
        :desc "Six"   "6" 'markdown-insert-atx-6))

Org Noter

These bindings should probably be after org-noter is loaded.

(map! :localleader
      :map (org-mode-map pdf-view-mode-map)
      (:prefix ("o" . "Org")
        (:prefix ("n" . "Noter")
          :desc "Noter" "n" 'org-noter
          )))

Org Mode additions

Apart from extension specific bindings, here we define useful functions which are a part of org-mode.

(after! org (map! :localleader
      :map org-mode-map
      :desc "Eval Block" "e" 'ober-eval-block-in-repl
      (:prefix "o"
        :desc "Tags" "t" 'org-set-tags
        :desc "Roam Bibtex" "b" 'orb-note-actions
        (:prefix ("p" . "Properties")
          :desc "Set" "s" 'org-set-property
          :desc "Delete" "d" 'org-delete-property
          :desc "Actions" "a" 'org-property-action
          )
        )
      (:prefix ("i" . "Insert")
        :desc "Link" "l" 'org-insert-link
        :desc "Item" "o" 'org-toggle-item
        :desc "Citation" "c" 'org-ref-insert-cite-link
        :desc "Footnote" "f" 'org-footnote-action
        :desc "Table" "t" 'org-table-create-or-convert-from-region
        (:prefix ("d" . "Download")
         :desc "Screenshot" "s" 'org-download-screenshot
         :desc "Clipboard" "c" 'org-download-clipboard
         :desc "Link" "l" 'org-download-image
         )
        (:prefix ("b" . "Math")
         :desc "Bold" "f" 'org-make-bold-math
         :desc "Blackboard" "b" 'org-make-blackboard-math
         :desc "Remove" "r" 'org-make-symrm-math
         :desc "Vert" "v" 'org-make-vert-math
         )
        (:prefix ("h" . "Headings")
          :desc "Normal" "h" 'org-insert-heading
          :desc "Todo" "t" 'org-insert-todo-heading
          (:prefix ("s" . "Subheadings")
            :desc "Normal" "s" 'org-insert-subheading
            :desc "Todo" "t" 'org-insert-todo-subheading
            )
          )
        (:prefix ("e" . "Exports")
          :desc "Dispatch" "d" 'org-export-dispatch
          )
        )
      )
  )

Math Environments

Modified from this TeXSE answer.

(add-hook 'LaTeX-mode-hook 'add-my-latex-environments)
(defun add-my-latex-environments ()
  (LaTeX-add-environments
   '("thm" LaTeX-env-label)
   '("prop" LaTeX-env-label)
   '("lem" LaTeX-env-label)
   '("cor" LaTeX-env-label)
   '("defn" LaTeX-env-label)
   '("not" LaTeX-env-label)
   '("rem" LaTeX-env-label)
   '("ex" LaTeX-env-label)
   '("align" LaTeX-env-label)
   '("notation" LaTeX-env-label)
   '("dmath" LaTeX-env-label)
     ))

;; Code I added to make syntax highlighting work in Auctex

(custom-set-variables
 '(font-latex-math-environments (quote
     ("display" "displaymath" "equation" "eqnarray" "gather" "multline"
      "align" "alignat" "xalignat" "dmath")))
  '(TeX-insert-braces nil)) ;;Stops putting {} on argumentless commands to "save" whitespace

;; Additionally, reftex code to recognize this environment as an equation
(setq reftex-label-alist
  '(("dmath" ?e nil nil t)))

Special Math Environments

Stolen from here. Note that these are meant to work with unicode-math.

(defun org-make-bold-math ()
  "If there's a selection -- wrap this with '\symbf{' and '}'
   and put the point to the end.  Otherwise -- put the point
   between '\symbf{' and '}'

   Also: when not in math mode -- enclose the thing in dollars."

  (interactive)

  (let (start end
              (delim "")
              (jump 1)
              )

    (when (not (texmathp))
      (setq delim "$")
      (setq jump 2)
      )

    (if (use-region-p)
        (progn
          (setq start (region-beginning))
          (setq end (region-end))

          (narrow-to-region start end)

          (goto-char (point-min))
          (insert (concat delim "\\symbf{"))

          (goto-char (point-max))
          (insert (concat "}" delim))
          (widen)
          )

      (progn
        (insert (concat delim "\\symbf{}" delim))
        (backward-char jump)
        )
      )
))


(defun org-make-blackboard-math ()
  "If there's a selection -- wrap this with '\symbb{' and '}'
   and put the point to the end.  Otherwise -- put the point
   between '\symbb{' and '}'

   Also: when not in math mode -- enclose the thing in dollars."

  (interactive)

  (let (start end
              (delim "")
              (jump 1)
              )

    (when (not (texmathp))
      (setq delim "$")
      (setq jump 2)
      )

    (if (use-region-p)
        (progn
          (setq start (region-beginning))
          (setq end (region-end))

          (narrow-to-region start end)

          (goto-char (point-min))
          (insert (concat delim "\\symbb{"))

          (goto-char (point-max))
          (insert (concat "}" delim))
          (widen)
          )

      (progn
        (insert (concat delim "\\symbb{}" delim))
        (backward-char jump)
        )
      )
))

(defun org-make-symrm-math ()
  "If there's a selection -- wrap this with '\symrm{' and '}'
   and put the point to the end.  Otherwise -- put the point
   between '\symrm{' and '}'

   Also: when not in math mode -- enclose the thing in dollars."

  (interactive)

  (let (start end
              (delim "")
              (jump 1)
              )

    (when (not (texmathp))
      (setq delim "$")
      (setq jump 2)
      )

    (if (use-region-p)
        (progn
          (setq start (region-beginning))
          (setq end (region-end))

          (narrow-to-region start end)

          (goto-char (point-min))
          (insert (concat delim "\\symrm{"))

          (goto-char (point-max))
          (insert (concat "}" delim))
          (widen)
          )

      (progn
        (insert (concat delim "\\symrm{}" delim))
        (backward-char jump)
        )
      )
))

(defun org-make-vert-math ()
  "If there's a selection -- wrap this with '\vert{' and '}'
   and put the point to the end.  Otherwise -- put the point
   between '\vert{' and '}'

   Also: when not in math mode -- enclose the thing in dollars."

  (interactive)

  (let (start end
              (delim "")
              (jump 1)
              )

    (when (not (texmathp))
      (setq delim "$")
      (setq jump 2)
      )

    (if (use-region-p)
        (progn
          (setq start (region-beginning))
          (setq end (region-end))

          (narrow-to-region start end)

          (goto-char (point-min))
          (insert (concat delim ""))

          (goto-char (point-max))
          (insert (concat "" delim))
          (widen)
          )

      (progn
        (insert (concat delim "‖‖" delim))
        (backward-char jump)
        )
      )
))

Anki Editor

These are only relevant to org-mode. Nevertheless they are not part of org-mode so semantically it makes no sense to use o after the localleader.

(map! :localleader
      :map org-mode-map
      (:prefix ("a" . "Anki")
        :desc "Push" "p" 'anki-editor-push-notes
        :desc "Retry" "r" 'anki-editor-retry-failure-notes
        :desc "Insert" "n" 'anki-editor-insert-note
        (:prefix ("c" . "Cloze")
          :desc "Dwim" "d" 'anki-editor-cloze-dwim
          :desc "Region" "r" 'anki-editor-cloze-region
          )
        )
 )

CC Mode

These are basically wrappers around various rtags functions.

(nmap
:prefix my-leader
:keymaps 'c-mode-base-map
"m" '(:ignore t :wk "Local Commands")
"m r" '(:ignore t :wk "Rtags")
"m r c" '(rtags-check-includes :wk "Check Includes")
;; All the find commands
"m r f" '(:ignore t :wk "Find")
"m r f s" '(:ignore t :wk "Symbol")
"m r f s a" '(rtags-find-symbol-at-point :wk "At point")
"m r f s s" '(rtags-find-symbol :wk "Symbol")
"m r f s c" '(:ignore t :wk "Current")
"m r f s c f" '(rtags-find-symbol-current-file :wk "File")
"m r f s c d" '(rtags-find-symbol-current-dir :wk "Directory")
"m r f f" '(rtags-find-functions-called-by-this-function :wk "Functions")
"m r f r" '(rtags-find-references :wk "References")
)

Evil Colemak

These are mostly because movement without hnei is horrible. Read about it here.

(use-package! evil-colemak-basics
  :after evil
  :config
  (setq evil-colemak-basics-rotate-t-f-j t)
  (global-evil-colemak-basics-mode)
  )

Visual Lines

Since I tend to keep visual-line-mode all the time, evil-better-visual-line is a natural choice.

(use-package! evil-better-visual-line
  :after evil-colemak-basics
  :config
  (evil-better-visual-line-on)
  (map! :map evil-colemak-basics-keymap
        (:nvm "n" 'evil-better-visual-line-next-line
         :nvm "e" 'evil-better-visual-line-previous-line
         :nvm "g n" 'evil-next-line
         :nvm "g e" 'evil-previous-line))
)

Search

Harmonizing with Vimium.

(after! evil (map! :map evil-motion-state-map
                   (:n :desc "Previous match" "K" 'evil-search-previous
                    :n :desc "Next match" "k" 'evil-search-next
                    :n :desc "Forward search" "/" 'evil-search-forward
                    )
                   ))

Window Bindings

These are somehow not part of the evil-colemak setup.

(after! evil
  (map! :map evil-window-map
        (:leader
         (:prefix ("w" . "Select Window")
          :n :desc "Left"  "h" 'evil-window-left
          :n :desc "Up"    "e" 'evil-window-up
          :n :desc "Down"  "n" 'evil-window-down
          :n :desc "Right" "i" 'evil-window-right
          ))
        ))

Page Movement

Harmonizing with Zathura.

(after! evil
  (map! :map evil-colemak-basics-keymap
      :nv "N" 'evil-scroll-page-down
      :nv "E" 'evil-scroll-page-up)
  )

Evil Org

Annoyingly, evil-org-mode had a map which kept overriding all my other settings. Thankfully it has a helper variable to set movement. I also do not need this anyway, at-least not by default.

(after! org
  (remove-hook 'org-mode-hook 'evil-org-mode)
  (setq evil-org-movement-bindings
        '((up . "e") (down . "n")
          (left . "h") (right . "i"))
        )
)

Neotree –> Treemacs

CANCELLED Toggle pane

This remaps SPC o N to use treemacs. I guess this doesn’t make all that much sense, but t and T and bound to terminals and that makes sense, so I guess this is fine.

;; Remap opening the sidebar
(map! :leader
      :nv "o n" nil
      :desc "Open treemacs pane"
      :n "o n" #'+treemacs/toggle)
;; Remap finding stuff
(map! :leader
      :nv "o N" nil
      :desc "Treemacs find file"
      :n "o N" 'treemacs-find-file)

Cancelled since this commit on the develop branch.

TeX Mode

These are more semantic for me.

(nmap
  :prefix my-leader
  :keymaps '(latex-mode-map tex-mode-map LaTeX-mode-map)
  ;; Folding Stuff
  "m f" '(:ignore t :wk "Fold Things")
  "m f c" '(TeX-fold-comment :wk "Comment")
  "m f e" '(TeX-fold-env :wk "Environment")
  "m f m" '(TeX-fold-math :wk "Math")
  ;; Insertions
  "m i" '(:ignore t :wk "Insert")
  "m i m" '(helm-insert-latex-math :wk "Math Symbols")
  "m i r" '(:ignore t :wk "References")
  "m i r h" '(helm-bibtex-with-local-bibliography :wk "Helm")
  "m i r r" '(reftex-citation :wk "Reftex")
  )

Safe Evals and Variables

MacOS Variables

To use path variables more easily.

(cond ((featurep :system 'macos) (use-package! exec-path-from-shell
                :config
                (exec-path-from-shell-initialize)
                )
              ))

Private Variables

On a Mac, when using Nix and Emacs 28, some of the path variables need to be set to find gpg. These should probably be handled through doom env since the terminal does not have trouble finding gpg.

(cond ((featurep :system 'macos) (custom-set-variables '(epg-gpg-program "/usr/local/bin/gpg")) ;; brew
              (setq exec-path (append exec-path '("/run/current-system/sw/bin"))) ;; nix
              (setq exec-path (append exec-path '("/usr/local/bin/"))) ;; brew
              ))

These are encrypted with gpg and are essentially set mostly by custom-*.

(use-package! epa-file
  :demand
  :config
  (epa-file-enable)
    (load (concat doom-private-dir "local/private.el.gpg"))
  )

Safe Variables

The problem is that packages.el isn’t being produced by the clever little ugly commit I tried so, this is a workaround to tangle any file to be produced in .el format in the same location.

Tangle

So adding the automatic tangling code doesn’t mangle things up everytime you open emacs.

(setq safe-local-variable-values '((after-save-hook . haozeke/org-save-and-export-latex)
 (before-save-hook . org-babel-execute-buffer)))

KILL Caveats

  • This actually forms it relative to the exact path. (Gotta move it to the config folder)
  • The actual code is much more elegant in every way possible.
  • Seriously there has to be a way to not have to do this.

Asynchronous Exports

As per this interesting answer on the superuser forums, I need to set org-export-async-init-file.

(setq org-export-async-init-file (concat doom-private-dir "local/async-ox.el"))

TRAMP Settings

I use some paths on my remote machines, which are non-standard.

(after! tramp
  (add-to-list 'tramp-remote-path "~/.local/bin")
  (add-to-list 'tramp-remote-path "~/.cargo/bin")
  (add-to-list 'tramp-remote-path "~/.hpc/bin")
  (add-to-list 'tramp-remote-path "~/.local/lsp/bin")
  (add-to-list 'tramp-remote-path "~/.micromamba/envs/lsp/bin/")
  )

Language Servers

Between lsp-mode and eglot I ended up with eglot for reasons better described here.

(after! eglot
  :config
  (add-hook 'nix-mode-hook 'eglot-ensure)
  (add-hook 'f90-mode-hook 'eglot-ensure)
  (set-eglot-client! 'cc-mode '("clangd" "-j=3" "--clang-tidy"))
  (set-eglot-client! 'python-mode '("pylsp"))
  (when (string= (system-name) "Rohits-MacBook-Pro.local")
  (setq exec-path (append exec-path '(
                                      (concat (getenv "HOME") "/.micromamba/envs/lsp/bin/") ;; python, fortran
                                      (concat (getenv "HOME") "/.local/lsp/bin/") ;; clangd
                                      (concat (getenv "HOME") "/.digestif/bin/") ;; tex
                                      (concat (getenv "HOME") "/.nvm/versions/node/v16.1.0/bin/bash-language-server")
                                      )))
    )
  )

Package Settings

These should eventually go into a different module. Each of these.

Word wrap

This section is to work with the settings for the word-wrap doom module.

;; enable word-wrap in C/C++/ObjC/Java
(add-hook! 'markdown-mode-hook #'+word-wrap-mode)
(add-hook! 'text-mode-hook #'+word-wrap-mode)
(add-hook! 'tex-mode-hook #'+word-wrap-mode)

Spellings

(after! spell-fu
  (setq spell-fu-idle-delay 0.5 ; default is 0.25
        ispell-program-name "hunspell"
        ;; aspell -> "--sug-mode=ultra"
        ;;ispell-extra-args '("-d en_US")
        ispell-dictionary "en_US" ; needed for MacOS in particular
        ispell-personal-dictionary "~/.aspell.en.pws" ; standard location
        spell-fu-dictionary "~/.config/dict" ; standard location
        )
  ;; use American English as ispell default dictionary
  (ispell-change-dictionary "american" t)
  (setq-default ispell-program-name "hunspell")
)

Also, it gets annoying to have spell check enabled while working with code.

;; Can always be enabled with SPC t s
(remove-hook 'text-mode-hook #'spell-fu-mode)

Magit Aids

Magit todos

Of course this is not really meant to be here.. A variation of this was included upstream in the develop branch.

(use-package! magit-org-todos
  :mode "\\COMMIT_EDITMSG\\'"
  :commands (magit-org-todods magit-org-todos-autoinsert)
  :config
  (magit-org-todos-autoinsert))
(use-package! magit-todos)

Magithub

This is for sweet github integration. Also integrated upstream.

(use-package! magithub
  :after magit
  :commands (magithub-clone
             magithub-completion-enable)
  ;; :ensure t
  :config
  (magithub-feature-autoinject t)
  (setq
   magithub-clone-default-directory "$HOME/Git/Github/"
   magithub-dir (concat doom-etc-dir "magithub/")
   magithub-preferred-remote-method 'clone_url))
(use-package! evil-magit :after magit
  :init
  (setq evil-magit-state 'normal))

HOLD Magit Delta

(use-package! magit-delta
  :if (executable-find "delta")
  :hook (magit-mode . magit-delta-mode)
  :config
  ; Kanged https://github.com/dandavison/magit-delta/issues/13
  (setq magit-delta-delta-args (append magit-delta-delta-args '("--features" "magit-delta")))
  ; Kanged https://github.com/dandavison/magit-delta/issues/15#issuecomment-865774240
  (defun hz/magit-delta-toggle ()
    "Toggle magit-delta-mode and refresh magit."
    (interactive)
    (progn
      (call-interactively 'magit-delta-mode)
      (magit-refresh)))
  :general
  (:keymaps 'magit-mode-map
            :states 'normal
            :prefix ","
            "t" '(hz/magit-delta-toggle :wk "toggle magit-delta"))
    )

Currently there are still performance issues, which is why the toggle keybinding is useful, however, there is a nicer workaround, simply disable for larger diffs as described here. Eventually this should be fixed somewhere else.

(defvar hz/magit-delta-point-max 50000)
;; Disable mode if there are too many characters
(advice-add 'magit-delta-call-delta-and-convert-ansi-escape-sequences :around
            (defun hz/magit-delta-colorize-maybe-a (fn &rest args)
              (if (<= (point-max) hz/magit-delta-point-max)
                  (apply fn args)
                (magit-delta-mode -1))))
;; Re-enable mode after `magit-refresh' if there aren't too many characters
(add-hook 'magit-post-refresh-hook
          (defun hz/magit-enable-magit-delta-maybe-h (&rest _args)
            (when (and (not magit-delta-mode)
                       (<= (point-max) hz/magit-delta-point-max))
              (magit-delta-mode +1))))

PDF Tools

These bindings are essentially part of org-noter however, they do not actually need to be bound in org-mode files. Also updated to have evil-colemak bindings.

(after! pdf-view
  ;; open pdfs scaled to fit page
  (setq-default pdf-view-display-size 'fit-width)
  (add-hook! 'pdf-view-mode-hook (evil-colemak-basics-mode -1))
  ;; automatically annotate highlights
  (setq pdf-annot-activate-created-annotations t
        pdf-view-resize-factor 1.1)
   ;; faster motion
 (map!
   :map pdf-view-mode-map
   :n "g g"          #'pdf-view-first-page
   :n "G"            #'pdf-view-last-page
   :n "N"            #'pdf-view-next-page-command
   :n "E"            #'pdf-view-previous-page-command
   :n "e"            #'evil-collection-pdf-view-previous-line-or-previous-page
   :n "n"            #'evil-collection-pdf-view-next-line-or-next-page
   :localleader
   (:prefix "o"
    (:prefix "n"
     :desc "Insert" "i" 'org-noter-insert-note
     ))
 ))

Anki Editor

This is for my favorite anki interaction mechanism.

(use-package! anki-editor
  :after org-noter
  :config
  ; I like making decks
  (setq anki-editor-create-decks 't))

Org Additions

These are numerous and complicated enough to be in a segment of their own.

todo Modifications

I like having the date on my TODO items.

(setq org-log-done "time"
      org-log-done-with-time 't)

Ignore Support

For the longest time I was setting this per file for bizarre reasons. This makes much more sense.

(use-package! ox-extra
  :after org
  :config
  (ox-extras-activate '(ignore-headlines))
  )

File Handling

This controls what is used to open links in org documents. Since there are only a few defaults defined, I am just prepending them to my changes instead of dealing with append and stuff.

(setq org-file-apps
  '((auto-mode . emacs)
    ("\\.mm\\'" . default)
    ("\\.x?html?\\'" . default)
    ("\\.pdf\\'" . default)
    ("\\.png\\'" . viewnior)
    ("\\.jpg\\'" . viewnior)
    ("\\.svg\\'" . viewnior)
    ))

Ob-Mermaid

(use-package! mermaid-mode)
(use-package! ob-mermaid)

KILL Ob-Julia

So julia support is inbuilt, however the process variable needs to be set:

(setq  inferior-julia-program-name "/bin/julia")

Org Download

This is already included in the standard doom setup. However, I was having trouble with relative exports so I have this one instead. Partially kanged from doom-emacs. Before setting this up, a new function is defined to call the screenshot process, this setup is kanged from here.

;; From https://github.com/poligen/dotfiles/blob/25785810f9bf98f6eec93e400c686a4ad65ac310/doom.d/config.el
;; My customized org-download to incorporate flameshot gui Workaround to setup flameshot, which enables annotation.
;; In flameshot, set filename as "screenshot", and the command as "flameshot gui -p /tmp", so that we always ends up
;; with /tmp/screenshot.png. Nullify org-download-screenshot-method by setting it to `echo', so that essentially we
;; are only calling (org-download-image org-download-screenshot-file).
(defun hz-org-download-screenshot ()
  "Capture screenshot and insert the resulting file.
The screenshot tool is determined by `org-download-screenshot-method'."
  (interactive)
  (let ((tmp-file "/tmp/screenshot.png"))
    (delete-file tmp-file)
    (call-process-shell-command "flameshot gui -p /tmp/")
    ;; Because flameshot exit immediately, keep polling to check file existence
    (while (not (file-exists-p tmp-file))
      (sleep-for 2))
    (org-download-image tmp-file)))

Now we can configure the package itself.

(use-package! org-download
  :after org
  :config
  (setq-default org-download-image-dir "./images/"
                ;; org-download-screenshot-method "flameshot gui --raw > %s"
                org-download-delete-image-after-download t
                org-download-method 'directory
                org-download-heading-lvl 1
                org-download-screenshot-file "/tmp/screenshot.png"
                )
  (cond ((featurep :system 'linux)
         (if (string-match-p "wayland" (getenv "WAYLAND_DISPLAY"))
             (setq-default org-download-screenshot-method "grim -g \"$(slurp)\" - > %s")
             (setq-default org-download-screenshot-method "xclip -selection clipboard -t image/png -o > %s")
           )
         )
        ((featurep :system 'macos) (setq-default org-download-screenshot-method "screencapture -i %s")))
  )

Org Babel

Julia and Mathematica are not set. Other languages might also be needed here eventually.

(after! 'org
            (org-babel-do-load-languages 'org-babel-load-languages
                                         (append org-babel-load-languages
                                                 ;; '((julia . t))
                                                 '((mathematica . t))
                                                 '((ditaa . t))
                                                 ))
            )
(setq org-babel-mathematica-command "~/.local/bin/mash"
      org-ditaa-jar-path (concat (getenv "HOME") "/.local/bin/ditaa0_9.jar")
)

Async Org Babel

From here. Now we can configure this.

(use-package! org-babel-eval-in-repl
  :after org
  :config
  (setq eir-jump-after-eval nil)
  )

Org Config

These are just variables I need to set to prevent things from dying.

Switching to XeLaTeX

Since I use a lot of unicode math; it makes sense to switch from LaTeX to XeLaTeX everywhere. This section borrows heavily from here and here. Might possibly want to look at this later too.

;; Set after the default-packages list anyway
(setq org-latex-packages-alist 'nil)
(setq org-latex-default-packages-alist
  '(("AUTO" "inputenc"  t ("pdflatex"))
    ("T1"   "fontenc"   t ("pdflatex"))
    (""     "graphicx"  t)
    (""     "grffile"   t)
    (""     "minted"   t)
    ;; ("dvipsnames,svgnames*,x11names*,table"     "xcolor"   t)
    (""     "longtable" nil)
    (""     "wrapfig"   nil)
    (""     "rotating"  nil)
    ("normalem" "ulem"  t)
    (""     "amsmath"   t)
    (""     "amssymb"   t)
    (""     "unicode-math"   t)
    (""     "mathtools"   t)
    (""     "textcomp"  t)
    (""     "capt-of"   nil)
    (""     "hyperref"  nil)))
;; (add-to-list 'org-latex-default-packages-alist '("" "fontspec" t))
;; (setq org-latex-inputenc-alist '(("utf8" . "utf8x")))
;; (add-to-list 'org-latex-packages-alist '("" "unicode-math"))
(after! org
  (plist-put org-format-latex-options :scale 2.2)
  (add-to-list 'org-preview-latex-process-alist '(dvixelatex :programs
                                                  ("xetex" "convert")
                                                  :description "pdf > png" :message "you need to install the programs: xetex and imagemagick." :image-input-type "pdf" :image-output-type "png" :image-size-adjust
                                                  (1.0 . 1.0)
                                                  :latex-compiler
                                                  ("xelatex -no-pdf -interaction nonstopmode -output-directory %o %f")
                                                  :image-converter
                                                  ("dvisvgm %f -n -b min -c %S -o %O")))

  (add-to-list 'org-preview-latex-process-alist '(imagexetex :programs
                                                  ("xelatex" "convert")
                                                  :description "pdf > png" :message "you need to install the programs: xelatex and imagemagick." :image-input-type "pdf" :image-output-type "png" :image-size-adjust
                                                  (1.0 . 1.0)
                                                  :latex-compiler
                                                  ("xelatex -interaction nonstopmode -output-directory %o %f")
                                                  :image-converter
                                                  ("convert -density %D -trim -antialias %f -quality 100 %O")))
  )

Inline images

These need to be disabled by default otherwise emacs stalls often. Also, it turns out that dvipng has some bugs on my system, so Imagemagick works better, once security policies have been adjusted in /etc/ImageMagick-/policy.xml.

(after! org
  (setq org-preview-latex-default-process 'imagexetex)
  (setq org-startup-with-inline-images 'nil)
  (setq org-image-actual-width 400)
  )

Org Latex Subfigure

This modified environment makes it easier to work with reports.

;; TODO: Figure out how to pass parameters other than width
;; TODO: Also make a subcaption label
  (org-link-set-parameters
   "subfig"
   :follow (lambda (file) (find-file file))
   :face '(:foreground "chocolate" :weight bold :underline t)
   :display 'full
   :export (lambda (file desc backend)
       (when (eq backend 'latex)
         (if (string-match ">(\\(.+\\))" desc)
       (concat "\\begin{subfigure}[b]{"
               (match-string 1 desc)
               "}\\caption{" (replace-regexp-in-string "\s+>(.+)" "" desc) "}"
         "\\includegraphics"
         "["
                           "width=\\textwidth"
         "]"
         "{"
            file
         "}"
            "\\end{subfigure}"
                      )
     (format "\\begin{subfigure}[b]{\\textwidth}\\includegraphics{%s}\\caption{%s}\\end{subfigure}" file desc)))))

Prevent invisible area modifications

First discussed here. The invisible area modifications are a plague. I can’t imagine why this is not set by default.

(setq org-catch-invisible-edits 'show-and-error)

Compact subtrees

We would like to not have additional newlines between trees.

(setq org-cycle-separator-lines 0)

Org Rifle

This probably needs to be refactored later. Or loaded elsewhere. The keymaps are defined in the following way:

(use-package! helm-org-rifle
  :after org
  :general
  (:keymaps 'org-mode-map
            :states 'normal
            :prefix my-leader
            "m r" '(:ignore t :wk "Rifle (Helm)")
            "m r b" '(helm-org-rifle-current-buffer :wk "Rifle buffer")
            "m r e" '(helm-org-rifle :wk "Rifle every open buffer")
            "m r d" '(helm-org-rifle-directory :wk "Rifle from org-directory")
            "m r a" '(helm-org-rifle-agenda-files :wk "Rifle agenda")
            "m r o" '(:ignore t :wk "Occur (Persistent)")
            "m r o b" '(helm-org-rifle-current-buffer :wk "Rifle buffer")
            "m r o e" '(helm-org-rifle :wk "Rifle every open buffer")
            "m r o d" '(helm-org-rifle-directory :wk "Rifle from org-directory")
            "m r o a" '(helm-org-rifle-agenda-files :wk "Rifle agenda")
            )
  )

Org Mind Map

This is used to create graphiz graphs from org-mode stuff.

(use-package! org-mind-map
  :general
  (:keymaps 'org-mode-map
            :states 'normal
            :prefix my-leader
            "m e m" '(org-mind-map-write :wk "Export mind-map") ))

Org Gantt

Useful TeX generator.

(use-package! org-gantt
 :after org)

Org Drill

It makes sense to keep this around so as to leverage existing resources.

;;(use-package! org-drill
;;  :after org)

Org Re-Reveal Additions

We will load a couple of helpers to get functionality closer to emacs-reveal. This is apparently the org-ref for org-re-reveal. For themes (e.g. robot-lung), we have a new directory. We would also now keep a local copy of our revealjs setup.

(after! org-re-reveal
  (use-package! org-re-reveal-ref) ; fixes org-ref
  (setq reveal-extras (concat doom-private-dir "reveal/extras/")
        org-re-reveal-root (concat doom-private-dir "reveal/rjs/")
        sfeir-root (concat doom-private-dir "reveal/sfeir-school-theme/"))
)

Note that this needs to be updated manually since themes change.

Org GCal

Note that we configure this as per the README in the private configuration since all of these are very personal.

(use-package! org-gcal)

Hugo Settings

This should be set for everything. I like to keep the last modified date, but only consider things to be modified if 12 hours have passed.

(setq org-hugo-auto-set-lastmod 't
      org-hugo-section "posts"
      org-hugo-suppress-lastmod-period 43200.0
      org-hugo-export-creator-string "Emacs 26.3 (Org mode 9.4 + ox-hugo + HaoZeke)"
)

Citeproc

I also like to have rational and numbered citations.

(after! ox-hugo
  (use-package! citeproc-org
    :config
    (citeproc-org-setup)
    (setq citeproc-org-org-bib-header "* References\n")
    )
  )

We will now lower the heading to the appropriate level. This is kanged from TimQuelch.

(after! citeproc-org
  (defun hz/min-headline-level ()
    (--> (org-element-parse-buffer)
         (org-element-map it 'headline (apply-partially #'org-element-property :level))
         (or it '(0))
         (-min it)))

  (defadvice! hz/citeproc-org-render-references (orig &rest args)
    :around 'citeproc-org-render-references
    (let* ((minlevel (hz/min-headline-level))
           (totallevel (max 1 minlevel))
           (citeproc-org-org-bib-header (concat (make-string totallevel ?*)
                                                (string-trim-left citeproc-org-org-bib-header "\\**"))))
      (apply orig args))))

CalcTeX

Writing TeX math can be painful, in-spite of all the unicode and other fancy tricks. This makes things a lot more feasible. Some of the configuration is directly lifted from Tecosaur’s configuration.

(use-package! calctex
  :commands calctex-mode
  :init
  (add-hook 'calc-mode-hook #'calctex-mode)
  :config
  (setq calctex-additional-latex-packages "
\\usepackage[usenames]{xcolor}
\\usepackage{soul}
\\usepackage{adjustbox}
\\usepackage{amsmath}
\\usepackage{amssymb}
\\usepackage{siunitx}
\\usepackage{cancel}
\\usepackage{mathtools}
\\usepackage{mathalpha}
\\usepackage{xparse}
\\usepackage{arevmath}"
        calctex-additional-latex-macros
        (concat calctex-additional-latex-macros
                "\n\\let\\evalto\\Rightarrow"))
  (defadvice! no-messaging-a (orig-fn &rest args)
    :around #'calctex-default-dispatching-render-process
    (let ((inhibit-message t) message-log-max)
      (apply orig-fn args)))
  ;; Fix hardcoded dvichop path (whyyyyyyy)
  (let ((vendor-folder (concat (file-truename doom-local-dir)
                               "straight/"
                               (format "build-%s" emacs-version)
                               "/calctex/vendor/")))
    (setq calctex-dvichop-sty (concat vendor-folder "texd/dvichop")
          calctex-dvichop-bin (concat vendor-folder "texd/dvichop")))
  (unless (file-exists-p calctex-dvichop-bin)
    (message "CalcTeX: Building dvichop binary")
    (let ((default-directory (file-name-directory calctex-dvichop-bin)))
      (call-process "make" nil nil nil))))

Now we would like also to have a sidebar for working in the embedded mode.

(defvar calc-embedded-trail-window nil)
(defvar calc-embedded-calculator-window nil)

(defadvice! calc-embedded-with-side-pannel (&rest _)
  :after #'calc-do-embedded
  (when calc-embedded-trail-window
    (ignore-errors
      (delete-window calc-embedded-trail-window))
    (setq calc-embedded-trail-window nil))
  (when calc-embedded-calculator-window
    (ignore-errors
      (delete-window calc-embedded-calculator-window))
    (setq calc-embedded-calculator-window nil))
  (when (and calc-embedded-info
             (> (* (window-width) (window-height)) 1200))
    (let ((main-window (selected-window))
          (vertical-p (> (window-width) 80)))
      (select-window
       (setq calc-embedded-trail-window
             (if vertical-p
                 (split-window-horizontally (- (max 30 (/ (window-width) 3))))
               (split-window-vertically (- (max 8 (/ (window-height) 4)))))))
      (switch-to-buffer "*Calc Trail*")
      (select-window
       (setq calc-embedded-calculator-window
             (if vertical-p
                 (split-window-vertically -6)
               (split-window-horizontally (- (/ (window-width) 2))))))
      (switch-to-buffer "*Calculator*")
      (select-window main-window))))

Syntax Highlighting

This section is for setting up major modes for various file formats which are typically non-standard. These are matched by extensions.

Misc Highlighting

Direnv

direnv is essentially a specialized bash script. Until I have time to make a proper font locking mode for it, this should suffice.

(setq auto-mode-alist (append '(("\\.envrc$" . shell-script-mode))
                              auto-mode-alist))

Vim

I still keep my vim dotfiles up to date…

(use-package! vimrc-mode
  :mode "\\.vimrc\\'")

CPP Additions

Doxygen Support

(use-package! highlight-doxygen
  :hook ((c-mode c++-mode) . highlight-doxygen-mode))

More Files

inl files are often used for extended header definitions.

(setq auto-mode-alist (append '(
                                ("\\.C$" . c++-mode)
                                ("\\.cc$" . c++-mode)
                                ("\\.cpp$" . c++-mode)
                                ("\\.inl$" . c++-mode)
                                ("\\.H$" . c++-mode)
                                ("\\.hh$" . c++-mode)
                                ("\\.hpp$" . c++-mode)
                                )
                              auto-mode-alist))

Fortran

By default, fortran-mode is used for f90 files which is less than ideal.

(setq auto-mode-alist (append '(
                              ("\\.f\\'" . fortran-mode)
                              ("\\.f77\\'" . fortran-mode)
                              ("\\.f90\\'" . f90-mode)
                              ("\\.F90\\'" . f90-mode)
                              )
                              auto-mode-alist))

Quarto

Not a fan, but is still occasionally useful.

(use-package! quarto-mode)

Meson

Mostly used with cpp files.

(use-package! meson-mode
  :mode "\\.build\\'"
  :config
  (add-hook!'meson-mode-hook 'company-mode)
  )

xonsh Mode

(use-package! xonsh-mode
  :mode "\\.xsh\\'"
  :config
  (add-hook!'xonsh-mode-hook 'company-mode)
  )

PKGBUILD Mode

This is the non doom way of loading this.

(autoload 'pkgbuild-mode "pkgbuild-mode.el" "PKGBUILD mode." t)
(setq auto-mode-alist (append '(("/PKGBUILD$" . pkgbuild-mode))
                              auto-mode-alist))

I use doom. So.

(use-package! pkgbuild-mode
  :mode "\\PKGBUILD")

LAMMPS Mode

No doom setup

For most users.

(autoload 'lammps-mode "lammps-mode.el" "LAMMPS mode." t)
(setq auto-mode-alist (append
                              '(("in\\.'" . lammps-mode))
                              '(("\\.lmp\\'" . lammps-mode))
                              auto-mode-alist
                              ))

Doom Version

With macros.

(use-package! lammps-mode)
(setq auto-mode-alist (append
                              '(("in\\.'" . lammps-mode))
                              '(("\\.lmp\\'" . lammps-mode))
                              auto-mode-alist
                              ))

Pug Mode

Need better font locking everywhere.

(use-package! pug-mode
  :mode "\\.pug\\'")

Conf Mode Files

The rc files are usually encountered while building android stuff. They are handled well by conf-mode. Turns out that vmd files also look just like conf-mode things…

(setq auto-mode-alist
             (append
             '(("\\.rc\\'" . conf-mode))
             '(("\\.vmd\\'" . conf-mode))
             auto-mode-alist
             ))

JVM Languages

Since java+meghnada, clojure, and scala are covered by the standard doom config, the rest of these need to be loaded here.

(use-package! kotlin-mode
  :mode "\\.kt\\'")

(use-package! groovy-mode
  :mode "\\.groovy\\'")

Systemd

For all those user-units.

(use-package! systemd
  :mode "\\.service\\'")

Dart Mode

Dart seems like a rather fun C-like language. Sort of fallen on the wayside what with Golang and what not but still might be worth a shot.

(use-package! dart-mode
  :mode "\\.dart\\'")

SaltStack

I like having spell checks for everything.

;; Load it
(use-package! salt-mode
  :config
;; Flyspell
(add-hook 'salt-mode-hook
        (lambda ()
            (flyspell-mode 1))))

Mathematica

Apparently, wolfram-mode is the best for syntax highlighting.

;; Load it
(use-package! wolfram-mode
  :config
  (setq mathematica-command-line "~/.local/bin/mash")
  (add-to-list 'org-src-lang-modes '("mathematica" . wolfram)))

Snakemake

(use-package! snakemake-mode
    :config
  (add-to-list 'auto-mode-alist '("\\.smk" . snakemake-mode)))

Aesthetics

Wakatime

Was removed from the core modules of doom-emacs.

(use-package! wakatime-mode
  :config
  (cond ((featurep :system 'linux) (setq wakatime-cli-path "/usr/bin/wakatime"))
        ((featurep :system 'macos) (setq wakatime-cli-path "/usr/local/bin/wakatime-cli")) ;; We assume homebrew
        )
  )

Dockerfile Mode

This package from spotify has support for building things as well as highlighting Dockerfiles.

(use-package! dockerfile-mode
  :mode "Dockerfile\\'"
  :config
  (put 'dockerfile-image-name 'safe-local-variable #'stringp)
  )

Functions

Org-Export HTML with useful IDs

This minor mode from here is crucial to having sane reveal-js slides which don’t keep jumping back to the title slide on every export.

(define-minor-mode unpackaged/org-export-html-with-useful-ids-mode
  "Attempt to export Org as HTML with useful link IDs.
Instead of random IDs like \"#orga1b2c3\", use heading titles,
made unique when necessary."
  :global t
  (if unpackaged/org-export-html-with-useful-ids-mode
      (advice-add #'org-export-get-reference :override #'unpackaged/org-export-get-reference)
    (advice-remove #'org-export-get-reference #'unpackaged/org-export-get-reference)))

(defun unpackaged/org-export-get-reference (datum info)
  "Like `org-export-get-reference', except uses heading titles instead of random numbers."
  (let ((cache (plist-get info :internal-references)))
    (or (car (rassq datum cache))
        (let* ((crossrefs (plist-get info :crossrefs))
               (cells (org-export-search-cells datum))
               ;; Preserve any pre-existing association between
               ;; a search cell and a reference, i.e., when some
               ;; previously published document referenced a location
               ;; within current file (see
               ;; `org-publish-resolve-external-link').
               ;;
               ;; However, there is no guarantee that search cells are
               ;; unique, e.g., there might be duplicate custom ID or
               ;; two headings with the same title in the file.
               ;;
               ;; As a consequence, before re-using any reference to
               ;; an element or object, we check that it doesn't refer
               ;; to a previous element or object.
               (new (or (cl-some
                         (lambda (cell)
                           (let ((stored (cdr (assoc cell crossrefs))))
                             (when stored
                               (let ((old (org-export-format-reference stored)))
                                 (and (not (assoc old cache)) stored)))))
                         cells)
                        (when (org-element-property :raw-value datum)
                          ;; Heading with a title
                          (unpackaged/org-export-new-title-reference datum cache))
                        ;; NOTE: This probably breaks some Org Export
                        ;; feature, but if it does what I need, fine.
                        (org-export-format-reference
                         (org-export-new-reference cache))))
               (reference-string new))
          ;; Cache contains both data already associated to
          ;; a reference and in-use internal references, so as to make
          ;; unique references.
          (dolist (cell cells) (push (cons cell new) cache))
          ;; Retain a direct association between reference string and
          ;; DATUM since (1) not every object or element can be given
          ;; a search cell (2) it permits quick lookup.
          (push (cons reference-string datum) cache)
          (plist-put info :internal-references cache)
          reference-string))))

(defun unpackaged/org-export-new-title-reference (datum cache)
  "Return new reference for DATUM that is unique in CACHE."
  (cl-macrolet ((inc-suffixf (place)
                             `(progn
                                (string-match (rx bos
                                                  (minimal-match (group (1+ anything)))
                                                  (optional "--" (group (1+ digit)))
                                                  eos)
                                              ,place)
                                ;; HACK: `s1' instead of a gensym.
                                (-let* (((s1 suffix) (list (match-string 1 ,place)
                                                           (match-string 2 ,place)))
                                        (suffix (if suffix
                                                    (string-to-number suffix)
                                                  0)))
                                  (setf ,place (format "%s--%s" s1 (cl-incf suffix)))))))
    (let* ((title (org-element-property :raw-value datum))
           (ref (url-hexify-string (substring-no-properties title)))
           (parent (org-element-property :parent datum)))
      (while (--any (equal ref (car it))
                    cache)
        ;; Title not unique: make it so.
        (if parent
            ;; Append ancestor title.
            (setf title (concat (org-element-property :raw-value parent)
                                "--" title)
                  ref (url-hexify-string (substring-no-properties title))
                  parent (org-element-property :parent parent))
          ;; No more ancestors: add and increment a number.
          (inc-suffixf ref)))
      ref)))

Org-mode export pdf when saved

This one is to generate pdfs whenever a buffer is saved. Mainly taken from this stack exchange question.

; Pdf
(defun haozeke/org-save-and-export-pdf ()
  (if (eq major-mode 'org-mode)
    (org-latex-export-to-pdf :async t)))

Org-mode export koma-letter

Since the koma-letter backend is separate, this needs a function as well.

(defun haozeke/org-save-and-export-koma-letter-pdf ()
  (if (eq major-mode 'org-mode)
    (org-koma-letter-export-to-pdf)))

Org-mode export TeX

Similar to the one above, but tex generation is much faster and this way I can keep editing my files without waiting for it to finish creating the pdf.

; LaTeX
(defun haozeke/org-save-and-export-latex ()
  (interactive)
  (if (eq major-mode 'org-mode)
    (org-latex-export-to-latex t)))
(defun haozeke/org-save-and-export-beamer ()
  (interactive)
  (if (eq major-mode 'org-mode)
    (org-beamer-export-to-latex t)))

Caveats

  • Minted needs to be setup.
  • There are really a lot of optimizations to the above.

Helper function

Figure out if I can replicate this some other way. Taken from sam217pa’s github repo.

;; this function is used to append multiple elements to the list 'ox-latex
(defun append-to-list (list-var elements)
  "Append ELEMENTS to the end of LIST-VAR. The return value is the new value of LIST-VAR."
  (unless (consp elements) (error "ELEMENTS must be a list"))
  (let ((list (symbol-value list-var)))
    (if list
        (setcdr (last list) elements)
      (set list-var elements)))
(symbol-value list-var))

Async Command without Buffers

This supresses the output window. Useful for when I do async exports. From this question.

(defun async-shell-command-no-window
    (command)
  (interactive)
  (let
      ((display-buffer-alist
        (list
         (cons
          "\\*Async Shell Command\\*.*"
          (cons #'display-buffer-no-window nil)))))
    (async-shell-command
     command)))

Better Rust Formatting

Some of these are kanged from here.

(setq rustic-format-on-save t
      ;; rustfmt uses `--edition 2015` by default. For now, 2021 seems to be a
      ;; reasonable alternative.
      rustic-rustfmt-args "--edition 2021")

Smarter Clang Formatting

This is taken from this blog.

(defun haozeke/clang-format-buffer-conditional ()
(interactive)
  "Reformat buffer if .clang-format exists in the projectile root."
  (when (f-exists? (expand-file-name ".clang-format" (projectile-project-root)))
    (+format/buffer)))

Org-mode export to Markdown

This is a convinience function for working with nanoc.

(defun haozeke/org-pandoc-markdown (dir &optional pargs)
  "A wrapper to generate yaml metadata markdown files. Takes the output
  directory followed by pandoc arguments"
  (if (not (file-exists-p dir)) (make-directory dir))
  (async-shell-command-no-window
   (concat "pandoc -f org -t markdown -s " pargs " " (buffer-name) " -o "
           dir "/" (file-name-sans-extension (buffer-name)) ".md"))
    )

Smartparens Wrapping

  • [ ] Make this conditional and only when +smartparens is active

This is to define some more wrapping functions I use often (for markdown and org-mode inline code):

(defun sp-wrap-backtick ()
  "Wrap following sexp in backticks."
  (interactive)
  (sp-wrap-with-pair "`"))
(defun sp-wrap-tilda ()
  "Wrap following sexp in tildes."
  (interactive)
  (sp-wrap-with-pair "~"))

Sort words

From the Emacs Wiki.

(defun sort-words (reverse beg end)
  "Sort words in region alphabetically, in REVERSE if negative.
Prefixed with negative \\[universal-argument], sorts in reverse.

The variable `sort-fold-case' determines whether alphabetic case
affects the sort order.

See `sort-regexp-fields'."
  (interactive "*P\nr")
  (sort-regexp-fields reverse "\\w+" "\\&" beg end))

doas helpers

doom-emacs comes with sudo support, these are simply clones with doas instead.

(defun doom--doas-file-path (file)
  (let ((host (or (file-remote-p file 'host) "localhost")))
    (concat "/" (when (file-remote-p file)
                  (concat (file-remote-p file 'method) ":"
                          (if-let (user (file-remote-p file 'user))
                              (concat user "@" host)
                            host)
                          "|"))
            "doas:root@" host
            ":" (or (file-remote-p file 'localname)
                    file))))

(defun doom/doas-find-file (file &optional arg)
  "Open FILE as root.

This will prompt you to save the current buffer, unless prefix ARG is given, in
which case it will save it without prompting."
  (interactive
   (list (read-file-name "Open file as root: ")
         current-prefix-arg))
  ;; HACK: Teach `save-place' to treat the new "remote" buffer as if it were
  ;;   visiting the same local file (because it is), and preserve the cursor
  ;;   position as usual.
  (letf! ((defun remote-local-name (path)
            (if path (or (file-remote-p path 'localname) path)))
          (defmacro with-local-name (&rest body)
            `(when save-place-mode
               (let ((buffer-file-name (remote-local-name buffer-file-name))
                     (default-directory (remote-local-name default-directory)))
                 ,@body))))
    (let ((window-start (window-start))
          (buffer (current-buffer)))
      (when (and buffer-file-name (file-equal-p buffer-file-name file))
        (when (buffer-modified-p)
          (save-some-buffers arg (lambda () (eq (current-buffer) buffer))))
        (with-local-name (save-place-to-alist)))
      (prog1
          ;; HACK: Disable auto-save in temporary tramp buffers because it could
          ;;   trigger processes that hang silently in the background, making
          ;;   those buffers inoperable for the rest of that session (Tramp
          ;;   caches them).
          (let ((auto-save-default nil)
                ;; REVIEW: use only these when we drop 28 support
                (remote-file-name-inhibit-auto-save t)
                (remote-file-name-inhibit-auto-save-visited t)
                ;; Prevent redundant work
                save-place-mode)
            (find-file (doom--doas-file-path (expand-file-name file))))
        ;; Record of the cursor's old position if it isn't at BOB (indicating
        ;; this buffer was already open), in case the user wishes to go to it.
        (unless (bobp)
          (doom-set-jump-h)
          ;; save-place-find-file-hook requires point be a BOB to do its thang.
          (goto-char (point-min)))
        (with-local-name (save-place-find-file-hook))
        (set-window-start nil window-start)))))
(defun doom/doas-this-file ()
  "Open the current file as root on Alpine Linux."
  (interactive)
  (doom/doas-find-file
   (or (buffer-file-name (buffer-base-buffer))
       (when (or (derived-mode-p 'dired-mode)
                 (derived-mode-p 'wdired-mode))
         default-directory)
       (user-error "Current buffer isn't visiting a file"))))

(defun doom/doas-save-buffer ()
  "Save this file as root on Alpine Linux."
  (interactive)
  (let ((file (doom--doas-file-path (buffer-file-name (buffer-base-buffer)))))
    (if-let (buffer (find-file-noselect file))
        (let ((origin (current-buffer)))
          (copy-to-buffer buffer (point-min) (point-max))
          (unwind-protect
              (with-current-buffer buffer
                (save-buffer))
            (unless (eq origin buffer)
              (kill-buffer buffer))
            (with-current-buffer origin
              (revert-buffer t t))))
      (user-error "Unable to open %S" file))))

Chat Clients

Matrix

Configuring the only emacs chat client I use.

;; (use-package! matrix-client
;;   :init
;;   :commands matrix-client-connect)

Projects

These are to help setup org-mode workflows.

; Make sure it's not set before adding to it
(unless (boundp 'org-publish-project-alist)
  (setq org-publish-project-alist nil))

dotDoom

This is used to generate plain HTML for my dotDoom repo. The setup is taken from the worg documentation and this repository. It so turns out that we can host the entire thing from the master branch on GitHub, but only if it is in a docs/ subfolder… Plus org-html-export-to-html does not accept filenames which was a real bummer.

; dotDoom stuff
; This is a rather harmless useful variable
(setq dotdoom-root-dir "~/.config/doom/")
(setq dotdoom-publish-dir  (concat dotdoom-root-dir "docs"))

Now that the variables are set, we can move on to actually setting up the rest of the export, this includes my own analytics and stuff. Infact maybe the analytics would be better handled by offloading the damn thing to Netlify, though their recent changes to the TOS are worrying, so Microsoft owned GitHub seems to be the better option for now.

Org Setup

It turns out that each part of the site which needs a separate publish function needs to be added to the org-publish-project-alist so we will define each rule.

(add-to-list 'org-publish-project-alist
      `("dotdoom-org"
         :base-directory ,dotdoom-root-dir
         :publishing-directory ,dotdoom-publish-dir
         :base-extension "org"
         :infojs-opt "view:t toc:t ltoc:t mouse:underline buttons:0 path:https://thomasf.github.io/solarized-css/org-info.min.js"
         :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://thomasf.github.io/solarized-css/solarized-dark.min.css\" />"
         :recursive t
         :publishing-function org-html-publish-to-html
         :auto-index nil ; I make my own from the readme.org
         ;; :html-head-include-default-style nil ; supresses the rest
         ;; :index-filename "README.org"
         ;; :index-title "index"
         ;; :auto-sitemap t                ; Generate sitemap.org automagically...
         ;; :sitemap-filename "index.org"  ; ... call it sitemap.org (it's the default)...
         ;; :sitemap-title "index"         ; ... with title 'sitemap'.
         :link-home "index.html"))

Static Content

We will at the very least need the .txt files to be transferred as is for keybase.

(add-to-list 'org-publish-project-alist
      `("dotdoom-static"
         :base-directory ,dotdoom-root-dir
         :publishing-directory ,dotdoom-publish-dir
         :base-extension "txt"
         :recursive nil
         :publishing-function org-publish-attachment))

Inherit and Combine

Now we compose the previous projects, keeping in mind the fact that they are in the LTR order of preference.

(add-to-list 'org-publish-project-alist
      `("dotdoom"
        :components ("dotdoom-org" "dotdoom-static")
        ))

Firestarter

Since I switched to using Nix for R I needed a way to reload my system-wide config.nix firestarter is the best of the auto-exec in my opinion, and would probably replace a lot of my other hooks eventually as well.

(use-package! firestarter
  :init
  (firestarter-mode)
  :config
  (setq firestarter-default-type t)
)

Hooks

Caveats

Move all the hooks to this section if possible.

Out of Focus Hook

Similar to neovim and its set autowrite and au FocusLost * update:

;; Save on focus lost (The frame's focus that is...)
;; (defun save-all ()
;;   (interactive)
;;   (save-some-buffers t))

;; (add-hook 'focus-out-hook 'save-all)

From here.

Before Save Hooks

CC Mode

Currently I only need to use the clang formatting hook here.

; The interactive thing is REQUIRED
(defun haozeke/clang-format-buffer-smart-on-save ()
(interactive)
  "Add auto-save hook for clang-format-buffer-smart."
  (add-hook 'before-save-hook 'haozeke/clang-format-buffer-conditional nil t))
; This is a doom-emacs convinience macro
(add-hook! (c-mode c++-mode cc-mode) #'haozeke/clang-format-buffer-smart-on-save)
(remove-hook! (c-mode c++-mode cc-mode) #'haozeke/clang-format-buffer-smart-on-save)

Disable Auto RDM

This conflicts with the ArchLinux systemctl --user start rdm thing.

; Do not automatically try to run rdm
(remove-hook 'c-mode-common-hook #'+cc|init-rtags)

Troubleshooting

These are strictly temporary hacks to resolve problems until they are fixed upstream.

(after! doom-themes
  (remove-hook 'doom-load-theme-hook #'doom-themes-treemacs-config))

Sphinx and RsT

As mentioned in packages.org, we have some packages which make life easier.

(use-package! ox-rst
:after org)
(use-package! sphinx-mode)

R Helpers

This section is essentially to configure working with R above and beyond the default ess configuration supplied by doom-emacs.

R Markdown

Basically only poly-markdown for rmd files.

;; Load
(use-package! poly-R
:config
(map! (:localleader
      :map polymode-mode-map
      :desc "Export"   "e" 'polymode-export
      :desc "Errors" "$" 'polymode-show-process-buffer
      :desc "Weave" "w" 'polymode-weave
      ;; (:prefix ("n" . "Navigation")
      ;;   :desc "Next" "n" . 'polymode-next-chunk
      ;;   :desc "Previous" "N" . 'polymode-previous-chunk)
      ;; (:prefix ("c" . "Chunks")
      ;;   :desc "Narrow" "n" . 'polymode-toggle-chunk-narrowing
      ;;   :desc "Kill" "k" . 'polymode-kill-chunk
      ;;   :desc "Mark-Extend" "m" . 'polymode-mark-or-extend-chunk)
      ))
  )

Rmd to Rorg

The idea is to replace md completely with org. Since polymode is pretty finicky for most of my org files, I will ensure it is only enabled for Rorg files.

(use-package! poly-org
:config
(add-to-list 'auto-mode-alist '("\\.org" . org-mode))
(add-to-list 'auto-mode-alist '("\\.Rorg" . poly-org-mode))
(map! (:localleader
      :map polymode-mode-map
      :desc "Export"   "E" 'polymode-export
      :desc "Errors" "$" 'polymode-show-process-buffer
      :desc "Weave" "w" 'polymode-weave
      ))
  )

Keybindings

(defun then_R_operator ()
  "R - %>% operator or 'then' pipe operator"
  (interactive)
  (just-one-space 1)
  (insert "%>%")
  (reindent-then-newline-and-indent))
(map! :leader
      :map (ess-mode-map, inferior-ess-mode-map)
      :desc "Insert pipe"  ">" 'then_R_operator)

Org LaTeX

Portions of this section are to be mirrored in the async init file since. That’s also why here it’s better to not use very doom specific code. I think it would be a lot better to just work these into a single literate block instead of maintaining two different sets of syntax.

Async Config

This is essentially the same, only some extra packages are added.

;;; autoExport.el --- For async exports -*- lexical-binding: t; -*-

(require 'package)
(setq package-enable-at-startup nil)
(package-initialize)

(require 'org)
(require 'ox)
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/org-mode/contrib/lisp/")
(require 'ox-koma-letter)
(require 'ox-beamer)

;; Org-Ref Stuff
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/org-ref/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/dash.el/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/helm.el/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/helm/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/build/helm/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/helm-bibtex/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/ivy/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/hydra/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/key-chord/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/s.el/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/f.el/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/pdf-tools/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/emacs-htmlize/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/parsebib/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/build/async/")
(add-to-list 'load-path "~/.emacs.d/.local/straight/repos/biblio.el/")
(require 'org-ref)

;; Path addtion
<<orgPaths>>

;; Functions
<<appList>>
;; Feature parity with doom
<<orgConf>>
(provide 'autoExport)
;;; autoExport.el ends here

Path Additions

Due to my recent switch to using tlmgr, I had to make some modifications to the emacs path.

(cond ((featurep :system 'linux)
       (setenv "PATH" (concat (getenv "PATH") ":" (concat (getenv "HOME") "/.local/share/texlive-20230827/bin/x86_64-linux")))
       (setq exec-path (append exec-path (list (concat (getenv "HOME") "/.local/share/texlive-20230827/bin/x86_64-linux"))))
      )
      ((featurep :system 'macos)
       (setenv "PATH" (concat (getenv "PATH") ":" (concat (getenv "HOME") "/usr/local/texlive/2021/bin/universal-darwin")))
       (setq exec-path (append exec-path (list (concat (getenv "HOME") "/usr/local/texlive/2021/bin/universal-darwin"))))
      )
)

Config

This is the part which is exported normally.

(eval-after-load 'ox '(require 'ox-koma-letter))
(with-eval-after-load 'ox-latex
  <<tex_process>>
  <<common_pkgs>>
  <<tufte_book>>
  <<koma_art>>
  <<koma_rprt>>
)

Shared Preferences

Compiler

It makes sense to use latexmk anyway. This way I can set sane defaults.

;; Compiler
(setq org-latex-pdf-process (list "latexmk -shell-escape -f -pdfxe %f"))

Packages

Some of these are damn near universal given my set up, so they are declared here.

;; Configuration
(add-to-list 'org-latex-packages-alist '("" "minted" "xcolor"))
(setq org-latex-listings 'minted)
(setq org-latex-minted-options
  '(("bgcolor" "white") ("breaklines" "true") ("linenos" "true") ("style" "tango")))

Export Templates

Most of the configuration is to be moved into the file snippets. However, class definitions and other packages are still to be loaded here. Though here in the config.el I could use doom semantics and might as well to keep things DRY, it appears that the async file needs to keep things in the old syntax.

KOMA Article

Inspired by the post here.

(add-to-list 'org-latex-classes
             '("koma-article" "\\documentclass{scrartcl}"
               ("\\section{%s}" . "\\section*{%s}")
               ("\\subsection{%s}" . "\\subsection*{%s}")
               ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
               ("\\paragraph{%s}" . "\\paragraph*{%s}")
               ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

KOMA Report

Inspired by the post here.

(add-to-list 'org-latex-classes
             '("koma-report" "\\documentclass{scrreprt}"))

Tufte Book

This is really ad-hoc right now and from this reddit thread.

(append-to-list
 'org-latex-classes
 '(("tufte-book"
    "\\documentclass[a4paper, sfsidenotes, openany, justified]{tufte-book}"
    ("\\part{%s}" . "\\part*{%s}")
    ("\\chapter{%s}" . "\\chapter*{%s}")
    ("\\section{%s}" . "\\section*{%s}")
    ("utf8" . "utf8x")
    ("\\subsection{%s}" . "\\subsection*{%s}"))))

LaTeX Preview for Org mode

Basically I need to see math and physics. Originally borrowed from this stackexchange question.

Process

'(org-preview-latex-process-alist
       (quote
       ((dvipng :programs
         ("lualatex" "dvipng")
         :description "dvi > png" :message "you need to install the programs: latex and dvipng." :image-input-type "dvi" :image-output-type "png" :image-size-adjust
         (1.0 . 1.0)
         :latex-compiler
         ("lualatex -output-format dvi -interaction nonstopmode -output-directory %o %f")
         :image-converter
         ("dvipng -fg %F -bg %B -D %D -T tight -o %O %f"))
 (dvisvgm :programs
          ("latex" "dvisvgm")
          :description "dvi > svg" :message "you need to install the programs: latex and dvisvgm." :use-xcolor t :image-input-type "xdv" :image-output-type "svg" :image-size-adjust
          (1.7 . 1.5)
          :latex-compiler
          ("xelatex -no-pdf -interaction nonstopmode -output-directory %o %f")
          :image-converter
          ("dvisvgm %f -n -b min -c %S -o %O"))
 (imagemagick :programs
              ("latex" "convert")
              :description "pdf > png" :message "you need to install the programs: latex and imagemagick." :use-xcolor t :image-input-type "pdf" :image-output-type "png" :image-size-adjust
              (1.0 . 1.0)
              :latex-compiler
              ("xelatex -no-pdf -interaction nonstopmode -output-directory %o %f")
              :image-converter
              ("convert -density %D -trim -antialias %f -quality 100 %O")))))

Packages

These are required to view math properly.

Math support

This is from this reddit thread.

(map! :map cdlatex-mode-map
    :i "TAB" #'cdlatex-tab)

Also we need some more things.

(use-package! cdlatex
    :after (:any org-mode LaTeX-mode)
    :hook
    ((LaTeX-mode . turn-on-cdlatex)
     (org-mode . turn-on-org-cdlatex)))

(use-package! company-math
    :after (:any org-mode TeX-mode)
    :config
    (set-company-backend! 'org-mode 'company-math-symbols-latex)
    (set-company-backend! 'TeX-mode 'company-math-symbols-latex)
    (set-company-backend! 'org-mode 'company-latex-commands)
    (set-company-backend! 'TeX-mode 'company-latex-commands)
    (setq company-tooltip-align-annotations t)
    (setq company-math-allow-latex-symbols-in-faces t))

We would also like to set up the math-symbol-list unicode input from here.

(use-package! math-symbol-lists
  :config
  (quail-define-package "math" "UTF-8" "Ω" t)
  (quail-define-rules ; add whatever extra rules you want to define here...
   ("\\from"    #X2190)
   ("\\to"      #X2192)
   ("\\lhd"     #X22B2)
   ("\\rhd"     #X22B3)
   ("\\unlhd"   #X22B4)
   ("\\unrhd"   #X22B5))
  (mapc (lambda (x)
          (if (cddr x)
              (quail-defrule (cadr x) (car (cddr x)))))
        (append math-symbol-list-basic math-symbol-list-extended math-symbol-list-subscripts math-symbol-list-superscripts)))

Prettier TeX buffers

From here. Reduces the size of inessential tex.

(defface endless/unimportant-latex-face
  '((t :height 0.7
       :inherit font-lock-comment-face))
  "Face used on less relevant math commands."
  :group 'LaTeX-math)

(setq font-latex-user-keyword-classes
      '(("mathunimportant"
         ("left" "right"
          "big" "Big"
          "bigl" "bigr"
          "Bigl" "Bigr"
          "biggl" "biggr"
          "Biggl" "Biggr"
          "," "." ";" "!")
         endless/unimportant-latex-face
         noarg)))

Babel Tabs

Evidently there was some sort of re-indentation going on during the export process which was breaking a lot of python, this should fix that: More generally, it is best set with # -*- org-src-preserve-indentation: t; org-edit-src-content: 0; -*- on a per-file basis, however given that the indentation is handled by the programming major mode, this is a good global setting as well.

(setq org-src-preserve-indentation t
      org-edit-src-content-indentation 0)

Pandoc Babel

As fully described in this post, I felt the need to export some common pandoc formats with babel.

Restructured Text

(defun org-babel-execute:rst (body params)
  "Execute a block of rst code with org-babel.
This function is called by `org-babel-execute-src-block'."
  (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
       (in-file (org-babel-temp-file "rst-"))
       (cmdline (cdr (assoc :cmdline params)))
       (to (cdr (assoc :to params)))
       (template (cdr (assoc :template params)))
       (cmd (concat "pandoc"
                    " -t  org"
                    " -i " (org-babel-process-file-name in-file)
                    " -f rst "
                    " " cmdline)))
    (with-temp-file in-file (insert body))
    (message cmd)
    (shell-command-to-string cmd))) ;; Send to results

(defun org-babel-prep-session:rst (session params)
  "Return an error because rst does not support sessions."
  (error "rst does not support sessions"))

Markdown HTML

A helper execution method mostly for better formatting with org-gcal, the method is described in this post.

(defun org-babel-execute:mdhtml (body params)
  "Execute a block of rst code with org-babel.
This function is called by `org-babel-execute-src-block'."
  (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
         (in-file (org-babel-temp-file "mdhtml-"))
         (cmdline (cdr (assoc :cmdline params)))
         (to (cdr (assoc :to params)))
         (template (cdr (assoc :template params)))
         (cmd (concat "pandoc"
                      " -t  html"
                      " -i " (org-babel-process-file-name in-file)
                      " -f gfm "
                      " " cmdline)))
    (with-temp-file in-file (insert body))
    (message cmd)
    (shell-command-to-string cmd))) ;; Send to results

(defun org-babel-prep-session:mdhtml (session params)
  "Return an error because mdhtml does not support sessions."
  (error "mdhtml does not support sessions"))

Completion

Github Copilot

;; accept completion from copilot and fallback to company
;; (use-package! copilot
;;   :hook (prog-mode . copilot-mode)
;;   :bind (:map copilot-completion-map
;;               ("<tab>" . 'copilot-accept-completion)
;;               ("TAB" . 'copilot-accept-completion)
;;               ("C-TAB" . 'copilot-accept-completion-by-word)
;;               ("C-<tab>" . 'copilot-accept-completion-by-word)))

Flycheck Additions

These are basically meant to aid in development. The relevant linters are also added here.

MELPA Helpers

This includes settings for both flycheck and the packages it needs.

(use-package! flycheck-package
  :after flycheck
  :config (flycheck-package-setup))

Xenops Inspired

Xenops is essentially a supercharged literate set of functions for using Mathematica and SymPy. It is similar to my previous mash et. al. setup but automatically appends // TeXForm // ToString and other helpful conversions. Due to the many modifications of my default environment and setup; these functions are adapted for use from xenops but cannot be loaded directly with it. Also my use case here is more focused towards only working in orgmode as opposed to xenops’s focus on TeX.

Notes

noteYoda

This is largely inspired from this reddit comment. For clarity and extensibility this will be broken down into a per-package configuration. The heart of this is an rclone mega folder to manage all these transparently. With this setup links to the files are stored in zotero and managed by zotfile. Now described in this post.

Org-Ref

This seems like an ubiquitous choice for working with org files and references, though quite a bit of the config here relates to helm-bibtex. Commented sections are set in my private config.

(use-package! org-ref
    :init
    (with-eval-after-load 'ox
      (defun my/org-ref-process-buffer--html (backend)
        "Preprocess `org-ref' citations to HTML format.

  Do this only if the export backend is `html' or a derivative of
  that."
        ;; `ox-hugo' is derived indirectly from `ox-html'.
        ;; ox-hugo <- ox-blackfriday <- ox-md <- ox-html
        (when (org-export-derived-backend-p backend 'html)
          (org-ref-process-buffer 'html)))
      (add-to-list 'org-export-before-parsing-hook #'my/org-ref-process-buffer--html))
    :config
    (setq
         org-ref-completion-library 'org-ref-ivy-cite
         org-ref-get-pdf-filename-function 'org-ref-get-pdf-filename-helm-bibtex
         bibtex-completion-bibliography (list (concat (getenv "HOME") "/GDrive/zotLib.bib"))
         bibtex-completion-notes-path (concat (getenv "HOME") "/Git/Gitlab/Mine/Notes/bibnotes.org")
         org-ref-note-title-format "* TODO %y - %t\n :PROPERTIES:\n  :Custom_ID: %k\n  :NOTER_DOCUMENT: %F\n :ROAM_KEY: cite:%k\n  :AUTHOR: %9a\n  :JOURNAL: %j\n  :YEAR: %y\n  :VOLUME: %v\n  :PAGES: %p\n  :DOI: %D\n  :URL: %U\n :END:\n\n"
         org-ref-notes-directory (concat (getenv "HOME") "/Git/Gitlab/Mine/Notes/")
         org-ref-notes-function 'orb-edit-notes
    ))

Apparently, org-ref is also able to fetch pdf files when DOI or URL links are dragged onto the .bib file. However, since zotero will handle the metadata, this remains to be considered.

Ivy is used exclusively throughout doom, makes sense to use it here too, but I recently switched to helm. Turns out helm is probably faster for larger collections since it can be asynchronous. Basically, this is because using the minibuffer, as ivy does is a blocking action while the helm buffer may be opened asynchronously. Name aside, helm-bibtex also works for ivy. Basically meant to interface with bibliographies in general. However, since I’m using org-ref, I won’t be configuring or loading that anymore.

Helm Bibtex

For some reason, org-ref-notes isn’t working very nicely, so the setup above prioritizes the helm-bibtex note-taking setup.

(after! org-ref
  (setq
   bibtex-completion-notes-path (concat (getenv "HOME") "/Git/Gitlab/Mine/Notes/")
   bibtex-completion-bibliography zot_bib
   bibtex-completion-pdf-field "file"
   bibtex-completion-notes-template-multiple-files
   (concat
    "#+TITLE: ${title}\n"
    "#+ROAM_KEY: cite:${=key=}\n"
    "#+ROAM_TAGS: ${keywords}\n"
    "* TODO Notes\n"
    ":PROPERTIES:\n"
    ":Custom_ID: ${=key=}\n"
    ":NOTER_DOCUMENT: %(orb-process-file-field \"${=key=}\")\n"
    ":AUTHOR: ${author-abbrev}\n"
    ":JOURNAL: ${journaltitle}\n"
    ":DATE: ${date}\n"
    ":YEAR: ${year}\n"
    ":DOI: ${doi}\n"
    ":URL: ${url}\n"
    ":END:\n\n"
    )
   )
)

Org-Roam

Will also setup the org-roam-bibtex thing here. As foretold in the last line, there are more settings for ORB. The template is modified from here.

 (use-package! org-roam-bibtex
  :after (org-roam)
  :hook (org-roam-mode . org-roam-bibtex-mode)
  :config
  (setq org-roam-bibtex-preformat-keywords
   '("=key=" "title" "url" "file" "author-or-editor" "keywords"))
  (setq orb-templates
        '(("r" "ref" plain (function org-roam-capture--get-point)
           ""
           :file-name "${slug}"
           :head "#+TITLE: ${=key=}: ${title}\n#+ROAM_KEY: ${ref}\n#+ROAM_TAGS: 

- keywords :: ${keywords}

\n* ${title}\n  :PROPERTIES:\n  :Custom_ID: ${=key=}\n  :URL: ${url}\n  :AUTHOR: ${author-or-editor}\n  :NOTER_DOCUMENT: %(orb-process-file-field \"${=key=}\")\n  :NOTER_PAGE: \n  :END:\n\n"

           :unnarrowed t))))

Org-Noter

I decided to use org-noter over the more commonly described interleave because it has better support for working with multiple documents linked to one file.

(use-package! org-noter
  :after (:any org pdf-view)
  :config
  (setq
   ;; The WM can handle splits
   org-noter-notes-window-location 'other-frame
   ;; Please stop opening frames
   org-noter-always-create-frame nil
   ;; I want to see the whole file
   org-noter-hide-other nil
   ;; Everything is relative to the rclone mega
   org-noter-notes-search-path (list org_notes)
   )
  )

I have a rather involved setup in mind, so I have spun this section off from the rest. The basic idea is to use ~deft~ for short-to-long lookup notes, and org-capture templates with org-protocol for the rest. I am also considering notdeft since it might work better for what I want to achieve. Though it isn’t really part of a note taking workflow, I also intend to use michel2 to sync my tasks…

Org Capture

I am not really sure how to use these correctly, but I have the bare minimum required for the Firefox browser extension (setup from here), and a random article thing.

Buffer Size

(set-popup-rule! "^CAPTURE-.*\\.org$" :size 0.5 :quit nil :select t :autosave t)

Functions

These are needed for org-capture alone for now.

;; Fix some link issues
(defun transform-square-brackets-to-round-ones(string-to-transform)
  "Transforms [ into ( and ] into ), other chars left unchanged."
  (concat
   (mapcar #'(lambda (c) (if (equal c ?\[) ?\( (if (equal c ?\]) ?\) c))) string-to-transform))
  )

Templates

This might get complicated but I am only trying to get the bare minimum for org-protocol right now. Will look into orca and doct.

;; Actually start using templates
(after! org-capture
  ;; Firefox
  (add-to-list 'org-capture-templates
               '("P" "Protocol" entry
                 (file+headline +org-capture-notes-file "Inbox")
                 "* %^{Title}\nSource: %u, %c\n #+BEGIN_QUOTE\n%i\n#+END_QUOTE\n\n\n%?"
                 :prepend t
                 :kill-buffer t))
  (add-to-list 'org-capture-templates
               '("L" "Protocol Link" entry
                 (file+headline +org-capture-notes-file "Inbox")
                 "* %? [[%:link][%(transform-square-brackets-to-round-ones \"%:description\")]]\n"
                 :prepend t
                 :kill-buffer t))
  ;; Misc
  (add-to-list 'org-capture-templates
         '("a"               ; key
           "Article"         ; name
           entry             ; type
           (file+headline +org-capture-notes-file "Article")  ; target
           "* %^{Title} %(org-set-tags)  :article: \n:PROPERTIES:\n:Created: %U\n:Linked: %a\n:END:\n%i\nBrief description:\n%?"  ; template
           :prepend t        ; properties
           :empty-lines 1    ; properties
           :created t        ; properties
           ))
)

HTML Parsing

The standard capture method isn’t too great, but this makes it better.

(use-package! org-protocol-capture-html
  :after org-protocol
  :config
  (add-to-list 'org-capture-templates
               '("w"
                 "Web site"
                 entry
                 (file+headline +org-capture-notes-file "Website")  ; target
                 "* %a :website:\n\n%U %?\n\n%:initial")
               )
  )
(setq org-roam-ref-capture-templates
        '(("r" "ref" plain (function org-roam--capture-get-point)
           "%?"
           :file-name "websites/${slug}"
           :head "#+SETUPFILE:./hugo_setup.org
#+ROAM_KEY: ${ref}
#+HUGO_SLUG: ${slug}
#+TITLE: ${title}