unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: [QUESTION] Add conditional indicator to `mode-line-misc-info' not working
Date: Thu, 07 Mar 2024 09:54:58 -0500	[thread overview]
Message-ID: <jwv34t214uy.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 65e96d12.170a0220.932f6.0c8f@mx.google.com

> #+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	other threads:[~2024-03-07 14:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07  7:30 [QUESTION] Add conditional indicator to `mode-line-misc-info' not working Christopher M. Miles
2024-03-07 14:54 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]
2024-03-07 15:13   ` On manuals and the Intertubes [was: Add conditional indicator to `mode-line-misc-info'] " tomas
2024-03-07 15:34     ` Eric S Fraga
2024-03-07 15:43       ` [OFFTOPIC] Re: On manuals and the Intertubes " Stefan Monnier via Users list for the GNU Emacs text editor
2024-03-07 17:07         ` Eric S Fraga
2024-03-07 22:56           ` Stefan Monnier via Users list for the GNU Emacs text editor

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=jwv34t214uy.fsf-monnier+emacs@gnu.org \
    --to=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
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).