]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: hello: use batch count
[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-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-longest-label (searches-alist)
290   (or (loop for elem in searches-alist
291             maximize (length (car elem)))
292       0))
293
294 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
295   (let ((len (length list)))
296     (loop for col from 0 to (- ncols 1)
297           collect (let ((offset (+ (* nrows col) row)))
298                     (if (< offset len)
299                         (nth offset list)
300                       ;; Don't forget to insert an empty slot in the
301                       ;; output matrix if there is no corresponding
302                       ;; value in the input matrix.
303                       nil)))))
304
305 (defun notmuch-hello-reflect (list ncols)
306   "Reflect a `ncols' wide matrix represented by `list' along the
307 diagonal."
308   ;; Not very lispy...
309   (let ((nrows (ceiling (length list) ncols)))
310     (loop for row from 0 to (- nrows 1)
311           append (notmuch-hello-reflect-generate-row ncols nrows row list))))
312
313 (defun notmuch-hello-widget-search (widget &rest ignore)
314   (notmuch-search (widget-get widget
315                               :notmuch-search-terms)
316                   notmuch-search-oldest-first
317                   nil nil #'notmuch-hello-search-continuation))
318
319 (defun notmuch-saved-search-count (search)
320   (car (process-lines notmuch-command "count" search)))
321
322 (defun notmuch-hello-tags-per-line (widest)
323   "Determine how many tags to show per line and how wide they
324 should be. Returns a cons cell `(tags-per-line width)'."
325   (let ((tags-per-line
326          (cond
327           ((integerp notmuch-column-control)
328            (max 1
329                 (/ (- (window-width) notmuch-hello-indent)
330                    ;; Count is 9 wide (8 digits plus space), 1 for the space
331                    ;; after the name.
332                    (+ 9 1 (max notmuch-column-control widest)))))
333
334           ((floatp notmuch-column-control)
335            (let* ((available-width (- (window-width) notmuch-hello-indent))
336                   (proposed-width (max (* available-width notmuch-column-control) widest)))
337              (floor available-width proposed-width)))
338
339           (t
340            (max 1
341                 (/ (- (window-width) notmuch-hello-indent)
342                    ;; Count is 9 wide (8 digits plus space), 1 for the space
343                    ;; after the name.
344                    (+ 9 1 widest)))))))
345
346     (cons tags-per-line (/ (max 1
347                                 (- (window-width) notmuch-hello-indent
348                                    ;; Count is 9 wide (8 digits plus
349                                    ;; space), 1 for the space after the
350                                    ;; name.
351                                    (* tags-per-line (+ 9 1))))
352                            tags-per-line))))
353
354 (defun notmuch-hello-filtered-query (query filter)
355   "Constructs a query to search all messages matching QUERY and FILTER.
356
357 If FILTER is a string, it is directly used in the returned query.
358
359 If FILTER is a function, it is called with QUERY as a parameter and
360 the string it returns is used as the query. If nil is returned,
361 the entry is hidden.
362
363 Otherwise, FILTER is ignored.
364 "
365   (cond
366    ((functionp filter) (funcall filter query))
367    ((stringp filter)
368     (concat "(" query ") and (" filter ")"))
369    (t query)))
370
371 (defun notmuch-hello-query-counts (query-alist &rest options)
372   "Compute list of counts of matched messages from QUERY-ALIST.
373
374 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
375 or (NAME QUERY COUNT-QUERY). If the latter form is used,
376 COUNT-QUERY specifies an alternate query to be used to generate
377 the count for the associated query.
378
379 The result is the list of elements of the form (NAME QUERY COUNT).
380
381 The values :show-empty-searches, :filter and :filter-count from
382 options will be handled as specified for
383 `notmuch-hello-insert-searches'."
384   (with-temp-buffer
385     (dolist (elem query-alist nil)
386       (let ((count-query (if (consp (cdr elem))
387                              ;; do we have a different query for the message count?
388                              (third elem)
389                            (cdr elem))))
390         (insert
391          (notmuch-hello-filtered-query count-query
392                                        (or (plist-get options :filter-count)
393                                            (plist-get options :filter)))
394          "\n")))
395
396     (call-process-region (point-min) (point-max) notmuch-command
397                          t t nil "count" "--batch")
398     (goto-char (point-min))
399
400     (notmuch-remove-if-not
401      #'identity
402      (mapcar
403       (lambda (elem)
404         (let ((name (car elem))
405               (search-query (if (consp (cdr elem))
406                                  ;; do we have a different query for the message count?
407                                  (second elem)
408                               (cdr elem)))
409               (message-count (prog1 (read (current-buffer))
410                                 (forward-line 1))))
411           (and (or (plist-get options :show-empty-searches) (> message-count 0))
412                (list name (notmuch-hello-filtered-query
413                            search-query (plist-get options :filter))
414                      message-count))))
415       query-alist))))
416
417 (defun notmuch-hello-insert-buttons (searches)
418   "Insert buttons for SEARCHES.
419
420 SEARCHES must be a list containing lists of the form (NAME QUERY COUNT), where
421 QUERY is the query to start when the button for the corresponding entry is
422 activated. COUNT should be the number of messages matching the query.
423 Such a list can be computed with `notmuch-hello-query-counts'."
424   (let* ((widest (notmuch-hello-longest-label searches))
425          (tags-and-width (notmuch-hello-tags-per-line widest))
426          (tags-per-line (car tags-and-width))
427          (column-width (cdr tags-and-width))
428          (column-indent 0)
429          (count 0)
430          (reordered-list (notmuch-hello-reflect searches tags-per-line))
431          ;; Hack the display of the buttons used.
432          (widget-push-button-prefix "")
433          (widget-push-button-suffix ""))
434     ;; dme: It feels as though there should be a better way to
435     ;; implement this loop than using an incrementing counter.
436     (mapc (lambda (elem)
437             ;; (not elem) indicates an empty slot in the matrix.
438             (when elem
439               (if (> column-indent 0)
440                   (widget-insert (make-string column-indent ? )))
441               (let* ((name (first elem))
442                      (query (second elem))
443                      (msg-count (third elem)))
444                 (widget-insert (format "%8s "
445                                        (notmuch-hello-nice-number msg-count)))
446                 (widget-create 'push-button
447                                :notify #'notmuch-hello-widget-search
448                                :notmuch-search-terms query
449                                name)
450                 (setq column-indent
451                       (1+ (max 0 (- column-width (length name)))))))
452             (setq count (1+ count))
453             (when (eq (% count tags-per-line) 0)
454               (setq column-indent 0)
455               (widget-insert "\n")))
456           reordered-list)
457
458     ;; If the last line was not full (and hence did not include a
459     ;; carriage return), insert one now.
460     (unless (eq (% count tags-per-line) 0)
461       (widget-insert "\n"))))
462
463 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
464
465 (defun notmuch-hello-search-continuation()
466   (notmuch-hello-update t))
467
468 (defun notmuch-hello-update (&optional no-display)
469   "Update the current notmuch view."
470   ;; Lazy - rebuild everything.
471   (interactive)
472   (notmuch-hello no-display))
473
474 (defun notmuch-hello-poll-and-update ()
475   "Invoke `notmuch-poll' to import mail, then refresh the current view."
476   (interactive)
477   (notmuch-poll)
478   (notmuch-hello-update))
479
480
481 (defvar notmuch-hello-mode-map
482   (let ((map (make-sparse-keymap)))
483     (set-keymap-parent map widget-keymap)
484     (define-key map "v" (lambda () "Display the notmuch version" (interactive)
485                           (message "notmuch version %s" (notmuch-version))))
486     (define-key map "?" 'notmuch-help)
487     (define-key map "q" 'notmuch-kill-this-buffer)
488     (define-key map "=" 'notmuch-hello-update)
489     (define-key map "G" 'notmuch-hello-poll-and-update)
490     (define-key map (kbd "<C-tab>") 'widget-backward)
491     (define-key map "m" 'notmuch-mua-new-mail)
492     (define-key map "s" 'notmuch-hello-search)
493     map)
494   "Keymap for \"notmuch hello\" buffers.")
495 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
496
497 (defun notmuch-hello-mode ()
498  "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
499
500 Complete list of currently available key bindings:
501
502 \\{notmuch-hello-mode-map}"
503  (interactive)
504  (kill-all-local-variables)
505  (use-local-map notmuch-hello-mode-map)
506  (setq major-mode 'notmuch-hello-mode
507         mode-name "notmuch-hello")
508  (run-mode-hooks 'notmuch-hello-mode-hook)
509  ;;(setq buffer-read-only t)
510 )
511
512 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
513   "Return an alist from tags to queries to display in the all-tags section."
514   (mapcar (lambda (tag)
515             (cons tag (concat "tag:" (notmuch-escape-boolean-term tag))))
516           (notmuch-remove-if-not
517            (lambda (tag)
518              (not (member tag hide-tags)))
519            (process-lines notmuch-command "search" "--output=tags" "*"))))
520
521 (defun notmuch-hello-insert-header ()
522   "Insert the default notmuch-hello header."
523   (when notmuch-show-logo
524     (let ((image notmuch-hello-logo))
525       ;; The notmuch logo uses transparency. That can display poorly
526       ;; when inserting the image into an emacs buffer (black logo on
527       ;; a black background), so force the background colour of the
528       ;; image. We use a face to represent the colour so that
529       ;; `defface' can be used to declare the different possible
530       ;; colours, which depend on whether the frame has a light or
531       ;; dark background.
532       (setq image (cons 'image
533                         (append (cdr image)
534                                 (list :background (face-background 'notmuch-hello-logo-background)))))
535       (insert-image image))
536     (widget-insert "  "))
537
538   (widget-insert "Welcome to ")
539   ;; Hack the display of the links used.
540   (let ((widget-link-prefix "")
541         (widget-link-suffix ""))
542     (widget-create 'link
543                    :notify (lambda (&rest ignore)
544                              (browse-url notmuch-hello-url))
545                    :help-echo "Visit the notmuch website."
546                    "notmuch")
547     (widget-insert ". ")
548     (widget-insert "You have ")
549     (widget-create 'link
550                    :notify (lambda (&rest ignore)
551                              (notmuch-hello-update))
552                    :help-echo "Refresh"
553                    (notmuch-hello-nice-number
554                     (string-to-number (car (process-lines notmuch-command "count")))))
555     (widget-insert " messages.\n")))
556
557
558 (defun notmuch-hello-insert-saved-searches ()
559   "Insert the saved-searches section."
560   (let ((searches (notmuch-hello-query-counts
561                    (if notmuch-saved-search-sort-function
562                        (funcall notmuch-saved-search-sort-function
563                                 notmuch-saved-searches)
564                      notmuch-saved-searches)
565                    :show-empty-searches notmuch-show-empty-saved-searches)))
566     (when searches
567       (widget-insert "Saved searches: ")
568       (widget-create 'push-button
569                      :notify (lambda (&rest ignore)
570                                (customize-variable 'notmuch-saved-searches))
571                      "edit")
572       (widget-insert "\n\n")
573       (let ((start (point)))
574         (notmuch-hello-insert-buttons searches)
575         (indent-rigidly start (point) notmuch-hello-indent)))))
576
577 (defun notmuch-hello-insert-search ()
578   "Insert a search widget."
579   (widget-insert "Search: ")
580   (widget-create 'editable-field
581                  ;; Leave some space at the start and end of the
582                  ;; search boxes.
583                  :size (max 8 (- (window-width) notmuch-hello-indent
584                                  (length "Search: ")))
585                  :action (lambda (widget &rest ignore)
586                            (notmuch-hello-search (widget-value widget))))
587   ;; Add an invisible dot to make `widget-end-of-line' ignore
588   ;; trailing spaces in the search widget field.  A dot is used
589   ;; instead of a space to make `show-trailing-whitespace'
590   ;; happy, i.e. avoid it marking the whole line as trailing
591   ;; spaces.
592   (widget-insert ".")
593   (put-text-property (1- (point)) (point) 'invisible t)
594   (widget-insert "\n"))
595
596 (defun notmuch-hello-insert-recent-searches ()
597   "Insert recent searches."
598   (when notmuch-search-history
599     (widget-insert "Recent searches: ")
600     (widget-create 'push-button
601                    :notify (lambda (&rest ignore)
602                              (setq notmuch-search-history nil)
603                              (notmuch-hello-update))
604                    "clear")
605     (widget-insert "\n\n")
606     (let ((start (point)))
607       (loop for i from 1 to notmuch-hello-recent-searches-max
608             for search in notmuch-search-history do
609             (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
610               (set widget-symbol
611                    (widget-create 'editable-field
612                                   ;; Don't let the search boxes be
613                                   ;; less than 8 characters wide.
614                                   :size (max 8
615                                              (- (window-width)
616                                                 ;; Leave some space
617                                                 ;; at the start and
618                                                 ;; end of the
619                                                 ;; boxes.
620                                                 (* 2 notmuch-hello-indent)
621                                                 ;; 1 for the space
622                                                 ;; before the
623                                                 ;; `[save]' button. 6
624                                                 ;; for the `[save]'
625                                                 ;; button.
626                                                 1 6))
627                                   :action (lambda (widget &rest ignore)
628                                             (notmuch-hello-search (widget-value widget)))
629                                   search))
630               (widget-insert " ")
631               (widget-create 'push-button
632                              :notify (lambda (widget &rest ignore)
633                                        (notmuch-hello-add-saved-search widget))
634                              :notmuch-saved-search-widget widget-symbol
635                              "save"))
636             (widget-insert "\n"))
637       (indent-rigidly start (point) notmuch-hello-indent))
638     nil))
639
640 (defun notmuch-hello-insert-searches (title query-alist &rest options)
641   "Insert a section with TITLE showing a list of buttons made from QUERY-ALIST.
642
643 QUERY-ALIST must be a list containing elements of the form (NAME . QUERY)
644 or (NAME QUERY COUNT-QUERY). If the latter form is used,
645 COUNT-QUERY specifies an alternate query to be used to generate
646 the count for the associated item.
647
648 Supports the following entries in OPTIONS as a plist:
649 :initially-hidden - if non-nil, section will be hidden on startup
650 :show-empty-searches - show buttons with no matching messages
651 :hide-if-empty - hide if no buttons would be shown
652    (only makes sense without :show-empty-searches)
653 :filter - This can be a function that takes the search query as its argument and
654    returns a filter to be used in conjuction with the query for that search or nil
655    to hide the element. This can also be a string that is used as a combined with
656    each query using \"and\".
657 :filter-count - Separate filter to generate the count displayed each search. Accepts
658    the same values as :filter. If :filter and :filter-count are specified, this
659    will be used instead of :filter, not in conjunction with it."
660   (widget-insert title ": ")
661   (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
662       (add-to-list 'notmuch-hello-hidden-sections title))
663   (let ((is-hidden (member title notmuch-hello-hidden-sections))
664         (start (point)))
665     (if is-hidden
666         (widget-create 'push-button
667                        :notify `(lambda (widget &rest ignore)
668                                   (setq notmuch-hello-hidden-sections
669                                         (delete ,title notmuch-hello-hidden-sections))
670                                   (notmuch-hello-update))
671                        "show")
672       (widget-create 'push-button
673                      :notify `(lambda (widget &rest ignore)
674                                 (add-to-list 'notmuch-hello-hidden-sections
675                                              ,title)
676                                 (notmuch-hello-update))
677                      "hide"))
678     (widget-insert "\n")
679     (when (not is-hidden)
680       (let ((searches (apply 'notmuch-hello-query-counts query-alist options)))
681         (when (or (not (plist-get options :hide-if-empty))
682                   searches)
683           (widget-insert "\n")
684           (notmuch-hello-insert-buttons searches)
685           (indent-rigidly start (point) notmuch-hello-indent))))))
686
687 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
688   "Insert a section displaying all tags with message counts.
689
690 TITLE defaults to \"All tags\".
691 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
692 following:
693
694 :hide-tags - List of tags that should be excluded."
695   (apply 'notmuch-hello-insert-searches
696          (or title "All tags")
697          (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
698          options))
699
700 (defun notmuch-hello-insert-inbox ()
701   "Show an entry for each saved search and inboxed messages for each tag"
702   (notmuch-hello-insert-searches "What's in your inbox"
703                                  (append
704                                   notmuch-saved-searches
705                                   (notmuch-hello-generate-tag-alist))
706                                  :filter "tag:inbox"))
707
708 (defun notmuch-hello-insert-alltags ()
709   "Insert a section displaying all tags and associated message counts"
710   (notmuch-hello-insert-tags-section
711    nil
712    :initially-hidden (not notmuch-show-all-tags-list)
713    :hide-tags notmuch-hello-hide-tags
714    :filter notmuch-hello-tag-list-make-query))
715
716 (defun notmuch-hello-insert-footer ()
717   "Insert the notmuch-hello footer."
718   (let ((start (point)))
719     (widget-insert "Type a search query and hit RET to view matching threads.\n")
720     (when notmuch-search-history
721       (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
722       (widget-insert "Save recent searches with the `save' button.\n"))
723     (when notmuch-saved-searches
724       (widget-insert "Edit saved searches with the `edit' button.\n"))
725     (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
726     (widget-insert "`=' to refresh this screen. `s' to search messages. `q' to quit.\n")
727     (widget-create 'link
728                    :notify (lambda (&rest ignore)
729                              (customize-variable 'notmuch-hello-sections))
730                    :button-prefix "" :button-suffix ""
731                    "Customize")
732     (widget-insert " this page.")
733     (let ((fill-column (- (window-width) notmuch-hello-indent)))
734       (center-region start (point)))))
735
736 ;;;###autoload
737 (defun notmuch-hello (&optional no-display)
738   "Run notmuch and display saved searches, known tags, etc."
739   (interactive)
740
741   (if no-display
742       (set-buffer "*notmuch-hello*")
743     (switch-to-buffer "*notmuch-hello*"))
744
745   (let ((target-line (line-number-at-pos))
746         (target-column (current-column))
747         (inhibit-read-only t))
748
749     ;; Delete all editable widget fields.  Editable widget fields are
750     ;; tracked in a buffer local variable `widget-field-list' (and
751     ;; others).  If we do `erase-buffer' without properly deleting the
752     ;; widgets, some widget-related functions are confused later.
753     (mapc 'widget-delete widget-field-list)
754
755     (erase-buffer)
756
757     (unless (eq major-mode 'notmuch-hello-mode)
758       (notmuch-hello-mode))
759
760     (let ((all (overlay-lists)))
761       ;; Delete all the overlays.
762       (mapc 'delete-overlay (car all))
763       (mapc 'delete-overlay (cdr all)))
764
765     (mapc
766      (lambda (section)
767        (let ((point-before (point)))
768          (if (functionp section)
769              (funcall section)
770            (apply (car section) (cdr section)))
771          ;; don't insert a newline when the previous section didn't
772          ;; show anything.
773          (unless (eq (point) point-before)
774            (widget-insert "\n"))))
775      notmuch-hello-sections)
776     (widget-setup)
777
778     ;; Move point back to where it was before refresh. Use line and
779     ;; column instead of point directly to be insensitive to additions
780     ;; and removals of text within earlier lines.
781     (goto-char (point-min))
782     (forward-line (1- target-line))
783     (move-to-column target-column))
784   (run-hooks 'notmuch-hello-refresh-hook)
785   (setq notmuch-hello-first-run nil))
786
787 (defun notmuch-folder ()
788   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
789   (interactive)
790   (notmuch-hello))
791
792 ;;
793
794 (provide 'notmuch-hello)