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