[PATCH] emacs: Fix regression in (notmuch-)message-mode initialization

Michal Sojka sojkam1 at fel.cvut.cz
Tue Nov 3 09:52:41 PST 2015


Recent addition of notmuch-message-mode introduced a few regressions.
I use message-setup-hook to set some buffer local variables and this
stopped working. The reason is that notmuch-message-mode, which cleans
all buffer local variables, is called after calling message-mail,
which calls the message-setup-hook. Another problem with the previous
implementation might be that the message-mode-hook is called twice -
first when message-mail calls message-mode and second when
notmuch-message-mode calls it. This can be problematic when the hook
has side effects.

This commit uses advice mechanism to call notmuch-message-mode instead
of message-mode. This way, a call to message-mail initializes directly
notmuch-message-mode rather than message-mode which is later changed
to notmuch-message-mode. The advice is constructed in such a way, that
it is effective only once and when called by notmuch. The second call
to message-mode (from notmuch-message-mode) calls the original
message-mode.

This implementation uses the new advice mechanism introduced in Emacs
24.4. If we want to support older version, this must be changed.
---
 emacs/notmuch-mua.el | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index fd98ea4..540a676 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -276,6 +276,19 @@ Note that these functions use `mail-citation-hook' if that is non-nil."
 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
 
+(defun message-mode--notmuch-advice ()
+  (if (boundp 'use-notmuch-message-mode)
+      (progn
+	(makunbound 'use-notmuch-message-mode)
+	(notmuch-message-mode)
+	nil) ; Do not call original message-mode
+    t))	     ; Call original message-mode
+
+;; Advice message-mode to call notmuch-message-mode when
+;; use-notmuch-message-mode is bound. Otherwise message-mode is called
+;; without changes.
+(advice-add 'message-mode :before-while #'message-mode--notmuch-advice)
+
 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
   "Invoke the notmuch mail composition window.
 
@@ -291,8 +304,11 @@ OTHER-ARGS are passed through to `message-mail'."
     (push (cons 'From (concat
 		       (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
 
-  (apply #'message-mail to subject other-headers other-args)
-  (notmuch-message-mode)
+  ;; message-mail calls message-mode directly (via
+  ;; message-pop-to-buffer). We want it to call notmuch-message-mode
+  ;; instead.
+  (let ((use-notmuch-message-mode))
+    (apply #'message-mail to subject other-headers other-args))
   (notmuch-fcc-header-setup)
   (message-sort-headers)
   (message-hide-headers)
-- 
2.5.3



More information about the notmuch mailing list