]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: Add notmuch-hello.el, a friendly frontend to notmuch
[notmuch] / emacs / notmuch-hello.el
1 ;; notmuch-hello.el --- welcome to notmuch, a frontend
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (require 'widget)
23 (require 'wid-edit) ; For `widget-forward'.
24 (require 'cl)
25
26 (require 'notmuch-lib)
27 (require 'notmuch)
28
29 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line))
30 (declare-function notmuch-folder-count "notmuch" (search))
31
32 (defcustom notmuch-hello-recent-searches-max 10
33   "The number of recent searches to store and display."
34   :type 'integer
35   :group 'notmuch)
36
37 (defcustom notmuch-hello-show-empty-saved-searches nil
38   "Should saved searches with no messages be listed?"
39   :type 'boolean
40   :group 'notmuch)
41
42 (defcustom notmuch-hello-indent 4
43   "How much to indent non-headers."
44   :type 'integer
45   :group 'notmuch)
46
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)
50   :group 'notmuch)
51
52 (defcustom notmuch-hello-show-logo t
53   "Should the notmuch logo be shown?"
54   :type 'boolean
55   :group 'notmuch)
56
57 (defcustom notmuch-hello-logo-background "#5f5f5f"
58   "Background colour for the notmuch logo."
59   :type 'color
60   :group 'notmuch)
61
62 (defcustom notmuch-hello-jump-to-search nil
63   "Whether `notmuch-hello' should always jump to the search
64 field."
65   :type 'boolean
66   :group 'notmuch)
67
68 (defvar notmuch-hello-url "http://notmuchmail.org"
69   "The `notmuch' web site.")
70
71 (defvar notmuch-hello-recent-searches nil)
72
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))))
79
80 (defun notmuch-hello-trim (search)
81   "Trim whitespace."
82   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
83       (match-string 1 search)
84     search))
85
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)))
90
91 (defun notmuch-hello-add-saved-search (widget)
92   (interactive)
93   (let ((search (widget-value
94                  (symbol-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
101                 if (not (equal name
102                                (car elem)))
103                 collect elem))
104     ;; Add the new one.
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)))
110
111 (defun notmuch-hello-longest-label (tag-alist)
112   (or (loop for elem in tag-alist
113             maximize (length (car elem)))
114       0))
115
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)))
120
121 (defun notmuch-hello-reflect (list width)
122   "Reflect a `width' wide matrix represented by `list' along the
123 diagonal."
124   ;; Not very lispy...
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
130                        ;; per inner-loop?
131                        if (< (+ (* nrows col) row) len)
132                        collect (nth (+ (* nrows col) row) list)
133                        else
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.
137                        collect nil))))
138
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))
144
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
150                                 ;; after the name.
151                                 (+ 7 1 widest))))
152          (count 0)
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
160           do (progn
161                ;; (not elem) indicates an empty slot in the matrix.
162                (when elem
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"))))
171
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"))))
176
177 (defun notmuch-hello-goto-search ()
178   "Put point inside the `search' widget, which we know is first."
179   (interactive)
180   (goto-char (point-min))
181   (widget-forward 3))
182
183 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
184
185 (defun notmuch-hello-search-continuation()
186   (notmuch-hello t))
187
188 (defun notmuch-hello-update (&optional no-display)
189   ;; Lazy - rebuild everything.
190   (interactive)
191   (notmuch-hello no-display))
192
193 (defun notmuch-hello (&optional no-display)
194   (interactive)
195
196   (if no-display
197       (set-buffer "*notmuch-hello*")
198     (switch-to-buffer "*notmuch-hello*"))
199
200   (kill-all-local-variables)
201   (let ((inhibit-read-only t))
202     (erase-buffer))
203
204   (let ((all (overlay-lists)))
205     ;; Delete all the overlays.
206     (mapc 'delete-overlay (car all))
207     (mapc 'delete-overlay (cdr all)))
208
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))
218     (widget-insert "  "))
219
220   (widget-insert "Welcome to ")
221   ;; Hack the display of the links used.
222   (let ((widget-link-prefix "")
223         (widget-link-suffix ""))
224     (widget-create 'link
225                    :notify (lambda (&rest ignore)
226                              (browse-url notmuch-hello-url))
227                    :help-echo "Visit the notmuch website."
228                    "notmuch")
229     (widget-insert ". ")
230     (widget-insert "You have ")
231     (widget-create 'link
232                    :notify (lambda (&rest ignore)
233                              (notmuch-hello-update))
234                    :help-echo "Refresh"
235                    (car (process-lines notmuch-command "count")))
236     (widget-insert " messages (that's not much mail).\n\n"))
237
238   (let ((start (point)))
239     (widget-insert "Search: ")
240     (widget-create 'editable-field
241                    ;; Leave some space at the start and end of the
242                    ;; search boxes.
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))))
247     (widget-insert "\n")
248     (indent-rigidly start (point) notmuch-hello-indent))
249
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))
256                    "clear")
257     (widget-insert "\n\n")
258     (let ((start (point))
259           (key 0))
260       (mapc '(lambda (search)
261                (widget-insert (format "%2d: " key))
262                (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
263                  (set widget-symbol
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'
270                                      ;; button.
271                                      :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
272                                                      4 1 6))
273                                      :action (lambda (widget &rest ignore)
274                                                (notmuch-hello-search (widget-value widget)))
275                                      search))
276                  (widget-insert " ")
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
281                                 "save"))
282                (widget-insert "\n")
283                (setq key (1+ key)))
284             notmuch-hello-recent-searches)
285       (indent-rigidly start (point) notmuch-hello-indent)))
286
287   (let* ((saved-alist
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)
293                   collect elem)))
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)))
299
300     (when saved-alist
301       (widget-insert "\nSaved searches: ")
302       (widget-create 'push-button
303                      :notify (lambda (&rest ignore)
304                                (customize-variable 'notmuch-hello-saved-searches))
305                      "edit")
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)))
310
311     (when alltags-alist
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))))
316
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)))
323         (widget-insert
324          (if (eq 1 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"
327                    (- searches 1)))))
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))))
335
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))))
342
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)
346                             `(lambda ()
347                                (interactive)
348                                (notmuch-search (widget-value ,widget-symbol)
349                                                notmuch-search-oldest-first
350                                                nil nil #'notmuch-hello-search-continuation)))))
351   (widget-setup)
352
353   (if notmuch-hello-jump-to-search
354       (notmuch-hello-goto-search)
355     (goto-char (point-min))))
356
357 ;;
358
359 (provide 'notmuch-hello)