7
votes

I use the function TeX-parse-error defined by Ivan Andrus at the bottom of Emacs latexmk function throws me into an empty buffer in order to automatically open the *TeX Help* buffer when there was an error during the compilation (C-c C-c). After correcting an error and compiling again, the *TeX Help* buffer remains open (although the error has been corrected). Is there any way to adjust the function (unfortunately, I'm not experienced in elisp programming) so that the *TeX Help* buffer is closed if the error was resolved and updated (and still open) if the error wasn't resolved? That would save a lot of typing like C-c ' to show the *TeX Help* buffer and C-x 1 to hide it again.

2

2 Answers

6
votes

First, let's define a function that finds the *TeX Help* buffer, if it exists, closes its window, and then kills the buffer:

(defun demolish-tex-help ()
  (interactive)
  (if (get-buffer "*TeX Help*") ;; Tests if the buffer exists
      (progn ;; Do the following commands in sequence
        (if (get-buffer-window (get-buffer "*TeX Help*")) ;; Tests if the window exists
            (delete-window (get-buffer-window (get-buffer "*TeX Help*")))
          ) ;; That should close the window
        (kill-buffer "*TeX Help*") ;; This should kill the buffer
        )
    )
  )

Now, you have to call this when you call whatever function it is that you use to compile. Taking the example from that other page, you can modify Ivan Andrus's function to be:

(defun run-latexmk ()
  (interactive)
  (let ((TeX-save-query nil)
        (TeX-process-asynchronous nil)
        (master-file (TeX-master-file)))
    (TeX-save-document "")
    (TeX-run-TeX "latexmk"
                 (TeX-command-expand "latexmk %t" 'TeX-master-file)
                 master-file)
    (if (plist-get TeX-error-report-switches (intern master-file))
        (TeX-next-error t)
      (progn
       (demolish-tex-help)
       (minibuffer-message "latexmk done")))))

(add-hook 'LaTeX-mode-hook
          (lambda () (local-set-key (kbd "C-0") #'run-latexmk)))

(Note: This doesn't actually work for me, because my latexmk is screwed up, so I haven't successfully tested it. But if Ivan's version worked for you, then this should too.)

So now, any time you call latexmk with this function (by hitting C-0, for example), once the compilation is done, it checks for errors. If there were errors, it automatically opens the Help window and gets the first error. If there were none, it checks to see if the Help buffer is open; if so, it closes that window and kills the buffer.

1
votes

Note: After some clarification, I see that the following is not quite what was asked for. My other answer to this question should do the job.

It looks like automatically opening the buffer requires changing the function that's called when you run latex. (At least, I can't find any hook that will let you do it.) That's actually one of the things that's done in the earlier answer to that question you mention. In particular, the lines

    (if (plist-get TeX-error-report-switches (intern master-file))
    (TeX-next-error t)
  (minibuffer-message "latexmk done"))))

check to see if there was an error. If so, the function runs TeX-next-error; if not it just prints the message. So you might want to try installing that function.

Automatically closing the Help buffer just requires a little adjustment to Ivan Andrus's function. In the area around "No more errors", just replace the relevant part with this:

     ((null
       (re-search-forward regexp nil t))
      ;; No more errors.
      (message "No more errors.")
      (beep)
  (delete-window (get-buffer-window (get-buffer "*TeX Help*")))
  (kill-buffer "*TeX Help*")
      (TeX-pop-to-buffer old)
      nil)

This actually kills the Help buffer after closing its window, but you can comment that out if you dislike it for some reason.

Another nice option is to put (setq TeX-display-help nil) in your ~/.emacs. With this, the Help buffer never even displays. Instead, a terse message describing the error is put into the minibuffer, and the cursor is put on the bad line in the original. (I never find most of that help output useful anyway.)

Finally, you might want to try another keybinding for TeX-next-error because C-c ' is awkward.