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)
28 (require 'notmuch-mua)
30 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
31 (declare-function notmuch-folder-count "notmuch" (search))
33 (defcustom notmuch-hello-recent-searches-max 10
34 "The number of recent searches to store and display."
38 (defcustom notmuch-hello-show-empty-saved-searches nil
39 "Should saved searches with no messages be listed?"
43 (defcustom notmuch-hello-indent 4
44 "How much to indent non-headers."
48 (defcustom notmuch-hello-saved-searches notmuch-folders
49 "A list of saved searches to display."
50 :type '(alist :key-type string :value-type string)
53 (defcustom notmuch-hello-show-logo t
54 "Should the notmuch logo be shown?"
58 (defcustom notmuch-hello-logo-background "#5f5f5f"
59 "Background colour for the notmuch logo."
63 (defcustom notmuch-hello-jump-to-search nil
64 "Whether `notmuch-hello' should always jump to the search
69 (defvar notmuch-hello-url "http://notmuchmail.org"
70 "The `notmuch' web site.")
72 (defvar notmuch-hello-recent-searches nil)
74 (defun notmuch-hello-remember-search (search)
75 (if (not (memq search notmuch-hello-recent-searches))
76 (push search notmuch-hello-recent-searches))
77 (if (> (length notmuch-hello-recent-searches)
78 notmuch-hello-recent-searches-max)
79 (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
81 (defun notmuch-hello-trim (search)
83 (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
84 (match-string 1 search)
87 (defun notmuch-hello-search (search)
88 (let ((search (notmuch-hello-trim search)))
89 (notmuch-hello-remember-search search)
90 (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
92 (defun notmuch-hello-add-saved-search (widget)
94 (let ((search (widget-value
96 (widget-get widget :notmuch-saved-search-widget))))
97 (name (completing-read "Name for saved search: "
98 notmuch-hello-saved-searches)))
99 ;; If an existing saved search with this name exists, remove it.
100 (setq notmuch-hello-saved-searches
101 (loop for elem in notmuch-hello-saved-searches
106 (customize-save-variable 'notmuch-hello-saved-searches
107 (push (cons name search)
108 notmuch-hello-saved-searches))
109 (message "Saved '%s' as '%s'." search name)
110 (notmuch-hello-update)))
112 (defun notmuch-hello-longest-label (tag-alist)
113 (or (loop for elem in tag-alist
114 maximize (length (car elem)))
117 (defun notmuch-hello-roundup (dividend divisor)
118 "Return the rounded up value of dividing `dividend' by `divisor'."
119 (+ (/ dividend divisor)
120 (if (> (% dividend divisor) 0) 1 0)))
122 (defun notmuch-hello-reflect (list width)
123 "Reflect a `width' wide matrix represented by `list' along the
126 (let* ((len (length list))
127 (nrows (notmuch-hello-roundup len width)))
128 (loop for row from 0 to (- nrows 1)
129 append (loop for col from 0 to (- width 1)
130 ;; How could we calculate the offset just once
132 if (< (+ (* nrows col) row) len)
133 collect (nth (+ (* nrows col) row) list)
135 ;; Don't forget to insert an empty slot in the
136 ;; output matrix if there is no corresponding
137 ;; value in the input matrix.
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-hello-insert-tags (tag-alist widest)
147 (let* ((tag-format-string (format "%%-%ds " widest))
148 (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 ;; dme: It feels as though there should be a better way to
159 ;; implement this loop than using an incrementing counter.
160 (loop for elem in reordered-list
162 ;; (not elem) indicates an empty slot in the matrix.
164 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
165 (widget-create 'push-button
166 :notify #'notmuch-hello-widget-search
167 :notmuch-search-terms (cdr elem)
168 (format tag-format-string (car elem))))
169 (setq count (1+ count))
170 (if (eq (% count tags-per-line) 0)
171 (widget-insert "\n"))))
173 ;; If the last line was not full (and hence did not include a
174 ;; carriage return), insert one now.
175 (if (not (eq (% count tags-per-line) 0))
176 (widget-insert "\n"))))
178 (defun notmuch-hello-goto-search ()
179 "Put point inside the `search' widget, which we know is first."
181 (goto-char (point-min))
184 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
186 (defun notmuch-hello-search-continuation()
189 (defun notmuch-hello-update (&optional no-display)
190 ;; Lazy - rebuild everything.
192 (notmuch-hello no-display))
194 (defun notmuch-hello (&optional no-display)
198 (set-buffer "*notmuch-hello*")
199 (switch-to-buffer "*notmuch-hello*"))
201 (kill-all-local-variables)
202 (let ((inhibit-read-only t))
205 (let ((all (overlay-lists)))
206 ;; Delete all the overlays.
207 (mapc 'delete-overlay (car all))
208 (mapc 'delete-overlay (cdr all)))
210 (when notmuch-hello-show-logo
211 (let ((image notmuch-hello-logo))
212 ;; dme: Sorry, I don't know any other way to achieve this :-( The
213 ;; notmuch logo uses transparency. That works out badly when
214 ;; inserting the image into an emacs buffer, so force the
215 ;; background colour of the image.
216 (setq image (cons 'image (append (cdr image)
217 `(:background ,notmuch-hello-logo-background))))
218 (insert-image image))
221 (widget-insert "Welcome to ")
222 ;; Hack the display of the links used.
223 (let ((widget-link-prefix "")
224 (widget-link-suffix ""))
226 :notify (lambda (&rest ignore)
227 (browse-url notmuch-hello-url))
228 :help-echo "Visit the notmuch website."
231 (widget-insert "You have ")
233 :notify (lambda (&rest ignore)
234 (notmuch-hello-update))
236 (car (process-lines notmuch-command "count")))
237 (widget-insert " messages (that's not much mail).\n\n"))
239 (let ((start (point)))
240 (widget-insert "Search: ")
241 (widget-create 'editable-field
242 ;; Leave some space at the start and end of the
244 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
245 (length "Search: ")))
246 :action (lambda (widget &rest ignore)
247 (notmuch-hello-search (widget-value widget))))
249 (indent-rigidly start (point) notmuch-hello-indent))
251 (when notmuch-hello-recent-searches
252 (widget-insert "\nRecent searches: ")
253 (widget-create 'push-button
254 :notify (lambda (&rest ignore)
255 (setq notmuch-hello-recent-searches nil)
256 (notmuch-hello-update))
258 (widget-insert "\n\n")
259 (let ((start (point))
261 (mapc '(lambda (search)
262 (widget-insert (format "%2d: " key))
263 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
265 (widget-create 'editable-field
266 ;; Leave some space at the start
267 ;; and end of the search boxes. 4
268 ;; for the accelerator key. 1 for
269 ;; the space before the `save'
270 ;; button. 6 for the `save'
272 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
274 :action (lambda (widget &rest ignore)
275 (notmuch-hello-search (widget-value widget)))
278 (widget-create 'push-button
279 :notify (lambda (widget &rest ignore)
280 (notmuch-hello-add-saved-search widget))
281 :notmuch-saved-search-widget widget-symbol
285 notmuch-hello-recent-searches)
286 (indent-rigidly start (point) notmuch-hello-indent)))
289 ;; Filter out empty saved seaches if required.
290 (if notmuch-hello-show-empty-saved-searches
291 notmuch-hello-saved-searches
292 (loop for elem in notmuch-hello-saved-searches
293 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
295 (saved-widest (notmuch-hello-longest-label saved-alist))
296 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
297 (process-lines notmuch-command "search-tags")))
298 (alltags-widest (notmuch-hello-longest-label alltags-alist))
299 (widest (max saved-widest alltags-widest)))
302 (widget-insert "\nSaved searches: ")
303 (widget-create 'push-button
304 :notify (lambda (&rest ignore)
305 (customize-variable 'notmuch-hello-saved-searches))
307 (widget-insert "\n\n")
308 (let ((start (point)))
309 (notmuch-hello-insert-tags saved-alist widest)
310 (indent-rigidly start (point) notmuch-hello-indent)))
313 (widget-insert "\nAll tags:\n\n")
314 (let ((start (point)))
315 (notmuch-hello-insert-tags alltags-alist widest)
316 (indent-rigidly start (point) notmuch-hello-indent))))
318 (let ((start (point)))
319 (widget-insert "\n\n")
320 (widget-insert "Type a search query and hit RET to view matching threads.\n")
321 (when notmuch-hello-recent-searches
322 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
323 (let ((searches (length notmuch-hello-recent-searches)))
326 "Key 0 acts as an accelerator for the previous query.\n"
327 (format "Keys 0-%d act as accelerators for the previous queries.\n"
329 (widget-insert "Save recent searches with the `save' button.\n"))
330 (when notmuch-hello-saved-searches
331 (widget-insert "Edit saved searches with the `edit' button.\n"))
332 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
333 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
334 (let ((fill-column (- (window-width) notmuch-hello-indent)))
335 (center-region start (point))))
337 (use-local-map widget-keymap)
338 (local-set-key "=" 'notmuch-hello-update)
339 (local-set-key "m" 'notmuch-mua-mail)
340 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
341 (local-set-key "s" 'notmuch-hello-goto-search)
342 (local-set-key "v" '(lambda () (interactive)
343 (message "notmuch version %s" (notmuch-version))))
345 (loop for key from 0 to (- (length notmuch-hello-recent-searches) 1)
346 do (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
347 (local-set-key (number-to-string key)
350 (notmuch-search (widget-value ,widget-symbol)
351 notmuch-search-oldest-first
352 nil nil #'notmuch-hello-search-continuation)))))
355 (if notmuch-hello-jump-to-search
356 (notmuch-hello-goto-search)
357 (goto-char (point-min))))
361 (provide 'notmuch-hello)