Search code examples
emacsemacs-faces

How to display text with different colors in mode-line


I want to display parts of my mode line with different colors but it isn't working as expected and I can't find a good web reference for this. I can change the text to bold or italic but not change the colors as required.

The simplest possible example is to display a simple mode line with the buffer-file-name in white rather than the default face color.

(custom-set-variables
 '(mode-line-format
   (quote
    ("%e" mode-line-front-space
     "[" mode-name "] %l:%i"
     "\t"
     propertize buffer-file-name 'font-lock-face '(:foreground "white")))))

Thanks to legosica for pointing out that I should have included other examples of what I've tried ...

  1. Replacing 'font-lock-face with 'face:

    propertize buffer-file-name 'face '(:foreground "white")))))
    



Follow Up

Thanks to TacticalCoder I now have exactly what I want - multiple fonts and colours in my modeline. The reason why setting 'face '(:foreground "white") didn't work is that it needed to be wrapped in '(:eval ...).

I ended up with this ...

(setq-default mode-line-format
  (list

    mode-line-front-space ; +-- just like in the default mode-line-format

    '(:eval (propertize (concat "\t[" mode-name "] %l:%i\t") 'face '(:foreground "black" :height 0.9 :weight normal)
        'help-echo (buffer-file-name)))

    '(:eval (propertize (file-name-directory buffer-file-name)  'face 'info-title-4
        'help-echo (buffer-file-name)))

    '(:eval (propertize (file-name-nondirectory buffer-file-name)  'face 'info-title-3
        'help-echo (buffer-file-name)))

    ))

Combined with ...

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(info-title-3 ((t (:inherit info-title-4 :foreground "white" :height 1.2))))
 '(info-title-4 ((t (:inherit info-title-4 :foreground "black"))))
 '(mode-line ((t (:background "#6483af" :foreground "#001122" :box (:line-width 3 :color "#6483af") :weight ultra-bold :height 118 :family "Monospace")))))

... I get a nice simple mode-line that demonstrates most of what I want. More work to do but thanks to TacticalCoder, I'm back on track.


Solution

  • Here's a tiny part of the custom modeline I'm using (I don't remember where I found it), modified as you asked to show the buffer name in another color. In this example I'm using font-lock-warning-face (which is 'red' in my color scheme):

    This is not a complete modeline by any mean:

    (setq-default mode-line-format
      (list
    
        mode-line-front-space ; +-- just like in the default mode-line-format
        mode-line-mule-info   ; |
        mode-line-client      ; |
    
        ;; the buffer name; the file name as a tool tip if you hover the mouse on it
        '(:eval (propertize "%b " 'face 'font-lock-warning-face
            'help-echo (buffer-file-name)))
    
        '(:eval (propertize (if overwrite-mode "OVERWRITE" "")
                  'face 'font-lock-warning-face
                  'help-echo (concat "Buffer is in "
                               (if overwrite-mode "overwrite" "insert") " mode")))
    
        "%-"                  ; fill what's left with '-'
        ))
    

    Does that work for you? I did also put the part where OVERWRITE appears in font-lock-warning-face in case you turn overwrite on (I kinda hate being in overwrite mode, so I want it to be very obvious).