database error
[notmuch-archives.git] / 65 / deee0a06a031c3178decdabec5148b8c1d513b
1 Return-Path: <dme@dme.org>\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 1B933431FC3\r
6         for <notmuch@notmuchmail.org>; Mon, 24 Nov 2014 02:44:43 -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.699\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.699 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7, UNPARSEABLE_RELAY=0.001]\r
13         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 4lPDwKPrIUhH for <notmuch@notmuchmail.org>;\r
17         Mon, 24 Nov 2014 02:44:35 -0800 (PST)\r
18 Received: from mail-wi0-f175.google.com (mail-wi0-f175.google.com\r
19         [209.85.212.175]) (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 726FE431FBC\r
22         for <notmuch@notmuchmail.org>; Mon, 24 Nov 2014 02:44:35 -0800 (PST)\r
23 Received: by mail-wi0-f175.google.com with SMTP id l15so5192792wiw.2\r
24         for <notmuch@notmuchmail.org>; Mon, 24 Nov 2014 02:44:33 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:user-agent:from:to:subject:date:message-id;\r
28         bh=xjV5NQcf5hT6+6v4H6Ui+UknvaZ0tlFMq7h6dt3ftcw=;\r
29         b=IAUUXQTA8j3CT7nMULZx019XqFkLpngTHZkkuv3U/g4oHUDC0lLKi+ftRwMwPKhyjA\r
30         iZVMS62jmBwoU1grLVGHdPzIHfUx40priVekxgiMn/CM21Z1vWX6sBcgaUsHEnjWdjRf\r
31         GnbXuUZezDSnv5n45AQ8owQrsumKPiliIuDenbEv/RRFRkEzRxihngSwl3MNULRfMqaf\r
32         C2cf2+exXFYV5kJHDc4trLU2FFZYq8eWJe4brPaMiLlxZCHo3rsnHUvgrYqXVRbI4mS8\r
33         R5lu5EW83ZbKbHCnyhTboqof+gBPMhf7yytPexks/PYgzwDwlT2tpYnv1V5EJjkEuilY\r
34         r72g==\r
35 X-Gm-Message-State:\r
36  ALoCoQnCVqWL8ShdLKZCLydMldj9U9lCgORb0ZjY4RU+ohyZ4d37v9Ume2/Gw0+xqxK3WpEFBYyd\r
37 X-Received: by 10.194.81.70 with SMTP id y6mr31256421wjx.113.1416825872035;\r
38         Mon, 24 Nov 2014 02:44:32 -0800 (PST)\r
39 Received: from disaster-area.hh.sledj.net\r
40         ([2a01:348:1a2:1:ea39:35ff:fe2c:a227])\r
41         by mx.google.com with ESMTPSA id\r
42         ec2sm11434921wib.23.2014.11.24.02.44.31 for <notmuch@notmuchmail.org>\r
43         (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
44         Mon, 24 Nov 2014 02:44:31 -0800 (PST)\r
45 Received: from localhost (30000@localhost [local]);\r
46         by localhost (OpenSMTPD) with ESMTPA id 1811d9ff;\r
47         for <notmuch@notmuchmail.org>; Mon, 24 Nov 2014 10:44:29 +0000 (UTC)\r
48 User-Agent: OpenSMTPD enqueuer (Demoostik)\r
49 From: David Edmondson <dme@dme.org>\r
50 To: notmuch@notmuchmail.org\r
51 Subject: [RFC][PATCH v1] emacs: Replace `notmuch-search-result-format'\r
52         evaluation.\r
53 Date: Mon, 24 Nov 2014 10:44:29 +0000\r
54 Message-Id: <1416825869-28472-1-git-send-email-dme@dme.org>\r
55 X-Mailer: git-send-email 2.1.3\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Mon, 24 Nov 2014 10:44:43 -0000\r
69 \r
70 Alternative implementation of code that interprets\r
71 `notmuch-search-result-format' to insert the results of a search.\r
72 ---\r
73 \r
74 First pass at this. Thoughts?\r
75 \r
76  emacs/notmuch.el | 260 +++++++++++++++++++++++++++++--------------------------\r
77  1 file changed, 138 insertions(+), 122 deletions(-)\r
78 \r
79 diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
80 index 218486a..0490936 100644\r
81 --- a/emacs/notmuch.el\r
82 +++ b/emacs/notmuch.el\r
83 @@ -66,23 +66,63 @@\r
84      "Placeholder variable when notmuch-version.el[c] is not available."))\r
85  \r
86  (defcustom notmuch-search-result-format\r
87 -  `(("date" . "%12s ")\r
88 -    ("count" . "%-7s ")\r
89 -    ("authors" . "%-20s ")\r
90 -    ("subject" . "%s ")\r
91 -    ("tags" . "(%s)"))\r
92 -  "Search result formatting. Supported fields are:\r
93 -       date, count, authors, subject, tags\r
94 -For example:\r
95 -       (setq notmuch-search-result-format \(\(\"authors\" . \"%-40s\"\)\r
96 -                                            \(\"subject\" . \"%s\"\)\)\)\r
97 -Line breaks are permitted in format strings (though this is\r
98 -currently experimental).  Note that a line break at the end of an\r
99 -\"authors\" field will get elided if the authors list is long;\r
100 -place it instead at the beginning of the following field.  To\r
101 -enter a line break when setting this variable with setq, use \\n.\r
102 -To enter a line break in customize, press \\[quoted-insert] C-j."\r
103 -  :type '(alist :key-type (string) :value-type (string))\r
104 +  '((:width -12 :date_relative) " "\r
105 +    (:width -7 (:concat "[" :matched "/" :total "]")) " "\r
106 +    ;; This splitting will not be necessary once\r
107 +    ;; id:1414172643-28270-1-git-send-email-dme@dme.org is integrated.\r
108 +    (:width 40 (:eval (let* ((split-authors (split-string\r
109 +                                            (plist-get notmuch-thread :authors)\r
110 +                                            "|" t split-string-default-separators))\r
111 +                            ;; There will always be matched authors.\r
112 +                            (matched-authors (car split-authors))\r
113 +                            ;; There may not be non-matched authors.\r
114 +                            (non-matched-authors (mapconcat #'identity (cdr split-authors) " ")))\r
115 +                       (notmuch-search-format-authors matched-authors non-matched-authors)))) " "\r
116 +    :subject " "\r
117 +    "(" (:eval (notmuch-tag-format-tags (plist-get notmuch-thread :tags)\r
118 +                                       (plist-get notmuch-thread :orig-tags)))\r
119 +    ")" "\n")\r
120 +\r
121 +  "Template for displaying search results.\r
122 +\r
123 +The value is a list of items to insert in the search\r
124 +results. Individual items should be in one of the following\r
125 +forms:\r
126 +\r
127 +A string that is inserted directly (e.g. \" \").\r
128 +\r
129 +A number that is inserted directly (e.g. 5).\r
130 +\r
131 +A list that is recursively evaluated (e.g. `(:concat FORM1\r
132 + FORM2)').\r
133 +\r
134 +A function that is evaluated with a single argument, the current\r
135 + thread.\r
136 +\r
137 +A symbol corresponding to an attribute of the\r
138 + thread (e.g. `:date_relative' or `:subject').\r
139 +\r
140 +A directive that describes how to evaluate the remainder of the\r
141 + list. The following directives are available:\r
142 +\r
143 + `:concat FORM...' -- evaluate each element of FORM and return\r
144 + the concatenated results,\r
145 +\r
146 + `:width WIDTH FORM' -- evaluate FORM and return the result\r
147 + truncated to WIDTH characters. The result will be padded with\r
148 + spaces to WIDTH characters, with padding at on the left if the\r
149 + WIDTH is negative.\r
150 +\r
151 + `:tag TAG FORM' -- if the thread has the tag TAG, insert the\r
152 + result of evaluating FORM.\r
153 +\r
154 + `:eval COMPLEX-FORM' -- evaluate COMPLEX-FORM as emacs lisp and\r
155 + evaluate the result.\r
156 +\r
157 +During the evaluation of `notmuch-search-result-format', the\r
158 +symbol `notmuch-thread' is bound to the current thread (a\r
159 +property list), which can then be used in `:eval' forms."\r
160 +  :type 'list\r
161    :group 'notmuch-search)\r
162  \r
163  ;; The name of this variable `notmuch-init-file' is consistent with the\r
164 @@ -672,109 +712,84 @@ foreground and blue background."\r
165         ;; Reverse the list so earlier entries take precedence\r
166         (reverse notmuch-search-line-faces)))\r
167  \r
168 -(defun notmuch-search-author-propertize (authors)\r
169 -  "Split `authors' into matching and non-matching authors and\r
170 -propertize appropriately. If no boundary between authors and\r
171 -non-authors is found, assume that all of the authors match."\r
172 -  (if (string-match "\\(.*\\)|\\(.*\\)" authors)\r
173 -      (concat (propertize (concat (match-string 1 authors) ",")\r
174 -                         'face 'notmuch-search-matching-authors)\r
175 -             (propertize (match-string 2 authors)\r
176 -                         'face 'notmuch-search-non-matching-authors))\r
177 -    (propertize authors 'face 'notmuch-search-matching-authors)))\r
178 -\r
179 -(defun notmuch-search-insert-authors (format-string authors)\r
180 -  ;; Save the match data to avoid interfering with\r
181 -  ;; `notmuch-search-process-filter'.\r
182 -  (save-match-data\r
183 -    (let* ((formatted-authors (format format-string authors))\r
184 -          (formatted-sample (format format-string ""))\r
185 -          (visible-string formatted-authors)\r
186 -          (invisible-string "")\r
187 -          (padding ""))\r
188 -\r
189 -      ;; Truncate the author string to fit the specification.\r
190 -      (if (> (length formatted-authors)\r
191 -            (length formatted-sample))\r
192 -         (let ((visible-length (- (length formatted-sample)\r
193 -                                  (length "... "))))\r
194 -           ;; Truncate the visible string according to the width of\r
195 -           ;; the display string.\r
196 -           (setq visible-string (substring formatted-authors 0 visible-length)\r
197 -                 invisible-string (substring formatted-authors visible-length))\r
198 -           ;; If possible, truncate the visible string at a natural\r
199 -           ;; break (comma or pipe), as incremental search doesn't\r
200 -           ;; match across the visible/invisible border.\r
201 -           (when (string-match "\\(.*\\)\\([,|] \\)\\([^,|]*\\)" visible-string)\r
202 -             ;; Second clause is destructive on `visible-string', so\r
203 -             ;; order is important.\r
204 -             (setq invisible-string (concat (match-string 3 visible-string)\r
205 -                                            invisible-string)\r
206 -                   visible-string (concat (match-string 1 visible-string)\r
207 -                                          (match-string 2 visible-string))))\r
208 -           ;; `visible-string' may be shorter than the space allowed\r
209 -           ;; by `format-string'. If so we must insert some padding\r
210 -           ;; after `invisible-string'.\r
211 -           (setq padding (make-string (- (length formatted-sample)\r
212 -                                         (length visible-string)\r
213 -                                         (length "..."))\r
214 -                                      ? ))))\r
215 -\r
216 -      ;; Use different faces to show matching and non-matching authors.\r
217 -      (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)\r
218 -         ;; The visible string contains both matching and\r
219 -         ;; non-matching authors.\r
220 -         (setq visible-string (notmuch-search-author-propertize visible-string)\r
221 -               ;; The invisible string must contain only non-matching\r
222 -               ;; authors, as the visible-string contains both.\r
223 -               invisible-string (propertize invisible-string\r
224 -                                            'face 'notmuch-search-non-matching-authors))\r
225 -       ;; The visible string contains only matching authors.\r
226 -       (setq visible-string (propertize visible-string\r
227 -                                        'face 'notmuch-search-matching-authors)\r
228 -             ;; The invisible string may contain both matching and\r
229 -             ;; non-matching authors.\r
230 -             invisible-string (notmuch-search-author-propertize invisible-string)))\r
231 -\r
232 -      ;; If there is any invisible text, add it as a tooltip to the\r
233 -      ;; visible text.\r
234 -      (when (not (string= invisible-string ""))\r
235 -       (setq visible-string (propertize visible-string 'help-echo (concat "..." invisible-string))))\r
236 -\r
237 -      ;; Insert the visible and, if present, invisible author strings.\r
238 -      (insert visible-string)\r
239 -      (when (not (string= invisible-string ""))\r
240 -       (let ((start (point))\r
241 -             overlay)\r
242 -         (insert invisible-string)\r
243 -         (setq overlay (make-overlay start (point)))\r
244 -         (overlay-put overlay 'invisible 'ellipsis)\r
245 -         (overlay-put overlay 'isearch-open-invisible #'delete-overlay)))\r
246 -      (insert padding))))\r
247 -\r
248 -(defun notmuch-search-insert-field (field format-string result)\r
249 +(defun notmuch-search-format-authors (matched non-matched)\r
250 +  (if (string-equal "" non-matched)\r
251 +      (propertize matched 'face 'notmuch-search-matching-authors)\r
252 +    (concat (propertize (concat matched ", ")\r
253 +                       'face 'notmuch-search-matching-authors)\r
254 +           (propertize non-matched\r
255 +                       'face 'notmuch-search-non-matching-authors))))\r
256 +\r
257 +(defun notmuch-search-make-width (width string)\r
258 +  (let ((neg (< 0 width))\r
259 +       (width (abs width))\r
260 +       (orig-len (length string)))\r
261 +\r
262 +    (if (< width orig-len)\r
263 +       ;; A sub-set of the string will be visible.\r
264 +       (let* ((ellipsis "...")\r
265 +              (visible-len (- width (length ellipsis)))\r
266 +              (visible-string (substring string 0 visible-len))\r
267 +              (invisible-string (substring string visible-len)))\r
268 +\r
269 +         (concat (propertize (concat visible-string\r
270 +                                     ;; The ellipsis should share the\r
271 +                                     ;; face of the preceding\r
272 +                                     ;; character.\r
273 +                                     (propertize ellipsis 'face\r
274 +                                                 (get-text-property visible-len 'face string)))\r
275 +                             'help-echo (concat ellipsis invisible-string))))\r
276 +\r
277 +      ;; All of the string is visible - pad it.\r
278 +      (concat (if neg string)\r
279 +             (make-string (- width orig-len) ? )\r
280 +             (if neg "" string)))))\r
281 +\r
282 +(defun notmuch-search-elem-repr (elem thread)\r
283    (cond\r
284 -   ((string-equal field "date")\r
285 -    (insert (propertize (format format-string (plist-get result :date_relative))\r
286 -                       'face 'notmuch-search-date)))\r
287 -   ((string-equal field "count")\r
288 -    (insert (propertize (format format-string\r
289 -                               (format "[%s/%s]" (plist-get result :matched)\r
290 -                                       (plist-get result :total)))\r
291 -                       'face 'notmuch-search-count)))\r
292 -   ((string-equal field "subject")\r
293 -    (insert (propertize (format format-string\r
294 -                               (notmuch-sanitize (plist-get result :subject)))\r
295 -                       'face 'notmuch-search-subject)))\r
296 -\r
297 -   ((string-equal field "authors")\r
298 -    (notmuch-search-insert-authors\r
299 -     format-string (notmuch-sanitize (plist-get result :authors))))\r
300 -\r
301 -   ((string-equal field "tags")\r
302 -    (let ((tags (plist-get result :tags))\r
303 -         (orig-tags (plist-get result :orig-tags)))\r
304 -      (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))\r
305 +   ((not elem)\r
306 +    "")\r
307 +   \r
308 +   ((numberp elem)\r
309 +    (format "%d" elem))\r
310 +\r
311 +   ((stringp elem)\r
312 +    elem)\r
313 +\r
314 +   ((functionp elem)\r
315 +    (funcall elem thread))\r
316 +\r
317 +   ((listp elem)\r
318 +    (let ((op (car elem))\r
319 +         (rest (cdr elem)))\r
320 +      (case op\r
321 +       (:concat\r
322 +        (mapconcat (lambda (inner-elem)\r
323 +                     (notmuch-search-elem-repr inner-elem thread))\r
324 +                   rest ""))\r
325 +\r
326 +       (:width\r
327 +        (notmuch-search-make-width (car rest)\r
328 +                                   (notmuch-search-elem-repr (cadr rest) thread)))\r
329 +\r
330 +       (:tag\r
331 +        (when (member (car rest) (plist-get thread :tags))\r
332 +          (notmuch-search-elem-repr (cadr rest) thread)))\r
333 +\r
334 +       (:eval\r
335 +        (notmuch-search-elem-repr (apply #'eval rest) thread))\r
336 +\r
337 +       (otherwise\r
338 +        (mapconcat\r
339 +         (lambda (inner-elem) (notmuch-search-elem-repr inner-elem thread))\r
340 +         elem "")))))\r
341 +\r
342 +   (t\r
343 +    (let ((val (plist-get thread elem)))\r
344 +      (if val\r
345 +         (notmuch-search-elem-repr val thread)\r
346 +       (message "Unknown message attribute in `notmuch-search-elem-repr': %s" elem)\r
347 +       "")))))\r
348  \r
349  (defun notmuch-search-show-result (result pos)\r
350    "Insert RESULT at POS."\r
351 @@ -782,9 +797,10 @@ non-authors is found, assume that all of the authors match."\r
352    (unless (= (plist-get result :matched) 0)\r
353      (save-excursion\r
354        (goto-char pos)\r
355 -      (dolist (spec notmuch-search-result-format)\r
356 -       (notmuch-search-insert-field (car spec) (cdr spec) result))\r
357 -      (insert "\n")\r
358 +       ;; `notmuch-thread' is a well known symbol for functions to\r
359 +       ;; use during evaluation of `notmuch-search-result-format'.\r
360 +      (insert (let ((notmuch-thread result))\r
361 +               (notmuch-search-elem-repr notmuch-search-result-format notmuch-thread)))\r
362        (notmuch-search-color-line pos (point) (plist-get result :tags))\r
363        (put-text-property pos (point) 'notmuch-search-result result))))\r
364  \r
365 -- \r
366 2.1.3\r
367 \r