根据缓冲区状态更改模式行的颜色

时间:2013-06-07 13:46:30

标签: emacs

是否有办法根据特定条件动态更改模式行的颜色,例如如果我在狭窄的视图中会改变颜色,如果缓冲区是只读的则改变为不同的颜色

非常感谢!

2 个答案:

答案 0 :(得分:5)

您可以使用post-command-hook,然后只评估您需要的任何内容并设置模式线面部颜色。我这样做是为了在3种颜色之间进行更改,具体取决于我所处的邪恶模式状态以及缓冲区是否有任何未保存的更改。

(lexical-let ((default-color (cons (face-background 'mode-line)
                                   (face-foreground 'mode-line))))
  (add-hook 'post-command-hook
    (lambda ()
      (let ((color (cond ((minibufferp) default-color)
                         ((evil-insert-state-p) '("#e80000" . "#ffffff"))
                         ((evil-emacs-state-p)  '("#af00d7" . "#ffffff"))
                         ((buffer-modified-p)   '("#006fa0" . "#ffffff"))
                         (t default-color))))
        (set-face-background 'mode-line (car color))
        (set-face-foreground 'mode-line (cdr color))))))

答案 1 :(得分:3)

我使用此代码。如果它是只读的,它会将左侧橙色的缓冲区修改指示器着色,如果它被修改则为红色。缩小缓冲区时,会将行号指示器的颜色设置为黄色。显然你可能想自己改变格式。

(defface my-narrow-face
  '((t (:foreground "black" :background "yellow3")))
  "todo/fixme highlighting."
  :group 'faces)

(defface my-read-only-face
  '((t (:foreground "black" :background "orange3")))
  "Read-only buffer highlighting."
  :group 'faces)

(defface my-modified-face
  '((t (:foreground "gray80" :background "red4")))
  "Modified buffer highlighting."
  :group 'faces)

(setq-default
 mode-line-format
 '("  "
   (:eval (let ((str (if buffer-read-only
                         (if (buffer-modified-p) "%%*" "%%%%")
                       (if (buffer-modified-p) "**" "--"))))
            (if buffer-read-only
                (propertize str 'face 'my-read-only-face)
              (if (buffer-modified-p)
                  (propertize str 'face 'my-modified-face)
                str))))
   (list 'line-number-mode "  ")
   (:eval (when line-number-mode
            (let ((str "L%l"))
              (if (/= (buffer-size) (- (point-max) (point-min)))
                  (propertize str 'face 'my-narrow-face)
                str))))
   "  %p"
   (list 'column-number-mode "  C%c")
   "  " mode-line-buffer-identification
   "  " mode-line-modes))