Mu4e: make attachments possible without pain

This commit is contained in:
TEC 2020-10-31 19:34:42 +08:00
parent eddbda6650
commit d945987b93
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C
2 changed files with 82 additions and 1 deletions

View file

@ -231,6 +231,82 @@ is tomorrow. With two prefixes, select the deadline."
((= arg 4) "tomorrow")
(t "later"))))))
;;;###autoload
(defun +mu4e/attach-files (&optional files-to-attach)
"When called in a dired buffer, ask for a message to attach the marked files to.
When called in a mu4e:compose or org-msg buffer, open a dired window to select file attachments in,
and enable `dired-mu4e-attach-ctrl-c-ctrl-c' which adds C-c C-c as a keybinding to attach the files.
When otherwise called, open a dired buffer."
;; TODO add ability to attach files (+dirs) as a single (named) archive
(interactive "p")
(mu4e-compose-org-msg-handle-toggle (/= 1 files-to-attach))
(pcase major-mode
((or 'mu4e-compose-mode 'org-msg-edit-mode)
(let ((mail-buffer (current-buffer))
(location (read-file-name "Attach: ")))
(if (not (file-directory-p location))
(pcase major-mode
('mu4e-compose-mode (mail-add-attachment location))
('org-msg-edit-mode (org-msg-attach-attach location)))
(split-window-sensibly)
(with-current-buffer (dired location)
(setq-local dired-mail-buffer mail-buffer)
(dired-mu4e-attach-ctrl-c-ctrl-c 1)))))
('dired-mode
(unless (and files-to-attach (/= 1 files-to-attach))
(setq files-to-attach
(delq nil
(mapcar
;; don't attach directories
(lambda (f) (if (file-directory-p f) nil f))
(nreverse (dired-map-over-marks (dired-get-filename) nil))))))
(if (not files-to-attach)
(progn
(message "No files marked, aborting.")
(kill-buffer-and-window))
(if-let ((mail-target-buffer (bound-and-true-p dired-mail-buffer)))
(progn (kill-buffer-and-window)
(switch-to-buffer mail-target-buffer))
(if (and (+mu4e-current-buffers)
(y-or-n-p "Attach files to existing mail composition buffer? "))
(progn (setf mail-target-buffer
(completing-read "Message: " (+mu4e-current-buffers)))
(kill-buffer-and-window)
(switch-to-buffer mail-target-buffer))
(kill-buffer-and-window)
(mu4e-compose 'new)))
(mapcar
(pcase major-mode
('mu4e-compose-mode #'mail-add-attachment)
('org-msg-edit-mode #'org-msg-attach-attach))
files-to-attach)))
(_
(split-window-sensibly)
(with-current-buffer (call-interactively #'find-file)
(dired-mu4e-attach-ctrl-c-ctrl-c 1)))))
;;;###autoload
(define-minor-mode dired-mu4e-attach-ctrl-c-ctrl-c
"Adds C-c C-c as an keybinding to attach files to a message."
:lighter "attach"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") '+mu4e/attach-files)
map))
;;;###autoload
(defun +mu4e-current-buffers ()
"Return a list of active message buffers."
(let (buffers)
(save-current-buffer
(dolist (buffer (buffer-list t))
(set-buffer buffer)
(when (or (and (derived-mode-p 'message-mode)
(null message-sent-message-via))
(eq major-mode 'org-msg-edit-mode))
(push (buffer-name buffer) buffers))))
(nreverse buffers)))
;;
;; Hooks