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-folder-count "notmuch" (search))
32 (defcustom notmuch-hello-recent-searches-max 10
33 "The number of recent searches to store and display."
37 (defcustom notmuch-hello-show-empty-saved-searches nil
38 "Should saved searches with no messages be listed?"
42 (defcustom notmuch-hello-indent 4
43 "How much to indent non-headers."
47 (defcustom notmuch-hello-saved-searches notmuch-folders
48 "A list of saved searches to display."
49 :type '(alist :key-type string :value-type string)
52 (defcustom notmuch-hello-show-logo t
53 "Should the notmuch logo be shown?"
57 (defface notmuch-hello-logo-background
60 (:background "#5f5f5f"))
63 (:background "white")))
64 "Background colour for the notmuch logo."
67 (defcustom notmuch-hello-jump-to-search t
68 "Whether `notmuch-hello' should always jump to the search
73 (defvar notmuch-hello-url "http://notmuchmail.org"
74 "The `notmuch' web site.")
76 (defvar notmuch-hello-recent-searches nil)
78 (defun notmuch-hello-remember-search (search)
79 (if (not (member search notmuch-hello-recent-searches))
80 (push search notmuch-hello-recent-searches))
81 (if (> (length notmuch-hello-recent-searches)
82 notmuch-hello-recent-searches-max)
83 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
85 (defun notmuch-hello-trim (search)
87 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
88 (match-string 1 search)
91 (defun notmuch-hello-search (search)
92 (let ((search (notmuch-hello-trim search)))
93 (notmuch-hello-remember-search search)
94 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
96 (defun notmuch-hello-add-saved-search (widget)
98 (let ((search (widget-value
100 (widget-get widget :notmuch-saved-search-widget))))
101 (name (completing-read "Name for saved search: "
102 notmuch-hello-saved-searches)))
103 ;; If an existing saved search with this name exists, remove it.
104 (setq notmuch-hello-saved-searches
105 (loop for elem in notmuch-hello-saved-searches
110 (customize-save-variable 'notmuch-hello-saved-searches
111 (push (cons name search)
112 notmuch-hello-saved-searches))
113 (message "Saved '%s' as '%s'." search name)
114 (notmuch-hello-update)))
116 (defun notmuch-hello-longest-label (tag-alist)
117 (or (loop for elem in tag-alist
118 maximize (length (car elem)))
121 (defun notmuch-hello-roundup (dividend divisor)
122 "Return the rounded up value of dividing `dividend' by `divisor'."
123 (+ (/ dividend divisor)
124 (if (> (% dividend divisor) 0) 1 0)))
126 (defun notmuch-hello-reflect (list width)
127 "Reflect a `width' wide matrix represented by `list' along the
130 (let* ((len (length list))
131 (nrows (notmuch-hello-roundup len width)))
132 (loop for row from 0 to (- nrows 1)
133 append (loop for col from 0 to (- width 1)
134 ;; How could we calculate the offset just once
136 if (< (+ (* nrows col) row) len)
137 collect (nth (+ (* nrows col) row) list)
139 ;; Don't forget to insert an empty slot in the
140 ;; output matrix if there is no corresponding
141 ;; value in the input matrix.
144 (defun notmuch-hello-widget-search (widget &rest ignore)
145 (notmuch-search (widget-get widget
146 :notmuch-search-terms)
147 notmuch-search-oldest-first
148 nil nil #'notmuch-hello-search-continuation))
150 (defun notmuch-hello-insert-tags (tag-alist widest)
151 (let* ((tag-format-string (format "%%-%ds " widest))
152 (tags-per-line (max 1
153 (/ (- (window-width) notmuch-hello-indent)
154 ;; Count is 7 wide, 1 for the space
158 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
159 ;; Hack the display of the buttons used.
160 (widget-push-button-prefix "")
161 (widget-push-button-suffix ""))
162 ;; dme: It feels as though there should be a better way to
163 ;; implement this loop than using an incrementing counter.
164 (loop for elem in reordered-list
166 ;; (not elem) indicates an empty slot in the matrix.
168 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
169 (widget-create 'push-button
170 :notify #'notmuch-hello-widget-search
171 :notmuch-search-terms (cdr elem)
172 (format tag-format-string (car elem))))
173 (setq count (1+ count))
174 (if (eq (% count tags-per-line) 0)
175 (widget-insert "\n"))))
177 ;; If the last line was not full (and hence did not include a
178 ;; carriage return), insert one now.
179 (if (not (eq (% count tags-per-line) 0))
180 (widget-insert "\n"))))
182 (defun notmuch-hello-goto-search ()
183 "Put point inside the `search' widget, which we know is first."
185 (goto-char (point-min))
188 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
190 (defun notmuch-hello-search-continuation()
193 (defun notmuch-hello-update (&optional no-display)
194 ;; Lazy - rebuild everything.
196 (notmuch-hello no-display))
198 (defun notmuch-hello (&optional no-display)
202 (set-buffer "*notmuch-hello*")
203 (switch-to-buffer "*notmuch-hello*"))
205 (kill-all-local-variables)
206 (let ((inhibit-read-only t))
209 (let ((all (overlay-lists)))
210 ;; Delete all the overlays.
211 (mapc 'delete-overlay (car all))
212 (mapc 'delete-overlay (cdr all)))
214 (when notmuch-hello-show-logo
215 (let ((image notmuch-hello-logo))
216 ;; The notmuch logo uses transparency. That can display poorly
217 ;; when inserting the image into an emacs buffer (black logo on
218 ;; a black background), so force the background colour of the
219 ;; image. We use a face to represent the colour so that
220 ;; `defface' can be used to declare the different possible
221 ;; colours, which depend on whether the frame has a light or
223 (setq image (cons 'image
225 (list :background (face-background 'notmuch-hello-logo-background)))))
226 (insert-image image))
229 (widget-insert "Welcome to ")
230 ;; Hack the display of the links used.
231 (let ((widget-link-prefix "")
232 (widget-link-suffix ""))
234 :notify (lambda (&rest ignore)
235 (browse-url notmuch-hello-url))
236 :help-echo "Visit the notmuch website."
239 (widget-insert "You have ")
241 :notify (lambda (&rest ignore)
242 (notmuch-hello-update))
244 (car (process-lines notmuch-command "count")))
245 (widget-insert " messages (that's not much mail).\n\n"))
247 (let ((start (point)))
248 (widget-insert "Search: ")
249 (widget-create 'editable-field
250 ;; Leave some space at the start and end of the
252 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
253 (length "Search: ")))
254 :action (lambda (widget &rest ignore)
255 (notmuch-hello-search (widget-value widget))))
257 (indent-rigidly start (point) notmuch-hello-indent))
259 (when notmuch-hello-recent-searches
260 (widget-insert "\nRecent searches: ")
261 (widget-create 'push-button
262 :notify (lambda (&rest ignore)
263 (setq notmuch-hello-recent-searches nil)
264 (notmuch-hello-update))
266 (widget-insert "\n\n")
267 (let ((start (point))
269 (mapc '(lambda (search)
270 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
272 (widget-create 'editable-field
273 ;; Don't let the search boxes be
274 ;; less than 8 characters wide.
281 (* 2 notmuch-hello-indent)
284 ;; `[save]' button. 6
288 :action (lambda (widget &rest ignore)
289 (notmuch-hello-search (widget-value widget)))
292 (widget-create 'push-button
293 :notify (lambda (widget &rest ignore)
294 (notmuch-hello-add-saved-search widget))
295 :notmuch-saved-search-widget widget-symbol
299 notmuch-hello-recent-searches)
300 (indent-rigidly start (point) notmuch-hello-indent)))
303 ;; Filter out empty saved seaches if required.
304 (if notmuch-hello-show-empty-saved-searches
305 notmuch-hello-saved-searches
306 (loop for elem in notmuch-hello-saved-searches
307 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
309 (saved-widest (notmuch-hello-longest-label saved-alist))
310 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
311 (process-lines notmuch-command "search-tags")))
312 (alltags-widest (notmuch-hello-longest-label alltags-alist))
313 (widest (max saved-widest alltags-widest)))
316 (widget-insert "\nSaved searches: ")
317 (widget-create 'push-button
318 :notify (lambda (&rest ignore)
319 (customize-variable 'notmuch-hello-saved-searches))
321 (widget-insert "\n\n")
322 (let ((start (point)))
323 (notmuch-hello-insert-tags saved-alist widest)
324 (indent-rigidly start (point) notmuch-hello-indent)))
327 (widget-insert "\nAll tags:\n\n")
328 (let ((start (point)))
329 (notmuch-hello-insert-tags alltags-alist widest)
330 (indent-rigidly start (point) notmuch-hello-indent))))
332 (let ((start (point)))
333 (widget-insert "\n\n")
334 (widget-insert "Type a search query and hit RET to view matching threads.\n")
335 (when notmuch-hello-recent-searches
336 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
337 (widget-insert "Save recent searches with the `save' button.\n"))
338 (when notmuch-hello-saved-searches
339 (widget-insert "Edit saved searches with the `edit' button.\n"))
340 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
341 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
342 (let ((fill-column (- (window-width) notmuch-hello-indent)))
343 (center-region start (point))))
345 (use-local-map widget-keymap)
346 (local-set-key "=" 'notmuch-hello-update)
347 (local-set-key "m" 'notmuch-mua-mail)
348 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
349 (local-set-key "s" 'notmuch-hello-goto-search)
350 (local-set-key "v" '(lambda () (interactive)
351 (message "notmuch version %s" (notmuch-version))))
355 (if notmuch-hello-jump-to-search
356 (notmuch-hello-goto-search)
357 (goto-char (point-min))))
361 (provide 'notmuch-hello)