Emacs hl-line:本地更改颜色

6

我通常让 hl-line 采用当前背景的略深一点的色调。这在编辑缓冲区中效果很好。然而,在某些缓冲区,例如 Org 日程表和 Gnus 组缓冲区,我希望使用更漂亮的颜色(代替光标)。

具体来说,我想在 gnus-hl-line 中更改 hl-line 的颜色,而不影响其他缓冲区中 hl-line 的颜色。

(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))

谢谢,

使用Phil的建议得出最终解决方案。大部分时间使用中性高亮行,但有时候需要使用粗体高亮行,例如在Gnus和Org日程表中。

;; 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)

请问在代码中哪里可以设置 org-agenda、gnus-summary 和 gnus-group 的颜色和强度(intensity)?谢谢! - Alexander Shcheblikin
1个回答

5

hl-line-face是一个变量,包含hl-line-mode所使用的外观,因此我们可以在这些模式下将该变量设置为缓冲区本地变量,并分配一个新的自定义外观。

您可以按照以下方式创建新的外观:

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

您可以使用 M-x customize-face RET gnus-hl-line 来自定义它,并将其与其他内容区分开。

然后在您的 gnus-hl-line 函数中添加以下内容(在调用 hl-line-mode 之前似乎最明智)。

(set (make-local-variable 'hl-line-face) 'gnus-hl-line)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接