]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: Make notmuch-hello jumpt to search bar by default.
[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 (defcustom notmuch-hello-logo-background "#5f5f5f"
59   "Background colour for the notmuch logo."
60   :type 'color
61   :group 'notmuch)
62
63 (defcustom notmuch-hello-jump-to-search t
64   "Whether `notmuch-hello' should always jump to the search
65 field."
66   :type 'boolean
67   :group 'notmuch)
68
69 (defvar notmuch-hello-url "http://notmuchmail.org"
70   "The `notmuch' web site.")
71
72 (defvar notmuch-hello-recent-searches nil)
73
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))))
80
81 (defun notmuch-hello-trim (search)
82   "Trim whitespace."
83   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
84       (match-string 1 search)
85     search))
86
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)))
91
92 (defun notmuch-hello-add-saved-search (widget)
93   (interactive)
94   (let ((search (widget-value
95                  (symbol-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
102                 if (not (equal name
103                                (car elem)))
104                 collect elem))
105     ;; Add the new one.
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)))
111
112 (defun notmuch-hello-longest-label (tag-alist)
113   (or (loop for elem in tag-alist
114             maximize (length (car elem)))
115       0))
116
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)))
121
122 (defun notmuch-hello-reflect (list width)
123   "Reflect a `width' wide matrix represented by `list' along the
124 diagonal."
125   ;; Not very lispy...
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
131                        ;; per inner-loop?
132                        if (< (+ (* nrows col) row) len)
133                        collect (nth (+ (* nrows col) row) list)
134                        else
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.
138                        collect nil))))
139
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))
145
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
151                                 ;; after the name.
152                                 (+ 7 1 widest))))
153          (count 0)
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
161           do (progn
162                ;; (not elem) indicates an empty slot in the matrix.
163                (when elem
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"))))
172
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"))))
177
178 (defun notmuch-hello-goto-search ()
179   "Put point inside the `search' widget, which we know is first."
180   (interactive)
181   (goto-char (point-min))
182   (widget-forward 3))
183
184 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
185
186 (defun notmuch-hello-search-continuation()
187   (notmuch-hello t))
188
189 (defun notmuch-hello-update (&optional no-display)
190   ;; Lazy - rebuild everything.
191   (interactive)
192   (notmuch-hello no-display))
193
194 (defun notmuch-hello (&optional no-display)
195   (interactive)
196
197   (if no-display
198       (set-buffer "*notmuch-hello*")
199     (switch-to-buffer "*notmuch-hello*"))
200
201   (kill-all-local-variables)
202   (let ((inhibit-read-only t))
203     (erase-buffer))
204
205   (let ((all (overlay-lists)))
206     ;; Delete all the overlays.
207     (mapc 'delete-overlay (car all))
208     (mapc 'delete-overlay (cdr all)))
209
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))
219     (widget-insert "  "))
220
221   (widget-insert "Welcome to ")
222   ;; Hack the display of the links used.
223   (let ((widget-link-prefix "")
224         (widget-link-suffix ""))
225     (widget-create 'link
226                    :notify (lambda (&rest ignore)
227                              (browse-url notmuch-hello-url))
228                    :help-echo "Visit the notmuch website."
229                    "notmuch")
230     (widget-insert ". ")
231     (widget-insert "You have ")
232     (widget-create 'link
233                    :notify (lambda (&rest ignore)
234                              (notmuch-hello-update))
235                    :help-echo "Refresh"
236                    (car (process-lines notmuch-command "count")))
237     (widget-insert " messages (that's not much mail).\n\n"))
238
239   (let ((start (point)))
240     (widget-insert "Search: ")
241     (widget-create 'editable-field
242                    ;; Leave some space at the start and end of the
243                    ;; search boxes.
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))))
248     (widget-insert "\n")
249     (indent-rigidly start (point) notmuch-hello-indent))
250
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))
257                    "clear")
258     (widget-insert "\n\n")
259     (let ((start (point))
260           (key 0))
261       (mapc '(lambda (search)
262                (widget-insert (format "%2d: " key))
263                (let ((widget-symbol (intern (format "notmuch-hello-search-%d" key))))
264                  (set widget-symbol
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'
271                                      ;; button.
272                                      :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
273                                                      4 1 6))
274                                      :action (lambda (widget &rest ignore)
275                                                (notmuch-hello-search (widget-value widget)))
276                                      search))
277                  (widget-insert " ")
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
282                                 "save"))
283                (widget-insert "\n")
284                (setq key (1+ key)))
285             notmuch-hello-recent-searches)
286       (indent-rigidly start (point) notmuch-hello-indent)))
287
288   (let* ((saved-alist
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)
294                   collect elem)))
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)))
300
301     (when saved-alist
302       (widget-insert "\nSaved searches: ")
303       (widget-create 'push-button
304                      :notify (lambda (&rest ignore)
305                                (customize-variable 'notmuch-hello-saved-searches))
306                      "edit")
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)))
311
312     (when alltags-alist
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))))
317
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)))
324         (widget-insert
325          (if (eq 1 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"
328                    (- searches 1)))))
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))))
336
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))))
344
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)
348                             `(lambda ()
349                                (interactive)
350                                (notmuch-search (widget-value ,widget-symbol)
351                                                notmuch-search-oldest-first
352                                                nil nil #'notmuch-hello-search-continuation)))))
353   (widget-setup)
354
355   (if notmuch-hello-jump-to-search
356       (notmuch-hello-goto-search)
357     (goto-char (point-min))))
358
359 ;;
360
361 (provide 'notmuch-hello)