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 <https://www.gnu.org/licenses/>.
20 ;; Authors: David Edmondson <dme@dme.org>
26 (require 'wid-edit) ; For `widget-forward'.
28 (require 'notmuch-lib)
29 (require 'notmuch-mua)
31 (declare-function notmuch-search "notmuch"
32 (&optional query oldest-first target-thread target-line continuation))
33 (declare-function notmuch-poll "notmuch" ())
34 (declare-function notmuch-tree "notmuch-tree"
35 (&optional query query-context target buffer-name open-target unthreaded))
36 (declare-function notmuch-unthreaded
37 (&optional query query-context target buffer-name open-target))
40 (defun notmuch-saved-search-get (saved-search field)
41 "Get FIELD from SAVED-SEARCH.
43 If SAVED-SEARCH is a plist, this is just `plist-get', but for
44 backwards compatibility, this also deals with the two other
45 possible formats for SAVED-SEARCH: cons cells (NAME . QUERY) and
46 lists (NAME QUERY COUNT-QUERY)."
48 ((keywordp (car saved-search))
49 (plist-get saved-search field))
50 ;; It is not a plist so it is an old-style entry.
51 ((consp (cdr saved-search))
52 (pcase-let ((`(,name ,query ,count-query) saved-search))
56 (:count-query count-query)
59 (pcase-let ((`(,name . ,query) saved-search))
65 (defun notmuch-hello-saved-search-to-plist (saved-search)
66 "Return a copy of SAVED-SEARCH in plist form.
68 If saved search is a plist then just return a copy. In other
69 cases, for backwards compatibility, convert to plist form and
71 (if (keywordp (car saved-search))
72 (copy-sequence saved-search)
73 (let ((fields (list :name :query :count-query))
75 (dolist (field fields plist-search)
76 (let ((string (notmuch-saved-search-get saved-search field)))
78 (setq plist-search (append plist-search (list field string)))))))))
80 (defun notmuch-hello--saved-searches-to-plist (symbol)
81 "Extract a saved-search variable into plist form.
83 The new style saved search is just a plist, but for backwards
84 compatibility we use this function to extract old style saved
85 searches so they still work in customize."
86 (let ((saved-searches (default-value symbol)))
87 (mapcar #'notmuch-hello-saved-search-to-plist saved-searches)))
89 (define-widget 'notmuch-saved-search-plist 'list
90 "A single saved search property list."
92 :args '((list :inline t
94 (group :format "%v" :inline t
95 (const :format " Name: " :name)
96 (string :format "%v"))
97 (group :format "%v" :inline t
98 (const :format " Query: " :query)
99 (string :format "%v")))
102 (group :format "%v" :inline t
103 (const :format "Shortcut key: " :key)
104 (key-sequence :format "%v"))
105 (group :format "%v" :inline t
106 (const :format "Count-Query: " :count-query)
107 (string :format "%v"))
108 (group :format "%v" :inline t
109 (const :format "" :sort-order)
110 (choice :tag " Sort Order"
111 (const :tag "Default" nil)
112 (const :tag "Oldest-first" oldest-first)
113 (const :tag "Newest-first" newest-first)))
114 (group :format "%v" :inline t
115 (const :format "" :search-type)
116 (choice :tag " Search Type"
117 (const :tag "Search mode" nil)
118 (const :tag "Tree mode" tree)
119 (const :tag "Unthreaded mode" unthreaded))))))
121 (defcustom notmuch-saved-searches
122 `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
123 (:name "unread" :query "tag:unread" :key ,(kbd "u"))
124 (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
125 (:name "sent" :query "tag:sent" :key ,(kbd "t"))
126 (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
127 (:name "all mail" :query "*" :key ,(kbd "a")))
128 "A list of saved searches to display.
130 The saved search can be given in 3 forms. The preferred way is as
131 a plist. Supported properties are
133 :name Name of the search (required).
134 :query Search to run (required).
135 :key Optional shortcut key for `notmuch-jump-search'.
136 :count-query Optional extra query to generate the count
137 shown. If not present then the :query property
139 :sort-order Specify the sort order to be used for the search.
140 Possible values are `oldest-first', `newest-first'
141 or nil. Nil means use the default sort order.
142 :search-type Specify whether to run the search in search-mode,
143 tree mode or unthreaded mode. Set to 'tree to specify tree
144 mode, 'unthreaded to specify unthreaded mode, and set to nil
145 (or anything except tree and unthreaded) to specify search mode.
147 Other accepted forms are a cons cell of the form (NAME . QUERY)
148 or a list of the form (NAME QUERY COUNT-QUERY)."
149 ;; The saved-search format is also used by the all-tags notmuch-hello
150 ;; section. This section generates its own saved-search list in one of
151 ;; the latter two forms.
152 :get 'notmuch-hello--saved-searches-to-plist
153 :type '(repeat notmuch-saved-search-plist)
154 :tag "List of Saved Searches"
155 :group 'notmuch-hello)
157 (defcustom notmuch-hello-recent-searches-max 10
158 "The number of recent searches to display."
160 :group 'notmuch-hello)
162 (defcustom notmuch-show-empty-saved-searches nil
163 "Should saved searches with no messages be listed?"
165 :group 'notmuch-hello)
167 (defun notmuch-sort-saved-searches (saved-searches)
168 "Generate an alphabetically sorted saved searches list."
169 (sort (copy-sequence saved-searches)
171 (string< (notmuch-saved-search-get a :name)
172 (notmuch-saved-search-get b :name)))))
174 (defcustom notmuch-saved-search-sort-function nil
175 "Function used to sort the saved searches for the notmuch-hello view.
177 This variable controls how saved searches should be sorted. No
178 sorting (nil) displays the saved searches in the order they are
179 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
180 saved searches in alphabetical order. Custom sort function should
181 be a function or a lambda expression that takes the saved
182 searches list as a parameter, and returns a new saved searches
183 list to be used. For compatibility with the various saved-search
184 formats it should use notmuch-saved-search-get to access the
185 fields of the search."
186 :type '(choice (const :tag "No sorting" nil)
187 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
188 (function :tag "Custom sort function"
189 :value notmuch-sort-saved-searches))
190 :group 'notmuch-hello)
192 (defvar notmuch-hello-indent 4
193 "How much to indent non-headers.")
195 (defcustom notmuch-show-logo t
196 "Should the notmuch logo be shown?"
198 :group 'notmuch-hello)
200 (defcustom notmuch-show-all-tags-list nil
201 "Should all tags be shown in the notmuch-hello view?"
203 :group 'notmuch-hello)
205 (defcustom notmuch-hello-tag-list-make-query nil
206 "Function or string to generate queries for the all tags list.
208 This variable controls which query results are shown for each tag
209 in the \"all tags\" list. If nil, it will use all messages with
210 that tag. If this is set to a string, it is used as a filter for
211 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
212 Finally this can be a function that will be called for each tag and
213 should return a filter for that tag, or nil to hide the tag."
214 :type '(choice (const :tag "All messages" nil)
215 (const :tag "Unread messages" "tag:unread")
216 (string :tag "Custom filter"
218 (function :tag "Custom filter function"))
219 :group 'notmuch-hello)
221 (defcustom notmuch-hello-hide-tags nil
222 "List of tags to be hidden in the \"all tags\"-section."
223 :type '(repeat string)
224 :group 'notmuch-hello)
226 (defface notmuch-hello-logo-background
229 (:background "#5f5f5f"))
232 (:background "white")))
233 "Background colour for the notmuch logo."
234 :group 'notmuch-hello
235 :group 'notmuch-faces)
237 (defcustom notmuch-column-control t
238 "Controls the number of columns for saved searches/tags in notmuch view.
240 This variable has three potential sets of values:
242 - t: automatically calculate the number of columns possible based
243 on the tags to be shown and the window width,
244 - an integer: a lower bound on the number of characters that will
245 be used to display each column,
246 - a float: a fraction of the window width that is the lower bound
247 on the number of characters that should be used for each
251 - if you would like two columns of tags, set this to 0.5.
252 - if you would like a single column of tags, set this to 1.0.
253 - if you would like tags to be 30 characters wide, set this to
255 - if you don't want to worry about all of this nonsense, leave
258 (const :tag "Automatically calculated" t)
259 (integer :tag "Number of characters")
260 (float :tag "Fraction of window"))
261 :group 'notmuch-hello)
263 (defcustom notmuch-hello-thousands-separator " "
264 "The string used as a thousands separator.
266 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
267 The latter is recommended in the SI/ISO 31-0 standard and by the
268 International Bureau of Weights and Measures."
270 :group 'notmuch-hello)
272 (defcustom notmuch-hello-mode-hook nil
273 "Functions called after entering `notmuch-hello-mode'."
275 :group 'notmuch-hello
276 :group 'notmuch-hooks)
278 (defcustom notmuch-hello-refresh-hook nil
279 "Functions called after updating a `notmuch-hello' buffer."
281 :group 'notmuch-hello
282 :group 'notmuch-hooks)
284 (defconst notmuch-hello-url "https://notmuchmail.org"
285 "The `notmuch' web site.")
287 (defvar notmuch-hello-custom-section-options
288 '((:filter (string :tag "Filter for each tag"))
289 (:filter-count (string :tag "Different filter to generate message counts"))
290 (:initially-hidden (const :tag "Hide this section on startup" t))
291 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
292 (:hide-if-empty (const :tag "Hide this section if all queries are empty
293 \(and not shown by show-empty-searches)" t)))
294 "Various customization-options for notmuch-hello-tags/query-section.")
296 (define-widget 'notmuch-hello-tags-section 'lazy
297 "Customize-type for notmuch-hello tag-list sections."
298 :tag "Customized tag-list section (see docstring for details)"
301 (const :tag "" notmuch-hello-insert-tags-section)
302 (string :tag "Title for this section")
306 ,(append notmuch-hello-custom-section-options
307 '((:hide-tags (repeat :tag "Tags that will be hidden"
310 (define-widget 'notmuch-hello-query-section 'lazy
311 "Customize-type for custom saved-search-like sections"
312 :tag "Customized queries section (see docstring for details)"
315 (const :tag "" notmuch-hello-insert-searches)
316 (string :tag "Title for this section")
317 (repeat :tag "Queries"
318 (cons (string :tag "Name") (string :tag "Query")))
319 (plist :inline t :options ,notmuch-hello-custom-section-options)))
321 (defcustom notmuch-hello-sections
322 (list #'notmuch-hello-insert-header
323 #'notmuch-hello-insert-saved-searches
324 #'notmuch-hello-insert-search
325 #'notmuch-hello-insert-recent-searches
326 #'notmuch-hello-insert-alltags
327 #'notmuch-hello-insert-footer)
328 "Sections for notmuch-hello.
330 The list contains functions which are used to construct sections in
331 notmuch-hello buffer. When notmuch-hello buffer is constructed,
332 these functions are run in the order they appear in this list. Each
333 function produces a section simply by adding content to the current
334 buffer. A section should not end with an empty line, because a
335 newline will be inserted after each section by `notmuch-hello'.
337 Each function should take no arguments. The return value is
340 For convenience an element can also be a list of the form (FUNC ARG1
341 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
344 A \"Customized tag-list section\" item in the customize-interface
345 displays a list of all tags, optionally hiding some of them. It
346 is also possible to filter the list of messages matching each tag
347 by an additional filter query. Similarly, the count of messages
348 displayed next to the buttons can be generated by applying a
349 different filter to the tag query. These filters are also
350 supported for \"Customized queries section\" items."
351 :group 'notmuch-hello
354 (choice (function-item notmuch-hello-insert-header)
355 (function-item notmuch-hello-insert-saved-searches)
356 (function-item notmuch-hello-insert-search)
357 (function-item notmuch-hello-insert-recent-searches)
358 (function-item notmuch-hello-insert-alltags)
359 (function-item notmuch-hello-insert-footer)
360 (function-item notmuch-hello-insert-inbox)
361 notmuch-hello-tags-section
362 notmuch-hello-query-section
363 (function :tag "Custom section"))))
365 (defcustom notmuch-hello-auto-refresh t
366 "Automatically refresh when returning to the notmuch-hello buffer."
367 :group 'notmuch-hello
370 (defvar notmuch-hello-hidden-sections nil
371 "List of sections titles whose contents are hidden.")
373 (defvar notmuch-hello-first-run t
374 "True if `notmuch-hello' is run for the first time, set to nil afterwards.")
376 (defun notmuch-hello-nice-number (n)
379 (push (% n 1000) result)
381 (setq result (or result '(0)))
383 (number-to-string (car result))
384 (mapcar (lambda (elem)
385 (format "%s%03d" notmuch-hello-thousands-separator elem))
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
422 (defun notmuch-hello-search (widget &rest _event)
423 (let ((search (widget-value widget)))
425 (setq search (string-trim search))
426 (let ((history-delete-duplicates t))
427 (add-to-history 'notmuch-search-history search)))
428 (notmuch-search search notmuch-search-oldest-first)))
430 (defun notmuch-hello-add-saved-search (widget &rest _event)
431 (let ((search (widget-value (widget-get widget :parent)))
432 (name (completing-read "Name for saved search: "
433 notmuch-saved-searches)))
434 ;; If an existing saved search with this name exists, remove it.
435 (setq notmuch-saved-searches
436 (cl-loop for elem in notmuch-saved-searches
438 (notmuch-saved-search-get elem :name)))
441 (customize-save-variable 'notmuch-saved-searches
442 (add-to-list 'notmuch-saved-searches
443 (list :name name :query search) t))
444 (message "Saved '%s' as '%s'." search name)
445 (notmuch-hello-update)))
447 (defun notmuch-hello-delete-search-from-history (widget &rest _event)
448 (when (y-or-n-p "Are you sure you want to delete this search? ")
449 (let ((search (widget-value (widget-get widget :parent))))
450 (setq notmuch-search-history
451 (delete search notmuch-search-history)))
452 (notmuch-hello-update)))
454 (defun notmuch-hello-longest-label (searches-alist)
455 (or (cl-loop for elem in searches-alist
456 maximize (length (notmuch-saved-search-get elem :name)))
459 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
460 (let ((len (length list)))
461 (cl-loop for col from 0 to (- ncols 1)
462 collect (let ((offset (+ (* nrows col) row)))
465 ;; Don't forget to insert an empty slot in the
466 ;; output matrix if there is no corresponding
467 ;; value in the input matrix.
470 (defun notmuch-hello-reflect (list ncols)
471 "Reflect a `ncols' wide matrix represented by `list' along the
474 (let ((nrows (ceiling (length list) ncols)))
475 (cl-loop for row from 0 to (- nrows 1)
476 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
478 (defun notmuch-hello-widget-search (widget &rest ignore)
480 ((eq (widget-get widget :notmuch-search-type) 'tree)
481 (notmuch-tree (widget-get widget
482 :notmuch-search-terms)))
483 ((eq (widget-get widget :notmuch-search-type) 'unthreaded)
484 (notmuch-unthreaded (widget-get widget
485 :notmuch-search-terms)))
487 (notmuch-search (widget-get widget
488 :notmuch-search-terms)
490 :notmuch-search-oldest-first)))))
492 (defun notmuch-saved-search-count (search)
493 (car (process-lines notmuch-command "count" search)))
495 (defun notmuch-hello-tags-per-line (widest)
496 "Determine how many tags to show per line and how wide they
497 should be. Returns a cons cell `(tags-per-line width)'."
500 ((integerp notmuch-column-control)
502 (/ (- (window-width) notmuch-hello-indent)
503 ;; Count is 9 wide (8 digits plus space), 1 for the space
505 (+ 9 1 (max notmuch-column-control widest)))))
506 ((floatp notmuch-column-control)
507 (let* ((available-width (- (window-width) notmuch-hello-indent))
508 (proposed-width (max (* available-width notmuch-column-control)
510 (floor available-width proposed-width)))
513 (/ (- (window-width) notmuch-hello-indent)
514 ;; Count is 9 wide (8 digits plus space), 1 for the space
517 (cons tags-per-line (/ (max 1
518 (- (window-width) notmuch-hello-indent
519 ;; Count is 9 wide (8 digits plus
520 ;; space), 1 for the space after the
522 (* tags-per-line (+ 9 1))))
525 (defun notmuch-hello-filtered-query (query filter)
526 "Constructs a query to search all messages matching QUERY and FILTER.
528 If FILTER is a string, it is directly used in the returned query.
530 If FILTER is a function, it is called with QUERY as a parameter and
531 the string it returns is used as the query. If nil is returned,
534 Otherwise, FILTER is ignored."
536 ((functionp filter) (funcall filter query))
538 (concat "(" query ") and (" filter ")"))
541 (defun notmuch-hello-query-counts (query-list &rest options)
542 "Compute list of counts of matched messages from QUERY-LIST.
544 QUERY-LIST must be a list of saved-searches. Ideally each of
545 these is a plist but other options are available for backwards
546 compatibility: see `notmuch-saved-searches' for details.
548 The result is a list of plists each of which includes the
549 properties :name NAME, :query QUERY and :count COUNT, together
550 with any properties in the original saved-search.
552 The values :show-empty-searches, :filter and :filter-count from
553 options will be handled as specified for
554 `notmuch-hello-insert-searches'."
556 (dolist (elem query-list nil)
557 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
558 (notmuch-saved-search-get elem :query))))
560 (replace-regexp-in-string
562 (notmuch-hello-filtered-query count-query
563 (or (plist-get options :filter-count)
564 (plist-get options :filter))))
566 (unless (= (call-process-region (point-min) (point-max) notmuch-command
567 t t nil "count" "--batch") 0)
568 (notmuch-logged-error
569 "notmuch count --batch failed"
570 "Please check that the notmuch CLI is new enough to support `count
571 --batch'. In general we recommend running matching versions of
572 the CLI and emacs interface."))
573 (goto-char (point-min))
576 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
577 (search-query (plist-get elem-plist :query))
578 (filtered-query (notmuch-hello-filtered-query
579 search-query (plist-get options :filter)))
580 (message-count (prog1 (read (current-buffer))
582 (when (and filtered-query (or (plist-get options :show-empty-searches)
583 (> message-count 0)))
584 (setq elem-plist (plist-put elem-plist :query filtered-query))
585 (list (plist-put elem-plist :count message-count)))))
588 (defun notmuch-hello-insert-buttons (searches)
589 "Insert buttons for SEARCHES.
591 SEARCHES must be a list of plists each of which should contain at
592 least the properties :name NAME :query QUERY and :count COUNT,
593 where QUERY is the query to start when the button for the
594 corresponding entry is activated, and COUNT should be the number
595 of messages matching the query. Such a plist can be computed
596 with `notmuch-hello-query-counts'."
597 (let* ((widest (notmuch-hello-longest-label searches))
598 (tags-and-width (notmuch-hello-tags-per-line widest))
599 (tags-per-line (car tags-and-width))
600 (column-width (cdr tags-and-width))
603 (reordered-list (notmuch-hello-reflect searches tags-per-line))
604 ;; Hack the display of the buttons used.
605 (widget-push-button-prefix "")
606 (widget-push-button-suffix ""))
607 ;; dme: It feels as though there should be a better way to
608 ;; implement this loop than using an incrementing counter.
610 ;; (not elem) indicates an empty slot in the matrix.
612 (when (> column-indent 0)
613 (widget-insert (make-string column-indent ? )))
614 (let* ((name (plist-get elem :name))
615 (query (plist-get elem :query))
616 (oldest-first (cl-case (plist-get elem :sort-order)
619 (otherwise notmuch-search-oldest-first)))
620 (search-type (plist-get elem :search-type))
621 (msg-count (plist-get elem :count)))
622 (widget-insert (format "%8s "
623 (notmuch-hello-nice-number msg-count)))
624 (widget-create 'push-button
625 :notify #'notmuch-hello-widget-search
626 :notmuch-search-terms query
627 :notmuch-search-oldest-first oldest-first
628 :notmuch-search-type search-type
631 (1+ (max 0 (- column-width (length name)))))))
633 (when (eq (% count tags-per-line) 0)
634 (setq column-indent 0)
635 (widget-insert "\n")))
637 ;; If the last line was not full (and hence did not include a
638 ;; carriage return), insert one now.
639 (unless (eq (% count tags-per-line) 0)
640 (widget-insert "\n"))))
642 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
644 (defun notmuch-hello-update ()
645 "Update the notmuch-hello buffer."
646 ;; Lazy - rebuild everything.
650 (defun notmuch-hello-window-configuration-change ()
651 "Hook function to update the hello buffer when it is switched to."
652 (let ((hello-buf (get-buffer "*notmuch-hello*"))
654 ;; Consider all windows in the currently selected frame, since
655 ;; that's where the configuration change happened. This also
656 ;; refreshes our snapshot of all windows, so we have to do this
657 ;; even if we know we won't refresh (e.g., hello-buf is null).
658 (dolist (window (window-list))
659 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
660 (cur-buf (window-buffer window)))
661 (unless (eq last-buf cur-buf)
662 ;; This window changed or is new. Update recorded buffer
664 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
665 (when (and (eq cur-buf hello-buf) last-buf)
666 ;; The user just switched to hello in this window (hello
667 ;; is currently visible, was not visible on the last
668 ;; configuration change, and this is not a new window)
669 (setq do-refresh t)))))
670 (when (and do-refresh notmuch-hello-auto-refresh)
671 ;; Refresh hello as soon as we get back to redisplay. On Emacs
672 ;; 24, we can't do it right here because something in this
673 ;; hook's call stack overrides hello's point placement.
674 ;; FIXME And on Emacs releases that we still support?
675 (run-at-time nil nil #'notmuch-hello t))
678 (remove-hook 'window-configuration-change-hook
679 #'notmuch-hello-window-configuration-change))))
681 (defvar notmuch-hello-mode-map
682 ;; Inherit both widget-keymap and notmuch-common-keymap. We have
683 ;; to use make-sparse-keymap to force this to be a new keymap (so
684 ;; that when we modify map it does not modify widget-keymap).
685 (let ((map (make-composed-keymap (list (make-sparse-keymap) widget-keymap))))
686 (set-keymap-parent map notmuch-common-keymap)
687 (define-key map (kbd "<C-tab>") 'widget-backward)
689 "Keymap for \"notmuch hello\" buffers.")
691 (define-derived-mode notmuch-hello-mode fundamental-mode "notmuch-hello"
692 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
694 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
695 or click on a saved search to view matching threads. Edit saved
696 searches with the `edit' button. Type `\\[notmuch-jump-search]'
697 in any Notmuch screen for quick access to saved searches that
700 Type new searches in the search box and hit RET to view matching
701 threads. Hit RET in a recent search box to re-submit a previous
702 search. Edit it first if you like. Save a recent search to saved
703 searches with the `save' button.
705 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
706 screen to search for messages and view matching threads or
707 messages, respectively. Recent searches are available in the
710 Expand the all tags view with the `show' button (and collapse
711 again with the `hide' button). Hit RET or click on a tag name to
712 view matching threads.
714 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
715 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
717 The screen may be customized via `\\[customize]'.
719 Complete list of currently available key bindings:
721 \\{notmuch-hello-mode-map}"
722 (setq notmuch-buffer-refresh-function #'notmuch-hello-update)
723 ;;(setq buffer-read-only t)
726 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
727 "Return an alist from tags to queries to display in the all-tags section."
728 (cl-mapcan (lambda (tag)
729 (and (not (member tag hide-tags))
732 (notmuch-escape-boolean-term tag))))))
733 (process-lines notmuch-command "search" "--output=tags" "*")))
735 (defun notmuch-hello-insert-header ()
736 "Insert the default notmuch-hello header."
737 (when notmuch-show-logo
738 (let ((image notmuch-hello-logo))
739 ;; The notmuch logo uses transparency. That can display poorly
740 ;; when inserting the image into an emacs buffer (black logo on
741 ;; a black background), so force the background colour of the
742 ;; image. We use a face to represent the colour so that
743 ;; `defface' can be used to declare the different possible
744 ;; colours, which depend on whether the frame has a light or
746 (setq image (cons 'image
750 'notmuch-hello-logo-background)))))
751 (insert-image image))
754 (widget-insert "Welcome to ")
755 ;; Hack the display of the links used.
756 (let ((widget-link-prefix "")
757 (widget-link-suffix ""))
759 :notify (lambda (&rest ignore)
760 (browse-url notmuch-hello-url))
761 :help-echo "Visit the notmuch website."
764 (widget-insert "You have ")
766 :notify (lambda (&rest ignore)
767 (notmuch-hello-update))
769 (notmuch-hello-nice-number
771 (car (process-lines notmuch-command "count")))))
772 (widget-insert " messages.\n")))
774 (defun notmuch-hello-insert-saved-searches ()
775 "Insert the saved-searches section."
776 (let ((searches (notmuch-hello-query-counts
777 (if notmuch-saved-search-sort-function
778 (funcall notmuch-saved-search-sort-function
779 notmuch-saved-searches)
780 notmuch-saved-searches)
781 :show-empty-searches notmuch-show-empty-saved-searches)))
783 (widget-insert "Saved searches: ")
784 (widget-create 'push-button
785 :notify (lambda (&rest ignore)
786 (customize-variable 'notmuch-saved-searches))
788 (widget-insert "\n\n")
789 (let ((start (point)))
790 (notmuch-hello-insert-buttons searches)
791 (indent-rigidly start (point) notmuch-hello-indent)))))
793 (defun notmuch-hello-insert-search ()
794 "Insert a search widget."
795 (widget-insert "Search: ")
796 (widget-create 'editable-field
797 ;; Leave some space at the start and end of the
799 :size (max 8 (- (window-width) notmuch-hello-indent
800 (length "Search: ")))
801 :action #'notmuch-hello-search)
802 ;; Add an invisible dot to make `widget-end-of-line' ignore
803 ;; trailing spaces in the search widget field. A dot is used
804 ;; instead of a space to make `show-trailing-whitespace'
805 ;; happy, i.e. avoid it marking the whole line as trailing
808 (put-text-property (1- (point)) (point) 'invisible t)
809 (widget-insert "\n"))
811 (defun notmuch-hello-insert-recent-searches ()
812 "Insert recent searches."
813 (when notmuch-search-history
814 (widget-insert "Recent searches: ")
817 :notify (lambda (&rest _ignore)
818 (when (y-or-n-p "Are you sure you want to clear the searches? ")
819 (setq notmuch-search-history nil)
820 (notmuch-hello-update)))
822 (widget-insert "\n\n")
823 (let ((width (notmuch-search-item-field-width)))
824 (dolist (search (seq-take notmuch-search-history
825 notmuch-hello-recent-searches-max))
826 (widget-create 'notmuch-search-item :value search :size width)))))
828 (defun notmuch-hello-insert-searches (title query-list &rest options)
829 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
831 QUERY-LIST should ideally be a plist but for backwards
832 compatibility other forms are also accepted (see
833 `notmuch-saved-searches' for details). The plist should
834 contain keys :name and :query; if :count-query is also present
835 then it specifies an alternate query to be used to generate the
836 count for the associated search.
838 Supports the following entries in OPTIONS as a plist:
839 :initially-hidden - if non-nil, section will be hidden on startup
840 :show-empty-searches - show buttons with no matching messages
841 :hide-if-empty - hide if no buttons would be shown
842 (only makes sense without :show-empty-searches)
843 :filter - This can be a function that takes the search query as its argument and
844 returns a filter to be used in conjunction with the query for that search or nil
845 to hide the element. This can also be a string that is used as a combined with
846 each query using \"and\".
847 :filter-count - Separate filter to generate the count displayed each search. Accepts
848 the same values as :filter. If :filter and :filter-count are specified, this
849 will be used instead of :filter, not in conjunction with it."
850 (widget-insert title ": ")
851 (when (and notmuch-hello-first-run (plist-get options :initially-hidden))
852 (add-to-list 'notmuch-hello-hidden-sections title))
853 (let ((is-hidden (member title notmuch-hello-hidden-sections))
856 (widget-create 'push-button
857 :notify `(lambda (widget &rest ignore)
858 (setq notmuch-hello-hidden-sections
859 (delete ,title notmuch-hello-hidden-sections))
860 (notmuch-hello-update))
862 (widget-create 'push-button
863 :notify `(lambda (widget &rest ignore)
864 (add-to-list 'notmuch-hello-hidden-sections
866 (notmuch-hello-update))
870 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
871 (when (or (not (plist-get options :hide-if-empty))
874 (notmuch-hello-insert-buttons searches)
875 (indent-rigidly start (point) notmuch-hello-indent))))))
877 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
878 "Insert a section displaying all tags with message counts.
880 TITLE defaults to \"All tags\".
881 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
884 :hide-tags - List of tags that should be excluded."
885 (apply 'notmuch-hello-insert-searches
886 (or title "All tags")
887 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
890 (defun notmuch-hello-insert-inbox ()
891 "Show an entry for each saved search and inboxed messages for each tag."
892 (notmuch-hello-insert-searches "What's in your inbox"
894 notmuch-saved-searches
895 (notmuch-hello-generate-tag-alist))
896 :filter "tag:inbox"))
898 (defun notmuch-hello-insert-alltags ()
899 "Insert a section displaying all tags and associated message counts."
900 (notmuch-hello-insert-tags-section
902 :initially-hidden (not notmuch-show-all-tags-list)
903 :hide-tags notmuch-hello-hide-tags
904 :filter notmuch-hello-tag-list-make-query))
906 (defun notmuch-hello-insert-footer ()
907 "Insert the notmuch-hello footer."
908 (let ((start (point)))
909 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
910 (widget-insert "Customize ")
912 :notify (lambda (&rest ignore)
913 (customize-group 'notmuch))
914 :button-prefix "" :button-suffix ""
916 (widget-insert " or ")
918 :notify (lambda (&rest ignore)
919 (customize-variable 'notmuch-hello-sections))
920 :button-prefix "" :button-suffix ""
922 (let ((fill-column (- (window-width) notmuch-hello-indent)))
923 (center-region start (point)))))
926 (defun notmuch-hello (&optional no-display)
927 "Run notmuch and display saved searches, known tags, etc."
929 (notmuch-assert-cli-sane)
930 ;; This may cause a window configuration change, so if the
931 ;; auto-refresh hook is already installed, avoid recursive refresh.
932 (let ((notmuch-hello-auto-refresh nil))
934 (set-buffer "*notmuch-hello*")
935 (pop-to-buffer-same-window "*notmuch-hello*")))
936 ;; Install auto-refresh hook
937 (when notmuch-hello-auto-refresh
938 (add-hook 'window-configuration-change-hook
939 #'notmuch-hello-window-configuration-change))
940 (let ((target-line (line-number-at-pos))
941 (target-column (current-column))
942 (inhibit-read-only t))
943 ;; Delete all editable widget fields. Editable widget fields are
944 ;; tracked in a buffer local variable `widget-field-list' (and
945 ;; others). If we do `erase-buffer' without properly deleting the
946 ;; widgets, some widget-related functions are confused later.
947 (mapc 'widget-delete widget-field-list)
949 (unless (eq major-mode 'notmuch-hello-mode)
950 (notmuch-hello-mode))
951 (let ((all (overlay-lists)))
952 ;; Delete all the overlays.
953 (mapc 'delete-overlay (car all))
954 (mapc 'delete-overlay (cdr all)))
957 (let ((point-before (point)))
958 (if (functionp section)
960 (apply (car section) (cdr section)))
961 ;; don't insert a newline when the previous section didn't
963 (unless (eq (point) point-before)
964 (widget-insert "\n"))))
965 notmuch-hello-sections)
967 ;; Move point back to where it was before refresh. Use line and
968 ;; column instead of point directly to be insensitive to additions
969 ;; and removals of text within earlier lines.
970 (goto-char (point-min))
971 (forward-line (1- target-line))
972 (move-to-column target-column))
973 (run-hooks 'notmuch-hello-refresh-hook)
974 (setq notmuch-hello-first-run nil))
978 (provide 'notmuch-hello)
980 ;;; notmuch-hello.el ends here