--- /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 196096DE0244\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Dec 2015 01:50:19 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.209\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.209 tagged_above=-999 required=5 tests=[AWL=1.641,\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 W55gUB2-nRaA for <notmuch@notmuchmail.org>;\r
+ Mon, 21 Dec 2015 01:50:16 -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 4F74B6DE0BF6\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Dec 2015 01:50:16 -0800 (PST)\r
+Received: from localhost (unknown [192.168.200.7])\r
+ by max.feld.cvut.cz (Postfix) with ESMTP id 9E3D019F41EC\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Dec 2015 10:41:34 +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 77BayyHw4VNk for <notmuch@notmuchmail.org>;\r
+ Mon, 21 Dec 2015 10:41:32 +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 B8DD819F43F9\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Dec 2015 10:41:32 +0100 (CET)\r
+Received: from wsh by steelpick.2x.cz with local (Exim 4.86)\r
+ (envelope-from <sojkam1@fel.cvut.cz>)\r
+ id 1aAwyB-0000Yo-5i; Mon, 21 Dec 2015 10:41:31 +0100\r
+From: Michal Sojka <sojkam1@fel.cvut.cz>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2] emacs: Improve notmuch-message-mode initialization\r
+Date: Mon, 21 Dec 2015 10:41:15 +0100\r
+Message-Id: <1450690875-2111-2-git-send-email-sojkam1@fel.cvut.cz>\r
+X-Mailer: git-send-email 2.6.4\r
+In-Reply-To: <1450690875-2111-1-git-send-email-sojkam1@fel.cvut.cz>\r
+References: <1450690875-2111-1-git-send-email-sojkam1@fel.cvut.cz>\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: Mon, 21 Dec 2015 09:50:19 -0000\r
+\r
+Recent addition of notmuch-message-mode introduced several problems:\r
+\r
+1. When message-setup-hook is used to set buffer local variables,\r
+ these settings are not effective, because all buffer local\r
+ variables are immediately erased by notmuch-message-mode\r
+ initialization.\r
+\r
+2. message-mode-hook gets invoked twice - first when message-mail\r
+ invokes message-mode and second when notmuch-mua-mail invokes\r
+ notmuch-message-mode.\r
+\r
+This commit fixes these problems by replacing a call to message-mail\r
+with notmuch-specific code that is (hopefully) equivalent to\r
+message-mail functionality before introduction of\r
+notmuch-message-mode.\r
+\r
+We first initialize notmuch-message-mode with\r
+notmuch-mua-pop-to-buffer, which is a modified version of\r
+message-pop-to-buffer and then call message-setup-1, which is the only\r
+functionality of message-mail that is needed for notmuch.\r
+---\r
+ emacs/notmuch-mua.el | 49 +++++++++++++++++++++++++++++++++++++++++++------\r
+ 1 file changed, 43 insertions(+), 6 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
+index 466edd7..a66a306 100644\r
+--- a/emacs/notmuch-mua.el\r
++++ b/emacs/notmuch-mua.el\r
+@@ -278,10 +278,36 @@ 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 notmuch-mua-mail (&optional to subject other-headers &rest other-args)\r
+- "Invoke the notmuch mail composition window.\r
+-\r
+-OTHER-ARGS are passed through to `message-mail'."\r
++(defun notmuch-mua-pop-to-buffer (name)\r
++ "Pop to buffer NAME, and warn if it already exists and is\r
++modified. This function is notmuch addaptation of\r
++`message-pop-to-buffer'."\r
++ (let ((buffer (get-buffer name)))\r
++ (if (and buffer\r
++ (buffer-name buffer))\r
++ (let ((window (get-buffer-window buffer 0)))\r
++ (if window\r
++ ;; Raise the frame already displaying the message buffer.\r
++ (progn\r
++ (gnus-select-frame-set-input-focus (window-frame window))\r
++ (select-window window))\r
++ (funcall (notmuch-mua-get-switch-function) buffer)\r
++ (set-buffer buffer))\r
++ (when (and (buffer-modified-p)\r
++ (not (prog1\r
++ (y-or-n-p\r
++ "Message already being composed; erase? ")\r
++ (message nil))))\r
++ (error "Message being composed")))\r
++ (funcall (notmuch-mua-get-switch-function) name)\r
++ (set-buffer name))\r
++ (erase-buffer)\r
++ (notmuch-message-mode)))\r
++\r
++(defun notmuch-mua-mail (&optional to subject other-headers continue\r
++ switch-function yank-action send-actions\r
++ return-action &rest ignored)\r
++ "Invoke the notmuch mail composition window."\r
+ (interactive)\r
+ \r
+ (when notmuch-mua-user-agent-function\r
+@@ -293,8 +319,19 @@ 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
++ (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to))\r
++ (message-setup-1\r
++ ;; The following sexp is copied from `message-mail'\r
++ (nconc\r
++ `((To . ,(or to "")) (Subject . ,(or subject "")))\r
++ ;; C-h f compose-mail says that headers should be specified as\r
++ ;; (string . value); however all the rest of message expects\r
++ ;; headers to be symbols, not strings (eg message-header-format-alist).\r
++ ;; http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html\r
++ ;; We need to convert any string input, eg from rmail-start-mail.\r
++ (dolist (h other-headers other-headers)\r
++ (if (stringp (car h)) (setcar h (intern (capitalize (car h)))))))\r
++ yank-action send-actions return-action)\r
+ (notmuch-fcc-header-setup)\r
+ (message-sort-headers)\r
+ (message-hide-headers)\r
+-- \r
+2.6.4\r
+\r