mu4e-view: fix flycheck warnings

Let's make things clean.
This commit is contained in:
Dirk-Jan C. Binnema 2022-01-15 11:35:47 +02:00
parent a4e5e3adf5
commit e5ead56b2a
1 changed files with 94 additions and 75 deletions

View File

@ -52,9 +52,9 @@
;;; Options ;;; Options
(defcustom mu4e-view-scroll-to-next t (defcustom mu4e-view-scroll-to-next t
"Move to the next message when calling "Move to the next message with `mu4e-view-scroll-up-or-next'.
`mu4e-view-scroll-up-or-next' (typically bound to SPC) when at When at the end of a message, move to the next one, if any.
the end of a message. Otherwise, don't move to the next message." Otherwise, don't move to the next message."
:type 'boolean :type 'boolean
:group 'mu4e-view) :group 'mu4e-view)
@ -214,43 +214,43 @@ Then, display the results."
(mu4e-process-file-through-pipe path cmd))) (mu4e-process-file-through-pipe path cmd)))
(defmacro mu4e~view-in-headers-context (&rest body) (defmacro mu4e~view-in-headers-context (&rest body)
"Evaluate BODY in the context of the headers buffer connected to "Evaluate BODY in the context of the headers buffer."
this view."
`(progn `(progn
(unless (buffer-live-p (mu4e-get-headers-buffer)) (unless (buffer-live-p (mu4e-get-headers-buffer))
(mu4e-error "no headers buffer connected")) (mu4e-error "No headers buffer connected"))
(let* ((msg (mu4e-message-at-point)) (let* ((msg (mu4e-message-at-point))
(docid (mu4e-message-field msg :docid))) (docid (mu4e-message-field msg :docid)))
(unless docid (unless docid
(mu4e-error "message without docid: action is not possible.")) (mu4e-error "Message without docid: action is not possible"))
(with-current-buffer (mu4e-get-headers-buffer) (with-current-buffer (mu4e-get-headers-buffer)
(unless (eq mu4e-split-view 'single-window) (unless (eq mu4e-split-view 'single-window)
(when (get-buffer-window) (when (get-buffer-window)
(select-window (get-buffer-window)))) (select-window (get-buffer-window))))
(if (mu4e~headers-goto-docid docid) (if (mu4e~headers-goto-docid docid)
,@body ,@body
(mu4e-error "cannot find message in headers buffer.")))))) (mu4e-error "Cannot find message in headers buffer"))))))
(defun mu4e-view-headers-next (&optional n) (defun mu4e-view-headers-next (&optional n)
"Move point to the next message header in the headers buffer "Move point to the next message header.
connected with this message view. If this succeeds, return the new If this succeeds, return the new docid. Otherwise, return nil.
docid. Otherwise, return nil. Optionally, takes an integer Optionally, takes an integer N (prefix argument), to the Nth next
N (prefix argument), to the Nth next header." header."
(interactive "P") (interactive "P")
(mu4e~view-in-headers-context (mu4e~view-in-headers-context
(mu4e~headers-move (or n 1)))) (mu4e~headers-move (or n 1))))
(defun mu4e-view-headers-prev (&optional n) (defun mu4e-view-headers-prev (&optional n)
"Move point to the previous message header in the headers buffer "Move point to the previous message header.
connected with this message view. If this succeeds, return the new If this succeeds, return the new docid. Otherwise, return nil.
docid. Otherwise, return nil. Optionally, takes an integer Optionally, takes an integer N (prefix argument), to the Nth
N (prefix argument), to the Nth previous header." previous header."
(interactive "P") (interactive "P")
(mu4e~view-in-headers-context (mu4e~view-in-headers-context
(mu4e~headers-move (- (or n 1))))) (mu4e~headers-move (- (or n 1)))))
(defun mu4e~view-prev-or-next-unread (backwards) (defun mu4e~view-prev-or-next-unread (backwards)
"Move point to the next or previous (when BACKWARDS is non-`nil') "Move point to the next or previous message.
Go to the previous message if BACKWARDS is non-nil.
unread message header in the headers buffer connected with this unread message header in the headers buffer connected with this
message view. If this succeeds, return the new docid. Otherwise, message view. If this succeeds, return the new docid. Otherwise,
return nil." return nil."
@ -264,16 +264,14 @@ return nil."
(mu4e-headers-view-message))) (mu4e-headers-view-message)))
(defun mu4e-view-headers-prev-unread () (defun mu4e-view-headers-prev-unread ()
"Move point to the previous unread message header in the headers "Move point to the previous unread message header.
buffer connected with this message view. If this succeeds, return If this succeeds, return the new docid. Otherwise, return nil."
the new docid. Otherwise, return nil."
(interactive) (interactive)
(mu4e~view-prev-or-next-unread t)) (mu4e~view-prev-or-next-unread t))
(defun mu4e-view-headers-next-unread () (defun mu4e-view-headers-next-unread ()
"Move point to the next unread message header in the headers "Move point to the next unread message header.
buffer connected with this message view. If this succeeds, return If this succeeds, return the new docid. Otherwise, return nil."
the new docid. Otherwise, return nil."
(interactive) (interactive)
(mu4e~view-prev-or-next-unread nil)) (mu4e~view-prev-or-next-unread nil))
@ -290,14 +288,16 @@ bymessage-at-point. The actions are specified in
(funcall actionfunc msg))) (funcall actionfunc msg)))
(defun mu4e-view-mark-pattern () (defun mu4e-view-mark-pattern ()
"Ask user for a kind of mark (move, delete etc.), a field to "Mark messages that match a certain pattern.
Ask user for a kind of mark, (move, delete etc.), a field to
match and a regular expression to match with. Then, mark all match and a regular expression to match with. Then, mark all
matching messages with that mark." matching messages with that mark."
(interactive) (interactive)
(mu4e~view-in-headers-context (mu4e-headers-mark-pattern))) (mu4e~view-in-headers-context (mu4e-headers-mark-pattern)))
(defun mu4e-view-mark-thread (&optional markpair) (defun mu4e-view-mark-thread (&optional markpair)
"Ask user for a kind of mark (move, delete etc.), and apply it "Mark whole thread with a certain mark.
Ask user for a kind of mark (move, delete etc.), and apply it
to all messages in the thread at point in the headers view. The to all messages in the thread at point in the headers view. The
optional MARKPAIR can also be used to provide the mark optional MARKPAIR can also be used to provide the mark
selection." selection."
@ -307,7 +307,8 @@ selection."
(call-interactively 'mu4e-headers-mark-thread)))) (call-interactively 'mu4e-headers-mark-thread))))
(defun mu4e-view-mark-subthread (&optional markpair) (defun mu4e-view-mark-subthread (&optional markpair)
"Ask user for a kind of mark (move, delete etc.), and apply it "Mark subthread with a certain mark.
Ask user for a kind of mark (move, delete etc.), and apply it
to all messages in the subthread at point in the headers view. to all messages in the subthread at point in the headers view.
The optional MARKPAIR can also be used to provide the mark The optional MARKPAIR can also be used to provide the mark
selection." selection."
@ -369,8 +370,8 @@ Add this function to `mu4e-view-mode-hook' to enable this feature."
(defun mu4e-view-scroll-up-or-next () (defun mu4e-view-scroll-up-or-next ()
"Scroll-up the current message. "Scroll-up the current message.
If `mu4e-view-scroll-to-next' is non-nil, and we can't scroll-up If `mu4e-view-scroll-to-next' is non-nil, and we cannot scroll up
anymore, go the next message." any further, go the next message."
(interactive) (interactive)
(condition-case nil (condition-case nil
(scroll-up) (scroll-up)
@ -409,7 +410,7 @@ list."
(mu4e-message "Unmarking needs to be done in the header list view"))) (mu4e-message "Unmarking needs to be done in the header list view")))
(defmacro mu4e~view-defun-mark-for (mark) (defmacro mu4e~view-defun-mark-for (mark)
"Define a function mu4e-view-mark-for-MARK." "Define a function mu4e-view-mark-for- MARK."
(let ((funcname (intern (format "mu4e-view-mark-for-%s" mark))) (let ((funcname (intern (format "mu4e-view-mark-for-%s" mark)))
(docstring (format "Mark the current message for %s." mark))) (docstring (format "Mark the current message for %s." mark)))
`(progn `(progn
@ -454,8 +455,8 @@ list."
(defvar mu4e~view-beginning-of-url-regexp (defvar mu4e~view-beginning-of-url-regexp
"https?\\://\\|mailto:" "https?\\://\\|mailto:"
"Regexp that matches the beginning of http:/https:/mailto: "Regexp that matches the beginning of certain URLs.
URLs; match-string 1 will contain the matched URL, if any.") Match-string 1 will contain the matched URL, if any.")
(defun mu4e~view-browse-url-from-binding (&optional url) (defun mu4e~view-browse-url-from-binding (&optional url)
@ -515,11 +516,11 @@ Also number them so they can be opened using `mu4e-view-go-to-url'."
(defun mu4e~view-get-urls-num (prompt &optional multi) (defun mu4e~view-get-urls-num (prompt &optional multi)
"Ask the user with PROMPT for an URL number for MSG, and ensure "Ask the user with PROMPT for an URL number for MSG.
it is valid. The number is [1..n] for URLs \[0..(n-1)] in the The number is [1..n] for URLs \[0..(n-1)] in the message. If
message. If MULTI is nil, return the number for the URL; MULTI is nil, return the number for the URL; otherwise (MULTI is
otherwise (MULTI is non-nil), accept ranges of URL numbers, as non-nil), accept ranges of URL numbers, as per
per `mu4e-split-ranges-to-numbers', and return the corresponding `mu4e-split-ranges-to-numbers', and return the corresponding
string." string."
(let* ((count (hash-table-count mu4e~view-link-map)) (def)) (let* ((count (hash-table-count mu4e~view-link-map)) (def))
(when (zerop count) (mu4e-error "No links for this message")) (when (zerop count) (mu4e-error "No links for this message"))
@ -533,16 +534,16 @@ string."
nil nil def))))) nil nil def)))))
(defun mu4e-view-go-to-url (&optional multi) (defun mu4e-view-go-to-url (&optional multi)
"Offer to go to url(s). If MULTI (prefix-argument) is nil, go to "Offer to go visit one or more URLs.
a single one, otherwise, offer to go to a range of urls." If MULTI (prefix-argument) is non-nil, offer to go to a range of URLs."
(interactive "P") (interactive "P")
(mu4e~view-handle-urls "URL to visit" (mu4e~view-handle-urls "URL to visit"
multi multi
(lambda (url) (mu4e~view-browse-url-from-binding url)))) (lambda (url) (mu4e~view-browse-url-from-binding url))))
(defun mu4e-view-save-url (&optional multi) (defun mu4e-view-save-url (&optional multi)
"Offer to save urls(s) to the kill-ring. If "Offer to save URLs to the kill ring.
MULTI (prefix-argument) is nil, save a single one, otherwise, offer If MULTI (prefix-argument) is nil, save a single one, otherwise, offer
to save a range of URLs." to save a range of URLs."
(interactive "P") (interactive "P")
(mu4e~view-handle-urls "URL to save" multi (mu4e~view-handle-urls "URL to save" multi
@ -551,35 +552,39 @@ to save a range of URLs."
(mu4e-message "Saved %s to the kill-ring" url)))) (mu4e-message "Saved %s to the kill-ring" url))))
(defun mu4e-view-fetch-url (&optional multi) (defun mu4e-view-fetch-url (&optional multi)
"Offer to fetch (download) urls(s). If MULTI (prefix-argument) is nil, "Offer to fetch (download) URLs.
If MULTI (prefix-argument) is nil,
download a single one, otherwise, offer to fetch a range of download a single one, otherwise, offer to fetch a range of
URLs. The urls are fetched to `mu4e-attachment-dir'." URLs. The urls are fetched to `mu4e-attachment-dir'."
(interactive "P") (interactive "P")
(mu4e~view-handle-urls "URL to fetch" multi (mu4e~view-handle-urls
(lambda (url) "URL to fetch" multi
(let ((target (concat (mu4e~get-attachment-dir url) "/" (lambda (url)
(file-name-nondirectory url)))) (let ((target (concat (mu4e~get-attachment-dir url) "/"
(url-copy-file url target) (file-name-nondirectory url))))
(mu4e-message "Fetched %s -> %s" url target))))) (url-copy-file url target)
(mu4e-message "Fetched %s -> %s" url target)))))
(defun mu4e~view-handle-urls (prompt multi urlfunc) (defun mu4e~view-handle-urls (prompt multi urlfunc)
"If MULTI is nil, apply URLFUNC to a single uri, otherwise, apply "Handle URLs.
If MULTI is nil, apply URLFUNC to a single uri, otherwise, apply
it to a range of uris. PROMPT is the query to present to the user." it to a range of uris. PROMPT is the query to present to the user."
(if multi (if multi
(mu4e~view-handle-multi-urls prompt urlfunc) (mu4e~view-handle-multi-urls prompt urlfunc)
(mu4e~view-handle-single-url prompt urlfunc))) (mu4e~view-handle-single-url prompt urlfunc)))
(defun mu4e~view-handle-single-url (prompt urlfunc &optional num) (defun mu4e~view-handle-single-url (prompt urlfunc &optional num)
"Apply URLFUNC to url NUM in the current message, prompting the "Apply URLFUNC to some URL with NUM in the current message.
user with PROMPT." Prompting the user with PROMPT for the number."
(let* ((num (or num (mu4e~view-get-urls-num prompt))) (let* ((num (or num (mu4e~view-get-urls-num prompt)))
(url (gethash num mu4e~view-link-map))) (url (gethash num mu4e~view-link-map)))
(unless url (mu4e-warn "Invalid number for URL")) (unless url (mu4e-warn "Invalid number for URL"))
(funcall urlfunc url))) (funcall urlfunc url)))
(defun mu4e~view-handle-multi-urls (prompt urlfunc) (defun mu4e~view-handle-multi-urls (prompt urlfunc)
"Apply URLFUNC to a a range of urls in the current message, "Apply URLFUNC to a a range of URLs in the current message.
prompting the user with PROMPT.
Prompting the user with PROMPT for the numbers.
Default is to apply it to all URLs, [1..n], where n is the number Default is to apply it to all URLs, [1..n], where n is the number
of urls. You can type multiple values separated by space, e.g. 1 of urls. You can type multiple values separated by space, e.g. 1
@ -599,7 +604,8 @@ this is the default, you may not need it."
(maphash (lambda (_num uri) (funcall func uri)) mu4e~view-link-map)) (maphash (lambda (_num uri) (funcall func uri)) mu4e~view-link-map))
(defun mu4e-view-message-with-message-id (msgid) (defun mu4e-view-message-with-message-id (msgid)
"View message with message-id MSGID. This (re)creates a "View message with message-id MSGID.
This (re)creates a
headers-buffer with a search for MSGID, then open a view for that headers-buffer with a search for MSGID, then open a view for that
message." message."
(mu4e-search (concat "msgid:" msgid) nil nil t msgid t)) (mu4e-search (concat "msgid:" msgid) nil nil t msgid t))
@ -667,9 +673,9 @@ determine which browser function to use."
(mu4e-message-readable-path msg) nil nil nil t) (mu4e-message-readable-path msg) nil nil nil t)
(run-hooks 'gnus-article-decode-hook) (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-fetch-field 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.
(when (and (bufferp (car parts)) (when (and (bufferp (car parts))
@ -1073,7 +1079,7 @@ The alist uniquely maps the number to the gnus-part."
(defun mu4e-view-save-attachments (&optional arg) (defun mu4e-view-save-attachments (&optional arg)
"Save mime parts from current mu4e gnus view buffer. "Save MIME-parts from current mu4e gnus view buffer.
When helm-mode is enabled provide completion on attachments and When helm-mode is enabled provide completion on attachments and
possibility to mark candidates to save, otherwise completion on possibility to mark candidates to save, otherwise completion on
@ -1081,6 +1087,8 @@ attachments is done with `completing-read-multiple', in this case
use \",\" to separate candidate, completion is provided after use \",\" to separate candidate, completion is provided after
each \",\". each \",\".
ARG is specific for the handler, see below.
Note, currently this does not work well with file names Note, currently this does not work well with file names
containing commas." containing commas."
(interactive "P") (interactive "P")
@ -1098,7 +1106,8 @@ containing commas."
(dolist (part parts) (dolist (part parts)
(let ((fname (or (cdr (assoc 'filename (assoc "attachment" (cdr part)))) (let ((fname (or (cdr (assoc 'filename (assoc "attachment" (cdr part))))
(cl-loop for item in part (cl-loop for item in part
for name = (and (listp item) (assoc-default 'name item)) for name = (and (listp item)
(assoc-default 'name item))
thereis (and (stringp name) name))))) thereis (and (stringp name) name)))))
(when fname (when fname
(push `(,fname . ,(cdr part)) handles) (push `(,fname . ,(cdr part)) handles)
@ -1107,7 +1116,8 @@ containing commas."
(progn (progn
(setq files (let ((helm-comp-read-use-marked t)) (setq files (let ((helm-comp-read-use-marked t))
(funcall compfn "Save part(s): " files)) (funcall compfn "Save part(s): " files))
dir (if arg (read-directory-name "Save to directory: ") mu4e-attachment-dir)) dir (if arg (read-directory-name "Save to directory: ")
mu4e-attachment-dir))
(cl-loop for (f . h) in handles (cl-loop for (f . h) in handles
when (member f files) when (member f files)
do (mm-save-part-to-file do (mm-save-part-to-file
@ -1156,7 +1166,8 @@ containing commas."
(:name "emacs" :handler find-file :receives temp) (:name "emacs" :handler find-file :receives temp)
;; open in this emacs instance, "raw" ;; open in this emacs instance, "raw"
(:name "raw" :handler (lambda (str) (:name "raw" :handler (lambda (str)
(let ((tmpbuf (get-buffer-create " *mu4e-raw-mime*"))) (let ((tmpbuf
(get-buffer-create " *mu4e-raw-mime*")))
(with-current-buffer tmpbuf (with-current-buffer tmpbuf
(insert str) (insert str)
(view-mode) (view-mode)
@ -1195,7 +1206,8 @@ The directory and file are self-destructed."
(let ((temporary-file-directory tmpdir)) (let ((temporary-file-directory tmpdir))
(make-temp-file "mimepart"))))) (make-temp-file "mimepart")))))
(mm-save-part-to-file handle fname) (mm-save-part-to-file handle fname)
(run-at-time "30 sec" nil (lambda () (ignore-errors (delete-directory tmpdir t)))) (run-at-time "30 sec" nil
(lambda () (ignore-errors (delete-directory tmpdir t))))
fname)) fname))
@ -1215,16 +1227,21 @@ If N is not specified, ask for it. For instance, '3 A o' opens
the third MIME-part." the third MIME-part."
(interactive "NNumber of MIME-part: ") (interactive "NNumber of MIME-part: ")
(let* ((parts (mu4e~view-gather-mime-parts)) (let* ((parts (mu4e~view-gather-mime-parts))
(options (mapcar (lambda (action) `(,(plist-get action :name) . ,action)) (options
mu4e-view-mime-part-actions)) (mapcar (lambda (action) `(,(plist-get action :name) . ,action))
(handle (or (cdr-safe (cl-find-if (lambda (part) (eq (car part) n)) parts)) mu4e-view-mime-part-actions))
(mu4e-error "MIME-part %s not found" n))) (handle
(action (or (and options (mu4e-read-option "Action on MIME-part: " options)) (or (cdr-safe (cl-find-if (lambda (part) (eq (car part) n)) parts))
(mu4e-error "No such action"))) (mu4e-error "MIME-part %s not found" n)))
(handler (or (plist-get action :handler) (action
(mu4e-error "No :handler item found for action %S" action))) (or (and options (mu4e-read-option "Action on MIME-part: " options))
(receives (or (plist-get action :receives) (mu4e-error "No such action")))
(mu4e-error "No :receives item found for action %S" action)))) (handler
(or (plist-get action :handler)
(mu4e-error "No :handler item found for action %S" action)))
(receives
(or (plist-get action :receives)
(mu4e-error "No :receives item found for action %S" action))))
(save-excursion (save-excursion
(cond (cond
((functionp handler) ((functionp handler)
@ -1238,12 +1255,14 @@ the third MIME-part."
(t (mu4e-error "Invalid :receive for %S" action)))) (t (mu4e-error "Invalid :receive for %S" action))))
((stringp handler) ((stringp handler)
(cond (cond
((eq receives 'index) (shell-command (concat handler " " (shell-quote-argument n)))) ((eq receives 'index)
(shell-command (concat handler " " (shell-quote-argument n))))
((eq receives 'pipe) (mm-pipe-part handle handler)) ((eq receives 'pipe) (mm-pipe-part handle handler))
((eq receives 'temp) ((eq receives 'temp)
(shell-command (shell-command (concat handler " " (shell-command
(shell-quote-argument (shell-command (concat handler " "
(mu4e~view-mime-part-to-temp-file handle)))))) (shell-quote-argument
(mu4e~view-mime-part-to-temp-file handle))))))
(t (mu4e-error "Invalid action %S" action)))))))) (t (mu4e-error "Invalid action %S" action))))))))
(defun mu4e-view-toggle-html () (defun mu4e-view-toggle-html ()