2016-02-06 An attachment reminder in mu4e

A nice feature of Gmail is that you can ask it to remind you about attachments if the message contains things like “I attach” and there is no attachment. Some time ago I decided that I want my mu4e to do this, too. (And it seems that I'm far from being the only one.) And here it my take on the subject.

(defun mbork/message-attachment-present-p ()
  "Return t if an attachment is found in the current message."
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (when (search-forward "<#part" nil t) t))))

(defcustom mbork/message-attachment-intent-re
  (regexp-opt '("I attach"
		"I have attached"
		"I've attached"
		"I have included"
		"I've included"
		"see the attached"
		"see the attachment"
		"attached file"))
  "A regex which - if found in the message, and if there is no
attachment - should launch the no-attachment warning.")

(defcustom mbork/message-attachment-reminder
  "Are you sure you want to send this message without any attachment? "
  "The default question asked when trying to send a message
containing `mbork/message-attachment-intent-re' without an
actual attachment.")

(defun mbork/message-warn-if-no-attachments ()
  "Ask the user if s?he wants to send the message even though
there are no attachments."
  (when (and (save-excursion
	       (save-restriction
		 (widen)
		 (goto-char (point-min))
		 (re-search-forward mbork/message-attachment-intent-re nil t)))
	     (not (mbork/message-attachment-present-p)))
    (unless (y-or-n-p mbork/message-attachment-reminder)
      (keyboard-quit))))

(add-hook 'message-send-hook #'mbork/message-warn-if-no-attachments)

(In fact, it should work in Gnus, too, since I use message-send-hook.)

I admit that the function mbork/message-attachment-present-p is rather crude, but it seems to work fine. I’d gladly hear about why it’s wrong and how to correct it if there are any cases when it might break.

Notice also the use of regexp-opt; it is a convenience function which creates an efficient regex out of a list of strings.

Also, I decided that if the user decides not to send the message, using keyboard-quit is simple and effective – this way I didn’t have to advise any functions (AFAIK, you can’t use message-send-hook to actually prevent sending the message.)

Finally, I’d like to mention that the above code is an excerpt from a library of functions which make writing emails more pleasant. The library is here: https://github.com/mbork/message, it is WiP, and some functions might even not work. (I have a small hiatus now, but I will come back to work on it. Some of its features might even find their way to Emacs some day.)

CategoryEnglish, CategoryBlog, CategoryEmacs