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 (defvar notmuch-hello-url "http://notmuchmail.org"
69 "The `notmuch' web site.")
71 (defvar notmuch-hello-recent-searches nil)
73 (defun notmuch-hello-remember-search (search)
74 (if (not (member search notmuch-hello-recent-searches))
75 (push search notmuch-hello-recent-searches))
76 (if (> (length notmuch-hello-recent-searches)
77 notmuch-recent-searches-max)
78 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
80 (defun notmuch-hello-trim (search)
82 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
83 (match-string 1 search)
86 (defun notmuch-hello-search (search)
87 (let ((search (notmuch-hello-trim search)))
88 (notmuch-hello-remember-search search)
89 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
91 (defun notmuch-hello-add-saved-search (widget)
93 (let ((search (widget-value
95 (widget-get widget :notmuch-saved-search-widget))))
96 (name (completing-read "Name for saved search: "
97 notmuch-saved-searches)))
98 ;; If an existing saved search with this name exists, remove it.
99 (setq notmuch-saved-searches
100 (loop for elem in notmuch-saved-searches
105 (customize-save-variable 'notmuch-saved-searches
106 (push (cons name search)
107 notmuch-saved-searches))
108 (message "Saved '%s' as '%s'." search name)
109 (notmuch-hello-update)))
111 (defun notmuch-hello-longest-label (tag-alist)
112 (or (loop for elem in tag-alist
113 maximize (length (car elem)))
116 (defun notmuch-hello-roundup (dividend divisor)
117 "Return the rounded up value of dividing `dividend' by `divisor'."
118 (+ (/ dividend divisor)
119 (if (> (% dividend divisor) 0) 1 0)))
121 (defun notmuch-hello-reflect (list width)
122 "Reflect a `width' wide matrix represented by `list' along the
125 (let* ((len (length list))
126 (nrows (notmuch-hello-roundup len width)))
127 (loop for row from 0 to (- nrows 1)
128 append (loop for col from 0 to (- width 1)
129 ;; How could we calculate the offset just once
131 if (< (+ (* nrows col) row) len)
132 collect (nth (+ (* nrows col) row) list)
134 ;; Don't forget to insert an empty slot in the
135 ;; output matrix if there is no corresponding
136 ;; value in the input matrix.
139 (defun notmuch-hello-widget-search (widget &rest ignore)
140 (notmuch-search (widget-get widget
141 :notmuch-search-terms)
142 notmuch-search-oldest-first
143 nil nil #'notmuch-hello-search-continuation))
145 (defun notmuch-hello-insert-tags (tag-alist widest target)
146 (let* ((tags-per-line (max 1
147 (/ (- (window-width) notmuch-hello-indent)
148 ;; Count is 7 wide, 1 for the space
152 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
153 ;; Hack the display of the buttons used.
154 (widget-push-button-prefix "")
155 (widget-push-button-suffix "")
156 (found-target-pos nil))
157 ;; dme: It feels as though there should be a better way to
158 ;; implement this loop than using an incrementing counter.
159 (loop for elem in reordered-list
161 ;; (not elem) indicates an empty slot in the matrix.
163 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
164 (if (string= (format "%s " (car elem)) target)
165 (setq found-target-pos (point-marker)))
166 (widget-create 'push-button
167 :notify #'notmuch-hello-widget-search
168 :notmuch-search-terms (cdr elem)
169 (format "%s " (car elem)))
170 (insert (make-string (1+ (- widest (length (car elem)))) ? )))
171 (setq count (1+ count))
172 (if (eq (% count tags-per-line) 0)
173 (widget-insert "\n"))))
175 ;; If the last line was not full (and hence did not include a
176 ;; carriage return), insert one now.
177 (if (not (eq (% count tags-per-line) 0))
178 (widget-insert "\n"))
181 (defun notmuch-hello-goto-search ()
182 "Put point inside the `search' widget."
184 (goto-char notmuch-hello-search-bar-marker))
186 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
188 (defun notmuch-hello-search-continuation()
189 (notmuch-hello-update t))
191 (defun notmuch-hello-update (&optional no-display)
192 ;; Lazy - rebuild everything.
194 (notmuch-hello no-display))
196 (defun notmuch-hello-poll-and-update ()
197 "Invoke `notmuch-poll' to import mail, then refresh the current view."
200 (notmuch-hello-update))
202 (defun notmuch-hello (&optional no-display)
205 ; Jump through a hoop to get this value from the deprecated variable
206 ; name (`notmuch-folders') or from the default value.
207 (if (not notmuch-saved-searches)
208 (setq notmuch-saved-searches (notmuch-saved-searches)))
211 (set-buffer "*notmuch-hello*")
212 (switch-to-buffer "*notmuch-hello*"))
214 (let ((target (if (widget-at)
215 (widget-value (widget-at))
219 (widget-value (widget-at)))
222 (kill-all-local-variables)
223 (let ((inhibit-read-only t))
226 (let ((all (overlay-lists)))
227 ;; Delete all the overlays.
228 (mapc 'delete-overlay (car all))
229 (mapc 'delete-overlay (cdr all)))
231 (when notmuch-show-logo
232 (let ((image notmuch-hello-logo))
233 ;; The notmuch logo uses transparency. That can display poorly
234 ;; when inserting the image into an emacs buffer (black logo on
235 ;; a black background), so force the background colour of the
236 ;; image. We use a face to represent the colour so that
237 ;; `defface' can be used to declare the different possible
238 ;; colours, which depend on whether the frame has a light or
240 (setq image (cons 'image
242 (list :background (face-background 'notmuch-hello-logo-background)))))
243 (insert-image image))
246 (widget-insert "Welcome to ")
247 ;; Hack the display of the links used.
248 (let ((widget-link-prefix "")
249 (widget-link-suffix ""))
251 :notify (lambda (&rest ignore)
252 (browse-url notmuch-hello-url))
253 :help-echo "Visit the notmuch website."
256 (widget-insert "You have ")
258 :notify (lambda (&rest ignore)
259 (notmuch-hello-update))
261 (car (process-lines notmuch-command "count")))
262 (widget-insert " messages (that's not much mail).\n\n"))
264 (let ((found-target-pos nil)
265 (final-target-pos nil))
267 ;; Filter out empty saved seaches if required.
268 (if notmuch-show-empty-saved-searches
269 notmuch-saved-searches
270 (loop for elem in notmuch-saved-searches
271 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
273 (saved-widest (notmuch-hello-longest-label saved-alist))
274 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
275 (process-lines notmuch-command "search-tags")))
276 (alltags-widest (notmuch-hello-longest-label alltags-alist))
277 (widest (max saved-widest alltags-widest)))
280 (widget-insert "Saved searches: ")
281 (widget-create 'push-button
282 :notify (lambda (&rest ignore)
283 (customize-variable 'notmuch-saved-searches))
285 (widget-insert "\n\n")
286 (setq final-target-pos (point-marker))
287 (let ((start (point)))
288 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
290 (setq final-target-pos found-target-pos))
291 (indent-rigidly start (point) notmuch-hello-indent)))
293 (let ((start (point)))
294 (widget-insert "\nSearch: ")
295 (setq notmuch-hello-search-bar-marker (point-marker))
296 (widget-create 'editable-field
297 ;; Leave some space at the start and end of the
299 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
300 (length "Search: ")))
301 :action (lambda (widget &rest ignore)
302 (notmuch-hello-search (widget-value widget))))
304 (indent-rigidly start (point) notmuch-hello-indent))
306 (when notmuch-hello-recent-searches
307 (widget-insert "\nRecent searches: ")
308 (widget-create 'push-button
309 :notify (lambda (&rest ignore)
310 (setq notmuch-hello-recent-searches nil)
311 (notmuch-hello-update))
313 (widget-insert "\n\n")
314 (let ((start (point))
316 (mapc '(lambda (search)
317 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
319 (widget-create 'editable-field
320 ;; Don't let the search boxes be
321 ;; less than 8 characters wide.
328 (* 2 notmuch-hello-indent)
331 ;; `[save]' button. 6
335 :action (lambda (widget &rest ignore)
336 (notmuch-hello-search (widget-value widget)))
339 (widget-create 'push-button
340 :notify (lambda (widget &rest ignore)
341 (notmuch-hello-add-saved-search widget))
342 :notmuch-saved-search-widget widget-symbol
346 notmuch-hello-recent-searches)
347 (indent-rigidly start (point) notmuch-hello-indent)))
350 (if notmuch-show-all-tags-list
352 (widget-insert "\nAll tags: ")
353 (widget-create 'push-button
354 :notify (lambda (widget &rest ignore)
355 (setq notmuch-show-all-tags-list nil)
356 (notmuch-hello-update))
358 (widget-insert "\n\n")
359 (let ((start (point)))
360 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
361 (if (not final-target-pos)
362 (setq final-target-pos found-target-pos))
363 (indent-rigidly start (point) notmuch-hello-indent)))
365 (widget-create 'push-button
366 :notify (lambda (widget &rest ignore)
367 (setq notmuch-show-all-tags-list t)
368 (notmuch-hello-update))
371 (let ((start (point)))
372 (widget-insert "\n\n")
373 (widget-insert "Type a search query and hit RET to view matching threads.\n")
374 (when notmuch-hello-recent-searches
375 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
376 (widget-insert "Save recent searches with the `save' button.\n"))
377 (when notmuch-saved-searches
378 (widget-insert "Edit saved searches with the `edit' button.\n"))
379 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
380 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
381 (let ((fill-column (- (window-width) notmuch-hello-indent)))
382 (center-region start (point))))
384 (use-local-map widget-keymap)
385 (local-set-key "=" 'notmuch-hello-update)
386 (local-set-key "G" 'notmuch-hello-poll-and-update)
387 (local-set-key "m" 'notmuch-mua-mail)
388 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
389 (local-set-key "s" 'notmuch-hello-goto-search)
390 (local-set-key "v" '(lambda () (interactive)
391 (message "notmuch version %s" (notmuch-version))))
395 (goto-char final-target-pos)
396 (if (not (widget-at))
397 (widget-forward 1)))))
401 (provide 'notmuch-hello)