1 ;; notmuch-hello.el --- welcome to notmuch, a frontend
3 ;; Copyright © David Edmondson
5 ;; This file is part of Notmuch.
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.
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.
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/>.
20 ;; Authors: David Edmondson <dme@dme.org>
22 (eval-when-compile (require 'cl))
24 (require 'wid-edit) ; For `widget-forward'.
26 (require 'notmuch-lib)
27 (require 'notmuch-mua)
29 (declare-function notmuch-search "notmuch" (&optional query oldest-first target-thread target-line continuation))
30 (declare-function notmuch-poll "notmuch" ())
31 (declare-function notmuch-tree "notmuch-tree"
32 (&optional query query-context target buffer-name open-target))
34 (defun notmuch-saved-search-get (saved-search field)
35 "Get FIELD from SAVED-SEARCH.
37 If SAVED-SEARCH is a plist, this is just `plist-get', but for
38 backwards compatibility, this also deals with the two other
39 possible formats for SAVED-SEARCH: cons cells (NAME . QUERY) and
40 lists (NAME QUERY COUNT-QUERY)."
42 ((keywordp (car saved-search))
43 (plist-get saved-search field))
44 ;; It is not a plist so it is an old-style entry.
45 ((consp (cdr saved-search)) ;; It is a list (NAME QUERY COUNT-QUERY)
47 (:name (first saved-search))
48 (:query (second saved-search))
49 (:count-query (third saved-search))
51 (t ;; It is a cons-cell (NAME . QUERY)
53 (:name (car saved-search))
54 (:query (cdr saved-search))
57 (defun notmuch-hello-saved-search-to-plist (saved-search)
58 "Return a copy of SAVED-SEARCH in plist form.
60 If saved search is a plist then just return a copy. In other
61 cases, for backwards compatibility, convert to plist form and
63 (if (keywordp (car saved-search))
64 (copy-seq saved-search)
65 (let ((fields (list :name :query :count-query))
67 (dolist (field fields plist-search)
68 (let ((string (notmuch-saved-search-get saved-search field)))
70 (setq plist-search (append plist-search (list field string)))))))))
72 (defun notmuch-hello--saved-searches-to-plist (symbol)
73 "Extract a saved-search variable into plist form.
75 The new style saved search is just a plist, but for backwards
76 compatibility we use this function to extract old style saved
77 searches so they still work in customize."
78 (let ((saved-searches (default-value symbol)))
79 (mapcar #'notmuch-hello-saved-search-to-plist saved-searches)))
81 (define-widget 'notmuch-saved-search-plist 'list
82 "A single saved search property list."
84 :args '((list :inline t
86 (group :format "%v" :inline t (const :format " Name: " :name) (string :format "%v"))
87 (group :format "%v" :inline t (const :format " Query: " :query) (string :format "%v")))
90 (group :format "%v" :inline t (const :format "Shortcut key: " :key) (key-sequence :format "%v"))
91 (group :format "%v" :inline t (const :format "Count-Query: " :count-query) (string :format "%v"))
92 (group :format "%v" :inline t (const :format "" :sort-order)
93 (choice :tag " Sort Order"
94 (const :tag "Default" nil)
95 (const :tag "Oldest-first" oldest-first)
96 (const :tag "Newest-first" newest-first)))
97 (group :format "%v" :inline t (const :format "" :search-type)
98 (choice :tag " Search Type"
99 (const :tag "Search mode" nil)
100 (const :tag "Tree mode" tree))))))
102 (defcustom notmuch-saved-searches
103 `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
104 (:name "unread" :query "tag:unread" :key ,(kbd "u"))
105 (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
106 (:name "sent" :query "tag:sent" :key ,(kbd "t"))
107 (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
108 (:name "all mail" :query "*" :key ,(kbd "a")))
109 "A list of saved searches to display.
111 The saved search can be given in 3 forms. The preferred way is as
112 a plist. Supported properties are
114 :name Name of the search (required).
115 :query Search to run (required).
116 :key Optional shortcut key for `notmuch-jump-search'.
117 :count-query Optional extra query to generate the count
118 shown. If not present then the :query property
120 :sort-order Specify the sort order to be used for the search.
121 Possible values are 'oldest-first 'newest-first or
122 nil. Nil means use the default sort order.
123 :search-type Specify whether to run the search in search-mode
124 or tree mode. Set to 'tree to specify tree
125 mode, set to nil (or anything except tree) to
128 Other accepted forms are a cons cell of the form (NAME . QUERY)
129 or a list of the form (NAME QUERY COUNT-QUERY)."
130 ;; The saved-search format is also used by the all-tags notmuch-hello
131 ;; section. This section generates its own saved-search list in one of
132 ;; the latter two forms.
134 :get 'notmuch-hello--saved-searches-to-plist
135 :type '(repeat notmuch-saved-search-plist)
136 :tag "List of Saved Searches"
137 :group 'notmuch-hello)
139 (defcustom notmuch-hello-recent-searches-max 10
140 "The number of recent searches to display."
142 :group 'notmuch-hello)
144 (defcustom notmuch-show-empty-saved-searches nil
145 "Should saved searches with no messages be listed?"
147 :group 'notmuch-hello)
149 (defun notmuch-sort-saved-searches (saved-searches)
150 "Generate an alphabetically sorted saved searches list."
151 (sort (copy-sequence saved-searches)
153 (string< (notmuch-saved-search-get a :name)
154 (notmuch-saved-search-get b :name)))))
156 (defcustom notmuch-saved-search-sort-function nil
157 "Function used to sort the saved searches for the notmuch-hello view.
159 This variable controls how saved searches should be sorted. No
160 sorting (nil) displays the saved searches in the order they are
161 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
162 saved searches in alphabetical order. Custom sort function should
163 be a function or a lambda expression that takes the saved
164 searches list as a parameter, and returns a new saved searches
165 list to be used. For compatibility with the various saved-search
166 formats it should use notmuch-saved-search-get to access the
167 fields of the search."
168 :type '(choice (const :tag "No sorting" nil)
169 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
170 (function :tag "Custom sort function"
171 :value notmuch-sort-saved-searches))
172 :group 'notmuch-hello)
174 (defvar notmuch-hello-indent 4
175 "How much to indent non-headers.")
177 (defcustom notmuch-show-logo t
178 "Should the notmuch logo be shown?"
180 :group 'notmuch-hello)
182 (defcustom notmuch-show-all-tags-list nil
183 "Should all tags be shown in the notmuch-hello view?"
185 :group 'notmuch-hello)
187 (defcustom notmuch-hello-tag-list-make-query nil
188 "Function or string to generate queries for the all tags list.
190 This variable controls which query results are shown for each tag
191 in the \"all tags\" list. If nil, it will use all messages with
192 that tag. If this is set to a string, it is used as a filter for
193 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
194 Finally this can be a function that will be called for each tag and
195 should return a filter for that tag, or nil to hide the tag."
196 :type '(choice (const :tag "All messages" nil)
197 (const :tag "Unread messages" "tag:unread")
198 (string :tag "Custom filter"
200 (function :tag "Custom filter function"))
201 :group 'notmuch-hello)
203 (defcustom notmuch-hello-hide-tags nil
204 "List of tags to be hidden in the \"all tags\"-section."
205 :type '(repeat string)
206 :group 'notmuch-hello)
208 (defface notmuch-hello-logo-background
211 (:background "#5f5f5f"))
214 (:background "white")))
215 "Background colour for the notmuch logo."
216 :group 'notmuch-hello
217 :group 'notmuch-faces)
219 (defcustom notmuch-column-control t
220 "Controls the number of columns for saved searches/tags in notmuch view.
222 This variable has three potential sets of values:
224 - t: automatically calculate the number of columns possible based
225 on the tags to be shown and the window width,
226 - an integer: a lower bound on the number of characters that will
227 be used to display each column,
228 - a float: a fraction of the window width that is the lower bound
229 on the number of characters that should be used for each
233 - if you would like two columns of tags, set this to 0.5.
234 - if you would like a single column of tags, set this to 1.0.
235 - if you would like tags to be 30 characters wide, set this to
237 - if you don't want to worry about all of this nonsense, leave
240 (const :tag "Automatically calculated" t)
241 (integer :tag "Number of characters")
242 (float :tag "Fraction of window"))
243 :group 'notmuch-hello)
245 (defcustom notmuch-hello-thousands-separator " "
246 "The string used as a thousands separator.
248 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
249 The latter is recommended in the SI/ISO 31-0 standard and by the
250 International Bureau of Weights and Measures."
252 :group 'notmuch-hello)
254 (defcustom notmuch-hello-mode-hook nil
255 "Functions called after entering `notmuch-hello-mode'."
257 :group 'notmuch-hello
258 :group 'notmuch-hooks)
260 (defcustom notmuch-hello-refresh-hook nil
261 "Functions called after updating a `notmuch-hello' buffer."
263 :group 'notmuch-hello
264 :group 'notmuch-hooks)
266 (defvar notmuch-hello-url "http://notmuchmail.org"
267 "The `notmuch' web site.")
269 (defvar notmuch-hello-custom-section-options
270 '((:filter (string :tag "Filter for each tag"))
271 (:filter-count (string :tag "Different filter to generate message counts"))
272 (:initially-hidden (const :tag "Hide this section on startup" t))
273 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
274 (:hide-if-empty (const :tag "Hide this section if all queries are empty
275 \(and not shown by show-empty-searches)" t)))
276 "Various customization-options for notmuch-hello-tags/query-section.")
278 (define-widget 'notmuch-hello-tags-section 'lazy
279 "Customize-type for notmuch-hello tag-list sections."
280 :tag "Customized tag-list section (see docstring for details)"
283 (const :tag "" notmuch-hello-insert-tags-section)
284 (string :tag "Title for this section")
288 ,(append notmuch-hello-custom-section-options
289 '((:hide-tags (repeat :tag "Tags that will be hidden"
292 (define-widget 'notmuch-hello-query-section 'lazy
293 "Customize-type for custom saved-search-like sections"
294 :tag "Customized queries section (see docstring for details)"
297 (const :tag "" notmuch-hello-insert-searches)
298 (string :tag "Title for this section")
299 (repeat :tag "Queries"
300 (cons (string :tag "Name") (string :tag "Query")))
301 (plist :inline t :options ,notmuch-hello-custom-section-options)))
303 (defcustom notmuch-hello-sections
304 (list #'notmuch-hello-insert-header
305 #'notmuch-hello-insert-saved-searches
306 #'notmuch-hello-insert-search
307 #'notmuch-hello-insert-recent-searches
308 #'notmuch-hello-insert-alltags
309 #'notmuch-hello-insert-footer)
310 "Sections for notmuch-hello.
312 The list contains functions which are used to construct sections in
313 notmuch-hello buffer. When notmuch-hello buffer is constructed,
314 these functions are run in the order they appear in this list. Each
315 function produces a section simply by adding content to the current
316 buffer. A section should not end with an empty line, because a
317 newline will be inserted after each section by `notmuch-hello'.
319 Each function should take no arguments. The return value is
322 For convenience an element can also be a list of the form (FUNC ARG1
323 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
326 A \"Customized tag-list section\" item in the customize-interface
327 displays a list of all tags, optionally hiding some of them. It
328 is also possible to filter the list of messages matching each tag
329 by an additional filter query. Similarly, the count of messages
330 displayed next to the buttons can be generated by applying a
331 different filter to the tag query. These filters are also
332 supported for \"Customized queries section\" items."
333 :group 'notmuch-hello
336 (choice (function-item notmuch-hello-insert-header)
337 (function-item notmuch-hello-insert-saved-searches)
338 (function-item notmuch-hello-insert-search)
339 (function-item notmuch-hello-insert-recent-searches)
340 (function-item notmuch-hello-insert-alltags)
341 (function-item notmuch-hello-insert-footer)
342 (function-item notmuch-hello-insert-inbox)
343 notmuch-hello-tags-section
344 notmuch-hello-query-section
345 (function :tag "Custom section"))))
347 (defcustom notmuch-hello-auto-refresh t
348 "Automatically refresh when returning to the notmuch-hello buffer."
349 :group 'notmuch-hello
352 (defvar notmuch-hello-hidden-sections nil
353 "List of sections titles whose contents are hidden")
355 (defvar notmuch-hello-first-run t
356 "True if `notmuch-hello' is run for the first time, set to nil
359 (defun notmuch-hello-nice-number (n)
362 (push (% n 1000) result)
364 (setq result (or result '(0)))
366 (number-to-string (car result))
367 (mapcar (lambda (elem)
368 (format "%s%03d" notmuch-hello-thousands-separator elem))
371 (defun notmuch-hello-trim (search)
373 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
374 (match-string 1 search)
377 (defun notmuch-hello-search (&optional search)
378 (unless (null search)
379 (setq search (notmuch-hello-trim search))
380 (let ((history-delete-duplicates t))
381 (add-to-history 'notmuch-search-history search)))
382 (notmuch-search search notmuch-search-oldest-first))
384 (defun notmuch-hello-add-saved-search (widget)
386 (let ((search (widget-value
388 (widget-get widget :notmuch-saved-search-widget))))
389 (name (completing-read "Name for saved search: "
390 notmuch-saved-searches)))
391 ;; If an existing saved search with this name exists, remove it.
392 (setq notmuch-saved-searches
393 (loop for elem in notmuch-saved-searches
395 (notmuch-saved-search-get elem :name)))
398 (customize-save-variable 'notmuch-saved-searches
399 (add-to-list 'notmuch-saved-searches
400 (list :name name :query search) t))
401 (message "Saved '%s' as '%s'." search name)
402 (notmuch-hello-update)))
404 (defun notmuch-hello-delete-search-from-history (widget)
406 (let ((search (widget-value
408 (widget-get widget :notmuch-saved-search-widget)))))
409 (setq notmuch-search-history (delete search
410 notmuch-search-history))
411 (notmuch-hello-update)))
413 (defun notmuch-hello-longest-label (searches-alist)
414 (or (loop for elem in searches-alist
415 maximize (length (notmuch-saved-search-get elem :name)))
418 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
419 (let ((len (length list)))
420 (loop for col from 0 to (- ncols 1)
421 collect (let ((offset (+ (* nrows col) row)))
424 ;; Don't forget to insert an empty slot in the
425 ;; output matrix if there is no corresponding
426 ;; value in the input matrix.
429 (defun notmuch-hello-reflect (list ncols)
430 "Reflect a `ncols' wide matrix represented by `list' along the
433 (let ((nrows (ceiling (length list) ncols)))
434 (loop for row from 0 to (- nrows 1)
435 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
437 (defun notmuch-hello-widget-search (widget &rest ignore)
438 (if (widget-get widget :notmuch-search-type)
439 (notmuch-tree (widget-get widget
440 :notmuch-search-terms))
441 (notmuch-search (widget-get widget
442 :notmuch-search-terms)
444 :notmuch-search-oldest-first))))
446 (defun notmuch-saved-search-count (search)
447 (car (process-lines notmuch-command "count" search)))
449 (defun notmuch-hello-tags-per-line (widest)
450 "Determine how many tags to show per line and how wide they
451 should be. Returns a cons cell `(tags-per-line width)'."
454 ((integerp notmuch-column-control)
456 (/ (- (window-width) notmuch-hello-indent)
457 ;; Count is 9 wide (8 digits plus space), 1 for the space
459 (+ 9 1 (max notmuch-column-control widest)))))
461 ((floatp notmuch-column-control)
462 (let* ((available-width (- (window-width) notmuch-hello-indent))
463 (proposed-width (max (* available-width notmuch-column-control) widest)))
464 (floor available-width proposed-width)))
468 (/ (- (window-width) notmuch-hello-indent)
469 ;; Count is 9 wide (8 digits plus space), 1 for the space
473 (cons tags-per-line (/ (max 1
474 (- (window-width) notmuch-hello-indent
475 ;; Count is 9 wide (8 digits plus
476 ;; space), 1 for the space after the
478 (* tags-per-line (+ 9 1))))
481 (defun notmuch-hello-filtered-query (query filter)
482 "Constructs a query to search all messages matching QUERY and FILTER.
484 If FILTER is a string, it is directly used in the returned query.
486 If FILTER is a function, it is called with QUERY as a parameter and
487 the string it returns is used as the query. If nil is returned,
490 Otherwise, FILTER is ignored.
493 ((functionp filter) (funcall filter query))
495 (concat "(" query ") and (" filter ")"))
498 (defun notmuch-hello-query-counts (query-list &rest options)
499 "Compute list of counts of matched messages from QUERY-LIST.
501 QUERY-LIST must be a list of saved-searches. Ideally each of
502 these is a plist but other options are available for backwards
503 compatibility: see `notmuch-saved-searches' for details.
505 The result is a list of plists each of which includes the
506 properties :name NAME, :query QUERY and :count COUNT, together
507 with any properties in the original saved-search.
509 The values :show-empty-searches, :filter and :filter-count from
510 options will be handled as specified for
511 `notmuch-hello-insert-searches'."
513 (dolist (elem query-list nil)
514 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
515 (notmuch-saved-search-get elem :query))))
517 (replace-regexp-in-string
519 (notmuch-hello-filtered-query count-query
520 (or (plist-get options :filter-count)
521 (plist-get options :filter))))
524 (unless (= (call-process-region (point-min) (point-max) notmuch-command
525 t t nil "count" "--batch") 0)
526 (notmuch-logged-error "notmuch count --batch failed"
527 "Please check that the notmuch CLI is new enough to support `count
528 --batch'. In general we recommend running matching versions of
529 the CLI and emacs interface."))
531 (goto-char (point-min))
533 (notmuch-remove-if-not
537 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
538 (search-query (plist-get elem-plist :query))
539 (filtered-query (notmuch-hello-filtered-query
540 search-query (plist-get options :filter)))
541 (message-count (prog1 (read (current-buffer))
543 (when (and filtered-query (or (plist-get options :show-empty-searches) (> message-count 0)))
544 (setq elem-plist (plist-put elem-plist :query filtered-query))
545 (plist-put elem-plist :count message-count))))
548 (defun notmuch-hello-insert-buttons (searches)
549 "Insert buttons for SEARCHES.
551 SEARCHES must be a list of plists each of which should contain at
552 least the properties :name NAME :query QUERY and :count COUNT,
553 where QUERY is the query to start when the button for the
554 corresponding entry is activated, and COUNT should be the number
555 of messages matching the query. Such a plist can be computed
556 with `notmuch-hello-query-counts'."
557 (let* ((widest (notmuch-hello-longest-label searches))
558 (tags-and-width (notmuch-hello-tags-per-line widest))
559 (tags-per-line (car tags-and-width))
560 (column-width (cdr tags-and-width))
563 (reordered-list (notmuch-hello-reflect searches tags-per-line))
564 ;; Hack the display of the buttons used.
565 (widget-push-button-prefix "")
566 (widget-push-button-suffix ""))
567 ;; dme: It feels as though there should be a better way to
568 ;; implement this loop than using an incrementing counter.
570 ;; (not elem) indicates an empty slot in the matrix.
572 (if (> column-indent 0)
573 (widget-insert (make-string column-indent ? )))
574 (let* ((name (plist-get elem :name))
575 (query (plist-get elem :query))
576 (oldest-first (case (plist-get elem :sort-order)
579 (otherwise notmuch-search-oldest-first)))
580 (search-type (eq (plist-get elem :search-type) 'tree))
581 (msg-count (plist-get elem :count)))
582 (widget-insert (format "%8s "
583 (notmuch-hello-nice-number msg-count)))
584 (widget-create 'push-button
585 :notify #'notmuch-hello-widget-search
586 :notmuch-search-terms query
587 :notmuch-search-oldest-first oldest-first
588 :notmuch-search-type search-type
591 (1+ (max 0 (- column-width (length name)))))))
592 (setq count (1+ count))
593 (when (eq (% count tags-per-line) 0)
594 (setq column-indent 0)
595 (widget-insert "\n")))
598 ;; If the last line was not full (and hence did not include a
599 ;; carriage return), insert one now.
600 (unless (eq (% count tags-per-line) 0)
601 (widget-insert "\n"))))
603 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
605 (defun notmuch-hello-update (&optional no-display)
606 "Update the current notmuch view."
607 ;; Lazy - rebuild everything.
608 (notmuch-hello no-display))
610 (defun notmuch-hello-window-configuration-change ()
611 "Hook function to update the hello buffer when it is switched to."
612 (let ((hello-buf (get-buffer "*notmuch-hello*"))
614 ;; Consider all windows in the currently selected frame, since
615 ;; that's where the configuration change happened. This also
616 ;; refreshes our snapshot of all windows, so we have to do this
617 ;; even if we know we won't refresh (e.g., hello-buf is null).
618 (dolist (window (window-list))
619 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
620 (cur-buf (window-buffer window)))
621 (when (not (eq last-buf cur-buf))
622 ;; This window changed or is new. Update recorded buffer
624 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
625 (when (and (eq cur-buf hello-buf) last-buf)
626 ;; The user just switched to hello in this window (hello
627 ;; is currently visible, was not visible on the last
628 ;; configuration change, and this is not a new window)
629 (setq do-refresh t)))))
630 (when (and do-refresh notmuch-hello-auto-refresh)
631 ;; Refresh hello as soon as we get back to redisplay. On Emacs
632 ;; 24, we can't do it right here because something in this
633 ;; hook's call stack overrides hello's point placement.
634 (run-at-time nil nil #'notmuch-hello t))
635 (when (null hello-buf)
637 (remove-hook 'window-configuration-change-hook
638 #'notmuch-hello-window-configuration-change))))
640 ;; the following variable is defined as being defconst in notmuch-version.el
641 (defvar notmuch-emacs-version)
643 (defun notmuch-hello-versions ()
644 "Display the notmuch version(s)"
646 (let ((notmuch-cli-version (notmuch-cli-version)))
647 (message "notmuch version %s"
648 (if (string= notmuch-emacs-version notmuch-cli-version)
650 (concat notmuch-cli-version
651 " (emacs mua version " notmuch-emacs-version ")")))))
653 (defvar notmuch-hello-mode-map
654 (let ((map (if (fboundp 'make-composed-keymap)
655 ;; Inherit both widget-keymap and
656 ;; notmuch-common-keymap. We have to use
657 ;; make-sparse-keymap to force this to be a new
658 ;; keymap (so that when we modify map it does not
659 ;; modify widget-keymap).
660 (make-composed-keymap (list (make-sparse-keymap) widget-keymap))
661 ;; Before Emacs 24, keymaps didn't support multiple
662 ;; inheritance,, so just copy the widget keymap since
663 ;; it's unlikely to change.
664 (copy-keymap widget-keymap))))
665 (set-keymap-parent map notmuch-common-keymap)
666 (define-key map "v" 'notmuch-hello-versions)
667 (define-key map (kbd "<C-tab>") 'widget-backward)
669 "Keymap for \"notmuch hello\" buffers.")
670 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
672 (defun notmuch-hello-mode ()
673 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
675 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
676 or click on a saved search to view matching threads. Edit saved
677 searches with the `edit' button. Type `\\[notmuch-jump-search]'
678 in any Notmuch screen for quick access to saved searches that
681 Type new searches in the search box and hit RET to view matching
682 threads. Hit RET in a recent search box to re-submit a previous
683 search. Edit it first if you like. Save a recent search to saved
684 searches with the `save' button.
686 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
687 screen to search for messages and view matching threads or
688 messages, respectively. Recent searches are available in the
691 Expand the all tags view with the `show' button (and collapse
692 again with the `hide' button). Hit RET or click on a tag name to
693 view matching threads.
695 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
696 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
698 The screen may be customized via `\\[customize]'.
700 Complete list of currently available key bindings:
702 \\{notmuch-hello-mode-map}"
704 (kill-all-local-variables)
705 (setq notmuch-buffer-refresh-function #'notmuch-hello-update)
706 (use-local-map notmuch-hello-mode-map)
707 (setq major-mode 'notmuch-hello-mode
708 mode-name "notmuch-hello")
709 (run-mode-hooks 'notmuch-hello-mode-hook)
710 ;;(setq buffer-read-only t)
713 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
714 "Return an alist from tags to queries to display in the all-tags section."
715 (mapcar (lambda (tag)
716 (cons tag (concat "tag:" (notmuch-escape-boolean-term tag))))
717 (notmuch-remove-if-not
719 (not (member tag hide-tags)))
720 (process-lines notmuch-command "search" "--output=tags" "*"))))
722 (defun notmuch-hello-insert-header ()
723 "Insert the default notmuch-hello header."
724 (when notmuch-show-logo
725 (let ((image notmuch-hello-logo))
726 ;; The notmuch logo uses transparency. That can display poorly
727 ;; when inserting the image into an emacs buffer (black logo on
728 ;; a black background), so force the background colour of the
729 ;; image. We use a face to represent the colour so that
730 ;; `defface' can be used to declare the different possible
731 ;; colours, which depend on whether the frame has a light or
733 (setq image (cons 'image
735 (list :background (face-background 'notmuch-hello-logo-background)))))
736 (insert-image image))
739 (widget-insert "Welcome to ")
740 ;; Hack the display of the links used.
741 (let ((widget-link-prefix "")
742 (widget-link-suffix ""))
744 :notify (lambda (&rest ignore)
745 (browse-url notmuch-hello-url))
746 :help-echo "Visit the notmuch website."
749 (widget-insert "You have ")
751 :notify (lambda (&rest ignore)
752 (notmuch-hello-update))
754 (notmuch-hello-nice-number
755 (string-to-number (car (process-lines notmuch-command "count")))))
756 (widget-insert " messages.\n")))
759 (defun notmuch-hello-insert-saved-searches ()
760 "Insert the saved-searches section."
761 (let ((searches (notmuch-hello-query-counts
762 (if notmuch-saved-search-sort-function
763 (funcall notmuch-saved-search-sort-function
764 notmuch-saved-searches)
765 notmuch-saved-searches)
766 :show-empty-searches notmuch-show-empty-saved-searches)))
768 (widget-insert "Saved searches: ")
769 (widget-create 'push-button
770 :notify (lambda (&rest ignore)
771 (customize-variable 'notmuch-saved-searches))
773 (widget-insert "\n\n")
774 (let ((start (point)))
775 (notmuch-hello-insert-buttons searches)
776 (indent-rigidly start (point) notmuch-hello-indent)))))
778 (defun notmuch-hello-insert-search ()
779 "Insert a search widget."
780 (widget-insert "Search: ")
781 (widget-create 'editable-field
782 ;; Leave some space at the start and end of the
784 :size (max 8 (- (window-width) notmuch-hello-indent
785 (length "Search: ")))
786 :action (lambda (widget &rest ignore)
787 (notmuch-hello-search (widget-value widget))))
788 ;; Add an invisible dot to make `widget-end-of-line' ignore
789 ;; trailing spaces in the search widget field. A dot is used
790 ;; instead of a space to make `show-trailing-whitespace'
791 ;; happy, i.e. avoid it marking the whole line as trailing
794 (put-text-property (1- (point)) (point) 'invisible t)
795 (widget-insert "\n"))
797 (defun notmuch-hello-insert-recent-searches ()
798 "Insert recent searches."
799 (when notmuch-search-history
800 (widget-insert "Recent searches: ")
801 (widget-create 'push-button
802 :notify (lambda (&rest ignore)
803 (when (y-or-n-p "Are you sure you want to clear the searches? ")
804 (setq notmuch-search-history nil)
805 (notmuch-hello-update)))
807 (widget-insert "\n\n")
808 (let ((start (point)))
809 (loop for i from 1 to notmuch-hello-recent-searches-max
810 for search in notmuch-search-history do
811 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
813 (widget-create 'editable-field
814 ;; Don't let the search boxes be
815 ;; less than 8 characters wide.
822 (* 2 notmuch-hello-indent)
825 ;; `[save]' button. 6
830 ;; before the `[del]'
834 :action (lambda (widget &rest ignore)
835 (notmuch-hello-search (widget-value widget)))
838 (widget-create 'push-button
839 :notify (lambda (widget &rest ignore)
840 (notmuch-hello-add-saved-search widget))
841 :notmuch-saved-search-widget widget-symbol
844 (widget-create 'push-button
845 :notify (lambda (widget &rest ignore)
846 (when (y-or-n-p "Are you sure you want to delete this search? ")
847 (notmuch-hello-delete-search-from-history widget)))
848 :notmuch-saved-search-widget widget-symbol
850 (widget-insert "\n"))
851 (indent-rigidly start (point) notmuch-hello-indent))
854 (defun notmuch-hello-insert-searches (title query-list &rest options)
855 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
857 QUERY-LIST should ideally be a plist but for backwards
858 compatibility other forms are also accepted (see
859 `notmuch-saved-searches' for details). The plist should
860 contain keys :name and :query; if :count-query is also present
861 then it specifies an alternate query to be used to generate the
862 count for the associated search.
864 Supports the following entries in OPTIONS as a plist:
865 :initially-hidden - if non-nil, section will be hidden on startup
866 :show-empty-searches - show buttons with no matching messages
867 :hide-if-empty - hide if no buttons would be shown
868 (only makes sense without :show-empty-searches)
869 :filter - This can be a function that takes the search query as its argument and
870 returns a filter to be used in conjuction with the query for that search or nil
871 to hide the element. This can also be a string that is used as a combined with
872 each query using \"and\".
873 :filter-count - Separate filter to generate the count displayed each search. Accepts
874 the same values as :filter. If :filter and :filter-count are specified, this
875 will be used instead of :filter, not in conjunction with it."
876 (widget-insert title ": ")
877 (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
878 (add-to-list 'notmuch-hello-hidden-sections title))
879 (let ((is-hidden (member title notmuch-hello-hidden-sections))
882 (widget-create 'push-button
883 :notify `(lambda (widget &rest ignore)
884 (setq notmuch-hello-hidden-sections
885 (delete ,title notmuch-hello-hidden-sections))
886 (notmuch-hello-update))
888 (widget-create 'push-button
889 :notify `(lambda (widget &rest ignore)
890 (add-to-list 'notmuch-hello-hidden-sections
892 (notmuch-hello-update))
895 (when (not is-hidden)
896 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
897 (when (or (not (plist-get options :hide-if-empty))
900 (notmuch-hello-insert-buttons searches)
901 (indent-rigidly start (point) notmuch-hello-indent))))))
903 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
904 "Insert a section displaying all tags with message counts.
906 TITLE defaults to \"All tags\".
907 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
910 :hide-tags - List of tags that should be excluded."
911 (apply 'notmuch-hello-insert-searches
912 (or title "All tags")
913 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
916 (defun notmuch-hello-insert-inbox ()
917 "Show an entry for each saved search and inboxed messages for each tag"
918 (notmuch-hello-insert-searches "What's in your inbox"
920 notmuch-saved-searches
921 (notmuch-hello-generate-tag-alist))
922 :filter "tag:inbox"))
924 (defun notmuch-hello-insert-alltags ()
925 "Insert a section displaying all tags and associated message counts"
926 (notmuch-hello-insert-tags-section
928 :initially-hidden (not notmuch-show-all-tags-list)
929 :hide-tags notmuch-hello-hide-tags
930 :filter notmuch-hello-tag-list-make-query))
932 (defun notmuch-hello-insert-footer ()
933 "Insert the notmuch-hello footer."
934 (let ((start (point)))
935 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
936 (widget-insert "Customize ")
938 :notify (lambda (&rest ignore)
939 (customize-group 'notmuch))
940 :button-prefix "" :button-suffix ""
942 (widget-insert " or ")
944 :notify (lambda (&rest ignore)
945 (customize-variable 'notmuch-hello-sections))
946 :button-prefix "" :button-suffix ""
948 (let ((fill-column (- (window-width) notmuch-hello-indent)))
949 (center-region start (point)))))
952 (defun notmuch-hello (&optional no-display)
953 "Run notmuch and display saved searches, known tags, etc."
956 (notmuch-assert-cli-sane)
957 ;; This may cause a window configuration change, so if the
958 ;; auto-refresh hook is already installed, avoid recursive refresh.
959 (let ((notmuch-hello-auto-refresh nil))
961 (set-buffer "*notmuch-hello*")
962 (switch-to-buffer "*notmuch-hello*")))
964 ;; Install auto-refresh hook
965 (when notmuch-hello-auto-refresh
966 (add-hook 'window-configuration-change-hook
967 #'notmuch-hello-window-configuration-change))
969 (let ((target-line (line-number-at-pos))
970 (target-column (current-column))
971 (inhibit-read-only t))
973 ;; Delete all editable widget fields. Editable widget fields are
974 ;; tracked in a buffer local variable `widget-field-list' (and
975 ;; others). If we do `erase-buffer' without properly deleting the
976 ;; widgets, some widget-related functions are confused later.
977 (mapc 'widget-delete widget-field-list)
981 (unless (eq major-mode 'notmuch-hello-mode)
982 (notmuch-hello-mode))
984 (let ((all (overlay-lists)))
985 ;; Delete all the overlays.
986 (mapc 'delete-overlay (car all))
987 (mapc 'delete-overlay (cdr all)))
991 (let ((point-before (point)))
992 (if (functionp section)
994 (apply (car section) (cdr section)))
995 ;; don't insert a newline when the previous section didn't
997 (unless (eq (point) point-before)
998 (widget-insert "\n"))))
999 notmuch-hello-sections)
1002 ;; Move point back to where it was before refresh. Use line and
1003 ;; column instead of point directly to be insensitive to additions
1004 ;; and removals of text within earlier lines.
1005 (goto-char (point-min))
1006 (forward-line (1- target-line))
1007 (move-to-column target-column))
1008 (run-hooks 'notmuch-hello-refresh-hook)
1009 (setq notmuch-hello-first-run nil))
1011 (defun notmuch-folder ()
1012 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
1018 (provide 'notmuch-hello)