1 ;;; notmuch-hello.el --- welcome to notmuch, a frontend -*- lexical-binding: t -*-
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 <https://www.gnu.org/licenses/>.
20 ;; Authors: David Edmondson <dme@dme.org>
25 (require 'wid-edit) ; For `widget-forward'.
27 (require 'notmuch-lib)
28 (require 'notmuch-mua)
30 (declare-function notmuch-search "notmuch"
31 (&optional query oldest-first target-thread target-line continuation))
32 (declare-function notmuch-poll "notmuch" ())
33 (declare-function notmuch-tree "notmuch-tree"
34 (&optional query query-context target buffer-name open-target unthreaded))
35 (declare-function notmuch-unthreaded
36 (&optional query query-context target buffer-name open-target))
41 (defun notmuch-saved-search-get (saved-search field)
42 "Get FIELD from SAVED-SEARCH.
44 If SAVED-SEARCH is a plist, this is just `plist-get', but for
45 backwards compatibility, this also deals with the two other
46 possible formats for SAVED-SEARCH: cons cells (NAME . QUERY) and
47 lists (NAME QUERY COUNT-QUERY)."
49 ((keywordp (car saved-search))
50 (plist-get saved-search field))
51 ;; It is not a plist so it is an old-style entry.
52 ((consp (cdr saved-search))
53 (pcase-let ((`(,name ,query ,count-query) saved-search))
57 (:count-query count-query)
60 (pcase-let ((`(,name . ,query) saved-search))
66 (defun notmuch-hello-saved-search-to-plist (saved-search)
67 "Return a copy of SAVED-SEARCH in plist form.
69 If saved search is a plist then just return a copy. In other
70 cases, for backwards compatibility, convert to plist form and
72 (if (keywordp (car saved-search))
73 (copy-sequence saved-search)
74 (let ((fields (list :name :query :count-query))
76 (dolist (field fields plist-search)
77 (let ((string (notmuch-saved-search-get saved-search field)))
79 (setq plist-search (append plist-search (list field string)))))))))
81 (defun notmuch-hello--saved-searches-to-plist (symbol)
82 "Extract a saved-search variable into plist form.
84 The new style saved search is just a plist, but for backwards
85 compatibility we use this function to extract old style saved
86 searches so they still work in customize."
87 (let ((saved-searches (default-value symbol)))
88 (mapcar #'notmuch-hello-saved-search-to-plist saved-searches)))
90 (define-widget 'notmuch-saved-search-plist 'list
91 "A single saved search property list."
93 :args '((list :inline t
95 (group :format "%v" :inline t
96 (const :format " Name: " :name)
97 (string :format "%v"))
98 (group :format "%v" :inline t
99 (const :format " Query: " :query)
100 (string :format "%v")))
103 (group :format "%v" :inline t
104 (const :format "Shortcut key: " :key)
105 (key-sequence :format "%v"))
106 (group :format "%v" :inline t
107 (const :format "Count-Query: " :count-query)
108 (string :format "%v"))
109 (group :format "%v" :inline t
110 (const :format "" :sort-order)
111 (choice :tag " Sort Order"
112 (const :tag "Default" nil)
113 (const :tag "Oldest-first" oldest-first)
114 (const :tag "Newest-first" newest-first)))
115 (group :format "%v" :inline t
116 (const :format "" :search-type)
117 (choice :tag " Search Type"
118 (const :tag "Search mode" nil)
119 (const :tag "Tree mode" tree)
120 (const :tag "Unthreaded mode" unthreaded))))))
122 (defcustom notmuch-saved-searches
123 `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
124 (:name "unread" :query "tag:unread" :key ,(kbd "u"))
125 (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
126 (:name "sent" :query "tag:sent" :key ,(kbd "t"))
127 (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
128 (:name "all mail" :query "*" :key ,(kbd "a")))
129 "A list of saved searches to display.
131 The saved search can be given in 3 forms. The preferred way is as
132 a plist. Supported properties are
134 :name Name of the search (required).
135 :query Search to run (required).
136 :key Optional shortcut key for `notmuch-jump-search'.
137 :count-query Optional extra query to generate the count
138 shown. If not present then the :query property
140 :sort-order Specify the sort order to be used for the search.
141 Possible values are `oldest-first', `newest-first'
142 or nil. Nil means use the default sort order.
143 :search-type Specify whether to run the search in search-mode,
144 tree mode or unthreaded mode. Set to 'tree to specify tree
145 mode, 'unthreaded to specify unthreaded mode, and set to nil
146 (or anything except tree and unthreaded) to specify search mode.
148 Other accepted forms are a cons cell of the form (NAME . QUERY)
149 or a list of the form (NAME QUERY COUNT-QUERY)."
150 ;; The saved-search format is also used by the all-tags notmuch-hello
151 ;; section. This section generates its own saved-search list in one of
152 ;; the latter two forms.
153 :get 'notmuch-hello--saved-searches-to-plist
154 :type '(repeat notmuch-saved-search-plist)
155 :tag "List of Saved Searches"
156 :group 'notmuch-hello)
158 (defcustom notmuch-hello-recent-searches-max 10
159 "The number of recent searches to display."
161 :group 'notmuch-hello)
163 (defcustom notmuch-show-empty-saved-searches nil
164 "Should saved searches with no messages be listed?"
166 :group 'notmuch-hello)
168 (defun notmuch-sort-saved-searches (saved-searches)
169 "Generate an alphabetically sorted saved searches list."
170 (sort (copy-sequence saved-searches)
172 (string< (notmuch-saved-search-get a :name)
173 (notmuch-saved-search-get b :name)))))
175 (defcustom notmuch-saved-search-sort-function nil
176 "Function used to sort the saved searches for the notmuch-hello view.
178 This variable controls how saved searches should be sorted. No
179 sorting (nil) displays the saved searches in the order they are
180 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
181 saved searches in alphabetical order. Custom sort function should
182 be a function or a lambda expression that takes the saved
183 searches list as a parameter, and returns a new saved searches
184 list to be used. For compatibility with the various saved-search
185 formats it should use notmuch-saved-search-get to access the
186 fields of the search."
187 :type '(choice (const :tag "No sorting" nil)
188 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
189 (function :tag "Custom sort function"
190 :value notmuch-sort-saved-searches))
191 :group 'notmuch-hello)
193 (defvar notmuch-hello-indent 4
194 "How much to indent non-headers.")
196 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
198 (defcustom notmuch-show-logo t
199 "Should the notmuch logo be shown?"
201 :group 'notmuch-hello)
203 (defcustom notmuch-show-all-tags-list nil
204 "Should all tags be shown in the notmuch-hello view?"
206 :group 'notmuch-hello)
208 (defcustom notmuch-hello-tag-list-make-query nil
209 "Function or string to generate queries for the all tags list.
211 This variable controls which query results are shown for each tag
212 in the \"all tags\" list. If nil, it will use all messages with
213 that tag. If this is set to a string, it is used as a filter for
214 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
215 Finally this can be a function that will be called for each tag and
216 should return a filter for that tag, or nil to hide the tag."
217 :type '(choice (const :tag "All messages" nil)
218 (const :tag "Unread messages" "tag:unread")
219 (string :tag "Custom filter"
221 (function :tag "Custom filter function"))
222 :group 'notmuch-hello)
224 (defcustom notmuch-hello-hide-tags nil
225 "List of tags to be hidden in the \"all tags\"-section."
226 :type '(repeat string)
227 :group 'notmuch-hello)
229 (defface notmuch-hello-logo-background
232 (:background "#5f5f5f"))
235 (:background "white")))
236 "Background colour for the notmuch logo."
237 :group 'notmuch-hello
238 :group 'notmuch-faces)
240 (defcustom notmuch-column-control t
241 "Controls the number of columns for saved searches/tags in notmuch view.
243 This variable has three potential sets of values:
245 - t: automatically calculate the number of columns possible based
246 on the tags to be shown and the window width,
247 - an integer: a lower bound on the number of characters that will
248 be used to display each column,
249 - a float: a fraction of the window width that is the lower bound
250 on the number of characters that should be used for each
254 - if you would like two columns of tags, set this to 0.5.
255 - if you would like a single column of tags, set this to 1.0.
256 - if you would like tags to be 30 characters wide, set this to
258 - if you don't want to worry about all of this nonsense, leave
261 (const :tag "Automatically calculated" t)
262 (integer :tag "Number of characters")
263 (float :tag "Fraction of window"))
264 :group 'notmuch-hello)
266 (defcustom notmuch-hello-thousands-separator " "
267 "The string used as a thousands separator.
269 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
270 The latter is recommended in the SI/ISO 31-0 standard and by the
271 International Bureau of Weights and Measures."
273 :group 'notmuch-hello)
275 (defcustom notmuch-hello-mode-hook nil
276 "Functions called after entering `notmuch-hello-mode'."
278 :group 'notmuch-hello
279 :group 'notmuch-hooks)
281 (defcustom notmuch-hello-refresh-hook nil
282 "Functions called after updating a `notmuch-hello' buffer."
284 :group 'notmuch-hello
285 :group 'notmuch-hooks)
287 (defconst notmuch-hello-url "https://notmuchmail.org"
288 "The `notmuch' web site.")
290 (defvar notmuch-hello-custom-section-options
291 '((:filter (string :tag "Filter for each tag"))
292 (:filter-count (string :tag "Different filter to generate message counts"))
293 (:initially-hidden (const :tag "Hide this section on startup" t))
294 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
295 (:hide-if-empty (const :tag "Hide this section if all queries are empty
296 \(and not shown by show-empty-searches)" t)))
297 "Various customization-options for notmuch-hello-tags/query-section.")
299 (define-widget 'notmuch-hello-tags-section 'lazy
300 "Customize-type for notmuch-hello tag-list sections."
301 :tag "Customized tag-list section (see docstring for details)"
304 (const :tag "" notmuch-hello-insert-tags-section)
305 (string :tag "Title for this section")
309 ,(append notmuch-hello-custom-section-options
310 '((:hide-tags (repeat :tag "Tags that will be hidden"
313 (define-widget 'notmuch-hello-query-section 'lazy
314 "Customize-type for custom saved-search-like sections"
315 :tag "Customized queries section (see docstring for details)"
318 (const :tag "" notmuch-hello-insert-searches)
319 (string :tag "Title for this section")
320 (repeat :tag "Queries"
321 (cons (string :tag "Name") (string :tag "Query")))
322 (plist :inline t :options ,notmuch-hello-custom-section-options)))
324 (defcustom notmuch-hello-sections
325 (list #'notmuch-hello-insert-header
326 #'notmuch-hello-insert-saved-searches
327 #'notmuch-hello-insert-search
328 #'notmuch-hello-insert-recent-searches
329 #'notmuch-hello-insert-alltags
330 #'notmuch-hello-insert-footer)
331 "Sections for notmuch-hello.
333 The list contains functions which are used to construct sections in
334 notmuch-hello buffer. When notmuch-hello buffer is constructed,
335 these functions are run in the order they appear in this list. Each
336 function produces a section simply by adding content to the current
337 buffer. A section should not end with an empty line, because a
338 newline will be inserted after each section by `notmuch-hello'.
340 Each function should take no arguments. The return value is
343 For convenience an element can also be a list of the form (FUNC ARG1
344 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
347 A \"Customized tag-list section\" item in the customize-interface
348 displays a list of all tags, optionally hiding some of them. It
349 is also possible to filter the list of messages matching each tag
350 by an additional filter query. Similarly, the count of messages
351 displayed next to the buttons can be generated by applying a
352 different filter to the tag query. These filters are also
353 supported for \"Customized queries section\" items."
354 :group 'notmuch-hello
357 (choice (function-item notmuch-hello-insert-header)
358 (function-item notmuch-hello-insert-saved-searches)
359 (function-item notmuch-hello-insert-search)
360 (function-item notmuch-hello-insert-recent-searches)
361 (function-item notmuch-hello-insert-alltags)
362 (function-item notmuch-hello-insert-footer)
363 (function-item notmuch-hello-insert-inbox)
364 notmuch-hello-tags-section
365 notmuch-hello-query-section
366 (function :tag "Custom section"))))
368 (defcustom notmuch-hello-auto-refresh t
369 "Automatically refresh when returning to the notmuch-hello buffer."
370 :group 'notmuch-hello
373 ;;; Internal variables
375 (defvar notmuch-hello-hidden-sections nil
376 "List of sections titles whose contents are hidden.")
378 (defvar notmuch-hello-first-run t
379 "True if `notmuch-hello' is run for the first time, set to nil afterwards.")
381 ;;; Widgets for inserters
383 (define-widget 'notmuch-search-item 'item
386 :value-create 'notmuch-search-item-value-create)
388 (defun notmuch-search-item-value-create (widget)
389 (let ((value (widget-get widget :value)))
390 (widget-insert (make-string notmuch-hello-indent ?\s))
391 (widget-create 'editable-field
392 :size (widget-get widget :size)
394 :action #'notmuch-hello-search
397 (widget-create 'push-button
399 :notify #'notmuch-hello-add-saved-search
402 (widget-create 'push-button
404 :notify #'notmuch-hello-delete-search-from-history
407 (defun notmuch-search-item-field-width ()
408 (max 8 ; Don't let the search boxes be less than 8 characters wide.
410 notmuch-hello-indent ; space at bol
411 notmuch-hello-indent ; space at eol
412 1 ; for the space before the [save] button
413 6 ; for the [save] button
414 1 ; for the space before the [del] button
415 5))) ; for the [del] button
419 (defun notmuch-hello-search (widget &rest _event)
420 (let ((search (widget-value widget)))
422 (setq search (string-trim search))
423 (let ((history-delete-duplicates t))
424 (add-to-history 'notmuch-search-history search)))
425 (notmuch-search search notmuch-search-oldest-first)))
427 (defun notmuch-hello-add-saved-search (widget &rest _event)
428 (let ((search (widget-value (widget-get widget :parent)))
429 (name (completing-read "Name for saved search: "
430 notmuch-saved-searches)))
431 ;; If an existing saved search with this name exists, remove it.
432 (setq notmuch-saved-searches
433 (cl-loop for elem in notmuch-saved-searches
434 unless (equal name (notmuch-saved-search-get elem :name))
437 (customize-save-variable 'notmuch-saved-searches
438 (add-to-list 'notmuch-saved-searches
439 (list :name name :query search) t))
440 (message "Saved '%s' as '%s'." search name)
441 (notmuch-hello-update)))
443 (defun notmuch-hello-delete-search-from-history (widget &rest _event)
444 (when (y-or-n-p "Are you sure you want to delete this search? ")
445 (let ((search (widget-value (widget-get widget :parent))))
446 (setq notmuch-search-history
447 (delete search notmuch-search-history)))
448 (notmuch-hello-update)))
452 ;; `notmuch-hello-query-counts', `notmuch-hello-nice-number' and
453 ;; `notmuch-hello-insert-buttons' are used outside this section.
454 ;; All other functions that are defined in this section are only
455 ;; used by these two functions.
457 (defun notmuch-hello-longest-label (searches-alist)
458 (or (cl-loop for elem in searches-alist
459 maximize (length (notmuch-saved-search-get elem :name)))
462 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
463 (let ((len (length list)))
464 (cl-loop for col from 0 to (- ncols 1)
465 collect (let ((offset (+ (* nrows col) row)))
468 ;; Don't forget to insert an empty slot in the
469 ;; output matrix if there is no corresponding
470 ;; value in the input matrix.
473 (defun notmuch-hello-reflect (list ncols)
474 "Reflect a `ncols' wide matrix represented by `list' along the
477 (let ((nrows (ceiling (length list) ncols)))
478 (cl-loop for row from 0 to (- nrows 1)
479 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
481 (defun notmuch-hello-widget-search (widget &rest _ignore)
482 (cl-case (widget-get widget :notmuch-search-type)
484 (notmuch-tree (widget-get widget :notmuch-search-terms)))
486 (notmuch-unthreaded (widget-get widget :notmuch-search-terms)))
488 (notmuch-search (widget-get widget :notmuch-search-terms)
489 (widget-get widget :notmuch-search-oldest-first)))))
491 (defun notmuch-saved-search-count (search)
492 (car (process-lines notmuch-command "count" search)))
494 (defun notmuch-hello-tags-per-line (widest)
495 "Determine how many tags to show per line and how wide they
496 should be. Returns a cons cell `(tags-per-line width)'."
499 ((integerp notmuch-column-control)
501 (/ (- (window-width) notmuch-hello-indent)
502 ;; Count is 9 wide (8 digits plus space), 1 for the space
504 (+ 9 1 (max notmuch-column-control widest)))))
505 ((floatp notmuch-column-control)
506 (let* ((available-width (- (window-width) notmuch-hello-indent))
507 (proposed-width (max (* available-width notmuch-column-control)
509 (floor available-width proposed-width)))
512 (/ (- (window-width) notmuch-hello-indent)
513 ;; Count is 9 wide (8 digits plus space), 1 for the space
516 (cons tags-per-line (/ (max 1
517 (- (window-width) notmuch-hello-indent
518 ;; Count is 9 wide (8 digits plus
519 ;; space), 1 for the space after the
521 (* tags-per-line (+ 9 1))))
524 (defun notmuch-hello-filtered-query (query filter)
525 "Constructs a query to search all messages matching QUERY and FILTER.
527 If FILTER is a string, it is directly used in the returned query.
529 If FILTER is a function, it is called with QUERY as a parameter and
530 the string it returns is used as the query. If nil is returned,
533 Otherwise, FILTER is ignored."
535 ((functionp filter) (funcall filter query))
537 (concat "(" query ") and (" filter ")"))
540 (defun notmuch-hello-query-counts (query-list &rest options)
541 "Compute list of counts of matched messages from QUERY-LIST.
543 QUERY-LIST must be a list of saved-searches. Ideally each of
544 these is a plist but other options are available for backwards
545 compatibility: see `notmuch-saved-searches' for details.
547 The result is a list of plists each of which includes the
548 properties :name NAME, :query QUERY and :count COUNT, together
549 with any properties in the original saved-search.
551 The values :show-empty-searches, :filter and :filter-count from
552 options will be handled as specified for
553 `notmuch-hello-insert-searches'."
555 (dolist (elem query-list nil)
556 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
557 (notmuch-saved-search-get elem :query))))
559 (replace-regexp-in-string
561 (notmuch-hello-filtered-query count-query
562 (or (plist-get options :filter-count)
563 (plist-get options :filter))))
565 (unless (= (call-process-region (point-min) (point-max) notmuch-command
566 t t nil "count" "--batch") 0)
567 (notmuch-logged-error
568 "notmuch count --batch failed"
569 "Please check that the notmuch CLI is new enough to support `count
570 --batch'. In general we recommend running matching versions of
571 the CLI and emacs interface."))
572 (goto-char (point-min))
575 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
576 (search-query (plist-get elem-plist :query))
577 (filtered-query (notmuch-hello-filtered-query
578 search-query (plist-get options :filter)))
579 (message-count (prog1 (read (current-buffer))
581 (when (and filtered-query (or (plist-get options :show-empty-searches)
582 (> message-count 0)))
583 (setq elem-plist (plist-put elem-plist :query filtered-query))
584 (list (plist-put elem-plist :count message-count)))))
587 (defun notmuch-hello-nice-number (n)
590 (push (% n 1000) result)
592 (setq result (or result '(0)))
594 (number-to-string (car result))
595 (mapcar (lambda (elem)
596 (format "%s%03d" notmuch-hello-thousands-separator elem))
599 (defun notmuch-hello-insert-buttons (searches)
600 "Insert buttons for SEARCHES.
602 SEARCHES must be a list of plists each of which should contain at
603 least the properties :name NAME :query QUERY and :count COUNT,
604 where QUERY is the query to start when the button for the
605 corresponding entry is activated, and COUNT should be the number
606 of messages matching the query. Such a plist can be computed
607 with `notmuch-hello-query-counts'."
608 (let* ((widest (notmuch-hello-longest-label searches))
609 (tags-and-width (notmuch-hello-tags-per-line widest))
610 (tags-per-line (car tags-and-width))
611 (column-width (cdr tags-and-width))
614 (reordered-list (notmuch-hello-reflect searches tags-per-line))
615 ;; Hack the display of the buttons used.
616 (widget-push-button-prefix "")
617 (widget-push-button-suffix ""))
618 ;; dme: It feels as though there should be a better way to
619 ;; implement this loop than using an incrementing counter.
621 ;; (not elem) indicates an empty slot in the matrix.
623 (when (> column-indent 0)
624 (widget-insert (make-string column-indent ? )))
625 (let* ((name (plist-get elem :name))
626 (query (plist-get elem :query))
627 (oldest-first (cl-case (plist-get elem :sort-order)
630 (otherwise notmuch-search-oldest-first)))
631 (search-type (plist-get elem :search-type))
632 (msg-count (plist-get elem :count)))
633 (widget-insert (format "%8s "
634 (notmuch-hello-nice-number msg-count)))
635 (widget-create 'push-button
636 :notify #'notmuch-hello-widget-search
637 :notmuch-search-terms query
638 :notmuch-search-oldest-first oldest-first
639 :notmuch-search-type search-type
642 (1+ (max 0 (- column-width (length name)))))))
644 (when (eq (% count tags-per-line) 0)
645 (setq column-indent 0)
646 (widget-insert "\n")))
648 ;; If the last line was not full (and hence did not include a
649 ;; carriage return), insert one now.
650 (unless (eq (% count tags-per-line) 0)
651 (widget-insert "\n"))))
655 (defun notmuch-hello-update ()
656 "Update the notmuch-hello buffer."
657 ;; Lazy - rebuild everything.
661 (defun notmuch-hello-window-configuration-change ()
662 "Hook function to update the hello buffer when it is switched to."
663 (let ((hello-buf (get-buffer "*notmuch-hello*"))
665 ;; Consider all windows in the currently selected frame, since
666 ;; that's where the configuration change happened. This also
667 ;; refreshes our snapshot of all windows, so we have to do this
668 ;; even if we know we won't refresh (e.g., hello-buf is null).
669 (dolist (window (window-list))
670 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
671 (cur-buf (window-buffer window)))
672 (unless (eq last-buf cur-buf)
673 ;; This window changed or is new. Update recorded buffer
675 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
676 (when (and (eq cur-buf hello-buf) last-buf)
677 ;; The user just switched to hello in this window (hello
678 ;; is currently visible, was not visible on the last
679 ;; configuration change, and this is not a new window)
680 (setq do-refresh t)))))
681 (when (and do-refresh notmuch-hello-auto-refresh)
682 ;; Refresh hello as soon as we get back to redisplay. On Emacs
683 ;; 24, we can't do it right here because something in this
684 ;; hook's call stack overrides hello's point placement.
685 ;; FIXME And on Emacs releases that we still support?
686 (run-at-time nil nil #'notmuch-hello t))
689 (remove-hook 'window-configuration-change-hook
690 #'notmuch-hello-window-configuration-change))))
692 (defvar notmuch-hello-mode-map
693 ;; Inherit both widget-keymap and notmuch-common-keymap. We have
694 ;; to use make-sparse-keymap to force this to be a new keymap (so
695 ;; that when we modify map it does not modify widget-keymap).
696 (let ((map (make-composed-keymap (list (make-sparse-keymap) widget-keymap))))
697 (set-keymap-parent map notmuch-common-keymap)
698 (define-key map (kbd "<C-tab>") 'widget-backward)
700 "Keymap for \"notmuch hello\" buffers.")
702 (define-derived-mode notmuch-hello-mode fundamental-mode "notmuch-hello"
703 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
705 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
706 or click on a saved search to view matching threads. Edit saved
707 searches with the `edit' button. Type `\\[notmuch-jump-search]'
708 in any Notmuch screen for quick access to saved searches that
711 Type new searches in the search box and hit RET to view matching
712 threads. Hit RET in a recent search box to re-submit a previous
713 search. Edit it first if you like. Save a recent search to saved
714 searches with the `save' button.
716 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
717 screen to search for messages and view matching threads or
718 messages, respectively. Recent searches are available in the
721 Expand the all tags view with the `show' button (and collapse
722 again with the `hide' button). Hit RET or click on a tag name to
723 view matching threads.
725 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
726 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
728 The screen may be customized via `\\[customize]'.
730 Complete list of currently available key bindings:
732 \\{notmuch-hello-mode-map}"
733 (setq notmuch-buffer-refresh-function #'notmuch-hello-update))
737 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
738 "Return an alist from tags to queries to display in the all-tags section."
739 (cl-mapcan (lambda (tag)
740 (and (not (member tag hide-tags))
743 (notmuch-escape-boolean-term tag))))))
744 (process-lines notmuch-command "search" "--output=tags" "*")))
746 (defun notmuch-hello-insert-header ()
747 "Insert the default notmuch-hello header."
748 (when notmuch-show-logo
749 (let ((image notmuch-hello-logo))
750 ;; The notmuch logo uses transparency. That can display poorly
751 ;; when inserting the image into an emacs buffer (black logo on
752 ;; a black background), so force the background colour of the
753 ;; image. We use a face to represent the colour so that
754 ;; `defface' can be used to declare the different possible
755 ;; colours, which depend on whether the frame has a light or
757 (setq image (cons 'image
761 'notmuch-hello-logo-background)))))
762 (insert-image image))
765 (widget-insert "Welcome to ")
766 ;; Hack the display of the links used.
767 (let ((widget-link-prefix "")
768 (widget-link-suffix ""))
770 :notify (lambda (&rest _ignore)
771 (browse-url notmuch-hello-url))
772 :help-echo "Visit the notmuch website."
775 (widget-insert "You have ")
777 :notify (lambda (&rest _ignore)
778 (notmuch-hello-update))
780 (notmuch-hello-nice-number
782 (car (process-lines notmuch-command "count")))))
783 (widget-insert " messages.\n")))
785 (defun notmuch-hello-insert-saved-searches ()
786 "Insert the saved-searches section."
787 (let ((searches (notmuch-hello-query-counts
788 (if notmuch-saved-search-sort-function
789 (funcall notmuch-saved-search-sort-function
790 notmuch-saved-searches)
791 notmuch-saved-searches)
792 :show-empty-searches notmuch-show-empty-saved-searches)))
794 (widget-insert "Saved searches: ")
795 (widget-create 'push-button
796 :notify (lambda (&rest _ignore)
797 (customize-variable 'notmuch-saved-searches))
799 (widget-insert "\n\n")
800 (let ((start (point)))
801 (notmuch-hello-insert-buttons searches)
802 (indent-rigidly start (point) notmuch-hello-indent)))))
804 (defun notmuch-hello-insert-search ()
805 "Insert a search widget."
806 (widget-insert "Search: ")
807 (widget-create 'editable-field
808 ;; Leave some space at the start and end of the
810 :size (max 8 (- (window-width) notmuch-hello-indent
811 (length "Search: ")))
812 :action #'notmuch-hello-search)
813 ;; Add an invisible dot to make `widget-end-of-line' ignore
814 ;; trailing spaces in the search widget field. A dot is used
815 ;; instead of a space to make `show-trailing-whitespace'
816 ;; happy, i.e. avoid it marking the whole line as trailing
818 (widget-insert (propertize "." 'invisible t))
819 (widget-insert "\n"))
821 (defun notmuch-hello-insert-recent-searches ()
822 "Insert recent searches."
823 (when notmuch-search-history
824 (widget-insert "Recent searches: ")
827 :notify (lambda (&rest _ignore)
828 (when (y-or-n-p "Are you sure you want to clear the searches? ")
829 (setq notmuch-search-history nil)
830 (notmuch-hello-update)))
832 (widget-insert "\n\n")
833 (let ((width (notmuch-search-item-field-width)))
834 (dolist (search (seq-take notmuch-search-history
835 notmuch-hello-recent-searches-max))
836 (widget-create 'notmuch-search-item :value search :size width)))))
838 (defun notmuch-hello-insert-searches (title query-list &rest options)
839 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
841 QUERY-LIST should ideally be a plist but for backwards
842 compatibility other forms are also accepted (see
843 `notmuch-saved-searches' for details). The plist should
844 contain keys :name and :query; if :count-query is also present
845 then it specifies an alternate query to be used to generate the
846 count for the associated search.
848 Supports the following entries in OPTIONS as a plist:
849 :initially-hidden - if non-nil, section will be hidden on startup
850 :show-empty-searches - show buttons with no matching messages
851 :hide-if-empty - hide if no buttons would be shown
852 (only makes sense without :show-empty-searches)
853 :filter - This can be a function that takes the search query as its argument and
854 returns a filter to be used in conjunction with the query for that search or nil
855 to hide the element. This can also be a string that is used as a combined with
856 each query using \"and\".
857 :filter-count - Separate filter to generate the count displayed each search. Accepts
858 the same values as :filter. If :filter and :filter-count are specified, this
859 will be used instead of :filter, not in conjunction with it."
860 (widget-insert title ": ")
861 (when (and notmuch-hello-first-run (plist-get options :initially-hidden))
862 (add-to-list 'notmuch-hello-hidden-sections title))
863 (let ((is-hidden (member title notmuch-hello-hidden-sections))
866 (widget-create 'push-button
867 :notify `(lambda (widget &rest _ignore)
868 (setq notmuch-hello-hidden-sections
869 (delete ,title notmuch-hello-hidden-sections))
870 (notmuch-hello-update))
872 (widget-create 'push-button
873 :notify `(lambda (widget &rest _ignore)
874 (add-to-list 'notmuch-hello-hidden-sections
876 (notmuch-hello-update))
880 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
881 (when (or (not (plist-get options :hide-if-empty))
884 (notmuch-hello-insert-buttons searches)
885 (indent-rigidly start (point) notmuch-hello-indent))))))
887 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
888 "Insert a section displaying all tags with message counts.
890 TITLE defaults to \"All tags\".
891 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
894 :hide-tags - List of tags that should be excluded."
895 (apply 'notmuch-hello-insert-searches
896 (or title "All tags")
897 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
900 (defun notmuch-hello-insert-inbox ()
901 "Show an entry for each saved search and inboxed messages for each tag."
902 (notmuch-hello-insert-searches "What's in your inbox"
904 notmuch-saved-searches
905 (notmuch-hello-generate-tag-alist))
906 :filter "tag:inbox"))
908 (defun notmuch-hello-insert-alltags ()
909 "Insert a section displaying all tags and associated message counts."
910 (notmuch-hello-insert-tags-section
912 :initially-hidden (not notmuch-show-all-tags-list)
913 :hide-tags notmuch-hello-hide-tags
914 :filter notmuch-hello-tag-list-make-query))
916 (defun notmuch-hello-insert-footer ()
917 "Insert the notmuch-hello footer."
918 (let ((start (point)))
919 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
920 (widget-insert "Customize ")
922 :notify (lambda (&rest _ignore)
923 (customize-group 'notmuch))
924 :button-prefix "" :button-suffix ""
926 (widget-insert " or ")
928 :notify (lambda (&rest _ignore)
929 (customize-variable 'notmuch-hello-sections))
930 :button-prefix "" :button-suffix ""
932 (let ((fill-column (- (window-width) notmuch-hello-indent)))
933 (center-region start (point)))))
938 (defun notmuch-hello (&optional no-display)
939 "Run notmuch and display saved searches, known tags, etc."
941 (notmuch-assert-cli-sane)
942 ;; This may cause a window configuration change, so if the
943 ;; auto-refresh hook is already installed, avoid recursive refresh.
944 (let ((notmuch-hello-auto-refresh nil))
946 (set-buffer "*notmuch-hello*")
947 (pop-to-buffer-same-window "*notmuch-hello*")))
948 ;; Install auto-refresh hook
949 (when notmuch-hello-auto-refresh
950 (add-hook 'window-configuration-change-hook
951 #'notmuch-hello-window-configuration-change))
952 (let ((target-line (line-number-at-pos))
953 (target-column (current-column))
954 (inhibit-read-only t))
955 ;; Delete all editable widget fields. Editable widget fields are
956 ;; tracked in a buffer local variable `widget-field-list' (and
957 ;; others). If we do `erase-buffer' without properly deleting the
958 ;; widgets, some widget-related functions are confused later.
959 (mapc 'widget-delete widget-field-list)
961 (unless (eq major-mode 'notmuch-hello-mode)
962 (notmuch-hello-mode))
963 (let ((all (overlay-lists)))
964 ;; Delete all the overlays.
965 (mapc 'delete-overlay (car all))
966 (mapc 'delete-overlay (cdr all)))
969 (let ((point-before (point)))
970 (if (functionp section)
972 (apply (car section) (cdr section)))
973 ;; don't insert a newline when the previous section didn't
975 (unless (eq (point) point-before)
976 (widget-insert "\n"))))
977 notmuch-hello-sections)
979 ;; Move point back to where it was before refresh. Use line and
980 ;; column instead of point directly to be insensitive to additions
981 ;; and removals of text within earlier lines.
982 (goto-char (point-min))
983 (forward-line (1- target-line))
984 (move-to-column target-column))
985 (run-hooks 'notmuch-hello-refresh-hook)
986 (setq notmuch-hello-first-run nil))
990 (provide 'notmuch-hello)
992 ;;; notmuch-hello.el ends here