[PATCH v2 00/14] reply refactor, fixes
[notmuch-archives.git] / dc / 0b2c597df0908a49f28ce7dbaad171b44e6f14
1 Return-Path: <dmitry.kurochkin@gmail.com>\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 76FE0431FAE\r
6         for <notmuch@notmuchmail.org>; Fri, 27 Jan 2012 20:42:46 -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: -0.799\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13         FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id azQpjgK0miND for <notmuch@notmuchmail.org>;\r
17         Fri, 27 Jan 2012 20:42:40 -0800 (PST)\r
18 Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com\r
19         [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 91253431FC0\r
22         for <notmuch@notmuchmail.org>; Fri, 27 Jan 2012 20:42:36 -0800 (PST)\r
23 Received: by mail-bk0-f53.google.com with SMTP id zt19so2255515bkb.26\r
24         for <notmuch@notmuchmail.org>; Fri, 27 Jan 2012 20:42:36 -0800 (PST)\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
26         h=from:to:subject:date:message-id:x-mailer:in-reply-to:references;\r
27         bh=MhqE7ykX1duuHA/4d8mS0jUM3GLzsg0RV1zUZ0R7KhU=;\r
28         b=D8c7AzGNDjESB74ZLxO4/mUVg295bj0ClDeWFfE0V8IWmeXBgTwDnCti8PtMjaLu/E\r
29         7m/FeRmuDPkk90B/qRaSas/crKjOBGrKELdtJ5tPWlm0u7WC4hyIPnBCvPQOOEG5wf5w\r
30         ppSeDjSOnGb6o7dpj4PzpXsZ0SHTS715Gxc34=\r
31 Received: by 10.205.136.19 with SMTP id ii19mr4461614bkc.60.1327725756285;\r
32         Fri, 27 Jan 2012 20:42:36 -0800 (PST)\r
33 Received: from localhost ([91.144.186.21])\r
34         by mx.google.com with ESMTPS id 20sm9943849bkr.0.2012.01.27.20.42.35\r
35         (version=TLSv1/SSLv3 cipher=OTHER);\r
36         Fri, 27 Jan 2012 20:42:35 -0800 (PST)\r
37 From: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>\r
38 To: notmuch@notmuchmail.org\r
39 Subject: [PATCH 3/6] emacs: make "+" and "-" tagging operations more robust\r
40 Date: Sat, 28 Jan 2012 08:41:21 +0400\r
41 Message-Id: <1327725684-5887-3-git-send-email-dmitry.kurochkin@gmail.com>\r
42 X-Mailer: git-send-email 1.7.8.3\r
43 In-Reply-To: <1327725684-5887-1-git-send-email-dmitry.kurochkin@gmail.com>\r
44 References: <1327725684-5887-1-git-send-email-dmitry.kurochkin@gmail.com>\r
45 X-BeenThere: notmuch@notmuchmail.org\r
46 X-Mailman-Version: 2.1.13\r
47 Precedence: list\r
48 List-Id: "Use and development of the notmuch mail system."\r
49         <notmuch.notmuchmail.org>\r
50 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
52 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
53 List-Post: <mailto:notmuch@notmuchmail.org>\r
54 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
55 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
57 X-List-Received-Date: Sat, 28 Jan 2012 04:42:47 -0000\r
58 \r
59 Before the change, "+" and "-" tagging operations in notmuch-search\r
60 and notmuch-show views accepted only a single tag.  The patch makes\r
61 them use the recently added `notmuch-select-tags-with-completion'\r
62 function, which allows to enter multiple tags with "+" and "-"\r
63 prefixes.  So after the change, "+" and "-" bindings allow to both add\r
64 and remove multiple tags.  The only difference between "+" and "-" is\r
65 the minibuffer initial input ("+" and "-" respectively).\r
66 ---\r
67  emacs/notmuch-show.el |   65 +++++++------------\r
68  emacs/notmuch.el      |  165 +++++++++++++++++++++++++------------------------\r
69  2 files changed, 107 insertions(+), 123 deletions(-)\r
70 \r
71 diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
72 index 84ac624..03eadfb 100644\r
73 --- a/emacs/notmuch-show.el\r
74 +++ b/emacs/notmuch-show.el\r
75 @@ -38,8 +38,9 @@\r
76  \r
77  (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))\r
78  (declare-function notmuch-fontify-headers "notmuch" nil)\r
79 -(declare-function notmuch-select-tag-with-completion "notmuch" (prompt &rest search-terms))\r
80 +(declare-function notmuch-select-tags-with-completion "notmuch" (&optional initial-input &rest search-terms))\r
81  (declare-function notmuch-search-show-thread "notmuch" nil)\r
82 +(declare-function notmuch-update-tags "notmuch" (current-tags changed-tags))\r
83  \r
84  (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")\r
85    "Headers that should be shown in a message, in this order.\r
86 @@ -1267,7 +1268,7 @@ Some useful entries are:\r
87  \r
88  (defun notmuch-show-mark-read ()\r
89    "Mark the current message as read."\r
90 -  (notmuch-show-remove-tag "unread"))\r
91 +  (notmuch-show-tag-message "-unread"))\r
92  \r
93  ;; Functions for getting attributes of several messages in the current\r
94  ;; thread.\r
95 @@ -1470,51 +1471,33 @@ than only the current message."\r
96             (message (format "Command '%s' exited abnormally with code %d"\r
97                              shell-command exit-code))))))))\r
98  \r
99 -(defun notmuch-show-add-tags-worker (current-tags add-tags)\r
100 -  "Add to `current-tags' with any tags from `add-tags' not\r
101 -currently present and return the result."\r
102 -  (let ((result-tags (copy-sequence current-tags)))\r
103 -    (mapc (lambda (add-tag)\r
104 -           (unless (member add-tag current-tags)\r
105 -             (setq result-tags (push add-tag result-tags))))\r
106 -           add-tags)\r
107 -    (sort result-tags 'string<)))\r
108 -\r
109 -(defun notmuch-show-del-tags-worker (current-tags del-tags)\r
110 -  "Remove any tags in `del-tags' from `current-tags' and return\r
111 -the result."\r
112 -  (let ((result-tags (copy-sequence current-tags)))\r
113 -    (mapc (lambda (del-tag)\r
114 -           (setq result-tags (delete del-tag result-tags)))\r
115 -         del-tags)\r
116 -    result-tags))\r
117 -\r
118 -(defun notmuch-show-add-tag (&rest toadd)\r
119 -  "Add a tag to the current message."\r
120 -  (interactive\r
121 -   (list (notmuch-select-tag-with-completion "Tag to add: ")))\r
122 +(defun notmuch-show-tag-message (&rest changed-tags)\r
123 +  "Change tags for the current message.\r
124  \r
125 +`Changed-tags' is a list of tag operations for \"notmuch tag\",\r
126 +i.e. a list of tags to change with '+' and '-' prefixes."\r
127    (let* ((current-tags (notmuch-show-get-tags))\r
128 -        (new-tags (notmuch-show-add-tags-worker current-tags toadd)))\r
129 -\r
130 +        (new-tags (notmuch-update-tags current-tags changed-tags)))\r
131      (unless (equal current-tags new-tags)\r
132 -      (apply 'notmuch-tag (notmuch-show-get-message-id)\r
133 -            (mapcar (lambda (s) (concat "+" s)) toadd))\r
134 +      (apply 'notmuch-tag (notmuch-show-get-message-id) changed-tags)\r
135        (notmuch-show-set-tags new-tags))))\r
136  \r
137 -(defun notmuch-show-remove-tag (&rest toremove)\r
138 -  "Remove a tag from the current message."\r
139 -  (interactive\r
140 -   (list (notmuch-select-tag-with-completion\r
141 -         "Tag to remove: " (notmuch-show-get-message-id))))\r
142 +(defun notmuch-show-tag (&optional initial-input)\r
143 +  "Change tags for the current message, read input from the minibuffer."\r
144 +  (interactive)\r
145 +  (let ((changed-tags (notmuch-select-tags-with-completion\r
146 +                      initial-input (notmuch-show-get-message-id))))\r
147 +    (apply 'notmuch-show-tag-message changed-tags)))\r
148  \r
149 -  (let* ((current-tags (notmuch-show-get-tags))\r
150 -        (new-tags (notmuch-show-del-tags-worker current-tags toremove)))\r
151 +(defun notmuch-show-add-tag ()\r
152 +  "Same as `notmuch-show-tag' but sets initial input to '+'."\r
153 +  (interactive)\r
154 +  (notmuch-show-tag "+"))\r
155  \r
156 -    (unless (equal current-tags new-tags)\r
157 -      (apply 'notmuch-tag (notmuch-show-get-message-id)\r
158 -            (mapcar (lambda (s) (concat "-" s)) toremove))\r
159 -      (notmuch-show-set-tags new-tags))))\r
160 +(defun notmuch-show-remove-tag ()\r
161 +  "Same as `notmuch-show-tag' but sets initial input to '-'."\r
162 +  (interactive)\r
163 +  (notmuch-show-tag "-"))\r
164  \r
165  (defun notmuch-show-toggle-headers ()\r
166    "Toggle the visibility of the current message headers."\r
167 @@ -1559,7 +1542,7 @@ argument, hide all of the messages."\r
168  (defun notmuch-show-archive-thread-internal (show-next)\r
169    ;; Remove the tag from the current set of messages.\r
170    (goto-char (point-min))\r
171 -  (loop do (notmuch-show-remove-tag "inbox")\r
172 +  (loop do (notmuch-show-tag-message "-inbox")\r
173         until (not (notmuch-show-goto-message-next)))\r
174    ;; Move to the next item in the search results, if any.\r
175    (let ((parent-buffer notmuch-show-parent-buffer))\r
176 diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
177 index ff46617..24b0ea3 100644\r
178 --- a/emacs/notmuch.el\r
179 +++ b/emacs/notmuch.el\r
180 @@ -76,38 +76,56 @@ For example:\r
181  (defvar notmuch-query-history nil\r
182    "Variable to store minibuffer history for notmuch queries")\r
183  \r
184 -(defun notmuch-tag-completions (&optional prefixes search-terms)\r
185 -  (let ((tag-list\r
186 -        (split-string\r
187 -         (with-output-to-string\r
188 -           (with-current-buffer standard-output\r
189 -             (apply 'call-process notmuch-command nil t\r
190 -                    nil "search-tags" search-terms)))\r
191 -         "\n+" t)))\r
192 -    (if (null prefixes)\r
193 -       tag-list\r
194 -      (apply #'append\r
195 -            (mapcar (lambda (tag)\r
196 -                      (mapcar (lambda (prefix)\r
197 -                                (concat prefix tag)) prefixes))\r
198 -                    tag-list)))))\r
199 +(defun notmuch-tag-completions (&optional search-terms)\r
200 +  (split-string\r
201 +   (with-output-to-string\r
202 +     (with-current-buffer standard-output\r
203 +       (apply 'call-process notmuch-command nil t\r
204 +             nil "search-tags" search-terms)))\r
205 +   "\n+" t))\r
206  \r
207  (defun notmuch-select-tag-with-completion (prompt &rest search-terms)\r
208 -  (let ((tag-list (notmuch-tag-completions nil search-terms)))\r
209 +  (let ((tag-list (notmuch-tag-completions search-terms)))\r
210      (completing-read prompt tag-list)))\r
211  \r
212 -(defun notmuch-select-tags-with-completion (prompt &optional prefixes &rest search-terms)\r
213 -  (let ((tag-list (notmuch-tag-completions prefixes search-terms))\r
214 -       (crm-separator " ")\r
215 -       ;; By default, space is bound to "complete word" function.\r
216 -       ;; Re-bind it to insert a space instead.  Note that <tab>\r
217 -       ;; still does the completion.\r
218 -       (crm-local-completion-map\r
219 -        (let ((map (make-sparse-keymap)))\r
220 -          (set-keymap-parent map crm-local-completion-map)\r
221 -          (define-key map " " 'self-insert-command)\r
222 -          map)))\r
223 -    (delete "" (completing-read-multiple prompt tag-list))))\r
224 +(defun notmuch-select-tags-with-completion (&optional initial-input &rest search-terms)\r
225 +  (let* ((add-tag-list (mapcar (apply-partially 'concat "+")\r
226 +                              (notmuch-tag-completions)))\r
227 +        (remove-tag-list (mapcar (apply-partially 'concat "-")\r
228 +                                 (notmuch-tag-completions search-terms)))\r
229 +        (tag-list (append add-tag-list remove-tag-list))\r
230 +        (crm-separator " ")\r
231 +        ;; By default, space is bound to "complete word" function.\r
232 +        ;; Re-bind it to insert a space instead.  Note that <tab>\r
233 +        ;; still does the completion.\r
234 +        (crm-local-completion-map\r
235 +         (let ((map (make-sparse-keymap)))\r
236 +           (set-keymap-parent map crm-local-completion-map)\r
237 +           (define-key map " " 'self-insert-command)\r
238 +           map)))\r
239 +    (delete "" (completing-read-multiple\r
240 +               "Operations (+add -drop): notmuch tag " tag-list nil\r
241 +               nil initial-input))))\r
242 +\r
243 +(defun notmuch-update-tags (current-tags changed-tags)\r
244 +  "Update `current-tags' with `changed-tags' and return the result.\r
245 +\r
246 +`Changed-tags' is a list of tag operations given to \"notmuch\r
247 +tag\", i.e. a list of changed tags with '+' and '-' prefixes."\r
248 +  (let ((result-tags (copy-sequence current-tags)))\r
249 +    (mapc (lambda (changed-tag)\r
250 +           (unless (string= changed-tag "")\r
251 +             (let ((op (substring changed-tag 0 1))\r
252 +                   (tag (substring changed-tag 1)))\r
253 +               (cond ((string= op "+")\r
254 +                      (unless (member tag result-tags)\r
255 +                        (push tag result-tags)))\r
256 +                     ((string= op "-")\r
257 +                      (setq result-tags (delete tag result-tags)))\r
258 +                     (t\r
259 +                      (error "Changed tag must be of the form `+this_tag' or `-that_tag'"))))))\r
260 +       changed-tags)\r
261 +    (sort result-tags 'string<)))\r
262  \r
263  (defun notmuch-foreach-mime-part (function mm-handle)\r
264    (cond ((stringp (car mm-handle))\r
265 @@ -447,6 +465,10 @@ Complete list of currently available key bindings:\r
266    "Return a list of threads for the current region"\r
267    (notmuch-search-properties-in-region 'notmuch-search-thread-id beg end))\r
268  \r
269 +(defun notmuch-search-find-thread-id-region-search (beg end)\r
270 +  "Return a search string for threads for the current region"\r
271 +  (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or "))\r
272 +\r
273  (defun notmuch-search-find-authors ()\r
274    "Return the authors for the current thread"\r
275    (get-text-property (point) 'notmuch-search-authors))\r
276 @@ -590,74 +612,55 @@ the messages that were tagged"\r
277         (forward-line 1))\r
278        output)))\r
279  \r
280 -(defun notmuch-search-add-tag-thread (tag)\r
281 -  (notmuch-search-add-tag-region tag (point) (point)))\r
282 +(defun notmuch-search-tag-thread (&rest tags)\r
283 +  "Change tags for the currently selected thread.\r
284  \r
285 -(defun notmuch-search-add-tag-region (tag beg end)\r
286 -  (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))\r
287 -    (notmuch-tag search-id-string (concat "+" tag))\r
288 -    (save-excursion\r
289 -      (let ((last-line (line-number-at-pos end))\r
290 -           (max-line (- (line-number-at-pos (point-max)) 2)))\r
291 -       (goto-char beg)\r
292 -       (while (<= (line-number-at-pos) (min last-line max-line))\r
293 -         (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))\r
294 -         (forward-line))))))\r
295 +See `notmuch-search-tag-region' for details."\r
296 +  (apply 'notmuch-search-tag-region (point) (point) tags))\r
297  \r
298 -(defun notmuch-search-remove-tag-thread (tag)\r
299 -  (notmuch-search-remove-tag-region tag (point) (point)))\r
300 +(defun notmuch-search-tag-region (beg end &rest tags)\r
301 +  "Change tags for threads in the given region.\r
302  \r
303 -(defun notmuch-search-remove-tag-region (tag beg end)\r
304 -  (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))\r
305 -    (notmuch-tag search-id-string (concat "-" tag))\r
306 +`Tags' is a list of tag operations for \"notmuch tag\", i.e. a\r
307 +list of tags to change with '+' and '-' prefixes.  The tags are\r
308 +added or removed for all threads in the region from `beg' to\r
309 +`end'."\r
310 +  (let ((search-string (notmuch-search-find-thread-id-region-search beg end)))\r
311 +    (apply 'notmuch-tag search-string tags)\r
312      (save-excursion\r
313        (let ((last-line (line-number-at-pos end))\r
314             (max-line (- (line-number-at-pos (point-max)) 2)))\r
315         (goto-char beg)\r
316         (while (<= (line-number-at-pos) (min last-line max-line))\r
317 -         (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))\r
318 +         (notmuch-search-set-tags\r
319 +          (notmuch-update-tags (notmuch-search-get-tags) tags))\r
320           (forward-line))))))\r
321  \r
322 -(defun notmuch-search-add-tag (tag)\r
323 -  "Add a tag to the currently selected thread or region.\r
324 -\r
325 -The tag is added to all messages in the currently selected thread\r
326 -or threads in the current region."\r
327 -  (interactive\r
328 -   (list (notmuch-select-tag-with-completion "Tag to add: ")))\r
329 -  (save-excursion\r
330 -    (if (region-active-p)\r
331 -       (let* ((beg (region-beginning))\r
332 -              (end (region-end)))\r
333 -         (notmuch-search-add-tag-region tag beg end))\r
334 -      (notmuch-search-add-tag-thread tag))))\r
335 -\r
336 -(defun notmuch-search-remove-tag (tag)\r
337 -  "Remove a tag from the currently selected thread or region.\r
338 +(defun notmuch-search-tag (&optional initial-input)\r
339 +  "Change tags for the currently selected thread or region."\r
340 +  (interactive)\r
341 +  (let* ((beg (if (region-active-p) (region-beginning) (point)))\r
342 +        (end (if (region-active-p) (region-end) (point)))\r
343 +        (search-string (notmuch-search-find-thread-id-region-search beg end))\r
344 +        (tags (notmuch-select-tags-with-completion initial-input search-string)))\r
345 +    (apply 'notmuch-search-tag-region beg end tags)))\r
346 +\r
347 +(defun notmuch-search-add-tag ()\r
348 +  "Same as `notmuch-search-tag' but sets initial input to '+'."\r
349 +  (interactive)\r
350 +  (notmuch-search-tag "+"))\r
351  \r
352 -The tag is removed from all messages in the currently selected\r
353 -thread or threads in the current region."\r
354 -  (interactive\r
355 -   (list (notmuch-select-tag-with-completion\r
356 -         "Tag to remove: "\r
357 -         (if (region-active-p)\r
358 -             (mapconcat 'identity\r
359 -                        (notmuch-search-find-thread-id-region (region-beginning) (region-end))\r
360 -                        " ")\r
361 -           (notmuch-search-find-thread-id)))))\r
362 -  (save-excursion\r
363 -    (if (region-active-p)\r
364 -       (let* ((beg (region-beginning))\r
365 -              (end (region-end)))\r
366 -         (notmuch-search-remove-tag-region tag beg end))\r
367 -      (notmuch-search-remove-tag-thread tag))))\r
368 +(defun notmuch-search-remove-tag ()\r
369 +  "Same as `notmuch-search-tag' but sets initial input to '-'."\r
370 +  (interactive)\r
371 +  (notmuch-search-tag "-"))\r
372  \r
373  (defun notmuch-search-archive-thread ()\r
374    "Archive the currently selected thread (remove its \"inbox\" tag).\r
375  \r
376  This function advances the next thread when finished."\r
377    (interactive)\r
378 -  (notmuch-search-remove-tag-thread "inbox")\r
379 +  (notmuch-search-tag-thread "-inbox")\r
380    (notmuch-search-next-thread))\r
381  \r
382  (defvar notmuch-search-process-filter-data nil\r
383 @@ -893,9 +896,7 @@ will prompt for tags to be added or removed. Tags prefixed with\r
384  Each character of the tag name may consist of alphanumeric\r
385  characters as well as `_.+-'.\r
386  "\r
387 -  (interactive (notmuch-select-tags-with-completion\r
388 -               "Operations (+add -drop): notmuch tag "\r
389 -               '("+" "-")))\r
390 +  (interactive (notmuch-select-tags-with-completion))\r
391    (apply 'notmuch-tag notmuch-search-query-string actions))\r
392  \r
393  (defun notmuch-search-buffer-title (query)\r
394 -- \r
395 1.7.8.3\r
396 \r