0

I'm looking to add some elisp to my init file that will execute a number of additional functions when I call a particular command.

More specifically, when I execute the compile and view command using AUCTeX I have added the following code to automatically revert-buffer on the PDF output buffer (suggested to me in another post on this forum).

(add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)

The problem is after the execution of this function the active window is the PDF output buffer rather than the buffer containing the LaTeX source file. I'm using the ACE-windows package for window navigation and I notice that within this package there is a noninteractive function for switching to the previous active window called aw-flip-window.

How can I edit my code so that I can add this function to the list of functions executed when I called the compile and view command?

**edit: the eventual solution utilised the majority of the code suggested in the reply highlighted as the solution with one alteration. Namely, that the native Ace Windows function aw-flip-window was replaced with PDFview major mode command as part of the PDFtools package pdf-outline as can be seen in the code below.

(defun my-TeX-revert-document-buffer (file)
  (TeX-revert-document-buffer file)
  (pdf-outline))
(add-hook 'TeX-after-compilation-finished-functions #'My-TeX-revert-document-buffer)

However I don't know why this particular solution works and I would be interested to hear any ideas why this may be the case.

  • Add a different function to the hook: a function that does what you want, using Tex-revert-* but that puts things back the way you want. – Drew Oct 12 '18 at 16:00
  • You mean adding a second function ('aw-flip-window') to the existing hook ('TeX-revert-document-buffer')? Im trying to experiment with it now but due to my zero experience with elisp I'm not getting very far beyond error messages. What would be the syntax to do what you're suggesting? – esc123 Oct 12 '18 at 16:11
  • No, I meant define your own function that does what you want, and that calls Tex-whatever, and put your function on the hook instead. Just a thought - no idea what's really needed. – Drew Oct 12 '18 at 17:55
2

You can add multiple functions to the same hook, and the order of execution is predictable, but it's not recommended to use that feature when the order of execution matters (see emacs documentation). It's better to define a new function.

Since the name of the hook variable doesn't end with -hooks it's an abnormal hook so you have to look at the documentation to see what are the required arguments for the function. In your case something like this

(defun my-TeX-revert-document-buffer (file)
  (TeX-revert-document-buffer file)
  (aw-flip-window))

should work if you add it to the hook

(add-hook 'TeX-after-compilation-finished-functions #'My-TeX-revert-document-buffer)

instead of

(add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)

(completely untested since I don't use ACE-window).

  • I put your code in my init file and used the eval-last-sexp command to evaluate it but it doesn't seem to work. It's not an interactive command so I cant use 'M-x' on it. So i wrote the the function name in the scratch buffer along with both file name and file path of the file I need to compile as a string with the argument with or without surrounding parens but it just returns errors regardless. Am I executing the function incorrectly? – esc123 Oct 14 '18 at 12:13
  • I forgot to the code to add the new function to the hook, see the updated answer. – matteol Oct 14 '18 at 16:19
  • Thanks, I see what you're doing now. Tried it out and the errors went away, the latex file compiled successfully and the PDF buffer was auto-reverted but it still did not switch the active window back to the latex file. I tried replacing the (aw-flip-window) function with the emacs standard function (other-window) in your custom function but still no luck. It seems it's executing the first command because it's auto-reverting the pdf but not the second command. – esc123 Oct 14 '18 at 19:48
  • Also, just to note, when i use the replaced (other-window) function the pdf is updated but as well as not switching the active window I get the error "error in process sentinel: Wrong number of arguments: (1 . 2), 0" – esc123 Oct 14 '18 at 19:51
  • @esc123 that's because other-window takes 1-2 arguments; not zero. (other-window) is an error. See C-h f other-window. Interactively, COUNT defaults to 1 (that being the default numeric prefix argument value); so you need to use (other-window 1) to replicate that. – phils Oct 14 '18 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.