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)
29 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
30 (declare-function notmuch-folder-count "notmuch" (search))
32 (defcustom notmuch-hello-recent-searches-max 10
33 "The number of recent searches to store and display."
37 (defcustom notmuch-hello-show-empty-saved-searches nil
38 "Should saved searches with no messages be listed?"
42 (defcustom notmuch-hello-indent 4
43 "How much to indent non-headers."
47 (defcustom notmuch-hello-saved-searches notmuch-folders
48 "A list of saved searches to display."
49 :type '(alist :key-type string :value-type string)
52 (defcustom notmuch-hello-show-logo t
53 "Should the notmuch logo be shown?"
57 (defcustom notmuch-hello-logo-background "#5f5f5f"
58 "Background colour for the notmuch logo."
62 (defcustom notmuch-hello-jump-to-search nil
63 "Whether `notmuch-hello' should always jump to the search
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 (memq search notmuch-hello-recent-searches))
75 (push search notmuch-hello-recent-searches))
76 (if (> (length notmuch-hello-recent-searches)
77 notmuch-hello-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-hello-saved-searches)))
98 ;; If an existing saved search with this name exists, remove it.
99 (setq notmuch-hello-saved-searches
100 (loop for elem in notmuch-hello-saved-searches
105 (customize-save-variable 'notmuch-hello-saved-searches
106 (push (cons name search)
107 notmuch-hello-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-hello-insert-tags (tag-alist widest)
146 (let* ((tag-format-string (format "%%-%ds " widest))
147 (tags-per-line (max 1
148 (/ (- (window-width) notmuch-hello-indent)
149 ;; Count is 7 wide, 1 for the space
153 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
154 ;; Hack the display of the buttons used.
155 (widget-push-button-prefix "")
156 (widget-push-button-suffix ""))
157 ;; dme: It feels as though there should be a better way to
158 ;; implement this loop than using an incrementing counter.
159 (loop for elem in reordered-list
161 ;; (not elem) indicates an empty slot in the matrix.
163 (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
164 (widget-create 'push-button
165 :notify #'notmuch-hello-widget-search
166 :notmuch-search-terms (cdr elem)
167 (format tag-format-string (car elem))))
168 (setq count (1+ count))
169 (if (eq (% count tags-per-line) 0)
170 (widget-insert "\n"))))
172 ;; If the last line was not full (and hence did not include a
173 ;; carriage return), insert one now.
174 (if (not (eq (% count tags-per-line) 0))
175 (widget-insert "\n"))))
177 (defun notmuch-hello-goto-search ()
178 "Put point inside the `search' widget, which we know is first."
180 (goto-char (point-min))
183 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
185 (defun notmuch-hello-search-continuation()
188 (defun notmuch-hello-update (&optional no-display)
189 ;; Lazy - rebuild everything.
191 (notmuch-hello no-display))
193 (defun notmuch-hello (&optional no-display)
197 (set-buffer "*notmuch-hello*")
198 (switch-to-buffer "*notmuch-hello*"))
200 (kill-all-local-variables)
201 (let ((inhibit-read-only t))
204 (let ((all (overlay-lists)))
205 ;; Delete all the overlays.
206 (mapc 'delete-overlay (car all))
207 (mapc 'delete-overlay (cdr all)))
209 (when notmuch-hello-show-logo
210 (let ((image notmuch-hello-logo))
211 ;; dme: Sorry, I don't know any other way to achieve this :-( The
212 ;; notmuch logo uses transparency. That works out badly when
213 ;; inserting the image into an emacs buffer, so force the
214 ;; background colour of the image.
215 (setq image (cons 'image (append (cdr image)
216 `(:background ,notmuch-hello-logo-background))))
217 (insert-image image))
220 (widget-insert "Welcome to ")
221 ;; Hack the display of the links used.
222 (let ((widget-link-prefix "")
223 (widget-link-suffix ""))
225 :notify (lambda (&rest ignore)
226 (browse-url notmuch-hello-url))
227 :help-echo "Visit the notmuch website."
230 (widget-insert "You have ")
232 :notify (lambda (&rest ignore)
233 (notmuch-hello-update))
235 (car (process-lines notmuch-command "count")))
236 (widget-insert " messages (that's not much mail).\n\n"))
238 (let ((start (point)))
239 (widget-insert "Search: ")
240 (widget-create 'editable-field
241 ;; Leave some space at the start and end of the
243 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
244 (length "Search: ")))
245 :action (lambda (widget &rest ignore)
246 (notmuch-hello-search (widget-value widget))))
248 (indent-rigidly start (point) notmuch-hello-indent))
250 (when notmuch-hello-recent-searches
251 (widget-insert "\nRecent searches: ")
252 (widget-create 'push-button
253 :notify (lambda (&rest ignore)
254 (setq notmuch-hello-recent-searches nil)
255 (notmuch-hello-update))
257 (widget-insert "\n\n")
258 (let ((start (point))
260 (mapc '(lambda (search)
261 (widget-insert (format "%2d: " key))
262 (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
264 (widget-create 'editable-field
265 ;; Leave some space at the start
266 ;; and end of the search boxes. 4
267 ;; for the accelerator key. 1 for
268 ;; the space before the `save'
269 ;; button. 6 for the `save'
271 :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
273 :action (lambda (widget &rest ignore)
274 (notmuch-hello-search (widget-value widget)))
277 (widget-create 'push-button
278 :notify (lambda (widget &rest ignore)
279 (notmuch-hello-add-saved-search widget))
280 :notmuch-saved-search-widget widget-symbol
284 notmuch-hello-recent-searches)
285 (indent-rigidly start (point) notmuch-hello-indent)))
288 ;; Filter out empty saved seaches if required.
289 (if notmuch-hello-show-empty-saved-searches
290 notmuch-hello-saved-searches
291 (loop for elem in notmuch-hello-saved-searches
292 if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
294 (saved-widest (notmuch-hello-longest-label saved-alist))
295 (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
296 (process-lines notmuch-command "search-tags")))
297 (alltags-widest (notmuch-hello-longest-label alltags-alist))
298 (widest (max saved-widest alltags-widest)))
301 (widget-insert "\nSaved searches: ")
302 (widget-create 'push-button
303 :notify (lambda (&rest ignore)
304 (customize-variable 'notmuch-hello-saved-searches))
306 (widget-insert "\n\n")
307 (let ((start (point)))
308 (notmuch-hello-insert-tags saved-alist widest)
309 (indent-rigidly start (point) notmuch-hello-indent)))
312 (widget-insert "\nAll tags:\n\n")
313 (let ((start (point)))
314 (notmuch-hello-insert-tags alltags-alist widest)
315 (indent-rigidly start (point) notmuch-hello-indent))))
317 (let ((start (point)))
318 (widget-insert "\n\n")
319 (widget-insert "Type a search query and hit RET to view matching threads.\n")
320 (when notmuch-hello-recent-searches
321 (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
322 (let ((searches (length notmuch-hello-recent-searches)))
325 "Key 0 acts as an accelerator for the previous query.\n"
326 (format "Keys 0-%d act as accelerators for the previous queries.\n"
328 (widget-insert "Save recent searches with the `save' button.\n"))
329 (when notmuch-hello-saved-searches
330 (widget-insert "Edit saved searches with the `edit' button.\n"))
331 (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
332 (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
333 (let ((fill-column (- (window-width) notmuch-hello-indent)))
334 (center-region start (point))))
336 (use-local-map widget-keymap)
337 (local-set-key "=" 'notmuch-hello-update)
338 (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
339 (local-set-key "s" 'notmuch-hello-goto-search)
340 (local-set-key "v" '(lambda () (interactive)
341 (message "notmuch version %s" (notmuch-version))))
343 (loop for key from 0 to (- (length notmuch-hello-recent-searches) 1)
344 do (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
345 (local-set-key (number-to-string key)
348 (notmuch-search (widget-value ,widget-symbol)
349 notmuch-search-oldest-first
350 nil nil #'notmuch-hello-search-continuation)))))
353 (if notmuch-hello-jump-to-search
354 (notmuch-hello-goto-search)
355 (goto-char (point-min))))
359 (provide 'notmuch-hello)