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) (message-generate-headers '(Message-ID)))
(unless (message-fetch-field "Message-ID") ;; older Emacsen (<= 28 perhaps?) won't update the Date
(message-generate-headers '(Message-ID))) ;; if there already is one; so make sure it's gone.
;; older Emacsen (<= 28 perhaps?) won't update the Date (message-remove-header "Date")
;; if there already is one; so make sure it's gone. (message-generate-headers '(Date Subject From))
(message-remove-header "Date")
(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,26 +485,24 @@ 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) (forwarded-from)
(let ((in-reply-to (message-fetch-field "in-reply-to")) (references (message-field-value "references")))
(forwarded-from) (unless in-reply-to
(references (message-fetch-field "references"))) (when references
(unless in-reply-to (with-temp-buffer ;; inspired by `message-shorten-references'.
(when references (insert references)
(with-temp-buffer ;; inspired by `message-shorten-references'. (goto-char (point-min))
(insert references) (let ((refs))
(goto-char (point-min)) (while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
(let ((refs)) (push (match-string 0) refs))
(while (re-search-forward "<[^ <]+@[^ <]+>" nil t) ;; the last will be the first
(push (match-string 0) refs)) (setq forwarded-from (car refs))))))
;; the last will be the first ;; remove the <> and update the flags on the server-side.
(setq forwarded-from (car refs)))))) (when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to))
;; remove the <> and update the flags on the server-side. (mu4e--server-move (match-string 1 in-reply-to) nil "+R-N"))
(when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to)) (when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from))
(mu4e--server-move (match-string 1 in-reply-to) nil "+R-N")) (mu4e--server-move (match-string 1 forwarded-from) nil "+P-N")))))))
(when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from))
(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,29 +542,25 @@ 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 (when (eq mu4e-compose-type 'reply)
(message-narrow-to-headers) (unless (message-field-value "In-Reply-To")
(when (eq mu4e-compose-type 'reply) (message-remove-header "References")))
(unless (message-fetch-field "In-Reply-To") (when use-hard-newlines
(message-remove-header "References"))) (mu4e--send-harden-newlines))
(when use-hard-newlines ;; now handle what happens _after_ sending; typically, draft is gone and
(mu4e--send-harden-newlines)) ;; the sent message appears in sent. Update flags for related messages,
;; now handle what happens _after_ sending; typically, draft is gone and ;; i.e. for Forwarded ('Passed') and Replied messages, try to set the
;; the sent message appears in sent. Update flags for related messages, ;; appropriate flag at the message forwarded or replied-to.
;; i.e. for Forwarded ('Passed') and Replied messages, try to set the (add-hook 'message-sent-hook
;; appropriate flag at the message forwarded or replied-to. (lambda ()
(add-hook 'message-sent-hook (when-let ((fcc-path (message-field-value "Fcc")))
(lambda () (mu4e--set-parent-flags fcc-path)
(save-restriction ;; we end up with a ((buried) buffer here, visiting
(message-narrow-to-headers) ;; the fcc-path; not quite sure why. But let's
(when-let ((fcc-path (message-fetch-field "Fcc"))) ;; get rid of it (#2681)
(mu4e--set-parent-flags fcc-path) (when-let ((buf (find-buffer-visiting fcc-path)))
;; we end up with a ((buried) buffer here, visiting (kill-buffer buf))))
;; the fcc-path; not quite sure why. But let's nil t))
;; get rid of it (#2681)
(when-let ((buf (find-buffer-visiting fcc-path)))
(kill-buffer buf)))))
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) (goto-char (point-min))
(unless (message-fetch-field "Message-Id") (insert (format "Message-Id: <%s>\n" (plist-get msg :message-id))))
(goto-char (point-min))
(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")) (or (and sender (mu4e-personal-or-alternative-address-p
(from (message-fetch-field "sender"))) (car (mail-header-parse-address sender))))
(or (and sender (mu4e-personal-or-alternative-address-p (and from (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address sender)))) (car (mail-header-parse-address from))))))
(and from (mu4e-personal-or-alternative-address-p
(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 (setq-local message-reply-headers
(message-narrow-to-headers) (make-full-mail-header
(setq-local message-reply-headers 0
(make-full-mail-header (or (message-field-value "subject") "none")
0 (or (message-field-value "from") "nobody")
(or (message-fetch-field "subject") "none") (message-field-value "date")
(or (message-fetch-field "from") "nobody") (message-field-value "message-id" t)
(message-fetch-field "date") (message-field-value "references")
(message-fetch-field "message-id" t) 0 0 ""))
(message-fetch-field "references")
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.