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