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: Major-mode devel: how to drop font-lock 'display when mode is disabled?
Date: Mon, 11 Mar 2024 09:16:30 -0400	[thread overview]
Message-ID: <jwva5n4syxk.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: a5bd270b9424bcc130ec11c3c6c772773a1c7d05.camel@yandex.ru

> After some searching and experimentation I figured it's easy by making
> a major mode, which basically uses a `(put-text-property start end
> 'display human-redable-text)` to change the display of timestamps.
>
> It works fine but one problem I couldn't figure out how to get rid of
> is that upon changing the major mode the custom display does not
> go away.
>
> As a workaround I have the following call:
>
> 	(add-to-list 'font-lock-extra-managed-props 'display)

You should make this change buffer-locally rather than globally.
Beside this detail, this is not considered as a workaround but as the
officially supported solution.

> …however, I presume this will make all "display" changes disappear,
> even if they been done by another mode, which isn't good.

Yup, currently Emacs doesn't keep track of who a given `display` property
belongs to, so that's as good as it gets. 🙁

>     (defun zsh-hist-display ()
>       (let ((start (match-beginning 0))
>             (end (match-end 0)))
>         (put-text-property start end 'display
>                            (zsh-hist-convert-unix-timestamp (match-string-no-properties 1)))
>         ;; put some highlighting
>         (put-text-property start end 'face '(:weight bold))
>         (put-text-property start end 'zsh-hist:fontified t)
>         nil))

I'd do:

    (defun zsh-hist-display ()
      `( face bold       ;; Or `face (:weight bold)` if you insist.
         display ,(zsh-hist-convert-unix-timestamp (match-string-no-properties 1))
         zsh-hist:fontified t))

Note that doing it this way lets font-lock know that it has set those
extra properties, so *in theory* it could allow font-lock to remove
those display properties (and only those) automatically.  Font-lock does
not do this, currently, tho. 🙁

>     (define-derived-mode zsh-history-mode text-mode "Zsh History Files"
>       "Major mode for viewing zsh-history files."
>       (add-to-list 'font-lock-extra-managed-props 'zsh-hist:fontified)
>       ;; BUG: this may potentially remove font-lock for other modes that decided to use
>       ;; 'display. Unfortunately it is unclear how to make font-lock only remove 'display
>       ;; for our mode.
>       (add-to-list 'font-lock-extra-managed-props 'display)
>       (font-lock-add-keywords 'zsh-history-mode '(("^: \\([0-9]+\\)" (0 (zsh-hist-display))))))

Use something like:

      (setq-local font-lock-extra-managed-props '(zsh-hist:fontified display))


-- Stefan




  reply	other threads:[~2024-03-11 13:16 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-11  6:41 Major-mode devel: how to drop font-lock 'display when mode is disabled? Konstantin Kharlamov
2024-03-11 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-03-14 21:13 Konstantin Kharlamov
2024-03-15  7:06 ` Eli Zaretskii
2024-03-16 14:16 Konstantin Kharlamov
2024-03-16 14:28 ` Eli Zaretskii
2024-03-16 15:39 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-03-16 15:50   ` Konstantin Kharlamov
2024-03-19 14:50 ` Madhu

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=jwva5n4syxk.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).