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 (defface notmuch-hello-logo-background
63 (:background "#5f5f5f"))
66 (:background "white")))
67 "Background colour for the notmuch logo."
70 (defvar notmuch-hello-url "http://notmuchmail.org"
71 "The `notmuch' web site.")
73 (defvar notmuch-hello-recent-searches nil)
75 (defun notmuch-hello-remember-search (search)
76 (if (not (member search notmuch-hello-recent-searches))
77 (push search notmuch-hello-recent-searches))
78 (if (> (length notmuch-hello-recent-searches)
79 notmuch-hello-recent-searches-max)
80 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
82 (defun notmuch-hello-trim (search)
84 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
85 (match-string 1 search)
88 (defun notmuch-hello-search (search)
89 (let ((search (notmuch-hello-trim search)))
90 (notmuch-hello-remember-search search)
91 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
93 (defun notmuch-hello-add-saved-search (widget)
95 (let ((search (widget-value
97 (widget-get widget :notmuch-saved-search-widget))))
98 (name (completing-read "Name for saved search: "
99 notmuch-hello-saved-searches)))
100 ;; If an existing saved search with this name exists, remove it.
101 (setq notmuch-hello-saved-searches
102 (loop for elem in notmuch-hello-saved-searches
107 (customize-save-variable 'notmuch-hello-saved-searches
108 (push (cons name search)
109 notmuch-hello-saved-searches))
110 (message "Saved '%s' as '%s'." search name)
111 (notmuch-hello-update)))
113 (defun notmuch-hello-longest-label (tag-alist)
114 (or (loop for elem in tag-alist
115 maximize (length (car elem)))
118 (defun notmuch-hello-roundup (dividend divisor)
119 "Return the rounded up value of dividing `dividend' by `divisor'."
120 (+ (/ dividend divisor)
121 (if (> (% dividend divisor) 0) 1 0)))
123 (defun notmuch-hello-reflect (list width)
124 "Reflect a `width' wide matrix represented by `list' along the
127 (let* ((len (length list))
128 (nrows (notmuch-hello-roundup len width)))
129 (loop for row from 0 to (- nrows 1)
130 append (loop for col from 0 to (- width 1)
131 ;; How could we calculate the offset just once
133 if (< (+ (* nrows col) row) len)
134 collect (nth (+ (* nrows col) row) list)
136 ;; Don't forget to insert an empty slot in the
137 ;; output matrix if there is no corresponding
138 ;; value in the input matrix.
141 (defun notmuch-hello-widget-search (widget &rest ignore)
142 (notmuch-search (widget-get widget
143 :notmuch-search-terms)
144 notmuch-search-oldest-first
145 nil nil #'notmuch-hello-search-continuation))
147 (defun notmuch-hello-insert-tags (tag-alist widest target)
148 (let* ((tags-per-line (max 1
149 (/ (- (window-width) notmuch-hello-indent)
150 ;; Count is 7 wide, 1 for the space
154 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
155 ;; Hack the display of the buttons used.
156 (widget-push-button-prefix "")
157 (widget-push-button-suffix "")
158 (found-target-pos nil))
159 ;; dme: It feels as though there should be a better way to
160 ;; implement this loop than using an incrementing counter.
161 (loop for elem in reordered-list
163 ;; (not elem) indicates an empty slot in the matrix.
165 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
166 (if (string= (car elem) target)
168 (setq found-target-pos (point-marker))))
169 (widget-create 'push-button
170 :notify #'notmuch-hello-widget-search
171 :notmuch-search-terms (cdr elem)
173 (insert (make-string (- widest (length (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"))
184 (defun notmuch-hello-goto-search ()
185 "Put point inside the `search' widget."
187 (goto-char notmuch-hello-search-bar-marker))
189 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
191 (defun notmuch-hello-search-continuation()
192 (notmuch-hello-update t))
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 (let ((target (if (widget-at)
207 (widget-value (widget-at))
211 (widget-value (widget-at)))
214 (kill-all-local-variables)
215 (let ((inhibit-read-only t))
218 (let ((all (overlay-lists)))
219 ;; Delete all the overlays.
220 (mapc 'delete-overlay (car all))
221 (mapc 'delete-overlay (cdr all)))
223 (when notmuch-hello-show-logo
224 (let ((image notmuch-hello-logo))
225 ;; The notmuch logo uses transparency. That can display poorly
226 ;; when inserting the image into an emacs buffer (black logo on
227 ;; a black background), so force the background colour of the
228 ;; image. We use a face to represent the colour so that
229 ;; `defface' can be used to declare the different possible
230 ;; colours, which depend on whether the frame has a light or
232 (setq image (cons 'image
234 (list :background (face-background 'notmuch-hello-logo-background)))))
235 (insert-image image))
238 (widget-insert "Welcome to ")
239 ;; Hack the display of the links used.
240 (let ((widget-link-prefix "")
241 (widget-link-suffix ""))
243 :notify (lambda (&rest ignore)
244 (browse-url notmuch-hello-url))
245 :help-echo "Visit the notmuch website."
248 (widget-insert "You have ")
250 :notify (lambda (&rest ignore)
251 (notmuch-hello-update))
253 (car (process-lines notmuch-command "count")))
254 (widget-insert " messages (that's not much mail).\n\n"))
256 (let ((found-target-pos nil)
257 (final-target-pos nil))
259 ;; Filter out empty saved seaches if required.
260 (if notmuch-hello-show-empty-saved-searches
261 notmuch-hello-saved-searches
262 (loop for elem in notmuch-hello-saved-searches
263 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
265 (saved-widest (notmuch-hello-longest-label saved-alist))
266 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
267 (process-lines notmuch-command "search-tags")))
268 (alltags-widest (notmuch-hello-longest-label alltags-alist))
269 (widest (max saved-widest alltags-widest)))
272 (widget-insert "Saved searches: ")
273 (widget-create 'push-button
274 :notify (lambda (&rest ignore)
275 (customize-variable 'notmuch-hello-saved-searches))
277 (widget-insert "\n\n")
278 (setq final-target-pos (point-marker))
279 (let ((start (point)))
280 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
282 (setq final-target-pos found-target-pos))
283 (indent-rigidly start (point) notmuch-hello-indent)))
285 (let ((start (point)))
286 (widget-insert "\nSearch: ")
287 (setq notmuch-hello-search-bar-marker (point-marker))
288 (widget-create 'editable-field
289 ;; Leave some space at the start and end of the
291 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
292 (length "Search: ")))
293 :action (lambda (widget &rest ignore)
294 (notmuch-hello-search (widget-value widget))))
296 (indent-rigidly start (point) notmuch-hello-indent))
298 (when notmuch-hello-recent-searches
299 (widget-insert "\nRecent searches: ")
300 (widget-create 'push-button
301 :notify (lambda (&rest ignore)
302 (setq notmuch-hello-recent-searches nil)
303 (notmuch-hello-update))
305 (widget-insert "\n\n")
306 (let ((start (point))
308 (mapc '(lambda (search)
309 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
311 (widget-create 'editable-field
312 ;; Don't let the search boxes be
313 ;; less than 8 characters wide.
320 (* 2 notmuch-hello-indent)
323 ;; `[save]' button. 6
327 :action (lambda (widget &rest ignore)
328 (notmuch-hello-search (widget-value widget)))
331 (widget-create 'push-button
332 :notify (lambda (widget &rest ignore)
333 (notmuch-hello-add-saved-search widget))
334 :notmuch-saved-search-widget widget-symbol
338 notmuch-hello-recent-searches)
339 (indent-rigidly start (point) notmuch-hello-indent)))
342 (widget-insert "\nAll tags:\n\n")
343 (let ((start (point)))
344 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
345 (if (not final-target-pos)
346 (setq final-target-pos found-target-pos))
347 (indent-rigidly start (point) notmuch-hello-indent))))
349 (let ((start (point)))
350 (widget-insert "\n\n")
351 (widget-insert "Type a search query and hit RET to view matching threads.\n")
352 (when notmuch-hello-recent-searches
353 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
354 (widget-insert "Save recent searches with the `save' button.\n"))
355 (when notmuch-hello-saved-searches
356 (widget-insert "Edit saved searches with the `edit' button.\n"))
357 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
358 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
359 (let ((fill-column (- (window-width) notmuch-hello-indent)))
360 (center-region start (point))))
362 (use-local-map widget-keymap)
363 (local-set-key "=" 'notmuch-hello-update)
364 (local-set-key "m" 'notmuch-mua-mail)
365 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
366 (local-set-key "s" 'notmuch-hello-goto-search)
367 (local-set-key "v" '(lambda () (interactive)
368 (message "notmuch version %s" (notmuch-version))))
372 (goto-char final-target-pos)
373 (if (not (widget-at))
374 (widget-forward 1)))))
378 (provide 'notmuch-hello)