--- /dev/null
+Return-Path: <sojkam1@fel.cvut.cz>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id CEABA6DE0281\r
+ for <notmuch@notmuchmail.org>; Tue, 3 Nov 2015 09:53:01 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.985\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.985 tagged_above=-999 required=5 tests=[AWL=1.865,\r
+ RCVD_IN_DNSWL_MED=-2.3, RP_MATCHES_RCVD=-0.55] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 03ujwhthBE86 for <notmuch@notmuchmail.org>;\r
+ Tue, 3 Nov 2015 09:52:59 -0800 (PST)\r
+Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 61F0D6DE01EA\r
+ for <notmuch@notmuchmail.org>; Tue, 3 Nov 2015 09:52:58 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 188B619F4379\r
+ for <notmuch@notmuchmail.org>; Tue, 3 Nov 2015 18:52:56 +0100 (CET)\r
+X-Virus-Scanned: IMAP STYX AMAVIS\r
+Received: from max.feld.cvut.cz ([192.168.200.1])\r
+ by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new, port 10044)\r
+ with ESMTP id aM_tLtkP5RGe for <notmuch@notmuchmail.org>;\r
+ Tue, 3 Nov 2015 18:52:54 +0100 (CET)\r
+Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 70FCE19F37CC\r
+ for <notmuch@notmuchmail.org>; Tue, 3 Nov 2015 18:52:54 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.86)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1ZtflM-0007Jg-QM; Tue, 03 Nov 2015 18:52:52 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] emacs: Fix regression in (notmuch-)message-mode\r
+ initialization\r
+Date: Tue, 3 Nov 2015 18:52:41 +0100\r
+Message-Id: <1446573161-28068-1-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.5.3\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 03 Nov 2015 17:53:01 -0000\r
+\r
+Recent addition of notmuch-message-mode introduced a few regressions.\r
+I use message-setup-hook to set some buffer local variables and this\r
+stopped working. The reason is that notmuch-message-mode, which cleans\r
+all buffer local variables, is called after calling message-mail,\r
+which calls the message-setup-hook. Another problem with the previous\r
+implementation might be that the message-mode-hook is called twice -\r
+first when message-mail calls message-mode and second when\r
+notmuch-message-mode calls it. This can be problematic when the hook\r
+has side effects.\r
+\r
+This commit uses advice mechanism to call notmuch-message-mode instead\r
+of message-mode. This way, a call to message-mail initializes directly\r
+notmuch-message-mode rather than message-mode which is later changed\r
+to notmuch-message-mode. The advice is constructed in such a way, that\r
+it is effective only once and when called by notmuch. The second call\r
+to message-mode (from notmuch-message-mode) calls the original\r
+message-mode.\r
+\r
+This implementation uses the new advice mechanism introduced in Emacs\r
+24.4. If we want to support older version, this must be changed.\r
+---\r
+ emacs/notmuch-mua.el | 20 ++++++++++++++++++--\r
+ 1 file changed, 18 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
+index fd98ea4..540a676 100644\r
+--- a/emacs/notmuch-mua.el\r
++++ b/emacs/notmuch-mua.el\r
+@@ -276,6 +276,19 @@ Note that these functions use `mail-citation-hook' if that is non-nil."\r
+ (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)\r
+ (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)\r
+ \r
++(defun message-mode--notmuch-advice ()\r
++ (if (boundp 'use-notmuch-message-mode)\r
++ (progn\r
++ (makunbound 'use-notmuch-message-mode)\r
++ (notmuch-message-mode)\r
++ nil) ; Do not call original message-mode\r
++ t)) ; Call original message-mode\r
++\r
++;; Advice message-mode to call notmuch-message-mode when\r
++;; use-notmuch-message-mode is bound. Otherwise message-mode is called\r
++;; without changes.\r
++(advice-add 'message-mode :before-while #'message-mode--notmuch-advice)\r
++\r
+ (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)\r
+ "Invoke the notmuch mail composition window.\r
+ \r
+@@ -291,8 +304,11 @@ OTHER-ARGS are passed through to `message-mail'."\r
+ (push (cons 'From (concat\r
+ (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))\r
+ \r
+- (apply #'message-mail to subject other-headers other-args)\r
+- (notmuch-message-mode)\r
++ ;; message-mail calls message-mode directly (via\r
++ ;; message-pop-to-buffer). We want it to call notmuch-message-mode\r
++ ;; instead.\r
++ (let ((use-notmuch-message-mode))\r
++ (apply #'message-mail to subject other-headers other-args))\r
+ (notmuch-fcc-header-setup)\r
+ (message-sort-headers)\r
+ (message-hide-headers)\r
+-- \r
+2.5.3\r
+\r