Re: [BUG/PATCH v2] emacs: Fix the References header in reply
authorAustin Clements <amdragon@MIT.EDU>
Thu, 29 Mar 2012 05:32:21 +0000 (01:32 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:50 +0000 (09:45 -0800)
c9/5680633fb8ff721b13f2799334f31f5bebbed2 [new file with mode: 0644]

diff --git a/c9/5680633fb8ff721b13f2799334f31f5bebbed2 b/c9/5680633fb8ff721b13f2799334f31f5bebbed2
new file mode 100644 (file)
index 0000000..2be8401
--- /dev/null
@@ -0,0 +1,189 @@
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 89521431FAF\r
+       for <notmuch@notmuchmail.org>; Wed, 28 Mar 2012 22:32:26 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id iVi3+ZVkW7Bg for <notmuch@notmuchmail.org>;\r
+       Wed, 28 Mar 2012 22:32:25 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
+       [18.9.25.14])\r
+       by olra.theworths.org (Postfix) with ESMTP id 12FF9431FAE\r
+       for <notmuch@notmuchmail.org>; Wed, 28 Mar 2012 22:32:24 -0700 (PDT)\r
+X-AuditID: 1209190e-b7f7c6d0000008c3-3e-4f73f3e81975\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id E1.70.02243.8E3F37F4; Thu, 29 Mar 2012 01:32:24 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q2T5WNp2008977; \r
+       Thu, 29 Mar 2012 01:32:23 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q2T5WMqH009428\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Thu, 29 Mar 2012 01:32:23 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1SD7y9-0006pc-OY; Thu, 29 Mar 2012 01:32:21 -0400\r
+Date: Thu, 29 Mar 2012 01:32:21 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+Subject: Re: [BUG/PATCH v2] emacs: Fix the References header in reply\r
+Message-ID: <20120329053221.GA2670@mit.edu>\r
+References: <1332991226-510-1-git-send-email-awg+notmuch@xvx.ca>\r
+       <1332996818-15700-1-git-send-email-awg+notmuch@xvx.ca>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1332996818-15700-1-git-send-email-awg+notmuch@xvx.ca>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFupjleLIzCtJLcpLzFFi42IRYrdT133xudjf4MZ1Dosje2axW1y/OZPZ\r
+       gcnj2apbzB5NPxazBjBFcdmkpOZklqUW6dslcGWcuNbNWLBNteLNr0+sDYxdcl2MnBwSAiYS\r
+       N2/0sUDYYhIX7q1n62Lk4hAS2Mco8bRpApSzgVHixc2Z7CBVQgInmSRmdUZBJJYwSnz4+ZwV\r
+       JMEioCpx88sfZhCbTUBDYtv+5YwgtoiAlsSP9V/BapgFpCW+/W5mArGFBVwlut6+BovzCmhL\r
+       PH6+jBViQaXEs6OL2SDighInZz5hgejVkrjx7yVQLwfYnOX/OEBMTgFniaYpKiAVogIqElNO\r
+       bmObwCg0C0nzLCTNsxCaFzAyr2KUTcmt0s1NzMwpTk3WLU5OzMtLLdI11svNLNFLTSndxAgK\r
+       ak5Jvh2MXw8qHWIU4GBU4uFV0in2F2JNLCuuzD3EKMnBpCTKm/IJKMSXlJ9SmZFYnBFfVJqT\r
+       WnyIUYKDWUmE1/0cUI43JbGyKrUoHyYlzcGiJM6rpvXOT0ggPbEkNTs1tSC1CCYrw8GhJMEb\r
+       DoxeIcGi1PTUirTMnBKENBMHJ8hwHqDhKSA1vMUFibnFmekQ+VOMuhzr3l25zCjEkpeflyol\r
+       zmsOUiQAUpRRmgc3B5aMXjGKA70lzOsIUsUDTGRwk14BLWECWrLkSD7IkpJEhJRUA+NKu8eH\r
+       Fje7sMb8L9iU/PR2d9OZ9mvvBCd/Wipvxrft3bOG5OefK5QkS8IsOC6yibX8197VpNe6Klb/\r
+       dP3CntXbulQ8HqsdP9VxtWjmJ3WX+V8FbnqsfnLojpLKFsPaNvfmFV97JgjwbAtYqr436ipv\r
+       /cogy6/lEptYT/5YomnmyfR99v43UxqUWIozEg21mIuKEwGPw+coIQMAAA==\r
+Cc: notmuch@notmuchmail.org\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 29 Mar 2012 05:32:26 -0000\r
+\r
+Quoth Adam Wolfe Gordon on Mar 28 at 10:53 pm:\r
+> In the new reply code, the References header gets inserted by\r
+> message.el using a function called message-shorten-references. Unlike\r
+> all the other header-inserting functions, it doesn't put a newline\r
+> after the header, causing the next header to end up on the same\r
+> line. In our case, this header happened to be User-Agent, so it's hard\r
+> to notice. This is probably a bug in message.el, but we need to work\r
+> around it.\r
+> \r
+> This fixes the problem by wrapping message-shorten-references in a\r
+> function that inserts a newline after if necessary. This should\r
+> protect against the message.el bug being fixed in the future.\r
+> ---\r
+\r
+Ugh.  message-mode is such a rat's nest.  I dug through this and it\r
+looks like message-shorten-references really is at fault here.\r
+\r
+I'm sure you already tracked this down, but for others who may be\r
+interested, ultimately, the headers are inserted by\r
+mail-header-format, which calls formatter functions or, if there is no\r
+formatter, mail-header-format-function.  mail-header-format-function\r
+inserts a newline after the header and, indeed, mail-header-format\r
+does not insert anything between headers, so this is clearly up to the\r
+formatter.  message-shorten-references, however, inserts its header by\r
+calling message-insert-header, which looks remarkably like\r
+mail-header-format-function, minus the newline.  Ironically,\r
+message-shorten-references appears to be the only formatter configured\r
+by default.\r
+\r
+> This version adds the local variables to suppress 'cl warings, per\r
+> id:"1332995623-9055-1-git-send-email-amdragon@mit.edu".\r
+> \r
+>  emacs/notmuch-mua.el |   26 +++++++++++++++++++++++---\r
+>  1 files changed, 23 insertions(+), 3 deletions(-)\r
+> \r
+> diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el\r
+> index 24918d3..0d3fcd3 100644\r
+> --- a/emacs/notmuch-mua.el\r
+> +++ b/emacs/notmuch-mua.el\r
+> @@ -90,6 +90,15 @@ list."\r
+>      else if (notmuch-match-content-type (plist-get part :content-type) "text/*")\r
+>        collect part))\r
+>  \r
+> +;; There is a bug in emacs 23's message.el that results in a newline\r
+> +;; not being inserted after the References header, so the next header\r
+> +;; is concatenated to the end of it. This function fixes the problem,\r
+> +;; while guarding against the possibility that some current or future\r
+> +;; version of emacs has the bug fixed.\r
+> +(defun notmuch-mua-insert-references (header references)\r
+> +  (message-shorten-references header references)\r
+> +  (unless (bolp) (insert "\n")))\r
+> +\r
+\r
+Would it be safer to call whatever was associated with References in\r
+message-header-format-alist, rather than hard-coding\r
+message-shorten-references?\r
+\r
+>  (defun notmuch-mua-reply (query-string &optional sender reply-all)\r
+>    (let ((args '("reply" "--format=json"))\r
+>      reply\r
+> @@ -125,9 +134,16 @@ list."\r
+>        ;; Overlay the composition window on that being used to read\r
+>        ;; the original message.\r
+>        ((same-window-regexps '("\\*mail .*")))\r
+> -    (notmuch-mua-mail (plist-get reply-headers :To)\r
+> -                      (plist-get reply-headers :Subject)\r
+> -                      (notmuch-plist-to-alist reply-headers)))\r
+> +\r
+> +    ;; We modify message-header-format-alist to get around a bug in message.el.\r
+> +    ;; See the comment above on notmuch-mua-insert-references.\r
+> +    (let ((message-header-format-alist\r
+> +           (append '((References . notmuch-mua-insert-references))\r
+\r
+(cons '(References . notmuch-mua-insert-references) ...)\r
+\r
+> +                   (remove-if (lambda (x) (eq (car x) 'References))\r
+> +                              message-header-format-alist))))\r
+\r
+(assq-delete-all 'References (copy-alist message-header-format-alist))?\r
+\r
+Hmm.  That's less shorter than I would have expected, but I think it's\r
+less opaque.\r
+\r
+Actually, if I'm reading mail-header-format correctly, the order of\r
+this alist controls the order of the headers, so maybe what you\r
+actually want is\r
+\r
+(mapcar (lambda (x) (if (eq (car x) 'References)\r
+                        '(References . notmuch-mua-insert-references)\r
+                       x))\r
+        message-header-format-alist)\r
+\r
+> +      (notmuch-mua-mail (plist-get reply-headers :To)\r
+> +                        (plist-get reply-headers :Subject)\r
+> +                        (notmuch-plist-to-alist reply-headers))))\r
+>        ;; Insert the message body - but put it in front of the signature\r
+>        ;; if one is present\r
+>        (goto-char (point-max))\r
+> @@ -301,3 +317,7 @@ simply runs the corresponding `message-mode' hook functions."\r
+>  ;;\r
+>  \r
+>  (provide 'notmuch-mua)\r
+> +\r
+> +;; Local Variables:\r
+> +;; byte-compile-warnings: (not cl-functions)\r
+> +;; End:\r
+\r
+This won't be necessary if you use assq-delete-all or mapcar, but if\r
+you stick with the remove-if, you should also change the\r
+  (eval-when-compile (require 'cl))\r
+to\r
+  (require 'cl)\r