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 <http://www.gnu.org/licenses/>.
20 ;; Authors: David Edmondson <dme@dme.org>
22 (eval-when-compile (require 'cl))
24 (require 'wid-edit) ; For `widget-forward'.
26 (require 'notmuch-lib)
27 (require 'notmuch-mua)
29 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
30 (declare-function notmuch-poll "notmuch" ())
32 (defvar notmuch-hello-search-bar-marker nil
33 "The position of the search bar within the notmuch-hello buffer.")
35 (defcustom notmuch-recent-searches-max 10
36 "The number of recent searches to store and display."
40 (defcustom notmuch-show-empty-saved-searches nil
41 "Should saved searches with no messages be listed?"
45 (defun notmuch-sort-saved-searches (alist)
46 "Generate an alphabetically sorted saved searches alist."
47 (sort alist (lambda (a b) (string< (car a) (car b)))))
49 (defcustom notmuch-saved-search-sort-function nil
50 "Function used to sort the saved searches for the notmuch-hello view.
52 This variable controls how saved searches should be sorted. No
53 sorting (nil) displays the saved searches in the order they are
54 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
55 saved searches in alphabetical order. Custom sort function should
56 be a function or a lambda expression that takes the saved
57 searches alist as a parameter, and returns a new saved searches
59 :type '(choice (const :tag "No sorting" nil)
60 (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
61 (function :tag "Custom sort function"
62 :value notmuch-sort-saved-searches))
65 (defvar notmuch-hello-indent 4
66 "How much to indent non-headers.")
68 (defcustom notmuch-show-logo t
69 "Should the notmuch logo be shown?"
73 (defcustom notmuch-show-all-tags-list nil
74 "Should all tags be shown in the notmuch-hello view?"
78 (defcustom notmuch-hello-tag-list-make-query nil
79 "Function or string to generate queries for the all tags list.
81 This variable controls which query results are shown for each tag
82 in the \"all tags\" list. If nil, it will use all messages with
83 that tag. If this is set to a string, it is used as a filter for
84 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
85 Finally this can be a function that will be called for each tag and
86 should return a filter for that tag, or nil to hide the tag."
87 :type '(choice (const :tag "All messages" nil)
88 (const :tag "Unread messages" "tag:unread")
89 (const :tag "Custom filter" string)
90 (const :tag "Custom filter function" function))
93 (defcustom notmuch-hello-hide-tags nil
94 "List of tags to be hidden in the \"all tags\"-section."
95 :type '(repeat string)
98 (defface notmuch-hello-logo-background
101 (:background "#5f5f5f"))
104 (:background "white")))
105 "Background colour for the notmuch logo."
108 (defcustom notmuch-column-control t
109 "Controls the number of columns for saved searches/tags in notmuch view.
111 This variable has three potential sets of values:
113 - t: automatically calculate the number of columns possible based
114 on the tags to be shown and the window width,
115 - an integer: a lower bound on the number of characters that will
116 be used to display each column,
117 - a float: a fraction of the window width that is the lower bound
118 on the number of characters that should be used for each
122 - if you would like two columns of tags, set this to 0.5.
123 - if you would like a single column of tags, set this to 1.0.
124 - if you would like tags to be 30 characters wide, set this to
126 - if you don't want to worry about all of this nonsense, leave
130 (const :tag "Automatically calculated" t)
131 (integer :tag "Number of characters")
132 (float :tag "Fraction of window")))
134 (defcustom notmuch-decimal-separator ","
135 "The string used as a decimal separator.
137 Typically \",\" in the US and UK and \".\" in Europe."
141 (defvar notmuch-hello-url "http://notmuchmail.org"
142 "The `notmuch' web site.")
144 (defvar notmuch-hello-recent-searches nil)
146 (defun notmuch-hello-remember-search (search)
147 (if (not (member search notmuch-hello-recent-searches))
148 (push search notmuch-hello-recent-searches))
149 (if (> (length notmuch-hello-recent-searches)
150 notmuch-recent-searches-max)
151 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
153 (defun notmuch-hello-nice-number (n)
156 (push (% n 1000) result)
158 (setq result (or result '(0)))
160 (number-to-string (car result))
161 (mapcar (lambda (elem)
162 (format "%s%03d" notmuch-decimal-separator elem))
165 (defun notmuch-hello-trim (search)
167 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
168 (match-string 1 search)
171 (defun notmuch-hello-search (search)
172 (let ((search (notmuch-hello-trim search)))
173 (notmuch-hello-remember-search search)
174 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
176 (defun notmuch-hello-add-saved-search (widget)
178 (let ((search (widget-value
180 (widget-get widget :notmuch-saved-search-widget))))
181 (name (completing-read "Name for saved search: "
182 notmuch-saved-searches)))
183 ;; If an existing saved search with this name exists, remove it.
184 (setq notmuch-saved-searches
185 (loop for elem in notmuch-saved-searches
190 (customize-save-variable 'notmuch-saved-searches
191 (add-to-list 'notmuch-saved-searches
192 (cons name search) t))
193 (message "Saved '%s' as '%s'." search name)
194 (notmuch-hello-update)))
196 (defun notmuch-hello-longest-label (tag-alist)
197 (or (loop for elem in tag-alist
198 maximize (length (car elem)))
201 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
202 (let ((len (length list)))
203 (loop for col from 0 to (- ncols 1)
204 collect (let ((offset (+ (* nrows col) row)))
207 ;; Don't forget to insert an empty slot in the
208 ;; output matrix if there is no corresponding
209 ;; value in the input matrix.
212 (defun notmuch-hello-reflect (list ncols)
213 "Reflect a `ncols' wide matrix represented by `list' along the
216 (let ((nrows (ceiling (length list) ncols)))
217 (loop for row from 0 to (- nrows 1)
218 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
220 (defun notmuch-hello-widget-search (widget &rest ignore)
221 (notmuch-search (widget-get widget
222 :notmuch-search-terms)
223 notmuch-search-oldest-first
224 nil nil #'notmuch-hello-search-continuation))
226 (defun notmuch-saved-search-count (search)
227 (car (process-lines notmuch-command "count" search)))
229 (defun notmuch-hello-tags-per-line (widest)
230 "Determine how many tags to show per line and how wide they
231 should be. Returns a cons cell `(tags-per-line width)'."
234 ((integerp notmuch-column-control)
236 (/ (- (window-width) notmuch-hello-indent)
237 ;; Count is 9 wide (8 digits plus space), 1 for the space
239 (+ 9 1 (max notmuch-column-control widest)))))
241 ((floatp notmuch-column-control)
242 (let* ((available-width (- (window-width) notmuch-hello-indent))
243 (proposed-width (max (* available-width notmuch-column-control) widest)))
244 (floor available-width proposed-width)))
248 (/ (- (window-width) notmuch-hello-indent)
249 ;; Count is 9 wide (8 digits plus space), 1 for the space
253 (cons tags-per-line (/ (max 1
254 (- (window-width) notmuch-hello-indent
255 ;; Count is 9 wide (8 digits plus
256 ;; space), 1 for the space after the
258 (* tags-per-line (+ 9 1))))
261 (defun notmuch-hello-insert-tags (tag-alist widest target)
262 (let* ((tags-and-width (notmuch-hello-tags-per-line widest))
263 (tags-per-line (car tags-and-width))
264 (widest (cdr tags-and-width))
266 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
267 ;; Hack the display of the buttons used.
268 (widget-push-button-prefix "")
269 (widget-push-button-suffix "")
270 (found-target-pos nil))
271 ;; dme: It feels as though there should be a better way to
272 ;; implement this loop than using an incrementing counter.
274 ;; (not elem) indicates an empty slot in the matrix.
276 (let* ((name (car elem))
278 (formatted-name (format "%s " name)))
279 (widget-insert (format "%8s "
280 (notmuch-hello-nice-number
281 (string-to-number (notmuch-saved-search-count query)))))
282 (if (string= formatted-name target)
283 (setq found-target-pos (point-marker)))
284 (widget-create 'push-button
285 :notify #'notmuch-hello-widget-search
286 :notmuch-search-terms query
288 ;; Insert enough space to consume the rest of the
289 ;; column. Because the button for the name is `(1+
290 ;; (length name))' long (due to the trailing space) we
291 ;; can just insert `(- widest (length name))' spaces -
292 ;; the column separator is included in the button if
293 ;; `(equal widest (length name)'.
294 (widget-insert (make-string (max 1
295 (- widest (length name)))
297 (setq count (1+ count))
298 (if (eq (% count tags-per-line) 0)
299 (widget-insert "\n")))
302 ;; If the last line was not full (and hence did not include a
303 ;; carriage return), insert one now.
304 (if (not (eq (% count tags-per-line) 0))
305 (widget-insert "\n"))
308 (defun notmuch-hello-goto-search ()
309 "Put point inside the `search' widget."
311 (goto-char notmuch-hello-search-bar-marker))
313 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
315 (defun notmuch-hello-search-continuation()
316 (notmuch-hello-update t))
318 (defun notmuch-hello-update (&optional no-display)
319 "Update the current notmuch view."
320 ;; Lazy - rebuild everything.
322 (notmuch-hello no-display))
324 (defun notmuch-hello-poll-and-update ()
325 "Invoke `notmuch-poll' to import mail, then refresh the current view."
328 (notmuch-hello-update))
331 (defvar notmuch-hello-mode-map
332 (let ((map (make-sparse-keymap)))
333 (set-keymap-parent map widget-keymap)
334 (define-key map "v" '(lambda () "Display the notmuch version" (interactive)
335 (message "notmuch version %s" (notmuch-version))))
336 (define-key map "?" 'notmuch-help)
337 (define-key map "q" 'notmuch-kill-this-buffer)
338 (define-key map "=" 'notmuch-hello-update)
339 (define-key map "G" 'notmuch-hello-poll-and-update)
340 (define-key map (kbd "<C-tab>") 'widget-backward)
341 (define-key map "m" 'notmuch-mua-new-mail)
342 (define-key map "s" 'notmuch-hello-goto-search)
344 "Keymap for \"notmuch hello\" buffers.")
345 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
347 (defun notmuch-hello-mode ()
348 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
350 Complete list of currently available key bindings:
352 \\{notmuch-hello-mode-map}"
354 (kill-all-local-variables)
355 (use-local-map notmuch-hello-mode-map)
356 (setq major-mode 'notmuch-hello-mode
357 mode-name "notmuch-hello")
358 ;;(setq buffer-read-only t)
361 (defun notmuch-hello-generate-tag-alist ()
362 "Return an alist from tags to queries to display in the all-tags section."
363 (notmuch-remove-if-not
365 (mapcar (lambda (tag)
368 ((functionp notmuch-hello-tag-list-make-query)
369 (concat "tag:" tag " and ("
370 (funcall notmuch-hello-tag-list-make-query tag) ")"))
371 ((stringp notmuch-hello-tag-list-make-query)
372 (concat "tag:" tag " and ("
373 notmuch-hello-tag-list-make-query ")"))
374 (t (concat "tag:" tag)))))
375 (notmuch-remove-if-not
377 (not (member tag notmuch-hello-hide-tags)))
378 (process-lines notmuch-command "search-tags")))))
381 (defun notmuch-hello (&optional no-display)
382 "Run notmuch and display saved searches, known tags, etc."
385 ; Jump through a hoop to get this value from the deprecated variable
386 ; name (`notmuch-folders') or from the default value.
387 (if (not notmuch-saved-searches)
388 (setq notmuch-saved-searches (notmuch-saved-searches)))
391 (set-buffer "*notmuch-hello*")
392 (switch-to-buffer "*notmuch-hello*"))
394 (let ((target (if (widget-at)
395 (widget-value (widget-at))
399 (widget-value (widget-at)))
402 (kill-all-local-variables)
403 (let ((inhibit-read-only t))
406 (unless (eq major-mode 'notmuch-hello-mode)
407 (notmuch-hello-mode))
409 (let ((all (overlay-lists)))
410 ;; Delete all the overlays.
411 (mapc 'delete-overlay (car all))
412 (mapc 'delete-overlay (cdr all)))
414 (when notmuch-show-logo
415 (let ((image notmuch-hello-logo))
416 ;; The notmuch logo uses transparency. That can display poorly
417 ;; when inserting the image into an emacs buffer (black logo on
418 ;; a black background), so force the background colour of the
419 ;; image. We use a face to represent the colour so that
420 ;; `defface' can be used to declare the different possible
421 ;; colours, which depend on whether the frame has a light or
423 (setq image (cons 'image
425 (list :background (face-background 'notmuch-hello-logo-background)))))
426 (insert-image image))
429 (widget-insert "Welcome to ")
430 ;; Hack the display of the links used.
431 (let ((widget-link-prefix "")
432 (widget-link-suffix ""))
434 :notify (lambda (&rest ignore)
435 (browse-url notmuch-hello-url))
436 :help-echo "Visit the notmuch website."
439 (widget-insert "You have ")
441 :notify (lambda (&rest ignore)
442 (notmuch-hello-update))
444 (notmuch-hello-nice-number
445 (string-to-number (car (process-lines notmuch-command "count")))))
446 (widget-insert " messages.\n"))
448 (let ((found-target-pos nil)
449 (final-target-pos nil))
451 ;; Filter out empty saved searches if required.
452 (if notmuch-show-empty-saved-searches
453 notmuch-saved-searches
454 (loop for elem in notmuch-saved-searches
455 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
457 (saved-widest (notmuch-hello-longest-label saved-alist))
458 (alltags-alist (if notmuch-show-all-tags-list (notmuch-hello-generate-tag-alist)))
459 (alltags-widest (notmuch-hello-longest-label alltags-alist))
460 (widest (max saved-widest alltags-widest)))
463 ;; Sort saved searches if required.
464 (when notmuch-saved-search-sort-function
466 (funcall notmuch-saved-search-sort-function saved-alist)))
467 (widget-insert "\nSaved searches: ")
468 (widget-create 'push-button
469 :notify (lambda (&rest ignore)
470 (customize-variable 'notmuch-saved-searches))
472 (widget-insert "\n\n")
473 (setq final-target-pos (point-marker))
474 (let ((start (point)))
475 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
477 (setq final-target-pos found-target-pos))
478 (indent-rigidly start (point) notmuch-hello-indent)))
480 (widget-insert "\nSearch: ")
481 (setq notmuch-hello-search-bar-marker (point-marker))
482 (widget-create 'editable-field
483 ;; Leave some space at the start and end of the
485 :size (max 8 (- (window-width) notmuch-hello-indent
486 (length "Search: ")))
487 :action (lambda (widget &rest ignore)
488 (notmuch-hello-search (widget-value widget))))
489 ;; add an invisible space to make `widget-end-of-line' ignore
490 ;; trailine spaces in the search widget field
492 (put-text-property (1- (point)) (point) 'invisible t)
495 (when notmuch-hello-recent-searches
496 (widget-insert "\nRecent searches: ")
497 (widget-create 'push-button
498 :notify (lambda (&rest ignore)
499 (setq notmuch-hello-recent-searches nil)
500 (notmuch-hello-update))
502 (widget-insert "\n\n")
503 (let ((start (point))
505 (mapc '(lambda (search)
506 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
508 (widget-create 'editable-field
509 ;; Don't let the search boxes be
510 ;; less than 8 characters wide.
517 (* 2 notmuch-hello-indent)
520 ;; `[save]' button. 6
524 :action (lambda (widget &rest ignore)
525 (notmuch-hello-search (widget-value widget)))
528 (widget-create 'push-button
529 :notify (lambda (widget &rest ignore)
530 (notmuch-hello-add-saved-search widget))
531 :notmuch-saved-search-widget widget-symbol
535 notmuch-hello-recent-searches)
536 (indent-rigidly start (point) notmuch-hello-indent)))
539 (widget-insert "\nAll tags: ")
540 (widget-create 'push-button
541 :notify (lambda (widget &rest ignore)
542 (setq notmuch-show-all-tags-list nil)
543 (notmuch-hello-update))
545 (widget-insert "\n\n")
546 (let ((start (point)))
547 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
548 (if (not final-target-pos)
549 (setq final-target-pos found-target-pos))
550 (indent-rigidly start (point) notmuch-hello-indent)))
554 (if (not notmuch-show-all-tags-list)
555 (widget-create 'push-button
556 :notify (lambda (widget &rest ignore)
557 (setq notmuch-show-all-tags-list t)
558 (notmuch-hello-update))
561 (let ((start (point)))
562 (widget-insert "\n\n")
563 (widget-insert "Type a search query and hit RET to view matching threads.\n")
564 (when notmuch-hello-recent-searches
565 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
566 (widget-insert "Save recent searches with the `save' button.\n"))
567 (when notmuch-saved-searches
568 (widget-insert "Edit saved searches with the `edit' button.\n"))
569 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
570 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
571 (let ((fill-column (- (window-width) notmuch-hello-indent)))
572 (center-region start (point))))
576 (when final-target-pos
577 (goto-char final-target-pos)
582 (notmuch-hello-goto-search)))))
584 (defun notmuch-folder ()
585 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
591 (provide 'notmuch-hello)