unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* 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

* Re: 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, 0 replies; 9+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-03-11 13:16 UTC (permalink / raw)
  To: help-gnu-emacs

> 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




^ 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-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-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
  0 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2024-03-15  7:06 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ 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

* 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
  2024-03-16 15:39 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-03-19 14:50 ` Madhu
  2 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2024-03-16 14:28 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Konstantin Kharlamov <Hi-Angel@yandex.ru>
> Date: Sat, 16 Mar 2024 17:16:20 +0300
> 
> Eli Zaretskii wrote:
> >
> >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.

Then you'd need to talk to the list administrator (no idea who that
is).



^ 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
@ 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
  2 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-03-16 15:39 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Konstantin Kharlamov

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

No, I'm reading it in the `gmane.emacs.help` newsgroup, where the
tradition is to reply only to the newsgroup.
[ I manually added the `Cc:` this time.  Sorry, I missed your request
  to be in Cc last time.  And indeed, maybe I should always put the
  author in Cc: for that newsgroup.  ]


        Stefan




^ 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 15:39 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-03-16 15:50   ` Konstantin Kharlamov
  0 siblings, 0 replies; 9+ messages in thread
From: Konstantin Kharlamov @ 2024-03-16 15:50 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

(duplicating my reply, because it's only gotten to Stefan due to "TO"
field having some "undisclosed-recipients", which I presume was
supposed to be "help-gnu-emacs")

On Sat, 2024-03-16 at 11:39 -0400, Stefan Monnier wrote:
> The following message is a courtesy copy of an article
> that has been posted to gmane.emacs.help as well.
> 
> > 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.
> 
> No, I'm reading it in the `gmane.emacs.help` newsgroup, where the
> tradition is to reply only to the newsgroup.
> [ I manually added the `Cc:` this time.  Sorry, I missed your request
>   to be in Cc last time.  And indeed, maybe I should always put the
>   author in Cc: for that newsgroup.  ]


Ah, it's okay, and thank you for explanation! And an interesting point
about newsgroup, TBH I didn't even know about these things before you
just mentioned 😅



^ 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
  2024-03-16 15:39 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-03-19 14:50 ` Madhu
  2 siblings, 0 replies; 9+ messages in thread
From: Madhu @ 2024-03-19 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

* Konstantin Kharlamov <28ca7cfcb053724350a36bc57f15750f61d0980a.camel@yandex.ru> :
Wrote on Sat, 16 Mar 2024 17:16:20 +0300:

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

Sorry for the confusion.  When I replied by mail (since you requested
it), I also CC'ed the list, but I botched up the subject line. My
message is on the archive but it is under a different subject line,
prepending "Subject:"

I can access it on gmane gnus.emacs.help (via j on gnus)
 <20240312.201531.327797025962741547.enometh@meer.net>

Subject: "Subject: Re: Major-mode devel: how to drop font-lock 'display
when mode is disabled?"

and it's on the list archives here
https://lists.gnu.org/archive/html/help-gnu-emacs/2024-03/msg00076.html
under
https://lists.gnu.org/archive/html/help-gnu-emacs/2024-03/threads.html

> 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

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