"
(lambda () (interactive) (org-preview-latex-fragment 16)))
;; Put all the preview images in some directory
(setq org-preview-latex-image-directory "~/.ltximg/")
;; Define backends to preview LaTeX fragments
(setq org-preview-latex-process-alist '((imagemagick
:programs ("pdflatex" "convert")
:description "pdf > png"
:message "you need to install the programs: pdflatex and imagemagick."
:image-input-type "pdf"
:image-output-type "png"
:image-size-adjust (1.0 . 1.0)
:latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
:image-converter ("convert -density %D -trim -antialias %f -quality 100 %O"))
(dvipng
:programs ("latex" "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 (0.4 . 0.4)
:latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
:image-converter ("dvipng -D %D -T tight -o %O %f"))
(dvisvgm
:programs ("pdflatex" "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 (0.6 . 0.6)
:latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
:image-converter ("dvisvgm %f -n -b min -c %S -o %O"))
))
;; Use imagemagick/dvisvgm to generate png from pdf
(setq org-preview-latex-default-process 'dvisvgm)
;; Don't change the font size for subscripts and superscripts in latex fragments.
;; This cause the orgmode tables not to be well aligned.
(setq font-latex-fontify-script nil)
;; Colors of latex fragments
(setq org-format-latex-options (plist-put org-format-latex-options :foreground 'default))
(setq org-format-latex-options (plist-put org-format-latex-options :background (face-background 'fringe)))
)
#+end_src
*** LaTeX Classes
#+begin_src emacs-lisp
(after! org
;; Custom classes to use when exporting to latex
(add-to-list 'org-latex-classes
'("beamer"
,(concat "\\documentclass[presentation]{beamer}\n"
"[DEFAULT-PACKAGES]"
"[PACKAGES]"
"[EXTRA]\n")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
(add-to-list 'org-latex-classes
'("clean-cheatsheet"
"\\documentclass{clean-cheatsheet}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
)
(add-to-list 'org-latex-classes
'("clean-beamer"
"\\documentclass{clean-beamer}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
)
(add-to-list 'org-latex-classes
'("cleanreport"
"\\documentclass{cleanreport}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
)
(add-to-list 'org-latex-classes
'("scrreprt"
"\\documentclass{scrreprt}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
)
(add-to-list 'org-latex-classes
'("biblioreport"
"\\documentclass{biblioreport}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
)
)
#+end_src
*** Ox Latex Subfigure package
#+begin_src emacs-lisp
(use-package! ox-latex-subfigure
:after org
:config (require 'ox-latex-subfigure))
#+end_src
*** Clear page before heading
https://emacs.stackexchange.com/questions/30575/adding-latex-newpage-before-a-heading/30892
#+begin_src emacs-lisp
(after! org
(defun org/get-headline-string-element (headline backend info)
(let ((prop-point (next-property-change 0 headline)))
(if prop-point (plist-get (text-properties-at prop-point headline) :parent))))
(defun org/ensure-latex-clearpage (headline backend info)
(when (org-export-derived-backend-p backend 'latex)
(let ((elmnt (org/get-headline-string-element headline backend info)))
(when (and elmnt (org-element-property :CLEARPAGE elmnt))
(concat "\\clearpage\n" headline)))))
(add-to-list 'org-export-filter-headline-functions
'org/ensure-latex-clearpage)
)
#+end_src
*** Default added packages
#+begin_src emacs-lisp
(after! org
(add-to-list 'org-latex-packages-alist '("" "siunitx" t))
(add-to-list 'org-latex-packages-alist '("" "array" t))
(add-to-list 'org-latex-packages-alist '("" "tabularx" t))
(add-to-list 'org-latex-packages-alist '("" "booktabs" t))
(add-to-list 'org-latex-packages-alist '("" "bm" t))
(add-to-list 'org-latex-packages-alist '("most" "tcolorbox" t))
)
#+end_src
*** Some configurations
#+begin_src emacs-lisp
(after! org
;; Setup default option for image size when exporting to LaTeX
(setq org-latex-image-default-scale "")
(setq org-latex-image-default-width "")
(setq org-latex-image-default-height "")
(setq org-latex-image-default-option "")
;; Use define labels instead of automatic generated ones
(setq org-latex-prefer-user-labels t)
;; Captions above the table
(setq org-latex-caption-above '(table))
;; Settings to export code with `minted' instead of `verbatim'.
(setq org-latex-listings 'minted)
;; Command used when exporting to pdf
(setq org-latex-pdf-process
'("latexmk -cd -pdflatex=\"pdflatex -synctex=1 -shell-escape -interaction nonstopmode -output-directory %o\" -pdf -bibtex -f %f"))
)
#+end_src
*** Beamer
Bold Text
#+begin_src emacs-lisp
(after! org
(defun tdh-my-beamer-bold (contents backend info)
(when (eq backend 'beamer)
(replace-regexp-in-string
(concat "\\`\\\\" "[A-Za-z0-9]+") ;; If not, orgmode is crazy...
"\\\\textbf"
contents)))
(add-to-list 'org-export-filter-bold-functions 'tdh-my-beamer-bold)
)
#+end_src
Special Environments
- [ ] Make some comment those special environments
#+begin_src emacs-lisp
(after! org
(add-to-list 'org-beamer-environments-extra
'("cbox" ;; Name of environment
"m" ;; Selection key
"\\onslide%a{\\begin{cbox}[%h]%O"
"\\end{cbox}}\\vspace{0.5em}"))
(add-to-list 'org-beamer-environments-extra
'("csubbox" ;; Name of environment
"M" ;; Selection key
"\\onslide%a{\\tcbsubtitle{%h}"
"}"))
)
#+end_src
*** TODO Custom Export - Add Page and Label for LaTeX export
https://emacs.stackexchange.com/questions/156/emacs-function-to-convert-an-arbitrary-org-property-into-an-arbitrary-string-na?rq=1
#+begin_src emacs-lisp :tangle no
(defcustom tdh-org-property-mapping
'((latex ("CUSTOM_PAGE" . tdh-insert-org-page-latex)
("CUSTOM_LABEL" . tdh-insert-org-label-latex)))
"List of mappings from org property to arbitrary strings.
Each element is a list:
(BACKEND (PROPERTY1 . FUNCTION1) (PROPERTY2 . FUNCTION2) ...)
FUNCTION are functions which get called with a single
argument (the value of PROPERTY) and are responsible for doing
whatever should be done."
:type '(repeat (cons symbol (repeat (cons string string)))))
#+end_src
#+begin_src emacs-lisp :tangle no
(defun tdh-replace-org-property (backend)
"Convert org properties using `tdh-org-property-mapping'.
Lookup BACKEND in `tdh-org-property-mapping' for a list of
(PROPERTY REPLACEMENT). For each healine being exported, if it has a
PROPERTY listed insert a string immediately after the healine given by
(format REPLACEMENT PROPERTY-VALUE)"
(let ((map (cdr (assoc backend tdh-org-property-mapping)))
value replacement)
(when map
(org-map-entries
(lambda ()
(dolist (it map)
(save-excursion
(when (setq value (org-entry-get (point) (car it)))
(funcall (cdr it) value)))))))))
(add-hook 'org-export-before-processing-hook #'tdh-replace-org-property)
#+end_src
#+begin_src emacs-lisp :tangle no
(defun tdh-insert-org-label-latex (label)
"Insert \"\\\\label{LABEL}\\n\" after the :PROPERTY: drawer."
(search-forward-regexp org-property-end-re)
(forward-char 1)
(insert (format "\\label{%s}\n" label)))
(defun tdh-insert-org-page-latex (page)
"Insert \"\\\\page{PAGE}\\n\" after the :PROPERTY: drawer."
(search-forward-regexp org-property-end-re)
(forward-char 1)
(insert (format "\\page{%s}\n" page)))
#+end_src
#+begin_src emacs-lisp :tangle no
(defun org-latex-format-headline-default-function (todo _todo-type priority text tags _info)
"Default format function for a headline.
See `org-latex-format-headline-function' for details."
(concat
(and todo (format "{\\bfseries\\sffamily %s} " todo))
(and priority (format "\\framebox{\\#%c} " priority))
text
(and tags
(format "\\hfill{}\\textsc{%s}"
(mapconcat #'org-latex--protect-text tags ":")))
(and todo (format "{\n\\page{%s} " todo)))
#+end_src
*** Number Equations
#+begin_src emacs-lisp
(after! org
(defun org-renumber-environment (orig-func &rest args)
"A function to inject numbers in LaTeX fragment previews."
(let ((results '())
(counter -1)
(numberp))
(setq results (loop for (begin . env) in
(org-element-map (org-element-parse-buffer) 'latex-environment
(lambda (env)
(cons
(org-element-property :begin env)
(org-element-property :value env))))
collect
(cond
((and (string-match "\\\\begin{equation}" env)
(not (string-match "\\\\tag{" env)))
(incf counter)
(cons begin counter))
((string-match "\\\\begin{align}" env)
(prog2
(incf counter)
(cons begin counter)
(with-temp-buffer
(insert env)
(goto-char (point-min))
;; \\ is used for a new line. Each one leads to a number
(incf counter (count-matches "\\\\$"))
;; unless there are nonumbers.
(goto-char (point-min))
(decf counter (count-matches "\\nonumber")))))
(t
(cons begin nil)))))
(when (setq numberp (cdr (assoc (point) results)))
(setf (car args)
(concat
(format "\\setcounter{equation}{%s}\n" numberp)
(car args)))))
(apply orig-func args))
(advice-add 'org-create-formula-image :around #'org-renumber-environment)
)
#+end_src
** LaTeX macro both for LaTeX and HTML export
https://www.reddit.com/r/orgmode/comments/7u2n0h/tip_for_defining_latex_macros_for_use_in_both/
#+begin_src emacs-lisp
(after! org
(add-to-list 'org-src-lang-modes '("latex-macros" . latex))
(defvar org-babel-default-header-args:latex-macros
'((:results . "raw")
(:exports . "results")))
(defun prefix-all-lines (pre body)
(with-temp-buffer
(insert body)
(string-insert-rectangle (point-min) (point-max) pre)
(buffer-string)))
(defun org-babel-execute:latex-macros (body _params)
(concat
"\n#+HTML_HEAD_EXTRA: \\(\n"
(prefix-all-lines "#+HTML_HEAD_EXTRA: " body)
"\n#+HTML_HEAD_EXTRA: \\)
\n"))
)
#+end_src
** TODO Org Projects
#+begin_src emacs-lisp :tangle no
(setq org-publish-project-alist
'(("config"
:base-directory "~/.config/literate-dotfiles/dotfiles/"
:publishing-directory "~/.config/literate-dotfiles/docs/"
:recursive nil
:publishing-function org-html-publish-to-html
:auto-sitemap nil
:section-numbers nil
:table-of-contents nil)
("stewart-simscape"
:base-directory "~/Cloud/thesis/matlab/stewart-simscape/org/"
:base-extension "org"
:publishing-directory "~/Cloud/thesis/matlab/stewart-simscape/docs/"
:author "Dehaeze Thomas"
:email "dehaeze.thomas@gmail.com/"
:recursive nil
:publishing-function org-html-publish-to-html
:auto-preamble t
:auto-sitemap nil
:html-link-up "index.html"
:html-link-home "index.html"
:with-todo-keywords nil
:html-wrap-src-lines nil
:table-of-contents nil)
("nass-simscape"
:base-directory "~/Cloud/thesis/matlab/nass-simscape/org/"
:base-extension "org"
:publishing-directory "~/Cloud/thesis/matlab/nass-simscape/docs/"
:author "Dehaeze Thomas"
:email "dehaeze.thomas@gmail.com/"
:recursive nil
:publishing-function org-html-publish-to-html
:auto-preamble t
:auto-sitemap nil
:html-link-up "index.html"
:html-link-home "index.html"
:with-todo-keywords nil
:html-wrap-src-lines nil
:table-of-contents nil)
("tikz-maker"
:base-directory "~/Cloud/tikz/org/"
:base-extension "org"
:publishing-directory "~/Cloud/tikz/docs/"
:author "Dehaeze Thomas"
:email "dehaeze.thomas@gmail.com/"
:recursive nil
:publishing-function org-html-publish-to-html
:auto-preamble t
:auto-sitemap nil
:html-link-up "index.html"
:html-link-home "index.html"
:with-todo-keywords nil
:html-wrap-src-lines nil
:table-of-contents nil)))
#+end_src
** TODO Org big org-project
#+begin_src emacs-lisp
(setq org-publish-project-alist
'(("website"
:components ("nass-simscape" "nass-simscape-images"))
("nass-simscape"
:base-directory "~/Cloud/thesis/matlab/nass-simscape/org/"
:base-extension "org"
:publishing-directory "~/Cloud/website/nass-simscape/"
:publishing-function org-html-publish-to-html
:html-link-up "./index.html"
:html-link-home "~/Cloud/website/index.html"
:html-footnotes-section ""
:recursive nil
:auto-sitemap nil
:language en)
("nass-simscape-images"
:base-directory "~/Cloud/thesis/matlab/nass-simscape/docs/figs/"
:base-extension "png"
:publishing-directory "~/Cloud/website/nass-simscape/figs/"
:publishing-function org-publish-attachment)
))
#+end_src
** Automatically run =startblock= when opening org-mode files
#+begin_src emacs-lisp
(after! org
(defun tdh-eval-startblock ()
(if (member "startblock" (org-babel-src-block-names))
(save-excursion
(org-babel-goto-named-src-block "startblock")
(org-babel-execute-src-block))
nil
)
)
(add-hook 'org-mode-hook 'tdh-eval-startblock)
)
#+end_src
** TODO Insert ScreenShot or Picture from Phone
http://pragmaticemacs.com/emacs/a-workflow-to-quickly-add-photos-to-org-mode-notes/
- [ ] One function to move file from =~/Picture/= folder (where the screenshots are taken) to current directory and then insert and org link to the picture. Maybe ask if it should be copied in a sub directory (figs folder for instance).
- [ ] One function to copy file from =~/Cloud/Photos/= folder (where the pictures from phone are taken) to current directory (and ask for the new name of the picture) and insert org link.
#+begin_src emacs-lisp :tangle no
;; required libraries
(require 'dash)
;; (require 'swiper)
(require 's)
;; start directory
(defvar tdh-image-dir (expand-file-name "/home/thomas/Pictures"))
(defun tdh-insert-conference-image ()
"Insert image from conference directory, rename and add link in current file.
The file is taken from a start directory set by `tdh-image-dir' and moved to the current directory, renamed and embedded at the point as an org-mode link. The user is presented with a list of files in the start directory, from which to select the file to move, sorted by most recent first."
(interactive)
(let (file-list target-dir file-list-sorted start-file start-file-full file-ext end-file end-file-base end-file-full file-number)
;; Clean directories from list but keep times
(setq file-list
(-remove (lambda (x) (nth 1 x))
(directory-files-and-attributes tdh-image-dir)))
;; Get target directory
(setq target-dir (file-name-directory (buffer-file-name)))
;; Sort list by most recent
(setq file-list-sorted
(mapcar #'car
(sort file-list
#'(lambda (x y) (time-less-p (nth 6 y) (nth 6 x))))))
;; Use ivy to select start-file
(setq start-file (ivy-read
(concat "Move selected file to " target-dir ":")
file-list-sorted
:re-builder #'ivy--regex
:sort nil
:initial-input nil))
;; add full path to start file and end-file
(setq start-file-full
(expand-file-name start-file tdh-image-dir))
;; final file name including path
(setq end-file-full
(expand-file-name start-file target-dir))
;; rename file
(rename-file start-file-full end-file-full)
(message "moved %s to %s" start-file-full start-file)
;; insert link
(insert (org-make-link-string (format "file:%s" start-file)))
;; display image
(org-display-inline-images t t)))
#+end_src
** Render Tables
https://www.reddit.com/r/emacs/comments/d3a8or/pretty_org_tables_in_the_buffer_chapter_2_it/
#+begin_src emacs-lisp
(after! org
(defun tdh-render-org-table-at-point ()
(interactive)
(save-excursion
(beginning-of-line)
;; removes the overlay is already there
(if (overlays-at (point))
(delete-overlay (car (overlays-at (point))))
(let* ((element-type (org-element-type (org-element-at-point))))
(if (and (not (eq element-type 'table))
(not (eq element-type 'table-row)))
(error "not at an org table")
(while (not (eq 'table (org-element-type (org-element-at-point))))
(forward-line -1))
(tdh-render-org-table (org-element-at-point))
)))))
(defun tdh-render-org-table (table)
(interactive)
(let* ((begin (org-element-property :begin table))
(end (let ((pos (org-element-property :end table)))
(goto-char pos)
(beginning-of-line)
;; skip possible space after table
(while (not (looking-at " *[|#]"))
(setq pos (point))
(forward-line -1))
pos))
(tabletxt (buffer-substring-no-properties begin end))
(img (with-temp-buffer
(insert tabletxt)
(mark-whole-buffer)
(org-latex-convert-region-to-latex)
(org-latex-preview)
(goto-char (point-min))
(overlay-get (car (overlays-at (point))) 'display)))
(overlay (make-overlay begin end)))
(overlay-put overlay 'display img)
(forward-line -1))
)
(defun tdh-render-org-tables-in-buffer ()
(save-excursion
(org-element-map (org-element-parse-buffer) 'table 'tdh-render-org-table)))
;; Use F9 to globally generate tables
(map! :map org-mode-map :n "" (lambda () (interactive) (tdh-render-org-table-at-point)))
)
#+end_src
** Org Gcal
- https://cestlaz.github.io/posts/using-emacs-26-gcal/#.WIqBud9vGAk
#+begin_src emacs-lisp
(use-package! org-gcal
:after org
:init
:config
(setq org-gcal-client-id "396102378658-dcmbcmrnthbe925519otsjbd921otq0v.apps.googleusercontent.com"
org-gcal-client-secret "4M5PWrbhQjwYEMXGK85lDYX9"
org-gcal-file-alist '(("dehaeze.thomas@gmail.com" . "~/Cloud/org/gcal.org")
("8kjmhe2ar0abnm054ill1fb0gc@group.calendar.google.com" . "~/Cloud/org/gcal_phd.org")))
;; Automatic fetch of the new events
;; (add-hook 'org-agenda-mode-hook (lambda () (org-gcal-fetch) ))
)
#+end_src
** Custom Keybindings - =,= leader key
*** Insert Elements =,i=
Insert Link to paper
#+begin_src emacs-lisp
(defun tdh-insert-paper-org-link (paper)
"Insert an org link to some paper, choosing the file with completion"
(interactive
(list (read-file-name "Paper: " "~/Cloud/pdfs/" nil t)))
(insert (format "[[papers:%s]]" (file-name-base paper))))
#+end_src
Insert Link to notes
#+begin_src emacs-lisp
(defun tdh-insert-note-org-link (note)
"Insert an org link to some note, choosing the file with completion"
(interactive
(list (read-file-name "Note: " "~/Cloud/pdfs/" nil t)))
(insert (format "[[notes:%s]]" (file-name-base note))))
#+end_src
Insert Image that is in the figs folder
#+begin_src emacs-lisp
(defun tdh-insert-image-org-link (img)
"Insert an org image link, choosing the file with completion
and starting from `my-default-image-directory'."
(interactive
(list (file-relative-name (read-file-name "Image: " (concat default-directory "figs/")) default-directory)))
(insert (format "[[file:%s]]" img)))
#+end_src
Insert Image that is in the =figs= folder using SXIV
#+begin_src emacs-lisp
(defun tdh-insert-image-org-link-sxiv ()
"Insert an org image link, choosing the file with completion
and starting from `my-default-image-directory'."
(interactive)
(setq img (shell-command-to-string "ls figs/*.{jpg,jpeg,bmp,png,gif} 2> /dev/null | sxiv -i -t -o | tail -1 | tr -d '\n'"))
(unless (equal "" img)
(insert (format "[[file:%s]]" img)))
)
#+end_src
Copy picture from phone folder using SXIV and insert it
#+begin_src emacs-lisp
(defun tdh-insert-phone-picture ()
(interactive)
(setq img (shell-command-to-string "~/.config/doom/bin/copy-phone-picture.sh"))
(unless (equal "" img)
(insert (format "[[file:%s]]" img)))
)
#+end_src
Bash script for copying pictures taken by phone.
#+begin_src bash :comments both :mkdirp yes :shebang "#!/usr/bin/env bash" :tangle ~/.config/doom/bin/copy-phone-picture.sh
if [ -z "$1" ]; then
oldpath=$(ls -t ~/Cloud/photos/phone/*.jpg | sxiv -i -t -o | tail -1);
else
oldpath=$(ls -t $1 | sxiv -i -t -o | tail -1);
fi
if [ -n "$oldpath" ]; then
newfilename=$(basename $oldpath .jpg | rofi -i -dmenu -p "Filename")
if [ -n "$newfilename" ]; then
cp $oldpath "figs/$newfilename.jpg"
printf "figs/$newfilename.jpg"
fi
fi
#+end_src
Take Screenshot and insert a link:
- Ask for a name =screenshot_name=
- use =maim -s figs/screenshot_name.png= to take a screenshot with selection
- Then insert the following to the buffer
#+begin_src emacs-lisp
(defun tdh-insert-screenshot-org-link ()
"Capture screenshot and insert the resulting file.
The screenshot tool is determined by `org-download-screenshot-method'."
(interactive)
(setq filename (concat "./figs/" (read-string "Enter file name:") ".png"))
(shell-command (concat "maim -s " filename))
(insert (format "[[file:%s]]" filename))
)
#+end_src
Insert link to next figure:
#+begin_src emacs-lisp
(defun tdh-insert-link-to-next-figure ()
(interactive)
(save-excursion
(re-search-forward "^#\\+name:\s*\\(fig:.*\\)" nil t 1))
(insert (concat "[[" (match-string 1) "]]"))
)
#+end_src
Map Keys
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",i" . "Insert")
:n "p" 'tdh-insert-paper-org-link
:n "n" 'tdh-insert-note-org-link
:n "f" 'tdh-insert-image-org-link
:n "F" 'tdh-insert-image-org-link-sxiv
:n "i" 'tdh-insert-phone-picture
:n "l" 'tdh-insert-link-to-next-figure
:n "s" 'tdh-insert-screenshot-org-link)))
#+end_src
*** LaTeX =,l=
#+begin_src emacs-lisp
(defun tdh-latex-watch ()
"Watch LaTeX file using latexmk"
(interactive)
(start-process-shell-command "latexmk-watch" "*latexmk-watch-output*"
"latexmk" (format "-pdflatex=\"xelatex -synctex=1 -shell-escape -interaction nonstopmode -output-directory='%s'\" -pdf -pvc -bibtex -f %s.tex"
(file-name-directory buffer-file-name)
(file-name-base buffer-file-name))))
#+end_src
#+begin_src emacs-lisp
(defun tdh-latex-watch-kill ()
"Kill the currently running TeX job."
(interactive)
(delete-process "latexmk-watch")
)
#+end_src
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",l" . "LaTeX")
:n "w" 'tdh-latex-watch
:n "k" 'tdh-latex-watch-kill
:n "l" 'org-latex-export-to-latex)))
#+end_src
*** Org LaTeX Automatic fragment
#+begin_src emacs-lisp
(defun tdh-automatic-latex-fragment-activate ()
(interactive)
(add-hook 'org-mode-hook 'org-fragtog-mode))
(defun tdh-automatic-latex-fragment-deactivate ()
(interactive)
(remove-hook 'org-mode-hook 'org-fragtog-mode))
#+end_src
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",l" . "LaTeX")
:n "f" 'tdh-automatic-latex-fragment-activate
:n "F" 'tdh-automatic-latex-fragment-deactivate)))
#+end_src
*** Bibtex =,r=
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",r" . "References")
:n "b" 'helm-bibtex
:n "B" 'helm-bibtex-with-local-bibliography
:n "f" 'helm-bibtex-favorites
:n "r" 'helm-resume)))
#+end_src
*** Open ranger in current directory =,o=
#+begin_src emacs-lisp
(defun tdh-open-ranger-in-workdir ()
(interactive)
(call-process-shell-command
(concat "termite --directory=" default-directory " --exec=ranger") nil 0))
#+end_src
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
:n ",o" 'tdh-open-ranger-in-workdir))
#+end_src
*** View in External programs =,v=
Open PDF output with =zathura=
#+begin_src emacs-lisp
(defun tdh-open-org-pdf-externally ()
(interactive)
(call-process "zathura" nil 0 nil (concat (file-name-sans-extension (buffer-file-name)) ".pdf"))
)
#+end_src
Open HTML output externally
#+begin_src emacs-lisp
(defun tdh-open-org-html-externally ()
(interactive)
(call-process "xdg-open" nil 0 nil (concat (file-name-sans-extension (buffer-file-name)) ".html"))
)
#+end_src
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",v" . "View")
:n "p" 'tdh-open-org-pdf-externally
:n "h" 'tdh-open-org-html-externally)))
#+end_src
* Org Babel
** Main configuration
Don't ask for confirmation when evaluating following blocs
#+begin_src emacs-lisp
(defun tdh-org-confirm-babel-evaluate (lang body)
(not (member lang '("emacs-lisp" "latex" "matlab" "sh" "latex-macros" "python"))))
(after! org
(setq org-confirm-babel-evaluate 'tdh-org-confirm-babel-evaluate))
#+end_src
Use the current window for C-c ' source editing
#+begin_src emacs-lisp
(after! org
(setq org-src-window-setup 'current-window))
#+end_src
** Appearance of source blocks
#+begin_src emacs-lisp
(defun tdh-org-prettify-symbols ()
(mapc (apply-partially 'add-to-list 'prettify-symbols-alist)
(cl-reduce 'append
(mapcar (lambda (x) (list x (cons (upcase (car x)) (cdr x))))
`(("#+begin_src" . ?✎)
("#+end_src" . ?□)
("#+begin_quote" . ?«)
("#+end_quote" . ?»)))))
(turn-on-prettify-symbols-mode))
(add-hook 'org-mode-hook #'tdh-org-prettify-symbols)
#+end_src
** Indentation
#+begin_src emacs-lisp
(after! org
(setq org-edit-src-content-indentation 2
org-src-tab-acts-natively nil
org-src-preserve-indentation nil)
)
#+end_src
** Library of Babel
Add all named source blocks to =org-babel-library-of-babel= ([[file:emacs-library-babel.org][link]]).
#+begin_src emacs-lisp
(after! org
(org-babel-lob-ingest "~/.config/literate-dotfiles/dotfiles/emacs-library-babel.org"))
#+end_src
** Org-Babel Matlab
#+begin_src emacs-lisp
(after! org
(setq org-babel-matlab-shell-command "/home/thomas/.local/bin/matlab -softwareopengl -nodesktop -nosplash")
(setq org-babel-matlab-emacs-link-wrapper-method
"%s
if ischar(ans);
echo('test');
fid = fopen('%s', 'w');
fprintf(fid, '%s', ans);
fclose(fid);
else;
save -ascii %s ans;
end
delete('%s');
")
)
#+end_src
Default options for Matlab code
#+begin_src emacs-lisp
(after! org
(setq org-babel-default-header-args:matlab
'((:results . "none")
(:session . "*MATLAB*")
(:comments . "org")
(:exports . "both")
(:cache . "no")
(:noweb . "no")
(:hlines . "no")
(:tangle . "yes")
(:mkdir . "yes")
(:eval . "no-export")))
)
#+end_src
** Some functions for using Matlab with Org Babel =,m=
=whos= matlab function
#+begin_src emacs-lisp
(defun tdh-matlab-whos (&optional start end)
"Get what is in the Matlab workspace"
(interactive)
(if (use-region-p)
(let ((regionp (buffer-substring (region-beginning) (region-end))))
(process-send-string "*MATLAB*" (concat "whosEmacs " regionp "\n")))
(process-send-string "*MATLAB*" (concat "whosEmacs" "\n"))))
#+end_src
=help= matlab function
#+begin_src emacs-lisp
(defun tdh-matlab-help (&optional start end)
"Get help on the selected function"
(interactive)
(if (use-region-p)
(let ((regionp (buffer-substring (region-beginning) (region-end))))
(process-send-string "*MATLAB*" (concat "help " regionp "\n")))
(process-send-string "*MATLAB*" (concat "help " (read-string "Matlab help:") "\n")))
)
#+end_src
Specify a Matlab command to run
#+begin_src emacs-lisp
(defun tdh-matlab-run-command ()
"Prompt user to enter a matlab command"
(interactive)
(process-send-string "*MATLAB*" (concat (read-string "Matlab Command: ") "\n")))
#+end_src
Specify a Matlab command to run and show output in mini-buffer
#+begin_src emacs-lisp
(defun tdh-matlab-run-command-show-output ()
"Prompt user to enter a matlab command"
(interactive)
(process-send-string "*MATLAB*" (concat "evalEmacs('" (read-string "Matlab Command: ") "')\n")))
#+end_src
Org-Babel Tangle File and Execute with Matlab
#+begin_src emacs-lisp
(defun tdh-matlab-tangle-and-execute ()
"Jump to tangle file for the source block at point."
(interactive)
(let (file org-babel-pre-tangle-hook org-babel-post-tangle-hook)
(cl-letf (((symbol-function 'write-region) (lambda (start end filename &rest _ignore)
(setq file filename)))
((symbol-function 'delete-file) #'ignore))
(org-babel-tangle '(4)))
(when file
(setq file (expand-file-name file))
(if (file-readable-p file)
(process-send-string "*MATLAB*" (concat "run " file "\n"))
(error "Cannot open tangle file %S" file)))))
#+end_src
Map Functions
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",m" . "Matlab")
:n "e" 'tdh-matlab-run-command
:n "E" 'tdh-matlab-run-command-show-output
:n "T" 'tdh-matlab-tangle-and-execute
:nv "h" 'tdh-matlab-help
:nv "w" 'tdh-matlab-whos)))
#+end_src
** Remap =ctrl-ret= used to execute the src block and go to the next one
https://emacs.stackexchange.com/questions/13869/how-to-toggle-org-mode-source-code-block-eval-no-status
Remap =ctrl-ret= to execute the source block and go to the next source block
when inside a source block. Otherwise, keep the normal behavior for =ctrl-ret=.
#+begin_src emacs-lisp
(defun tdh-ctrl-ret ()
(interactive)
(defun in-src-block-p ()
"Returns t when the point is inside a source code block"
(string= "src" (org-in-block-p '("src"))))
(if (in-src-block-p)
(progn
(org-babel-execute-src-block)
(org-babel-next-src-block))
(+org--insert-item 'below)))
#+end_src
#+begin_src emacs-lisp
(map! :after evil-org
:map evil-org-mode-map
:n "" #'tdh-ctrl-ret)
#+end_src
** Remap =ctrl-shift-ret= used to execute the (matlab) src block in the background and go to the next one
*** =tdh-org-babel-execute-matlab-background=
#+begin_src emacs-lisp
(defun tdh-org-babel-execute-matlab-background (&optional arg info params)
(interactive)
(let* ((org-babel-current-src-block-location
(or org-babel-current-src-block-location
(nth 5 info)
(org-babel-where-is-src-block-head)))
(info (if info (copy-tree info) (org-babel-get-src-block-info))))
;; Merge PARAMS with INFO before considering source block
;; evaluation since both could disagree.
(cl-callf org-babel-merge-params (nth 2 info) params)
(when (org-babel-check-evaluate info)
(cl-callf org-babel-process-params (nth 2 info))
(let* ((params (nth 2 info))
(cache (let ((c (cdr (assq :cache params))))
(and (not arg) c (string= "yes" c))))
(new-hash (and cache (org-babel-sha1-hash info :eval)))
(old-hash (and cache (org-babel-current-result-hash)))
(current-cache (and new-hash (equal new-hash old-hash))))
(cond
(current-cache
(save-excursion ;Return cached result.
(goto-char (org-babel-where-is-src-block-result nil info))
(forward-line)
(skip-chars-forward " \t")
(let ((result (org-babel-read-result)))
(message (replace-regexp-in-string "%" "%%" (format "%S" result)))
result)))
((org-babel-confirm-evaluate info)
(let* ((lang (nth 0 info))
(result-params (cdr (assq :result-params params)))
;; Expand noweb references in BODY and remove any
;; coderef.
(body
(let ((coderef (nth 6 info))
(expand
(if (org-babel-noweb-p params :eval)
(org-babel-expand-noweb-references info)
(nth 1 info))))
(if (not coderef) expand
(replace-regexp-in-string
(org-src-coderef-regexp coderef) "" expand nil nil 1))))
(dir (cdr (assq :dir params)))
(mkdirp (cdr (assq :mkdirp params)))
(default-directory
(cond
((not dir) default-directory)
((member mkdirp '("no" "nil" nil))
(file-name-as-directory (expand-file-name dir)))
(t
(let ((d (file-name-as-directory (expand-file-name dir))))
(make-directory d 'parents)
d))))
(cmd (intern (concat "org-babel-execute:" lang)))
result)
(process-send-string "*MATLAB*" (concat body "\n"))
result))
)))
)
)
#+end_src
*** =tdh-matlab-execute-selected=
#+begin_src emacs-lisp
(defun tdh-matlab-execute-selected (start end)
"Execute selected text in the *MATLAB* buffer"
(interactive "r")
(let ((regionp (buffer-substring start end)))
(process-send-string "*MATLAB*" (concat regionp "\n"))))
#+end_src
*** Remap =ctrl-shift-ref=
This function:
- first check if inside a source block, if not does nothing
- when check if the language is =matlab=
- if it is not, it just runs the code and go to the next source block
- if it is in a =matlab= block, it first check if a region if selected, if so it just runs the selected region.
if no region is selected, it runs all the code blocks and goes to the next block
#+begin_src emacs-lisp
(defun tdh-ctrl-shift-ret ()
(interactive)
(defun in-src-block-p ()
"Returns t when the point is inside a source code block"
(string= "src" (org-in-block-p '("src"))))
(if (in-src-block-p)
(let ((lang (nth 0 (org-babel-get-src-block-info))))
(if (string= lang "matlab")
(if (region-active-p)
(tdh-matlab-execute-selected (region-beginning) (region-end))
(progn (tdh-org-babel-execute-matlab-background)
(org-babel-next-src-block)))
(tdh-ctrl-ret))
)
)
)
#+end_src
#+begin_src emacs-lisp
(map! :after evil-org
:map evil-org-mode-map
:n "" #'tdh-ctrl-shift-ret)
#+end_src
** Align Source Blocks
#+begin_src emacs-lisp
(defun tdh-align-src-block ()
(interactive)
(defun in-src-block-p ()
"Returns t when the point is inside a source code block"
(string= "src" (org-in-block-p '("src"))))
(if (in-src-block-p)
(progn
(org-edit-special)
(evil-indent (point-min) (point-max))
(org-edit-src-exit))
(org-table-eval-formula)))
#+end_src
#+begin_src emacs-lisp
(map! :after evil-org
:map evil-org-mode-map
:n "C-c =" #'tdh-align-src-block)
#+end_src
** Helping Functions - Tangling =,b=
Org-Babel Tangle Sub-tree
#+begin_src emacs-lisp
(defun tdh-org-babel-tangle-subtree ()
"Tangle the current subtree"
(interactive)
(progn
(org-narrow-to-subtree)
(org-babel-tangle)
(widen))
)
#+end_src
Org-Tangle and Org-Babel Jump to Tangle File
#+begin_src emacs-lisp
(defun tdh-org-babel-jump-to-tangle-file ()
"Jump to tangle file for the source block at point."
(interactive)
(let (file org-babel-pre-tangle-hook org-babel-post-tangle-hook)
(cl-letf (((symbol-function 'write-region) (lambda (start end filename &rest _ignore)
(setq file filename)))
((symbol-function 'delete-file) #'ignore))
(org-babel-tangle '(4)))
(when file
(setq file (expand-file-name file))
(if (file-readable-p file)
(find-file file)
(error "Cannot open tangle file %S" file)))))
#+end_src
Map Functions
#+begin_src emacs-lisp
(after! org
(map! :map org-mode-map
(:prefix (",b" . "Tangle")
:n "F" 'tdh-org-babel-jump-to-tangle-file
:n "T" 'tdh-org-babel-tangle-subtree)))
#+end_src
* Bibliography Management
My bibliography management is mainly based on the following packages:
- =org-ref= for nice citations
- =org-noter= to annotate documents
- =org-roam= to manage and links all my notes
- =helm-bibtex= as an interface to easily find references
- =org-roam-bibtex= that connects all the above packages
** Org Ref ([[https://github.com/jkitchin/org-ref][link]])
Nice Functions:
- =org-ref-insert-ref-link=
- =org-ref-helm-insert-cite-link=
- =org-ref-list-of-figures=
- =org-ref-find-bad-citations=
- =org-ref-clean-bibtex-entry=
#+begin_src emacs-lisp
(use-package! org-ref
:after org
:init
:config
;; Folder where the notes files are located (or file if just one Note file)
(setq org-ref-notes-directory "~/Cloud/brain")
(setq org-ref-bibliography-notes "~/Cloud/brain")
;; Bibliography File
(setq reftex-default-bibliography '("~/Cloud/brain/references.bib"))
(setq org-ref-default-bibliography '("~/Cloud/brain/references.bib"))
;; Folder where all the pdf are located
(setq org-ref-pdf-directory "~/Cloud/pdfs")
;; Tell org-ref to let helm-bibtex find notes for it
(setq org-ref-notes-function
(lambda (thekey)
(let ((bibtex-completion-bibliography (org-ref-find-bibliography)))
(bibtex-completion-edit-notes
(list (car (org-ref-get-bibtex-key-and-file thekey)))))))
;; Problem with speed: don't display broken links
(setq org-ref-show-broken-links t)
;; Display information on the citation
(setq org-ref-show-citation-on-enter t)
(add-to-list 'org-ref-helm-user-candidates
'("Open pdf in Zathura" . (lambda () (call-process "zathura" nil 0 nil (concat
(file-name-as-directory org-ref-pdf-directory)
(car (org-ref-get-bibtex-key-and-file))
".pdf"))))
t)
(add-to-list 'org-ref-helm-user-candidates
'("Drag and Drop" . (lambda () (call-process "/bin/bash" nil 0 nil "-c" (concat
"dragon-drag-and-drop "
(file-name-as-directory org-ref-pdf-directory)
(car (org-ref-get-bibtex-key-and-file))
".pdf"))))
t)
(map!
:map org-mode-map
(:desc "Insert Link"
:ni "C-c i" #'org-ref-insert-ref-link))
)
#+end_src
#+begin_src emacs-lisp
(defun tdh-org-ref-open-pdf-at-point ()
"Open the pdf in external program for bibtex key under point if it exists."
(interactive)
(let* ((results (org-ref-get-bibtex-key-and-file))
(key (car results))
(pdf-file (funcall org-ref-get-pdf-filename-function key)))
(if (file-exists-p pdf-file)
(call-process "zathura" nil 0 nil pdf-file)
(message "no pdf found for %s" key))))
#+end_src
** Org Noter ([[https://github.com/weirdNox/org-noter][link]])
#+begin_src emacs-lisp
(use-package! org-noter
:defer t
:after (:any org pdf-view)
:config
(setq org-noter-always-create-frame nil)
(setq org-noter-kill-frame-at-session-end nil)
;; Fraction of the frame that the document window will occupy when split
(setq org-noter-doc-split-fraction '(0.6 . 0.6))
;; Save the last visited location automatically; when starting a new session, go to that location
(setq org-noter-auto-save-last-location nil)
;; Add an empty line between each note's heading and content
(setq org-noter-separate-notes-from-heading t)
;; List of paths to check (non recursively) when searching for a notes file
(setq org-noter-notes-search-path "~/Cloud/brain")
(defun org-noter-init-pdf-view ()
(pdf-view-fit-page-to-window)
(pdf-view-auto-slice-minor-mode)
(run-at-time "0.5 sec" nil #'org-noter))
(add-hook 'pdf-view-mode-hook 'org-noter-init-pdf-view)
(map!
:map pdf-view-mode-map
(:desc "Insert Note"
:n "i" #'org-noter-insert-note))
)
#+end_src
** Org Roam ([[https://github.com/jethrokuan/org-roam/][link]])
#+begin_src emacs-lisp
(use-package! org-roam
:custom-face
(org-roam-link ((t (:inherit org-link :foreground "#cc241d"))))
:config
(setq org-roam-directory "~/Cloud/brain/")
(setq org-roam-completion-system 'helm)
(setq org-roam-capture-templates
`(("d" "default" plain (function org-roam--capture-get-point)
"%?"
:file-name "${slug}"
:head ,(concat "#+TITLE: ${title}\n"
"#+SETUPFILE ./org-roam-setup-file.org\n"
"\n"
"- Tags ::\n"
"\n")
:unnarrowed t)))
)
#+end_src
#+begin_src emacs-lisp
(defun my/org-roam--backlinks-list (file)
(if (org-roam--org-roam-file-p file)
(--reduce-from
(concat acc (format "- [[file:%s][%s]]\n"
(file-relative-name (car it) org-roam-directory)
(org-roam--get-title-or-slug (car it))))
"" (org-roam-db-query [:select [from] :from links :where (= to $s1)] file))
""))
(defun my/org-export-preprocessor (backend)
(let ((links (my/org-roam--backlinks-list (buffer-file-name))))
(unless (string= links "")
(save-excursion
(goto-char (point-max))
(insert (concat "\n* Backlinks\n") links)))))
(add-hook 'org-export-before-processing-hook 'my/org-export-preprocessor)
#+end_src
** Helm-Bibtex ([[https://github.com/tmalsburg/helm-bibtex][link]])
#+begin_src emacs-lisp
(use-package! helm-bibtex
:init
:config
;; Bibliography file
(setq bibtex-completion-bibliography "~/Cloud/brain/references.bib")
;; Directory with all the pdfs
(setq bibtex-completion-library-path "~/Cloud/pdfs")
;; Directory with notes files
(setq bibtex-completion-notes-path "~/Cloud/brain/")
(setq bibtex-completion-notes-extension ".org")
(setq bibtex-completion-pdf-extension '(".pdf" ".djvu"))
;; Use "tags" field when looking for bib entries
(setq helm-bibtex-additional-search-fields '(tags))
(setq helm-bibtex-full-frame nil)
;; Display of bibtex entries with helm
(setq bibtex-completion-display-formats
'((t . "${author:36} ${title:*} ${year:4} ${=type=:7} ${=has-note=:1}")))
;; Special symbols for notes and pdf
(setq bibtex-completion-pdf-symbol "⌘")
(setq bibtex-completion-notes-symbol "✎")
;; Template used when creating new Note file
(setq bibtex-completion-notes-template-multiple-files (concat "#+TITLE: ${title}\n"
"#+NOTER_DOCUMENT: ../pdfs/${=key=}.pdf\n"
"#+ROAM_KEY: cite:${=key=}\n"
"\n"
"- Author :: ${author}\n"
"- Year :: ${year}\n"
"- DOI :: ${DOI}\n"
"\n"))
;; Make "Edit notes" the default action
(helm-delete-action-from-source "Edit notes" helm-source-bibtex)
(helm-add-action-to-source "Edit notes" 'helm-bibtex-edit-notes helm-source-bibtex 0)
(helm-delete-action-from-source "Open PDF Externally" helm-source-bibtex)
(helm-add-action-to-source "Open PDF Externally" 'tdehaeze/open-pdf-externally helm-source-bibtex 1)
)
#+end_src
Open pdf externally
#+begin_src emacs-lisp
(defun tdehaeze/open-pdf-externally (key)
(call-process "zathura" nil 0 nil (nth 0 (-cons-to-list (bibtex-completion-find-pdf key)))))
#+end_src
Special Commands
#+begin_src emacs-lisp
(defun helm-bibtex-favorites (&optional arg)
"Search Favorite BibTeX entries"
(interactive "P")
(helm-bibtex arg nil "favorite "))
#+end_src
List all element of the bibliography without pdf associated
#+begin_src emacs-lisp
(defun tdh-list-bib-without-pdf-associated ()
(interactive)
(bibtex-completion-init)
(setq candidates (bibtex-completion-candidates))
(defun canditate-is-pdf-present (candidate)
(bibtex-completion-find-pdf-in-library (cdr (assoc "=key=" candidate)))
)
(setq candidates-without-pdf (remove-if #'canditate-is-pdf-present candidates))
(setq candidate-without-pdf-names (mapcar
(lambda (x) (cdr (assoc "title" x)))
candidates-without-pdf))
(with-output-to-temp-buffer "*bib-without-pdf*" (princ (string-join candidate-without-pdf-names "\n")))
(switch-to-buffer-other-window "*bib-without-pdf*")
)
#+end_src
** Org-Roam-Bibtex ([[https://github.com/Zaeph/org-roam-bibtex][link]])
#+begin_src emacs-lisp
(use-package! org-roam-bibtex
:load-path "~/.config/doom/packages/org-roam-bibtex/"
:config
(org-roam-bibtex-mode)
(setq org-roam-bibtex-preformat-keywords `("=key=" "title" "author" "year" "author-abbrev" "=type="))
(setq org-roam-bibtex-template
`(("r" "ref" plain (function org-roam-capture--get-point) ""
:file-name "${=key=}"
:head ,(concat "#+TITLE: ${title}\n"
"#+SETUPFILE ./org-notes-setup-file.org\n"
"#+ROAM_KEY: ${ref}\n"
"\n"
"- Tags :: %?\n"
"- Reference :: ${ref}\n"
"- Author(s) :: ${author}\n"
"- Year :: ${year}\n"
"\n"
"* ${author-abbrev} (${year}): ${title} :${=type=}:ignore:\n"
":PROPERTIES:\n"
":NOTER_DOCUMENT: ../pdfs/${=key=}.pdf\n"
":END:\n")
:unnarrowed t)))
)
#+end_src
* LaTeX
- https://tex.stackexchange.com/questions/52179/what-is-your-favorite-emacs-and-or-auctex-command-trick
- https://tex.stackexchange.com/questions/20843/useful-shortcuts-or-key-bindings-or-predefined-commands-for-emacsauctex
** Master file
#+begin_src emacs-lisp
(setq-default TeX-master nil)
#+end_src
** Clean Output Files
#+begin_src emacs-lisp
(defun latex/clean ()
"Clean LaTeX output using latexmk"
(interactive)
(async-shell-command
;; command and parameters
"latexmk -c "
(shell-quote-argument buffer-file-name)
" &"
))
(map! :map LaTeX-mode-map
:n ",C" 'latex/clean)
(add-hook 'TeX-mode-hook #'TeX-fold-mode)
#+end_src
** PDF Viewer
#+begin_src emacs-lisp
(setq TeX-view-program-selection '((output-pdf "Zathura")))
(setq TeX-source-correlate-mode t)
(setq TeX-source-correlate-start-server t)
(setq TeX-source-correlate-method 'synctex)
(setq TeX-view-program-list
'(("PDF Tools" TeX-pdf-tools-sync-view)))
#+end_src
** Bibtex
#+begin_src emacs-lisp
(use-package! bibtex
:config
(bibtex-set-dialect 'BibTeX))
#+end_src
* Matlab
- https://sourceforge.net/projects/matlab-emacs/
** Setup Matlab Mode
#+begin_src emacs-lisp
(setq matlab-shell-command "/home/thomas/.local/bin/matlab")
(setq matlab-shell-command-switches (list "-nodesktop -nosplash"))
(setq mlint-programs '("mlint" "/home/thomas/.local/bin/mlint"))
#+end_src
** Setup Flycheck to work with =mlint=
#+begin_src emacs-lisp
(defvar mlint-executable "/home/thomas/.local/bin/mlint")
(flycheck-define-command-checker 'matlab-mlint
"A Matlab checker based on mlint."
:command `(,mlint-executable source)
:error-patterns
'((warning line-start "L " line " (C " (1+ digit) "): " (message) line-end))
:modes '(matlab-mode))
(add-to-list 'flycheck-checkers 'matlab-mlint)
;; Automatic startup of flycheck for matlab
(add-hook 'matlab-mode-hook 'flycheck-mode)
#+end_src
** Completion in the Matlab Shell
#+begin_src emacs-lisp
(map! :map matlab-shell-mode-map
:i "" 'matlab-shell-tab)
#+end_src
** Beautify code
#+begin_src emacs-lisp
(defun tdh-matlab-beautify-buffer ()
"Beautify Current Matlab Buffer"
(interactive)
;; First verifies is the current file is a Matlab file
(if (string= (file-name-extension (buffer-file-name)) "m")
(progn
(save-buffer)
(matlab-shell-run-command (concat "MBeautify.formatFileNoEditor(\"" (buffer-file-name) "\", \"" (buffer-file-name) "\")"))
(revert-buffer :ignore-auto :noconfirm))
(message "Current buffer is not a matlab file")
)
)
#+end_src
** Key Bindings
#+begin_src emacs-lisp
(defun matlab-add-breakpoint ()
(interactive)
(matlab-shell-run-command (concat "dbstop in " (buffer-name) " at " (number-to-string (line-number-at-pos nil)))))
(defun matlab-remove-breakpoint ()
(interactive)
(matlab-shell-run-command (concat "dbclear in " (buffer-name) " at " (number-to-string (line-number-at-pos nil)))))
(defun matlab-list-breakpoints ()
(interactive)
(matlab-shell-run-command (concat "dbstatus " (buffer-name))))
(defun matlab-clear-breakpoints ()
(interactive)
(matlab-shell-run-command (concat "dbclear in " (buffer-name))))
(defun matlab-go-to-file-directory ()
(interactive)
(matlab-shell-run-command (concat "cd " (file-name-directory buffer-file-name))))
#+end_src
#+begin_src emacs-lisp
(map! :map matlab-mode-map
:n ",g" 'matlab-go-to-file-directory
:n ",da" 'matlab-add-breakpoint
:n ",dr" 'matlab-remove-breakpoint
:n ",dL" 'matlab-list-breakpoints
:n ",dc" 'matlab-clear-breakpoints
:n ",dl" 'gud-cont
:n ",ds" 'gud-step
:n ",dn" 'gud-next
:n ",dq" 'gud-finish)
#+end_src
* Mu4e
** Resources
Documentation:
- [ ] Mu4e documentation: http://www.djcbsoftware.nl/code/mu/mu4e/index.html#Top
- [ ] https://github.com/djcb/mu
Use contexts for multiple accounts:
- [ ] https://vxlabs.com/2017/02/07/mu4e-0-9-18-e-mailing-with-emacs-now-even-better/
- [ ] http://cachestocaches.com/2017/3/complete-guide-email-emacs-using-mu-and-/
- [ ] https://notanumber.io/2016-10-03/better-email-with-mu4e/
Send math and source code:
- [ ] https://vxlabs.com/2015/01/28/sending-emails-with-math-and-source-code/
** Cheatsheet
| Command | Usage |
|---------+-----------------------|
| =C-j= | Next mail |
| =C-k= | Previous mail |
| =R/C/F= | Reply/Compose/Forward |
| =t= | Move to Archive |
| =d= | Move to Trash |
** Helping function
This is a helper to help determine which account context I am in based on the folder in my maildir the email (eg. ~/.mail/nine27) is located in.
#+begin_src emacs-lisp
(defun mu4e-message-maildir-matches (msg rx)
(when rx
(if (listp rx)
;; If rx is a list, try each one for a match
(or (mu4e-message-maildir-matches msg (car rx))
(mu4e-message-maildir-matches msg (cdr rx)))
;; Not a list, check rx
(string-match rx (mu4e-message-field msg :maildir)))))
#+end_src
Choose account label to feed msmtp -a option based on From header in Message buffer; This function must be added to message-send-mail-hook for on-the-fly change of From address before sending message since message-send-mail-hook is processed right before sending message.
#+begin_src emacs-lisp
(defun choose-msmtp-account ()
(if (message-mail-p)
(save-excursion
(let*
((from (save-restriction
(message-narrow-to-headers)
(message-fetch-field "from")))
(account
(cond
((string-match "dehaeze.thomas@gmail.com" from) "gmail")
((string-match "thomas.dehaeze@esrf.fr" from) "esrf"))))
(setq message-sendmail-extra-arguments (list '"-a" account))))))
#+end_src
** Basic Config
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq mail-user-agent 'mu4e-user-agent
mu4e-mu-binary "/usr/bin/mu"
mu4e-maildir "~/.mail"
mu4e-compose-format-flowed t
mu4e-compose-in-new-frame nil
mu4e-view-show-images t
mu4e-html2text-command "w3m -dump -T text/html"
mu4e-use-fancy-chars t
mu4e-headers-include-related t
mu4e-attachment-dir "~/Downloads"
message-kill-buffer-on-exit t
mu4e-compose-signature-auto-include t
mu4e-view-show-images t
mu4e-view-show-addresses t)
)
#+end_src
** Additional config
#+begin_src emacs-lisp
(use-package! mu4e
:config
;; Use imagemagick, if available.
(when (fboundp 'imagemagick-register-types)
(imagemagick-register-types))
;; Sometimes html email is just not readable in a text based client, this lets me open the
;; email in my browser.
(add-to-list 'mu4e-view-actions '("View in browser" . mu4e-action-view-in-browser) t)
;; Spell checking ftw.
(add-hook 'mu4e-compose-mode-hook 'flyspell-mode)
;; Use Helm to select mailboxes
(setq mu4e-completing-read-function 'completing-read)
;; Don't ask for a 'context' upon opening mu4e
(setq mu4e-context-policy 'pick-first)
;; Don't ask to quit... why is this the default?
(setq mu4e-confirm-quit nil)
)
#+end_src
** Provide Information
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq mu4e-user-mail-address-list '("dehaeze.thomas@gmail.com" "thomas.dehaeze@esrf.fr")
mu4e-compose-signature "Thomas Dehaeze\n"
user-mail-address "dehaeze.thomas@gmail.com")
;; Default Folders
(setq mu4e-sent-folder "/gmail/Sent"
mu4e-drafts-folder "/gmail/Drafts"
mu4e-trash-folder "/gmail/Trash"
mu4e-refile-folder "/gmail/Archive")
)
#+end_src
** Receiving emails using mbsync
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq mu4e-get-mail-command "checkmail"
mu4e-update-interval nil
mu4e-change-filenames-when-moving t) ;; Fix for mbsync
)
#+end_src
** Contexts
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq mu4e-contexts
`( ,(make-mu4e-context
:name "gmail"
:enter-func (lambda () (mu4e-message "Switch to the gmail context"))
:match-func (lambda (msg) (when msg
(string-prefix-p "/gmail" (mu4e-message-field msg :maildir))))
:leave-func (lambda () (mu4e-clear-caches))
:vars '(
(user-mail-address . "dehaeze.thomas@gmail.com")
(user-full-name . "Thomas Dehaeze")
(mu4e-sent-folder . "/gmail/Sent")
(mu4e-trash-folder . "/gmail/Trash")
(mu4e-drafts-folder . "/gmail/Drafts")
(mu4e-refile-folder . "/gmail/Archive")
(mu4e-compose-signature . "Thomas Dehaeze\n")
))
,(make-mu4e-context
:name "esrf"
:enter-func (lambda () (mu4e-message "Switch to the esrf context"))
:match-func (lambda (msg) (when msg
(string-prefix-p "/esrf" (mu4e-message-field msg :maildir))))
:leave-func (lambda () (mu4e-clear-caches))
:vars '(
(user-mail-address . "thomas.dehaeze@esrf.fr")
(user-full-name . "Thomas Dehaeze")
(mu4e-sent-folder . "/esrf/Sent")
(mu4e-trash-folder . "/esrf/Trash")
(mu4e-drafts-folder . "/esrf/Drafts")
(mu4e-refile-folder . "/esrf/Archive")
(mu4e-compose-signature . "Thomas Dehaeze\n")
))
))
)
#+end_src
** Sending mails
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq message-send-mail-function 'message-send-mail-with-sendmail
sendmail-program "/usr/bin/msmtp"
user-full-name "Thomas Dehaeze")
;; This prevents saving the email to the Sent folder since gmail will do this for us on their end.
(setq mu4e-sent-messages-behavior 'delete)
;; Use the correct account context when sending mail based on the from header.
(setq message-sendmail-envelope-from 'header)
(add-hook 'message-send-mail-hook 'choose-msmtp-account)
)
#+end_src
** Bookmarks
#+begin_src emacs-lisp
(use-package! mu4e
:config
(setq mu4e-bookmarks `(,(make-mu4e-bookmark
:name "All Inboxes"
:query "maildir:/gmail/Inbox OR maildir:/esrf/Inbox"
:key ?i)
("flag:unread" "Unread messages" ?u)
("date:today..now" "Today's messages" ?t)
("date:7d..now" "Last 7 days" ?w)))
)
#+end_src
* Doom =init.el=
:PROPERTIES:
:header-args: :tangle ~/.config/doom/init.el
:END:
#+begin_src emacs-lisp
(when noninteractive
(after! undo-tree
(global-undo-tree-mode -1)))
(doom! :completion
company ; the ultimate code completion backend
helm ; the *other* search engine for love and life
ivy ; a search engine for love and life
:ui
doom ; what makes DOOM look the way it does
hl-todo ; highlight TODO/FIXME/NOTE/DEPRECATED/HACK/REVIEW
hydra
modeline ; snazzy, Atom-inspired modeline, plus API
nav-flash ; blink the current line after jumping
ophints ; highlight the region an operation acts on
(popup ; tame sudden yet inevitable temporary windows
+all ; catch all popups that start with an asterix
+defaults) ; default popup rules
unicode ; extended unicode support for various languages
vc-gutter ; vcs diff in the fringe
vi-tilde-fringe ; fringe tildes to mark beyond EOB
window-select ; visually switch windows
workspaces ; tab emulation, persistence & separate workspaces
:editor
(evil +everywhere); come to the dark side, we have cookies
fold ; (nigh) universal code folding
rotate-text ; cycle region at point between text candidates
snippets ; my elves. They type so I don't have to
word-wrap ; soft wrapping with language-aware indent
:emacs
(dired +icons) ; making dired pretty [functional]
electric ; smarter, keyword-based electric-indent
ibuffer ; interactive buffer management
vc ; version-control and Emacs, sitting in a tree
:term
eshell ; a consistent, cross-platform shell (WIP)
vterm
:tools
debugger ; Stepping through code, to help you add bugs
(eval +overlay) ; run code, run (also, repls)
(lookup ; helps you navigate your code and documentation
+docsets) ; ...or in Dash docsets locally
lsp
magit ; a git porcelain for Emacs
;;pass ; password manager for nerds
pdf ; pdf enhancements
eval
biblio
:checkers
syntax ; tasing you for every semicolon you forget
spell ; tasing you for misspelling mispelling
:lang
data ; config/data formats
emacs-lisp ; drown in parentheses
go
(javascript
+lsp)
latex ; writing papers in Emacs has never been so fun
;; markdown ; writing docs for people to ignore
(org ; organize your plain life in plain text
+dragndrop ; drag & drop files/images into org buffers
+hugo ; use Emacs for hugo blogging
+roam ;
+present) ; using org-mode for presentations
python ; beautiful is better than ugly
(sh ; she sells {ba,z,fi}sh shells on the C xor
+lsp)
(web
+lsp)
:email
(mu4e +gmail)
:app
calendar
;;(rss +org) ; emacs as an RSS reader
;;write ; emacs for writers (fiction, notes, papers, etc.)
:config
literate
(default +bindings)
)
#+end_src
* Doom =packages.el=
:PROPERTIES:
:header-args: :tangle ~/.config/doom/packages.el
:END:
#+begin_src emacs-lisp
(package! poet-theme)
(package! org-alert)
(package! org-gcal)
(package! ox-latex-subfigure
:recipe (:host github :repo "linktohack/ox-latex-subfigure"))
(package! matlab-mode
:recipe (:host github :repo "matlab-mode/mirror"))
(package! org-ref)
(package! org-ql)
(package! org-fancy-priorities)
#+end_src