]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
Merge tag '0.12'
[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 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          (widest (cdr tags-and-width))
433          (count 0)
434          (reordered-list (notmuch-hello-reflect searches tags-per-line))
435          ;; Hack the display of the buttons used.
436          (widget-push-button-prefix "")
437          (widget-push-button-suffix "")
438          (found-target-pos nil))
439     ;; dme: It feels as though there should be a better way to
440     ;; implement this loop than using an incrementing counter.
441     (mapc (lambda (elem)
442             ;; (not elem) indicates an empty slot in the matrix.
443             (when elem
444               (let* ((name (first elem))
445                      (query (second elem))
446                      (msg-count (third elem))
447                      (formatted-name (format "%s " name)))
448                 (widget-insert (format "%8s "
449                                        (notmuch-hello-nice-number msg-count)))
450                 (if (string= formatted-name notmuch-hello-target)
451                     (setq found-target-pos (point-marker)))
452                 (widget-create 'push-button
453                                :notify #'notmuch-hello-widget-search
454                                :notmuch-search-terms query
455                                formatted-name)
456                 (unless (eq (% count tags-per-line) (1- tags-per-line))
457                   ;; If this is not the last tag on the line, insert
458                   ;; enough space to consume the rest of the column.
459                   ;; Because the button for the name is `(1+ (length
460                   ;; name))' long (due to the trailing space) we can
461                   ;; just insert `(- widest (length name))' spaces - the
462                   ;; column separator is included in the button if
463                   ;; `(equal widest (length name)'.
464                   (widget-insert (make-string (max 1
465                                                    (- widest (length name)))
466                                               ? )))))
467             (setq count (1+ count))
468             (if (eq (% count tags-per-line) 0)
469                 (widget-insert "\n")))
470           reordered-list)
471
472     ;; If the last line was not full (and hence did not include a
473     ;; carriage return), insert one now.
474     (unless (eq (% count tags-per-line) 0)
475       (widget-insert "\n"))
476     found-target-pos))
477
478 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
479
480 (defun notmuch-hello-search-continuation()
481   (notmuch-hello-update t))
482
483 (defun notmuch-hello-update (&optional no-display)
484   "Update the current notmuch view."
485   ;; Lazy - rebuild everything.
486   (interactive)
487   (notmuch-hello no-display))
488
489 (defun notmuch-hello-poll-and-update ()
490   "Invoke `notmuch-poll' to import mail, then refresh the current view."
491   (interactive)
492   (notmuch-poll)
493   (notmuch-hello-update))
494
495
496 (defvar notmuch-hello-mode-map
497   (let ((map (make-sparse-keymap)))
498     (set-keymap-parent map widget-keymap)
499     (define-key map "v" (lambda () "Display the notmuch version" (interactive)
500                           (message "notmuch version %s" (notmuch-version))))
501     (define-key map "?" 'notmuch-help)
502     (define-key map "q" 'notmuch-kill-this-buffer)
503     (define-key map "=" 'notmuch-hello-update)
504     (define-key map "G" 'notmuch-hello-poll-and-update)
505     (define-key map (kbd "<C-tab>") 'widget-backward)
506     (define-key map "m" 'notmuch-mua-new-mail)
507     (define-key map "s" 'notmuch-hello-search)
508     map)
509   "Keymap for \"notmuch hello\" buffers.")
510 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
511
512 (defun notmuch-hello-mode ()
513  "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
514
515 Complete list of currently available key bindings:
516
517 \\{notmuch-hello-mode-map}"
518  (interactive)
519  (kill-all-local-variables)
520  (use-local-map notmuch-hello-mode-map)
521  (setq major-mode 'notmuch-hello-mode
522         mode-name "notmuch-hello")
523  (run-mode-hooks 'notmuch-hello-mode-hook)
524  ;;(setq buffer-read-only t)
525 )
526
527 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
528   "Return an alist from tags to queries to display in the all-tags section."
529   (mapcar (lambda (tag)
530             (cons tag (format "tag:%s" tag)))
531           (notmuch-remove-if-not
532            (lambda (tag)
533              (not (member tag hide-tags)))
534            (process-lines notmuch-command "search-tags"))))
535
536 (defun notmuch-hello-insert-header ()
537   "Insert the default notmuch-hello header."
538   (when notmuch-show-logo
539     (let ((image notmuch-hello-logo))
540       ;; The notmuch logo uses transparency. That can display poorly
541       ;; when inserting the image into an emacs buffer (black logo on
542       ;; a black background), so force the background colour of the
543       ;; image. We use a face to represent the colour so that
544       ;; `defface' can be used to declare the different possible
545       ;; colours, which depend on whether the frame has a light or
546       ;; dark background.
547       (setq image (cons 'image
548                         (append (cdr image)
549                                 (list :background (face-background 'notmuch-hello-logo-background)))))
550       (insert-image image))
551     (widget-insert "  "))
552
553   (widget-insert "Welcome to ")
554   ;; Hack the display of the links used.
555   (let ((widget-link-prefix "")
556         (widget-link-suffix ""))
557     (widget-create 'link
558                    :notify (lambda (&rest ignore)
559                              (browse-url notmuch-hello-url))
560                    :help-echo "Visit the notmuch website."
561                    "notmuch")
562     (widget-insert ". ")
563     (widget-insert "You have ")
564     (widget-create 'link
565                    :notify (lambda (&rest ignore)
566                              (notmuch-hello-update))
567                    :help-echo "Refresh"
568                    (notmuch-hello-nice-number
569                     (string-to-number (car (process-lines notmuch-command "count")))))
570     (widget-insert " messages.\n")))
571
572
573 (defun notmuch-hello-insert-saved-searches ()
574   "Insert the saved-searches section."
575   (let ((searches (notmuch-hello-query-counts
576                    (if notmuch-saved-search-sort-function
577                        (funcall notmuch-saved-search-sort-function
578                                 notmuch-saved-searches)
579                      notmuch-saved-searches)
580                    :show-empty-searches notmuch-show-empty-saved-searches))
581         found-target-pos)
582     (when searches
583       (widget-insert "Saved searches: ")
584       (widget-create 'push-button
585                      :notify (lambda (&rest ignore)
586                                (customize-variable 'notmuch-saved-searches))
587                      "edit")
588       (widget-insert "\n\n")
589       (let ((start (point)))
590         (setq found-target-pos
591               (notmuch-hello-insert-buttons searches))
592         (indent-rigidly start (point) notmuch-hello-indent)
593         found-target-pos))))
594
595 (defun notmuch-hello-insert-search ()
596   "Insert a search widget."
597   (widget-insert "Search: ")
598   (setq notmuch-hello-search-pos (point-marker))
599   (widget-create 'editable-field
600                  ;; Leave some space at the start and end of the
601                  ;; search boxes.
602                  :size (max 8 (- (window-width) notmuch-hello-indent
603                                  (length "Search: ")))
604                  :action (lambda (widget &rest ignore)
605                            (notmuch-hello-search (widget-value widget))))
606   ;; Add an invisible dot to make `widget-end-of-line' ignore
607   ;; trailing spaces in the search widget field.  A dot is used
608   ;; instead of a space to make `show-trailing-whitespace'
609   ;; happy, i.e. avoid it marking the whole line as trailing
610   ;; spaces.
611   (widget-insert ".")
612   (put-text-property (1- (point)) (point) 'invisible t)
613   (widget-insert "\n"))
614
615 (defun notmuch-hello-insert-recent-searches ()
616   "Insert recent searches."
617   (when notmuch-search-history
618     (widget-insert "Recent searches: ")
619     (widget-create 'push-button
620                    :notify (lambda (&rest ignore)
621                              (setq notmuch-search-history nil)
622                              (notmuch-hello-update))
623                    "clear")
624     (widget-insert "\n\n")
625     (let ((start (point)))
626       (loop for i from 1 to notmuch-hello-recent-searches-max
627             for search in notmuch-search-history do
628             (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
629               (set widget-symbol
630                    (widget-create 'editable-field
631                                   ;; Don't let the search boxes be
632                                   ;; less than 8 characters wide.
633                                   :size (max 8
634                                              (- (window-width)
635                                                 ;; Leave some space
636                                                 ;; at the start and
637                                                 ;; end of the
638                                                 ;; boxes.
639                                                 (* 2 notmuch-hello-indent)
640                                                 ;; 1 for the space
641                                                 ;; before the
642                                                 ;; `[save]' button. 6
643                                                 ;; for the `[save]'
644                                                 ;; button.
645                                                 1 6))
646                                   :action (lambda (widget &rest ignore)
647                                             (notmuch-hello-search (widget-value widget)))
648                                   search))
649               (widget-insert " ")
650               (widget-create 'push-button
651                              :notify (lambda (widget &rest ignore)
652                                        (notmuch-hello-add-saved-search widget))
653                              :notmuch-saved-search-widget widget-symbol
654                              "save"))
655             (widget-insert "\n"))
656       (indent-rigidly start (point) notmuch-hello-indent))
657     nil))
658
659 (defun notmuch-hello-insert-searches (title query-alist &rest options)
660   "Insert a section with TITLE showing a list of buttons made from QUERY-ALIST.
661
662 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
663 or (NAME QUERY COUNT-QUERY). If the latter form is used,
664 COUNT-QUERY specifies an alternate query to be used to generate
665 the count for the associated item.
666
667 Supports the following entries in OPTIONS as a plist:
668 :initially-hidden - if non-nil, section will be hidden on startup
669 :show-empty-searches - show buttons with no matching messages
670 :hide-if-empty - hide if no buttons would be shown
671    (only makes sense without :show-empty-searches)
672 :filter - This can be a function that takes the search query as its argument and
673    returns a filter to be used in conjuction with the query for that search or nil
674    to hide the element. This can also be a string that is used as a combined with
675    each query using \"and\".
676 :filter-count - Separate filter to generate the count displayed each search. Accepts
677    the same values as :filter. If :filter and :filter-count are specified, this
678    will be used instead of :filter, not in conjunction with it."
679   (widget-insert title ": ")
680   (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
681       (add-to-list 'notmuch-hello-hidden-sections title))
682   (let ((is-hidden (member title notmuch-hello-hidden-sections))
683         (start (point)))
684     (if is-hidden
685         (widget-create 'push-button
686                        :notify `(lambda (widget &rest ignore)
687                                   (setq notmuch-hello-hidden-sections
688                                         (delete ,title notmuch-hello-hidden-sections))
689                                   (notmuch-hello-update))
690                        "show")
691       (widget-create 'push-button
692                      :notify `(lambda (widget &rest ignore)
693                                 (add-to-list 'notmuch-hello-hidden-sections
694                                              ,title)
695                                 (notmuch-hello-update))
696                      "hide"))
697     (widget-insert "\n")
698     (let (target-pos)
699       (when (not is-hidden)
700         (let ((searches (apply 'notmuch-hello-query-counts query-alist options)))
701           (when (or (not (plist-get options :hide-if-empty))
702                     searches)
703             (widget-insert "\n")
704             (setq target-pos
705                   (notmuch-hello-insert-buttons searches))
706             (indent-rigidly start (point) notmuch-hello-indent))))
707       target-pos)))
708
709 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
710   "Insert a section displaying all tags with message counts.
711
712 TITLE defaults to \"All tags\".
713 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
714 following:
715
716 :hide-tags - List of tags that should be excluded."
717   (apply 'notmuch-hello-insert-searches
718          (or title "All tags")
719          (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
720          options))
721
722 (defun notmuch-hello-insert-inbox ()
723   "Show an entry for each saved search and inboxed messages for each tag"
724   (notmuch-hello-insert-searches "What's in your inbox"
725                                  (append
726                                   (notmuch-saved-searches)
727                                   (notmuch-hello-generate-tag-alist))
728                                  :filter "tag:inbox"))
729
730 (defun notmuch-hello-insert-alltags ()
731   "Insert a section displaying all tags and associated message counts"
732   (notmuch-hello-insert-tags-section
733    nil
734    :initially-hidden (not notmuch-show-all-tags-list)
735    :hide-tags notmuch-hello-hide-tags
736    :filter notmuch-hello-tag-list-make-query))
737
738 (defun notmuch-hello-insert-footer ()
739   "Insert the notmuch-hello footer."
740   (let ((start (point)))
741     (widget-insert "Type a search query and hit RET to view matching threads.\n")
742     (when notmuch-search-history
743       (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
744       (widget-insert "Save recent searches with the `save' button.\n"))
745     (when notmuch-saved-searches
746       (widget-insert "Edit saved searches with the `edit' button.\n"))
747     (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
748     (widget-insert "`=' to refresh this screen. `s' to search messages. `q' to quit.\n")
749     (widget-create 'link
750                    :notify (lambda (&rest ignore)
751                              (customize-variable 'notmuch-hello-sections))
752                    :button-prefix "" :button-suffix ""
753                    "Customize")
754     (widget-insert " this page.")
755     (let ((fill-column (- (window-width) notmuch-hello-indent)))
756       (center-region start (point)))))
757
758 ;;;###autoload
759 (defun notmuch-hello (&optional no-display)
760   "Run notmuch and display saved searches, known tags, etc."
761   (interactive)
762
763   ;; Jump through a hoop to get this value from the deprecated variable
764   ;; name (`notmuch-folders') or from the default value.
765   (unless notmuch-saved-searches
766     (setq notmuch-saved-searches (notmuch-saved-searches)))
767
768   (if no-display
769       (set-buffer "*notmuch-hello*")
770     (switch-to-buffer "*notmuch-hello*"))
771
772   (let ((notmuch-hello-target (if (widget-at)
773                                   (widget-value (widget-at))
774                                 (condition-case nil
775                                     (progn
776                                       (widget-forward 1)
777                                       (widget-value (widget-at)))
778                                   (error nil))))
779         (inhibit-read-only t))
780
781     ;; Delete all editable widget fields.  Editable widget fields are
782     ;; tracked in a buffer local variable `widget-field-list' (and
783     ;; others).  If we do `erase-buffer' without properly deleting the
784     ;; widgets, some widget-related functions are confused later.
785     (mapc 'widget-delete widget-field-list)
786
787     (erase-buffer)
788
789     (unless (eq major-mode 'notmuch-hello-mode)
790       (notmuch-hello-mode))
791
792     (let ((all (overlay-lists)))
793       ;; Delete all the overlays.
794       (mapc 'delete-overlay (car all))
795       (mapc 'delete-overlay (cdr all)))
796
797     (let (final-target-pos)
798       (mapc
799        (lambda (section)
800          (let ((point-before (point))
801                (result (if (functionp section)
802                            (funcall section)
803                          (apply (car section) (cdr section)))))
804            (if (and (not final-target-pos) (integer-or-marker-p result))
805                (setq final-target-pos result))
806            ;; don't insert a newline when the previous section didn't show
807            ;; anything.
808            (unless (eq (point) point-before)
809              (widget-insert "\n"))))
810        notmuch-hello-sections)
811       (widget-setup)
812
813       (when final-target-pos
814         (goto-char final-target-pos)
815         (unless (widget-at)
816           (widget-forward 1)))
817
818       (unless (widget-at)
819         (when notmuch-hello-search-pos
820           (goto-char notmuch-hello-search-pos)))))
821   (run-hooks 'notmuch-hello-refresh-hook)
822   (setq notmuch-hello-first-run nil))
823
824 (defun notmuch-folder ()
825   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
826   (interactive)
827   (notmuch-hello))
828
829 ;;
830
831 (provide 'notmuch-hello)