Removing the dependencies on gnus in eshell/mu4e-attach.

This commit is contained in:
Prashant Sachdeva 2016-08-28 10:03:02 +09:00
parent 9b6a7424df
commit eef00e7263
2 changed files with 39 additions and 38 deletions

View File

@ -163,19 +163,21 @@ For example for bogofile, use \"/usr/bin/bogofilter -Sn < %s\"")
;;; end of spam-filtering functions ;;; end of spam-filtering functions
;;; eshell functions ;;; eshell functions
;; Code for 'gnus-dired-attached' modifed to be run from eshell, allowing files ;; Code for 'gnus-dired-attached' modifed to run from eshell, allowing files to
;; to be attached to an email via mu4e using the eshell. ;; be attached to an email via mu4e using the eshell. Does not depend on gnus.
(defun eshell/mu4e-attach (&rest args) (defun eshell/mu4e-attach (&rest args)
"Attach files to a mu4e message using eshell." "Attach files to a mu4e message using eshell. If no mu4e
buffers found, compose a new message and then attach the file."
(let ((destination nil) (let ((destination nil)
(files-str nil) (files-str nil)
(bufs nil) (bufs nil)
;; Remove directories from the list ;; Remove directories from the list
(files-to-attach (delq nil (mapcar (files-to-attach
(lambda (f) (if (file-directory-p f) (delq nil (mapcar
nil (lambda (f) (if (or (not (file-exists-p f)) (file-directory-p f))
(expand-file-name f))) nil
(eshell-flatten-list (reverse args)))))) (expand-file-name f)))
(eshell-flatten-list (reverse args))))))
;; warn if user tries to attach without any files marked ;; warn if user tries to attach without any files marked
(if (null files-to-attach) (if (null files-to-attach)
(error "No files to attach") (error "No files to attach")
@ -183,43 +185,31 @@ For example for bogofile, use \"/usr/bin/bogofilter -Sn < %s\"")
(mapconcat (mapconcat
(lambda (f) (file-name-nondirectory f)) (lambda (f) (file-name-nondirectory f))
files-to-attach ", ")) files-to-attach ", "))
(setq bufs (gnus-dired-mail-buffers)) (setq bufs (mu4e~active-composition-buffers))
;; set up destination mail composition buffer ;; set up destination mail composition buffer
(if (and bufs (if (and bufs
(y-or-n-p "Attach files to existing mail composition buffer? ")) (y-or-n-p "Attach files to existing mail composition buffer? "))
(setq destination (setq destination
(if (= (length bufs) 1) (if (= (length bufs) 1)
(get-buffer (car bufs)) (get-buffer (car bufs))
(gnus-completing-read "Attach to buffer" (let ((prompt (mu4e-format "%s" "Attach to buffer")))
bufs t nil nil (car bufs)))) (funcall mu4e-completing-read-function prompt
bufs))))
;; setup a new mail composition buffer ;; setup a new mail composition buffer
(let ((mail-user-agent gnus-dired-mail-mode) (if (y-or-n-p "Compose new mail and attach this file? ")
;; The folling comment is verbatim from the source code (progn (mu4e-compose-new)
;; for 'gnus-dired-attach' in 'gnus-dired.el': (setq destination (current-buffer)))))
;; A workaround to prevent Gnus from displaying the Gnus ;; if buffer was found, set buffer to destination buffer, and attach files
;; logo when invoking this command without loading Gnus. (if (not (eq destination 'nil))
;; Gnus demonstrates it when gnus.elc is being loaded if (progn (set-buffer destination)
;; a command of which the name is prefixed with "gnus" (goto-char (point-max)) ;attach at end of buffer
;; causes that autoloading. See the code in question, (while files-to-attach
;; that is the one first found in gnus.el by performing (mml-attach-file (car files-to-attach)
;; `C-s this-command'. (or (mm-default-file-encoding (car files-to-attach))
(this-command (if (eq gnus-dired-mail-mode 'gnus-user-agent) "application/octet-stream") nil)
'gnoose-dired-attach (setq files-to-attach (cdr files-to-attach)))
this-command))) (message "Attached file(s) %s" files-str))
(compose-mail)) (message "No buffer to attach file to.")))))
(setq destination (current-buffer)))
;; set buffer to destination buffer, and attach files
(set-buffer destination)
(goto-char (point-max)) ;attach at end of buffer
(while files-to-attach
(mml-attach-file (car files-to-attach)
(or (mm-default-file-encoding (car files-to-attach))
"application/octet-stream") nil)
(setq files-to-attach (cdr files-to-attach)))
(message "Attached file(s) %s" files-str))))
;;; end of eshell functions ;;; end of eshell functions
(provide 'mu4e-contrib) (provide 'mu4e-contrib)

View File

@ -1280,6 +1280,17 @@ the view and compose modes."
"Quote a string to be used literally in the modeline." "Quote a string to be used literally in the modeline."
(replace-regexp-in-string "%" "%%" str t t)) (replace-regexp-in-string "%" "%%" str t t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e~active-composition-buffers ()
"Return all active mu4e composition buffers"
(let (buffers)
(save-excursion
(dolist (buffer (buffer-list t))
(set-buffer buffer)
(when (eq major-mode 'mu4e-compose-mode)
(push (buffer-name buffer) buffers))))
(nreverse buffers)))
(provide 'mu4e-utils) (provide 'mu4e-utils)
;;; End of mu4e-utils.el ;;; End of mu4e-utils.el