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 ;; Lazy - rebuild everything.
254 (notmuch-hello no-display))
256 (defun notmuch-hello-poll-and-update ()
257 "Invoke `notmuch-poll' to import mail, then refresh the current view."
260 (notmuch-hello-update))
262 (defun notmuch-hello (&optional no-display)
265 ; Jump through a hoop to get this value from the deprecated variable
266 ; name (`notmuch-folders') or from the default value.
267 (if (not notmuch-saved-searches)
268 (setq notmuch-saved-searches (notmuch-saved-searches)))
271 (set-buffer "*notmuch-hello*")
272 (switch-to-buffer "*notmuch-hello*"))
274 (let ((target (if (widget-at)
275 (widget-value (widget-at))
279 (widget-value (widget-at)))
282 (kill-all-local-variables)
283 (let ((inhibit-read-only t))
286 (let ((all (overlay-lists)))
287 ;; Delete all the overlays.
288 (mapc 'delete-overlay (car all))
289 (mapc 'delete-overlay (cdr all)))
291 (when notmuch-show-logo
292 (let ((image notmuch-hello-logo))
293 ;; The notmuch logo uses transparency. That can display poorly
294 ;; when inserting the image into an emacs buffer (black logo on
295 ;; a black background), so force the background colour of the
296 ;; image. We use a face to represent the colour so that
297 ;; `defface' can be used to declare the different possible
298 ;; colours, which depend on whether the frame has a light or
300 (setq image (cons 'image
302 (list :background (face-background 'notmuch-hello-logo-background)))))
303 (insert-image image))
306 (widget-insert "Welcome to ")
307 ;; Hack the display of the links used.
308 (let ((widget-link-prefix "")
309 (widget-link-suffix ""))
311 :notify (lambda (&rest ignore)
312 (browse-url notmuch-hello-url))
313 :help-echo "Visit the notmuch website."
316 (widget-insert "You have ")
318 :notify (lambda (&rest ignore)
319 (notmuch-hello-update))
321 (car (process-lines notmuch-command "count")))
322 (widget-insert " messages (that's not much mail).\n"))
324 (let ((found-target-pos nil)
325 (final-target-pos nil))
327 ;; Filter out empty saved seaches if required.
328 (if notmuch-show-empty-saved-searches
329 notmuch-saved-searches
330 (loop for elem in notmuch-saved-searches
331 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
333 (saved-widest (notmuch-hello-longest-label saved-alist))
334 (alltags-alist (if notmuch-show-all-tags-list
335 (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
336 (process-lines notmuch-command "search-tags"))))
337 (alltags-widest (notmuch-hello-longest-label alltags-alist))
338 (widest (max saved-widest alltags-widest)))
341 (widget-insert "\nSaved searches: ")
342 (widget-create 'push-button
343 :notify (lambda (&rest ignore)
344 (customize-variable 'notmuch-saved-searches))
346 (widget-insert "\n\n")
347 (setq final-target-pos (point-marker))
348 (let ((start (point)))
349 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
351 (setq final-target-pos found-target-pos))
352 (indent-rigidly start (point) notmuch-hello-indent)))
354 (widget-insert "\nSearch: ")
355 (setq notmuch-hello-search-bar-marker (point-marker))
356 (widget-create 'editable-field
357 ;; Leave some space at the start and end of the
359 :size (max 8 (- (window-width) notmuch-hello-indent
360 (length "Search: ")))
361 :action (lambda (widget &rest ignore)
362 (notmuch-hello-search (widget-value widget))))
365 (when notmuch-hello-recent-searches
366 (widget-insert "\nRecent searches: ")
367 (widget-create 'push-button
368 :notify (lambda (&rest ignore)
369 (setq notmuch-hello-recent-searches nil)
370 (notmuch-hello-update))
372 (widget-insert "\n\n")
373 (let ((start (point))
375 (mapc '(lambda (search)
376 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
378 (widget-create 'editable-field
379 ;; Don't let the search boxes be
380 ;; less than 8 characters wide.
387 (* 2 notmuch-hello-indent)
390 ;; `[save]' button. 6
394 :action (lambda (widget &rest ignore)
395 (notmuch-hello-search (widget-value widget)))
398 (widget-create 'push-button
399 :notify (lambda (widget &rest ignore)
400 (notmuch-hello-add-saved-search widget))
401 :notmuch-saved-search-widget widget-symbol
405 notmuch-hello-recent-searches)
406 (indent-rigidly start (point) notmuch-hello-indent)))
409 (widget-insert "\nAll tags: ")
410 (widget-create 'push-button
411 :notify (lambda (widget &rest ignore)
412 (setq notmuch-show-all-tags-list nil)
413 (notmuch-hello-update))
415 (widget-insert "\n\n")
416 (let ((start (point)))
417 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
418 (if (not final-target-pos)
419 (setq final-target-pos found-target-pos))
420 (indent-rigidly start (point) notmuch-hello-indent)))
424 (if (not notmuch-show-all-tags-list)
425 (widget-create 'push-button
426 :notify (lambda (widget &rest ignore)
427 (setq notmuch-show-all-tags-list t)
428 (notmuch-hello-update))
431 (let ((start (point)))
432 (widget-insert "\n\n")
433 (widget-insert "Type a search query and hit RET to view matching threads.\n")
434 (when notmuch-hello-recent-searches
435 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
436 (widget-insert "Save recent searches with the `save' button.\n"))
437 (when notmuch-saved-searches
438 (widget-insert "Edit saved searches with the `edit' button.\n"))
439 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
440 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
441 (let ((fill-column (- (window-width) notmuch-hello-indent)))
442 (center-region start (point))))
444 (use-local-map widget-keymap)
445 (local-set-key "=" 'notmuch-hello-update)
446 (local-set-key (kbd "<C-tab>") 'widget-backward)
447 (local-set-key "G" 'notmuch-hello-poll-and-update)
448 (local-set-key "m" 'notmuch-mua-mail)
449 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
450 (local-set-key "s" 'notmuch-hello-goto-search)
451 (local-set-key "v" '(lambda () (interactive)
452 (message "notmuch version %s" (notmuch-version))))
456 (when final-target-pos
457 (goto-char final-target-pos)
462 (notmuch-hello-goto-search)))))
465 (defun notmuch-folder ()
466 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
472 (provide 'notmuch-hello)