I use the mu4e package to read my emails. However, I also use the ivy completion framework, which includes file completion.

Using both packages together is a bit of a problem though. mu4e uses its own file-completion dialog. That's probably fine, but it's different enough that it confuses me when I try to attach files to outgoing messages.

Is there a way to use ivy's completion features for selecting files in mu4e?


It appears that mu4e uses its own file selection function, which is hard-coded. Which means you have to rewrite the functions in question after loading mu4e. The following code accomplishes this for adding attachments to outgoing files:

(eval-after-load 'mu4e-compose
  '(defun tws-attach-file (file &optional type description disposition)
     "Attach a file to the outgoing MIME message.
The file is not inserted or encoded until you send the message with
`\\[message-send-and-exit]' or `\\[message-send]' in Message mode,
or `\\[mail-send-and-exit]' or `\\[mail-send]' in Mail mode.

FILE is the name of the file to attach.  TYPE is its
content-type, a string of the form \"type/subtype\".  DESCRIPTION
is a one-line description of the attachment.  The DISPOSITION
specifies how the attachment is intended to be displayed.  It can
be either \"inline\" (displayed automatically within the message
body) or \"attachment\" (separate from the body).

If given a prefix interactively, no prompting will be done for
the TYPE, DESCRIPTION or DISPOSITION values.  Instead defaults
will be computed and used."
      ;;; Change added here:
      (let* ((file (ivy-read "Attach file: " #'read-file-name-internal))
      ;;; End of altered code
         (type (if current-prefix-arg
               (or (mm-default-file-encoding file)
             (mml-minibuffer-read-type file)))
         (description (if current-prefix-arg
         (disposition (if current-prefix-arg
                  (mml-content-disposition type file)
                (mml-minibuffer-read-disposition type nil file))))
        (list file type description disposition)))
     ;; If in the message header, attach at the end and leave point unchanged.
     (let ((head (unless (message-in-body-p) (point))))
       (if head (goto-char (point-max)))
       (mml-insert-empty-tag 'part
                 'type type
                 ;; icicles redefines read-file-name and returns a
                 ;; string w/ text properties :-/
                 'filename (substring-no-properties file)
                 'disposition (or disposition "attachment")
                 'description description)
       ;; When using Mail mode, make sure it does the mime encoding
       ;; when you send the message.
       (or (eq mail-user-agent 'message-user-agent)
       (setq mail-encode-mml t))
       (when head
         (unless (pos-visible-in-window-p)
       (message "The file \"%s\" has been attached at the end of the message"
            (file-name-nondirectory file)))
         (goto-char head)))))

(define-key mu4e-compose-mode-map (kbd "C-c C-a") 'tws-attach-file)

Similarly, for selecting the directory in which to save attachments from incoming emails:

(eval-after-load 'mu4e-view
  '(defun mu4e~view-request-attachments-dir (path)
     "Ask the user where to save multiple attachments (default is PATH)."
     (let ((fpath (expand-file-name
                    (mu4e-format "Save in directory ")
                    path) path)))
       (if (file-directory-p fpath)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.