mu4e: use message-field-value

Simplify

...instead of
save-restriction/message-narrow-to-headers/message-fetch-field.
This commit is contained in:
Dirk-Jan C. Binnema
2024-04-16 19:03:08 +03:00
parent a2f08a77f5
commit 4f72e2656d
3 changed files with 65 additions and 78 deletions

View File

@ -441,14 +441,12 @@ If MSGPATH is nil, do nothing."
;; - (temporarily) remove the mail-header separator ;; - (temporarily) remove the mail-header separator
(setq mu4e--compose-undo buffer-undo-list) (setq mu4e--compose-undo buffer-undo-list)
(save-excursion (save-excursion
(save-restriction (unless (message-field-value "Message-ID")
(message-narrow-to-headers)
(unless (message-fetch-field "Message-ID")
(message-generate-headers '(Message-ID))) (message-generate-headers '(Message-ID)))
;; older Emacsen (<= 28 perhaps?) won't update the Date ;; older Emacsen (<= 28 perhaps?) won't update the Date
;; if there already is one; so make sure it's gone. ;; if there already is one; so make sure it's gone.
(message-remove-header "Date") (message-remove-header "Date")
(message-generate-headers '(Date Subject From))) (message-generate-headers '(Date Subject From))
(mu4e--delimit-headers 'undelimit))) ;; remove separator (mu4e--delimit-headers 'undelimit))) ;; remove separator
(defvar mu4e--compose-buffer-max-name-length 48) (defvar mu4e--compose-buffer-max-name-length 48)
@ -487,11 +485,9 @@ message buffer."
(let ((buf (find-file-noselect path))) (let ((buf (find-file-noselect path)))
(when buf (when buf
(with-current-buffer buf (with-current-buffer buf
(save-restriction (let ((in-reply-to (message-field-value "in-reply-to"))
(message-narrow-to-headers)
(let ((in-reply-to (message-fetch-field "in-reply-to"))
(forwarded-from) (forwarded-from)
(references (message-fetch-field "references"))) (references (message-field-value "references")))
(unless in-reply-to (unless in-reply-to
(when references (when references
(with-temp-buffer ;; inspired by `message-shorten-references'. (with-temp-buffer ;; inspired by `message-shorten-references'.
@ -506,7 +502,7 @@ message buffer."
(when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to)) (when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to))
(mu4e--server-move (match-string 1 in-reply-to) nil "+R-N")) (mu4e--server-move (match-string 1 in-reply-to) nil "+R-N"))
(when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from)) (when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from))
(mu4e--server-move (match-string 1 forwarded-from) nil "+P-N")))))))) (mu4e--server-move (match-string 1 forwarded-from) nil "+P-N")))))))
(defun mu4e--compose-after-save() (defun mu4e--compose-after-save()
"Function called immediately after the draft buffer is saved." "Function called immediately after the draft buffer is saved."
@ -546,10 +542,8 @@ appropriate flag at the message forwarded or replied-to."
;; Remove References: if In-Reply-To: is missing. ;; Remove References: if In-Reply-To: is missing.
;; This allows the user to effectively start a new message-thread by ;; This allows the user to effectively start a new message-thread by
;; removing the In-Reply-To header. ;; removing the In-Reply-To header.
(save-restriction
(message-narrow-to-headers)
(when (eq mu4e-compose-type 'reply) (when (eq mu4e-compose-type 'reply)
(unless (message-fetch-field "In-Reply-To") (unless (message-field-value "In-Reply-To")
(message-remove-header "References"))) (message-remove-header "References")))
(when use-hard-newlines (when use-hard-newlines
(mu4e--send-harden-newlines)) (mu4e--send-harden-newlines))
@ -559,16 +553,14 @@ appropriate flag at the message forwarded or replied-to."
;; appropriate flag at the message forwarded or replied-to. ;; appropriate flag at the message forwarded or replied-to.
(add-hook 'message-sent-hook (add-hook 'message-sent-hook
(lambda () (lambda ()
(save-restriction (when-let ((fcc-path (message-field-value "Fcc")))
(message-narrow-to-headers)
(when-let ((fcc-path (message-fetch-field "Fcc")))
(mu4e--set-parent-flags fcc-path) (mu4e--set-parent-flags fcc-path)
;; we end up with a ((buried) buffer here, visiting ;; we end up with a ((buried) buffer here, visiting
;; the fcc-path; not quite sure why. But let's ;; the fcc-path; not quite sure why. But let's
;; get rid of it (#2681) ;; get rid of it (#2681)
(when-let ((buf (find-buffer-visiting fcc-path))) (when-let ((buf (find-buffer-visiting fcc-path)))
(kill-buffer buf))))) (kill-buffer buf))))
nil t))) nil t))
;;; Crypto ;;; Crypto
(defun mu4e--compose-setup-crypto (parent compose-type) (defun mu4e--compose-setup-crypto (parent compose-type)
@ -718,11 +710,9 @@ With HEADERS-ONLY non-nil, only include the headers part."
;; in rare (broken) case, if a message-id is missing use the generated one ;; in rare (broken) case, if a message-id is missing use the generated one
;; from mu. ;; from mu.
(mu4e--delimit-headers) (mu4e--delimit-headers)
(save-restriction (unless (message-field-value "Message-Id")
(message-narrow-to-headers)
(unless (message-fetch-field "Message-Id")
(goto-char (point-min)) (goto-char (point-min))
(insert (format "Message-Id: <%s>\n" (plist-get msg :message-id))))) (insert (format "Message-Id: <%s>\n" (plist-get msg :message-id))))
(mu4e--delimit-headers 'undelimit) (mu4e--delimit-headers 'undelimit)
(ignore-errors (run-hooks 'gnus-article-decode-hook)) (ignore-errors (run-hooks 'gnus-article-decode-hook))
(buffer-substring-no-properties (point-min) (point-max)))) (buffer-substring-no-properties (point-min) (point-max))))
@ -822,14 +812,12 @@ This is mu4e's version of `message-hidden-headers'.")
FUNC is the original function, and ARGS are its arguments. FUNC is the original function, and ARGS are its arguments.
Is this address yours?" Is this address yours?"
(if (mu4e-running-p) (if (mu4e-running-p)
(save-restriction (let ((sender (message-field-value "from"))
(message-narrow-to-headers) (from (message-field-value "sender")))
(let ((sender (message-fetch-field "from"))
(from (message-fetch-field "sender")))
(or (and sender (mu4e-personal-or-alternative-address-p (or (and sender (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address sender)))) (car (mail-header-parse-address sender))))
(and from (mu4e-personal-or-alternative-address-p (and from (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address from))))))) (car (mail-header-parse-address from))))))
(apply func args))) (apply func args)))
(defun mu4e--compose-setup-post (compose-type &optional parent) (defun mu4e--compose-setup-post (compose-type &optional parent)
@ -986,17 +974,15 @@ must be from current user, as determined through
(mu4e--delimit-headers) (mu4e--delimit-headers)
;; message-forward expects message-reply-headers to be set up; here we ;; message-forward expects message-reply-headers to be set up; here we
;; only need message-id & references, rest is for completeness. ;; only need message-id & references, rest is for completeness.
(save-restriction
(message-narrow-to-headers)
(setq-local message-reply-headers (setq-local message-reply-headers
(make-full-mail-header (make-full-mail-header
0 0
(or (message-fetch-field "subject") "none") (or (message-field-value "subject") "none")
(or (message-fetch-field "from") "nobody") (or (message-field-value "from") "nobody")
(message-fetch-field "date") (message-field-value "date")
(message-fetch-field "message-id" t) (message-field-value "message-id" t)
(message-fetch-field "references") (message-field-value "references")
0 0 ""))) 0 0 ""))
(mu4e--delimit-headers 'undelimit) (mu4e--delimit-headers 'undelimit)
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(message-forward))))) (message-forward)))))

View File

@ -232,7 +232,8 @@ If MSG is nil, use `mu4e-message-at-point'."
If the header appears multiple times, the field values are If the header appears multiple times, the field values are
concatenated, unless FIRST is non-nil, in which case only the concatenated, unless FIRST is non-nil, in which case only the
first value is returned. See `message-fetch-field' for details. first value is returned. See `message-field-value' and
`nessage-fetch-field' for details.
Note: this loads the full message file such that any available Note: this loads the full message file such that any available
message header can be used. If the header is part of the MSG message header can be used. If the header is part of the MSG
@ -240,7 +241,7 @@ plist, it is much more efficient to get the information from that
plist." plist."
(with-temp-buffer (with-temp-buffer
(insert (mu4e--decoded-message msg 'headers-only)) (insert (mu4e--decoded-message msg 'headers-only))
(message-fetch-field hdr first))) (message-field-value hdr first)))
;;; ;;;
(provide 'mu4e-message) (provide 'mu4e-message)
;;; mu4e-message.el ends here ;;; mu4e-message.el ends here

View File

@ -685,7 +685,7 @@ determine which browser function to use."
(ignore-errors (run-hooks 'gnus-article-decode-hook)) (ignore-errors (run-hooks 'gnus-article-decode-hook))
(let ((header (unless skip-headers (let ((header (unless skip-headers
(cl-loop for field in '("from" "to" "cc" "date" "subject") (cl-loop for field in '("from" "to" "cc" "date" "subject")
when (message-fetch-field field) when (message-field-value field)
concat (format "%s: %s\n" (capitalize field) it)))) concat (format "%s: %s\n" (capitalize field) it))))
(parts (mm-dissect-buffer t t))) (parts (mm-dissect-buffer t t)))
;; If singlepart, enforce a list. ;; If singlepart, enforce a list.