help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Konstantin Kharlamov
Subject: Major-mode devel: how to drop font-lock 'display when mode is disabled?
Date: Mon, 11 Mar 2024 09:41:56 +0300
User-agent: Evolution 3.50.4

(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 via email to

[Prev in Thread] Current Thread [Next in Thread]