Merge pull request #579 from halbtuerke/save-multiple-attachments-without-asking

mu4e: Add option for saving multiple attachments into same directory.
This commit is contained in:
Dirk-Jan C. Binnema
2015-03-17 19:14:46 +02:00

View File

@ -90,6 +90,12 @@ support, and `mu4e-view-show-images' is non-nil."
`mu4e-view-scroll-up-or-next' (typically bound to SPC) when at the `mu4e-view-scroll-up-or-next' (typically bound to SPC) when at the
end of a message. Otherwise, don't move to the next message.") end of a message. Otherwise, don't move to the next message.")
(defcustom mu4e-save-multiple-attachments-without-asking nil
"If non-nil, saving multiple attachments asks once for a
directory and saves all attachments in the chosen directory."
:type 'boolean
:group 'mu4e-view)
(defvar mu4e-view-actions (defvar mu4e-view-actions
'( ("capture message" . mu4e-action-capture-message) '( ("capture message" . mu4e-action-capture-message)
("view as pdf" . mu4e-action-view-as-pdf)) ("view as pdf" . mu4e-action-view-as-pdf))
@ -1018,6 +1024,15 @@ number ATTNUM."
(expand-file-name fname fpath) (expand-file-name fname fpath)
fpath))) fpath)))
(defun mu4e~view-request-attachments-dir (path)
"Ask the user where to save multiple attachments (default is PATH)."
(let ((fpath (expand-file-name
(read-directory-name
(mu4e-format "Save in directory ")
path nil nil nil) path)))
(if (file-directory-p fpath)
fpath)))
(defun mu4e-view-save-attachment-single (&optional msg attnum) (defun mu4e-view-save-attachment-single (&optional msg attnum)
"Save attachment number ATTNUM from MSG. "Save attachment number ATTNUM from MSG.
If MSG is nil use the message returned by `message-at-point'. If MSG is nil use the message returned by `message-at-point'.
@ -1057,8 +1072,23 @@ attachments, but as this is the default, you may not need it."
"Attachment number range (or 'a' for 'all')" msg t)) "Attachment number range (or 'a' for 'all')" msg t))
(count (hash-table-count mu4e~view-attach-map)) (count (hash-table-count mu4e~view-attach-map))
(attachnums (mu4e-split-ranges-to-numbers attachstr count))) (attachnums (mu4e-split-ranges-to-numbers attachstr count)))
(if mu4e-save-multiple-attachments-without-asking
(let* ((path (concat (mu4e~get-attachment-dir) "/"))
(attachdir (mu4e~view-request-attachments-dir path)))
(dolist (num attachnums) (dolist (num attachnums)
(mu4e-view-save-attachment-single msg num)))) (let* ((att (mu4e~view-get-attach msg num))
(fname (plist-get att :name))
(index (plist-get att :index))
(retry t))
(while retry
(setq fpath (expand-file-name (concat attachdir fname) path))
(setq retry
(and (file-exists-p fpath)
(not (y-or-n-p (mu4e-format "Overwrite '%s'?" fpath))))))
(mu4e~proc-extract
'save (mu4e-message-field msg :docid) index mu4e-decryption-policy fpath))))
(dolist (num attachnums)
(mu4e-view-save-attachment-single msg num)))))
(defun mu4e-view-save-attachment (&optional multi) (defun mu4e-view-save-attachment (&optional multi)
"Offer to save attachment(s). "Offer to save attachment(s).