refactor(mu4e): extract part pickers duplication

Both the attachment and part selection functions generate string
representations of the parts. No need to do the same thing twice.
This commit is contained in:
TEC 2021-09-11 23:27:52 +08:00
parent b489ae6452
commit 4fee7cf953
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C

View file

@ -188,20 +188,14 @@
(defun +mu4e-view-select-attachment () (defun +mu4e-view-select-attachment ()
"Use completing-read to select a single attachment. "Use completing-read to select a single attachment.
Acts like a singular `mu4e-view-save-attachments', without the saving." Acts like a singular `mu4e-view-save-attachments', without the saving."
(let ((parts (mu4e~view-gather-mime-parts)) files) (if-let ((parts (delq nil (mapcar
(dolist (part parts) (lambda (part)
(let ((fname (cdr (assoc 'filename (assoc "attachment" (cdr part)))))) (when (assoc "attachment" (cdr part))
(when fname part))
(push (cons (concat (format "%-2s " (car part)) (mu4e~view-gather-mime-parts))))
(when (featurep 'all-the-icons) (all-the-icons-icon-for-file fname)) (files (+mu4e-part-selectors parts)))
(format " %s " fname) (cdr (assoc (completing-read "Select attachment: " (mapcar #'car files)) files))
(format "(%s)" (file-size-human-readable (with-current-buffer (cadr part) (buffer-size))))) (user-error (mu4e-format "No attached files found"))))
part)
files))))
(if files
(cdr (assoc (completing-read "Select attachment: " (mapcar #'car files))
files))
(user-error (mu4e-format "No attached files found")))))
(defun +mu4e-view-open-attachment () (defun +mu4e-view-open-attachment ()
"Select an attachment, and open it." "Select an attachment, and open it."
@ -209,12 +203,29 @@ Acts like a singular `mu4e-view-save-attachments', without the saving."
(mu4e~view-open-file (mu4e~view-open-file
(mu4e~view-mime-part-to-temp-file (cdr (+mu4e-view-select-attachment))))) (mu4e~view-mime-part-to-temp-file (cdr (+mu4e-view-select-attachment)))))
(defun +mu4e-view-select-part () (defun +mu4e-view-select-mime-part-action ()
(let ((parts (mu4e~view-gather-mime-parts)) partinfo labeledparts "Select a MIME part, and perform an action on it."
maxfnamelen fnamefmt maxsizelen sizefmt) (interactive)
(let ((labeledparts (+mu4e-part-selectors (mu4e~view-gather-mime-parts))))
(if labeledparts
(mu4e-view-mime-part-action
(cadr (assoc (completing-read "Select part: " (mapcar #'car labeledparts))
labeledparts)))
(user-error (mu4e-format "No parts found")))))
(map! :map mu4e-view-mode-map
:ne "A" #'+mu4e-view-select-mime-part-action
:ne "p" #'mu4e-view-save-attachments
:ne "o" #'+mu4e-view-open-attachment)
(defun +mu4e-part-selectors (parts)
"Generate selection strings for PARTS."
(if parts
(let (partinfo labeledparts maxfnamelen fnamefmt maxsizelen sizefmt)
(dolist (part parts) (dolist (part parts)
(push (list :index (car part) (push (list :index (car part)
:mimetype (if (string= "text/plain" (caaddr part)) :mimetype (if (and (string= "text/plain" (caaddr part))
(alist-get 'charset (cdaddr part)))
(format "%s (%s)" (format "%s (%s)"
(caaddr part) (caaddr part)
(alist-get 'charset (cdaddr part))) (alist-get 'charset (cdaddr part)))
@ -238,19 +249,8 @@ Acts like a singular `mu4e-view-save-attachments', without the saving."
(propertize (plist-get pinfo :mimetype) 'face 'font-lock-constant-face)) (propertize (plist-get pinfo :mimetype) 'face 'font-lock-constant-face))
(plist-get pinfo :part)) (plist-get pinfo :part))
labeledparts)) labeledparts))
(cdr (assoc (completing-read "Select part: " (mapcar #'car labeledparts))
labeledparts)))) labeledparts))))
(defun +mu4e-view-select-mime-part-action ()
"Select a MIME part, and perform an action on it."
(interactive)
(mu4e-view-mime-part-action (car (+mu4e-view-select-part))))
(map! :map mu4e-view-mode-map
:ne "A" #'+mu4e-view-select-mime-part-action
:ne "p" #'mu4e-view-save-attachments
:ne "o" #'+mu4e-view-open-attachment))
(map! :localleader (map! :localleader
:map mu4e-compose-mode-map :map mu4e-compose-mode-map
:desc "send and exit" "s" #'message-send-and-exit :desc "send and exit" "s" #'message-send-and-exit