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= (format "%s " (car elem)) target)
172 (setq found-target-pos (point-marker)))
173 (widget-create 'push-button
174 :notify #'notmuch-hello-widget-search
175 :notmuch-search-terms (cdr elem)
176 (format "%s " (car elem)))
177 (insert (make-string (1+ (- widest (length (car elem)))) ? )))
178 (setq count (1+ count))
179 (if (eq (% count tags-per-line) 0)
180 (widget-insert "\n"))))
182 ;; If the last line was not full (and hence did not include a
183 ;; carriage return), insert one now.
184 (if (not (eq (% count tags-per-line) 0))
185 (widget-insert "\n"))
188 (defun notmuch-hello-goto-search ()
189 "Put point inside the `search' widget."
191 (goto-char notmuch-hello-search-bar-marker))
193 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
195 (defun notmuch-hello-search-continuation()
196 (notmuch-hello-update t))
198 (defun notmuch-hello-update (&optional no-display)
199 ;; Lazy - rebuild everything.
201 (notmuch-hello no-display))
203 (defun notmuch-hello (&optional no-display)
207 (set-buffer "*notmuch-hello*")
208 (switch-to-buffer "*notmuch-hello*"))
210 (let ((target (if (widget-at)
211 (widget-value (widget-at))
215 (widget-value (widget-at)))
218 (kill-all-local-variables)
219 (let ((inhibit-read-only t))
222 (let ((all (overlay-lists)))
223 ;; Delete all the overlays.
224 (mapc 'delete-overlay (car all))
225 (mapc 'delete-overlay (cdr all)))
227 (when notmuch-hello-show-logo
228 (let ((image notmuch-hello-logo))
229 ;; The notmuch logo uses transparency. That can display poorly
230 ;; when inserting the image into an emacs buffer (black logo on
231 ;; a black background), so force the background colour of the
232 ;; image. We use a face to represent the colour so that
233 ;; `defface' can be used to declare the different possible
234 ;; colours, which depend on whether the frame has a light or
236 (setq image (cons 'image
238 (list :background (face-background 'notmuch-hello-logo-background)))))
239 (insert-image image))
242 (widget-insert "Welcome to ")
243 ;; Hack the display of the links used.
244 (let ((widget-link-prefix "")
245 (widget-link-suffix ""))
247 :notify (lambda (&rest ignore)
248 (browse-url notmuch-hello-url))
249 :help-echo "Visit the notmuch website."
252 (widget-insert "You have ")
254 :notify (lambda (&rest ignore)
255 (notmuch-hello-update))
257 (car (process-lines notmuch-command "count")))
258 (widget-insert " messages (that's not much mail).\n\n"))
260 (let ((found-target-pos nil)
261 (final-target-pos nil))
263 ;; Filter out empty saved seaches if required.
264 (if notmuch-hello-show-empty-saved-searches
265 notmuch-hello-saved-searches
266 (loop for elem in notmuch-hello-saved-searches
267 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
269 (saved-widest (notmuch-hello-longest-label saved-alist))
270 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
271 (process-lines notmuch-command "search-tags")))
272 (alltags-widest (notmuch-hello-longest-label alltags-alist))
273 (widest (max saved-widest alltags-widest)))
276 (widget-insert "Saved searches: ")
277 (widget-create 'push-button
278 :notify (lambda (&rest ignore)
279 (customize-variable 'notmuch-hello-saved-searches))
281 (widget-insert "\n\n")
282 (setq final-target-pos (point-marker))
283 (let ((start (point)))
284 (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
286 (setq final-target-pos found-target-pos))
287 (indent-rigidly start (point) notmuch-hello-indent)))
289 (let ((start (point)))
290 (widget-insert "\nSearch: ")
291 (setq notmuch-hello-search-bar-marker (point-marker))
292 (widget-create 'editable-field
293 ;; Leave some space at the start and end of the
295 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
296 (length "Search: ")))
297 :action (lambda (widget &rest ignore)
298 (notmuch-hello-search (widget-value widget))))
300 (indent-rigidly start (point) notmuch-hello-indent))
302 (when notmuch-hello-recent-searches
303 (widget-insert "\nRecent searches: ")
304 (widget-create 'push-button
305 :notify (lambda (&rest ignore)
306 (setq notmuch-hello-recent-searches nil)
307 (notmuch-hello-update))
309 (widget-insert "\n\n")
310 (let ((start (point))
312 (mapc '(lambda (search)
313 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
315 (widget-create 'editable-field
316 ;; Don't let the search boxes be
317 ;; less than 8 characters wide.
324 (* 2 notmuch-hello-indent)
327 ;; `[save]' button. 6
331 :action (lambda (widget &rest ignore)
332 (notmuch-hello-search (widget-value widget)))
335 (widget-create 'push-button
336 :notify (lambda (widget &rest ignore)
337 (notmuch-hello-add-saved-search widget))
338 :notmuch-saved-search-widget widget-symbol
342 notmuch-hello-recent-searches)
343 (indent-rigidly start (point) notmuch-hello-indent)))
346 (if notmuch-hello-show-tags
348 (widget-insert "\nAll tags: ")
349 (widget-create 'push-button
350 :notify (lambda (widget &rest ignore)
351 (setq notmuch-hello-show-tags nil)
352 (notmuch-hello-update))
354 (widget-insert "\n\n")
355 (let ((start (point)))
356 (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
357 (if (not final-target-pos)
358 (setq final-target-pos found-target-pos))
359 (indent-rigidly start (point) notmuch-hello-indent)))
361 (widget-create 'push-button
362 :notify (lambda (widget &rest ignore)
363 (setq notmuch-hello-show-tags t)
364 (notmuch-hello-update))
367 (let ((start (point)))
368 (widget-insert "\n\n")
369 (widget-insert "Type a search query and hit RET to view matching threads.\n")
370 (when notmuch-hello-recent-searches
371 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
372 (widget-insert "Save recent searches with the `save' button.\n"))
373 (when notmuch-hello-saved-searches
374 (widget-insert "Edit saved searches with the `edit' button.\n"))
375 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
376 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
377 (let ((fill-column (- (window-width) notmuch-hello-indent)))
378 (center-region start (point))))
380 (use-local-map widget-keymap)
381 (local-set-key "=" 'notmuch-hello-update)
382 (local-set-key "m" 'notmuch-mua-mail)
383 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
384 (local-set-key "s" 'notmuch-hello-goto-search)
385 (local-set-key "v" '(lambda () (interactive)
386 (message "notmuch version %s" (notmuch-version))))
390 (goto-char final-target-pos)
391 (if (not (widget-at))
392 (widget-forward 1)))))
396 (provide 'notmuch-hello)