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 (defvar notmuch-hello-search-bar-marker nil
33 "The position of the search bar within the notmuch-hello buffer.")
35 (defcustom notmuch-hello-recent-searches-max 10
36 "The number of recent searches to store and display."
40 (defcustom notmuch-hello-show-empty-saved-searches nil
41 "Should saved searches with no messages be listed?"
45 (defcustom notmuch-hello-indent 4
46 "How much to indent non-headers."
50 (defcustom notmuch-hello-saved-searches notmuch-folders
51 "A list of saved searches to display."
52 :type '(alist :key-type string :value-type string)
55 (defcustom notmuch-hello-show-logo t
56 "Should the notmuch logo be shown?"
60 (defcustom notmuch-hello-show-tags nil
61 "Should all tags be shown in the notmuch-hello view?"
65 (defface notmuch-hello-logo-background
68 (:background "#5f5f5f"))
71 (:background "white")))
72 "Background colour for the notmuch logo."
75 (defvar notmuch-hello-url "http://notmuchmail.org"
76 "The `notmuch' web site.")
78 (defvar notmuch-hello-recent-searches nil)
80 (defun notmuch-hello-remember-search (search)
81 (if (not (member search notmuch-hello-recent-searches))
82 (push search notmuch-hello-recent-searches))
83 (if (> (length notmuch-hello-recent-searches)
84 notmuch-hello-recent-searches-max)
85 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
87 (defun notmuch-hello-trim (search)
89 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
90 (match-string 1 search)
93 (defun notmuch-hello-search (search)
94 (let ((search (notmuch-hello-trim search)))
95 (notmuch-hello-remember-search search)
96 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
98 (defun notmuch-hello-add-saved-search (widget)
100 (let ((search (widget-value
102 (widget-get widget :notmuch-saved-search-widget))))
103 (name (completing-read "Name for saved search: "
104 notmuch-hello-saved-searches)))
105 ;; If an existing saved search with this name exists, remove it.
106 (setq notmuch-hello-saved-searches
107 (loop for elem in notmuch-hello-saved-searches
112 (customize-save-variable 'notmuch-hello-saved-searches
113 (push (cons name search)
114 notmuch-hello-saved-searches))
115 (message "Saved '%s' as '%s'." search name)
116 (notmuch-hello-update)))
118 (defun notmuch-hello-longest-label (tag-alist)
119 (or (loop for elem in tag-alist
120 maximize (length (car elem)))
123 (defun notmuch-hello-roundup (dividend divisor)
124 "Return the rounded up value of dividing `dividend' by `divisor'."
125 (+ (/ dividend divisor)
126 (if (> (% dividend divisor) 0) 1 0)))
128 (defun notmuch-hello-reflect (list width)
129 "Reflect a `width' wide matrix represented by `list' along the
132 (let* ((len (length list))
133 (nrows (notmuch-hello-roundup len width)))
134 (loop for row from 0 to (- nrows 1)
135 append (loop for col from 0 to (- width 1)
136 ;; How could we calculate the offset just once
138 if (< (+ (* nrows col) row) len)
139 collect (nth (+ (* nrows col) row) list)
141 ;; Don't forget to insert an empty slot in the
142 ;; output matrix if there is no corresponding
143 ;; value in the input matrix.
146 (defun notmuch-hello-widget-search (widget &rest ignore)
147 (notmuch-search (widget-get widget
148 :notmuch-search-terms)
149 notmuch-search-oldest-first
150 nil nil #'notmuch-hello-search-continuation))
152 (defun notmuch-hello-insert-tags (tag-alist widest target)
153 (let* ((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 (found-target-pos nil))
164 ;; dme: It feels as though there should be a better way to
165 ;; implement this loop than using an incrementing counter.
166 (loop for elem in reordered-list
168 ;; (not elem) indicates an empty slot in the matrix.
170 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
171 (if (string= (car elem) target)
173 (setq found-target-pos (point-marker))))
174 (widget-create 'push-button
175 :notify #'notmuch-hello-widget-search
176 :notmuch-search-terms (cdr elem)
178 (insert (make-string (- widest (length (car elem))) ? )))
179 (setq count (1+ count))
180 (if (eq (% count tags-per-line) 0)
181 (widget-insert "\n"))))
183 ;; If the last line was not full (and hence did not include a
184 ;; carriage return), insert one now.
185 (if (not (eq (% count tags-per-line) 0))
186 (widget-insert "\n"))
189 (defun notmuch-hello-goto-search ()
190 "Put point inside the `search' widget."
192 (goto-char notmuch-hello-search-bar-marker))
194 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
196 (defun notmuch-hello-search-continuation()
197 (notmuch-hello-update t))
199 (defun notmuch-hello-update (&optional no-display)
200 ;; Lazy - rebuild everything.
202 (notmuch-hello no-display))
204 (defun notmuch-hello (&optional no-display)
208 (set-buffer "*notmuch-hello*")
209 (switch-to-buffer "*notmuch-hello*"))
211 (let ((target (if (widget-at)
212 (widget-value (widget-at))
216 (widget-value (widget-at)))
219 (kill-all-local-variables)
220 (let ((inhibit-read-only t))
223 (let ((all (overlay-lists)))
224 ;; Delete all the overlays.
225 (mapc 'delete-overlay (car all))
226 (mapc 'delete-overlay (cdr all)))
228 (when notmuch-hello-show-logo
229 (let ((image notmuch-hello-logo))
230 ;; The notmuch logo uses transparency. That can display poorly
231 ;; when inserting the image into an emacs buffer (black logo on
232 ;; a black background), so force the background colour of the
233 ;; image. We use a face to represent the colour so that
234 ;; `defface' can be used to declare the different possible
235 ;; colours, which depend on whether the frame has a light or
237 (setq image (cons 'image
239 (list :background (face-background 'notmuch-hello-logo-background)))))
240 (insert-image image))
243 (widget-insert "Welcome to ")
244 ;; Hack the display of the links used.
245 (let ((widget-link-prefix "")
246 (widget-link-suffix ""))
248 :notify (lambda (&rest ignore)
249 (browse-url notmuch-hello-url))
250 :help-echo "Visit the notmuch website."
253 (widget-insert "You have ")
255 :notify (lambda (&rest ignore)
256 (notmuch-hello-update))
258 (car (process-lines notmuch-command "count")))
259 (widget-insert " messages (that's not much mail).\n\n"))
261 (let ((found-target-pos nil)
262 (final-target-pos nil))
264 ;; Filter out empty saved seaches if required.
265 (if notmuch-hello-show-empty-saved-searches
266 notmuch-hello-saved-searches
267 (loop for elem in notmuch-hello-saved-searches
268 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
270 (saved-widest (notmuch-hello-longest-label saved-alist))
271 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
272 (process-lines notmuch-command "search-tags")))
273 (alltags-widest (notmuch-hello-longest-label alltags-alist))
274 (widest (max saved-widest alltags-widest)))
277 (widget-insert "Saved searches: ")
278 (widget-create 'push-button
279 :notify (lambda (&rest ignore)
280 (customize-variable 'notmuch-hello-saved-searches))
282 (widget-insert "\n\n")
283 (setq final-target-pos (point-marker))
284 (let ((start (point)))
285 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
287 (setq final-target-pos found-target-pos))
288 (indent-rigidly start (point) notmuch-hello-indent)))
290 (let ((start (point)))
291 (widget-insert "\nSearch: ")
292 (setq notmuch-hello-search-bar-marker (point-marker))
293 (widget-create 'editable-field
294 ;; Leave some space at the start and end of the
296 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
297 (length "Search: ")))
298 :action (lambda (widget &rest ignore)
299 (notmuch-hello-search (widget-value widget))))
301 (indent-rigidly start (point) notmuch-hello-indent))
303 (when notmuch-hello-recent-searches
304 (widget-insert "\nRecent searches: ")
305 (widget-create 'push-button
306 :notify (lambda (&rest ignore)
307 (setq notmuch-hello-recent-searches nil)
308 (notmuch-hello-update))
310 (widget-insert "\n\n")
311 (let ((start (point))
313 (mapc '(lambda (search)
314 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
316 (widget-create 'editable-field
317 ;; Don't let the search boxes be
318 ;; less than 8 characters wide.
325 (* 2 notmuch-hello-indent)
328 ;; `[save]' button. 6
332 :action (lambda (widget &rest ignore)
333 (notmuch-hello-search (widget-value widget)))
336 (widget-create 'push-button
337 :notify (lambda (widget &rest ignore)
338 (notmuch-hello-add-saved-search widget))
339 :notmuch-saved-search-widget widget-symbol
343 notmuch-hello-recent-searches)
344 (indent-rigidly start (point) notmuch-hello-indent)))
347 (if notmuch-hello-show-tags
349 (widget-insert "\nAll tags: ")
350 (widget-create 'push-button
351 :notify (lambda (widget &rest ignore)
352 (setq notmuch-hello-show-tags nil)
353 (notmuch-hello-update))
355 (widget-insert "\n\n")
356 (let ((start (point)))
357 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
358 (if (not final-target-pos)
359 (setq final-target-pos found-target-pos))
360 (indent-rigidly start (point) notmuch-hello-indent)))
362 (widget-create 'push-button
363 :notify (lambda (widget &rest ignore)
364 (setq notmuch-hello-show-tags t)
365 (notmuch-hello-update))
368 (let ((start (point)))
369 (widget-insert "\n\n")
370 (widget-insert "Type a search query and hit RET to view matching threads.\n")
371 (when notmuch-hello-recent-searches
372 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
373 (widget-insert "Save recent searches with the `save' button.\n"))
374 (when notmuch-hello-saved-searches
375 (widget-insert "Edit saved searches with the `edit' button.\n"))
376 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
377 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
378 (let ((fill-column (- (window-width) notmuch-hello-indent)))
379 (center-region start (point))))
381 (use-local-map widget-keymap)
382 (local-set-key "=" 'notmuch-hello-update)
383 (local-set-key "m" 'notmuch-mua-mail)
384 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
385 (local-set-key "s" 'notmuch-hello-goto-search)
386 (local-set-key "v" '(lambda () (interactive)
387 (message "notmuch version %s" (notmuch-version))))
391 (goto-char final-target-pos)
392 (if (not (widget-at))
393 (widget-forward 1)))))
397 (provide 'notmuch-hello)