help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Custom mode-line format (bug)


From: Michael Heerdegen
Subject: Re: Custom mode-line format (bug)
Date: Wed, 20 Mar 2013 18:07:58 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Hi,

>  - then whenever a file is visited the hook is executed which then checks 
> to see if the file is being opened with root privileges (/su(do):). If 
> so, it changes the face of the mode line to the custom one created 
> previously.

>  Snippet of code follows 
> -------------------------
> ;; Display a warning signal in the mode line when visiting a file with 
> root
> ;; privileges.
> (defgroup mode-line-custom nil
>   "Faces used by mode-line-custom."
>   :group 'mode-line-custom
>   :group 'faces)
>
> (defface mode-line-custom-warning-face
>   '((t (:background "dark red" :foreground "white")))
>   "Face used for custom mode line warnings."
>   :group 'mode-line-custom
>   :version "22.1")
>
> (defun root-file-warning ()
>   (when (string-match "^/su\\(do\\)?:" default-directory)
>     (setq mode-line-format
>           (format-mode-line mode-line-format 'mode-line-custom-warning-
> face))
>     (server-start-timed))
>   )
>           
> (add-hook 'find-file-hook 'root-file-warning)
> (add-hook 'dired-mode-hook 'root-file-warning)

No, that's not a bug.  You set `mode-line-format' to a already formatted
(calculated) mode-line, which is just a (constant) string.  Note that

 (format-mode-line mode-line-format 'mode-line-custom-warning-face)

is evaluated at load time.

What you want is something like this:

(defun root-file-warning ()
  (when (string-match "^/su\\(do\\)?:" default-directory)
    (setq mode-line-format
          `(:propertize ,mode-line-format face mode-line-custom-warning-face))
    (server-start-timed)
    ))

But maybe it's better (cleaner) to use face-remapping for that purpose,
instead of manipulating the mode-line:

(defun root-file-warning ()
  (when (string-match "^/su\\(do\\)?:" default-directory)
    (face-remap-add-relative
     'mode-line
     '(:background "dark red" :foreground "white"))
    (server-start-timed)))

You could as well remap the `mode-line-inactive' face so that the
mode-line looks different as well when the according window is not
selected.


Regards,

Michael.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]