all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Custom mode-line format (bug)
Date: Wed, 20 Mar 2013 18:07:58 +0100	[thread overview]
Message-ID: <87hak6ov81.fsf@web.de> (raw)
In-Reply-To: <5149bf3f$0$43297$862e30e2@ngroups.net> (Miguel Guedes's message of "20 Mar 2013 13:53:04 GMT")

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	other threads:[~2013-03-20 17:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-20 13:53 Custom mode-line format (bug) Miguel Guedes
2013-03-20 17:07 ` Michael Heerdegen [this message]
     [not found] ` <mailman.22546.1363799292.855.help-gnu-emacs@gnu.org>
2013-03-21  7:57   ` Miguel Guedes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87hak6ov81.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.