4

I sometimes use Makefiles with LaTeX documents, and so I'd like to define a command that does something similar to AUCTeX's C-c C-a (TeX-command-run-all) but using make.

I tried creating a function that calls both "Make" and "View", as suggested for Arara in this answer, but the "View" command never seems to be executed. I see a message that says "Compilation finished", but then the viewer does not open. Here's what I have in my init.el file:

(use-package tex
  :ensure auctex
  :mode ("\\.tex\\'" . TeX-latex-mode)
  :config
  (setq TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view)))
  (setq TeX-view-program-selection '((output-pdf "PDF Tools")))
  (add-hook 'TeX-after-compilation-finished-functions
            #'TeX-revert-document-buffer)
  (add-to-list
   'TeX-command-list
   '("Make"
     "make"
     TeX-run-compile
     nil
     t
     :help "Run make"))
  (defun adamliter-TeX-make ()
    "Interactive function for running GNU Make on a (La)TeX file."
    (interactive)
    (TeX-command-sequence '("Make" "View") t))
  :bind
  (:map TeX-mode-map
        ("C-c C-m" . adamliter-TeX-make)))

Moreover, how do I ensure that the document buffer is reverted? I have basically the same setup for Arara based on the linked answer, but when I run adamliter-TeX-arara, the document buffer does not seem to revert, despite having added TeX-revert-document-buffer to the hook TeX-after-compilation-finished-functions. Is this hook not run when commands are invoked via TeX-command-sequence?

Adam Liter
  • 143
  • 6

1 Answers1

2

TeX-run-compile runs the make process not in the current buffer but in the *compilation* buffer.

That is the reason why TeX-command-sequence does not find the running make process. It cannot set its sentinel for the make process. That sentinel would start the next command in the command sequence.

Use TeX-run-TeX instead of TeX-run-compile. With TeX-run-TeX the make process becomes the buffer process of the TeX buffer. That buffer is current in TeX-command-sequence.

That should also solve your problem with TeX-after-compilation-finished-functions.

Note, that I did not find a simple way to use TeX-run-compile. You cannot shift the buffer process to another buffer. One could advise get-buffer-process to return the process of TeX-command-buffer if the process of the current buffer is nil. But, then we get the problem that the sentinel also uses the process-buffer. So tweaking TeX-command-sequence for TeX-run-compile is a dead end.

Tobias
  • 32,569
  • 1
  • 34
  • 75
  • +1, thanks, this solves the first part of the question. Do you have any idea why `TeX-revert-document-buffer` is not run after a successful execution of `adamliter-TeX-make`, however? If I run `adamliter-TeX-make` with the PDF already open in a buffer, the changes will not show up unless I manually revert the buffer, despite having set `TeX-after-compilation-finished-functions` to include `TeX-revert-document-buffer`. – Adam Liter Jun 28 '19 at 18:08
  • @AdamLiter Do you have time for a debug session?: https://chat.stackexchange.com/rooms/95504/update-pdf-view-after-latex-compilation-https-emacs-stackexchange-com-questions – Tobias Jun 28 '19 at 18:37
  • Result of the debug session so far for others who want to investigate the issue: `pdf-view-revert-buffer` is always run after error-free compilation of the LaTeX file. But, it is ineffective if the LaTeX output is requested by `C-c C-l` during the compilation process. (a really strange effect) – Tobias Jun 28 '19 at 19:47
  • Thanks again for all you did to help. I figured out one further piece of information, which is that this only happens with pdf-tools. If I switch to using DocView, then this does not happen. I've opened a [bug report in the pdf-tools repository](https://github.com/politza/pdf-tools/issues/499). Also, since this seems like a bug, and you've answered the main part of the question, I've accepted your answer. Thanks again! – Adam Liter Jun 30 '19 at 14:22
  • It turns out that `TeX-revert-document-buffer` not working was a bug in `pdf-tools`, which has been [fixed](https://github.com/politza/pdf-tools/commit/db7de3901ae0e55f6ab8cf9baec257f706c3d16e). – Adam Liter Jul 01 '19 at 13:34
  • 1
    @AdamLiter Superb! Thank you for improving Emacs through persistency;-). – Tobias Jul 01 '19 at 13:36