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 nil
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 target)
151 (let* ((tags-per-line (max 1
152 (/ (- (window-width) notmuch-hello-indent)
153 ;; Count is 7 wide, 1 for the space
157 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
158 ;; Hack the display of the buttons used.
159 (widget-push-button-prefix "")
160 (widget-push-button-suffix "")
161 (found-target-pos nil))
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 (if (string= (car elem) target)
171 (setq found-target-pos (point-marker))))
172 (widget-create 'push-button
173 :notify #'notmuch-hello-widget-search
174 :notmuch-search-terms (cdr elem)
176 (insert (make-string (- widest (length (car elem))) ? )))
177 (setq count (1+ count))
178 (if (eq (% count tags-per-line) 0)
179 (widget-insert "\n"))))
181 ;; If the last line was not full (and hence did not include a
182 ;; carriage return), insert one now.
183 (if (not (eq (% count tags-per-line) 0))
184 (widget-insert "\n"))
187 (defun notmuch-hello-goto-search ()
188 "Put point inside the `search' widget, which we know is first."
190 (goto-char (point-min))
193 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
195 (defun notmuch-hello-search-continuation()
198 (defun notmuch-hello-update (&optional no-display)
199 ;; Lazy - rebuild everything.
201 (let ((target (if (widget-at)
202 (widget-value (widget-at))
205 (widget-value (widget-at))))))
206 (notmuch-hello no-display target)))
208 (defun notmuch-hello (&optional no-display target)
212 (set-buffer "*notmuch-hello*")
213 (switch-to-buffer "*notmuch-hello*"))
215 (kill-all-local-variables)
216 (let ((inhibit-read-only t))
219 (let ((all (overlay-lists)))
220 ;; Delete all the overlays.
221 (mapc 'delete-overlay (car all))
222 (mapc 'delete-overlay (cdr all)))
224 (when notmuch-hello-show-logo
225 (let ((image notmuch-hello-logo))
226 ;; The notmuch logo uses transparency. That can display poorly
227 ;; when inserting the image into an emacs buffer (black logo on
228 ;; a black background), so force the background colour of the
229 ;; image. We use a face to represent the colour so that
230 ;; `defface' can be used to declare the different possible
231 ;; colours, which depend on whether the frame has a light or
233 (setq image (cons 'image
235 (list :background (face-background 'notmuch-hello-logo-background)))))
236 (insert-image image))
239 (widget-insert "Welcome to ")
240 ;; Hack the display of the links used.
241 (let ((widget-link-prefix "")
242 (widget-link-suffix ""))
244 :notify (lambda (&rest ignore)
245 (browse-url notmuch-hello-url))
246 :help-echo "Visit the notmuch website."
249 (widget-insert "You have ")
251 :notify (lambda (&rest ignore)
252 (notmuch-hello-update))
254 (car (process-lines notmuch-command "count")))
255 (widget-insert " messages (that's not much mail).\n\n"))
257 (let ((start (point)))
258 (widget-insert "Search: ")
259 (widget-create 'editable-field
260 ;; Leave some space at the start and end of the
262 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
263 (length "Search: ")))
264 :action (lambda (widget &rest ignore)
265 (notmuch-hello-search (widget-value widget))))
267 (indent-rigidly start (point) notmuch-hello-indent))
269 (when notmuch-hello-recent-searches
270 (widget-insert "\nRecent searches: ")
271 (widget-create 'push-button
272 :notify (lambda (&rest ignore)
273 (setq notmuch-hello-recent-searches nil)
274 (notmuch-hello-update))
276 (widget-insert "\n\n")
277 (let ((start (point))
279 (mapc '(lambda (search)
280 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
282 (widget-create 'editable-field
283 ;; Don't let the search boxes be
284 ;; less than 8 characters wide.
291 (* 2 notmuch-hello-indent)
294 ;; `[save]' button. 6
298 :action (lambda (widget &rest ignore)
299 (notmuch-hello-search (widget-value widget)))
302 (widget-create 'push-button
303 :notify (lambda (widget &rest ignore)
304 (notmuch-hello-add-saved-search widget))
305 :notmuch-saved-search-widget widget-symbol
309 notmuch-hello-recent-searches)
310 (indent-rigidly start (point) notmuch-hello-indent)))
312 (let ((found-target-pos nil)
313 (final-target-pos nil))
315 ;; Filter out empty saved seaches if required.
316 (if notmuch-hello-show-empty-saved-searches
317 notmuch-hello-saved-searches
318 (loop for elem in notmuch-hello-saved-searches
319 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
321 (saved-widest (notmuch-hello-longest-label saved-alist))
322 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
323 (process-lines notmuch-command "search-tags")))
324 (alltags-widest (notmuch-hello-longest-label alltags-alist))
325 (widest (max saved-widest alltags-widest)))
328 (widget-insert "\nSaved searches: ")
329 (widget-create 'push-button
330 :notify (lambda (&rest ignore)
331 (customize-variable 'notmuch-hello-saved-searches))
333 (widget-insert "\n\n")
334 (let ((start (point)))
335 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
336 (if (not final-target-pos)
337 (setq final-target-pos found-target-pos))
338 (indent-rigidly start (point) notmuch-hello-indent)))
341 (widget-insert "\nAll tags:\n\n")
342 (let ((start (point)))
343 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
344 (if (not final-target-pos)
345 (setq final-target-pos found-target-pos))
346 (indent-rigidly start (point) notmuch-hello-indent))))
348 (let ((start (point)))
349 (widget-insert "\n\n")
350 (widget-insert "Type a search query and hit RET to view matching threads.\n")
351 (when notmuch-hello-recent-searches
352 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
353 (widget-insert "Save recent searches with the `save' button.\n"))
354 (when notmuch-hello-saved-searches
355 (widget-insert "Edit saved searches with the `edit' button.\n"))
356 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
357 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
358 (let ((fill-column (- (window-width) notmuch-hello-indent)))
359 (center-region start (point))))
361 (use-local-map widget-keymap)
362 (local-set-key "=" 'notmuch-hello-update)
363 (local-set-key "m" 'notmuch-mua-mail)
364 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
365 (local-set-key "s" 'notmuch-hello-goto-search)
366 (local-set-key "v" '(lambda () (interactive)
367 (message "notmuch version %s" (notmuch-version))))
372 (goto-char final-target-pos)
373 (if notmuch-hello-jump-to-search
374 (notmuch-hello-goto-search)
375 (goto-char (point-min))))))
379 (provide 'notmuch-hello)