Color change of the mode line depending on the state of the buffer

Is there a way to dynamically change the color of the mode line depending on specific conditions, for example. change the color if I'm narrowed and in a different color if the buffer is read-only

Thank you very much!

+4
source share
2 answers

I am using this code. It colors the buffer-modified indicator in left orange if it is read-only and red if it is changed. When you narrow the buffer, it turns the line number indicator yellow. Obviously, you can change the format yourself.

(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)) 
+3
source

You can use post-command-hook , and then just evaluate everything you need and set your complexion in line mode. I do this to change between 3 colors, depending on what state of evil I enter, and if there are unsaved changes in the buffer.

 (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)))))) 
+3
source

Source: https://habr.com/ru/post/1485042/


All Articles