Answers:
我已经实现了hl-line-mode
使用buffer-list-update-hook
。这是代码:
(defface hl-line-inactive
'((t nil))
"Inactive variant of `hl-line'."
:group 'hl-line)
(defun hl-line-update-face (window)
"Update the `hl-line' face in WINDOW to indicate whether the window is selected."
(with-current-buffer (window-buffer window)
(when hl-line-mode
(if (eq (current-buffer) (window-buffer (selected-window)))
(face-remap-reset-base 'hl-line)
(face-remap-set-base 'hl-line (face-all-attributes 'hl-line-inactive))))))
(add-hook 'buffer-list-update-hook (lambda () (walk-windows #'hl-line-update-face nil t)))
这段代码在做什么:
hl-line-inactive
要在不活动窗口中使用的新面孔。您可以用来M-x customize-face
根据自己的喜好修改此面孔的属性。buffer-list-update-hook
调用添加一个挂钩hl-line-update-face
。旧答案
上面的代码(我在自己的init
文件中使用的代码)比我最初发布的代码简单得多。感谢@Drew提供的使用建议walk-windows
。我还阅读了有关人脸重新映射的更多信息(请参阅Emacs Lisp手册中的人脸重新映射),并意识到我可以删除很多代码。
为了后代,这是我最初发布的内容:
;; Define a face for the inactive highlight line.
(defface hl-line-inactive
'((t nil))
"Inactive variant of `hl-line'."
:group 'local)
(defun toggle-active-window-highlighting ()
"Update the `hl-line' face in any visible buffers to indicate which window is active."
(let ((dups))
(mapc
(lambda (frame)
(mapc
(lambda (window)
(with-current-buffer (window-buffer window)
(when hl-line-mode
(make-local-variable 'face-remapping-alist)
(let ((inactive (rassoc '(hl-line-inactive) face-remapping-alist)))
(if (eq window (selected-window))
(progn
(setq dups (get-buffer-window-list nil nil 'visible))
(setq face-remapping-alist (delq inactive face-remapping-alist)))
(unless (or inactive (memq window dups))
(add-to-list 'face-remapping-alist '(hl-line hl-line-inactive))))))))
(window-list frame)))
(visible-frame-list))))
(add-hook 'buffer-list-update-hook #'toggle-active-window-highlighting)
walk-windows
,您可以使用该函数在可见框架的窗口上进行迭代等。(而+1为buffer-list-update-hook
,被调用select-window
。)
global-hl-line-mode
。似乎global-hl-line-mode
没有hl-line-mode
为每个缓冲区加载。它使用覆盖而不是使用hl-line
面部。我已经摆弄了一段时间,没有运气。有什么建议么?我应该另开一个问题吗?