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 (let ((target (if (widget-at)
198 (widget-value (widget-at))
201 (widget-value (widget-at))))))
202 (notmuch-hello no-display target)))
204 (defun notmuch-hello (&optional no-display target)
208 (set-buffer "*notmuch-hello*")
209 (switch-to-buffer "*notmuch-hello*"))
211 (kill-all-local-variables)
212 (let ((inhibit-read-only t))
215 (let ((all (overlay-lists)))
216 ;; Delete all the overlays.
217 (mapc 'delete-overlay (car all))
218 (mapc 'delete-overlay (cdr all)))
220 (when notmuch-hello-show-logo
221 (let ((image notmuch-hello-logo))
222 ;; The notmuch logo uses transparency. That can display poorly
223 ;; when inserting the image into an emacs buffer (black logo on
224 ;; a black background), so force the background colour of the
225 ;; image. We use a face to represent the colour so that
226 ;; `defface' can be used to declare the different possible
227 ;; colours, which depend on whether the frame has a light or
229 (setq image (cons 'image
231 (list :background (face-background 'notmuch-hello-logo-background)))))
232 (insert-image image))
235 (widget-insert "Welcome to ")
236 ;; Hack the display of the links used.
237 (let ((widget-link-prefix "")
238 (widget-link-suffix ""))
240 :notify (lambda (&rest ignore)
241 (browse-url notmuch-hello-url))
242 :help-echo "Visit the notmuch website."
245 (widget-insert "You have ")
247 :notify (lambda (&rest ignore)
248 (notmuch-hello-update))
250 (car (process-lines notmuch-command "count")))
251 (widget-insert " messages (that's not much mail).\n\n"))
253 (let ((found-target-pos nil)
254 (final-target-pos nil))
256 ;; Filter out empty saved seaches if required.
257 (if notmuch-hello-show-empty-saved-searches
258 notmuch-hello-saved-searches
259 (loop for elem in notmuch-hello-saved-searches
260 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
262 (saved-widest (notmuch-hello-longest-label saved-alist))
263 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
264 (process-lines notmuch-command "search-tags")))
265 (alltags-widest (notmuch-hello-longest-label alltags-alist))
266 (widest (max saved-widest alltags-widest)))
269 (widget-insert "Saved searches: ")
270 (widget-create 'push-button
271 :notify (lambda (&rest ignore)
272 (customize-variable 'notmuch-hello-saved-searches))
274 (widget-insert "\n\n")
275 (setq final-target-pos (point-marker))
276 (let ((start (point)))
277 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
279 (setq final-target-pos found-target-pos))
280 (indent-rigidly start (point) notmuch-hello-indent)))
282 (let ((start (point)))
283 (widget-insert "\nSearch: ")
284 (setq notmuch-hello-search-bar-marker (point-marker))
285 (widget-create 'editable-field
286 ;; Leave some space at the start and end of the
288 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
289 (length "Search: ")))
290 :action (lambda (widget &rest ignore)
291 (notmuch-hello-search (widget-value widget))))
293 (indent-rigidly start (point) notmuch-hello-indent))
295 (when notmuch-hello-recent-searches
296 (widget-insert "\nRecent searches: ")
297 (widget-create 'push-button
298 :notify (lambda (&rest ignore)
299 (setq notmuch-hello-recent-searches nil)
300 (notmuch-hello-update))
302 (widget-insert "\n\n")
303 (let ((start (point))
305 (mapc '(lambda (search)
306 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
308 (widget-create 'editable-field
309 ;; Don't let the search boxes be
310 ;; less than 8 characters wide.
317 (* 2 notmuch-hello-indent)
320 ;; `[save]' button. 6
324 :action (lambda (widget &rest ignore)
325 (notmuch-hello-search (widget-value widget)))
328 (widget-create 'push-button
329 :notify (lambda (widget &rest ignore)
330 (notmuch-hello-add-saved-search widget))
331 :notmuch-saved-search-widget widget-symbol
335 notmuch-hello-recent-searches)
336 (indent-rigidly start (point) notmuch-hello-indent)))
339 (widget-insert "\nAll tags:\n\n")
340 (let ((start (point)))
341 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
342 (if (not final-target-pos)
343 (setq final-target-pos found-target-pos))
344 (indent-rigidly start (point) notmuch-hello-indent))))
346 (let ((start (point)))
347 (widget-insert "\n\n")
348 (widget-insert "Type a search query and hit RET to view matching threads.\n")
349 (when notmuch-hello-recent-searches
350 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
351 (widget-insert "Save recent searches with the `save' button.\n"))
352 (when notmuch-hello-saved-searches
353 (widget-insert "Edit saved searches with the `edit' button.\n"))
354 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
355 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
356 (let ((fill-column (- (window-width) notmuch-hello-indent)))
357 (center-region start (point))))
359 (use-local-map widget-keymap)
360 (local-set-key "=" 'notmuch-hello-update)
361 (local-set-key "m" 'notmuch-mua-mail)
362 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
363 (local-set-key "s" 'notmuch-hello-goto-search)
364 (local-set-key "v" '(lambda () (interactive)
365 (message "notmuch version %s" (notmuch-version))))
369 (goto-char final-target-pos)
370 (if (not (widget-at))
371 (widget-forward 1))))
375 (provide 'notmuch-hello)