Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / dd / 833669cb54a9e70257bfc29f77992ed44ceca2
1 Return-Path: <markwalters1009@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 A8F78431FAE\r
6         for <notmuch@notmuchmail.org>; Tue,  4 Jun 2013 08:22:16 -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: 2.7\r
10 X-Spam-Level: **\r
11 X-Spam-Status: No, score=2.7 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
13         FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
14         FREEMAIL_REPLY=2.499, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
15 Received: from olra.theworths.org ([127.0.0.1])\r
16         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
17         with ESMTP id UG+AVvb19ev8 for <notmuch@notmuchmail.org>;\r
18         Tue,  4 Jun 2013 08:22:09 -0700 (PDT)\r
19 Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com\r
20  [74.125.82.51])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
21  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
22  01589431FAF    for <notmuch@notmuchmail.org>; Tue,  4 Jun 2013 08:22:06 -0700\r
23  (PDT)\r
24 Received: by mail-wg0-f51.google.com with SMTP id b13so342258wgh.30\r
25         for <notmuch@notmuchmail.org>; Tue, 04 Jun 2013 08:22:04 -0700 (PDT)\r
26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
28         bh=dgLBF3Eg9tdits3sxJq6MaQ4uqKQHoMctRIO9jq4FNU=;\r
29         b=kpfAPLOYHBkK4BOy2DWTGgxAUuIazW/3PI7VeiJvHuYg8wjxIpamtBoYkF3CCvYZGC\r
30         5kp2HWRuxwc32mVMB8+F3fn9/Au3pkoJss9U5YvCvzXWOX6vWZ7CYX3pkVIRK9fvHNZZ\r
31         Gj6f8w86uDCa9Or5iHCJrHTnZ/Gu8XlTFLNctbGh5OnoDDwSC3zDW7Rz1i1kCKWBuLQ/\r
32         +7Qc0Kg3KHT6bQTzrXl+pi5ggluKzawWWbBYRW5aoNSAeYhFCsa2v14FVX7j+Xd64bzn\r
33         juoD8GqPVJWguPWlBkeoTdOtFfDssFKIng58AB/kfFttra4j0s86t3PeBcbhrGllUkco\r
34         /hcA==\r
35 X-Received: by 10.194.158.34 with SMTP id wr2mr24441174wjb.57.1370359324416;\r
36         Tue, 04 Jun 2013 08:22:04 -0700 (PDT)\r
37 Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
38         by mx.google.com with ESMTPSA id ay7sm3308044wib.9.2013.06.04.08.22.02\r
39         for <multiple recipients>\r
40         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
41         Tue, 04 Jun 2013 08:22:03 -0700 (PDT)\r
42 From: Mark Walters <markwalters1009@gmail.com>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH] emacs: search: allow command line args as part of query\r
45 Date: Tue,  4 Jun 2013 16:21:59 +0100\r
46 Message-Id: <1370359319-2140-1-git-send-email-markwalters1009@gmail.com>\r
47 X-Mailer: git-send-email 1.7.9.1\r
48 In-Reply-To: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com>\r
49 References: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com>\r
50 X-BeenThere: notmuch@notmuchmail.org\r
51 X-Mailman-Version: 2.1.13\r
52 Precedence: list\r
53 List-Id: "Use and development of the notmuch mail system."\r
54         <notmuch.notmuchmail.org>\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
58 List-Post: <mailto:notmuch@notmuchmail.org>\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
62 X-List-Received-Date: Tue, 04 Jun 2013 15:22:16 -0000\r
63 \r
64 This allows command line arguments for notmuch-search to be part of\r
65 the query-string. The string must be of the form\r
66 [:blank:]*--cli-arguments -- query. I hope this doesn't clash with\r
67 xapian: I believe that queries shouldn't start with a "-".\r
68 \r
69 The cli-arguments must be arguments in a whitelist of arguments: this\r
70 adds a slight maintenance burden but means we don't have to deal with\r
71 users who passed "--format=text" or other incompatible options.\r
72 \r
73 Correctly parsed example queries are\r
74 --sort=oldest-first -- tag:inbox\r
75 --exclude=false -- from:fred\r
76 \r
77 Some options (currently only sort-order) we parse in emacs, the rest\r
78 we just pass to the cli. In light testing it seems to work.\r
79 \r
80 A full custom parser would be nicer but at least here we are only parsing\r
81 the non-query part of a string which is relatively simple: indeed we\r
82 already do that in the c code.\r
83 \r
84 We could just implement the option for sort-order, but I thought for\r
85 interface consistency making all the sensible options (sort-order\r
86 exclude limit and offset) work was worth the extra hassle.\r
87 ---\r
88 \r
89 This is a slight change to\r
90 1370292776-24535-1-git-send-email-markwalters1009@gmail.com The change\r
91 is that we add a whitelist of allowed cli options; other options are\r
92 removed and the user is warned (but the query is not aborted).\r
93 \r
94 One other tiny change is that a query starting with "[[:blank:]]*-- "\r
95 is allowed: everything after the -- is part of the real qeury so if\r
96 any strange query is accidentally being misparsed the user can prefix\r
97 with "-- " and it will give the current behaviour.\r
98 \r
99 Best wishes\r
100 \r
101 Mark\r
102 \r
103 \r
104  emacs/notmuch-hello.el |    5 +++--\r
105  emacs/notmuch-lib.el   |   44 ++++++++++++++++++++++++++++++++++++++++++++\r
106  emacs/notmuch.el       |   36 +++++++++++++++++++++++++-----------\r
107  3 files changed, 72 insertions(+), 13 deletions(-)\r
108 \r
109 diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el\r
110 index c1c6f4b..bcc1843 100644\r
111 --- a/emacs/notmuch-hello.el\r
112 +++ b/emacs/notmuch-hello.el\r
113 @@ -383,10 +383,11 @@ options will be handled as specified for\r
114  `notmuch-hello-insert-searches'."\r
115    (with-temp-buffer\r
116      (dolist (elem query-alist nil)\r
117 -      (let ((count-query (if (consp (cdr elem))\r
118 +      (let* ((full-count-query (if (consp (cdr elem))\r
119                              ;; do we have a different query for the message count?\r
120                              (third elem)\r
121 -                          (cdr elem))))\r
122 +                          (cdr elem)))\r
123 +            (count-query (car (notmuch-parse-query full-count-query))))\r
124         (insert\r
125          (notmuch-hello-filtered-query count-query\r
126                                        (or (plist-get options :filter-count)\r
127 diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
128 index 28f78e0..65c489e 100644\r
129 --- a/emacs/notmuch-lib.el\r
130 +++ b/emacs/notmuch-lib.el\r
131 @@ -189,6 +189,50 @@ user-friendly queries."\r
132    "Return a query that matches the message with id ID."\r
133    (concat "id:" (notmuch-escape-boolean-term id)))\r
134  \r
135 +(defun notmuch-search-parse-sort-order (args oldest-first)\r
136 +  (dolist (arg args nil)\r
137 +    (when (equal arg "--sort=oldest-first")\r
138 +      (setq oldest-first t))\r
139 +    (when (equal arg "--sort=newest-first")\r
140 +      (setq oldest-first nil)))\r
141 +  (setq args (delete "--sort=oldest-first" args))\r
142 +  (setq args (delete "--sort=newest-first" args))\r
143 +  (cons oldest-first args))\r
144 +\r
145 +(defvar notmuch-parse-option-whitelist\r
146 +  '("^--sort=oldest-first$"\r
147 +    "^--sort=newest-first$"\r
148 +    "^--exclude=true$"\r
149 +    "^--exclude=false$"\r
150 +    "^--exclude=flag$"\r
151 +    "^--limit=[0-9]*$"\r
152 +    "^--offset=[0-9]*$"\r
153 +    "^--$"))\r
154 +\r
155 +(defun notmuch-parse-in-whitelist-p (arg)\r
156 +  (let ((allowed nil))\r
157 +    (dolist (opt notmuch-parse-option-whitelist nil)\r
158 +      (setq allowed (or allowed (string-match-p opt arg))))\r
159 +    allowed))\r
160 +\r
161 +(defun notmuch-parse-query (query)\r
162 +  "Parse a query into a search and cli arguments\r
163 +\r
164 +Returns a list consisting of query followed by the cli-args (as a\r
165 +list). If the string does not have cli-args then this will be nil."\r
166 +\r
167 +  (if (or (string-match "^[[:blank:]]*--.*? -- " query)\r
168 +         (string-match "^[[:blank:]]*-- " query))\r
169 +      (let ((actual-query (substring query (match-end 0)))\r
170 +           (args (split-string (match-string 0 query) " " t)))\r
171 +       (message "Parsing query")\r
172 +       (dolist (arg args nil)\r
173 +         (unless (notmuch-parse-in-whitelist-p arg)\r
174 +           (setq args (delete arg args))\r
175 +           (message "Removing unknown option %s" arg)))\r
176 +       (cons actual-query args))\r
177 +    ;; no cli arguments\r
178 +    (list query)))\r
179  ;;\r
180  \r
181  (defun notmuch-common-do-stash (text)\r
182 diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
183 index 7994d74..6a4052e 100644\r
184 --- a/emacs/notmuch.el\r
185 +++ b/emacs/notmuch.el\r
186 @@ -255,6 +255,7 @@ For a mouse binding, return nil."\r
187    (notmuch-common-do-stash (notmuch-search-find-thread-id)))\r
188  \r
189  (defvar notmuch-search-query-string)\r
190 +(defvar notmuch-search-query-args)\r
191  (defvar notmuch-search-target-thread)\r
192  (defvar notmuch-search-target-line)\r
193  (defvar notmuch-search-continuation)\r
194 @@ -409,6 +410,7 @@ Complete list of currently available key bindings:\r
195    (interactive)\r
196    (kill-all-local-variables)\r
197    (make-local-variable 'notmuch-search-query-string)\r
198 +  (make-local-variable 'notmuch-search-query-args)\r
199    (make-local-variable 'notmuch-search-oldest-first)\r
200    (make-local-variable 'notmuch-search-target-thread)\r
201    (make-local-variable 'notmuch-search-target-line)\r
202 @@ -897,7 +899,7 @@ PROMPT is the string to prompt with."\r
203                               'notmuch-search-history nil nil)))))\r
204  \r
205  ;;;###autoload\r
206 -(defun notmuch-search (&optional query oldest-first target-thread target-line continuation)\r
207 +(defun notmuch-search (&optional query oldest-first target-thread target-line continuation cli-args)\r
208    "Run \"notmuch search\" with the given `query' and display results.\r
209  \r
210  If `query' is nil, it is read interactively from the minibuffer.\r
211 @@ -909,13 +911,20 @@ Other optional parameters are used as follows:\r
212    target-line: The line number to move to if the target thread does not\r
213                 appear in the search results."\r
214    (interactive)\r
215 -  (let* ((query (or query (notmuch-read-query "Notmuch search: ")))\r
216 +  (let* ((full-query (or query (notmuch-read-query "Notmuch search: ")))\r
217 +        (parsed-query (notmuch-parse-query full-query))\r
218 +        (query (car parsed-query))\r
219 +        (cli-args (or cli-args (cdr parsed-query)))\r
220 +        (combined-order-query (notmuch-search-parse-sort-order cli-args oldest-first))\r
221 +        (oldest-first (car combined-order-query))\r
222 +        (cli-args (cdr combined-order-query))\r
223          (buffer (get-buffer-create (notmuch-search-buffer-title query))))\r
224      (switch-to-buffer buffer)\r
225      (notmuch-search-mode)\r
226      ;; Don't track undo information for this buffer\r
227      (set 'buffer-undo-list t)\r
228      (set 'notmuch-search-query-string query)\r
229 +    (set 'notmuch-search-query-args cli-args)\r
230      (set 'notmuch-search-oldest-first oldest-first)\r
231      (set 'notmuch-search-target-thread target-thread)\r
232      (set 'notmuch-search-target-line target-line)\r
233 @@ -928,13 +937,13 @@ Other optional parameters are used as follows:\r
234        (erase-buffer)\r
235        (goto-char (point-min))\r
236        (save-excursion\r
237 -       (let ((proc (notmuch-start-notmuch\r
238 +       (let ((proc (apply #'notmuch-start-notmuch\r
239                      "notmuch-search" buffer #'notmuch-search-process-sentinel\r
240                      "search" "--format=sexp" "--format-version=1"\r
241 -                    (if oldest-first\r
242 +                    (if notmuch-search-oldest-first\r
243                          "--sort=oldest-first"\r
244                        "--sort=newest-first")\r
245 -                    query))\r
246 +                    (append cli-args (list query))))\r
247               ;; Use a scratch buffer to accumulate partial output.\r
248               ;; This buffer will be killed by the sentinel, which\r
249               ;; should be called no matter how the process dies.\r
250 @@ -957,9 +966,10 @@ same relative position within the new buffer."\r
251         (oldest-first notmuch-search-oldest-first)\r
252         (target-thread (notmuch-search-find-thread-id 'bare))\r
253         (query notmuch-search-query-string)\r
254 -       (continuation notmuch-search-continuation))\r
255 +       (continuation notmuch-search-continuation)\r
256 +       (cli-args notmuch-search-query-args))\r
257      (notmuch-kill-this-buffer)\r
258 -    (notmuch-search query oldest-first target-thread target-line continuation)\r
259 +    (notmuch-search query oldest-first target-thread target-line continuation cli-args)\r
260      (goto-char (point-min))))\r
261  \r
262  (defcustom notmuch-poll-script nil\r
263 @@ -1024,18 +1034,22 @@ search."\r
264    (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))\r
265    (notmuch-search-refresh-view))\r
266  \r
267 -(defun notmuch-search-filter (query)\r
268 +(defun notmuch-search-filter (full-query)\r
269    "Filter the current search results based on an additional query string.\r
270  \r
271  Runs a new search matching only messages that match both the\r
272  current search results AND the additional query string provided."\r
273    (interactive (list (notmuch-read-query "Filter search: ")))\r
274 -  (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)\r
275 +  (let* ((parsed-query (notmuch-parse-query full-query))\r
276 +        (query (car parsed-query))\r
277 +        (grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)\r
278                            (concat "( " query " )")\r
279 -                        query)))\r
280 +                        query))\r
281 +        (extra-cli-args (cdr parsed-query))\r
282 +        (cli-args (append notmuch-search-query-args extra-cli-args)))\r
283      (notmuch-search (if (string= notmuch-search-query-string "*")\r
284                         grouped-query\r
285 -                     (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))\r
286 +                     (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first nil nil nil cli-args)))\r
287  \r
288  (defun notmuch-search-filter-by-tag (tag)\r
289    "Filter the current search results based on a single tag.\r
290 -- \r
291 1.7.9.1\r
292 \r