mu4e-view: extract shared headers function refact

Extract the header insertion code from mu4e--view-gnus-display-mime into
a new mu4e--view-insert-headers function.

Add a fallback label in mu4e--view-gnus-insert-header for fields not in
mu4e-header-info, such as :user-agent.
This commit is contained in:
Dirk-Jan C. Binnema
2026-04-07 20:18:41 +03:00
committed by Seth Ladygo
parent f9760e8f98
commit 65b3dbb5c9

View File

@ -660,15 +660,12 @@ Note that for some messages, this can trigger high CPU load."
(setq gnus-article-emulate-mime (not gnus-article-emulate-mime)) (setq gnus-article-emulate-mime (not gnus-article-emulate-mime))
(mu4e-view-refresh)) (mu4e-view-refresh))
(defun mu4e--view-gnus-display-mime (msg) (defun mu4e--view-insert-headers (msg &optional raw-headers)
"Like `gnus-display-mime', but include mu4e headers to MSG." "Insert mu4e headers for MSG into the current buffer at point.
(lambda (&optional ihandles) RAW-HEADERS, when non-nil, is an alist of (FIELD . VALUE) strings
(gnus-display-mime ihandles) for standard RFC headers (From, To, Cc, etc.) that should be
(unless ihandles rendered directly. When nil, those fields are left for Gnus to
(save-restriction render. After inserting, highlight the headers."
(article-goto-body)
(forward-line -1)
(narrow-to-region (point) (point))
(dolist (field mu4e-view-fields) (dolist (field mu4e-view-fields)
(let ((fieldval (mu4e-message-field msg field))) (let ((fieldval (mu4e-message-field msg field)))
(pcase field (pcase field
@ -679,29 +676,51 @@ Note that for some messages, this can trigger high CPU load."
(mu4e--view-gnus-insert-header field (format "<%s>" msgid)))) (mu4e--view-gnus-insert-header field (format "<%s>" msgid))))
(':mailing-list (':mailing-list
(let ((list (plist-get msg :list))) (let ((list (plist-get msg :list)))
(if list (mu4e-get-mailing-list-shortname list) ""))) (when list
(mu4e--view-gnus-insert-header
field (mu4e-get-mailing-list-shortname list)))))
((or ':flags ':labels ':tags) ((or ':flags ':labels ':tags)
(let ((items (mapconcat (lambda (item) (let ((items (mapconcat (lambda (item)
(if (symbolp item) (if (symbolp item)
(symbol-name item) (symbol-name item)
item)) fieldval ", "))) item))
fieldval ", ")))
(mu4e--view-gnus-insert-header field items))) (mu4e--view-gnus-insert-header field items)))
(':size (mu4e--view-gnus-insert-header (':size (mu4e--view-gnus-insert-header
field (mu4e-display-size fieldval))) field (mu4e-display-size fieldval)))
((or ':subject ':to ':from ':cc ':bcc ':from-or-to ((or ':subject ':to ':from ':cc ':bcc ':from-or-to
':user-agent ':date ':attachments ':user-agent ':date)
':signature ':decryption)) ;; handled by Gnus ;; Standard fields: insert from raw-headers if available,
;; otherwise they are handled by Gnus.
(when-let* ((raw (and raw-headers (cdr (assq field raw-headers)))))
(mu4e--view-gnus-insert-header field raw)))
((or ':attachments ':signature ':decryption)) ;; skip
(_ (_
(mu4e--view-gnus-insert-header-custom msg field))))) (mu4e--view-gnus-insert-header-custom msg field)))))
;; Highlight the header block we just inserted
(let ((gnus-treatment-function-alist (let ((gnus-treatment-function-alist
'((gnus-treat-highlight-headers '((gnus-treat-highlight-headers
gnus-article-highlight-headers)))) gnus-article-highlight-headers))))
(gnus-treat-article 'head)))))) (gnus-treat-article 'head)))
(defun mu4e--view-gnus-display-mime (msg)
"Like `gnus-display-mime', but include mu4e headers to MSG."
(lambda (&optional ihandles)
(gnus-display-mime ihandles)
(unless ihandles
(save-restriction
(article-goto-body)
(forward-line -1)
(narrow-to-region (point) (point))
(mu4e--view-insert-headers msg)))))
(defun mu4e--view-gnus-insert-header (field val) (defun mu4e--view-gnus-insert-header (field val)
"Insert a header FIELD with value VAL." "Insert a header FIELD with value VAL."
(let* ((info (cdr (assoc field mu4e-header-info))) (let* ((info (cdr (assoc field mu4e-header-info)))
(key (plist-get info :name)) (key (or (plist-get info :name)
;; Fallback for fields not in mu4e-header-info
;; (e.g. :user-agent): derive from the keyword name.
(capitalize (substring (symbol-name field) 1))))
(help (plist-get info :help))) (help (plist-get info :help)))
(if (and val (> (length val) 0)) (if (and val (> (length val) 0))
(insert (propertize (concat key ":") 'help-echo help) (insert (propertize (concat key ":") 'help-echo help)