]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: hello: make --batch error gracefully
[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" (&optional query 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-custom-section-options
158   '((:filter (string :tag "Filter for each tag"))
159     (:filter-count (string :tag "Different filter to generate message counts"))
160     (:initially-hidden (const :tag "Hide this section on startup" t))
161     (:show-empty-searches (const :tag "Show queries with no matching messages" t))
162     (:hide-if-empty (const :tag "Hide this section if all queries are empty
163 \(and not shown by show-empty-searches)" t)))
164   "Various customization-options for notmuch-hello-tags/query-section.")
165
166 (define-widget 'notmuch-hello-tags-section 'lazy
167   "Customize-type for notmuch-hello tag-list sections."
168   :tag "Customized tag-list section (see docstring for details)"
169   :type
170   `(list :tag ""
171          (const :tag "" notmuch-hello-insert-tags-section)
172          (string :tag "Title for this section")
173          (plist
174           :inline t
175           :options
176           ,(append notmuch-hello-custom-section-options
177                    '((:hide-tags (repeat :tag "Tags that will be hidden"
178                                          string)))))))
179
180 (define-widget 'notmuch-hello-query-section 'lazy
181   "Customize-type for custom saved-search-like sections"
182   :tag "Customized queries section (see docstring for details)"
183   :type
184   `(list :tag ""
185          (const :tag "" notmuch-hello-insert-searches)
186          (string :tag "Title for this section")
187          (repeat :tag "Queries"
188                  (cons (string :tag "Name") (string :tag "Query")))
189          (plist :inline t :options ,notmuch-hello-custom-section-options)))
190
191 (defcustom notmuch-hello-sections
192   (list #'notmuch-hello-insert-header
193         #'notmuch-hello-insert-saved-searches
194         #'notmuch-hello-insert-search
195         #'notmuch-hello-insert-recent-searches
196         #'notmuch-hello-insert-alltags
197         #'notmuch-hello-insert-footer)
198   "Sections for notmuch-hello.
199
200 The list contains functions which are used to construct sections in
201 notmuch-hello buffer.  When notmuch-hello buffer is constructed,
202 these functions are run in the order they appear in this list.  Each
203 function produces a section simply by adding content to the current
204 buffer.  A section should not end with an empty line, because a
205 newline will be inserted after each section by `notmuch-hello'.
206
207 Each function should take no arguments. The return value is
208 ignored.
209
210 For convenience an element can also be a list of the form (FUNC ARG1
211 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
212 list.
213
214 A \"Customized tag-list section\" item in the customize-interface
215 displays a list of all tags, optionally hiding some of them. It
216 is also possible to filter the list of messages matching each tag
217 by an additional filter query. Similarly, the count of messages
218 displayed next to the buttons can be generated by applying a
219 different filter to the tag query. These filters are also
220 supported for \"Customized queries section\" items."
221   :group 'notmuch-hello
222   :type
223   '(repeat
224     (choice (function-item notmuch-hello-insert-header)
225             (function-item notmuch-hello-insert-saved-searches)
226             (function-item notmuch-hello-insert-search)
227             (function-item notmuch-hello-insert-recent-searches)
228             (function-item notmuch-hello-insert-alltags)
229             (function-item notmuch-hello-insert-footer)
230             (function-item notmuch-hello-insert-inbox)
231             notmuch-hello-tags-section
232             notmuch-hello-query-section
233             (function :tag "Custom section"))))
234
235 (defvar notmuch-hello-hidden-sections nil
236   "List of sections titles whose contents are hidden")
237
238 (defvar notmuch-hello-first-run t
239   "True if `notmuch-hello' is run for the first time, set to nil
240 afterwards.")
241
242 (defun notmuch-hello-nice-number (n)
243   (let (result)
244     (while (> n 0)
245       (push (% n 1000) result)
246       (setq n (/ n 1000)))
247     (setq result (or result '(0)))
248     (apply #'concat
249      (number-to-string (car result))
250      (mapcar (lambda (elem)
251               (format "%s%03d" notmuch-hello-thousands-separator elem))
252              (cdr result)))))
253
254 (defun notmuch-hello-trim (search)
255   "Trim whitespace."
256   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
257       (match-string 1 search)
258     search))
259
260 (defun notmuch-hello-search (&optional search)
261   (interactive)
262   (unless (null search)
263     (setq search (notmuch-hello-trim search))
264     (let ((history-delete-duplicates t))
265       (add-to-history 'notmuch-search-history search)))
266   (notmuch-search search notmuch-search-oldest-first nil nil
267                   #'notmuch-hello-search-continuation))
268
269 (defun notmuch-hello-add-saved-search (widget)
270   (interactive)
271   (let ((search (widget-value
272                  (symbol-value
273                   (widget-get widget :notmuch-saved-search-widget))))
274         (name (completing-read "Name for saved search: "
275                                notmuch-saved-searches)))
276     ;; If an existing saved search with this name exists, remove it.
277     (setq notmuch-saved-searches
278           (loop for elem in notmuch-saved-searches
279                 if (not (equal name
280                                (car elem)))
281                 collect elem))
282     ;; Add the new one.
283     (customize-save-variable 'notmuch-saved-searches
284                              (add-to-list 'notmuch-saved-searches
285                                           (cons name search) t))
286     (message "Saved '%s' as '%s'." search name)
287     (notmuch-hello-update)))
288
289 (defun notmuch-hello-delete-search-from-history (widget)
290   (interactive)
291   (let ((search (widget-value
292                  (symbol-value
293                   (widget-get widget :notmuch-saved-search-widget)))))
294     (setq notmuch-search-history (delete search
295                                          notmuch-search-history))
296     (notmuch-hello-update)))
297
298 (defun notmuch-hello-longest-label (searches-alist)
299   (or (loop for elem in searches-alist
300             maximize (length (car elem)))
301       0))
302
303 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
304   (let ((len (length list)))
305     (loop for col from 0 to (- ncols 1)
306           collect (let ((offset (+ (* nrows col) row)))
307                     (if (< offset len)
308                         (nth offset list)
309                       ;; Don't forget to insert an empty slot in the
310                       ;; output matrix if there is no corresponding
311                       ;; value in the input matrix.
312                       nil)))))
313
314 (defun notmuch-hello-reflect (list ncols)
315   "Reflect a `ncols' wide matrix represented by `list' along the
316 diagonal."
317   ;; Not very lispy...
318   (let ((nrows (ceiling (length list) ncols)))
319     (loop for row from 0 to (- nrows 1)
320           append (notmuch-hello-reflect-generate-row ncols nrows row list))))
321
322 (defun notmuch-hello-widget-search (widget &rest ignore)
323   (notmuch-search (widget-get widget
324                               :notmuch-search-terms)
325                   notmuch-search-oldest-first
326                   nil nil #'notmuch-hello-search-continuation))
327
328 (defun notmuch-saved-search-count (search)
329   (car (process-lines notmuch-command "count" search)))
330
331 (defun notmuch-hello-tags-per-line (widest)
332   "Determine how many tags to show per line and how wide they
333 should be. Returns a cons cell `(tags-per-line width)'."
334   (let ((tags-per-line
335          (cond
336           ((integerp notmuch-column-control)
337            (max 1
338                 (/ (- (window-width) notmuch-hello-indent)
339                    ;; Count is 9 wide (8 digits plus space), 1 for the space
340                    ;; after the name.
341                    (+ 9 1 (max notmuch-column-control widest)))))
342
343           ((floatp notmuch-column-control)
344            (let* ((available-width (- (window-width) notmuch-hello-indent))
345                   (proposed-width (max (* available-width notmuch-column-control) widest)))
346              (floor available-width proposed-width)))
347
348           (t
349            (max 1
350                 (/ (- (window-width) notmuch-hello-indent)
351                    ;; Count is 9 wide (8 digits plus space), 1 for the space
352                    ;; after the name.
353                    (+ 9 1 widest)))))))
354
355     (cons tags-per-line (/ (max 1
356                                 (- (window-width) notmuch-hello-indent
357                                    ;; Count is 9 wide (8 digits plus
358                                    ;; space), 1 for the space after the
359                                    ;; name.
360                                    (* tags-per-line (+ 9 1))))
361                            tags-per-line))))
362
363 (defun notmuch-hello-filtered-query (query filter)
364   "Constructs a query to search all messages matching QUERY and FILTER.
365
366 If FILTER is a string, it is directly used in the returned query.
367
368 If FILTER is a function, it is called with QUERY as a parameter and
369 the string it returns is used as the query. If nil is returned,
370 the entry is hidden.
371
372 Otherwise, FILTER is ignored.
373 "
374   (cond
375    ((functionp filter) (funcall filter query))
376    ((stringp filter)
377     (concat "(" query ") and (" filter ")"))
378    (t query)))
379
380 (defun notmuch-hello-query-counts (query-alist &rest options)
381   "Compute list of counts of matched messages from QUERY-ALIST.
382
383 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
384 or (NAME QUERY COUNT-QUERY). If the latter form is used,
385 COUNT-QUERY specifies an alternate query to be used to generate
386 the count for the associated query.
387
388 The result is the list of elements of the form (NAME QUERY COUNT).
389
390 The values :show-empty-searches, :filter and :filter-count from
391 options will be handled as specified for
392 `notmuch-hello-insert-searches'."
393   (with-temp-buffer
394     (dolist (elem query-alist nil)
395       (let ((count-query (if (consp (cdr elem))
396                              ;; do we have a different query for the message count?
397                              (third elem)
398                            (cdr elem))))
399         (insert
400          (notmuch-hello-filtered-query count-query
401                                        (or (plist-get options :filter-count)
402                                            (plist-get options :filter)))
403          "\n")))
404
405     (unless (= (call-process-region (point-min) (point-max) notmuch-command
406                                     t t nil "count" "--batch") 0)
407       (notmuch-logged-error "notmuch count --batch failed"
408                             "Please check that the notmuch CLI is new enough to support `count
409 --batch'. In general we recommend running matching versions of
410 the CLI and emacs interface."))
411
412     (goto-char (point-min))
413
414     (notmuch-remove-if-not
415      #'identity
416      (mapcar
417       (lambda (elem)
418         (let ((name (car elem))
419               (search-query (if (consp (cdr elem))
420                                  ;; do we have a different query for the message count?
421                                  (second elem)
422                               (cdr elem)))
423               (message-count (prog1 (read (current-buffer))
424                                 (forward-line 1))))
425           (and (or (plist-get options :show-empty-searches) (> message-count 0))
426                (list name (notmuch-hello-filtered-query
427                            search-query (plist-get options :filter))
428                      message-count))))
429       query-alist))))
430
431 (defun notmuch-hello-insert-buttons (searches)
432   "Insert buttons for SEARCHES.
433
434 SEARCHES must be a list containing lists of the form (NAME QUERY COUNT), where
435 QUERY is the query to start when the button for the corresponding entry is
436 activated. COUNT should be the number of messages matching the query.
437 Such a list can be computed with `notmuch-hello-query-counts'."
438   (let* ((widest (notmuch-hello-longest-label searches))
439          (tags-and-width (notmuch-hello-tags-per-line widest))
440          (tags-per-line (car tags-and-width))
441          (column-width (cdr tags-and-width))
442          (column-indent 0)
443          (count 0)
444          (reordered-list (notmuch-hello-reflect searches tags-per-line))
445          ;; Hack the display of the buttons used.
446          (widget-push-button-prefix "")
447          (widget-push-button-suffix ""))
448     ;; dme: It feels as though there should be a better way to
449     ;; implement this loop than using an incrementing counter.
450     (mapc (lambda (elem)
451             ;; (not elem) indicates an empty slot in the matrix.
452             (when elem
453               (if (> column-indent 0)
454                   (widget-insert (make-string column-indent ? )))
455               (let* ((name (first elem))
456                      (query (second elem))
457                      (msg-count (third elem)))
458                 (widget-insert (format "%8s "
459                                        (notmuch-hello-nice-number msg-count)))
460                 (widget-create 'push-button
461                                :notify #'notmuch-hello-widget-search
462                                :notmuch-search-terms query
463                                name)
464                 (setq column-indent
465                       (1+ (max 0 (- column-width (length name)))))))
466             (setq count (1+ count))
467             (when (eq (% count tags-per-line) 0)
468               (setq column-indent 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
477 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
478
479 (defun notmuch-hello-search-continuation()
480   (notmuch-hello-update t))
481
482 (defun notmuch-hello-update (&optional no-display)
483   "Update the current notmuch view."
484   ;; Lazy - rebuild everything.
485   (interactive)
486   (notmuch-hello no-display))
487
488 (defun notmuch-hello-poll-and-update ()
489   "Invoke `notmuch-poll' to import mail, then refresh the current view."
490   (interactive)
491   (notmuch-poll)
492   (notmuch-hello-update))
493
494
495 (defvar notmuch-hello-mode-map
496   (let ((map (make-sparse-keymap)))
497     (set-keymap-parent map widget-keymap)
498     (define-key map "v" (lambda () "Display the notmuch version" (interactive)
499                           (message "notmuch version %s" (notmuch-version))))
500     (define-key map "?" 'notmuch-help)
501     (define-key map "q" 'notmuch-kill-this-buffer)
502     (define-key map "=" 'notmuch-hello-update)
503     (define-key map "G" 'notmuch-hello-poll-and-update)
504     (define-key map (kbd "<C-tab>") 'widget-backward)
505     (define-key map "m" 'notmuch-mua-new-mail)
506     (define-key map "s" 'notmuch-hello-search)
507     map)
508   "Keymap for \"notmuch hello\" buffers.")
509 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
510
511 (defun notmuch-hello-mode ()
512  "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
513
514 Complete list of currently available key bindings:
515
516 \\{notmuch-hello-mode-map}"
517  (interactive)
518  (kill-all-local-variables)
519  (use-local-map notmuch-hello-mode-map)
520  (setq major-mode 'notmuch-hello-mode
521         mode-name "notmuch-hello")
522  (run-mode-hooks 'notmuch-hello-mode-hook)
523  ;;(setq buffer-read-only t)
524 )
525
526 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
527   "Return an alist from tags to queries to display in the all-tags section."
528   (mapcar (lambda (tag)
529             (cons tag (concat "tag:" (notmuch-escape-boolean-term tag))))
530           (notmuch-remove-if-not
531            (lambda (tag)
532              (not (member tag hide-tags)))
533            (process-lines notmuch-command "search" "--output=tags" "*"))))
534
535 (defun notmuch-hello-insert-header ()
536   "Insert the default notmuch-hello header."
537   (when notmuch-show-logo
538     (let ((image notmuch-hello-logo))
539       ;; The notmuch logo uses transparency. That can display poorly
540       ;; when inserting the image into an emacs buffer (black logo on
541       ;; a black background), so force the background colour of the
542       ;; image. We use a face to represent the colour so that
543       ;; `defface' can be used to declare the different possible
544       ;; colours, which depend on whether the frame has a light or
545       ;; dark background.
546       (setq image (cons 'image
547                         (append (cdr image)
548                                 (list :background (face-background 'notmuch-hello-logo-background)))))
549       (insert-image image))
550     (widget-insert "  "))
551
552   (widget-insert "Welcome to ")
553   ;; Hack the display of the links used.
554   (let ((widget-link-prefix "")
555         (widget-link-suffix ""))
556     (widget-create 'link
557                    :notify (lambda (&rest ignore)
558                              (browse-url notmuch-hello-url))
559                    :help-echo "Visit the notmuch website."
560                    "notmuch")
561     (widget-insert ". ")
562     (widget-insert "You have ")
563     (widget-create 'link
564                    :notify (lambda (&rest ignore)
565                              (notmuch-hello-update))
566                    :help-echo "Refresh"
567                    (notmuch-hello-nice-number
568                     (string-to-number (car (process-lines notmuch-command "count")))))
569     (widget-insert " messages.\n")))
570
571
572 (defun notmuch-hello-insert-saved-searches ()
573   "Insert the saved-searches section."
574   (let ((searches (notmuch-hello-query-counts
575                    (if notmuch-saved-search-sort-function
576                        (funcall notmuch-saved-search-sort-function
577                                 notmuch-saved-searches)
578                      notmuch-saved-searches)
579                    :show-empty-searches notmuch-show-empty-saved-searches)))
580     (when searches
581       (widget-insert "Saved searches: ")
582       (widget-create 'push-button
583                      :notify (lambda (&rest ignore)
584                                (customize-variable 'notmuch-saved-searches))
585                      "edit")
586       (widget-insert "\n\n")
587       (let ((start (point)))
588         (notmuch-hello-insert-buttons searches)
589         (indent-rigidly start (point) notmuch-hello-indent)))))
590
591 (defun notmuch-hello-insert-search ()
592   "Insert a search widget."
593   (widget-insert "Search: ")
594   (widget-create 'editable-field
595                  ;; Leave some space at the start and end of the
596                  ;; search boxes.
597                  :size (max 8 (- (window-width) notmuch-hello-indent
598                                  (length "Search: ")))
599                  :action (lambda (widget &rest ignore)
600                            (notmuch-hello-search (widget-value widget))))
601   ;; Add an invisible dot to make `widget-end-of-line' ignore
602   ;; trailing spaces in the search widget field.  A dot is used
603   ;; instead of a space to make `show-trailing-whitespace'
604   ;; happy, i.e. avoid it marking the whole line as trailing
605   ;; spaces.
606   (widget-insert ".")
607   (put-text-property (1- (point)) (point) 'invisible t)
608   (widget-insert "\n"))
609
610 (defun notmuch-hello-insert-recent-searches ()
611   "Insert recent searches."
612   (when notmuch-search-history
613     (widget-insert "Recent searches: ")
614     (widget-create 'push-button
615                    :notify (lambda (&rest ignore)
616                              (when (y-or-n-p "Are you sure you want to clear the searches? ")
617                                (setq notmuch-search-history nil)
618                                (notmuch-hello-update)))
619                    "clear")
620     (widget-insert "\n\n")
621     (let ((start (point)))
622       (loop for i from 1 to notmuch-hello-recent-searches-max
623             for search in notmuch-search-history do
624             (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
625               (set widget-symbol
626                    (widget-create 'editable-field
627                                   ;; Don't let the search boxes be
628                                   ;; less than 8 characters wide.
629                                   :size (max 8
630                                              (- (window-width)
631                                                 ;; Leave some space
632                                                 ;; at the start and
633                                                 ;; end of the
634                                                 ;; boxes.
635                                                 (* 2 notmuch-hello-indent)
636                                                 ;; 1 for the space
637                                                 ;; before the
638                                                 ;; `[save]' button. 6
639                                                 ;; for the `[save]'
640                                                 ;; button.
641                                                 1 6
642                                                 ;; 1 for the space
643                                                 ;; before the `[del]'
644                                                 ;; button. 5 for the
645                                                 ;; `[del]' button.
646                                                 1 5))
647                                   :action (lambda (widget &rest ignore)
648                                             (notmuch-hello-search (widget-value widget)))
649                                   search))
650               (widget-insert " ")
651               (widget-create 'push-button
652                              :notify (lambda (widget &rest ignore)
653                                        (notmuch-hello-add-saved-search widget))
654                              :notmuch-saved-search-widget widget-symbol
655                              "save")
656               (widget-insert " ")
657               (widget-create 'push-button
658                              :notify (lambda (widget &rest ignore)
659                                        (when (y-or-n-p "Are you sure you want to delete this search? ")
660                                          (notmuch-hello-delete-search-from-history widget)))
661                              :notmuch-saved-search-widget widget-symbol
662                              "del"))
663             (widget-insert "\n"))
664       (indent-rigidly start (point) notmuch-hello-indent))
665     nil))
666
667 (defun notmuch-hello-insert-searches (title query-alist &rest options)
668   "Insert a section with TITLE showing a list of buttons made from QUERY-ALIST.
669
670 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
671 or (NAME QUERY COUNT-QUERY). If the latter form is used,
672 COUNT-QUERY specifies an alternate query to be used to generate
673 the count for the associated item.
674
675 Supports the following entries in OPTIONS as a plist:
676 :initially-hidden - if non-nil, section will be hidden on startup
677 :show-empty-searches - show buttons with no matching messages
678 :hide-if-empty - hide if no buttons would be shown
679    (only makes sense without :show-empty-searches)
680 :filter - This can be a function that takes the search query as its argument and
681    returns a filter to be used in conjuction with the query for that search or nil
682    to hide the element. This can also be a string that is used as a combined with
683    each query using \"and\".
684 :filter-count - Separate filter to generate the count displayed each search. Accepts
685    the same values as :filter. If :filter and :filter-count are specified, this
686    will be used instead of :filter, not in conjunction with it."
687   (widget-insert title ": ")
688   (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
689       (add-to-list 'notmuch-hello-hidden-sections title))
690   (let ((is-hidden (member title notmuch-hello-hidden-sections))
691         (start (point)))
692     (if is-hidden
693         (widget-create 'push-button
694                        :notify `(lambda (widget &rest ignore)
695                                   (setq notmuch-hello-hidden-sections
696                                         (delete ,title notmuch-hello-hidden-sections))
697                                   (notmuch-hello-update))
698                        "show")
699       (widget-create 'push-button
700                      :notify `(lambda (widget &rest ignore)
701                                 (add-to-list 'notmuch-hello-hidden-sections
702                                              ,title)
703                                 (notmuch-hello-update))
704                      "hide"))
705     (widget-insert "\n")
706     (when (not is-hidden)
707       (let ((searches (apply 'notmuch-hello-query-counts query-alist options)))
708         (when (or (not (plist-get options :hide-if-empty))
709                   searches)
710           (widget-insert "\n")
711           (notmuch-hello-insert-buttons searches)
712           (indent-rigidly start (point) notmuch-hello-indent))))))
713
714 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
715   "Insert a section displaying all tags with message counts.
716
717 TITLE defaults to \"All tags\".
718 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
719 following:
720
721 :hide-tags - List of tags that should be excluded."
722   (apply 'notmuch-hello-insert-searches
723          (or title "All tags")
724          (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
725          options))
726
727 (defun notmuch-hello-insert-inbox ()
728   "Show an entry for each saved search and inboxed messages for each tag"
729   (notmuch-hello-insert-searches "What's in your inbox"
730                                  (append
731                                   notmuch-saved-searches
732                                   (notmuch-hello-generate-tag-alist))
733                                  :filter "tag:inbox"))
734
735 (defun notmuch-hello-insert-alltags ()
736   "Insert a section displaying all tags and associated message counts"
737   (notmuch-hello-insert-tags-section
738    nil
739    :initially-hidden (not notmuch-show-all-tags-list)
740    :hide-tags notmuch-hello-hide-tags
741    :filter notmuch-hello-tag-list-make-query))
742
743 (defun notmuch-hello-insert-footer ()
744   "Insert the notmuch-hello footer."
745   (let ((start (point)))
746     (widget-insert "Type a search query and hit RET to view matching threads.\n")
747     (when notmuch-search-history
748       (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
749       (widget-insert "Save recent searches with the `save' button.\n"))
750     (when notmuch-saved-searches
751       (widget-insert "Edit saved searches with the `edit' button.\n"))
752     (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
753     (widget-insert "`=' to refresh this screen. `s' to search messages. `q' to quit.\n")
754     (widget-create 'link
755                    :notify (lambda (&rest ignore)
756                              (customize-variable 'notmuch-hello-sections))
757                    :button-prefix "" :button-suffix ""
758                    "Customize")
759     (widget-insert " this page.")
760     (let ((fill-column (- (window-width) notmuch-hello-indent)))
761       (center-region start (point)))))
762
763 ;;;###autoload
764 (defun notmuch-hello (&optional no-display)
765   "Run notmuch and display saved searches, known tags, etc."
766   (interactive)
767
768   (if no-display
769       (set-buffer "*notmuch-hello*")
770     (switch-to-buffer "*notmuch-hello*"))
771
772   (let ((target-line (line-number-at-pos))
773         (target-column (current-column))
774         (inhibit-read-only t))
775
776     ;; Delete all editable widget fields.  Editable widget fields are
777     ;; tracked in a buffer local variable `widget-field-list' (and
778     ;; others).  If we do `erase-buffer' without properly deleting the
779     ;; widgets, some widget-related functions are confused later.
780     (mapc 'widget-delete widget-field-list)
781
782     (erase-buffer)
783
784     (unless (eq major-mode 'notmuch-hello-mode)
785       (notmuch-hello-mode))
786
787     (let ((all (overlay-lists)))
788       ;; Delete all the overlays.
789       (mapc 'delete-overlay (car all))
790       (mapc 'delete-overlay (cdr all)))
791
792     (mapc
793      (lambda (section)
794        (let ((point-before (point)))
795          (if (functionp section)
796              (funcall section)
797            (apply (car section) (cdr section)))
798          ;; don't insert a newline when the previous section didn't
799          ;; show anything.
800          (unless (eq (point) point-before)
801            (widget-insert "\n"))))
802      notmuch-hello-sections)
803     (widget-setup)
804
805     ;; Move point back to where it was before refresh. Use line and
806     ;; column instead of point directly to be insensitive to additions
807     ;; and removals of text within earlier lines.
808     (goto-char (point-min))
809     (forward-line (1- target-line))
810     (move-to-column target-column))
811   (run-hooks 'notmuch-hello-refresh-hook)
812   (setq notmuch-hello-first-run nil))
813
814 (defun notmuch-folder ()
815   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
816   (interactive)
817   (notmuch-hello))
818
819 ;;
820
821 (provide 'notmuch-hello)