unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: Major-mode devel: how to drop font-lock 'display when mode is disabled?
@ 2024-03-14 21:13 Konstantin Kharlamov
  2024-03-15  7:06 ` Eli Zaretskii
  0 siblings, 1 reply; 9+ messages in thread
From: Konstantin Kharlamov @ 2024-03-14 21:13 UTC (permalink / raw)
  To: monnier, help-gnu-emacs, enometh

Oh, thank you very much Stefan and Madhu for your replies!

Something odd is going on with the list: I did not get Stefan's email
(not even in the "spam" folder) and only accidentally found it while
looking at the archive. At the same time I got a reply from Madhu (who
mentioned a pre-existing `epoch-view-mode`) and when I wanted to refer
to this email, I found it is not on the list. Odd 🤷‍♂️

Anyway, thank you both for your replies!



^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: Major-mode devel: how to drop font-lock 'display when mode is disabled?
@ 2024-03-16 14:16 Konstantin Kharlamov
  2024-03-16 14:28 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Konstantin Kharlamov @ 2024-03-16 14:16 UTC (permalink / raw)
  To: eliz, help-gnu-emacs

Eli Zaretskii wrote:
>> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
>> Date: Fri, 15 Mar 2024 00:13:28 +0300
>>
>> Something odd is going on with the list: I did not get Stefan's email
>> (not even in the "spam" folder) and only accidentally found it while
>> looking at the archive. At the same time I got a reply from Madhu
>(who
>> mentioned a pre-existing `epoch-view-mode`) and when I wanted to
>refer
>> to this email, I found it is not on the list. Odd 🤷‍♂️
>
>Stefan replied only to the list, and Madhu probably replied to you
>personally.

Madhu replied both to me and to the list, which is why I'm wondering their message is not in the archive.

Stefan may have replied to the list only (I don't know for sure tho, because typically mailing list etiquette implies "reply to all"¹ and for the safe case I also asked to be kept in CC), although I kind of hoped the ML software would still send an email to all people in the thread.

1: https://subspace.kernel.org/etiquette.html#always-reply-to-all



^ permalink raw reply	[flat|nested] 9+ messages in thread
* Major-mode devel: how to drop font-lock 'display when mode is disabled?
@ 2024-03-11  6:41 Konstantin Kharlamov
  2024-03-11 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 9+ messages in thread
From: Konstantin Kharlamov @ 2024-03-11  6:41 UTC (permalink / raw)
  To: help-gnu-emacs

(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



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

end of thread, other threads:[~2024-03-19 14:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-14 21:13 Major-mode devel: how to drop font-lock 'display when mode is disabled? Konstantin Kharlamov
2024-03-15  7:06 ` Eli Zaretskii
  -- strict thread matches above, loose matches on Subject: below --
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
2024-03-11  6:41 Konstantin Kharlamov
2024-03-11 13:16 ` Stefan Monnier via Users list for the GNU Emacs text editor

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