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>
24 (eval-when-compile (require 'cl-lib))
27 (require 'wid-edit) ; For `widget-forward'.
29 (require 'notmuch-lib)
30 (require 'notmuch-mua)
32 (declare-function notmuch-search "notmuch"
33 (&optional query oldest-first target-thread target-line continuation))
34 (declare-function notmuch-poll "notmuch" ())
35 (declare-function notmuch-tree "notmuch-tree"
36 (&optional query query-context target buffer-name open-target unthreaded))
37 (declare-function notmuch-unthreaded
38 (&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 or
142 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 (defcustom notmuch-show-logo t
197 "Should the notmuch logo be shown?"
199 :group 'notmuch-hello)
201 (defcustom notmuch-show-all-tags-list nil
202 "Should all tags be shown in the notmuch-hello view?"
204 :group 'notmuch-hello)
206 (defcustom notmuch-hello-tag-list-make-query nil
207 "Function or string to generate queries for the all tags list.
209 This variable controls which query results are shown for each tag
210 in the \"all tags\" list. If nil, it will use all messages with
211 that tag. If this is set to a string, it is used as a filter for
212 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
213 Finally this can be a function that will be called for each tag and
214 should return a filter for that tag, or nil to hide the tag."
215 :type '(choice (const :tag "All messages" nil)
216 (const :tag "Unread messages" "tag:unread")
217 (string :tag "Custom filter"
219 (function :tag "Custom filter function"))
220 :group 'notmuch-hello)
222 (defcustom notmuch-hello-hide-tags nil
223 "List of tags to be hidden in the \"all tags\"-section."
224 :type '(repeat string)
225 :group 'notmuch-hello)
227 (defface notmuch-hello-logo-background
230 (:background "#5f5f5f"))
233 (:background "white")))
234 "Background colour for the notmuch logo."
235 :group 'notmuch-hello
236 :group 'notmuch-faces)
238 (defcustom notmuch-column-control t
239 "Controls the number of columns for saved searches/tags in notmuch view.
241 This variable has three potential sets of values:
243 - t: automatically calculate the number of columns possible based
244 on the tags to be shown and the window width,
245 - an integer: a lower bound on the number of characters that will
246 be used to display each column,
247 - a float: a fraction of the window width that is the lower bound
248 on the number of characters that should be used for each
252 - if you would like two columns of tags, set this to 0.5.
253 - if you would like a single column of tags, set this to 1.0.
254 - if you would like tags to be 30 characters wide, set this to
256 - if you don't want to worry about all of this nonsense, leave
259 (const :tag "Automatically calculated" t)
260 (integer :tag "Number of characters")
261 (float :tag "Fraction of window"))
262 :group 'notmuch-hello)
264 (defcustom notmuch-hello-thousands-separator " "
265 "The string used as a thousands separator.
267 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
268 The latter is recommended in the SI/ISO 31-0 standard and by the
269 International Bureau of Weights and Measures."
271 :group 'notmuch-hello)
273 (defcustom notmuch-hello-mode-hook nil
274 "Functions called after entering `notmuch-hello-mode'."
276 :group 'notmuch-hello
277 :group 'notmuch-hooks)
279 (defcustom notmuch-hello-refresh-hook nil
280 "Functions called after updating a `notmuch-hello' buffer."
282 :group 'notmuch-hello
283 :group 'notmuch-hooks)
285 (defconst notmuch-hello-url "https://notmuchmail.org"
286 "The `notmuch' web site.")
288 (defvar notmuch-hello-custom-section-options
289 '((:filter (string :tag "Filter for each tag"))
290 (:filter-count (string :tag "Different filter to generate message counts"))
291 (:initially-hidden (const :tag "Hide this section on startup" t))
292 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
293 (:hide-if-empty (const :tag "Hide this section if all queries are empty
294 \(and not shown by show-empty-searches)" t)))
295 "Various customization-options for notmuch-hello-tags/query-section.")
297 (define-widget 'notmuch-hello-tags-section 'lazy
298 "Customize-type for notmuch-hello tag-list sections."
299 :tag "Customized tag-list section (see docstring for details)"
302 (const :tag "" notmuch-hello-insert-tags-section)
303 (string :tag "Title for this section")
307 ,(append notmuch-hello-custom-section-options
308 '((:hide-tags (repeat :tag "Tags that will be hidden"
311 (define-widget 'notmuch-hello-query-section 'lazy
312 "Customize-type for custom saved-search-like sections"
313 :tag "Customized queries section (see docstring for details)"
316 (const :tag "" notmuch-hello-insert-searches)
317 (string :tag "Title for this section")
318 (repeat :tag "Queries"
319 (cons (string :tag "Name") (string :tag "Query")))
320 (plist :inline t :options ,notmuch-hello-custom-section-options)))
322 (defcustom notmuch-hello-sections
323 (list #'notmuch-hello-insert-header
324 #'notmuch-hello-insert-saved-searches
325 #'notmuch-hello-insert-search
326 #'notmuch-hello-insert-recent-searches
327 #'notmuch-hello-insert-alltags
328 #'notmuch-hello-insert-footer)
329 "Sections for notmuch-hello.
331 The list contains functions which are used to construct sections in
332 notmuch-hello buffer. When notmuch-hello buffer is constructed,
333 these functions are run in the order they appear in this list. Each
334 function produces a section simply by adding content to the current
335 buffer. A section should not end with an empty line, because a
336 newline will be inserted after each section by `notmuch-hello'.
338 Each function should take no arguments. The return value is
341 For convenience an element can also be a list of the form (FUNC ARG1
342 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
345 A \"Customized tag-list section\" item in the customize-interface
346 displays a list of all tags, optionally hiding some of them. It
347 is also possible to filter the list of messages matching each tag
348 by an additional filter query. Similarly, the count of messages
349 displayed next to the buttons can be generated by applying a
350 different filter to the tag query. These filters are also
351 supported for \"Customized queries section\" items."
352 :group 'notmuch-hello
355 (choice (function-item notmuch-hello-insert-header)
356 (function-item notmuch-hello-insert-saved-searches)
357 (function-item notmuch-hello-insert-search)
358 (function-item notmuch-hello-insert-recent-searches)
359 (function-item notmuch-hello-insert-alltags)
360 (function-item notmuch-hello-insert-footer)
361 (function-item notmuch-hello-insert-inbox)
362 notmuch-hello-tags-section
363 notmuch-hello-query-section
364 (function :tag "Custom section"))))
366 (defcustom notmuch-hello-auto-refresh t
367 "Automatically refresh when returning to the notmuch-hello buffer."
368 :group 'notmuch-hello
371 (defvar notmuch-hello-hidden-sections nil
372 "List of sections titles whose contents are hidden.")
374 (defvar notmuch-hello-first-run t
375 "True if `notmuch-hello' is run for the first time, set to nil
378 (defun notmuch-hello-nice-number (n)
381 (push (% n 1000) result)
383 (setq result (or result '(0)))
385 (number-to-string (car result))
386 (mapcar (lambda (elem)
387 (format "%s%03d" notmuch-hello-thousands-separator elem))
390 (defun notmuch-hello-search (&optional search)
391 (unless (null search)
392 (setq search (string-trim search))
393 (let ((history-delete-duplicates t))
394 (add-to-history 'notmuch-search-history search)))
395 (notmuch-search search notmuch-search-oldest-first))
397 (defun notmuch-hello-add-saved-search (widget)
399 (let ((search (widget-value
401 (widget-get widget :notmuch-saved-search-widget))))
402 (name (completing-read "Name for saved search: "
403 notmuch-saved-searches)))
404 ;; If an existing saved search with this name exists, remove it.
405 (setq notmuch-saved-searches
406 (cl-loop for elem in notmuch-saved-searches
408 (notmuch-saved-search-get elem :name)))
411 (customize-save-variable 'notmuch-saved-searches
412 (add-to-list 'notmuch-saved-searches
413 (list :name name :query search) t))
414 (message "Saved '%s' as '%s'." search name)
415 (notmuch-hello-update)))
417 (defun notmuch-hello-delete-search-from-history (widget)
419 (let ((search (widget-value
421 (widget-get widget :notmuch-saved-search-widget)))))
422 (setq notmuch-search-history (delete search
423 notmuch-search-history))
424 (notmuch-hello-update)))
426 (defun notmuch-hello-longest-label (searches-alist)
427 (or (cl-loop for elem in searches-alist
428 maximize (length (notmuch-saved-search-get elem :name)))
431 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
432 (let ((len (length list)))
433 (cl-loop for col from 0 to (- ncols 1)
434 collect (let ((offset (+ (* nrows col) row)))
437 ;; Don't forget to insert an empty slot in the
438 ;; output matrix if there is no corresponding
439 ;; value in the input matrix.
442 (defun notmuch-hello-reflect (list ncols)
443 "Reflect a `ncols' wide matrix represented by `list' along the
446 (let ((nrows (ceiling (length list) ncols)))
447 (cl-loop for row from 0 to (- nrows 1)
448 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
450 (defun notmuch-hello-widget-search (widget &rest ignore)
452 ((eq (widget-get widget :notmuch-search-type) 'tree)
453 (notmuch-tree (widget-get widget
454 :notmuch-search-terms)))
455 ((eq (widget-get widget :notmuch-search-type) 'unthreaded)
456 (notmuch-unthreaded (widget-get widget
457 :notmuch-search-terms)))
459 (notmuch-search (widget-get widget
460 :notmuch-search-terms)
462 :notmuch-search-oldest-first)))))
464 (defun notmuch-saved-search-count (search)
465 (car (process-lines notmuch-command "count" search)))
467 (defun notmuch-hello-tags-per-line (widest)
468 "Determine how many tags to show per line and how wide they
469 should be. Returns a cons cell `(tags-per-line width)'."
472 ((integerp notmuch-column-control)
474 (/ (- (window-width) notmuch-hello-indent)
475 ;; Count is 9 wide (8 digits plus space), 1 for the space
477 (+ 9 1 (max notmuch-column-control widest)))))
478 ((floatp notmuch-column-control)
479 (let* ((available-width (- (window-width) notmuch-hello-indent))
480 (proposed-width (max (* available-width notmuch-column-control)
482 (floor available-width proposed-width)))
485 (/ (- (window-width) notmuch-hello-indent)
486 ;; Count is 9 wide (8 digits plus space), 1 for the space
489 (cons tags-per-line (/ (max 1
490 (- (window-width) notmuch-hello-indent
491 ;; Count is 9 wide (8 digits plus
492 ;; space), 1 for the space after the
494 (* tags-per-line (+ 9 1))))
497 (defun notmuch-hello-filtered-query (query filter)
498 "Constructs a query to search all messages matching QUERY and FILTER.
500 If FILTER is a string, it is directly used in the returned query.
502 If FILTER is a function, it is called with QUERY as a parameter and
503 the string it returns is used as the query. If nil is returned,
506 Otherwise, FILTER is ignored."
508 ((functionp filter) (funcall filter query))
510 (concat "(" query ") and (" filter ")"))
513 (defun notmuch-hello-query-counts (query-list &rest options)
514 "Compute list of counts of matched messages from QUERY-LIST.
516 QUERY-LIST must be a list of saved-searches. Ideally each of
517 these is a plist but other options are available for backwards
518 compatibility: see `notmuch-saved-searches' for details.
520 The result is a list of plists each of which includes the
521 properties :name NAME, :query QUERY and :count COUNT, together
522 with any properties in the original saved-search.
524 The values :show-empty-searches, :filter and :filter-count from
525 options will be handled as specified for
526 `notmuch-hello-insert-searches'."
528 (dolist (elem query-list nil)
529 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
530 (notmuch-saved-search-get elem :query))))
532 (replace-regexp-in-string
534 (notmuch-hello-filtered-query count-query
535 (or (plist-get options :filter-count)
536 (plist-get options :filter))))
538 (unless (= (call-process-region (point-min) (point-max) notmuch-command
539 t t nil "count" "--batch") 0)
540 (notmuch-logged-error
541 "notmuch count --batch failed"
542 "Please check that the notmuch CLI is new enough to support `count
543 --batch'. In general we recommend running matching versions of
544 the CLI and emacs interface."))
545 (goto-char (point-min))
546 (notmuch-remove-if-not
550 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
551 (search-query (plist-get elem-plist :query))
552 (filtered-query (notmuch-hello-filtered-query
553 search-query (plist-get options :filter)))
554 (message-count (prog1 (read (current-buffer))
556 (when (and filtered-query (or (plist-get options :show-empty-searches)
557 (> message-count 0)))
558 (setq elem-plist (plist-put elem-plist :query filtered-query))
559 (plist-put elem-plist :count message-count))))
562 (defun notmuch-hello-insert-buttons (searches)
563 "Insert buttons for SEARCHES.
565 SEARCHES must be a list of plists each of which should contain at
566 least the properties :name NAME :query QUERY and :count COUNT,
567 where QUERY is the query to start when the button for the
568 corresponding entry is activated, and COUNT should be the number
569 of messages matching the query. Such a plist can be computed
570 with `notmuch-hello-query-counts'."
571 (let* ((widest (notmuch-hello-longest-label searches))
572 (tags-and-width (notmuch-hello-tags-per-line widest))
573 (tags-per-line (car tags-and-width))
574 (column-width (cdr tags-and-width))
577 (reordered-list (notmuch-hello-reflect searches tags-per-line))
578 ;; Hack the display of the buttons used.
579 (widget-push-button-prefix "")
580 (widget-push-button-suffix ""))
581 ;; dme: It feels as though there should be a better way to
582 ;; implement this loop than using an incrementing counter.
584 ;; (not elem) indicates an empty slot in the matrix.
586 (when (> column-indent 0)
587 (widget-insert (make-string column-indent ? )))
588 (let* ((name (plist-get elem :name))
589 (query (plist-get elem :query))
590 (oldest-first (cl-case (plist-get elem :sort-order)
593 (otherwise notmuch-search-oldest-first)))
594 (search-type (plist-get elem :search-type))
595 (msg-count (plist-get elem :count)))
596 (widget-insert (format "%8s "
597 (notmuch-hello-nice-number msg-count)))
598 (widget-create 'push-button
599 :notify #'notmuch-hello-widget-search
600 :notmuch-search-terms query
601 :notmuch-search-oldest-first oldest-first
602 :notmuch-search-type search-type
605 (1+ (max 0 (- column-width (length name)))))))
607 (when (eq (% count tags-per-line) 0)
608 (setq column-indent 0)
609 (widget-insert "\n")))
611 ;; If the last line was not full (and hence did not include a
612 ;; carriage return), insert one now.
613 (unless (eq (% count tags-per-line) 0)
614 (widget-insert "\n"))))
616 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
618 (defun notmuch-hello-update ()
619 "Update the notmuch-hello buffer."
620 ;; Lazy - rebuild everything.
624 (defun notmuch-hello-window-configuration-change ()
625 "Hook function to update the hello buffer when it is switched to."
626 (let ((hello-buf (get-buffer "*notmuch-hello*"))
628 ;; Consider all windows in the currently selected frame, since
629 ;; that's where the configuration change happened. This also
630 ;; refreshes our snapshot of all windows, so we have to do this
631 ;; even if we know we won't refresh (e.g., hello-buf is null).
632 (dolist (window (window-list))
633 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
634 (cur-buf (window-buffer window)))
635 (unless (eq last-buf cur-buf)
636 ;; This window changed or is new. Update recorded buffer
638 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
639 (when (and (eq cur-buf hello-buf) last-buf)
640 ;; The user just switched to hello in this window (hello
641 ;; is currently visible, was not visible on the last
642 ;; configuration change, and this is not a new window)
643 (setq do-refresh t)))))
644 (when (and do-refresh notmuch-hello-auto-refresh)
645 ;; Refresh hello as soon as we get back to redisplay. On Emacs
646 ;; 24, we can't do it right here because something in this
647 ;; hook's call stack overrides hello's point placement.
648 ;; FIXME And on Emacs releases that we still support?
649 (run-at-time nil nil #'notmuch-hello t))
652 (remove-hook 'window-configuration-change-hook
653 #'notmuch-hello-window-configuration-change))))
655 (defvar notmuch-hello-mode-map
656 ;; Inherit both widget-keymap and notmuch-common-keymap. We have
657 ;; to use make-sparse-keymap to force this to be a new keymap (so
658 ;; that when we modify map it does not modify widget-keymap).
659 (let ((map (make-composed-keymap (list (make-sparse-keymap) widget-keymap))))
660 (set-keymap-parent map notmuch-common-keymap)
661 (define-key map (kbd "<C-tab>") 'widget-backward)
663 "Keymap for \"notmuch hello\" buffers.")
665 (define-derived-mode notmuch-hello-mode fundamental-mode "notmuch-hello"
666 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
668 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
669 or click on a saved search to view matching threads. Edit saved
670 searches with the `edit' button. Type `\\[notmuch-jump-search]'
671 in any Notmuch screen for quick access to saved searches that
674 Type new searches in the search box and hit RET to view matching
675 threads. Hit RET in a recent search box to re-submit a previous
676 search. Edit it first if you like. Save a recent search to saved
677 searches with the `save' button.
679 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
680 screen to search for messages and view matching threads or
681 messages, respectively. Recent searches are available in the
684 Expand the all tags view with the `show' button (and collapse
685 again with the `hide' button). Hit RET or click on a tag name to
686 view matching threads.
688 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
689 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
691 The screen may be customized via `\\[customize]'.
693 Complete list of currently available key bindings:
695 \\{notmuch-hello-mode-map}"
696 (setq notmuch-buffer-refresh-function #'notmuch-hello-update)
697 ;;(setq buffer-read-only t)
700 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
701 "Return an alist from tags to queries to display in the all-tags section."
702 (mapcar (lambda (tag)
703 (cons tag (concat "tag:" (notmuch-escape-boolean-term tag))))
704 (notmuch-remove-if-not
706 (not (member tag hide-tags)))
707 (process-lines notmuch-command "search" "--output=tags" "*"))))
709 (defun notmuch-hello-insert-header ()
710 "Insert the default notmuch-hello header."
711 (when notmuch-show-logo
712 (let ((image notmuch-hello-logo))
713 ;; The notmuch logo uses transparency. That can display poorly
714 ;; when inserting the image into an emacs buffer (black logo on
715 ;; a black background), so force the background colour of the
716 ;; image. We use a face to represent the colour so that
717 ;; `defface' can be used to declare the different possible
718 ;; colours, which depend on whether the frame has a light or
720 (setq image (cons 'image
724 'notmuch-hello-logo-background)))))
725 (insert-image image))
728 (widget-insert "Welcome to ")
729 ;; Hack the display of the links used.
730 (let ((widget-link-prefix "")
731 (widget-link-suffix ""))
733 :notify (lambda (&rest ignore)
734 (browse-url notmuch-hello-url))
735 :help-echo "Visit the notmuch website."
738 (widget-insert "You have ")
740 :notify (lambda (&rest ignore)
741 (notmuch-hello-update))
743 (notmuch-hello-nice-number
745 (car (process-lines notmuch-command "count")))))
746 (widget-insert " messages.\n")))
748 (defun notmuch-hello-insert-saved-searches ()
749 "Insert the saved-searches section."
750 (let ((searches (notmuch-hello-query-counts
751 (if notmuch-saved-search-sort-function
752 (funcall notmuch-saved-search-sort-function
753 notmuch-saved-searches)
754 notmuch-saved-searches)
755 :show-empty-searches notmuch-show-empty-saved-searches)))
757 (widget-insert "Saved searches: ")
758 (widget-create 'push-button
759 :notify (lambda (&rest ignore)
760 (customize-variable 'notmuch-saved-searches))
762 (widget-insert "\n\n")
763 (let ((start (point)))
764 (notmuch-hello-insert-buttons searches)
765 (indent-rigidly start (point) notmuch-hello-indent)))))
767 (defun notmuch-hello-insert-search ()
768 "Insert a search widget."
769 (widget-insert "Search: ")
770 (widget-create 'editable-field
771 ;; Leave some space at the start and end of the
773 :size (max 8 (- (window-width) notmuch-hello-indent
774 (length "Search: ")))
775 :action (lambda (widget &rest ignore)
776 (notmuch-hello-search (widget-value widget))))
777 ;; Add an invisible dot to make `widget-end-of-line' ignore
778 ;; trailing spaces in the search widget field. A dot is used
779 ;; instead of a space to make `show-trailing-whitespace'
780 ;; happy, i.e. avoid it marking the whole line as trailing
783 (put-text-property (1- (point)) (point) 'invisible t)
784 (widget-insert "\n"))
786 (defun notmuch-hello-insert-recent-searches ()
787 "Insert recent searches."
788 (when notmuch-search-history
789 (widget-insert "Recent searches: ")
790 (widget-create 'push-button
791 :notify (lambda (&rest ignore)
792 (when (y-or-n-p "Are you sure you want to clear the searches? ")
793 (setq notmuch-search-history nil)
794 (notmuch-hello-update)))
796 (widget-insert "\n\n")
797 (let ((start (point)))
798 (cl-loop for i from 1 to notmuch-hello-recent-searches-max
799 for search in notmuch-search-history do
800 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
802 (widget-create 'editable-field
803 ;; Don't let the search boxes be
804 ;; less than 8 characters wide.
811 (* 2 notmuch-hello-indent)
814 ;; `[save]' button. 6
819 ;; before the `[del]'
823 :action (lambda (widget &rest ignore)
824 (notmuch-hello-search (widget-value widget)))
827 (widget-create 'push-button
828 :notify (lambda (widget &rest ignore)
829 (notmuch-hello-add-saved-search widget))
830 :notmuch-saved-search-widget widget-symbol
833 (widget-create 'push-button
834 :notify (lambda (widget &rest ignore)
835 (when (y-or-n-p "Are you sure you want to delete this search? ")
836 (notmuch-hello-delete-search-from-history widget)))
837 :notmuch-saved-search-widget widget-symbol
839 (widget-insert "\n"))
840 (indent-rigidly start (point) notmuch-hello-indent))
843 (defun notmuch-hello-insert-searches (title query-list &rest options)
844 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
846 QUERY-LIST should ideally be a plist but for backwards
847 compatibility other forms are also accepted (see
848 `notmuch-saved-searches' for details). The plist should
849 contain keys :name and :query; if :count-query is also present
850 then it specifies an alternate query to be used to generate the
851 count for the associated search.
853 Supports the following entries in OPTIONS as a plist:
854 :initially-hidden - if non-nil, section will be hidden on startup
855 :show-empty-searches - show buttons with no matching messages
856 :hide-if-empty - hide if no buttons would be shown
857 (only makes sense without :show-empty-searches)
858 :filter - This can be a function that takes the search query as its argument and
859 returns a filter to be used in conjunction with the query for that search or nil
860 to hide the element. This can also be a string that is used as a combined with
861 each query using \"and\".
862 :filter-count - Separate filter to generate the count displayed each search. Accepts
863 the same values as :filter. If :filter and :filter-count are specified, this
864 will be used instead of :filter, not in conjunction with it."
865 (widget-insert title ": ")
866 (when (and notmuch-hello-first-run (plist-get options :initially-hidden))
867 (add-to-list 'notmuch-hello-hidden-sections title))
868 (let ((is-hidden (member title notmuch-hello-hidden-sections))
871 (widget-create 'push-button
872 :notify `(lambda (widget &rest ignore)
873 (setq notmuch-hello-hidden-sections
874 (delete ,title notmuch-hello-hidden-sections))
875 (notmuch-hello-update))
877 (widget-create 'push-button
878 :notify `(lambda (widget &rest ignore)
879 (add-to-list 'notmuch-hello-hidden-sections
881 (notmuch-hello-update))
885 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
886 (when (or (not (plist-get options :hide-if-empty))
889 (notmuch-hello-insert-buttons searches)
890 (indent-rigidly start (point) notmuch-hello-indent))))))
892 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
893 "Insert a section displaying all tags with message counts.
895 TITLE defaults to \"All tags\".
896 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
899 :hide-tags - List of tags that should be excluded."
900 (apply 'notmuch-hello-insert-searches
901 (or title "All tags")
902 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
905 (defun notmuch-hello-insert-inbox ()
906 "Show an entry for each saved search and inboxed messages for each tag."
907 (notmuch-hello-insert-searches "What's in your inbox"
909 notmuch-saved-searches
910 (notmuch-hello-generate-tag-alist))
911 :filter "tag:inbox"))
913 (defun notmuch-hello-insert-alltags ()
914 "Insert a section displaying all tags and associated message counts."
915 (notmuch-hello-insert-tags-section
917 :initially-hidden (not notmuch-show-all-tags-list)
918 :hide-tags notmuch-hello-hide-tags
919 :filter notmuch-hello-tag-list-make-query))
921 (defun notmuch-hello-insert-footer ()
922 "Insert the notmuch-hello footer."
923 (let ((start (point)))
924 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
925 (widget-insert "Customize ")
927 :notify (lambda (&rest ignore)
928 (customize-group 'notmuch))
929 :button-prefix "" :button-suffix ""
931 (widget-insert " or ")
933 :notify (lambda (&rest ignore)
934 (customize-variable 'notmuch-hello-sections))
935 :button-prefix "" :button-suffix ""
937 (let ((fill-column (- (window-width) notmuch-hello-indent)))
938 (center-region start (point)))))
941 (defun notmuch-hello (&optional no-display)
942 "Run notmuch and display saved searches, known tags, etc."
944 (notmuch-assert-cli-sane)
945 ;; This may cause a window configuration change, so if the
946 ;; auto-refresh hook is already installed, avoid recursive refresh.
947 (let ((notmuch-hello-auto-refresh nil))
949 (set-buffer "*notmuch-hello*")
950 (pop-to-buffer-same-window "*notmuch-hello*")))
951 ;; Install auto-refresh hook
952 (when notmuch-hello-auto-refresh
953 (add-hook 'window-configuration-change-hook
954 #'notmuch-hello-window-configuration-change))
955 (let ((target-line (line-number-at-pos))
956 (target-column (current-column))
957 (inhibit-read-only t))
958 ;; Delete all editable widget fields. Editable widget fields are
959 ;; tracked in a buffer local variable `widget-field-list' (and
960 ;; others). If we do `erase-buffer' without properly deleting the
961 ;; widgets, some widget-related functions are confused later.
962 (mapc 'widget-delete widget-field-list)
964 (unless (eq major-mode 'notmuch-hello-mode)
965 (notmuch-hello-mode))
966 (let ((all (overlay-lists)))
967 ;; Delete all the overlays.
968 (mapc 'delete-overlay (car all))
969 (mapc 'delete-overlay (cdr all)))
972 (let ((point-before (point)))
973 (if (functionp section)
975 (apply (car section) (cdr section)))
976 ;; don't insert a newline when the previous section didn't
978 (unless (eq (point) point-before)
979 (widget-insert "\n"))))
980 notmuch-hello-sections)
982 ;; Move point back to where it was before refresh. Use line and
983 ;; column instead of point directly to be insensitive to additions
984 ;; and removals of text within earlier lines.
985 (goto-char (point-min))
986 (forward-line (1- target-line))
987 (move-to-column target-column))
988 (run-hooks 'notmuch-hello-refresh-hook)
989 (setq notmuch-hello-first-run nil))
991 (defun notmuch-folder ()
992 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
998 (provide 'notmuch-hello)
1000 ;;; notmuch-hello.el ends here