unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
To: help-gnu-emacs@gnu.org
Subject: Major-mode devel: how to drop font-lock 'display when mode is disabled?
Date: Mon, 11 Mar 2024 09:41:56 +0300	[thread overview]
Message-ID: <a5bd270b9424bcc130ec11c3c6c772773a1c7d05.camel@yandex.ru> (raw)

(please keep me CC'ed)

Someone on emacs.stackexchange wanted a mode to display timestamps in
`.zsh_history` file in a human-redable form¹.

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.

I've had very hard time finding any documentation or posts that would
explain how's that supposed to work. My gut feeling is that when I call
`(font-lock-add-keywords)` with the mode name as 2-nd arg, that allows
font-lock to associate display changes to the mode name and thus remove
them once the mode disappears. But it doesn't work for me that way.

As a workaround I have the following call:

	(add-to-list 'font-lock-extra-managed-props 'display)

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

For reference, the code from my answer on emacs.stackexchange is below:

    ;;; --- zsh_history highlighting -*- lexical-binding: t -*-
    (defvar zsh-hist-display-date-format "%a %d %b %Y %T %Z"
      "Date format for displaying the timestamp (see `man date')")

    (defun zsh-hist-convert-unix-timestamp (timestamp)
      (format-time-string zsh-hist-display-date-format
                          (seconds-to-time (string-to-number timestamp))))

    (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))

    (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))))))

    ;;;###autoload
    (add-to-list 'auto-mode-alist '("zsh_history" . zsh-history-mode))

1: https://emacs.stackexchange.com/q/80674/2671#view-timestamps-in-human-readable-format



             reply	other threads:[~2024-03-11  6:41 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-11  6:41 Konstantin Kharlamov [this message]
2024-03-11 13:16 ` Major-mode devel: how to drop font-lock 'display when mode is disabled? Stefan Monnier via Users list for the GNU Emacs text editor
  -- 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=a5bd270b9424bcc130ec11c3c6c772773a1c7d05.camel@yandex.ru \
    --to=hi-angel@yandex.ru \
    --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.
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).