# How to recenter the *compilation* buffer (on C-c C-x)? No process for this document

I have a .tex/.Rnw document which is compiled with a makefile. During the compilation, I would like to use C-c C-l (as for standard .tex files) to recenter the output buffer so that I can follow it. However, this gives me (Emacs 24, Ubuntu 12.10): No process for this document. I'd guess this is because there is no association with LaTeX anymore (make could do anything). The output buffer is called *compilation*, so if one could define C-c C-l to recenter this output buffer, that would solve it.

-

## migrated from stackoverflow.comJan 17 '13 at 6:04

This question came from our site for professional and enthusiast programmers.

Okay, I looked at TeX-recenter-output-buffer and adjusted it accordingly (I'm not an elisp programmer, so... use with care).

;; recenter *compilation* buffer (adjusted from TeX-recenter-output-buffer)
(defun compile-recenter-output-buffer (line)
"Redisplay *compilation* buffer of compile job output so that most recent
output can be seen. The last line of the buffer is displayed on line LINE
of the window, or at bottom if LINE is nil."
(interactive "P")
(let ((buffer (get-buffer "*compilation*"))); adjusted
(if buffer
(let ((old-buffer (current-buffer)))
(pop-to-buffer buffer t t); adjusted
(bury-buffer buffer)
(goto-char (point-max))
(recenter (if line
(prefix-numeric-value line)
(/ (window-height) 2)))
(pop-to-buffer old-buffer nil t)); adjusted
(message "No *compilation* buffer found."))))


I then bound it to C-c o via (global-set-key (kbd "C-c o") 'compile-recenter-output-buffer).

-

You could define a custom application of the tex-recenter-output-buffer function:

(defun recenter-compilation-buffer (linenum)
(interactive "P")
(let ((compilation-buffer (get-buffer "*compilation*"))
(window))
(if (null compilation-buffer)
(message "No compilation buffer")
(setq window (display-buffer compilation-buffer))
(save-selected-window
(select-window window)
(bury-buffer compilation-buffer)
(goto-char (point-max))
(recenter (if linenum
(prefix-numeric-value linenum)
(/ (window-height) 2)))))))


Then, add it as a hook to LaTeX mode:

(add-hook 'tex-mode-hook
'(lambda ()
(local-set-key "\C-c\C-l" 'recenter-compilation-buffer)))

-
Thanks, Daniel. I receive "C-c C-l is undefined". –  Marius Hofert Jan 14 '13 at 15:38
@Marius Hofert Does M-x recenter-compilation-buffer work? –  Daniel Martín Jan 14 '13 at 16:33
yes, that works! What's the problem then? –  Marius Hofert Jan 14 '13 at 16:45
ahh, if I execute C-c C-l from within the compilation buffer, then it works. Otherwise, either latex-mode searches for a process or R/ESS wants to start a new R session. But if I have to change to the compilation buffer, there is no point in recentering it, a simple M-shift->" will do that... –  Marius Hofert Jan 14 '13 at 16:49
@MariusHofert I've modified the keybinding code to hook into LaTeX mode instead. –  Daniel Martín Jan 14 '13 at 17:13
show 1 more comment

I assume you are invoking compile rather than using AUCTeX. This completely bypasses AUCTeX as you have guessed. The way to use AUCTeX is to add the following to your .emacs.

(add-to-list 'TeX-command-list
("Make"               ;; The command you use to invoke it: C-c C-c Make RET
"make pdf"           ;; The make command to run
TeX-run-TeX nil t    ;; TeX-run-TeX tells it to parse error as if from TeX
:help "Run make to build everything")


Doing things this way has the added advantage of allowing AUCTeX to parse the errors. If your make file emits compiler type errors then you can change TeX-run-TeX to TeX-run-compile and it will run in a compilation mode buffer. See the documentation for other options.

-
Thanks for helping, Ivan. Concerning your first sentence: I have a makefile which I invoke via C-c C-x (shortcut for M-x compile). Concerning your code: I already have a similar construction in .emacs, just with TeX-run-command instead of TeX-run-TeX. However, I don't know how this should solve the problem: I would like to recenter the compile buffer no matter what the makefile does (it could compile some C code, for example). I just used LaTeX as an example (which was probably bad since the question got migrated from stackoverflow) –  Marius Hofert Jan 18 '13 at 1:43
I see. I was telling you how to hook into the AUCTeX machinery so that the "normal" C-c C-l would work. If you want it for any compile command, then you can ignore this answer. –  Ivan Andrus Jan 18 '13 at 7:11
I already have the "normal" C-c C-l. It's so helpful that I thought it would be nice to have it for any compile command :-) –  Marius Hofert Jan 18 '13 at 7:40
TeX-run-compile uses the compile function, and C-c C-l doesn't work there, exactly as when using M-x compile RET RET`. –  T. Verron Mar 26 '13 at 13:42