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 ()
"Use completing-read to select a single attachment.
Acts like a singular `mu4e-view-save-attachments', without the saving."
(let ((parts (mu4e~view-gather-mime-parts)) files)
(dolist (part parts)
(let ((fname (cdr (assoc 'filename (assoc "attachment" (cdr part))))))
(when fname
(push (cons (concat (format "%-2s " (car part))
(when (featurep 'all-the-icons) (all-the-icons-icon-for-file fname))
(format " %s " fname)
(format "(%s)" (file-size-human-readable (with-current-buffer (cadr part) (buffer-size)))))
part)
files))))
(if files
(cdr (assoc (completing-read "Select attachment: " (mapcar #'car files))
files))
(user-error (mu4e-format "No attached files found")))))
(if-let ((parts (delq nil (mapcar
(lambda (part)
(when (assoc "attachment" (cdr part))
part))
(mu4e~view-gather-mime-parts))))
(files (+mu4e-part-selectors parts)))
(cdr (assoc (completing-read "Select attachment: " (mapcar #'car files)) files))
(user-error (mu4e-format "No attached files found"))))
(defun +mu4e-view-open-attachment ()
"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-mime-part-to-temp-file (cdr (+mu4e-view-select-attachment)))))
(defun +mu4e-view-select-part ()
(let ((parts (mu4e~view-gather-mime-parts)) partinfo labeledparts
maxfnamelen fnamefmt maxsizelen sizefmt)
(defun +mu4e-view-select-mime-part-action ()
"Select a MIME part, and perform an action on it."
(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)
(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)"
(caaddr 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))
(plist-get pinfo :part))
labeledparts))
(cdr (assoc (completing-read "Select part: " (mapcar #'car 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 mu4e-compose-mode-map
:desc "send and exit" "s" #'message-send-and-exit