窗口不清晰时更改突出显示颜色吗?


13

我正在使用hl-mode作为灵巧的次要模式。当精巧窗口不是当前窗口时,如何使突出显示的线条更改颜色(例如,变为灰色),然后当精巧窗口再次变为当前窗口时,如何使其变回默认的突出显示颜色?


事实证明,有一个选项,请参阅emacs.stackexchange.com/a/15141/780
glucas

Answers:


5

我已经实现了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)

另请参见function walk-windows,您可以使用该函数在可见框架的窗口上进行迭代等。(而+1为buffer-list-update-hook,被调用select-window。)
Drew Drew

为此+1!对于hl-line而言,并不是特别重要,但多年来我一直在寻找一种可靠的方法来对框架中非活动窗口的背景进行着色。这可行!谢谢!
亚伦·米勒

谢谢,太棒了!不幸的是,使用时似乎有问题global-hl-line-mode。似乎global-hl-line-mode没有hl-line-mode为每个缓冲区加载。它使用覆盖而不是使用hl-line面部。我已经摆弄了一段时间,没有运气。有什么建议么?我应该另开一个问题吗?
Lorem Ipsum
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.