HighlightCurrentLine

This page is about libraries that highlight the current line of characters, so you can find it easily.

Standard Library `hl-line.el'

GnuEmacs version 21 has library `hl-line.el’, which provides a local and a global minor mode for highlighting the current line. See highlight current line.

Extension Library `hl-spotlight.el'

Library hl-spotlight.el extends standard library `hl-line.el’. It spotlights the current few lines using a different face from that used by `hl-line.el’. It is designed to be used together with library centered-cursor-mode.el, so that the lines surrounding the cursor remain highlighted, and these spotlighted lines remain in the center of the window.

`hl-spotlight.el’ separates its spotlighting from your normal use of ‘hl-line’ highlighting. It does this in these ways:

Extension Library `hl-line+.el'

Library hl-line+.el extends standard library `hl-line.el’ in several ways. It provides these commands:

The idea for both of these is to let ‘hl-line-mode’ help you find the current line, but not keep highlighting it after you’ve found it.

In addition, library hl-line+.el provides local and global modes (commands ‘hl-spotlight-mode’ and ‘global-hl-spotlight-mode’) to highlight several lines surrounding the cursor using a different face, ‘hl-spotlight’. You can enlarge or shrink this spotlight highlighting using command ‘hl-spotlight-enlarge’. You can repeat, via `C-x z z z z…’, to enlarge or shrink the spotlight incrementally.

Spotlight highlighting can be used together with library centered-cursor-mode.el, by AndreRiemann, which keeps the cursor (hence also the spotlight) centered in the window. This can be helpful when reading text (as opposed to code). This is controlled by user option ‘hl-spotlight-keep-centered-flag’.

See Also:

Discussion

Question: I’ve some troubles installing hl-line+.el. I copied hl-line+.el to /usr/local/share/emacs/site-lisp (debian box), byte-compiled to /usr/local/share/emacs/site-lisp/hl-line+.elc (with message “** The following functions are not known to be defined: global-hl-line-highlight, global-hl-line-unhighlight”), and added (require 'hl-line+) and (toggle-cursor-type-when-idle 1) to my InitFile, but I get the error “Symbol's function definition is void: toggle-cursor-type-when-idle” while loading it. What do I miss? Thanks. – host-84-221-75-6.cust-adsl.tiscali.it

There was a typo in the Commentary. ‘toggle-cursor-type-when-idle’ should be ‘toggle-hl-line-when-idle’. I corrected this in the latest version. Thx – DrewAdams

Question: In ~/.emacs (toggle-hl-line-when-idle 1) now works, but (flash-line-highlight 1) gives “Symbol’s function definition is void: global-hl-line-highlight”. (require 'hl-line) and (global-hl-line-mode 1) both work: so I suppose I’m loading hl-line.el well. I cannot see its content, because it’s an .elc installed by debian emacs21 package (21.4). Thanks a lot… maybe I’ll upgrade. Bye!

You are apparently using Emacs 21, not Emacs 22. As the Compatibility: field in the header of hl-line+.el states, it works with GnuEmacs 22. It is an extension of hl-line.el of Emacs 22, where ‘global-hl-line-highlight’ is defined. You can try to see if hl-line+.el works at all with Emacs 21, but all bets are off. To try that, you’ll need to tweak things a bit, to at least suppress the error message. It is useless to (setq global-hl-line-highlight 1) – it is a function (in Emacs 22), not a variable. Thx – DrewAdams


For some reason, it seems that having 'hl-line-unhighlight-now permanently in 'pre-command-hook results in some freezes when scrolling. So I slightly modified hl-line+ in the following way (all it does is to run hl-line-unhighlight-now exactly when needed)

    (defun hl-line-toggle-when-idle (&optional arg)
    "Turn on or off using `global-hl-line-mode' when Emacs is idle.
    When on, use `global-hl-line-mode' whenever Emacs is idle.
    With prefix argument, turn on if ARG > 0; else turn off."
      (interactive "P")
      (setq hl-line-when-idle-p
            (if arg (> (prefix-numeric-value arg) 0) (not hl-line-when-idle-p)))
      (cond (hl-line-when-idle-p
             (timer-activate-when-idle hl-line-idle-timer)
             (message "Turned ON using `global-hl-line-mode' when Emacs is idle."))
            (t
             (cancel-timer hl-line-idle-timer)
             (message "Turned OFF using `global-hl-line-mode' when Emacs is idle."))))
    
    (defun hl-line-highlight-now ()
      "Turn on `global-hl-line-mode' and highlight current line now."
      (unless global-hl-line-mode
        (global-hl-line-mode 1)
        (global-hl-line-highlight)
        (add-hook 'pre-command-hook 'hl-line-unhighlight-now)
        ))
    
    (defun hl-line-unhighlight-now ()
      "Turn off `global-hl-line-mode' and unhighlight current line now."
      (global-hl-line-mode -1)
      (global-hl-line-unhighlight)
      (remove-hook 'pre-command-hook 'hl-line-unhighlight-now))

Thanks, Yann. Assuming you agree, I’ve updated hl-line+.el. – DrewAdams

Question:. In Customize Mode, for example, highlighting with hl-line-mode doesn’t work properly. Usually, every highlighted line has the background and foreground color of the face called highlight (M-x customize-face RET highlight RET). This works fine for all modes I’m using (e.g. Python, Java, AUCTeX) but not in Customize Mode. Here, some faces keep their colors, for example custom-link. Depending on the face, this makes parts of the buffer unreadable. I use the Robin Hood ColorTheme. Thanks. – ditch

Please provide a clear recipe to produce the problem, starting with emacs -Q. State your Emacs version and platform. Are you using hl-line+.el or only hl-line.el? If you are using hl-line+.el, do you also have the problem with only hl-line.el. What happens if you don’t use color-theme. Provide any other info you think might be pertinent. Always start from scratch (emacs -Q), and provide the minimum recipe needed to reproduce the problem completely. Thx – DrewAdams

I use hl-line.el and emacs 23.0.60.1. After emacs -Q I say M-x global-hl-line-mode. The highlighted line differs from an unhighlighted line in that the highlighted line’s background color is darkseagreen2 (default setting for face highlight). When I move the cursor up in the *scratch* buffer, there’s some red text. Moving the cursor over one of those lines has the following effect: foreground color stays red, background color changes from white to darkseagreen2 – works as expected. The question that arises is what happens if you highlight text which has a foreground color that is very similar or even equal to the highlight face?

I use color-theme – usually the Robin Hood theme. When I switch to this theme the highlighting behaviour is different. The highlight face now defines foreground and background color. This means that no matter what colors are used in a buffer, a highlighted line always has the same two colors with good contrast, avoiding the aforementioned problem of the highlight face interfering with text color. This works fine in all the modes I need except for Customize Mode. Here, some faces like custom-link “don’t care” about the highlight face. They remain the same color when they are in a highlighted line. My knowledege of Emacs limited but I assume that there is some face property that blocks highlighting. Buttons, checkboxes, text fields, etc. in Customize Mode do it too but in their case it makes sense. I apologize for this lenghty explanation. I’m beginning to think that hl-line.el might not be the right place to tackle this problem. Thanks. – ditch

Not sure I followed all of that. Perhaps someone else can help more. My impression is that you might want to file an Emacs bug: ‘M-x report-emacs-bug’. – DrewAdams

As usual, I seem to have a weird problem that no one else has. I’ll try to make it clear in the bug report. – ditch.

Question:. I’m using (toggle-hl-line-when-idle 1) in my ~/.emacs. and I don’t want to highlight line when I’m in Info mode or Help mode etc. I try to use (add-hook ‘Info-mode-hook ‘(lambda () (toggle-hl-line-when-idle -1))) but after I trigger this it turn off highlight on other buffers too. Is there a way to turn on/off this mode only for current buffer and how? – Yuanle

Good question. No, I’m sorry, there is no simple way to do that. hl-line+.el uses only global hl-line mode. There is a single idle timer for this, and it has no notion of the current mode.

You might try to change the definition of ‘hl-line-idle-timer’ to run not ‘hl-line-highlight-now’, but some new command ‘hl-line-highlight-local-now’, which is like the global version but uses ‘hl-line-mode’ etc. instead of ‘global-hl-line-mode’ etc. You will need an unhighlight version too, etc.

If you do that and like what you end up with, I could add it to the library, if you like. – DrewAdams

Hi Drew, I have a simple patch that allow user to define modes that should not be highlighted by hl-line+. It’s based on a older version, but you get the idea.

+(defcustom hl-line-disable-for-modes '(Info-mode help-mode view-mode Man-mode)
+  "hl-line+ will not hl anything on these modes."
+  :type 'list
+  :group 'hl-line)
+
... 
 (defun hl-line-highlight-now ()
   "Turn on `global-hl-line-mode' and highlight current line now."
-  (unless global-hl-line-mode
-    (global-hl-line-mode 1)
-    (global-hl-line-highlight)
-    (add-hook 'pre-command-hook 'hl-line-unhighlight-now)))
+  (if (not (member major-mode hl-line-disable-for-modes))
+      (unless global-hl-line-mode
+	(global-hl-line-mode 1)
+	(global-hl-line-highlight)
+	(add-hook 'pre-command-hook 'hl-line-unhighlight-now))))

sylecn

OK, I’ve added that (assuming you agree). I set the default value to ‘nil’, however. It’s not clear to me that any given nonempty list of modes would be good by default, especially since ‘hl-line-highlight-now’ can be called from other code. Thx – DrewAdams

of course I agree:) Thanks Drew. --sylecn

When ‘global-hl-line-mode’ is turned on, ‘hl-line-inhibit-highlighting-for-modes’ does not work anymore. IMO, the inhibiting should work for ‘global-hl-line-mode’, shouldn’t it? – Kiwon

Yes. Should be OK now; thanks. – DrewAdams

Library `highlight-current-line.el'

There is also Lisp:highlight-current-line.el

Library `highline.el'

At least Gentoo ships a library with this name written and maintained by ViniciusJoseLatorre.

See Lisp:highline.el (HighlineMode).

Library `pulse.el'

See PulseRegion, function ‘pulse-momentary-highlight-one-line’.

XEmacs Compatibility


CategoryEditing CategoryDisplay CategoryAccessibility CategoryFaces HighlightTemporarily