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

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

Re: [QUESTION] Add conditional indicator to `mode-line-misc-info' not wo


From: Stefan Monnier
Subject: Re: [QUESTION] Add conditional indicator to `mode-line-misc-info' not working
Date: Thu, 07 Mar 2024 09:54:58 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> #+begin_src emacs-lisp
> (add-to-list 'mode-line-misc-info
>              '(image-mode (:eval (image-size (image-get-display-property) 
> :pixels))))
> #+end_src
>
> But it's not displaying image info on mode-line when I open an image
> with [M-x find-file RET /path/to/image.png RET].
>
> I searched GitHub code for `mode-line-misc-info` examples. I don't know
> what does the `image-mode` mean in upper code.

I know searching the 'net is fun and all, but Emacs was designed before
it, so it comes with a manual that can answer those questions even when
greed will have finished rendering the internet useless.

    C-h i m elisp RET m mode line format RET m data RET

will get you to the relevant node.

> Is it a condition which only evaluate following `(:eval (image-size
> ...))` snippet in buffer which major-mode is `image-mode`?

Close, but no: it shows it only when the `image-mode` variable is non-nil.
IOW it was designed for minor modes rather than major modes.

For major modes, you usually use a buffer-local setting instead, e.g.

    (add-hook 'image-mode-hook
              (lambda ()
                (setq mode-name `("" ,mode-name (:eval ...)))))

I used here a variable that's already used buffer-locally (another is
`mode-line-process`), which is less likely to cause surprises than if
you make `mode-line-misc-info` buffer-local.

Note also that the `:eval` thing should return a string, not a number or
a `cons`.

> How to correctly add indicator to `mode-line-misc-info`?

You could do:

    (defvar-local my-indicator nil)
    (unless (memq 'my-indicator mode-line-misc-info)
      (setq mode-line-misc-info `("" my-indicator . ,mode-line-misc-info)))

and then

    (add-hook 'image-mode-hook
              (lambda ()
                (setq my-indicator `(:eval ...))))


-- Stefan




reply via email to

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