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
33 (declare-function notmuch-poll "notmuch-lib" ())
34 (declare-function notmuch-tree "notmuch-tree"
35 (&optional query query-context target buffer-name
36 open-target unthreaded parent-buffer oldest-first))
37 (declare-function notmuch-unthreaded "notmuch-tree"
38 (&optional query query-context target buffer-name
44 (defun notmuch-saved-search-get (saved-search field)
45 "Get FIELD from SAVED-SEARCH.
47 If SAVED-SEARCH is a plist, this is just `plist-get', but for
48 backwards compatibility, this also deals with the two other
49 possible formats for SAVED-SEARCH: cons cells (NAME . QUERY) and
50 lists (NAME QUERY COUNT-QUERY)."
52 ((keywordp (car saved-search))
53 (plist-get saved-search field))
54 ;; It is not a plist so it is an old-style entry.
55 ((consp (cdr saved-search))
56 (pcase-let ((`(,name ,query ,count-query) saved-search))
60 (:count-query count-query)
63 (pcase-let ((`(,name . ,query) saved-search))
69 (defun notmuch-hello-saved-search-to-plist (saved-search)
70 "Return a copy of SAVED-SEARCH in plist form.
72 If saved search is a plist then just return a copy. In other
73 cases, for backwards compatibility, convert to plist form and
75 (if (keywordp (car saved-search))
76 (copy-sequence saved-search)
77 (let ((fields (list :name :query :count-query))
79 (dolist (field fields plist-search)
80 (let ((string (notmuch-saved-search-get saved-search field)))
82 (setq plist-search (append plist-search (list field string)))))))))
84 (defun notmuch-hello--saved-searches-to-plist (symbol)
85 "Extract a saved-search variable into plist form.
87 The new style saved search is just a plist, but for backwards
88 compatibility we use this function to extract old style saved
89 searches so they still work in customize."
90 (let ((saved-searches (default-value symbol)))
91 (mapcar #'notmuch-hello-saved-search-to-plist saved-searches)))
93 (define-widget 'notmuch-saved-search-plist 'list
94 "A single saved search property list."
96 :args '((list :inline t
98 (group :format "%v" :inline t
99 (const :format " Name: " :name)
100 (string :format "%v"))
101 (group :format "%v" :inline t
102 (const :format " Query: " :query)
103 (string :format "%v")))
106 (group :format "%v" :inline t
107 (const :format "Shortcut key: " :key)
108 (key-sequence :format "%v"))
109 (group :format "%v" :inline t
110 (const :format "Count-Query: " :count-query)
111 (string :format "%v"))
112 (group :format "%v" :inline t
113 (const :format "" :sort-order)
114 (choice :tag " Sort Order"
115 (const :tag "Default" nil)
116 (const :tag "Oldest-first" oldest-first)
117 (const :tag "Newest-first" newest-first)))
118 (group :format "%v" :inline t
119 (const :format "" :search-type)
120 (choice :tag " Search Type"
121 (const :tag "Search mode" nil)
122 (const :tag "Tree mode" tree)
123 (const :tag "Unthreaded mode" unthreaded))))))
125 (defcustom notmuch-saved-searches
126 `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
127 (:name "unread" :query "tag:unread" :key ,(kbd "u"))
128 (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
129 (:name "sent" :query "tag:sent" :key ,(kbd "t"))
130 (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
131 (:name "all mail" :query "*" :key ,(kbd "a")))
132 "A list of saved searches to display.
134 The saved search can be given in 3 forms. The preferred way is as
135 a plist. Supported properties are
137 :name Name of the search (required).
138 :query Search to run (required).
139 :key Optional shortcut key for `notmuch-jump-search'.
140 :count-query Optional extra query to generate the count
141 shown. If not present then the :query property
143 :sort-order Specify the sort order to be used for the search.
144 Possible values are `oldest-first', `newest-first'
145 or nil. Nil means use the default sort order.
146 :search-type Specify whether to run the search in search-mode,
147 tree mode or unthreaded mode. Set to `tree' to
148 specify tree mode, 'unthreaded to specify
149 unthreaded mode, and set to nil (or anything
150 except tree and unthreaded) to specify search
153 Other accepted forms are a cons cell of the form (NAME . QUERY)
154 or a list of the form (NAME QUERY COUNT-QUERY)."
155 ;; The saved-search format is also used by the all-tags notmuch-hello
156 ;; section. This section generates its own saved-search list in one of
157 ;; the latter two forms.
158 :get 'notmuch-hello--saved-searches-to-plist
159 :type '(repeat notmuch-saved-search-plist)
160 :tag "List of Saved Searches"
161 :group 'notmuch-hello)
163 (defcustom notmuch-hello-recent-searches-max 10
164 "The number of recent searches to display."
166 :group 'notmuch-hello)
168 (defcustom notmuch-show-empty-saved-searches nil
169 "Should saved searches with no messages be listed?"
171 :group 'notmuch-hello)
173 (defun notmuch-sort-saved-searches (saved-searches)
174 "Generate an alphabetically sorted saved searches list."
175 (sort (copy-sequence saved-searches)
177 (string< (notmuch-saved-search-get a :name)
178 (notmuch-saved-search-get b :name)))))
180 (defcustom notmuch-saved-search-sort-function nil
181 "Function used to sort the saved searches for the notmuch-hello view.
183 This variable controls how saved searches should be sorted. No
184 sorting (nil) displays the saved searches in the order they are
185 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
186 saved searches in alphabetical order. Custom sort function should
187 be a function or a lambda expression that takes the saved
188 searches list as a parameter, and returns a new saved searches
189 list to be used. For compatibility with the various saved-search
190 formats it should use notmuch-saved-search-get to access the
191 fields of the search."
192 :type '(choice (const :tag "No sorting" nil)
193 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
194 (function :tag "Custom sort function"
195 :value notmuch-sort-saved-searches))
196 :group 'notmuch-hello)
198 (defvar notmuch-hello-indent 4
199 "How much to indent non-headers.")
201 (defimage notmuch-hello-logo ((:type svg :file "notmuch-logo.svg")))
203 (defcustom notmuch-show-logo t
204 "Should the notmuch logo be shown?"
206 :group 'notmuch-hello)
208 (defcustom notmuch-show-all-tags-list nil
209 "Should all tags be shown in the notmuch-hello view?"
211 :group 'notmuch-hello)
213 (defcustom notmuch-hello-tag-list-make-query nil
214 "Function or string to generate queries for the all tags list.
216 This variable controls which query results are shown for each tag
217 in the \"all tags\" list. If nil, it will use all messages with
218 that tag. If this is set to a string, it is used as a filter for
219 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
220 Finally this can be a function that will be called for each tag and
221 should return a filter for that tag, or nil to hide the tag."
222 :type '(choice (const :tag "All messages" nil)
223 (const :tag "Unread messages" "tag:unread")
224 (string :tag "Custom filter"
226 (function :tag "Custom filter function"))
227 :group 'notmuch-hello)
229 (defcustom notmuch-hello-hide-tags nil
230 "List of tags to be hidden in the \"all tags\"-section."
231 :type '(repeat string)
232 :group 'notmuch-hello)
234 (defface notmuch-hello-logo-background
237 (:background "#5f5f5f"))
240 (:background "white")))
241 "Background colour for the notmuch logo."
242 :group 'notmuch-hello
243 :group 'notmuch-faces)
245 (defcustom notmuch-column-control t
246 "Controls the number of columns for saved searches/tags in notmuch view.
248 This variable has three potential sets of values:
250 - t: automatically calculate the number of columns possible based
251 on the tags to be shown and the window width,
252 - an integer: a lower bound on the number of characters that will
253 be used to display each column,
254 - a float: a fraction of the window width that is the lower bound
255 on the number of characters that should be used for each
259 - if you would like two columns of tags, set this to 0.5.
260 - if you would like a single column of tags, set this to 1.0.
261 - if you would like tags to be 30 characters wide, set this to
263 - if you don't want to worry about all of this nonsense, leave
266 (const :tag "Automatically calculated" t)
267 (integer :tag "Number of characters")
268 (float :tag "Fraction of window"))
269 :group 'notmuch-hello)
271 (defcustom notmuch-hello-thousands-separator " "
272 "The string used as a thousands separator.
274 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
275 The latter is recommended in the SI/ISO 31-0 standard and by the
276 International Bureau of Weights and Measures."
278 :group 'notmuch-hello)
280 (defcustom notmuch-hello-mode-hook nil
281 "Functions called after entering `notmuch-hello-mode'."
283 :group 'notmuch-hello
284 :group 'notmuch-hooks)
286 (defcustom notmuch-hello-refresh-hook nil
287 "Functions called after updating a `notmuch-hello' buffer."
289 :group 'notmuch-hello
290 :group 'notmuch-hooks)
292 (defconst notmuch-hello-url "https://notmuchmail.org"
293 "The `notmuch' web site.")
295 (defvar notmuch-hello-custom-section-options
296 '((:filter (string :tag "Filter for each tag"))
297 (:filter-count (string :tag "Different filter to generate message counts"))
298 (:initially-hidden (const :tag "Hide this section on startup" t))
299 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
300 (:hide-if-empty (const :tag "Hide this section if all queries are empty
301 \(and not shown by show-empty-searches)" t)))
302 "Various customization-options for notmuch-hello-tags/query-section.")
304 (define-widget 'notmuch-hello-tags-section 'lazy
305 "Customize-type for notmuch-hello tag-list sections."
306 :tag "Customized tag-list section (see docstring for details)"
309 (const :tag "" notmuch-hello-insert-tags-section)
310 (string :tag "Title for this section")
314 ,(append notmuch-hello-custom-section-options
315 '((:hide-tags (repeat :tag "Tags that will be hidden"
318 (define-widget 'notmuch-hello-query-section 'lazy
319 "Customize-type for custom saved-search-like sections"
320 :tag "Customized queries section (see docstring for details)"
323 (const :tag "" notmuch-hello-insert-searches)
324 (string :tag "Title for this section")
325 (repeat :tag "Queries"
326 (cons (string :tag "Name") (string :tag "Query")))
327 (plist :inline t :options ,notmuch-hello-custom-section-options)))
329 (defcustom notmuch-hello-sections
330 (list #'notmuch-hello-insert-header
331 #'notmuch-hello-insert-saved-searches
332 #'notmuch-hello-insert-search
333 #'notmuch-hello-insert-recent-searches
334 #'notmuch-hello-insert-alltags
335 #'notmuch-hello-insert-footer)
336 "Sections for notmuch-hello.
338 The list contains functions which are used to construct sections in
339 notmuch-hello buffer. When notmuch-hello buffer is constructed,
340 these functions are run in the order they appear in this list. Each
341 function produces a section simply by adding content to the current
342 buffer. A section should not end with an empty line, because a
343 newline will be inserted after each section by `notmuch-hello'.
345 Each function should take no arguments. The return value is
348 For convenience an element can also be a list of the form (FUNC ARG1
349 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
352 A \"Customized tag-list section\" item in the customize-interface
353 displays a list of all tags, optionally hiding some of them. It
354 is also possible to filter the list of messages matching each tag
355 by an additional filter query. Similarly, the count of messages
356 displayed next to the buttons can be generated by applying a
357 different filter to the tag query. These filters are also
358 supported for \"Customized queries section\" items."
359 :group 'notmuch-hello
362 (choice (function-item notmuch-hello-insert-header)
363 (function-item notmuch-hello-insert-saved-searches)
364 (function-item notmuch-hello-insert-search)
365 (function-item notmuch-hello-insert-recent-searches)
366 (function-item notmuch-hello-insert-alltags)
367 (function-item notmuch-hello-insert-footer)
368 (function-item notmuch-hello-insert-inbox)
369 notmuch-hello-tags-section
370 notmuch-hello-query-section
371 (function :tag "Custom section"))))
373 (defcustom notmuch-hello-auto-refresh t
374 "Automatically refresh when returning to the notmuch-hello buffer."
375 :group 'notmuch-hello
378 ;;; Internal variables
380 (defvar notmuch-hello-hidden-sections nil
381 "List of sections titles whose contents are hidden.")
383 (defvar notmuch-hello-first-run t
384 "True if `notmuch-hello' is run for the first time, set to nil afterwards.")
386 ;;; Widgets for inserters
388 (define-widget 'notmuch-search-item 'item
391 :value-create 'notmuch-search-item-value-create)
393 (defun notmuch-search-item-value-create (widget)
394 (let ((value (widget-get widget :value)))
395 (widget-insert (make-string notmuch-hello-indent ?\s))
396 (widget-create 'editable-field
397 :size (widget-get widget :size)
399 :action #'notmuch-hello-search
402 (widget-create 'push-button
404 :notify #'notmuch-hello-add-saved-search
407 (widget-create 'push-button
409 :notify #'notmuch-hello-delete-search-from-history
412 (defun notmuch-search-item-field-width ()
413 (max 8 ; Don't let the search boxes be less than 8 characters wide.
415 notmuch-hello-indent ; space at bol
416 notmuch-hello-indent ; space at eol
417 1 ; for the space before the [save] button
418 6 ; for the [save] button
419 1 ; for the space before the [del] button
420 5))) ; for the [del] button
424 (defun notmuch-hello-search (widget &rest _event)
425 (let ((search (widget-value widget)))
427 (setq search (string-trim search))
428 (let ((history-delete-duplicates t))
429 (add-to-history 'notmuch-search-history search)))
430 (notmuch-search search notmuch-search-oldest-first)))
432 (defun notmuch-hello-add-saved-search (widget &rest _event)
433 (let ((search (widget-value (widget-get widget :parent)))
434 (name (completing-read "Name for saved search: "
435 notmuch-saved-searches)))
436 ;; If an existing saved search with this name exists, remove it.
437 (setq notmuch-saved-searches
438 (cl-loop for elem in notmuch-saved-searches
439 unless (equal name (notmuch-saved-search-get elem :name))
442 (customize-save-variable 'notmuch-saved-searches
443 (add-to-list 'notmuch-saved-searches
444 (list :name name :query search) t))
445 (message "Saved '%s' as '%s'." search name)
446 (notmuch-hello-update)))
448 (defun notmuch-hello-delete-search-from-history (widget &rest _event)
449 (when (y-or-n-p "Are you sure you want to delete this search? ")
450 (let ((search (widget-value (widget-get widget :parent))))
451 (setq notmuch-search-history
452 (delete search notmuch-search-history)))
453 (notmuch-hello-update)))
457 ;; `notmuch-hello-query-counts', `notmuch-hello-nice-number' and
458 ;; `notmuch-hello-insert-buttons' are used outside this section.
459 ;; All other functions that are defined in this section are only
460 ;; used by these two functions.
462 (defun notmuch-hello-longest-label (searches-alist)
463 (or (cl-loop for elem in searches-alist
464 maximize (length (notmuch-saved-search-get elem :name)))
467 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
468 (let ((len (length list)))
469 (cl-loop for col from 0 to (- ncols 1)
470 collect (let ((offset (+ (* nrows col) row)))
473 ;; Don't forget to insert an empty slot in the
474 ;; output matrix if there is no corresponding
475 ;; value in the input matrix.
478 (defun notmuch-hello-reflect (list ncols)
479 "Reflect a `ncols' wide matrix represented by `list' along the
482 (let ((nrows (ceiling (length list) ncols)))
483 (cl-loop for row from 0 to (- nrows 1)
484 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
486 (defun notmuch-hello-widget-search (widget &rest _ignore)
487 (cl-case (widget-get widget :notmuch-search-type)
489 (let ((n (notmuch-search-format-buffer-name (widget-value widget) "tree" t)))
490 (notmuch-tree (widget-get widget :notmuch-search-terms)
491 nil nil n nil nil nil
492 (widget-get widget :notmuch-search-oldest-first))))
494 (let ((n (notmuch-search-format-buffer-name (widget-value widget)
496 (notmuch-unthreaded (widget-get widget :notmuch-search-terms) nil nil n)))
498 (notmuch-search (widget-get widget :notmuch-search-terms)
499 (widget-get widget :notmuch-search-oldest-first)))))
501 (defun notmuch-saved-search-count (search)
502 (car (notmuch--process-lines notmuch-command "count" search)))
504 (defun notmuch-hello-tags-per-line (widest)
505 "Determine how many tags to show per line and how wide they
506 should be. Returns a cons cell `(tags-per-line width)'."
509 ((integerp notmuch-column-control)
511 (/ (- (window-width) notmuch-hello-indent)
512 ;; Count is 9 wide (8 digits plus space), 1 for the space
514 (+ 9 1 (max notmuch-column-control widest)))))
515 ((floatp notmuch-column-control)
516 (let* ((available-width (- (window-width) notmuch-hello-indent))
517 (proposed-width (max (* available-width notmuch-column-control)
519 (floor available-width proposed-width)))
522 (/ (- (window-width) notmuch-hello-indent)
523 ;; Count is 9 wide (8 digits plus space), 1 for the space
526 (cons tags-per-line (/ (max 1
527 (- (window-width) notmuch-hello-indent
528 ;; Count is 9 wide (8 digits plus
529 ;; space), 1 for the space after the
531 (* tags-per-line (+ 9 1))))
534 (defun notmuch-hello-filtered-query (query filter)
535 "Constructs a query to search all messages matching QUERY and FILTER.
537 If FILTER is a string, it is directly used in the returned query.
539 If FILTER is a function, it is called with QUERY as a parameter and
540 the string it returns is used as the query. If nil is returned,
543 Otherwise, FILTER is ignored."
545 ((functionp filter) (funcall filter query))
547 (concat "(" query ") and (" filter ")"))
550 (defun notmuch-hello-query-counts (query-list &rest options)
551 "Compute list of counts of matched messages from QUERY-LIST.
553 QUERY-LIST must be a list of saved-searches. Ideally each of
554 these is a plist but other options are available for backwards
555 compatibility: see `notmuch-saved-searches' for details.
557 The result is a list of plists each of which includes the
558 properties :name NAME, :query QUERY and :count COUNT, together
559 with any properties in the original saved-search.
561 The values :show-empty-searches, :filter and :filter-count from
562 options will be handled as specified for
563 `notmuch-hello-insert-searches'. :disable-includes can be used to
564 turn off the default exclude processing in `notmuch-count(1)'"
566 (dolist (elem query-list nil)
567 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
568 (notmuch-saved-search-get elem :query))))
570 (replace-regexp-in-string
572 (notmuch-hello-filtered-query count-query
573 (or (plist-get options :filter-count)
574 (plist-get options :filter))))
576 (unless (= (notmuch--call-process-region (point-min) (point-max) notmuch-command
578 (if (plist-get options :disable-excludes)
582 (notmuch-logged-error
583 "notmuch count --batch failed"
584 "Please check that the notmuch CLI is new enough to support `count
585 --batch'. In general we recommend running matching versions of
586 the CLI and emacs interface."))
587 (goto-char (point-min))
590 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
591 (search-query (plist-get elem-plist :query))
592 (filtered-query (notmuch-hello-filtered-query
593 search-query (plist-get options :filter)))
594 (message-count (prog1 (read (current-buffer))
596 (when (and filtered-query (or (plist-get options :show-empty-searches)
597 (> message-count 0)))
598 (setq elem-plist (plist-put elem-plist :query filtered-query))
599 (list (plist-put elem-plist :count message-count)))))
602 (defun notmuch-hello-nice-number (n)
605 (push (% n 1000) result)
607 (setq result (or result '(0)))
609 (number-to-string (car result))
610 (mapcar (lambda (elem)
611 (format "%s%03d" notmuch-hello-thousands-separator elem))
614 (defun notmuch-hello-insert-buttons (searches)
615 "Insert buttons for SEARCHES.
617 SEARCHES must be a list of plists each of which should contain at
618 least the properties :name NAME :query QUERY and :count COUNT,
619 where QUERY is the query to start when the button for the
620 corresponding entry is activated, and COUNT should be the number
621 of messages matching the query. Such a plist can be computed
622 with `notmuch-hello-query-counts'."
623 (let* ((widest (notmuch-hello-longest-label searches))
624 (tags-and-width (notmuch-hello-tags-per-line widest))
625 (tags-per-line (car tags-and-width))
626 (column-width (cdr tags-and-width))
629 (reordered-list (notmuch-hello-reflect searches tags-per-line))
630 ;; Hack the display of the buttons used.
631 (widget-push-button-prefix "")
632 (widget-push-button-suffix ""))
633 ;; dme: It feels as though there should be a better way to
634 ;; implement this loop than using an incrementing counter.
636 ;; (not elem) indicates an empty slot in the matrix.
638 (when (> column-indent 0)
639 (widget-insert (make-string column-indent ? )))
640 (let* ((name (plist-get elem :name))
641 (query (plist-get elem :query))
642 (oldest-first (cl-case (plist-get elem :sort-order)
645 (otherwise notmuch-search-oldest-first)))
646 (search-type (plist-get elem :search-type))
647 (msg-count (plist-get elem :count)))
648 (widget-insert (format "%8s "
649 (notmuch-hello-nice-number msg-count)))
650 (widget-create 'push-button
651 :notify #'notmuch-hello-widget-search
652 :notmuch-search-terms query
653 :notmuch-search-oldest-first oldest-first
654 :notmuch-search-type search-type
657 (1+ (max 0 (- column-width (length name)))))))
659 (when (eq (% count tags-per-line) 0)
660 (setq column-indent 0)
661 (widget-insert "\n")))
663 ;; If the last line was not full (and hence did not include a
664 ;; carriage return), insert one now.
665 (unless (eq (% count tags-per-line) 0)
666 (widget-insert "\n"))))
670 (defun notmuch-hello-update ()
671 "Update the notmuch-hello buffer."
672 ;; Lazy - rebuild everything.
676 (defun notmuch-hello-window-configuration-change ()
677 "Hook function to update the hello buffer when it is switched to."
678 (let ((hello-buf (get-buffer "*notmuch-hello*"))
680 ;; Consider all windows in the currently selected frame, since
681 ;; that's where the configuration change happened. This also
682 ;; refreshes our snapshot of all windows, so we have to do this
683 ;; even if we know we won't refresh (e.g., hello-buf is null).
684 (dolist (window (window-list))
685 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
686 (cur-buf (window-buffer window)))
687 (unless (eq last-buf cur-buf)
688 ;; This window changed or is new. Update recorded buffer
690 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
691 (when (and (eq cur-buf hello-buf) last-buf)
692 ;; The user just switched to hello in this window (hello
693 ;; is currently visible, was not visible on the last
694 ;; configuration change, and this is not a new window)
695 (setq do-refresh t)))))
696 (when (and do-refresh notmuch-hello-auto-refresh)
697 ;; Refresh hello as soon as we get back to redisplay. On Emacs
698 ;; 24, we can't do it right here because something in this
699 ;; hook's call stack overrides hello's point placement.
700 ;; FIXME And on Emacs releases that we still support?
701 (run-at-time nil nil #'notmuch-hello t))
704 (remove-hook 'window-configuration-change-hook
705 #'notmuch-hello-window-configuration-change))))
707 (defvar notmuch-hello-mode-map
708 ;; Inherit both widget-keymap and notmuch-common-keymap. We have
709 ;; to use make-sparse-keymap to force this to be a new keymap (so
710 ;; that when we modify map it does not modify widget-keymap).
711 (let ((map (make-composed-keymap (list (make-sparse-keymap) widget-keymap))))
712 (set-keymap-parent map notmuch-common-keymap)
713 ;; Currently notmuch-hello-mode supports free text entry, but not
714 ;; tagging operations, so provide standard undo.
715 (define-key map [remap notmuch-tag-undo] #'undo)
717 "Keymap for \"notmuch hello\" buffers.")
719 (define-derived-mode notmuch-hello-mode fundamental-mode "notmuch-hello"
720 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
722 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
723 or click on a saved search to view matching threads. Edit saved
724 searches with the `edit' button. Type `\\[notmuch-jump-search]'
725 in any Notmuch screen for quick access to saved searches that
728 Type new searches in the search box and hit RET to view matching
729 threads. Hit RET in a recent search box to re-submit a previous
730 search. Edit it first if you like. Save a recent search to saved
731 searches with the `save' button.
733 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
734 screen to search for messages and view matching threads or
735 messages, respectively. Recent searches are available in the
738 Expand the all tags view with the `show' button (and collapse
739 again with the `hide' button). Hit RET or click on a tag name to
740 view matching threads.
742 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
743 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
745 The screen may be customized via `\\[customize]'.
747 Complete list of currently available key bindings:
749 \\{notmuch-hello-mode-map}"
750 (setq notmuch-buffer-refresh-function #'notmuch-hello-update))
754 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
755 "Return an alist from tags to queries to display in the all-tags section."
756 (cl-mapcan (lambda (tag)
757 (and (not (member tag hide-tags))
760 (notmuch-escape-boolean-term tag))))))
761 (notmuch--process-lines notmuch-command "search" "--output=tags" "*")))
763 (defun notmuch-hello-insert-header ()
764 "Insert the default notmuch-hello header."
765 (when notmuch-show-logo
766 (let ((image notmuch-hello-logo))
767 ;; The notmuch logo uses transparency. That can display poorly
768 ;; when inserting the image into an emacs buffer (black logo on
769 ;; a black background), so force the background colour of the
770 ;; image. We use a face to represent the colour so that
771 ;; `defface' can be used to declare the different possible
772 ;; colours, which depend on whether the frame has a light or
774 (setq image (cons 'image
778 'notmuch-hello-logo-background)))))
779 (insert-image image))
782 (widget-insert "Welcome to ")
783 ;; Hack the display of the links used.
784 (let ((widget-link-prefix "")
785 (widget-link-suffix ""))
787 :notify (lambda (&rest _ignore)
788 (browse-url notmuch-hello-url))
789 :help-echo "Visit the notmuch website."
792 (widget-insert "You have ")
794 :notify (lambda (&rest _ignore)
795 (notmuch-hello-update))
797 (notmuch-hello-nice-number
799 (car (notmuch--process-lines notmuch-command "count" "--exclude=false")))))
800 (widget-insert " messages.\n")))
802 (defun notmuch-hello-insert-saved-searches ()
803 "Insert the saved-searches section."
804 (let ((searches (notmuch-hello-query-counts
805 (if notmuch-saved-search-sort-function
806 (funcall notmuch-saved-search-sort-function
807 notmuch-saved-searches)
808 notmuch-saved-searches)
809 :show-empty-searches notmuch-show-empty-saved-searches)))
811 (widget-insert "Saved searches: ")
812 (widget-create 'push-button
813 :notify (lambda (&rest _ignore)
814 (customize-variable 'notmuch-saved-searches))
816 (widget-insert "\n\n")
817 (let ((start (point)))
818 (notmuch-hello-insert-buttons searches)
819 (indent-rigidly start (point) notmuch-hello-indent)))))
821 (defun notmuch-hello-insert-search ()
822 "Insert a search widget."
823 (widget-insert "Search: ")
824 (widget-create 'editable-field
825 ;; Leave some space at the start and end of the
827 :size (max 8 (- (window-width) notmuch-hello-indent
828 (length "Search: ")))
829 :action #'notmuch-hello-search)
830 ;; Add an invisible dot to make `widget-end-of-line' ignore
831 ;; trailing spaces in the search widget field. A dot is used
832 ;; instead of a space to make `show-trailing-whitespace'
833 ;; happy, i.e. avoid it marking the whole line as trailing
835 (widget-insert (propertize "." 'invisible t))
836 (widget-insert "\n"))
838 (defun notmuch-hello-insert-recent-searches ()
839 "Insert recent searches."
840 (when notmuch-search-history
841 (widget-insert "Recent searches: ")
844 :notify (lambda (&rest _ignore)
845 (when (y-or-n-p "Are you sure you want to clear the searches? ")
846 (setq notmuch-search-history nil)
847 (notmuch-hello-update)))
849 (widget-insert "\n\n")
850 (let ((width (notmuch-search-item-field-width)))
851 (dolist (search (seq-take notmuch-search-history
852 notmuch-hello-recent-searches-max))
853 (widget-create 'notmuch-search-item :value search :size width)))))
855 (defun notmuch-hello-insert-searches (title query-list &rest options)
856 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
858 QUERY-LIST should ideally be a plist but for backwards
859 compatibility other forms are also accepted (see
860 `notmuch-saved-searches' for details). The plist should
861 contain keys :name and :query; if :count-query is also present
862 then it specifies an alternate query to be used to generate the
863 count for the associated search.
865 Supports the following entries in OPTIONS as a plist:
866 :initially-hidden - if non-nil, section will be hidden on startup
867 :show-empty-searches - show buttons with no matching messages
868 :hide-if-empty - hide if no buttons would be shown
869 (only makes sense without :show-empty-searches)
870 :filter - This can be a function that takes the search query as its argument and
871 returns a filter to be used in conjunction with the query for that search or nil
872 to hide the element. This can also be a string that is used as a combined with
873 each query using \"and\".
874 :filter-count - Separate filter to generate the count displayed each search. Accepts
875 the same values as :filter. If :filter and :filter-count are specified, this
876 will be used instead of :filter, not in conjunction with it."
877 (widget-insert title ": ")
878 (when (and notmuch-hello-first-run (plist-get options :initially-hidden))
879 (add-to-list 'notmuch-hello-hidden-sections title))
880 (let ((is-hidden (member title notmuch-hello-hidden-sections))
883 (widget-create 'push-button
884 :notify (lambda (&rest _ignore)
885 (setq notmuch-hello-hidden-sections
886 (delete title notmuch-hello-hidden-sections))
887 (notmuch-hello-update))
889 (widget-create 'push-button
890 :notify (lambda (&rest _ignore)
891 (add-to-list 'notmuch-hello-hidden-sections
893 (notmuch-hello-update))
897 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
898 (when (or (not (plist-get options :hide-if-empty))
901 (notmuch-hello-insert-buttons searches)
902 (indent-rigidly start (point) notmuch-hello-indent))))))
904 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
905 "Insert a section displaying all tags with message counts.
907 TITLE defaults to \"All tags\".
908 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
911 :hide-tags - List of tags that should be excluded."
912 (apply 'notmuch-hello-insert-searches
913 (or title "All tags")
914 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
917 (defun notmuch-hello-insert-inbox ()
918 "Show an entry for each saved search and inboxed messages for each tag."
919 (notmuch-hello-insert-searches "What's in your inbox"
921 notmuch-saved-searches
922 (notmuch-hello-generate-tag-alist))
923 :filter "tag:inbox"))
925 (defun notmuch-hello-insert-alltags ()
926 "Insert a section displaying all tags and associated message counts."
927 (notmuch-hello-insert-tags-section
929 :initially-hidden (not notmuch-show-all-tags-list)
930 :hide-tags notmuch-hello-hide-tags
931 :filter notmuch-hello-tag-list-make-query
932 :disable-excludes t))
934 (defun notmuch-hello-insert-footer ()
935 "Insert the notmuch-hello footer."
936 (let ((start (point)))
937 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
938 (widget-insert "Customize ")
940 :notify (lambda (&rest _ignore)
941 (customize-group 'notmuch))
942 :button-prefix "" :button-suffix ""
944 (widget-insert " or ")
946 :notify (lambda (&rest _ignore)
947 (customize-variable 'notmuch-hello-sections))
948 :button-prefix "" :button-suffix ""
950 (let ((fill-column (- (window-width) notmuch-hello-indent)))
951 (center-region start (point)))))
956 (defun notmuch-hello (&optional no-display)
957 "Run notmuch and display saved searches, known tags, etc."
959 (notmuch-assert-cli-sane)
960 ;; This may cause a window configuration change, so if the
961 ;; auto-refresh hook is already installed, avoid recursive refresh.
962 (let ((notmuch-hello-auto-refresh nil))
964 (set-buffer "*notmuch-hello*")
965 (pop-to-buffer-same-window "*notmuch-hello*")))
966 ;; Install auto-refresh hook
967 (when notmuch-hello-auto-refresh
968 (add-hook 'window-configuration-change-hook
969 #'notmuch-hello-window-configuration-change))
970 (let ((target-line (line-number-at-pos))
971 (target-column (current-column))
972 (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)
979 (unless (eq major-mode 'notmuch-hello-mode)
980 (notmuch-hello-mode))
981 (let ((all (overlay-lists)))
982 ;; Delete all the overlays.
983 (mapc 'delete-overlay (car all))
984 (mapc 'delete-overlay (cdr all)))
987 (let ((point-before (point)))
988 (if (functionp section)
990 (apply (car section) (cdr section)))
991 ;; don't insert a newline when the previous section didn't
993 (unless (eq (point) point-before)
994 (widget-insert "\n"))))
995 notmuch-hello-sections)
997 ;; Move point back to where it was before refresh. Use line and
998 ;; column instead of point directly to be insensitive to additions
999 ;; and removals of text within earlier lines.
1000 (goto-char (point-min))
1001 (forward-line (1- target-line))
1002 (move-to-column target-column))
1003 (run-hooks 'notmuch-hello-refresh-hook)
1004 (setq notmuch-hello-first-run nil))
1008 (provide 'notmuch-hello)
1010 ;;; notmuch-hello.el ends here