unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Custom mode-line format (bug)
@ 2013-03-20 13:53 Miguel Guedes
  2013-03-20 17:07 ` Michael Heerdegen
       [not found] ` <mailman.22546.1363799292.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 3+ messages in thread
From: Miguel Guedes @ 2013-03-20 13:53 UTC (permalink / raw)
  To: help-gnu-emacs

The attached relevant snippet of code (see below),

 - defines a custom face group

   - creates a face

 - sets up a hook to find-file-hook and dired-mode-hook

 - 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.

The objective is to have the mode line switch to a white fg over red bg 
whenever a file is opened as root.

However, it's not working as intended. The mode-line reflects the status 
of the previously active buffer and only when I run (revert-buffer) does 
the mode line finally reflect the status of the active buffer. 

For instance, if I visit file '/su::/etc/fstab' and the currently active 
buffer is 'something-other' the mode line shows the status for 'something-
other' and not '/su::/etc/fstab' as expected. Only after reverting the 
buffer does the mode-line update and reflect the status of the correct 
buffer.

Two questions then:

 1) is this a bug or am I violating the way emacs operates?

 2) if it is a bug, what sort of workaround (if any) would you recommend?




 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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Custom mode-line format (bug)
  2013-03-20 13:53 Custom mode-line format (bug) Miguel Guedes
@ 2013-03-20 17:07 ` Michael Heerdegen
       [not found] ` <mailman.22546.1363799292.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Heerdegen @ 2013-03-20 17:07 UTC (permalink / raw)
  To: help-gnu-emacs

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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Custom mode-line format (bug)
       [not found] ` <mailman.22546.1363799292.855.help-gnu-emacs@gnu.org>
@ 2013-03-21  7:57   ` Miguel Guedes
  0 siblings, 0 replies; 3+ messages in thread
From: Miguel Guedes @ 2013-03-21  7:57 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Michael,

On Wed, 20 Mar 2013 18:07:58 +0100, Michael Heerdegen wrote:

> 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.

This solution of yours works absolutely beautiful; also thanks for the 
explanation why it wasn't working.

Here's what I came up with in the end (added mode-line-inactive as per 
your recommendation):

(defun root-file-warning ()
  (when (string-match "^/su\\(do\\)?:" default-directory)
    (face-remap-add-relative
     'mode-line
     '(:background "red3" :foreground "white"))
    (face-remap-add-relative
     'mode-line-inactive
     '(:background "red4" :foreground "dark gray"))
  )

Thanks for your help.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-03-21  7:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-20 13:53 Custom mode-line format (bug) Miguel Guedes
2013-03-20 17:07 ` Michael Heerdegen
     [not found] ` <mailman.22546.1363799292.855.help-gnu-emacs@gnu.org>
2013-03-21  7:57   ` Miguel Guedes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).