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-poll "notmuch" ())
32 (defvar notmuch-hello-search-bar-marker nil
33 "The position of the search bar within the notmuch-hello buffer.")
35 (defcustom notmuch-recent-searches-max 10
36 "The number of recent searches to store and display."
40 (defcustom notmuch-show-empty-saved-searches nil
41 "Should saved searches with no messages be listed?"
45 (defvar notmuch-hello-indent 4
46 "How much to indent non-headers.")
48 (defcustom notmuch-show-logo t
49 "Should the notmuch logo be shown?"
53 (defcustom notmuch-show-all-tags-list nil
54 "Should all tags be shown in the notmuch-hello view?"
58 (defface notmuch-hello-logo-background
61 (:background "#5f5f5f"))
64 (:background "white")))
65 "Background colour for the notmuch logo."
68 (defvar notmuch-hello-url "http://notmuchmail.org"
69 "The `notmuch' web site.")
71 (defvar notmuch-hello-recent-searches nil)
73 (defun notmuch-hello-remember-search (search)
74 (if (not (member search notmuch-hello-recent-searches))
75 (push search notmuch-hello-recent-searches))
76 (if (> (length notmuch-hello-recent-searches)
77 notmuch-recent-searches-max)
78 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
80 (defun notmuch-hello-trim (search)
82 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
83 (match-string 1 search)
86 (defun notmuch-hello-search (search)
87 (let ((search (notmuch-hello-trim search)))
88 (notmuch-hello-remember-search search)
89 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
91 (defun notmuch-hello-add-saved-search (widget)
93 (let ((search (widget-value
95 (widget-get widget :notmuch-saved-search-widget))))
96 (name (completing-read "Name for saved search: "
97 notmuch-saved-searches)))
98 ;; If an existing saved search with this name exists, remove it.
99 (setq notmuch-saved-searches
100 (loop for elem in notmuch-saved-searches
105 (customize-save-variable 'notmuch-saved-searches
106 (push (cons name search)
107 notmuch-saved-searches))
108 (message "Saved '%s' as '%s'." search name)
109 (notmuch-hello-update)))
111 (defun notmuch-hello-longest-label (tag-alist)
112 (or (loop for elem in tag-alist
113 maximize (length (car elem)))
116 (defun notmuch-hello-roundup (dividend divisor)
117 "Return the rounded up value of dividing `dividend' by `divisor'."
118 (+ (/ dividend divisor)
119 (if (> (% dividend divisor) 0) 1 0)))
121 (defun notmuch-hello-reflect (list width)
122 "Reflect a `width' wide matrix represented by `list' along the
125 (let* ((len (length list))
126 (nrows (notmuch-hello-roundup len width)))
127 (loop for row from 0 to (- nrows 1)
128 append (loop for col from 0 to (- width 1)
129 ;; How could we calculate the offset just once
131 if (< (+ (* nrows col) row) len)
132 collect (nth (+ (* nrows col) row) list)
134 ;; Don't forget to insert an empty slot in the
135 ;; output matrix if there is no corresponding
136 ;; value in the input matrix.
139 (defun notmuch-hello-widget-search (widget &rest ignore)
140 (notmuch-search (widget-get widget
141 :notmuch-search-terms)
142 notmuch-search-oldest-first
143 nil nil #'notmuch-hello-search-continuation))
145 (defun notmuch-saved-search-count (search)
146 (car (process-lines notmuch-command "count" search)))
148 (defun notmuch-hello-insert-tags (tag-alist widest target)
149 (let* ((tags-per-line (max 1
150 (/ (- (window-width) notmuch-hello-indent)
151 ;; Count is 7 wide, 1 for the space
155 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
156 ;; Hack the display of the buttons used.
157 (widget-push-button-prefix "")
158 (widget-push-button-suffix "")
159 (found-target-pos nil))
160 ;; dme: It feels as though there should be a better way to
161 ;; implement this loop than using an incrementing counter.
162 (loop for elem in reordered-list
164 ;; (not elem) indicates an empty slot in the matrix.
166 (widget-insert (format "%6s " (notmuch-saved-search-count (cdr elem))))
167 (if (string= (format "%s " (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)
172 (format "%s " (car elem)))
173 (insert (make-string (1+ (- 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-poll-and-update ()
200 "Invoke `notmuch-poll' to import mail, then refresh the current view."
203 (notmuch-hello-update))
205 (defun notmuch-hello (&optional no-display)
208 ; Jump through a hoop to get this value from the deprecated variable
209 ; name (`notmuch-folders') or from the default value.
210 (if (not notmuch-saved-searches)
211 (setq notmuch-saved-searches (notmuch-saved-searches)))
214 (set-buffer "*notmuch-hello*")
215 (switch-to-buffer "*notmuch-hello*"))
217 (let ((target (if (widget-at)
218 (widget-value (widget-at))
222 (widget-value (widget-at)))
225 (kill-all-local-variables)
226 (let ((inhibit-read-only t))
229 (let ((all (overlay-lists)))
230 ;; Delete all the overlays.
231 (mapc 'delete-overlay (car all))
232 (mapc 'delete-overlay (cdr all)))
234 (when notmuch-show-logo
235 (let ((image notmuch-hello-logo))
236 ;; The notmuch logo uses transparency. That can display poorly
237 ;; when inserting the image into an emacs buffer (black logo on
238 ;; a black background), so force the background colour of the
239 ;; image. We use a face to represent the colour so that
240 ;; `defface' can be used to declare the different possible
241 ;; colours, which depend on whether the frame has a light or
243 (setq image (cons 'image
245 (list :background (face-background 'notmuch-hello-logo-background)))))
246 (insert-image image))
249 (widget-insert "Welcome to ")
250 ;; Hack the display of the links used.
251 (let ((widget-link-prefix "")
252 (widget-link-suffix ""))
254 :notify (lambda (&rest ignore)
255 (browse-url notmuch-hello-url))
256 :help-echo "Visit the notmuch website."
259 (widget-insert "You have ")
261 :notify (lambda (&rest ignore)
262 (notmuch-hello-update))
264 (car (process-lines notmuch-command "count")))
265 (widget-insert " messages (that's not much mail).\n\n"))
267 (let ((found-target-pos nil)
268 (final-target-pos nil))
270 ;; Filter out empty saved seaches if required.
271 (if notmuch-show-empty-saved-searches
272 notmuch-saved-searches
273 (loop for elem in notmuch-saved-searches
274 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
276 (saved-widest (notmuch-hello-longest-label saved-alist))
277 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
278 (process-lines notmuch-command "search-tags")))
279 (alltags-widest (notmuch-hello-longest-label alltags-alist))
280 (widest (max saved-widest alltags-widest)))
283 (widget-insert "Saved searches: ")
284 (widget-create 'push-button
285 :notify (lambda (&rest ignore)
286 (customize-variable 'notmuch-saved-searches))
288 (widget-insert "\n\n")
289 (setq final-target-pos (point-marker))
290 (let ((start (point)))
291 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
293 (setq final-target-pos found-target-pos))
294 (indent-rigidly start (point) notmuch-hello-indent)))
296 (let ((start (point)))
297 (widget-insert "\nSearch: ")
298 (setq notmuch-hello-search-bar-marker (point-marker))
299 (widget-create 'editable-field
300 ;; Leave some space at the start and end of the
302 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
303 (length "Search: ")))
304 :action (lambda (widget &rest ignore)
305 (notmuch-hello-search (widget-value widget))))
307 (indent-rigidly start (point) notmuch-hello-indent))
309 (when notmuch-hello-recent-searches
310 (widget-insert "\nRecent searches: ")
311 (widget-create 'push-button
312 :notify (lambda (&rest ignore)
313 (setq notmuch-hello-recent-searches nil)
314 (notmuch-hello-update))
316 (widget-insert "\n\n")
317 (let ((start (point))
319 (mapc '(lambda (search)
320 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
322 (widget-create 'editable-field
323 ;; Don't let the search boxes be
324 ;; less than 8 characters wide.
331 (* 2 notmuch-hello-indent)
334 ;; `[save]' button. 6
338 :action (lambda (widget &rest ignore)
339 (notmuch-hello-search (widget-value widget)))
342 (widget-create 'push-button
343 :notify (lambda (widget &rest ignore)
344 (notmuch-hello-add-saved-search widget))
345 :notmuch-saved-search-widget widget-symbol
349 notmuch-hello-recent-searches)
350 (indent-rigidly start (point) notmuch-hello-indent)))
353 (if notmuch-show-all-tags-list
355 (widget-insert "\nAll tags: ")
356 (widget-create 'push-button
357 :notify (lambda (widget &rest ignore)
358 (setq notmuch-show-all-tags-list nil)
359 (notmuch-hello-update))
361 (widget-insert "\n\n")
362 (let ((start (point)))
363 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
364 (if (not final-target-pos)
365 (setq final-target-pos found-target-pos))
366 (indent-rigidly start (point) notmuch-hello-indent)))
368 (widget-create 'push-button
369 :notify (lambda (widget &rest ignore)
370 (setq notmuch-show-all-tags-list t)
371 (notmuch-hello-update))
374 (let ((start (point)))
375 (widget-insert "\n\n")
376 (widget-insert "Type a search query and hit RET to view matching threads.\n")
377 (when notmuch-hello-recent-searches
378 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
379 (widget-insert "Save recent searches with the `save' button.\n"))
380 (when notmuch-saved-searches
381 (widget-insert "Edit saved searches with the `edit' button.\n"))
382 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
383 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
384 (let ((fill-column (- (window-width) notmuch-hello-indent)))
385 (center-region start (point))))
387 (use-local-map widget-keymap)
388 (local-set-key "=" 'notmuch-hello-update)
389 (local-set-key "G" 'notmuch-hello-poll-and-update)
390 (local-set-key "m" 'notmuch-mua-mail)
391 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
392 (local-set-key "s" 'notmuch-hello-goto-search)
393 (local-set-key "v" '(lambda () (interactive)
394 (message "notmuch version %s" (notmuch-version))))
398 (goto-char final-target-pos)
399 (if (not (widget-at))
400 (widget-forward 1)))))
403 (defun notmuch-folder ()
404 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
410 (provide 'notmuch-hello)