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-generate-row (ncols nrows row list)
122 (let ((len (length list)))
123 (loop for col from 0 to (- ncols 1)
124 collect (let ((offset (+ (* nrows col) row)))
127 ;; Don't forget to insert an empty slot in the
128 ;; output matrix if there is no corresponding
129 ;; value in the input matrix.
132 (defun notmuch-hello-reflect (list ncols)
133 "Reflect a `ncols' wide matrix represented by `list' along the
136 (let ((nrows (notmuch-hello-roundup (length list) ncols)))
137 (loop for row from 0 to (- nrows 1)
138 append (notmuch-hello-reflect-generate-row ncols nrows row list))))
140 (defun notmuch-hello-widget-search (widget &rest ignore)
141 (notmuch-search (widget-get widget
142 :notmuch-search-terms)
143 notmuch-search-oldest-first
144 nil nil #'notmuch-hello-search-continuation))
146 (defun notmuch-saved-search-count (search)
147 (car (process-lines notmuch-command "count" search)))
149 (defun notmuch-hello-insert-tags (tag-alist widest target)
150 (let* ((tags-per-line (max 1
151 (/ (- (window-width) notmuch-hello-indent)
152 ;; Count is 7 wide (6 digits plus
153 ;; space), 1 for the space after the
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.
165 ;; (not elem) indicates an empty slot in the matrix.
167 (let* ((name (car elem))
169 (formatted-name (format "%s " name)))
170 (widget-insert (format "%6s " (notmuch-saved-search-count query)))
171 (if (string= formatted-name target)
172 (setq found-target-pos (point-marker)))
173 (widget-create 'push-button
174 :notify #'notmuch-hello-widget-search
175 :notmuch-search-terms query
177 ;; Insert enough space to consume the rest of the
178 ;; column. Because the button for the name is `(1+
179 ;; (length name))' long (due to the trailing space) we
180 ;; can just insert `(- widest (length name))' spaces -
181 ;; the column separator is included in the button if
182 ;; `(equal widest (length name)'.
183 (widget-insert (make-string (- widest (length name)) ? ))))
184 (setq count (1+ count))
185 (if (eq (% count tags-per-line) 0)
186 (widget-insert "\n")))
189 ;; If the last line was not full (and hence did not include a
190 ;; carriage return), insert one now.
191 (if (not (eq (% count tags-per-line) 0))
192 (widget-insert "\n"))
195 (defun notmuch-hello-goto-search ()
196 "Put point inside the `search' widget."
198 (goto-char notmuch-hello-search-bar-marker))
200 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
202 (defun notmuch-hello-search-continuation()
203 (notmuch-hello-update t))
205 (defun notmuch-hello-update (&optional no-display)
206 ;; Lazy - rebuild everything.
208 (notmuch-hello no-display))
210 (defun notmuch-hello-poll-and-update ()
211 "Invoke `notmuch-poll' to import mail, then refresh the current view."
214 (notmuch-hello-update))
216 (defun notmuch-hello (&optional no-display)
219 ; Jump through a hoop to get this value from the deprecated variable
220 ; name (`notmuch-folders') or from the default value.
221 (if (not notmuch-saved-searches)
222 (setq notmuch-saved-searches (notmuch-saved-searches)))
225 (set-buffer "*notmuch-hello*")
226 (switch-to-buffer "*notmuch-hello*"))
228 (let ((target (if (widget-at)
229 (widget-value (widget-at))
233 (widget-value (widget-at)))
236 (kill-all-local-variables)
237 (let ((inhibit-read-only t))
240 (let ((all (overlay-lists)))
241 ;; Delete all the overlays.
242 (mapc 'delete-overlay (car all))
243 (mapc 'delete-overlay (cdr all)))
245 (when notmuch-show-logo
246 (let ((image notmuch-hello-logo))
247 ;; The notmuch logo uses transparency. That can display poorly
248 ;; when inserting the image into an emacs buffer (black logo on
249 ;; a black background), so force the background colour of the
250 ;; image. We use a face to represent the colour so that
251 ;; `defface' can be used to declare the different possible
252 ;; colours, which depend on whether the frame has a light or
254 (setq image (cons 'image
256 (list :background (face-background 'notmuch-hello-logo-background)))))
257 (insert-image image))
260 (widget-insert "Welcome to ")
261 ;; Hack the display of the links used.
262 (let ((widget-link-prefix "")
263 (widget-link-suffix ""))
265 :notify (lambda (&rest ignore)
266 (browse-url notmuch-hello-url))
267 :help-echo "Visit the notmuch website."
270 (widget-insert "You have ")
272 :notify (lambda (&rest ignore)
273 (notmuch-hello-update))
275 (car (process-lines notmuch-command "count")))
276 (widget-insert " messages (that's not much mail).\n"))
278 (let ((found-target-pos nil)
279 (final-target-pos nil))
281 ;; Filter out empty saved seaches if required.
282 (if notmuch-show-empty-saved-searches
283 notmuch-saved-searches
284 (loop for elem in notmuch-saved-searches
285 if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
287 (saved-widest (notmuch-hello-longest-label saved-alist))
288 (alltags-alist (if notmuch-show-all-tags-list
289 (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
290 (process-lines notmuch-command "search-tags"))))
291 (alltags-widest (notmuch-hello-longest-label alltags-alist))
292 (widest (max saved-widest alltags-widest)))
295 (widget-insert "\nSaved searches: ")
296 (widget-create 'push-button
297 :notify (lambda (&rest ignore)
298 (customize-variable 'notmuch-saved-searches))
300 (widget-insert "\n\n")
301 (setq final-target-pos (point-marker))
302 (let ((start (point)))
303 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
305 (setq final-target-pos found-target-pos))
306 (indent-rigidly start (point) notmuch-hello-indent)))
308 (widget-insert "\nSearch: ")
309 (setq notmuch-hello-search-bar-marker (point-marker))
310 (widget-create 'editable-field
311 ;; Leave some space at the start and end of the
313 :size (max 8 (- (window-width) notmuch-hello-indent
314 (length "Search: ")))
315 :action (lambda (widget &rest ignore)
316 (notmuch-hello-search (widget-value widget))))
319 (when notmuch-hello-recent-searches
320 (widget-insert "\nRecent searches: ")
321 (widget-create 'push-button
322 :notify (lambda (&rest ignore)
323 (setq notmuch-hello-recent-searches nil)
324 (notmuch-hello-update))
326 (widget-insert "\n\n")
327 (let ((start (point))
329 (mapc '(lambda (search)
330 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
332 (widget-create 'editable-field
333 ;; Don't let the search boxes be
334 ;; less than 8 characters wide.
341 (* 2 notmuch-hello-indent)
344 ;; `[save]' button. 6
348 :action (lambda (widget &rest ignore)
349 (notmuch-hello-search (widget-value widget)))
352 (widget-create 'push-button
353 :notify (lambda (widget &rest ignore)
354 (notmuch-hello-add-saved-search widget))
355 :notmuch-saved-search-widget widget-symbol
359 notmuch-hello-recent-searches)
360 (indent-rigidly start (point) notmuch-hello-indent)))
363 (widget-insert "\nAll tags: ")
364 (widget-create 'push-button
365 :notify (lambda (widget &rest ignore)
366 (setq notmuch-show-all-tags-list nil)
367 (notmuch-hello-update))
369 (widget-insert "\n\n")
370 (let ((start (point)))
371 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
372 (if (not final-target-pos)
373 (setq final-target-pos found-target-pos))
374 (indent-rigidly start (point) notmuch-hello-indent)))
378 (if (not notmuch-show-all-tags-list)
379 (widget-create 'push-button
380 :notify (lambda (widget &rest ignore)
381 (setq notmuch-show-all-tags-list t)
382 (notmuch-hello-update))
385 (let ((start (point)))
386 (widget-insert "\n\n")
387 (widget-insert "Type a search query and hit RET to view matching threads.\n")
388 (when notmuch-hello-recent-searches
389 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
390 (widget-insert "Save recent searches with the `save' button.\n"))
391 (when notmuch-saved-searches
392 (widget-insert "Edit saved searches with the `edit' button.\n"))
393 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
394 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
395 (let ((fill-column (- (window-width) notmuch-hello-indent)))
396 (center-region start (point))))
398 (use-local-map widget-keymap)
399 (local-set-key "=" 'notmuch-hello-update)
400 (local-set-key "G" 'notmuch-hello-poll-and-update)
401 (local-set-key "m" 'notmuch-mua-mail)
402 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
403 (local-set-key "s" 'notmuch-hello-goto-search)
404 (local-set-key "v" '(lambda () (interactive)
405 (message "notmuch version %s" (notmuch-version))))
409 (when final-target-pos
410 (goto-char final-target-pos)
415 (notmuch-hello-goto-search)))))
418 (defun notmuch-folder ()
419 "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
425 (provide 'notmuch-hello)