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 (defvar notmuch-hello-url "http://notmuchmail.org"
68 "The `notmuch' web site.")
70 (defvar notmuch-hello-recent-searches nil)
72 (defun notmuch-hello-remember-search (search)
73 (if (not (member search notmuch-hello-recent-searches))
74 (push search notmuch-hello-recent-searches))
75 (if (> (length notmuch-hello-recent-searches)
76 notmuch-hello-recent-searches-max)
77 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
79 (defun notmuch-hello-trim (search)
81 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
82 (match-string 1 search)
85 (defun notmuch-hello-search (search)
86 (let ((search (notmuch-hello-trim search)))
87 (notmuch-hello-remember-search search)
88 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
90 (defun notmuch-hello-add-saved-search (widget)
92 (let ((search (widget-value
94 (widget-get widget :notmuch-saved-search-widget))))
95 (name (completing-read "Name for saved search: "
96 notmuch-hello-saved-searches)))
97 ;; If an existing saved search with this name exists, remove it.
98 (setq notmuch-hello-saved-searches
99 (loop for elem in notmuch-hello-saved-searches
104 (customize-save-variable 'notmuch-hello-saved-searches
105 (push (cons name search)
106 notmuch-hello-saved-searches))
107 (message "Saved '%s' as '%s'." search name)
108 (notmuch-hello-update)))
110 (defun notmuch-hello-longest-label (tag-alist)
111 (or (loop for elem in tag-alist
112 maximize (length (car elem)))
115 (defun notmuch-hello-roundup (dividend divisor)
116 "Return the rounded up value of dividing `dividend' by `divisor'."
117 (+ (/ dividend divisor)
118 (if (> (% dividend divisor) 0) 1 0)))
120 (defun notmuch-hello-reflect (list width)
121 "Reflect a `width' wide matrix represented by `list' along the
124 (let* ((len (length list))
125 (nrows (notmuch-hello-roundup len width)))
126 (loop for row from 0 to (- nrows 1)
127 append (loop for col from 0 to (- width 1)
128 ;; How could we calculate the offset just once
130 if (< (+ (* nrows col) row) len)
131 collect (nth (+ (* nrows col) row) list)
133 ;; Don't forget to insert an empty slot in the
134 ;; output matrix if there is no corresponding
135 ;; value in the input matrix.
138 (defun notmuch-hello-widget-search (widget &rest ignore)
139 (notmuch-search (widget-get widget
140 :notmuch-search-terms)
141 notmuch-search-oldest-first
142 nil nil #'notmuch-hello-search-continuation))
144 (defun notmuch-hello-insert-tags (tag-alist widest target)
145 (let* ((tags-per-line (max 1
146 (/ (- (window-width) notmuch-hello-indent)
147 ;; Count is 7 wide, 1 for the space
151 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
152 ;; Hack the display of the buttons used.
153 (widget-push-button-prefix "")
154 (widget-push-button-suffix "")
155 (found-target-pos nil))
156 ;; dme: It feels as though there should be a better way to
157 ;; implement this loop than using an incrementing counter.
158 (loop for elem in reordered-list
160 ;; (not elem) indicates an empty slot in the matrix.
162 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
163 (if (string= (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)
170 (insert (make-string (- 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, which we know is first."
184 (goto-char (point-min))
187 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
189 (defun notmuch-hello-search-continuation()
190 (notmuch-hello-update t))
192 (defun notmuch-hello-update (&optional no-display)
193 ;; Lazy - rebuild everything.
195 (let ((target (if (widget-at)
196 (widget-value (widget-at))
199 (widget-value (widget-at))))))
200 (notmuch-hello no-display target)))
202 (defun notmuch-hello (&optional no-display target)
206 (set-buffer "*notmuch-hello*")
207 (switch-to-buffer "*notmuch-hello*"))
209 (kill-all-local-variables)
210 (let ((inhibit-read-only t))
213 (let ((all (overlay-lists)))
214 ;; Delete all the overlays.
215 (mapc 'delete-overlay (car all))
216 (mapc 'delete-overlay (cdr all)))
218 (when notmuch-hello-show-logo
219 (let ((image notmuch-hello-logo))
220 ;; The notmuch logo uses transparency. That can display poorly
221 ;; when inserting the image into an emacs buffer (black logo on
222 ;; a black background), so force the background colour of the
223 ;; image. We use a face to represent the colour so that
224 ;; `defface' can be used to declare the different possible
225 ;; colours, which depend on whether the frame has a light or
227 (setq image (cons 'image
229 (list :background (face-background 'notmuch-hello-logo-background)))))
230 (insert-image image))
233 (widget-insert "Welcome to ")
234 ;; Hack the display of the links used.
235 (let ((widget-link-prefix "")
236 (widget-link-suffix ""))
238 :notify (lambda (&rest ignore)
239 (browse-url notmuch-hello-url))
240 :help-echo "Visit the notmuch website."
243 (widget-insert "You have ")
245 :notify (lambda (&rest ignore)
246 (notmuch-hello-update))
248 (car (process-lines notmuch-command "count")))
249 (widget-insert " messages (that's not much mail).\n\n"))
251 (let ((start (point)))
252 (widget-insert "Search: ")
253 (widget-create 'editable-field
254 ;; Leave some space at the start and end of the
256 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
257 (length "Search: ")))
258 :action (lambda (widget &rest ignore)
259 (notmuch-hello-search (widget-value widget))))
261 (indent-rigidly start (point) notmuch-hello-indent))
263 (when notmuch-hello-recent-searches
264 (widget-insert "\nRecent searches: ")
265 (widget-create 'push-button
266 :notify (lambda (&rest ignore)
267 (setq notmuch-hello-recent-searches nil)
268 (notmuch-hello-update))
270 (widget-insert "\n\n")
271 (let ((start (point))
273 (mapc '(lambda (search)
274 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
276 (widget-create 'editable-field
277 ;; Don't let the search boxes be
278 ;; less than 8 characters wide.
285 (* 2 notmuch-hello-indent)
288 ;; `[save]' button. 6
292 :action (lambda (widget &rest ignore)
293 (notmuch-hello-search (widget-value widget)))
296 (widget-create 'push-button
297 :notify (lambda (widget &rest ignore)
298 (notmuch-hello-add-saved-search widget))
299 :notmuch-saved-search-widget widget-symbol
303 notmuch-hello-recent-searches)
304 (indent-rigidly start (point) notmuch-hello-indent)))
306 (let ((found-target-pos nil)
307 (final-target-pos nil))
309 ;; Filter out empty saved seaches if required.
310 (if notmuch-hello-show-empty-saved-searches
311 notmuch-hello-saved-searches
312 (loop for elem in notmuch-hello-saved-searches
313 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
315 (saved-widest (notmuch-hello-longest-label saved-alist))
316 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
317 (process-lines notmuch-command "search-tags")))
318 (alltags-widest (notmuch-hello-longest-label alltags-alist))
319 (widest (max saved-widest alltags-widest)))
322 (widget-insert "\nSaved searches: ")
323 (widget-create 'push-button
324 :notify (lambda (&rest ignore)
325 (customize-variable 'notmuch-hello-saved-searches))
327 (widget-insert "\n\n")
328 (let ((start (point)))
329 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
330 (if (not final-target-pos)
331 (setq final-target-pos found-target-pos))
332 (indent-rigidly start (point) notmuch-hello-indent)))
335 (widget-insert "\nAll tags:\n\n")
336 (let ((start (point)))
337 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
338 (if (not final-target-pos)
339 (setq final-target-pos found-target-pos))
340 (indent-rigidly start (point) notmuch-hello-indent))))
342 (let ((start (point)))
343 (widget-insert "\n\n")
344 (widget-insert "Type a search query and hit RET to view matching threads.\n")
345 (when notmuch-hello-recent-searches
346 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
347 (widget-insert "Save recent searches with the `save' button.\n"))
348 (when notmuch-hello-saved-searches
349 (widget-insert "Edit saved searches with the `edit' button.\n"))
350 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
351 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
352 (let ((fill-column (- (window-width) notmuch-hello-indent)))
353 (center-region start (point))))
355 (use-local-map widget-keymap)
356 (local-set-key "=" 'notmuch-hello-update)
357 (local-set-key "m" 'notmuch-mua-mail)
358 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
359 (local-set-key "s" 'notmuch-hello-goto-search)
360 (local-set-key "v" '(lambda () (interactive)
361 (message "notmuch version %s" (notmuch-version))))
366 (goto-char final-target-pos)
368 (goto-char (point-min))
369 (widget-forward 1)))))
373 (provide 'notmuch-hello)