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>
23 (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 (defvar notmuch-hello-indent 4
46 "How much to indent non-headers.")
48 (defcustom notmuch-show-logo t
49 "Should the notmuch logo be shown?"
53 (defcustom notmuch-show-all-tags-list nil
54 "Should all tags be shown in the notmuch-hello view?"
58 (defface notmuch-hello-logo-background
61 (:background "#5f5f5f"))
64 (:background "white")))
65 "Background colour for the notmuch logo."
68 (defcustom notmuch-column-control t
69 "Controls the number of columns for saved searches/tags in notmuch view.
71 This variable has three potential sets of values:
73 - t: automatically calculate the number of columns possible based
74 on the tags to be shown and the window width,
75 - an integer: a lower bound on the number of characters that will
76 be used to display each column,
77 - a float: a fraction of the window width that is the lower bound
78 on the number of characters that should be used for each
82 - if you would like two columns of tags, set this to 0.5.
83 - if you would like a single column of tags, set this to 1.0.
84 - if you would like tags to be 30 characters wide, set this to
86 - if you don't want to worry about all of this nonsense, leave
90 (const :tag "Automatically calculated" t)
91 (integer :tag "Number of characters")
92 (float :tag "Fraction of window")))
94 (defvar notmuch-hello-url "http://notmuchmail.org"
95 "The `notmuch' web site.")
97 (defvar notmuch-hello-recent-searches nil)
99 (defun notmuch-hello-remember-search (search)
100 (if (not (member search notmuch-hello-recent-searches))
101 (push search notmuch-hello-recent-searches))
102 (if (> (length notmuch-hello-recent-searches)
103 notmuch-recent-searches-max)
104 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
106 (defun notmuch-hello-trim (search)
108 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
109 (match-string 1 search)
112 (defun notmuch-hello-search (search)
113 (let ((search (notmuch-hello-trim search)))
114 (notmuch-hello-remember-search search)
115 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
117 (defun notmuch-hello-add-saved-search (widget)
119 (let ((search (widget-value
121 (widget-get widget :notmuch-saved-search-widget))))
122 (name (completing-read "Name for saved search: "
123 notmuch-saved-searches)))
124 ;; If an existing saved search with this name exists, remove it.
125 (setq notmuch-saved-searches
126 (loop for elem in notmuch-saved-searches
131 (customize-save-variable 'notmuch-saved-searches
132 (push (cons name search)
133 notmuch-saved-searches))
134 (message "Saved '%s' as '%s'." search name)
135 (notmuch-hello-update)))
137 (defun notmuch-hello-longest-label (tag-alist)
138 (or (loop for elem in tag-alist
139 maximize (length (car elem)))
142 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
143 (let ((len (length list)))
144 (loop for col from 0 to (- ncols 1)
145 collect (let ((offset (+ (* nrows col) row)))
148 ;; Don't forget to insert an empty slot in the
149 ;; output matrix if there is no corresponding
150 ;; value in the input matrix.
153 (defun notmuch-hello-reflect (list ncols)
154 "Reflect a `ncols' wide matrix represented by `list' along the
157 (let ((nrows (ceiling (length list) ncols)))
158 (loop for row from 0 to (- nrows 1)
159 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
161 (defun notmuch-hello-widget-search (widget &rest ignore)
162 (notmuch-search (widget-get widget
163 :notmuch-search-terms)
164 notmuch-search-oldest-first
165 nil nil #'notmuch-hello-search-continuation))
167 (defun notmuch-saved-search-count (search)
168 (car (process-lines notmuch-command "count" search)))
170 (defun notmuch-hello-tags-per-line (widest)
171 "Determine how many tags to show per line and how wide they
172 should be. Returns a cons cell `(tags-per-line width)'."
175 ((integerp notmuch-column-control)
177 (/ (- (window-width) notmuch-hello-indent)
178 ;; Count is 7 wide (6 digits plus space), 1 for the space
180 (+ 7 1 (max notmuch-column-control widest)))))
182 ((floatp notmuch-column-control)
183 (let* ((available-width (- (window-width) notmuch-hello-indent))
184 (proposed-width (max (* available-width notmuch-column-control) widest)))
185 (floor available-width proposed-width)))
189 (/ (- (window-width) notmuch-hello-indent)
190 ;; Count is 7 wide (6 digits plus space), 1 for the space
194 (cons tags-per-line (/ (- (window-width) notmuch-hello-indent
195 (* tags-per-line (+ 7 1)))
198 (defun notmuch-hello-insert-tags (tag-alist widest target)
199 (let* ((tags-and-width (notmuch-hello-tags-per-line widest))
200 (tags-per-line (car tags-and-width))
201 (widest (cdr tags-and-width))
203 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
204 ;; Hack the display of the buttons used.
205 (widget-push-button-prefix "")
206 (widget-push-button-suffix "")
207 (found-target-pos nil))
208 ;; dme: It feels as though there should be a better way to
209 ;; implement this loop than using an incrementing counter.
211 ;; (not elem) indicates an empty slot in the matrix.
213 (let* ((name (car elem))
215 (formatted-name (format "%s " name)))
216 (widget-insert (format "%6s " (notmuch-saved-search-count query)))
217 (if (string= formatted-name target)
218 (setq found-target-pos (point-marker)))
219 (widget-create 'push-button
220 :notify #'notmuch-hello-widget-search
221 :notmuch-search-terms query
223 ;; Insert enough space to consume the rest of the
224 ;; column. Because the button for the name is `(1+
225 ;; (length name))' long (due to the trailing space) we
226 ;; can just insert `(- widest (length name))' spaces -
227 ;; the column separator is included in the button if
228 ;; `(equal widest (length name)'.
229 (widget-insert (make-string (- widest (length name)) ? ))))
230 (setq count (1+ count))
231 (if (eq (% count tags-per-line) 0)
232 (widget-insert "\n")))
235 ;; If the last line was not full (and hence did not include a
236 ;; carriage return), insert one now.
237 (if (not (eq (% count tags-per-line) 0))
238 (widget-insert "\n"))
241 (defun notmuch-hello-goto-search ()
242 "Put point inside the `search' widget."
244 (goto-char notmuch-hello-search-bar-marker))
246 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
248 (defun notmuch-hello-search-continuation()
249 (notmuch-hello-update t))
251 (defun notmuch-hello-update (&optional no-display)
252 "Update the current notmuch view."
253 ;; Lazy - rebuild everything.
255 (notmuch-hello no-display))
257 (defun notmuch-hello-poll-and-update ()
258 "Invoke `notmuch-poll' to import mail, then refresh the current view."
261 (notmuch-hello-update))
264 (defvar notmuch-hello-mode-map
265 (let ((map (copy-keymap widget-keymap)))
266 (define-key map "v" '(lambda () "Display the notmuch version" (interactive)
267 (message "notmuch version %s" (notmuch-version))))
268 (define-key map "?" 'notmuch-help)
269 (define-key map "q" 'kill-this-buffer)
270 (define-key map "=" 'notmuch-hello-update)
271 (define-key map "G" 'notmuch-hello-poll-and-update)
272 (define-key map (kbd "<C-tab>") 'widget-backward)
273 (define-key map "m" 'notmuch-mua-mail)
274 (define-key map "s" 'notmuch-hello-goto-search)
276 "Keymap for \"notmuch hello\" buffers.")
277 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
279 (defun notmuch-hello-mode ()
280 "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
282 Complete list of currently available key bindings:
284 \\{notmuch-hello-mode-map}"
286 (kill-all-local-variables)
287 (use-local-map notmuch-hello-mode-map)
288 (setq major-mode 'notmuch-hello-mode
289 mode-name "notmuch-hello")
290 ;;(setq buffer-read-only t)
293 (defun notmuch-hello (&optional no-display)
296 ; Jump through a hoop to get this value from the deprecated variable
297 ; name (`notmuch-folders') or from the default value.
298 (if (not notmuch-saved-searches)
299 (setq notmuch-saved-searches (notmuch-saved-searches)))
302 (set-buffer "*notmuch-hello*")
303 (switch-to-buffer "*notmuch-hello*"))
305 (let ((target (if (widget-at)
306 (widget-value (widget-at))
310 (widget-value (widget-at)))
313 (kill-all-local-variables)
314 (let ((inhibit-read-only t))
317 (unless (eq major-mode 'notmuch-hello-mode)
318 (notmuch-hello-mode))
320 (let ((all (overlay-lists)))
321 ;; Delete all the overlays.
322 (mapc 'delete-overlay (car all))
323 (mapc 'delete-overlay (cdr all)))
325 (when notmuch-show-logo
326 (let ((image notmuch-hello-logo))
327 ;; The notmuch logo uses transparency. That can display poorly
328 ;; when inserting the image into an emacs buffer (black logo on
329 ;; a black background), so force the background colour of the
330 ;; image. We use a face to represent the colour so that
331 ;; `defface' can be used to declare the different possible
332 ;; colours, which depend on whether the frame has a light or
334 (setq image (cons 'image
336 (list :background (face-background 'notmuch-hello-logo-background)))))
337 (insert-image image))
340 (widget-insert "Welcome to ")
341 ;; Hack the display of the links used.
342 (let ((widget-link-prefix "")
343 (widget-link-suffix ""))
345 :notify (lambda (&rest ignore)
346 (browse-url notmuch-hello-url))
347 :help-echo "Visit the notmuch website."
350 (widget-insert "You have ")
352 :notify (lambda (&rest ignore)
353 (notmuch-hello-update))
355 (car (process-lines notmuch-command "count")))
356 (widget-insert " messages (that's not much mail).\n"))
358 (let ((found-target-pos nil)
359 (final-target-pos nil))
361 ;; Filter out empty saved seaches if required.
362 (if notmuch-show-empty-saved-searches
363 notmuch-saved-searches
364 (loop for elem in notmuch-saved-searches
365 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
367 (saved-widest (notmuch-hello-longest-label saved-alist))
368 (alltags-alist (if notmuch-show-all-tags-list
369 (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
370 (process-lines notmuch-command "search-tags"))))
371 (alltags-widest (notmuch-hello-longest-label alltags-alist))
372 (widest (max saved-widest alltags-widest)))
375 (widget-insert "\nSaved searches: ")
376 (widget-create 'push-button
377 :notify (lambda (&rest ignore)
378 (customize-variable 'notmuch-saved-searches))
380 (widget-insert "\n\n")
381 (setq final-target-pos (point-marker))
382 (let ((start (point)))
383 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
385 (setq final-target-pos found-target-pos))
386 (indent-rigidly start (point) notmuch-hello-indent)))
388 (widget-insert "\nSearch: ")
389 (setq notmuch-hello-search-bar-marker (point-marker))
390 (widget-create 'editable-field
391 ;; Leave some space at the start and end of the
393 :size (max 8 (- (window-width) notmuch-hello-indent
394 (length "Search: ")))
395 :action (lambda (widget &rest ignore)
396 (notmuch-hello-search (widget-value widget))))
399 (when notmuch-hello-recent-searches
400 (widget-insert "\nRecent searches: ")
401 (widget-create 'push-button
402 :notify (lambda (&rest ignore)
403 (setq notmuch-hello-recent-searches nil)
404 (notmuch-hello-update))
406 (widget-insert "\n\n")
407 (let ((start (point))
409 (mapc '(lambda (search)
410 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
412 (widget-create 'editable-field
413 ;; Don't let the search boxes be
414 ;; less than 8 characters wide.
421 (* 2 notmuch-hello-indent)
424 ;; `[save]' button. 6
428 :action (lambda (widget &rest ignore)
429 (notmuch-hello-search (widget-value widget)))
432 (widget-create 'push-button
433 :notify (lambda (widget &rest ignore)
434 (notmuch-hello-add-saved-search widget))
435 :notmuch-saved-search-widget widget-symbol
439 notmuch-hello-recent-searches)
440 (indent-rigidly start (point) notmuch-hello-indent)))
443 (widget-insert "\nAll tags: ")
444 (widget-create 'push-button
445 :notify (lambda (widget &rest ignore)
446 (setq notmuch-show-all-tags-list nil)
447 (notmuch-hello-update))
449 (widget-insert "\n\n")
450 (let ((start (point)))
451 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
452 (if (not final-target-pos)
453 (setq final-target-pos found-target-pos))
454 (indent-rigidly start (point) notmuch-hello-indent)))
458 (if (not notmuch-show-all-tags-list)
459 (widget-create 'push-button
460 :notify (lambda (widget &rest ignore)
461 (setq notmuch-show-all-tags-list t)
462 (notmuch-hello-update))
465 (let ((start (point)))
466 (widget-insert "\n\n")
467 (widget-insert "Type a search query and hit RET to view matching threads.\n")
468 (when notmuch-hello-recent-searches
469 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
470 (widget-insert "Save recent searches with the `save' button.\n"))
471 (when notmuch-saved-searches
472 (widget-insert "Edit saved searches with the `edit' button.\n"))
473 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
474 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
475 (let ((fill-column (- (window-width) notmuch-hello-indent)))
476 (center-region start (point))))
480 (when final-target-pos
481 (goto-char final-target-pos)
486 (notmuch-hello-goto-search)))))
489 (defun notmuch-folder ()
490 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
496 (provide 'notmuch-hello)