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
(setq mu4e--compose-undo buffer-undo-list)
(save-excursion
(save-restriction
(message-narrow-to-headers)
(unless (message-fetch-field "Message-ID")
(message-generate-headers '(Message-ID)))
;; older Emacsen (<= 28 perhaps?) won't update the Date
;; if there already is one; so make sure it's gone.
(message-remove-header "Date")
(message-generate-headers '(Date Subject From)))
(unless (message-field-value "Message-ID")
(message-generate-headers '(Message-ID)))
;; older Emacsen (<= 28 perhaps?) won't update the Date
;; if there already is one; so make sure it's gone.
(message-remove-header "Date")
(message-generate-headers '(Date Subject From))
(mu4e--delimit-headers 'undelimit))) ;; remove separator
(defvar mu4e--compose-buffer-max-name-length 48)
@ -487,26 +485,24 @@ message buffer."
(let ((buf (find-file-noselect path)))
(when buf
(with-current-buffer buf
(save-restriction
(message-narrow-to-headers)
(let ((in-reply-to (message-fetch-field "in-reply-to"))
(forwarded-from)
(references (message-fetch-field "references")))
(unless in-reply-to
(when references
(with-temp-buffer ;; inspired by `message-shorten-references'.
(insert references)
(goto-char (point-min))
(let ((refs))
(while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
(push (match-string 0) refs))
;; the last will be the first
(setq forwarded-from (car refs))))))
;; remove the <> and update the flags on the server-side.
(when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to))
(mu4e--server-move (match-string 1 in-reply-to) nil "+R-N"))
(when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from))
(mu4e--server-move (match-string 1 forwarded-from) nil "+P-N"))))))))
(let ((in-reply-to (message-field-value "in-reply-to"))
(forwarded-from)
(references (message-field-value "references")))
(unless in-reply-to
(when references
(with-temp-buffer ;; inspired by `message-shorten-references'.
(insert references)
(goto-char (point-min))
(let ((refs))
(while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
(push (match-string 0) refs))
;; the last will be the first
(setq forwarded-from (car refs))))))
;; remove the <> and update the flags on the server-side.
(when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to))
(mu4e--server-move (match-string 1 in-reply-to) nil "+R-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()
"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.
;; This allows the user to effectively start a new message-thread by
;; removing the In-Reply-To header.
(save-restriction
(message-narrow-to-headers)
(when (eq mu4e-compose-type 'reply)
(unless (message-fetch-field "In-Reply-To")
(message-remove-header "References")))
(when use-hard-newlines
(mu4e--send-harden-newlines))
;; now handle what happens _after_ sending; typically, draft is gone and
;; the sent message appears in sent. Update flags for related messages,
;; i.e. for Forwarded ('Passed') and Replied messages, try to set the
;; appropriate flag at the message forwarded or replied-to.
(add-hook 'message-sent-hook
(lambda ()
(save-restriction
(message-narrow-to-headers)
(when-let ((fcc-path (message-fetch-field "Fcc")))
(mu4e--set-parent-flags fcc-path)
;; we end up with a ((buried) buffer here, visiting
;; the fcc-path; not quite sure why. But let's
;; get rid of it (#2681)
(when-let ((buf (find-buffer-visiting fcc-path)))
(kill-buffer buf)))))
nil t)))
(when (eq mu4e-compose-type 'reply)
(unless (message-field-value "In-Reply-To")
(message-remove-header "References")))
(when use-hard-newlines
(mu4e--send-harden-newlines))
;; now handle what happens _after_ sending; typically, draft is gone and
;; the sent message appears in sent. Update flags for related messages,
;; i.e. for Forwarded ('Passed') and Replied messages, try to set the
;; appropriate flag at the message forwarded or replied-to.
(add-hook 'message-sent-hook
(lambda ()
(when-let ((fcc-path (message-field-value "Fcc")))
(mu4e--set-parent-flags fcc-path)
;; we end up with a ((buried) buffer here, visiting
;; the fcc-path; not quite sure why. But let's
;; get rid of it (#2681)
(when-let ((buf (find-buffer-visiting fcc-path)))
(kill-buffer buf))))
nil t))
;;; Crypto
(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
;; from mu.
(mu4e--delimit-headers)
(save-restriction
(message-narrow-to-headers)
(unless (message-fetch-field "Message-Id")
(goto-char (point-min))
(insert (format "Message-Id: <%s>\n" (plist-get msg :message-id)))))
(unless (message-field-value "Message-Id")
(goto-char (point-min))
(insert (format "Message-Id: <%s>\n" (plist-get msg :message-id))))
(mu4e--delimit-headers 'undelimit)
(ignore-errors (run-hooks 'gnus-article-decode-hook))
(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.
Is this address yours?"
(if (mu4e-running-p)
(save-restriction
(message-narrow-to-headers)
(let ((sender (message-fetch-field "from"))
(from (message-fetch-field "sender")))
(or (and sender (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address sender))))
(and from (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address from)))))))
(let ((sender (message-field-value "from"))
(from (message-field-value "sender")))
(or (and sender (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address sender))))
(and from (mu4e-personal-or-alternative-address-p
(car (mail-header-parse-address from))))))
(apply func args)))
(defun mu4e--compose-setup-post (compose-type &optional parent)
@ -986,17 +974,15 @@ must be from current user, as determined through
(mu4e--delimit-headers)
;; message-forward expects message-reply-headers to be set up; here we
;; only need message-id & references, rest is for completeness.
(save-restriction
(message-narrow-to-headers)
(setq-local message-reply-headers
(make-full-mail-header
0
(or (message-fetch-field "subject") "none")
(or (message-fetch-field "from") "nobody")
(message-fetch-field "date")
(message-fetch-field "message-id" t)
(message-fetch-field "references")
0 0 "")))
(setq-local message-reply-headers
(make-full-mail-header
0
(or (message-field-value "subject") "none")
(or (message-field-value "from") "nobody")
(message-field-value "date")
(message-field-value "message-id" t)
(message-field-value "references")
0 0 ""))
(mu4e--delimit-headers 'undelimit)
(set-buffer-modified-p nil)
(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
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
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."
(with-temp-buffer
(insert (mu4e--decoded-message msg 'headers-only))
(message-fetch-field hdr first)))
(message-field-value hdr first)))
;;;
(provide 'mu4e-message)
;;; 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))
(let ((header (unless skip-headers
(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))))
(parts (mm-dissect-buffer t t)))
;; If singlepart, enforce a list.