根据evil和内置输入法的状态调整modeline的背景色

;; {{ change modeline color by evil&ime state
(defconst my-default-color (cons (face-background 'mode-line)
                                 (face-foreground 'mode-line)))

(defun my-show-evil-state ()
  "Change modeline color to notify user evil current state."
  (let ((color (cond
                ((minibufferp)
                 my-default-color)
                (current-input-method
                 '("#e80074" . "#ffffff"))
                ((evil-insert-state-p)
                 '("#e80000" . "#ffffff"))
                ((evil-emacs-state-p)
                 '("#444488" . "#ffffff"))
                ((buffer-modified-p)
                 '("#006fa0" . "#ffffff"))
                (t
                 my-default-color))))
    (set-face-background 'mode-line (car color))
    (set-face-foreground 'mode-line (cdr color))))
(add-hook 'post-command-hook #'my-show-evil-state)
;; }}

输入法启动后modline外观

5 个赞

感谢 @redguardtoo,根据代码改了个 meow 版本。

;; change modeline color by meow&ime state
(defconst my-default-color (cons (face-background 'mode-line)
                                 (face-foreground 'mode-line)))

(defun my-show-meow-state ()
  "Change modeline color to notify user current ime & meow state."
  (let ((color (cond
                ((minibufferp)
                 my-default-color)
                ;; 因为各种状态有重叠,以下顺序影响最终效果,重要的条目往前排
                (current-input-method
                 '("#e80074" . "#ffffff"))
                ((meow-normal-mode-p)
                 my-default-color)
                ((meow-insert-mode-p)
                 '("#e80000" . "#ffffff"))
                ;; ((meow-motion-mode-p)
                ;;  '("#444488" . "#ffffff"))
                ;; ((meow-beacon-mode-p)
                ;;  my-default-color)
                ;; ((meow-keypad-mode-p)
                ;;  my-default-color)
                ((buffer-modified-p)
                 '("#006fa0" . "#ffffff"))
                (t
                 my-default-color))))
    (set-face-background 'mode-line (car color))
    (set-face-foreground 'mode-line (cdr color))))

(add-hook 'post-command-hook #'my-show-meow-state)
;; 可能有用的hook,根据需要启用:
;; 1. 若开启了老猫auto-save或者super-save,则用after-save-hook触发更新(buffer-modified-p)
;; (add-hook 'after-save-hook #'my-show-meow-state)
;; 2. 根据窗口变动
;; (add-hook 'window-configuration-change-hook #'my-show-meow-state)
;; 3. 根据聚焦窗口变动
;; (add-function :after after-focus-change-function #'my-show-meow-state)

Edit 1~ 4: 调整了代码和相关说明。

1 个赞

友谊提醒一下,post command hook 慎重使用,前两天刚解决了一个编辑卡顿的问题,就是某 hook 导致的。