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" (&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 (const :format " Name: " :name) (string :format "%v"))
95 (group :format "%v" :inline t (const :format " Query: " :query) (string :format "%v")))
98 (group :format "%v" :inline t (const :format "Shortcut key: " :key) (key-sequence :format "%v"))
99 (group :format "%v" :inline t (const :format "Count-Query: " :count-query) (string :format "%v"))
100 (group :format "%v" :inline t (const :format "" :sort-order)
101 (choice :tag " Sort Order"
102 (const :tag "Default" nil)
103 (const :tag "Oldest-first" oldest-first)
104 (const :tag "Newest-first" newest-first)))
105 (group :format "%v" :inline t (const :format "" :search-type)
106 (choice :tag " Search Type"
107 (const :tag "Search mode" nil)
108 (const :tag "Tree mode" tree)
109 (const :tag "Unthreaded mode" unthreaded))))))
111 (defcustom notmuch-saved-searches
112 `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
113 (:name "unread" :query "tag:unread" :key ,(kbd "u"))
114 (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
115 (:name "sent" :query "tag:sent" :key ,(kbd "t"))
116 (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
117 (:name "all mail" :query "*" :key ,(kbd "a")))
118 "A list of saved searches to display.
120 The saved search can be given in 3 forms. The preferred way is as
121 a plist. Supported properties are
123 :name Name of the search (required).
124 :query Search to run (required).
125 :key Optional shortcut key for `notmuch-jump-search'.
126 :count-query Optional extra query to generate the count
127 shown. If not present then the :query property
129 :sort-order Specify the sort order to be used for the search.
130 Possible values are 'oldest-first 'newest-first or
131 nil. Nil means use the default sort order.
132 :search-type Specify whether to run the search in search-mode,
133 tree mode or unthreaded mode. Set to 'tree to specify tree
134 mode, 'unthreaded to specify unthreaded mode, and set to nil
135 (or anything except tree and unthreaded) to specify search mode.
137 Other accepted forms are a cons cell of the form (NAME . QUERY)
138 or a list of the form (NAME QUERY COUNT-QUERY)."
139 ;; The saved-search format is also used by the all-tags notmuch-hello
140 ;; section. This section generates its own saved-search list in one of
141 ;; the latter two forms.
143 :get 'notmuch-hello--saved-searches-to-plist
144 :type '(repeat notmuch-saved-search-plist)
145 :tag "List of Saved Searches"
146 :group 'notmuch-hello)
148 (defcustom notmuch-hello-recent-searches-max 10
149 "The number of recent searches to display."
151 :group 'notmuch-hello)
153 (defcustom notmuch-show-empty-saved-searches nil
154 "Should saved searches with no messages be listed?"
156 :group 'notmuch-hello)
158 (defun notmuch-sort-saved-searches (saved-searches)
159 "Generate an alphabetically sorted saved searches list."
160 (sort (copy-sequence saved-searches)
162 (string< (notmuch-saved-search-get a :name)
163 (notmuch-saved-search-get b :name)))))
165 (defcustom notmuch-saved-search-sort-function nil
166 "Function used to sort the saved searches for the notmuch-hello view.
168 This variable controls how saved searches should be sorted. No
169 sorting (nil) displays the saved searches in the order they are
170 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
171 saved searches in alphabetical order. Custom sort function should
172 be a function or a lambda expression that takes the saved
173 searches list as a parameter, and returns a new saved searches
174 list to be used. For compatibility with the various saved-search
175 formats it should use notmuch-saved-search-get to access the
176 fields of the search."
177 :type '(choice (const :tag "No sorting" nil)
178 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
179 (function :tag "Custom sort function"
180 :value notmuch-sort-saved-searches))
181 :group 'notmuch-hello)
183 (defvar notmuch-hello-indent 4
184 "How much to indent non-headers.")
186 (defcustom notmuch-show-logo t
187 "Should the notmuch logo be shown?"
189 :group 'notmuch-hello)
191 (defcustom notmuch-show-all-tags-list nil
192 "Should all tags be shown in the notmuch-hello view?"
194 :group 'notmuch-hello)
196 (defcustom notmuch-hello-tag-list-make-query nil
197 "Function or string to generate queries for the all tags list.
199 This variable controls which query results are shown for each tag
200 in the \"all tags\" list. If nil, it will use all messages with
201 that tag. If this is set to a string, it is used as a filter for
202 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
203 Finally this can be a function that will be called for each tag and
204 should return a filter for that tag, or nil to hide the tag."
205 :type '(choice (const :tag "All messages" nil)
206 (const :tag "Unread messages" "tag:unread")
207 (string :tag "Custom filter"
209 (function :tag "Custom filter function"))
210 :group 'notmuch-hello)
212 (defcustom notmuch-hello-hide-tags nil
213 "List of tags to be hidden in the \"all tags\"-section."
214 :type '(repeat string)
215 :group 'notmuch-hello)
217 (defface notmuch-hello-logo-background
220 (:background "#5f5f5f"))
223 (:background "white")))
224 "Background colour for the notmuch logo."
225 :group 'notmuch-hello
226 :group 'notmuch-faces)
228 (defcustom notmuch-column-control t
229 "Controls the number of columns for saved searches/tags in notmuch view.
231 This variable has three potential sets of values:
233 - t: automatically calculate the number of columns possible based
234 on the tags to be shown and the window width,
235 - an integer: a lower bound on the number of characters that will
236 be used to display each column,
237 - a float: a fraction of the window width that is the lower bound
238 on the number of characters that should be used for each
242 - if you would like two columns of tags, set this to 0.5.
243 - if you would like a single column of tags, set this to 1.0.
244 - if you would like tags to be 30 characters wide, set this to
246 - if you don't want to worry about all of this nonsense, leave
249 (const :tag "Automatically calculated" t)
250 (integer :tag "Number of characters")
251 (float :tag "Fraction of window"))
252 :group 'notmuch-hello)
254 (defcustom notmuch-hello-thousands-separator " "
255 "The string used as a thousands separator.
257 Typically \",\" in the US and UK and \".\" or \" \" in Europe.
258 The latter is recommended in the SI/ISO 31-0 standard and by the
259 International Bureau of Weights and Measures."
261 :group 'notmuch-hello)
263 (defcustom notmuch-hello-mode-hook nil
264 "Functions called after entering `notmuch-hello-mode'."
266 :group 'notmuch-hello
267 :group 'notmuch-hooks)
269 (defcustom notmuch-hello-refresh-hook nil
270 "Functions called after updating a `notmuch-hello' buffer."
272 :group 'notmuch-hello
273 :group 'notmuch-hooks)
275 (defvar notmuch-hello-url "https://notmuchmail.org"
276 "The `notmuch' web site.")
278 (defvar notmuch-hello-custom-section-options
279 '((:filter (string :tag "Filter for each tag"))
280 (:filter-count (string :tag "Different filter to generate message counts"))
281 (:initially-hidden (const :tag "Hide this section on startup" t))
282 (:show-empty-searches (const :tag "Show queries with no matching messages" t))
283 (:hide-if-empty (const :tag "Hide this section if all queries are empty
284 \(and not shown by show-empty-searches)" t)))
285 "Various customization-options for notmuch-hello-tags/query-section.")
287 (define-widget 'notmuch-hello-tags-section 'lazy
288 "Customize-type for notmuch-hello tag-list sections."
289 :tag "Customized tag-list section (see docstring for details)"
292 (const :tag "" notmuch-hello-insert-tags-section)
293 (string :tag "Title for this section")
297 ,(append notmuch-hello-custom-section-options
298 '((:hide-tags (repeat :tag "Tags that will be hidden"
301 (define-widget 'notmuch-hello-query-section 'lazy
302 "Customize-type for custom saved-search-like sections"
303 :tag "Customized queries section (see docstring for details)"
306 (const :tag "" notmuch-hello-insert-searches)
307 (string :tag "Title for this section")
308 (repeat :tag "Queries"
309 (cons (string :tag "Name") (string :tag "Query")))
310 (plist :inline t :options ,notmuch-hello-custom-section-options)))
312 (defcustom notmuch-hello-sections
313 (list #'notmuch-hello-insert-header
314 #'notmuch-hello-insert-saved-searches
315 #'notmuch-hello-insert-search
316 #'notmuch-hello-insert-recent-searches
317 #'notmuch-hello-insert-alltags
318 #'notmuch-hello-insert-footer)
319 "Sections for notmuch-hello.
321 The list contains functions which are used to construct sections in
322 notmuch-hello buffer. When notmuch-hello buffer is constructed,
323 these functions are run in the order they appear in this list. Each
324 function produces a section simply by adding content to the current
325 buffer. A section should not end with an empty line, because a
326 newline will be inserted after each section by `notmuch-hello'.
328 Each function should take no arguments. The return value is
331 For convenience an element can also be a list of the form (FUNC ARG1
332 ARG2 .. ARGN) in which case FUNC will be applied to the rest of the
335 A \"Customized tag-list section\" item in the customize-interface
336 displays a list of all tags, optionally hiding some of them. It
337 is also possible to filter the list of messages matching each tag
338 by an additional filter query. Similarly, the count of messages
339 displayed next to the buttons can be generated by applying a
340 different filter to the tag query. These filters are also
341 supported for \"Customized queries section\" items."
342 :group 'notmuch-hello
345 (choice (function-item notmuch-hello-insert-header)
346 (function-item notmuch-hello-insert-saved-searches)
347 (function-item notmuch-hello-insert-search)
348 (function-item notmuch-hello-insert-recent-searches)
349 (function-item notmuch-hello-insert-alltags)
350 (function-item notmuch-hello-insert-footer)
351 (function-item notmuch-hello-insert-inbox)
352 notmuch-hello-tags-section
353 notmuch-hello-query-section
354 (function :tag "Custom section"))))
356 (defcustom notmuch-hello-auto-refresh t
357 "Automatically refresh when returning to the notmuch-hello buffer."
358 :group 'notmuch-hello
361 (defvar notmuch-hello-hidden-sections nil
362 "List of sections titles whose contents are hidden")
364 (defvar notmuch-hello-first-run t
365 "True if `notmuch-hello' is run for the first time, set to nil
368 (defun notmuch-hello-nice-number (n)
371 (push (% n 1000) result)
373 (setq result (or result '(0)))
375 (number-to-string (car result))
376 (mapcar (lambda (elem)
377 (format "%s%03d" notmuch-hello-thousands-separator elem))
380 (defun notmuch-hello-trim (search)
382 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
383 (match-string 1 search)
386 (defun notmuch-hello-search (&optional search)
387 (unless (null search)
388 (setq search (notmuch-hello-trim search))
389 (let ((history-delete-duplicates t))
390 (add-to-history 'notmuch-search-history search)))
391 (notmuch-search search notmuch-search-oldest-first))
393 (defun notmuch-hello-add-saved-search (widget)
395 (let ((search (widget-value
397 (widget-get widget :notmuch-saved-search-widget))))
398 (name (completing-read "Name for saved search: "
399 notmuch-saved-searches)))
400 ;; If an existing saved search with this name exists, remove it.
401 (setq notmuch-saved-searches
402 (cl-loop for elem in notmuch-saved-searches
404 (notmuch-saved-search-get elem :name)))
407 (customize-save-variable 'notmuch-saved-searches
408 (add-to-list 'notmuch-saved-searches
409 (list :name name :query search) t))
410 (message "Saved '%s' as '%s'." search name)
411 (notmuch-hello-update)))
413 (defun notmuch-hello-delete-search-from-history (widget)
415 (let ((search (widget-value
417 (widget-get widget :notmuch-saved-search-widget)))))
418 (setq notmuch-search-history (delete search
419 notmuch-search-history))
420 (notmuch-hello-update)))
422 (defun notmuch-hello-longest-label (searches-alist)
423 (or (cl-loop for elem in searches-alist
424 maximize (length (notmuch-saved-search-get elem :name)))
427 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
428 (let ((len (length list)))
429 (cl-loop for col from 0 to (- ncols 1)
430 collect (let ((offset (+ (* nrows col) row)))
433 ;; Don't forget to insert an empty slot in the
434 ;; output matrix if there is no corresponding
435 ;; value in the input matrix.
438 (defun notmuch-hello-reflect (list ncols)
439 "Reflect a `ncols' wide matrix represented by `list' along the
442 (let ((nrows (ceiling (length list) ncols)))
443 (cl-loop for row from 0 to (- nrows 1)
444 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
446 (defun notmuch-hello-widget-search (widget &rest ignore)
448 ((eq (widget-get widget :notmuch-search-type) 'tree)
449 (notmuch-tree (widget-get widget
450 :notmuch-search-terms)))
451 ((eq (widget-get widget :notmuch-search-type) 'unthreaded)
452 (notmuch-unthreaded (widget-get widget
453 :notmuch-search-terms)))
455 (notmuch-search (widget-get widget
456 :notmuch-search-terms)
458 :notmuch-search-oldest-first)))))
460 (defun notmuch-saved-search-count (search)
461 (car (process-lines notmuch-command "count" search)))
463 (defun notmuch-hello-tags-per-line (widest)
464 "Determine how many tags to show per line and how wide they
465 should be. Returns a cons cell `(tags-per-line width)'."
468 ((integerp notmuch-column-control)
470 (/ (- (window-width) notmuch-hello-indent)
471 ;; Count is 9 wide (8 digits plus space), 1 for the space
473 (+ 9 1 (max notmuch-column-control widest)))))
475 ((floatp notmuch-column-control)
476 (let* ((available-width (- (window-width) notmuch-hello-indent))
477 (proposed-width (max (* available-width notmuch-column-control) widest)))
478 (floor available-width proposed-width)))
482 (/ (- (window-width) notmuch-hello-indent)
483 ;; Count is 9 wide (8 digits plus space), 1 for the space
487 (cons tags-per-line (/ (max 1
488 (- (window-width) notmuch-hello-indent
489 ;; Count is 9 wide (8 digits plus
490 ;; space), 1 for the space after the
492 (* tags-per-line (+ 9 1))))
495 (defun notmuch-hello-filtered-query (query filter)
496 "Constructs a query to search all messages matching QUERY and FILTER.
498 If FILTER is a string, it is directly used in the returned query.
500 If FILTER is a function, it is called with QUERY as a parameter and
501 the string it returns is used as the query. If nil is returned,
504 Otherwise, FILTER is ignored.
507 ((functionp filter) (funcall filter query))
509 (concat "(" query ") and (" filter ")"))
512 (defun notmuch-hello-query-counts (query-list &rest options)
513 "Compute list of counts of matched messages from QUERY-LIST.
515 QUERY-LIST must be a list of saved-searches. Ideally each of
516 these is a plist but other options are available for backwards
517 compatibility: see `notmuch-saved-searches' for details.
519 The result is a list of plists each of which includes the
520 properties :name NAME, :query QUERY and :count COUNT, together
521 with any properties in the original saved-search.
523 The values :show-empty-searches, :filter and :filter-count from
524 options will be handled as specified for
525 `notmuch-hello-insert-searches'."
527 (dolist (elem query-list nil)
528 (let ((count-query (or (notmuch-saved-search-get elem :count-query)
529 (notmuch-saved-search-get elem :query))))
531 (replace-regexp-in-string
533 (notmuch-hello-filtered-query count-query
534 (or (plist-get options :filter-count)
535 (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 "notmuch count --batch failed"
541 "Please check that the notmuch CLI is new enough to support `count
542 --batch'. In general we recommend running matching versions of
543 the CLI and emacs interface."))
545 (goto-char (point-min))
547 (notmuch-remove-if-not
551 (let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
552 (search-query (plist-get elem-plist :query))
553 (filtered-query (notmuch-hello-filtered-query
554 search-query (plist-get options :filter)))
555 (message-count (prog1 (read (current-buffer))
557 (when (and filtered-query (or (plist-get options :show-empty-searches) (> 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 (if (> 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)))))))
606 (setq count (1+ count))
607 (when (eq (% count tags-per-line) 0)
608 (setq column-indent 0)
609 (widget-insert "\n")))
612 ;; If the last line was not full (and hence did not include a
613 ;; carriage return), insert one now.
614 (unless (eq (% count tags-per-line) 0)
615 (widget-insert "\n"))))
617 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
619 (defun notmuch-hello-update ()
620 "Update the notmuch-hello buffer."
621 ;; Lazy - rebuild everything.
625 (defun notmuch-hello-window-configuration-change ()
626 "Hook function to update the hello buffer when it is switched to."
627 (let ((hello-buf (get-buffer "*notmuch-hello*"))
629 ;; Consider all windows in the currently selected frame, since
630 ;; that's where the configuration change happened. This also
631 ;; refreshes our snapshot of all windows, so we have to do this
632 ;; even if we know we won't refresh (e.g., hello-buf is null).
633 (dolist (window (window-list))
634 (let ((last-buf (window-parameter window 'notmuch-hello-last-buffer))
635 (cur-buf (window-buffer window)))
636 (when (not (eq last-buf cur-buf))
637 ;; This window changed or is new. Update recorded buffer
639 (set-window-parameter window 'notmuch-hello-last-buffer cur-buf)
640 (when (and (eq cur-buf hello-buf) last-buf)
641 ;; The user just switched to hello in this window (hello
642 ;; is currently visible, was not visible on the last
643 ;; configuration change, and this is not a new window)
644 (setq do-refresh t)))))
645 (when (and do-refresh notmuch-hello-auto-refresh)
646 ;; Refresh hello as soon as we get back to redisplay. On Emacs
647 ;; 24, we can't do it right here because something in this
648 ;; hook's call stack overrides hello's point placement.
649 (run-at-time nil nil #'notmuch-hello t))
650 (when (null hello-buf)
652 (remove-hook 'window-configuration-change-hook
653 #'notmuch-hello-window-configuration-change))))
655 ;; the following variable is defined as being defconst in notmuch-version.el
656 (defvar notmuch-emacs-version)
658 (defun notmuch-hello-versions ()
659 "Display the notmuch version(s)"
661 (let ((notmuch-cli-version (notmuch-cli-version)))
662 (message "notmuch version %s"
663 (if (string= notmuch-emacs-version notmuch-cli-version)
665 (concat notmuch-cli-version
666 " (emacs mua version " notmuch-emacs-version ")")))))
668 (defvar notmuch-hello-mode-map
669 (let ((map (if (fboundp 'make-composed-keymap)
670 ;; Inherit both widget-keymap and
671 ;; notmuch-common-keymap. We have to use
672 ;; make-sparse-keymap to force this to be a new
673 ;; keymap (so that when we modify map it does not
674 ;; modify widget-keymap).
675 (make-composed-keymap (list (make-sparse-keymap) widget-keymap))
676 ;; Before Emacs 24, keymaps didn't support multiple
677 ;; inheritance,, so just copy the widget keymap since
678 ;; it's unlikely to change.
679 (copy-keymap widget-keymap))))
680 (set-keymap-parent map notmuch-common-keymap)
681 (define-key map "v" 'notmuch-hello-versions)
682 (define-key map (kbd "<C-tab>") 'widget-backward)
684 "Keymap for \"notmuch hello\" buffers.")
685 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
687 (define-derived-mode notmuch-hello-mode fundamental-mode "notmuch-hello"
688 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
690 Saved searches are \"bookmarks\" for arbitrary queries. Hit RET
691 or click on a saved search to view matching threads. Edit saved
692 searches with the `edit' button. Type `\\[notmuch-jump-search]'
693 in any Notmuch screen for quick access to saved searches that
696 Type new searches in the search box and hit RET to view matching
697 threads. Hit RET in a recent search box to re-submit a previous
698 search. Edit it first if you like. Save a recent search to saved
699 searches with the `save' button.
701 Hit `\\[notmuch-search]' or `\\[notmuch-tree]' in any Notmuch
702 screen to search for messages and view matching threads or
703 messages, respectively. Recent searches are available in the
706 Expand the all tags view with the `show' button (and collapse
707 again with the `hide' button). Hit RET or click on a tag name to
708 view matching threads.
710 Hit `\\[notmuch-refresh-this-buffer]' to refresh the screen and
711 `\\[notmuch-bury-or-kill-this-buffer]' to quit.
713 The screen may be customized via `\\[customize]'.
715 Complete list of currently available key bindings:
717 \\{notmuch-hello-mode-map}"
718 (setq notmuch-buffer-refresh-function #'notmuch-hello-update)
719 ;;(setq buffer-read-only t)
722 (defun notmuch-hello-generate-tag-alist (&optional hide-tags)
723 "Return an alist from tags to queries to display in the all-tags section."
724 (mapcar (lambda (tag)
725 (cons tag (concat "tag:" (notmuch-escape-boolean-term tag))))
726 (notmuch-remove-if-not
728 (not (member tag hide-tags)))
729 (process-lines notmuch-command "search" "--output=tags" "*"))))
731 (defun notmuch-hello-insert-header ()
732 "Insert the default notmuch-hello header."
733 (when notmuch-show-logo
734 (let ((image notmuch-hello-logo))
735 ;; The notmuch logo uses transparency. That can display poorly
736 ;; when inserting the image into an emacs buffer (black logo on
737 ;; a black background), so force the background colour of the
738 ;; image. We use a face to represent the colour so that
739 ;; `defface' can be used to declare the different possible
740 ;; colours, which depend on whether the frame has a light or
742 (setq image (cons 'image
744 (list :background (face-background 'notmuch-hello-logo-background)))))
745 (insert-image image))
748 (widget-insert "Welcome to ")
749 ;; Hack the display of the links used.
750 (let ((widget-link-prefix "")
751 (widget-link-suffix ""))
753 :notify (lambda (&rest ignore)
754 (browse-url notmuch-hello-url))
755 :help-echo "Visit the notmuch website."
758 (widget-insert "You have ")
760 :notify (lambda (&rest ignore)
761 (notmuch-hello-update))
763 (notmuch-hello-nice-number
764 (string-to-number (car (process-lines notmuch-command "count")))))
765 (widget-insert " messages.\n")))
768 (defun notmuch-hello-insert-saved-searches ()
769 "Insert the saved-searches section."
770 (let ((searches (notmuch-hello-query-counts
771 (if notmuch-saved-search-sort-function
772 (funcall notmuch-saved-search-sort-function
773 notmuch-saved-searches)
774 notmuch-saved-searches)
775 :show-empty-searches notmuch-show-empty-saved-searches)))
777 (widget-insert "Saved searches: ")
778 (widget-create 'push-button
779 :notify (lambda (&rest ignore)
780 (customize-variable 'notmuch-saved-searches))
782 (widget-insert "\n\n")
783 (let ((start (point)))
784 (notmuch-hello-insert-buttons searches)
785 (indent-rigidly start (point) notmuch-hello-indent)))))
787 (defun notmuch-hello-insert-search ()
788 "Insert a search widget."
789 (widget-insert "Search: ")
790 (widget-create 'editable-field
791 ;; Leave some space at the start and end of the
793 :size (max 8 (- (window-width) notmuch-hello-indent
794 (length "Search: ")))
795 :action (lambda (widget &rest ignore)
796 (notmuch-hello-search (widget-value widget))))
797 ;; Add an invisible dot to make `widget-end-of-line' ignore
798 ;; trailing spaces in the search widget field. A dot is used
799 ;; instead of a space to make `show-trailing-whitespace'
800 ;; happy, i.e. avoid it marking the whole line as trailing
803 (put-text-property (1- (point)) (point) 'invisible t)
804 (widget-insert "\n"))
806 (defun notmuch-hello-insert-recent-searches ()
807 "Insert recent searches."
808 (when notmuch-search-history
809 (widget-insert "Recent searches: ")
810 (widget-create 'push-button
811 :notify (lambda (&rest ignore)
812 (when (y-or-n-p "Are you sure you want to clear the searches? ")
813 (setq notmuch-search-history nil)
814 (notmuch-hello-update)))
816 (widget-insert "\n\n")
817 (let ((start (point)))
818 (cl-loop for i from 1 to notmuch-hello-recent-searches-max
819 for search in notmuch-search-history do
820 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" i))))
822 (widget-create 'editable-field
823 ;; Don't let the search boxes be
824 ;; less than 8 characters wide.
831 (* 2 notmuch-hello-indent)
834 ;; `[save]' button. 6
839 ;; before the `[del]'
843 :action (lambda (widget &rest ignore)
844 (notmuch-hello-search (widget-value widget)))
847 (widget-create 'push-button
848 :notify (lambda (widget &rest ignore)
849 (notmuch-hello-add-saved-search widget))
850 :notmuch-saved-search-widget widget-symbol
853 (widget-create 'push-button
854 :notify (lambda (widget &rest ignore)
855 (when (y-or-n-p "Are you sure you want to delete this search? ")
856 (notmuch-hello-delete-search-from-history widget)))
857 :notmuch-saved-search-widget widget-symbol
859 (widget-insert "\n"))
860 (indent-rigidly start (point) notmuch-hello-indent))
863 (defun notmuch-hello-insert-searches (title query-list &rest options)
864 "Insert a section with TITLE showing a list of buttons made from QUERY-LIST.
866 QUERY-LIST should ideally be a plist but for backwards
867 compatibility other forms are also accepted (see
868 `notmuch-saved-searches' for details). The plist should
869 contain keys :name and :query; if :count-query is also present
870 then it specifies an alternate query to be used to generate the
871 count for the associated search.
873 Supports the following entries in OPTIONS as a plist:
874 :initially-hidden - if non-nil, section will be hidden on startup
875 :show-empty-searches - show buttons with no matching messages
876 :hide-if-empty - hide if no buttons would be shown
877 (only makes sense without :show-empty-searches)
878 :filter - This can be a function that takes the search query as its argument and
879 returns a filter to be used in conjunction with the query for that search or nil
880 to hide the element. This can also be a string that is used as a combined with
881 each query using \"and\".
882 :filter-count - Separate filter to generate the count displayed each search. Accepts
883 the same values as :filter. If :filter and :filter-count are specified, this
884 will be used instead of :filter, not in conjunction with it."
885 (widget-insert title ": ")
886 (if (and notmuch-hello-first-run (plist-get options :initially-hidden))
887 (add-to-list 'notmuch-hello-hidden-sections title))
888 (let ((is-hidden (member title notmuch-hello-hidden-sections))
891 (widget-create 'push-button
892 :notify `(lambda (widget &rest ignore)
893 (setq notmuch-hello-hidden-sections
894 (delete ,title notmuch-hello-hidden-sections))
895 (notmuch-hello-update))
897 (widget-create 'push-button
898 :notify `(lambda (widget &rest ignore)
899 (add-to-list 'notmuch-hello-hidden-sections
901 (notmuch-hello-update))
904 (when (not is-hidden)
905 (let ((searches (apply 'notmuch-hello-query-counts query-list options)))
906 (when (or (not (plist-get options :hide-if-empty))
909 (notmuch-hello-insert-buttons searches)
910 (indent-rigidly start (point) notmuch-hello-indent))))))
912 (defun notmuch-hello-insert-tags-section (&optional title &rest options)
913 "Insert a section displaying all tags with message counts.
915 TITLE defaults to \"All tags\".
916 Allowed options are those accepted by `notmuch-hello-insert-searches' and the
919 :hide-tags - List of tags that should be excluded."
920 (apply 'notmuch-hello-insert-searches
921 (or title "All tags")
922 (notmuch-hello-generate-tag-alist (plist-get options :hide-tags))
925 (defun notmuch-hello-insert-inbox ()
926 "Show an entry for each saved search and inboxed messages for each tag"
927 (notmuch-hello-insert-searches "What's in your inbox"
929 notmuch-saved-searches
930 (notmuch-hello-generate-tag-alist))
931 :filter "tag:inbox"))
933 (defun notmuch-hello-insert-alltags ()
934 "Insert a section displaying all tags and associated message counts"
935 (notmuch-hello-insert-tags-section
937 :initially-hidden (not notmuch-show-all-tags-list)
938 :hide-tags notmuch-hello-hide-tags
939 :filter notmuch-hello-tag-list-make-query))
941 (defun notmuch-hello-insert-footer ()
942 "Insert the notmuch-hello footer."
943 (let ((start (point)))
944 (widget-insert "Hit `?' for context-sensitive help in any Notmuch screen.\n")
945 (widget-insert "Customize ")
947 :notify (lambda (&rest ignore)
948 (customize-group 'notmuch))
949 :button-prefix "" :button-suffix ""
951 (widget-insert " or ")
953 :notify (lambda (&rest ignore)
954 (customize-variable 'notmuch-hello-sections))
955 :button-prefix "" :button-suffix ""
957 (let ((fill-column (- (window-width) notmuch-hello-indent)))
958 (center-region start (point)))))
961 (defun notmuch-hello (&optional no-display)
962 "Run notmuch and display saved searches, known tags, etc."
965 (notmuch-assert-cli-sane)
966 ;; This may cause a window configuration change, so if the
967 ;; auto-refresh hook is already installed, avoid recursive refresh.
968 (let ((notmuch-hello-auto-refresh nil))
970 (set-buffer "*notmuch-hello*")
971 (switch-to-buffer "*notmuch-hello*")))
973 ;; Install auto-refresh hook
974 (when notmuch-hello-auto-refresh
975 (add-hook 'window-configuration-change-hook
976 #'notmuch-hello-window-configuration-change))
978 (let ((target-line (line-number-at-pos))
979 (target-column (current-column))
980 (inhibit-read-only t))
982 ;; Delete all editable widget fields. Editable widget fields are
983 ;; tracked in a buffer local variable `widget-field-list' (and
984 ;; others). If we do `erase-buffer' without properly deleting the
985 ;; widgets, some widget-related functions are confused later.
986 (mapc 'widget-delete widget-field-list)
990 (unless (eq major-mode 'notmuch-hello-mode)
991 (notmuch-hello-mode))
993 (let ((all (overlay-lists)))
994 ;; Delete all the overlays.
995 (mapc 'delete-overlay (car all))
996 (mapc 'delete-overlay (cdr all)))
1000 (let ((point-before (point)))
1001 (if (functionp section)
1003 (apply (car section) (cdr section)))
1004 ;; don't insert a newline when the previous section didn't
1006 (unless (eq (point) point-before)
1007 (widget-insert "\n"))))
1008 notmuch-hello-sections)
1011 ;; Move point back to where it was before refresh. Use line and
1012 ;; column instead of point directly to be insensitive to additions
1013 ;; and removals of text within earlier lines.
1014 (goto-char (point-min))
1015 (forward-line (1- target-line))
1016 (move-to-column target-column))
1017 (run-hooks 'notmuch-hello-refresh-hook)
1018 (setq notmuch-hello-first-run nil))
1020 (defun notmuch-folder ()
1021 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
1027 (provide 'notmuch-hello)
1029 ;;; notmuch-hello.el ends here