5

On next-error in the grep or compilation window, I can use something like

(setq next-error-recenter 35)

to ensure that window displaying the match scrolls to show the whole match, as described in more detail in this stackoverflow question.

How do I do the same thing in the grep or compilation window itself? I often have grep output or compile output that is off the bottom of the window, and on days when I spend all of my time going through grep output or compile errors, switching back and forth between windows adds a significant overhead.

TooTone
  • 401
  • 4
  • 11
  • You want to look at `compilation-goto-locus` and put in a `recenter` command where appropriate -- at the section that begins with the comment: `If the compilation buffer window was selected . . . ,` You can recenter at different location of the window -- see the doc-string for `recenter`. – lawlist Feb 26 '16 at 07:40
  • Added a command `recenter-current-error`, bound to `l` in the `*Occur*` and `*Grep*` buffers; the effect of this command is like calling `C-l` in the window displaying the current error. This will be available in a future release Emacs-28. – Tino Feb 07 '21 at 18:40

1 Answers1

1

I use such workaround for the exact saving of point position in the compilation/grep buffer:

(defun eab/compile-goto-error ()
  (interactive)
  (let ((istc? truncate-lines))
    (toggle-truncate-lines t)
    (let ((buf (current-buffer))
          (line (- (count-lines (window-start) (point))
                   (if (eq (point) (point-at-bol)) 0 1)))
          (point (point)))
      (compile-goto-error)
      (run-with-timer 0.01 nil `(lambda ()
                                 (let ((cb (current-buffer)))
                                       (pop-to-buffer ,buf)
                                       (recenter ,line)
                                       (goto-char ,point)
                                       (toggle-truncate-lines ,istc?)
                                       (pop-to-buffer cb)))))))

If you want simple centering of the point in the compilation/grep buffer, use:

(defun eab/next-error ()
  (interactive)
  (let ((buf (current-buffer)))
    (next-error)
    (run-with-timer 0.01 nil `(lambda ()
                             (let ((cb (current-buffer)))
                               (pop-to-buffer ,buf)
                               (recenter)
                               (pop-to-buffer cb))))))

You can use next-error instead of compile-goto-error in the first example.

Upd. Emacs 25.1 is needed for non-zero time period.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
artscan
  • 445
  • 2
  • 12