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)
28 (require 'notmuch-mua)
30 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
31 (declare-function notmuch-folder-count "notmuch" (search))
33 (defcustom notmuch-hello-recent-searches-max 10
34 "The number of recent searches to store and display."
38 (defcustom notmuch-hello-show-empty-saved-searches nil
39 "Should saved searches with no messages be listed?"
43 (defcustom notmuch-hello-indent 4
44 "How much to indent non-headers."
48 (defcustom notmuch-hello-saved-searches notmuch-folders
49 "A list of saved searches to display."
50 :type '(alist :key-type string :value-type string)
53 (defcustom notmuch-hello-show-logo t
54 "Should the notmuch logo be shown?"
58 (defface notmuch-hello-logo-background
61 (:background "#5f5f5f"))
64 (:background "white")))
65 "Background colour for the notmuch logo."
68 (defcustom notmuch-hello-jump-to-search t
69 "Whether `notmuch-hello' should always jump to the search
74 (defvar notmuch-hello-url "http://notmuchmail.org"
75 "The `notmuch' web site.")
77 (defvar notmuch-hello-recent-searches nil)
79 (defun notmuch-hello-remember-search (search)
80 (if (not (member search notmuch-hello-recent-searches))
81 (push search notmuch-hello-recent-searches))
82 (if (> (length notmuch-hello-recent-searches)
83 notmuch-hello-recent-searches-max)
84 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
86 (defun notmuch-hello-trim (search)
88 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
89 (match-string 1 search)
92 (defun notmuch-hello-search (search)
93 (let ((search (notmuch-hello-trim search)))
94 (notmuch-hello-remember-search search)
95 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
97 (defun notmuch-hello-add-saved-search (widget)
99 (let ((search (widget-value
101 (widget-get widget :notmuch-saved-search-widget))))
102 (name (completing-read "Name for saved search: "
103 notmuch-hello-saved-searches)))
104 ;; If an existing saved search with this name exists, remove it.
105 (setq notmuch-hello-saved-searches
106 (loop for elem in notmuch-hello-saved-searches
111 (customize-save-variable 'notmuch-hello-saved-searches
112 (push (cons name search)
113 notmuch-hello-saved-searches))
114 (message "Saved '%s' as '%s'." search name)
115 (notmuch-hello-update)))
117 (defun notmuch-hello-longest-label (tag-alist)
118 (or (loop for elem in tag-alist
119 maximize (length (car elem)))
122 (defun notmuch-hello-roundup (dividend divisor)
123 "Return the rounded up value of dividing `dividend' by `divisor'."
124 (+ (/ dividend divisor)
125 (if (> (% dividend divisor) 0) 1 0)))
127 (defun notmuch-hello-reflect (list width)
128 "Reflect a `width' wide matrix represented by `list' along the
131 (let* ((len (length list))
132 (nrows (notmuch-hello-roundup len width)))
133 (loop for row from 0 to (- nrows 1)
134 append (loop for col from 0 to (- width 1)
135 ;; How could we calculate the offset just once
137 if (< (+ (* nrows col) row) len)
138 collect (nth (+ (* nrows col) row) list)
140 ;; Don't forget to insert an empty slot in the
141 ;; output matrix if there is no corresponding
142 ;; value in the input matrix.
145 (defun notmuch-hello-widget-search (widget &rest ignore)
146 (notmuch-search (widget-get widget
147 :notmuch-search-terms)
148 notmuch-search-oldest-first
149 nil nil #'notmuch-hello-search-continuation))
151 (defun notmuch-hello-insert-tags (tag-alist widest)
152 (let* ((tag-format-string (format "%%-%ds " widest))
153 (tags-per-line (max 1
154 (/ (- (window-width) notmuch-hello-indent)
155 ;; Count is 7 wide, 1 for the space
159 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
160 ;; Hack the display of the buttons used.
161 (widget-push-button-prefix "")
162 (widget-push-button-suffix ""))
163 ;; dme: It feels as though there should be a better way to
164 ;; implement this loop than using an incrementing counter.
165 (loop for elem in reordered-list
167 ;; (not elem) indicates an empty slot in the matrix.
169 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
170 (widget-create 'push-button
171 :notify #'notmuch-hello-widget-search
172 :notmuch-search-terms (cdr elem)
173 (format tag-format-string (car elem))))
174 (setq count (1+ count))
175 (if (eq (% count tags-per-line) 0)
176 (widget-insert "\n"))))
178 ;; If the last line was not full (and hence did not include a
179 ;; carriage return), insert one now.
180 (if (not (eq (% count tags-per-line) 0))
181 (widget-insert "\n"))))
183 (defun notmuch-hello-goto-search ()
184 "Put point inside the `search' widget, which we know is first."
186 (goto-char (point-min))
189 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
191 (defun notmuch-hello-search-continuation()
194 (defun notmuch-hello-update (&optional no-display)
195 ;; Lazy - rebuild everything.
197 (notmuch-hello no-display))
199 (defun notmuch-hello (&optional no-display)
203 (set-buffer "*notmuch-hello*")
204 (switch-to-buffer "*notmuch-hello*"))
206 (kill-all-local-variables)
207 (let ((inhibit-read-only t))
210 (let ((all (overlay-lists)))
211 ;; Delete all the overlays.
212 (mapc 'delete-overlay (car all))
213 (mapc 'delete-overlay (cdr all)))
215 (when notmuch-hello-show-logo
216 (let ((image notmuch-hello-logo))
217 ;; The notmuch logo uses transparency. That can display poorly
218 ;; when inserting the image into an emacs buffer (black logo on
219 ;; a black background), so force the background colour of the
220 ;; image. We use a face to represent the colour so that
221 ;; `defface' can be used to declare the different possible
222 ;; colours, which depend on whether the frame has a light or
224 (setq image (cons 'image
226 (list :background (face-background 'notmuch-hello-logo-background)))))
227 (insert-image image))
230 (widget-insert "Welcome to ")
231 ;; Hack the display of the links used.
232 (let ((widget-link-prefix "")
233 (widget-link-suffix ""))
235 :notify (lambda (&rest ignore)
236 (browse-url notmuch-hello-url))
237 :help-echo "Visit the notmuch website."
240 (widget-insert "You have ")
242 :notify (lambda (&rest ignore)
243 (notmuch-hello-update))
245 (car (process-lines notmuch-command "count")))
246 (widget-insert " messages (that's not much mail).\n\n"))
248 (let ((start (point)))
249 (widget-insert "Search: ")
250 (widget-create 'editable-field
251 ;; Leave some space at the start and end of the
253 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
254 (length "Search: ")))
255 :action (lambda (widget &rest ignore)
256 (notmuch-hello-search (widget-value widget))))
258 (indent-rigidly start (point) notmuch-hello-indent))
260 (when notmuch-hello-recent-searches
261 (widget-insert "\nRecent searches: ")
262 (widget-create 'push-button
263 :notify (lambda (&rest ignore)
264 (setq notmuch-hello-recent-searches nil)
265 (notmuch-hello-update))
267 (widget-insert "\n\n")
268 (let ((start (point))
270 (mapc '(lambda (search)
271 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
273 (widget-create 'editable-field
274 ;; Don't let the search boxes be
275 ;; less than 8 characters wide.
282 (* 2 notmuch-hello-indent)
285 ;; `[save]' button. 6
289 :action (lambda (widget &rest ignore)
290 (notmuch-hello-search (widget-value widget)))
293 (widget-create 'push-button
294 :notify (lambda (widget &rest ignore)
295 (notmuch-hello-add-saved-search widget))
296 :notmuch-saved-search-widget widget-symbol
300 notmuch-hello-recent-searches)
301 (indent-rigidly start (point) notmuch-hello-indent)))
304 ;; Filter out empty saved seaches if required.
305 (if notmuch-hello-show-empty-saved-searches
306 notmuch-hello-saved-searches
307 (loop for elem in notmuch-hello-saved-searches
308 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
310 (saved-widest (notmuch-hello-longest-label saved-alist))
311 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
312 (process-lines notmuch-command "search-tags")))
313 (alltags-widest (notmuch-hello-longest-label alltags-alist))
314 (widest (max saved-widest alltags-widest)))
317 (widget-insert "\nSaved searches: ")
318 (widget-create 'push-button
319 :notify (lambda (&rest ignore)
320 (customize-variable 'notmuch-hello-saved-searches))
322 (widget-insert "\n\n")
323 (let ((start (point)))
324 (notmuch-hello-insert-tags saved-alist widest)
325 (indent-rigidly start (point) notmuch-hello-indent)))
328 (widget-insert "\nAll tags:\n\n")
329 (let ((start (point)))
330 (notmuch-hello-insert-tags alltags-alist widest)
331 (indent-rigidly start (point) notmuch-hello-indent))))
333 (let ((start (point)))
334 (widget-insert "\n\n")
335 (widget-insert "Type a search query and hit RET to view matching threads.\n")
336 (when notmuch-hello-recent-searches
337 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
338 (widget-insert "Save recent searches with the `save' button.\n"))
339 (when notmuch-hello-saved-searches
340 (widget-insert "Edit saved searches with the `edit' button.\n"))
341 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
342 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
343 (let ((fill-column (- (window-width) notmuch-hello-indent)))
344 (center-region start (point))))
346 (use-local-map widget-keymap)
347 (local-set-key "=" 'notmuch-hello-update)
348 (local-set-key "m" 'notmuch-mua-mail)
349 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
350 (local-set-key "s" 'notmuch-hello-goto-search)
351 (local-set-key "v" '(lambda () (interactive)
352 (message "notmuch version %s" (notmuch-version))))
356 (if notmuch-hello-jump-to-search
357 (notmuch-hello-goto-search)
358 (goto-char (point-min))))
362 (provide 'notmuch-hello)