I know how to highlight the source code corresponding to the selected compilation-mode error. I also know about the little arrow in the fringe that points to the error in the compilation buffer, and that if the fringe is not visible, next-error will show the error right at the top of the window to indicate which error is selected.

What I want to know instead is: How can I highlight the selected error in the compilation-mode buffer with a face that I can customize?

I want this because I want to see where I've jumped to when I both:

  1. am running emacs in a terminal (-nw)

  2. have compilation-context-lines set to some high value like 15 so I can see a whole Python traceback rather than just the last line

  • 1
    Not an answer to your question, but I have a simple modified version of hl-line-mode active in buffer that shows my error messages -- usually dealing with Grep results. And, I set the face to just a yellow underline. And, my custom version adds an overlay-arrow-bitmap / fringe indicator in the left fringe of wherever point is at.
    – lawlist
    Sep 24 '18 at 22:31
  • Here is a link to a screenshot that depicts grep results, with surrounding context lines in grey, error lines in royal blue (sandwiched between context above and below), error line number in red, divider dashes in plain default white, overlay-arrow-bitmap of a dark blue arrow at point, and a yellow underline of a modified hl-line-mode wherever I move my point (either manually with an arrow key, or by jumping next/previous error): lawlist.com/images/example_compilation.png . If something in that screenshot looks appealing and you need assistance achieving it, I'd be happy to help.
    – lawlist
    Sep 26 '18 at 19:26
  • @lawlist the yellow line looks appealing! But I don't understand how to achieve it, because while next-error (with -nw at least) scrolls the compilation window and sets its current line, face hl-line doesn't get applied to the new current line (and removed from the old one) until I switch to the compilation window (but I basically never have the cursor in the compilation window). Sep 29 '18 at 19:34
  • 1
    Thank you for teaching me about the ability to call next-error/previous-error from the source file. I was unaware of that feature. Here is one idea: Enable hl-line-mode in the compilation buffer, which can be enabled automatically by turning it on with the compilation-mode-hook; e.g., (add-hook 'compilation-mode-hook (lambda () (hl-line-mode 1))). And, then add this to your .emacs/init.el file: (defun my-func () (with-current-buffer next-error-last-buffer (when hl-line-mode (hl-line-highlight)))) (add-hook 'next-error-hook 'my-func)
    – lawlist
    Sep 29 '18 at 23:40
  • To the extent you want to write a custom function to run your own highlighting stuff in the compilation mode buffer, the following variables can be used from the next-error-hook to get the location of point in the compilation buffer: compilation-current-error and overlay-arrow-position, both of which are markers. Here is a message that prints the values for the markers: (defun my-func () (with-current-buffer next-error-last-buffer (message "%d | %d" (marker-position overlay-arrow-position) (marker-position compilation-current-error)))) (add-hook 'next-error-hook 'my-func)
    – lawlist
    Sep 29 '18 at 23:47

From lawlist's comment (this works nicely in both terminal and GUI emacs -- I find the highlighting a lot easier to see at a glance than the little arrow fringe marker):

(defun my/turn-on-hl-line-mode ()
  (hl-line-mode 1))
(add-hook 'compilation-mode-hook #'my/turn-on-hl-line-mode)
(defun my/update-compilation-hl-line ()
  (with-current-buffer next-error-last-buffer
    (when hl-line-mode (hl-line-highlight))))
(add-hook 'next-error-hook 'my/update-compilation-hl-line)

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.