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 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 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 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 #+begin_src emacs-lisp
(defun custom/TeX-compile-in-root () (defun custom/TeX-root-directory (start)
"Execute ~TeX-command-run-all~ in the nearest ~.latexmkrc~ directory or (let* (
projectile project root." (dir-latexmkrc (projectile-locate-dominating-file start ".latexmkrc"))
(interactive)
(let*
((dir-latexmkrc
(projectile-locate-dominating-file default-directory ".latexmkrc"))
(dir-projectile (projectile-project-root)) (dir-projectile (projectile-project-root))
(is-latexmk-child-of-projectile (string-match-p (regexp-quote dir-projectile) dir-latexmkrc)) (is-latexmk-child-of-projectile (and dir-projectile dir-latexmkrc
(dir-work (cond (string-match-p (regexp-quote dir-projectile) dir-latexmkrc))))
(cond
((and dir-latexmkrc is-latexmk-child-of-projectile) dir-latexmkrc) ((and dir-latexmkrc is-latexmk-child-of-projectile) dir-latexmkrc)
(dir-projectile dir-projectile) (dir-projectile dir-projectile)
(t default-directory))) (t start))))
(dir-current-rel (string-remove-prefix dir-work default-directory)) (defun custom/TeX-master-directory-filter (dir-master)
(file-current-rel (concat dir-current-rel (TeX-master-file)))) (custom/TeX-root-directory dir-master))
; Execute in shadowed ~default-directory~ (defun custom/TeX-master-file-filter (file-master)
(cl-labels ((replace-pwd () dir-work) (replace-file (&optional extension nondirectory ask) file-current-rel)) (concat
(advice-add 'TeX-master-directory :override #'replace-pwd) (string-remove-prefix (custom/TeX-root-directory default-directory) default-directory)
(advice-add 'TeX-master-file :override #'replace-file) file-master
(TeX-command-run-all nil) ))
(advice-remove 'TeX-master-file #'replace-file)
(advice-remove 'TeX-master-directory #'replace-pwd) (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 #+end_src
@ -502,7 +500,6 @@ introduced.
:desc "View Compilation Log" "l" #'TeX-recenter-output-buffer :desc "View Compilation Log" "l" #'TeX-recenter-output-buffer
:desc "Next Error" "e" #'TeX-next-error :desc "Next Error" "e" #'TeX-next-error
:desc "Close Environment" "]" #'LaTeX-close-environment :desc "Close Environment" "]" #'LaTeX-close-environment
:desc "Compile in root" "." #'custom/TeX-compile-in-root
(:prefix ("f" . "Fold") (:prefix ("f" . "Fold")
:desc "Fold Mode" "f" #'TeX-fold-mode :desc "Fold Mode" "f" #'TeX-fold-mode
:desc "Environment" "e" #'TeX-fold-env :desc "Environment" "e" #'TeX-fold-env