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