Last active
June 5, 2023 08:54
-
-
Save karthink/4585277eea52a8b9a670d29362ea1695 to your computer and use it in GitHub Desktop.
Precompile Org-mode's LaTeX headers in the background.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Precompilation freezes emacs, do it in the background when possible. | |
(defun my/org-latex-preview-precompile-idle () | |
(when (featurep 'async) | |
(run-with-idle-timer | |
2 nil #'my/org-latex-preview-precompile-async | |
(current-buffer)))) | |
(defun my/org-latex-preview-precompile-async (&optional org-buf) | |
(when (buffer-live-p org-buf) | |
(with-current-buffer org-buf | |
(when org-latex-preview-precompile | |
(let* ((org-location (org-find-library-dir "org")) | |
(compiler-keywords | |
(org-collect-keywords | |
'("LATEX_PREVIEW_COMPILER" "LATEX_COMPILER") | |
'("LATEX_PREVIEW_COMPILER" "LATEX_COMPILER"))) | |
(compiler | |
(or (cdr (assoc "LATEX_PREVIEW_COMPILER" compiler-keywords)) | |
(and (boundp 'org-latex-preview-compiler) | |
org-latex-preview-compiler) | |
(cdr (assoc "LATEX_COMPILER" compiler-keywords)) | |
org-latex-compiler)) | |
(header (concat | |
(or org-latex-preview--preamble-content | |
(org-latex-preview--get-preamble)) | |
org-latex-preview--include-preview-string)) | |
(relative-file-p | |
(string-match-p "\\(?:\\\\input{\\|\\\\include{\\)[^/]" header)) | |
(remote-file-p (file-remote-p default-directory))) | |
(when (and (equal compiler "pdflatex") (not remote-file-p)) | |
(async-start | |
`(lambda () | |
(add-to-list 'load-path ,org-location) | |
(require 'ox) | |
(require 'org-latex-preview) | |
(org-latex--precompile | |
(list :latex-compiler ,compiler | |
:precompile-format-spec | |
(let ((org-tex-compiler | |
(cdr (assoc ,compiler org-latex-preview-compiler-command-map)))) | |
`((?l . ,org-tex-compiler) | |
(?L . ,(car (split-string org-tex-compiler)))))) | |
,header | |
(not ,relative-file-p))) | |
(lambda (result) | |
(let ((inhibit-message t)) | |
(org-persist--load-index) | |
(message "Precompiled in background: %S" | |
(file-name-base result))))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment