]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: Remove the accelerator keys from the hello buffer
[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 (require 'notmuch-mua)
29
30 (declare-function notmuch-search "notmuch" (query &optional oldest-first target-thread target-line continuation))
31 (declare-function notmuch-folder-count "notmuch" (search))
32
33 (defcustom notmuch-hello-recent-searches-max 10
34   "The number of recent searches to store and display."
35   :type 'integer
36   :group 'notmuch)
37
38 (defcustom notmuch-hello-show-empty-saved-searches nil
39   "Should saved searches with no messages be listed?"
40   :type 'boolean
41   :group 'notmuch)
42
43 (defcustom notmuch-hello-indent 4
44   "How much to indent non-headers."
45   :type 'integer
46   :group 'notmuch)
47
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)
51   :group 'notmuch)
52
53 (defcustom notmuch-hello-show-logo t
54   "Should the notmuch logo be shown?"
55   :type 'boolean
56   :group 'notmuch)
57
58 (defface notmuch-hello-logo-background
59   '((((class color)
60       (background dark))
61      (:background "#5f5f5f"))
62     (((class color)
63       (background light))
64      (:background "white")))
65   "Background colour for the notmuch logo."
66   :group 'notmuch)
67
68 (defcustom notmuch-hello-jump-to-search t
69   "Whether `notmuch-hello' should always jump to the search
70 field."
71   :type 'boolean
72   :group 'notmuch)
73
74 (defvar notmuch-hello-url "http://notmuchmail.org"
75   "The `notmuch' web site.")
76
77 (defvar notmuch-hello-recent-searches nil)
78
79 (defun notmuch-hello-remember-search (search)
80   (if (not (memq search notmuch-hello-recent-searches))
81       (push search notmuch-hello-recent-searches))
82   (if (> (length notmuch-hello-recent-searches)
83          notmuch-hello-recent-searches-max)
84       (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
85
86 (defun notmuch-hello-trim (search)
87   "Trim whitespace."
88   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
89       (match-string 1 search)
90     search))
91
92 (defun notmuch-hello-search (search)
93   (let ((search (notmuch-hello-trim search)))
94     (notmuch-hello-remember-search search)
95     (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
96
97 (defun notmuch-hello-add-saved-search (widget)
98   (interactive)
99   (let ((search (widget-value
100                  (symbol-value
101                   (widget-get widget :notmuch-saved-search-widget))))
102         (name (completing-read "Name for saved search: "
103                                notmuch-hello-saved-searches)))
104     ;; If an existing saved search with this name exists, remove it.
105     (setq notmuch-hello-saved-searches
106           (loop for elem in notmuch-hello-saved-searches
107                 if (not (equal name
108                                (car elem)))
109                 collect elem))
110     ;; Add the new one.
111     (customize-save-variable 'notmuch-hello-saved-searches
112                              (push (cons name search)
113                                    notmuch-hello-saved-searches))
114     (message "Saved '%s' as '%s'." search name)
115     (notmuch-hello-update)))
116
117 (defun notmuch-hello-longest-label (tag-alist)
118   (or (loop for elem in tag-alist
119             maximize (length (car elem)))
120       0))
121
122 (defun notmuch-hello-roundup (dividend divisor)
123   "Return the rounded up value of dividing `dividend' by `divisor'."
124   (+ (/ dividend divisor)
125      (if (> (% dividend divisor) 0) 1 0)))
126
127 (defun notmuch-hello-reflect (list width)
128   "Reflect a `width' wide matrix represented by `list' along the
129 diagonal."
130   ;; Not very lispy...
131   (let* ((len (length list))
132          (nrows (notmuch-hello-roundup len width)))
133     (loop for row from 0 to (- nrows 1)
134           append (loop for col from 0 to (- width 1)
135                        ;; How could we calculate the offset just once
136                        ;; per inner-loop?
137                        if (< (+ (* nrows col) row) len)
138                        collect (nth (+ (* nrows col) row) list)
139                        else
140                        ;; Don't forget to insert an empty slot in the
141                        ;; output matrix if there is no corresponding
142                        ;; value in the input matrix.
143                        collect nil))))
144
145 (defun notmuch-hello-widget-search (widget &rest ignore)
146   (notmuch-search (widget-get widget
147                               :notmuch-search-terms)
148                   notmuch-search-oldest-first
149                   nil nil #'notmuch-hello-search-continuation))
150
151 (defun notmuch-hello-insert-tags (tag-alist widest)
152   (let* ((tag-format-string (format "%%-%ds " widest))
153          (tags-per-line (max 1
154                              (/ (- (window-width) notmuch-hello-indent)
155                                 ;; Count is 7 wide, 1 for the space
156                                 ;; after the name.
157                                 (+ 7 1 widest))))
158          (count 0)
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     ;; dme: It feels as though there should be a better way to
164     ;; implement this loop than using an incrementing counter.
165     (loop for elem in reordered-list
166           do (progn
167                ;; (not elem) indicates an empty slot in the matrix.
168                (when elem
169                  (widget-insert (format "%6s " (notmuch-folder-count (cdr elem))))
170                  (widget-create 'push-button
171                                 :notify #'notmuch-hello-widget-search
172                                 :notmuch-search-terms (cdr elem)
173                                 (format tag-format-string (car elem))))
174                (setq count (1+ count))
175                (if (eq (% count tags-per-line) 0)
176                    (widget-insert "\n"))))
177
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"))))
182
183 (defun notmuch-hello-goto-search ()
184   "Put point inside the `search' widget, which we know is first."
185   (interactive)
186   (goto-char (point-min))
187   (widget-forward 3))
188
189 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
190
191 (defun notmuch-hello-search-continuation()
192   (notmuch-hello t))
193
194 (defun notmuch-hello-update (&optional no-display)
195   ;; Lazy - rebuild everything.
196   (interactive)
197   (notmuch-hello no-display))
198
199 (defun notmuch-hello (&optional no-display)
200   (interactive)
201
202   (if no-display
203       (set-buffer "*notmuch-hello*")
204     (switch-to-buffer "*notmuch-hello*"))
205
206   (kill-all-local-variables)
207   (let ((inhibit-read-only t))
208     (erase-buffer))
209
210   (let ((all (overlay-lists)))
211     ;; Delete all the overlays.
212     (mapc 'delete-overlay (car all))
213     (mapc 'delete-overlay (cdr all)))
214
215   (when notmuch-hello-show-logo
216     (let ((image notmuch-hello-logo))
217       ;; The notmuch logo uses transparency. That can display poorly
218       ;; when inserting the image into an emacs buffer (black logo on
219       ;; a black background), so force the background colour of the
220       ;; image. We use a face to represent the colour so that
221       ;; `defface' can be used to declare the different possible
222       ;; colours, which depend on whether the frame has a light or
223       ;; dark background.
224       (setq image (cons 'image
225                         (append (cdr image)
226                                 (list :background (face-background 'notmuch-hello-logo-background)))))
227       (insert-image image))
228     (widget-insert "  "))
229
230   (widget-insert "Welcome to ")
231   ;; Hack the display of the links used.
232   (let ((widget-link-prefix "")
233         (widget-link-suffix ""))
234     (widget-create 'link
235                    :notify (lambda (&rest ignore)
236                              (browse-url notmuch-hello-url))
237                    :help-echo "Visit the notmuch website."
238                    "notmuch")
239     (widget-insert ". ")
240     (widget-insert "You have ")
241     (widget-create 'link
242                    :notify (lambda (&rest ignore)
243                              (notmuch-hello-update))
244                    :help-echo "Refresh"
245                    (car (process-lines notmuch-command "count")))
246     (widget-insert " messages (that's not much mail).\n\n"))
247
248   (let ((start (point)))
249     (widget-insert "Search: ")
250     (widget-create 'editable-field
251                    ;; Leave some space at the start and end of the
252                    ;; search boxes.
253                    :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
254                                    (length "Search: ")))
255                    :action (lambda (widget &rest ignore)
256                              (notmuch-hello-search (widget-value widget))))
257     (widget-insert "\n")
258     (indent-rigidly start (point) notmuch-hello-indent))
259
260   (when notmuch-hello-recent-searches
261     (widget-insert "\nRecent searches: ")
262     (widget-create 'push-button
263                    :notify (lambda (&rest ignore)
264                              (setq notmuch-hello-recent-searches nil)
265                              (notmuch-hello-update))
266                    "clear")
267     (widget-insert "\n\n")
268     (let ((start (point))
269           (nth 0))
270       (mapc '(lambda (search)
271                (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
272                  (set widget-symbol
273                       (widget-create 'editable-field
274                                      ;; Don't let the search boxes be
275                                      ;; less than 8 characters wide.
276                                      :size (max 8
277                                                 (- (window-width)
278                                                    ;; Leave some space
279                                                    ;; at the start and
280                                                    ;; end of the
281                                                    ;; boxes.
282                                                    (* 2 notmuch-hello-indent)
283                                                    ;; 1 for the space
284                                                    ;; before the
285                                                    ;; `[save]' button. 6
286                                                    ;; for the `[save]'
287                                                    ;; button.
288                                                    1 6))
289                                      :action (lambda (widget &rest ignore)
290                                                (notmuch-hello-search (widget-value widget)))
291                                      search))
292                  (widget-insert " ")
293                  (widget-create 'push-button
294                                 :notify (lambda (widget &rest ignore)
295                                           (notmuch-hello-add-saved-search widget))
296                                 :notmuch-saved-search-widget widget-symbol
297                                 "save"))
298                (widget-insert "\n")
299                (setq nth (1+ nth)))
300             notmuch-hello-recent-searches)
301       (indent-rigidly start (point) notmuch-hello-indent)))
302
303   (let* ((saved-alist
304           ;; Filter out empty saved seaches if required.
305           (if notmuch-hello-show-empty-saved-searches
306               notmuch-hello-saved-searches
307             (loop for elem in notmuch-hello-saved-searches
308                   if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
309                   collect elem)))
310          (saved-widest (notmuch-hello-longest-label saved-alist))
311          (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
312                                 (process-lines notmuch-command "search-tags")))
313          (alltags-widest (notmuch-hello-longest-label alltags-alist))
314          (widest (max saved-widest alltags-widest)))
315
316     (when saved-alist
317       (widget-insert "\nSaved searches: ")
318       (widget-create 'push-button
319                      :notify (lambda (&rest ignore)
320                                (customize-variable 'notmuch-hello-saved-searches))
321                      "edit")
322       (widget-insert "\n\n")
323       (let ((start (point)))
324         (notmuch-hello-insert-tags saved-alist widest)
325         (indent-rigidly start (point) notmuch-hello-indent)))
326
327     (when alltags-alist
328       (widget-insert "\nAll tags:\n\n")
329       (let ((start (point)))
330         (notmuch-hello-insert-tags alltags-alist widest)
331         (indent-rigidly start (point) notmuch-hello-indent))))
332
333   (let ((start (point)))
334     (widget-insert "\n\n")
335     (widget-insert "Type a search query and hit RET to view matching threads.\n")
336     (when notmuch-hello-recent-searches
337       (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
338       (widget-insert "Save recent searches with the `save' button.\n"))
339     (when notmuch-hello-saved-searches
340       (widget-insert "Edit saved searches with the `edit' button.\n"))
341     (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
342     (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
343     (let ((fill-column (- (window-width) notmuch-hello-indent)))
344       (center-region start (point))))
345
346   (use-local-map widget-keymap)
347   (local-set-key "=" 'notmuch-hello-update)
348   (local-set-key "m" 'notmuch-mua-mail)
349   (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
350   (local-set-key "s" 'notmuch-hello-goto-search)
351   (local-set-key "v" '(lambda () (interactive)
352                         (message "notmuch version %s" (notmuch-version))))
353
354   (widget-setup)
355
356   (if notmuch-hello-jump-to-search
357       (notmuch-hello-goto-search)
358     (goto-char (point-min))))
359
360 ;;
361
362 (provide 'notmuch-hello)