]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: get rid of trailing spaces in notmuch-hello view
[notmuch] / emacs / notmuch-hello.el
1 ;; notmuch-hello.el --- welcome to notmuch, a frontend
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (eval-when-compile (require 'cl))
23 (require 'widget)
24 (require 'wid-edit) ; For `widget-forward'.
25
26 (require 'notmuch-lib)
27 (require 'notmuch-mua)
28
29 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
30 (declare-function notmuch-poll "notmuch" ())
31
32 (defcustom notmuch-hello-recent-searches-max 10
33   "The number of recent searches to display."
34   :type 'integer
35   :group 'notmuch-hello)
36
37 (defcustom notmuch-show-empty-saved-searches nil
38   "Should saved searches with no messages be listed?"
39   :type 'boolean
40   :group 'notmuch-hello)
41
42 (defun notmuch-sort-saved-searches (alist)
43   "Generate an alphabetically sorted saved searches alist."
44   (sort (copy-sequence alist) (lambda (a b) (string< (car a) (car b)))))
45
46 (defcustom notmuch-saved-search-sort-function nil
47   "Function used to sort the saved searches for the notmuch-hello view.
48
49 This variable controls how saved searches should be sorted. No
50 sorting (nil) displays the saved searches in the order they are
51 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
52 saved searches in alphabetical order. Custom sort function should
53 be a function or a lambda expression that takes the saved
54 searches alist as a parameter, and returns a new saved searches
55 alist to be used."
56   :type '(choice (const :tag "No sorting" nil)
57                  (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
58                  (function :tag "Custom sort function"
59                            :value notmuch-sort-saved-searches))
60   :group 'notmuch-hello)
61
62 (defvar notmuch-hello-indent 4
63   "How much to indent non-headers.")
64
65 (defcustom notmuch-show-logo t
66   "Should the notmuch logo be shown?"
67   :type 'boolean
68   :group 'notmuch-hello)
69
70 (defcustom notmuch-show-all-tags-list nil
71   "Should all tags be shown in the notmuch-hello view?"
72   :type 'boolean
73   :group 'notmuch-hello)
74
75 (defcustom notmuch-hello-tag-list-make-query nil
76   "Function or string to generate queries for the all tags list.
77
78 This variable controls which query results are shown for each tag
79 in the \"all tags\" list. If nil, it will use all messages with
80 that tag. If this is set to a string, it is used as a filter for
81 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
82 Finally this can be a function that will be called for each tag and
83 should return a filter for that tag, or nil to hide the tag."
84   :type '(choice (const :tag "All messages" nil)
85                  (const :tag "Unread messages" "tag:unread")
86                  (string :tag "Custom filter"
87                          :value "tag:unread")
88                  (function :tag "Custom filter function"))
89   :group 'notmuch-hello)
90
91 (defcustom notmuch-hello-hide-tags nil
92   "List of tags to be hidden in the \"all tags\"-section."
93   :type '(repeat string)
94   :group 'notmuch-hello)
95
96 (defface notmuch-hello-logo-background
97   '((((class color)
98       (background dark))
99      (:background "#5f5f5f"))
100     (((class color)
101       (background light))
102      (:background "white")))
103   "Background colour for the notmuch logo."
104   :group 'notmuch-hello
105   :group 'notmuch-faces)
106
107 (defcustom notmuch-column-control t
108   "Controls the number of columns for saved searches/tags in notmuch view.
109
110 This variable has three potential sets of values:
111
112 - t: automatically calculate the number of columns possible based
113   on the tags to be shown and the window width,
114 - an integer: a lower bound on the number of characters that will
115   be used to display each column,
116 - a float: a fraction of the window width that is the lower bound
117   on the number of characters that should be used for each
118   column.
119
120 So:
121 - if you would like two columns of tags, set this to 0.5.
122 - if you would like a single column of tags, set this to 1.0.
123 - if you would like tags to be 30 characters wide, set this to
124   30.
125 - if you don't want to worry about all of this nonsense, leave
126   this set to `t'."
127   :type '(choice
128           (const :tag "Automatically calculated" t)
129           (integer :tag "Number of characters")
130           (float :tag "Fraction of window"))
131   :group 'notmuch-hello)
132
133 (defcustom notmuch-hello-thousands-separator " "
134   "The string used as a thousands separator.
135
136 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
137 The latter is recommended in the SI/ISO 31-0 standard and by the
138 International Bureau of Weights and Measures."
139   :type 'string
140   :group 'notmuch-hello)
141
142 (defcustom notmuch-hello-mode-hook nil
143   "Functions called after entering `notmuch-hello-mode'."
144   :type 'hook
145   :group 'notmuch-hello
146   :group 'notmuch-hooks)
147
148 (defcustom notmuch-hello-refresh-hook nil
149   "Functions called after updating a `notmuch-hello' buffer."
150   :type 'hook
151   :group 'notmuch-hello
152   :group 'notmuch-hooks)
153
154 (defvar notmuch-hello-url "http://notmuchmail.org"
155   "The `notmuch' web site.")
156
157 (defvar notmuch-hello-search-pos nil
158   "Position of search widget, if any.
159
160 This should only be set by `notmuch-hello-insert-search'.")
161
162 (defvar notmuch-hello-custom-section-options
163   '((:filter (string :tag "Filter for each tag"))
164     (:filter-count (string :tag "Different filter to generate message counts"))
165     (:initially-hidden (const :tag "Hide this section on startup" t))
166     (:show-empty-searches (const :tag "Show queries with no matching messages" t))
167     (:hide-if-empty (const :tag "Hide this section if all queries are empty
168 \(and not shown by show-empty-searches)" t)))
169   "Various customization-options for notmuch-hello-tags/query-section.")
170
171 (define-widget 'notmuch-hello-tags-section 'lazy
172   "Customize-type for notmuch-hello tag-list sections."
173   :tag "Customized tag-list section (see docstring for details)"
174   :type
175   `(list :tag ""
176          (const :tag "" notmuch-hello-insert-tags-section)
177          (string :tag "Title for this section")
178          (plist
179           :inline t
180           :options
181           ,(append notmuch-hello-custom-section-options
182                    '((:hide-tags (repeat :tag "Tags that will be hidden"
183                                          string)))))))
184
185 (define-widget 'notmuch-hello-query-section 'lazy
186   "Customize-type for custom saved-search-like sections"
187   :tag "Customized queries section (see docstring for details)"
188   :type
189   `(list :tag ""
190          (const :tag "" notmuch-hello-insert-query-list)
191          (string :tag "Title for this section")
192          (repeat :tag "Queries"
193                  (cons (string :tag "Name") (string :tag "Query")))
194          (plist :inline t :options ,notmuch-hello-custom-section-options)))
195
196 (defcustom notmuch-hello-sections
197   (list #'notmuch-hello-insert-header
198         #'notmuch-hello-insert-saved-searches
199         #'notmuch-hello-insert-search
200         #'notmuch-hello-insert-recent-searches
201         #'notmuch-hello-insert-alltags
202         #'notmuch-hello-insert-footer)
203   "Sections for notmuch-hello.
204
205 The list contains functions which are used to construct sections in
206 notmuch-hello buffer.  When notmuch-hello buffer is constructed,
207 these functions are run in the order they appear in this list.  Each
208 function produces a section simply by adding content to the current
209 buffer.  A section should not end with an empty line, because a
210 newline will be inserted after each section by `notmuch-hello'.
211
212 Each function should take no arguments.  If the produced section
213 includes `notmuch-hello-target' (i.e. cursor should be positioned
214 inside this section), the function should return this element's
215 position.
216 Otherwise, it should return nil.
217
218 For convenience an element can also be a list of the form (FUNC ARG1
219 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
220 list.
221
222 A \"Customized tag-list section\" item in the customize-interface
223 displays a list of all tags, optionally hiding some of them. It
224 is also possible to filter the list of messages matching each tag
225 by an additional filter query. Similarly, the count of messages
226 displayed next to the buttons can be generated by applying a
227 different filter to the tag query. These filters are also
228 supported for \"Customized queries section\" items."
229   :group 'notmuch
230   :type
231   '(repeat
232     (choice (function-item notmuch-hello-insert-header)
233             (function-item notmuch-hello-insert-saved-searches)
234             (function-item notmuch-hello-insert-search)
235             (function-item notmuch-hello-insert-recent-searches)
236             (function-item notmuch-hello-insert-alltags)
237             (function-item notmuch-hello-insert-footer)
238             (function-item notmuch-hello-insert-inbox)
239             notmuch-hello-tags-section
240             notmuch-hello-query-section
241             (function :tag "Custom section"))))
242
243 (defvar notmuch-hello-target nil
244   "Button text at position of point before rebuilding the notmuch-buffer.
245
246 This variable contains the text of the button, if any, the
247 point was positioned at before the notmuch-hello buffer was
248 rebuilt. This should never actually be global and is defined as a
249 defvar only for documentation purposes and to avoid a compiler
250 warning about it occurring as a free variable.")
251
252 (defvar notmuch-hello-hidden-sections nil
253   "List of sections titles whose contents are hidden")
254
255 (defvar notmuch-hello-first-run t
256   "True if `notmuch-hello' is run for the first time, set to nil
257 afterwards.")
258
259 (defun notmuch-hello-nice-number (n)
260   (let (result)
261     (while (> n 0)
262       (push (% n 1000) result)
263       (setq n (/ n 1000)))
264     (setq result (or result '(0)))
265     (apply #'concat
266      (number-to-string (car result))
267      (mapcar (lambda (elem)
268               (format "%s%03d" notmuch-hello-thousands-separator elem))
269              (cdr result)))))
270
271 (defun notmuch-hello-trim (search)
272   "Trim whitespace."
273   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
274       (match-string 1 search)
275     search))
276
277 (defun notmuch-hello-search (&optional search)
278   (interactive)
279   (unless (null search)
280     (setq search (notmuch-hello-trim search))
281     (let ((history-delete-duplicates t))
282       (add-to-history 'notmuch-search-history search)))
283   (notmuch-search search notmuch-search-oldest-first nil nil
284                   #'notmuch-hello-search-continuation))
285
286 (defun notmuch-hello-add-saved-search (widget)
287   (interactive)
288   (let ((search (widget-value
289                  (symbol-value
290                   (widget-get widget :notmuch-saved-search-widget))))
291         (name (completing-read "Name for saved search: "
292                                notmuch-saved-searches)))
293     ;; If an existing saved search with this name exists, remove it.
294     (setq notmuch-saved-searches
295           (loop for elem in notmuch-saved-searches
296                 if (not (equal name
297                                (car elem)))
298                 collect elem))
299     ;; Add the new one.
300     (customize-save-variable 'notmuch-saved-searches
301                              (add-to-list 'notmuch-saved-searches
302                                           (cons name search) t))
303     (message "Saved '%s' as '%s'." search name)
304     (notmuch-hello-update)))
305
306 (defun notmuch-hello-longest-label (searches-alist)
307   (or (loop for elem in searches-alist
308             maximize (length (car elem)))
309       0))
310
311 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
312   (let ((len (length list)))
313     (loop for col from 0 to (- ncols 1)
314           collect (let ((offset (+ (* nrows col) row)))
315                     (if (< offset len)
316                         (nth offset list)
317                       ;; Don't forget to insert an empty slot in the
318                       ;; output matrix if there is no corresponding
319                       ;; value in the input matrix.
320                       nil)))))
321
322 (defun notmuch-hello-reflect (list ncols)
323   "Reflect a `ncols' wide matrix represented by `list' along the
324 diagonal."
325   ;; Not very lispy...
326   (let ((nrows (ceiling (length list) ncols)))
327     (loop for row from 0 to (- nrows 1)
328           append (notmuch-hello-reflect-generate-row ncols nrows row list))))
329
330 (defun notmuch-hello-widget-search (widget &rest ignore)
331   (notmuch-search (widget-get widget
332                               :notmuch-search-terms)
333                   notmuch-search-oldest-first
334                   nil nil #'notmuch-hello-search-continuation))
335
336 (defun notmuch-saved-search-count (search)
337   (car (process-lines notmuch-command "count" search)))
338
339 (defun notmuch-hello-tags-per-line (widest)
340   "Determine how many tags to show per line and how wide they
341 should be. Returns a cons cell `(tags-per-line width)'."
342   (let ((tags-per-line
343          (cond
344           ((integerp notmuch-column-control)
345            (max 1
346                 (/ (- (window-width) notmuch-hello-indent)
347                    ;; Count is 9 wide (8 digits plus space), 1 for the space
348                    ;; after the name.
349                    (+ 9 1 (max notmuch-column-control widest)))))
350
351           ((floatp notmuch-column-control)
352            (let* ((available-width (- (window-width) notmuch-hello-indent))
353                   (proposed-width (max (* available-width notmuch-column-control) widest)))
354              (floor available-width proposed-width)))
355
356           (t
357            (max 1
358                 (/ (- (window-width) notmuch-hello-indent)
359                    ;; Count is 9 wide (8 digits plus space), 1 for the space
360                    ;; after the name.
361                    (+ 9 1 widest)))))))
362
363     (cons tags-per-line (/ (max 1
364                                 (- (window-width) notmuch-hello-indent
365                                    ;; Count is 9 wide (8 digits plus
366                                    ;; space), 1 for the space after the
367                                    ;; name.
368                                    (* tags-per-line (+ 9 1))))
369                            tags-per-line))))
370
371 (defun notmuch-hello-filtered-query (query filter)
372   "Constructs a query to search all messages matching QUERY and FILTER.
373
374 If FILTER is a string, it is directly used in the returned query.
375
376 If FILTER is a function, it is called with QUERY as a parameter and
377 the string it returns is used as the query. If nil is returned,
378 the entry is hidden.
379
380 Otherwise, FILTER is ignored.
381 "
382   (cond
383    ((functionp filter) (funcall filter query))
384    ((stringp filter)
385     (concat "(" query ") and (" filter ")"))
386    (t query)))
387
388 (defun notmuch-hello-query-counts (query-alist &rest options)
389   "Compute list of counts of matched messages from QUERY-ALIST.
390
391 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
392 or (NAME QUERY COUNT-QUERY). If the latter form is used,
393 COUNT-QUERY specifies an alternate query to be used to generate
394 the count for the associated query.
395
396 The result is the list of elements of the form (NAME QUERY COUNT).
397
398 The values :show-empty-searches, :filter and :filter-count from
399 options will be handled as specified for
400 `notmuch-hello-insert-searches'."
401   (notmuch-remove-if-not
402    #'identity
403    (mapcar
404     (lambda (elem)
405       (let* ((name (car elem))
406              (query-and-count (if (consp (cdr elem))
407                                   ;; do we have a different query for the message count?
408                                   (cons (second elem) (third elem))
409                                 (cons (cdr elem) (cdr elem))))
410              (message-count
411               (string-to-number
412                (notmuch-saved-search-count
413                 (notmuch-hello-filtered-query (cdr query-and-count)
414                                               (or (plist-get options :filter-count)
415                                                  (plist-get options :filter)))))))
416         (and (or (plist-get options :show-empty-searches) (> message-count 0))
417              (list name (notmuch-hello-filtered-query
418                          (car query-and-count) (plist-get options :filter))
419                    message-count))))
420     query-alist)))
421
422 (defun notmuch-hello-insert-buttons (searches)
423   "Insert buttons for SEARCHES.
424
425 SEARCHES must be a list containing lists of the form (NAME QUERY COUNT), where
426 QUERY is the query to start when the button for the corresponding entry is
427 activated. COUNT should be the number of messages matching the query.
428 Such a list can be computed with `notmuch-hello-query-counts'."
429   (let* ((widest (notmuch-hello-longest-label searches))
430          (tags-and-width (notmuch-hello-tags-per-line widest))
431          (tags-per-line (car tags-and-width))
432          (column-width (cdr tags-and-width))
433          (column-indent 0)
434          (count 0)
435          (reordered-list (notmuch-hello-reflect searches tags-per-line))
436          ;; Hack the display of the buttons used.
437          (widget-push-button-prefix "")
438          (widget-push-button-suffix "")
439          (found-target-pos nil))
440     ;; dme: It feels as though there should be a better way to
441     ;; implement this loop than using an incrementing counter.
442     (mapc (lambda (elem)
443             ;; (not elem) indicates an empty slot in the matrix.
444             (when elem
445               (if (> column-indent 0)
446                   (widget-insert (make-string column-indent ? )))
447               (let* ((name (first elem))
448                      (query (second elem))
449                      (msg-count (third elem)))
450                 (widget-insert (format "%8s "
451                                        (notmuch-hello-nice-number msg-count)))
452                 (if (string= name notmuch-hello-target)
453                     (setq found-target-pos (point-marker)))
454                 (widget-create 'push-button
455                                :notify #'notmuch-hello-widget-search
456                                :notmuch-search-terms query
457                                name)
458                 (setq column-indent
459                       (1+ (max 0 (- column-width (length name)))))))
460             (setq count (1+ count))
461             (when (eq (% count tags-per-line) 0)
462               (setq column-indent 0)
463               (widget-insert "\n")))
464           reordered-list)
465
466     ;; If the last line was not full (and hence did not include a
467     ;; carriage return), insert one now.
468     (unless (eq (% count tags-per-line) 0)
469       (widget-insert "\n"))
470     found-target-pos))
471
472 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
473
474 (defun notmuch-hello-search-continuation()
475   (notmuch-hello-update t))
476
477 (defun notmuch-hello-update (&optional no-display)
478   "Update the current notmuch view."
479   ;; Lazy - rebuild everything.
480   (interactive)
481   (notmuch-hello no-display))
482
483 (defun notmuch-hello-poll-and-update ()
484   "Invoke `notmuch-poll' to import mail, then refresh the current view."
485   (interactive)
486   (notmuch-poll)
487   (notmuch-hello-update))
488
489
490 (defvar notmuch-hello-mode-map
491   (let ((map (make-sparse-keymap)))
492     (set-keymap-parent map widget-keymap)
493     (define-key map "v" (lambda () "Display the notmuch version" (interactive)
494                           (message "notmuch version %s" (notmuch-version))))
495     (define-key map "?" 'notmuch-help)
496     (define-key map "q" 'notmuch-kill-this-buffer)
497     (define-key map "=" 'notmuch-hello-update)
498     (define-key map "G" 'notmuch-hello-poll-and-update)
499     (define-key map (kbd "<C-tab>") 'widget-backward)
500     (define-key map "m" 'notmuch-mua-new-mail)
501     (define-key map "s" 'notmuch-hello-search)
502     map)
503   "Keymap for \"notmuch hello\" buffers.")
504 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
505
506 (defun notmuch-hello-mode ()
507  "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
508
509 Complete list of currently available key bindings:
510
511 \\{notmuch-hello-mode-map}"
512  (interactive)
513  (kill-all-local-variables)
514  (use-local-map notmuch-hello-mode-map)
515  (setq major-mode 'notmuch-hello-mode
516         mode-name "notmuch-hello")
517  (run-mode-hooks 'notmuch-hello-mode-hook)
518  ;;(setq buffer-read-only t)
519 )
520
521 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
522   "Return an alist from tags to queries to display in the all-tags section."
523   (mapcar (lambda (tag)
524             (cons tag (format "tag:%s" tag)))
525           (notmuch-remove-if-not
526            (lambda (tag)
527              (not (member tag hide-tags)))
528            (process-lines notmuch-command "search-tags"))))
529
530 (defun notmuch-hello-insert-header ()
531   "Insert the default notmuch-hello header."
532   (when notmuch-show-logo
533     (let ((image notmuch-hello-logo))
534       ;; The notmuch logo uses transparency. That can display poorly
535       ;; when inserting the image into an emacs buffer (black logo on
536       ;; a black background), so force the background colour of the
537       ;; image. We use a face to represent the colour so that
538       ;; `defface' can be used to declare the different possible
539       ;; colours, which depend on whether the frame has a light or
540       ;; dark background.
541       (setq image (cons 'image
542                         (append (cdr image)
543                                 (list :background (face-background 'notmuch-hello-logo-background)))))
544       (insert-image image))
545     (widget-insert "  "))
546
547   (widget-insert "Welcome to ")
548   ;; Hack the display of the links used.
549   (let ((widget-link-prefix "")
550         (widget-link-suffix ""))
551     (widget-create 'link
552                    :notify (lambda (&rest ignore)
553                              (browse-url notmuch-hello-url))
554                    :help-echo "Visit the notmuch website."
555                    "notmuch")
556     (widget-insert ". ")
557     (widget-insert "You have ")
558     (widget-create 'link
559                    :notify (lambda (&rest ignore)
560                              (notmuch-hello-update))
561                    :help-echo "Refresh"
562                    (notmuch-hello-nice-number
563                     (string-to-number (car (process-lines notmuch-command "count")))))
564     (widget-insert " messages.\n")))
565
566
567 (defun notmuch-hello-insert-saved-searches ()
568   "Insert the saved-searches section."
569   (let ((searches (notmuch-hello-query-counts
570                    (if notmuch-saved-search-sort-function
571                        (funcall notmuch-saved-search-sort-function
572                                 notmuch-saved-searches)
573                      notmuch-saved-searches)
574                    :show-empty-searches notmuch-show-empty-saved-searches))
575         found-target-pos)
576     (when searches
577       (widget-insert "Saved searches: ")
578       (widget-create 'push-button
579                      :notify (lambda (&rest ignore)
580                                (customize-variable 'notmuch-saved-searches))
581                      "edit")
582       (widget-insert "\n\n")
583       (let ((start (point)))
584         (setq found-target-pos
585               (notmuch-hello-insert-buttons searches))
586         (indent-rigidly start (point) notmuch-hello-indent)
587         found-target-pos))))
588
589 (defun notmuch-hello-insert-search ()
590   "Insert a search widget."
591   (widget-insert "Search: ")
592   (setq notmuch-hello-search-pos (point-marker))
593   (widget-create 'editable-field
594                  ;; Leave some space at the start and end of the
595                  ;; search boxes.
596                  :size (max 8 (- (window-width) notmuch-hello-indent
597                                  (length "Search: ")))
598                  :action (lambda (widget &rest ignore)
599                            (notmuch-hello-search (widget-value widget))))
600   ;; Add an invisible dot to make `widget-end-of-line' ignore
601   ;; trailing spaces in the search widget field.  A dot is used
602   ;; instead of a space to make `show-trailing-whitespace'
603   ;; happy, i.e. avoid it marking the whole line as trailing
604   ;; spaces.
605   (widget-insert ".")
606   (put-text-property (1- (point)) (point) 'invisible t)
607   (widget-insert "\n"))
608
609 (defun notmuch-hello-insert-recent-searches ()
610   "Insert recent searches."
611   (when notmuch-search-history
612     (widget-insert "Recent searches: ")
613     (widget-create 'push-button
614                    :notify (lambda (&rest ignore)
615                              (setq notmuch-search-history nil)
616                              (notmuch-hello-update))
617                    "clear")
618     (widget-insert "\n\n")
619     (let ((start (point)))
620       (loop for i from 1 to notmuch-hello-recent-searches-max
621             for search in notmuch-search-history do
622             (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
623               (set widget-symbol
624                    (widget-create 'editable-field
625                                   ;; Don't let the search boxes be
626                                   ;; less than 8 characters wide.
627                                   :size (max 8
628                                              (- (window-width)
629                                                 ;; Leave some space
630                                                 ;; at the start and
631                                                 ;; end of the
632                                                 ;; boxes.
633                                                 (* 2 notmuch-hello-indent)
634                                                 ;; 1 for the space
635                                                 ;; before the
636                                                 ;; `[save]' button. 6
637                                                 ;; for the `[save]'
638                                                 ;; button.
639                                                 1 6))
640                                   :action (lambda (widget &rest ignore)
641                                             (notmuch-hello-search (widget-value widget)))
642                                   search))
643               (widget-insert " ")
644               (widget-create 'push-button
645                              :notify (lambda (widget &rest ignore)
646                                        (notmuch-hello-add-saved-search widget))
647                              :notmuch-saved-search-widget widget-symbol
648                              "save"))
649             (widget-insert "\n"))
650       (indent-rigidly start (point) notmuch-hello-indent))
651     nil))
652
653 (defun notmuch-hello-insert-searches (title query-alist &rest options)
654   "Insert a section with TITLE showing a list of buttons made from QUERY-ALIST.
655
656 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
657 or (NAME QUERY COUNT-QUERY). If the latter form is used,
658 COUNT-QUERY specifies an alternate query to be used to generate
659 the count for the associated item.
660
661 Supports the following entries in OPTIONS as a plist:
662 :initially-hidden - if non-nil, section will be hidden on startup
663 :show-empty-searches - show buttons with no matching messages
664 :hide-if-empty - hide if no buttons would be shown
665    (only makes sense without :show-empty-searches)
666 :filter - This can be a function that takes the search query as its argument and
667    returns a filter to be used in conjuction with the query for that search or nil
668    to hide the element. This can also be a string that is used as a combined with
669    each query using \"and\".
670 :filter-count - Separate filter to generate the count displayed each search. Accepts
671    the same values as :filter. If :filter and :filter-count are specified, this
672    will be used instead of :filter, not in conjunction with it."
673   (widget-insert title ": ")
674   (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
675       (add-to-list 'notmuch-hello-hidden-sections title))
676   (let ((is-hidden (member title notmuch-hello-hidden-sections))
677         (start (point)))
678     (if is-hidden
679         (widget-create 'push-button
680                        :notify `(lambda (widget &rest ignore)
681                                   (setq notmuch-hello-hidden-sections
682                                         (delete ,title notmuch-hello-hidden-sections))
683                                   (notmuch-hello-update))
684                        "show")
685       (widget-create 'push-button
686                      :notify `(lambda (widget &rest ignore)
687                                 (add-to-list 'notmuch-hello-hidden-sections
688                                              ,title)
689                                 (notmuch-hello-update))
690                      "hide"))
691     (widget-insert "\n")
692     (let (target-pos)
693       (when (not is-hidden)
694         (let ((searches (apply 'notmuch-hello-query-counts query-alist options)))
695           (when (or (not (plist-get options :hide-if-empty))
696                     searches)
697             (widget-insert "\n")
698             (setq target-pos
699                   (notmuch-hello-insert-buttons searches))
700             (indent-rigidly start (point) notmuch-hello-indent))))
701       target-pos)))
702
703 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
704   "Insert a section displaying all tags with message counts.
705
706 TITLE defaults to \"All tags\".
707 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
708 following:
709
710 :hide-tags - List of tags that should be excluded."
711   (apply 'notmuch-hello-insert-searches
712          (or title "All tags")
713          (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
714          options))
715
716 (defun notmuch-hello-insert-inbox ()
717   "Show an entry for each saved search and inboxed messages for each tag"
718   (notmuch-hello-insert-searches "What's in your inbox"
719                                  (append
720                                   (notmuch-saved-searches)
721                                   (notmuch-hello-generate-tag-alist))
722                                  :filter "tag:inbox"))
723
724 (defun notmuch-hello-insert-alltags ()
725   "Insert a section displaying all tags and associated message counts"
726   (notmuch-hello-insert-tags-section
727    nil
728    :initially-hidden (not notmuch-show-all-tags-list)
729    :hide-tags notmuch-hello-hide-tags
730    :filter notmuch-hello-tag-list-make-query))
731
732 (defun notmuch-hello-insert-footer ()
733   "Insert the notmuch-hello footer."
734   (let ((start (point)))
735     (widget-insert "Type a search query and hit RET to view matching threads.\n")
736     (when notmuch-search-history
737       (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
738       (widget-insert "Save recent searches with the `save' button.\n"))
739     (when notmuch-saved-searches
740       (widget-insert "Edit saved searches with the `edit' button.\n"))
741     (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
742     (widget-insert "`=' to refresh this screen. `s' to search messages. `q' to quit.\n")
743     (widget-create 'link
744                    :notify (lambda (&rest ignore)
745                              (customize-variable 'notmuch-hello-sections))
746                    :button-prefix "" :button-suffix ""
747                    "Customize")
748     (widget-insert " this page.")
749     (let ((fill-column (- (window-width) notmuch-hello-indent)))
750       (center-region start (point)))))
751
752 ;;;###autoload
753 (defun notmuch-hello (&optional no-display)
754   "Run notmuch and display saved searches, known tags, etc."
755   (interactive)
756
757   ;; Jump through a hoop to get this value from the deprecated variable
758   ;; name (`notmuch-folders') or from the default value.
759   (unless notmuch-saved-searches
760     (setq notmuch-saved-searches (notmuch-saved-searches)))
761
762   (if no-display
763       (set-buffer "*notmuch-hello*")
764     (switch-to-buffer "*notmuch-hello*"))
765
766   (let ((notmuch-hello-target (if (widget-at)
767                                   (widget-value (widget-at))
768                                 (condition-case nil
769                                     (progn
770                                       (widget-forward 1)
771                                       (widget-value (widget-at)))
772                                   (error nil))))
773         (inhibit-read-only t))
774
775     ;; Delete all editable widget fields.  Editable widget fields are
776     ;; tracked in a buffer local variable `widget-field-list' (and
777     ;; others).  If we do `erase-buffer' without properly deleting the
778     ;; widgets, some widget-related functions are confused later.
779     (mapc 'widget-delete widget-field-list)
780
781     (erase-buffer)
782
783     (unless (eq major-mode 'notmuch-hello-mode)
784       (notmuch-hello-mode))
785
786     (let ((all (overlay-lists)))
787       ;; Delete all the overlays.
788       (mapc 'delete-overlay (car all))
789       (mapc 'delete-overlay (cdr all)))
790
791     (let (final-target-pos)
792       (mapc
793        (lambda (section)
794          (let ((point-before (point))
795                (result (if (functionp section)
796                            (funcall section)
797                          (apply (car section) (cdr section)))))
798            (if (and (not final-target-pos) (integer-or-marker-p result))
799                (setq final-target-pos result))
800            ;; don't insert a newline when the previous section didn't show
801            ;; anything.
802            (unless (eq (point) point-before)
803              (widget-insert "\n"))))
804        notmuch-hello-sections)
805       (widget-setup)
806
807       (when final-target-pos
808         (goto-char final-target-pos)
809         (unless (widget-at)
810           (widget-forward 1)))
811
812       (unless (widget-at)
813         (when notmuch-hello-search-pos
814           (goto-char notmuch-hello-search-pos)))))
815   (run-hooks 'notmuch-hello-refresh-hook)
816   (setq notmuch-hello-first-run nil))
817
818 (defun notmuch-folder ()
819   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
820   (interactive)
821   (notmuch-hello))
822
823 ;;
824
825 (provide 'notmuch-hello)