[PATCH 1/4] emacs: JSON based search improvements.
[notmuch-archives.git] / f3 / 9b4abd0203536bd45736085f8b16268b3dec1c
1 Return-Path: <dme@ut.hh.sledj.net>\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 70A91431FB6\r
6         for <notmuch@notmuchmail.org>; Tue, 30 Nov 2010 03:00:38 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 1.363\r
10 X-Spam-Level: *\r
11 X-Spam-Status: No, score=1.363 tagged_above=-999 required=5\r
12         tests=[RDNS_DYNAMIC=0.363, TO_NO_BRKTS_DYNIP=1] 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 FsWrmNF+JQ1K for <notmuch@notmuchmail.org>;\r
16         Tue, 30 Nov 2010 03:00:37 -0800 (PST)\r
17 Received: from ut.hh.sledj.net (host81-149-164-25.in-addr.btopenworld.com\r
18         [81.149.164.25])\r
19         (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 61375431FB5\r
22         for <notmuch@notmuchmail.org>; Tue, 30 Nov 2010 03:00:37 -0800 (PST)\r
23 Received: by ut.hh.sledj.net (Postfix, from userid 1000)\r
24         id E026659422B; Tue, 30 Nov 2010 11:00:26 +0000 (GMT)\r
25 From: David Edmondson <dme@dme.org>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [PATCH 1/4] emacs: JSON based search improvements.\r
28 Date: Tue, 30 Nov 2010 11:00:22 +0000\r
29 Message-Id: <1291114825-3513-1-git-send-email-dme@dme.org>\r
30 X-Mailer: git-send-email 1.7.2.3\r
31 In-Reply-To: <1291026599-14795-4-git-send-email-dme@dme.org>\r
32 References: <1291026599-14795-4-git-send-email-dme@dme.org>\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.13\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37         <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Tue, 30 Nov 2010 11:00:38 -0000\r
46 \r
47 Fix variable name typos.\r
48 \r
49 Attempt to avoid display flashing by removing newlines from the\r
50 inserted string.\r
51 \r
52 Ease debugging by moving the `condition-case' closer into the code\r
53 that it is really intended to cover (the parsing of the JSON object),\r
54 allowing errors in the remaining code to still fire correctly. As a\r
55 consequence, test for :json-eof when examining the next character to\r
56 be parsed, as that throws an error that was previously covered by the\r
57 `condition-case'.\r
58 ---\r
59  emacs/notmuch.el |   74 ++++++++++++++++++++++++++++++-----------------------\r
60  1 files changed, 42 insertions(+), 32 deletions(-)\r
61 \r
62 diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
63 index bde8c47..f4aefc8 100644\r
64 --- a/emacs/notmuch.el\r
65 +++ b/emacs/notmuch.el\r
66 @@ -717,57 +717,67 @@ foreground and blue background."\r
67      (put-text-property beg (point-marker) 'notmuch-search-subject subject)))\r
68  \r
69  (defvar notmuch-search-parse-start nil)\r
70 -(make-variable-buffer-local 'notmuch-show-parse-start)\r
71 +(make-variable-buffer-local 'notmuch-search-parse-start)\r
72  \r
73  (defun notmuch-search-process-insert (proc buffer string)\r
74    (with-current-buffer buffer\r
75      (let ((inhibit-read-only t)\r
76           (inhibit-redisplay t)\r
77           ;; Vectors are not as useful here.\r
78 -         (json-array-type 'list)\r
79 -         object)\r
80 +         (json-array-type 'list))\r
81        (save-excursion\r
82         ;; Insert the text, advancing the process marker\r
83         (goto-char (point-max))\r
84 -       (insert string)\r
85 +       ;; Flatten the string (remove newlines) to reduce the flashing\r
86 +       ;; that occurs when we insert the multi-line object and then\r
87 +       ;; replace it with a single line summary. This is safe because\r
88 +       ;; we know that none of the JSON fields can contain newlines -\r
89 +       ;; only the whitespace between fields.\r
90 +       (insert (replace-regexp-in-string "\n" "" string))\r
91         (set-marker (process-mark proc) (point)))\r
92  \r
93        (save-excursion\r
94         (goto-char notmuch-search-parse-start)\r
95 -       (condition-case nil\r
96             (while\r
97 -               (cond\r
98 -                ;; Opening bracket or comma separator between\r
99 -                ;; objects.\r
100 -                ((or (char-equal (json-peek) ?\[)\r
101 -                     (char-equal (json-peek) ?\,))\r
102 -                 (json-advance)\r
103 -                 (delete-region notmuch-search-parse-start (point))\r
104 -                 t)\r
105 -\r
106 -                ;; Closing array.\r
107 -                ((char-equal (json-peek) ?\])\r
108 -                 ;; Consume both the closing bracket and any trailing\r
109 -                 ;; whitespace (typically a carriage return).\r
110 -                 (json-advance)\r
111 -                 (json-skip-whitespace)\r
112 -                 (delete-region notmuch-search-parse-start (point))\r
113 -                 nil)\r
114 -\r
115 -                ;; Single object.\r
116 -                ((setq object (json-read-object))\r
117 -                 ;; Delete the object that we consumed.\r
118 -                 (delete-region notmuch-search-parse-start (point))\r
119 -                 ;; Insert the corresponding results.\r
120 -                 (notmuch-search-process-insert-object object)\r
121 -                 t))\r
122 +               (let ((next-char (json-peek)))\r
123 +                 (cond\r
124 +                  ;; No more data (yet).\r
125 +                  ((eq next-char :json-eof)\r
126 +                   nil)\r
127 +\r
128 +                  ;; Opening bracket or comma separator between\r
129 +                  ;; objects.\r
130 +                  ((or (char-equal next-char ?\[)\r
131 +                       (char-equal next-char ?\,))\r
132 +                   (json-advance)\r
133 +                   (delete-region notmuch-search-parse-start (point))\r
134 +                   t)\r
135 +\r
136 +                  ;; Closing array.\r
137 +                  ((char-equal next-char ?\])\r
138 +                   ;; Consume both the closing bracket and any trailing\r
139 +                   ;; whitespace (typically a carriage return).\r
140 +                   (json-advance)\r
141 +                   (json-skip-whitespace)\r
142 +                   (delete-region notmuch-search-parse-start (point))\r
143 +                   nil)\r
144 +\r
145 +                  ;; Single object.\r
146 +                  ((condition-case nil\r
147 +                       (let ((object (json-read-object)))\r
148 +                         ;; Delete the object that we consumed.\r
149 +                         (delete-region notmuch-search-parse-start (point))\r
150 +                         ;; Insert the corresponding results.\r
151 +                         (notmuch-search-process-insert-object object)\r
152 +                         t)\r
153 +                     (error nil)))))\r
154 +\r
155               ;; Consume any white space between terms.\r
156               (let ((p (point)))\r
157                 (json-skip-whitespace)\r
158                 (delete-region p (point)))\r
159               ;; Remember where we got up to.\r
160 -             (setq notmuch-search-parse-start (point)))\r
161 -         (error nil))))))\r
162 +             (setq notmuch-search-parse-start (point)))))))\r
163  \r
164  (defun notmuch-search-process-filter (proc string)\r
165    "Process and filter the output of `notmuch search'."\r
166 -- \r
167 1.7.2.3\r
168 \r