Globally advise TeX-master-{directory,file}

This commit is contained in:
Leni Aniva 2023-09-17 10:18:50 -07:00
parent ca3d679c54
commit 95932db545
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
1 changed files with 22 additions and 25 deletions

View File

@ -452,31 +452,29 @@ The target indentation style of ~LaTeX~ is as follows:
The ~SPC m c~ command which by default binds to ~TeX-command-run-all~ is
modified so it runs in the directory of the nearest ~.latexmkrc~ file or the
projectile root, whichever is closer. If a ~.latexmkrc~ file is not found, we
fall back to the current directory.
fall back to the current directory. We improve this below by decreeing that all
compilation has to happen with the ~.latexmkrc~ file's directory as PWD.
#+begin_src emacs-lisp
(defun custom/TeX-compile-in-root ()
"Execute ~TeX-command-run-all~ in the nearest ~.latexmkrc~ directory or
projectile project root."
(interactive)
(let*
((dir-latexmkrc
(projectile-locate-dominating-file default-directory ".latexmkrc"))
(dir-projectile (projectile-project-root))
(is-latexmk-child-of-projectile (string-match-p (regexp-quote dir-projectile) dir-latexmkrc))
(dir-work (cond
((and dir-latexmkrc is-latexmk-child-of-projectile) dir-latexmkrc)
(dir-projectile dir-projectile)
(t default-directory)))
(dir-current-rel (string-remove-prefix dir-work default-directory))
(file-current-rel (concat dir-current-rel (TeX-master-file))))
; Execute in shadowed ~default-directory~
(cl-labels ((replace-pwd () dir-work) (replace-file (&optional extension nondirectory ask) file-current-rel))
(advice-add 'TeX-master-directory :override #'replace-pwd)
(advice-add 'TeX-master-file :override #'replace-file)
(TeX-command-run-all nil)
(advice-remove 'TeX-master-file #'replace-file)
(advice-remove 'TeX-master-directory #'replace-pwd)
)))
(defun custom/TeX-root-directory (start)
(let* (
(dir-latexmkrc (projectile-locate-dominating-file start ".latexmkrc"))
(dir-projectile (projectile-project-root))
(is-latexmk-child-of-projectile (and dir-projectile dir-latexmkrc
(string-match-p (regexp-quote dir-projectile) dir-latexmkrc))))
(cond
((and dir-latexmkrc is-latexmk-child-of-projectile) dir-latexmkrc)
(dir-projectile dir-projectile)
(t start))))
(defun custom/TeX-master-directory-filter (dir-master)
(custom/TeX-root-directory dir-master))
(defun custom/TeX-master-file-filter (file-master)
(concat
(string-remove-prefix (custom/TeX-root-directory default-directory) default-directory)
file-master
))
(advice-add 'TeX-master-directory :filter-return #'custom/TeX-master-directory-filter)
(advice-add 'TeX-master-file :filter-return #'custom/TeX-master-file-filter)
#+end_src
@ -502,7 +500,6 @@ introduced.
:desc "View Compilation Log" "l" #'TeX-recenter-output-buffer
:desc "Next Error" "e" #'TeX-next-error
:desc "Close Environment" "]" #'LaTeX-close-environment
:desc "Compile in root" "." #'custom/TeX-compile-in-root
(:prefix ("f" . "Fold")
:desc "Fold Mode" "f" #'TeX-fold-mode
:desc "Environment" "e" #'TeX-fold-env