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-roundup (dividend divisor)
143 "Return the rounded up value of dividing `dividend' by `divisor'."
144 (+ (/ dividend divisor)
145 (if (> (% dividend divisor) 0) 1 0)))
147 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
148 (let ((len (length list)))
149 (loop for col from 0 to (- ncols 1)
150 collect (let ((offset (+ (* nrows col) row)))
153 ;; Don't forget to insert an empty slot in the
154 ;; output matrix if there is no corresponding
155 ;; value in the input matrix.
158 (defun notmuch-hello-reflect (list ncols)
159 "Reflect a `ncols' wide matrix represented by `list' along the
162 (let ((nrows (notmuch-hello-roundup (length list) ncols)))
163 (loop for row from 0 to (- nrows 1)
164 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
166 (defun notmuch-hello-widget-search (widget &rest ignore)
167 (notmuch-search (widget-get widget
168 :notmuch-search-terms)
169 notmuch-search-oldest-first
170 nil nil #'notmuch-hello-search-continuation))
172 (defun notmuch-saved-search-count (search)
173 (car (process-lines notmuch-command "count" search)))
175 (defun notmuch-hello-tags-per-line (widest)
176 "Determine how many tags to show per line and how wide they
177 should be. Returns a cons cell `(tags-per-line width)'."
180 ((integerp notmuch-column-control)
182 (/ (- (window-width) notmuch-hello-indent)
183 ;; Count is 7 wide (6 digits plus space), 1 for the space
185 (+ 7 1 (max notmuch-column-control widest)))))
187 ((floatp notmuch-column-control)
188 (let* ((available-width (- (window-width) notmuch-hello-indent))
189 (proposed-width (max (* available-width notmuch-column-control) widest)))
190 (floor available-width proposed-width)))
194 (/ (- (window-width) notmuch-hello-indent)
195 ;; Count is 7 wide (6 digits plus space), 1 for the space
199 (cons tags-per-line (/ (- (window-width) notmuch-hello-indent
200 (* tags-per-line (+ 7 1)))
203 (defun notmuch-hello-insert-tags (tag-alist widest target)
204 (let* ((tags-and-width (notmuch-hello-tags-per-line widest))
205 (tags-per-line (car tags-and-width))
206 (widest (cdr tags-and-width))
208 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
209 ;; Hack the display of the buttons used.
210 (widget-push-button-prefix "")
211 (widget-push-button-suffix "")
212 (found-target-pos nil))
213 ;; dme: It feels as though there should be a better way to
214 ;; implement this loop than using an incrementing counter.
216 ;; (not elem) indicates an empty slot in the matrix.
218 (let* ((name (car elem))
220 (formatted-name (format "%s " name)))
221 (widget-insert (format "%6s " (notmuch-saved-search-count query)))
222 (if (string= formatted-name target)
223 (setq found-target-pos (point-marker)))
224 (widget-create 'push-button
225 :notify #'notmuch-hello-widget-search
226 :notmuch-search-terms query
228 ;; Insert enough space to consume the rest of the
229 ;; column. Because the button for the name is `(1+
230 ;; (length name))' long (due to the trailing space) we
231 ;; can just insert `(- widest (length name))' spaces -
232 ;; the column separator is included in the button if
233 ;; `(equal widest (length name)'.
234 (widget-insert (make-string (- widest (length name)) ? ))))
235 (setq count (1+ count))
236 (if (eq (% count tags-per-line) 0)
237 (widget-insert "\n")))
240 ;; If the last line was not full (and hence did not include a
241 ;; carriage return), insert one now.
242 (if (not (eq (% count tags-per-line) 0))
243 (widget-insert "\n"))
246 (defun notmuch-hello-goto-search ()
247 "Put point inside the `search' widget."
249 (goto-char notmuch-hello-search-bar-marker))
251 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
253 (defun notmuch-hello-search-continuation()
254 (notmuch-hello-update t))
256 (defun notmuch-hello-update (&optional no-display)
257 ;; Lazy - rebuild everything.
259 (notmuch-hello no-display))
261 (defun notmuch-hello-poll-and-update ()
262 "Invoke `notmuch-poll' to import mail, then refresh the current view."
265 (notmuch-hello-update))
267 (defun notmuch-hello (&optional no-display)
270 ; Jump through a hoop to get this value from the deprecated variable
271 ; name (`notmuch-folders') or from the default value.
272 (if (not notmuch-saved-searches)
273 (setq notmuch-saved-searches (notmuch-saved-searches)))
276 (set-buffer "*notmuch-hello*")
277 (switch-to-buffer "*notmuch-hello*"))
279 (let ((target (if (widget-at)
280 (widget-value (widget-at))
284 (widget-value (widget-at)))
287 (kill-all-local-variables)
288 (let ((inhibit-read-only t))
291 (let ((all (overlay-lists)))
292 ;; Delete all the overlays.
293 (mapc 'delete-overlay (car all))
294 (mapc 'delete-overlay (cdr all)))
296 (when notmuch-show-logo
297 (let ((image notmuch-hello-logo))
298 ;; The notmuch logo uses transparency. That can display poorly
299 ;; when inserting the image into an emacs buffer (black logo on
300 ;; a black background), so force the background colour of the
301 ;; image. We use a face to represent the colour so that
302 ;; `defface' can be used to declare the different possible
303 ;; colours, which depend on whether the frame has a light or
305 (setq image (cons 'image
307 (list :background (face-background 'notmuch-hello-logo-background)))))
308 (insert-image image))
311 (widget-insert "Welcome to ")
312 ;; Hack the display of the links used.
313 (let ((widget-link-prefix "")
314 (widget-link-suffix ""))
316 :notify (lambda (&rest ignore)
317 (browse-url notmuch-hello-url))
318 :help-echo "Visit the notmuch website."
321 (widget-insert "You have ")
323 :notify (lambda (&rest ignore)
324 (notmuch-hello-update))
326 (car (process-lines notmuch-command "count")))
327 (widget-insert " messages (that's not much mail).\n"))
329 (let ((found-target-pos nil)
330 (final-target-pos nil))
332 ;; Filter out empty saved seaches if required.
333 (if notmuch-show-empty-saved-searches
334 notmuch-saved-searches
335 (loop for elem in notmuch-saved-searches
336 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
338 (saved-widest (notmuch-hello-longest-label saved-alist))
339 (alltags-alist (if notmuch-show-all-tags-list
340 (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
341 (process-lines notmuch-command "search-tags"))))
342 (alltags-widest (notmuch-hello-longest-label alltags-alist))
343 (widest (max saved-widest alltags-widest)))
346 (widget-insert "\nSaved searches: ")
347 (widget-create 'push-button
348 :notify (lambda (&rest ignore)
349 (customize-variable 'notmuch-saved-searches))
351 (widget-insert "\n\n")
352 (setq final-target-pos (point-marker))
353 (let ((start (point)))
354 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
356 (setq final-target-pos found-target-pos))
357 (indent-rigidly start (point) notmuch-hello-indent)))
359 (widget-insert "\nSearch: ")
360 (setq notmuch-hello-search-bar-marker (point-marker))
361 (widget-create 'editable-field
362 ;; Leave some space at the start and end of the
364 :size (max 8 (- (window-width) notmuch-hello-indent
365 (length "Search: ")))
366 :action (lambda (widget &rest ignore)
367 (notmuch-hello-search (widget-value widget))))
370 (when notmuch-hello-recent-searches
371 (widget-insert "\nRecent searches: ")
372 (widget-create 'push-button
373 :notify (lambda (&rest ignore)
374 (setq notmuch-hello-recent-searches nil)
375 (notmuch-hello-update))
377 (widget-insert "\n\n")
378 (let ((start (point))
380 (mapc '(lambda (search)
381 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
383 (widget-create 'editable-field
384 ;; Don't let the search boxes be
385 ;; less than 8 characters wide.
392 (* 2 notmuch-hello-indent)
395 ;; `[save]' button. 6
399 :action (lambda (widget &rest ignore)
400 (notmuch-hello-search (widget-value widget)))
403 (widget-create 'push-button
404 :notify (lambda (widget &rest ignore)
405 (notmuch-hello-add-saved-search widget))
406 :notmuch-saved-search-widget widget-symbol
410 notmuch-hello-recent-searches)
411 (indent-rigidly start (point) notmuch-hello-indent)))
414 (widget-insert "\nAll tags: ")
415 (widget-create 'push-button
416 :notify (lambda (widget &rest ignore)
417 (setq notmuch-show-all-tags-list nil)
418 (notmuch-hello-update))
420 (widget-insert "\n\n")
421 (let ((start (point)))
422 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
423 (if (not final-target-pos)
424 (setq final-target-pos found-target-pos))
425 (indent-rigidly start (point) notmuch-hello-indent)))
429 (if (not notmuch-show-all-tags-list)
430 (widget-create 'push-button
431 :notify (lambda (widget &rest ignore)
432 (setq notmuch-show-all-tags-list t)
433 (notmuch-hello-update))
436 (let ((start (point)))
437 (widget-insert "\n\n")
438 (widget-insert "Type a search query and hit RET to view matching threads.\n")
439 (when notmuch-hello-recent-searches
440 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
441 (widget-insert "Save recent searches with the `save' button.\n"))
442 (when notmuch-saved-searches
443 (widget-insert "Edit saved searches with the `edit' button.\n"))
444 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
445 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
446 (let ((fill-column (- (window-width) notmuch-hello-indent)))
447 (center-region start (point))))
449 (use-local-map widget-keymap)
450 (local-set-key "=" 'notmuch-hello-update)
451 (local-set-key (kbd "<C-tab>") 'widget-backward)
452 (local-set-key "G" 'notmuch-hello-poll-and-update)
453 (local-set-key "m" 'notmuch-mua-mail)
454 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
455 (local-set-key "s" 'notmuch-hello-goto-search)
456 (local-set-key "v" '(lambda () (interactive)
457 (message "notmuch version %s" (notmuch-version))))
461 (when final-target-pos
462 (goto-char final-target-pos)
467 (notmuch-hello-goto-search)))))
470 (defun notmuch-folder ()
471 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
477 (provide 'notmuch-hello)