[PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 5f / 8b4b3790066c49ad120d649022f8175c0d1fb1
1 Return-Path: <amdragon@mit.edu>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id F1467429E26\r
6         for <notmuch@notmuchmail.org>; Mon,  9 Jul 2012 14:42:57 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id oSiK4-+G36UT for <notmuch@notmuchmail.org>;\r
16         Mon,  9 Jul 2012 14:42:55 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
18         [18.9.25.14])\r
19         by olra.theworths.org (Postfix) with ESMTP id 4211C429E37\r
20         for <notmuch@notmuchmail.org>; Mon,  9 Jul 2012 14:42:51 -0700 (PDT)\r
21 X-AuditID: 1209190e-b7fb56d0000008b2-59-4ffb5058c977\r
22 Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
23         by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 27.51.02226.8505BFF4; Mon,  9 Jul 2012 17:42:48 -0400 (EDT)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id q69Lglf7003849; \r
27         Mon, 9 Jul 2012 17:42:47 -0400\r
28 Received: from drake.dyndns.org (26-4-176.dynamic.csail.mit.edu [18.26.4.176])\r
29         (authenticated bits=0)\r
30         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
31         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q69Lgi6q022690\r
32         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
33         Mon, 9 Jul 2012 17:42:47 -0400 (EDT)\r
34 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
35         (envelope-from <amdragon@mit.edu>)\r
36         id 1SoLjA-0004g9-2a; Mon, 09 Jul 2012 17:42:44 -0400\r
37 From: Austin Clements <amdragon@MIT.EDU>\r
38 To: notmuch@notmuchmail.org\r
39 Subject: [PATCH v3 8/9] emacs: Switch from text to JSON format for search\r
40         results\r
41 Date: Mon,  9 Jul 2012 17:42:41 -0400\r
42 Message-Id: <1341870162-17782-9-git-send-email-amdragon@mit.edu>\r
43 X-Mailer: git-send-email 1.7.10\r
44 In-Reply-To: <1341870162-17782-1-git-send-email-amdragon@mit.edu>\r
45 References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
46         <1341870162-17782-1-git-send-email-amdragon@mit.edu>\r
47 X-Brightmail-Tracker:\r
48  H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixG6nohsR8NvfYOsVPYvVc3ksrt+cyWzx\r
49         ZuU8Vgdmj52z7rJ7HP66kMXj2apbzAHMUVw2Kak5mWWpRfp2CVwZ+x6ZF1yyr5i18whTA+N8\r
50         oy5GTg4JAROJlbPWs0DYYhIX7q1n62Lk4hAS2McocehlDzOEs55R4t3Oh0wQzkkmieN9Mxgh\r
51         nLmMEq/+tTGC9LMJaEhs278czBYRkJbYeXc2K4jNLBAnsWXKf7C4sECgxPldb9lAbBYBVYmG\r
52         PTPAangFHCSWbGtghbhDXuLp/T6wGk4BR4k5N3eA9QoJlEt8vvGPeQIj/wJGhlWMsim5Vbq5\r
53         iZk5xanJusXJiXl5qUW6xnq5mSV6qSmlmxhBwcUpybeD8etBpUOMAhyMSjy80Uy//YVYE8uK\r
54         K3MPMUpyMCmJ8uZ5AoX4kvJTKjMSizPii0pzUosPMUpwMCuJ8N5yBcrxpiRWVqUW5cOkpDlY\r
55         lMR5r6Tc9BcSSE8sSc1OTS1ILYLJynBwKEnwTvMHahQsSk1PrUjLzClBSDNxcIIM5wEafg6k\r
56         hre4IDG3ODMdIn+KUVFKnPc6SEIAJJFRmgfXC4v+V4ziQK8I8+4AqeIBJg647ldAg5mABrf3\r
57         /AIZXJKIkJJqYFzE3avAt6GoKcasJ+Ph+oiZ95weXBdT2d2yS/R3YS33HZEEMXu2gtsTucW6\r
58         lqumuj36eIWl+VbwNOsUpXnRN0tP3dwgEPFEyM1E+8L/TjHVPo+ayjyvtVPS52qu0nggoZNr\r
59         UfN2X7fhEf+6h9x501S05/1arRsUvLHLManWV/bAwuTSs59llViKMxINtZiLihMBbJFpctkC        AAA=\r
60 Cc: tomi.ollila@iki.fi\r
61 X-BeenThere: notmuch@notmuchmail.org\r
62 X-Mailman-Version: 2.1.13\r
63 Precedence: list\r
64 List-Id: "Use and development of the notmuch mail system."\r
65         <notmuch.notmuchmail.org>\r
66 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
69 List-Post: <mailto:notmuch@notmuchmail.org>\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
71 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
72         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
73 X-List-Received-Date: Mon, 09 Jul 2012 21:42:58 -0000\r
74 \r
75 The JSON format eliminates the complex escaping issues that have\r
76 plagued the text search format.  This uses the incremental JSON parser\r
77 so that, like the text parser, it can output search results\r
78 incrementally.\r
79 \r
80 This slows down the parser by about ~4X, but puts us in a good\r
81 position to optimize either by improving the JSON parser (evidence\r
82 suggests this can reduce the overhead to ~40% over the text format) or\r
83 by switching to S-expressions (evidence suggests this will more than\r
84 double performance over the text parser).  [1]\r
85 \r
86 This also fixes the incremental search parsing test.\r
87 \r
88 This has one minor side-effect on search result formatting.\r
89 Previously, the date field was always padded to a fixed width of 12\r
90 characters because of how the text parser's regexp was written.  The\r
91 JSON format doesn't do this.  We could pad it out in Emacs before\r
92 formatting it, but, since all of the other fields are variable width,\r
93 we instead fix notmuch-search-result-format to take the variable-width\r
94 field and pad it out.  For users who have customized this variable,\r
95 we'll mention in the NEWS how to fix this slight format change.\r
96 \r
97 [1] id:"20110720205007.GB21316@mit.edu"\r
98 ---\r
99  emacs/notmuch.el |  110 +++++++++++++++++++++++++++++++-----------------------\r
100  test/emacs       |    1 -\r
101  2 files changed, 64 insertions(+), 47 deletions(-)\r
102 \r
103 diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
104 index dfeaf35..fabb7c0 100644\r
105 --- a/emacs/notmuch.el\r
106 +++ b/emacs/notmuch.el\r
107 @@ -60,7 +60,7 @@\r
108  (require 'notmuch-message)\r
109  \r
110  (defcustom notmuch-search-result-format\r
111 -  `(("date" . "%s ")\r
112 +  `(("date" . "%12s ")\r
113      ("count" . "%-7s ")\r
114      ("authors" . "%-20s ")\r
115      ("subject" . "%s ")\r
116 @@ -557,17 +557,14 @@ This function advances the next thread when finished."\r
117    (notmuch-search-tag '("-inbox"))\r
118    (notmuch-search-next-thread))\r
119  \r
120 -(defvar notmuch-search-process-filter-data nil\r
121 -  "Data that has not yet been processed.")\r
122 -(make-variable-buffer-local 'notmuch-search-process-filter-data)\r
123 -\r
124  (defun notmuch-search-process-sentinel (proc msg)\r
125    "Add a message to let user know when \"notmuch search\" exits"\r
126    (let ((buffer (process-buffer proc))\r
127         (status (process-status proc))\r
128         (exit-status (process-exit-status proc))\r
129         (never-found-target-thread nil))\r
130 -    (if (memq status '(exit signal))\r
131 +    (when (memq status '(exit signal))\r
132 +       (kill-buffer (process-get proc 'parse-buf))\r
133         (if (buffer-live-p buffer)\r
134             (with-current-buffer buffer\r
135               (save-excursion\r
136 @@ -577,8 +574,6 @@ This function advances the next thread when finished."\r
137                   (if (eq status 'signal)\r
138                       (insert "Incomplete search results (search process was killed).\n"))\r
139                   (when (eq status 'exit)\r
140 -                   (if notmuch-search-process-filter-data\r
141 -                       (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data)))\r
142                     (insert "End of search results.")\r
143                     (unless (= exit-status 0)\r
144                       (insert (format " (process returned %d)" exit-status)))\r
145 @@ -758,45 +753,59 @@ non-authors is found, assume that all of the authors match."\r
146      (insert (apply #'format string objects))\r
147      (insert "\n")))\r
148  \r
149 +(defvar notmuch-search-process-state nil\r
150 +  "Parsing state of the search process filter.")\r
151 +\r
152 +(defvar notmuch-search-json-parser nil\r
153 +  "Incremental JSON parser for the search process filter.")\r
154 +\r
155  (defun notmuch-search-process-filter (proc string)\r
156    "Process and filter the output of \"notmuch search\""\r
157 -  (let ((buffer (process-buffer proc)))\r
158 -    (if (buffer-live-p buffer)\r
159 -       (with-current-buffer buffer\r
160 -           (let ((line 0)\r
161 -                 (more t)\r
162 -                 (inhibit-read-only t)\r
163 -                 (string (concat notmuch-search-process-filter-data string)))\r
164 -             (setq notmuch-search-process-filter-data nil)\r
165 -             (while more\r
166 -               (while (and (< line (length string)) (= (elt string line) ?\n))\r
167 -                 (setq line (1+ line)))\r
168 -               (if (string-match "^thread:\\([0-9A-Fa-f]*\\) \\([^][]*\\) \\[\\([0-9]*\\)/\\([0-9]*\\)\\] \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)\r
169 -                   (let* ((thread-id (match-string 1 string))\r
170 -                          (tags-str (match-string 7 string))\r
171 -                          (result (list :thread thread-id\r
172 -                                        :date_relative (match-string 2 string)\r
173 -                                        :matched (string-to-number\r
174 -                                                  (match-string 3 string))\r
175 -                                        :total (string-to-number\r
176 -                                                (match-string 4 string))\r
177 -                                        :authors (match-string 5 string)\r
178 -                                        :subject (match-string 6 string)\r
179 -                                        :tags (if tags-str\r
180 -                                                  (save-match-data\r
181 -                                                    (split-string tags-str))))))\r
182 -                     (if (/= (match-beginning 0) line)\r
183 -                         (notmuch-search-show-error\r
184 -                          (substring string line (match-beginning 0))))\r
185 -                     (notmuch-search-show-result result)\r
186 -                     (set 'line (match-end 0)))\r
187 -                 (set 'more nil)\r
188 -                 (while (and (< line (length string)) (= (elt string line) ?\n))\r
189 -                   (setq line (1+ line)))\r
190 -                 (if (< line (length string))\r
191 -                     (setq notmuch-search-process-filter-data (substring string line)))\r
192 -                 ))))\r
193 -      (delete-process proc))))\r
194 +  (let ((results-buf (process-buffer proc))\r
195 +       (parse-buf (process-get proc 'parse-buf))\r
196 +       (inhibit-read-only t)\r
197 +       done)\r
198 +    (if (not (buffer-live-p results-buf))\r
199 +       (delete-process proc)\r
200 +      (with-current-buffer parse-buf\r
201 +       ;; Insert new data\r
202 +       (save-excursion\r
203 +         (goto-char (point-max))\r
204 +         (insert string)))\r
205 +      (with-current-buffer results-buf\r
206 +       (while (not done)\r
207 +         (condition-case nil\r
208 +             (case notmuch-search-process-state\r
209 +               ((begin)\r
210 +                ;; Enter the results list\r
211 +                (if (eq (notmuch-json-begin-compound\r
212 +                         notmuch-search-json-parser) 'retry)\r
213 +                    (setq done t)\r
214 +                  (setq notmuch-search-process-state 'result)))\r
215 +               ((result)\r
216 +                ;; Parse a result\r
217 +                (let ((result (notmuch-json-read notmuch-search-json-parser)))\r
218 +                  (case result\r
219 +                    ((retry) (setq done t))\r
220 +                    ((end) (setq notmuch-search-process-state 'end))\r
221 +                    (otherwise (notmuch-search-show-result result)))))\r
222 +               ((end)\r
223 +                ;; Any trailing data is unexpected\r
224 +                (notmuch-json-eof notmuch-search-json-parser)\r
225 +                (setq done t)))\r
226 +           (json-error\r
227 +            ;; Do our best to resynchronize and ensure forward\r
228 +            ;; progress\r
229 +            (notmuch-search-show-error\r
230 +             "%s"\r
231 +             (with-current-buffer parse-buf\r
232 +               (let ((bad (buffer-substring (line-beginning-position)\r
233 +                                            (line-end-position))))\r
234 +                 (forward-line)\r
235 +                 bad))))))\r
236 +       ;; Clear out what we've parsed\r
237 +       (with-current-buffer parse-buf\r
238 +         (delete-region (point-min) (point)))))))\r
239  \r
240  (defun notmuch-search-tag-all (&optional tag-changes)\r
241    "Add/remove tags from all messages in current search buffer.\r
242 @@ -899,10 +908,19 @@ Other optional parameters are used as follows:\r
243         (let ((proc (start-process\r
244                      "notmuch-search" buffer\r
245                      notmuch-command "search"\r
246 +                    "--format=json"\r
247                      (if oldest-first\r
248                          "--sort=oldest-first"\r
249                        "--sort=newest-first")\r
250 -                    query)))\r
251 +                    query))\r
252 +             ;; Use a scratch buffer to accumulate partial output.\r
253 +             ;; This buffer will be killed by the sentinel, which\r
254 +             ;; should be called no matter how the process dies.\r
255 +             (parse-buf (generate-new-buffer " *notmuch search parse*")))\r
256 +         (set (make-local-variable 'notmuch-search-process-state) 'begin)\r
257 +         (set (make-local-variable 'notmuch-search-json-parser)\r
258 +              (notmuch-json-create-parser parse-buf))\r
259 +         (process-put proc 'parse-buf parse-buf)\r
260           (set-process-sentinel proc 'notmuch-search-process-sentinel)\r
261           (set-process-filter proc 'notmuch-search-process-filter)\r
262           (set-process-query-on-exit-flag proc nil))))\r
263 diff --git a/test/emacs b/test/emacs\r
264 index 293b12a..afe35ba 100755\r
265 --- a/test/emacs\r
266 +++ b/test/emacs\r
267 @@ -36,7 +36,6 @@ test_emacs '(notmuch-search "tag:inbox")\r
268  test_expect_equal_file OUTPUT $EXPECTED/notmuch-search-tag-inbox\r
269  \r
270  test_begin_subtest "Incremental parsing of search results"\r
271 -test_subtest_known_broken\r
272  test_emacs "(ad-enable-advice 'notmuch-search-process-filter 'around 'pessimal)\r
273             (ad-activate 'notmuch-search-process-filter)\r
274             (notmuch-search \"tag:inbox\")\r
275 -- \r
276 1.7.10\r
277 \r