Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs hl-line: change color locally

I normally let hl-line take a slightly darker shade of current background. This works nice in editing buffers. However, in some buffers, such as Org agenda and the Gnus group buffer, I would like to use a more spiffy color (in place of the cursor).

To be specific I would like to change the color of hl-line in the gnus-hl-line without affecting the color of hl-line in other buffers.

(add-hook 'gnus-summary-mode-hook 'gnus-hl-line)
(add-hook 'gnus-group-mode-hook 'gnus-hl-line)

(defun gnus-hl-line ()
  (hl-line-mode 1)
  (set (make-local-variable 'line-move-visual) nil)
  (setq cursor-type nil))

Thanks,

Final solution using Phil's suggestion. It uses a neutral hl line most of the time, but sometimes a bold hl-line is appreciable, e.g. in Gnus and Org agenda

;; From emacs-wiki:
(defun shade-color (intensity)
  "print the #rgb color of the background, dimmed according to intensity"
  (interactive "nIntensity of the shade : ")
  (apply 'format "#%02x%02x%02x"
         (mapcar (lambda (x)
                   (if (> (lsh x -8) intensity)
                       (- (lsh x -8) intensity)
                     0))
                 (color-values (cdr (assoc 'background-color (frame-parameters)))))))

;; Default hl
(global-hl-line-mode t)
(make-variable-buffer-local 'global-hl-line-mode)
(set-face-background hl-line-face (shade-color 08))  

(defface hl-line-highlight-face
  '((t :inherit highlight))
  "Face for highlighting the current line with `hl-line-fancy-highlight'."
  :group 'hl-line)

(defun hl-line-fancy-highlight ()
  (set (make-local-variable 'hl-line-face) 'hl-line-highlight-face)
  ;;    (set (make-local-variable 'line-move-visual) nil)
  ;;    (set (make-local-variable 'cursor-type) nil)
  (setq global-hl-line-mode nil)
  (hl-line-mode))

(add-hook 'org-agenda-mode-hook 'hl-line-fancy-highlight)
(add-hook 'gnus-summary-mode-hook 'hl-line-fancy-highlight)
(add-hook 'gnus-group-mode-hook 'hl-line-fancy-highlight)
like image 579
Rasmus Avatar asked Apr 20 '12 01:04

Rasmus


1 Answers

hl-line-face is a variable containing the face to use for hl-line-mode, so we can make that variable buffer-local in these modes, and assign it a new custom face.

You can create a new face like this:

(defface gnus-hl-line
  '((t :inherit hl-line))
  "Face for highlighting the current line with `gnus-hl-line'."
  :group 'hl-line)

and customize it with M-x customize-face RET gnus-hl-line RET

Then add this in your gnus-hl-line function (before calling hl-line-mode would seem most sensible).

(set (make-local-variable 'hl-line-face) 'gnus-hl-line)
like image 80
phils Avatar answered Sep 21 '22 17:09

phils