Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96294

Highlight current line only in line mode (not in char mode)

When working in ansi-term, how can I have emacs only highlight the current line when I am in line mode? (and not in char mode?).

I currently have (global-hl-line-mode t) which activates hl-line-mode in every buffer (which I want). I just want to specifically disable it in char run mode.

Upvotes: 1

Views: 343

Answers (1)

Thomas
Thomas

Reputation: 17422

You can achieve the effect you want in two steps. First, replace (global-hl-line-mode t) in your .emacs file with the following lines:

(add-hook 'after-change-major-mode-hook
          '(lambda () (hl-line-mode (if (equal major-mode 'term-mode) 0 1))))

This basically does the same thing as making hl-line-mode a global minor mode, as it turns on hl-line-mode every time the major mode of a buffer changes. But it doesn't turn on hl-line-mode if the new major mode of a buffer is term-mode. This way, hl-line-mode is disabled by default for ansi-term.

However, you do want to turn it on when you're in line-mode (but not in char run mode). For that, add the following lines as well to your .emacs file:

(defadvice term-line-mode (after enable-hl-line-in-term-line-mode)
  (hl-line-mode 1))

(defadvice term-char-mode (after disable-hl-line-in-term-char-mode)
  (hl-line-mode 0))    

Depending on which version of Emacs you're using, you might experience an odd behavior in the minibuffer with the above code: either the full line or parts of the line might get highlighted every time you use the minibuffer. To fix that, also add the following line to your .emacs file:

(add-hook 'minibuffer-setup-hook '(lambda () (hl-line-mode 0)))

This approach gives you quite a bit of flexibility over when hl-line-mode should be turned on or off. For instance, if you wanted to have other major modes for which hl-line-mode should be turned off, you could replace the (equal major-mode 'term-mode) portion of the above code with:

(member major-mode '(term-mode other-mode1 other-mode2))

where other-modeN are the names of the major modes for which you want hl-line-mode to be disabled. Of course you're not limited to only two such names.

Upvotes: 3

Related Questions