]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
notmuch-hello: Make this work with a notmuch-folders variable set in ~/.emacs
[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-recent-searches-max 10
36   "The number of recent searches to store and display."
37   :type 'integer
38   :group 'notmuch)
39
40 (defcustom notmuch-show-empty-saved-searches nil
41   "Should saved searches with no messages be listed?"
42   :type 'boolean
43   :group 'notmuch)
44
45 (defvar notmuch-hello-indent 4
46   "How much to indent non-headers.")
47
48 (defcustom notmuch-saved-searches nil
49   "A list of saved searches to display."
50   :type '(alist :key-type string :value-type string)
51   :group 'notmuch)
52
53 (defcustom notmuch-show-logo t
54   "Should the notmuch logo be shown?"
55   :type 'boolean
56   :group 'notmuch)
57
58 (defcustom notmuch-show-all-tags-list nil
59   "Should all tags be shown in the notmuch-hello view?"
60   :type 'boolean
61   :group 'notmuch)
62
63 (defface notmuch-hello-logo-background
64   '((((class color)
65       (background dark))
66      (:background "#5f5f5f"))
67     (((class color)
68       (background light))
69      (:background "white")))
70   "Background colour for the notmuch logo."
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-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-saved-searches)))
103     ;; If an existing saved search with this name exists, remove it.
104     (setq notmuch-saved-searches
105           (loop for elem in notmuch-saved-searches
106                 if (not (equal name
107                                (car elem)))
108                 collect elem))
109     ;; Add the new one.
110     (customize-save-variable 'notmuch-saved-searches
111                              (push (cons name search)
112                                    notmuch-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 target)
151   (let* ((tags-per-line (max 1
152                              (/ (- (window-width) notmuch-hello-indent)
153                                 ;; Count is 7 wide, 1 for the space
154                                 ;; after the name.
155                                 (+ 7 1 widest))))
156          (count 0)
157          (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
158          ;; Hack the display of the buttons used.
159          (widget-push-button-prefix "")
160          (widget-push-button-suffix "")
161          (found-target-pos nil))
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                  (if (string= (format "%s " (car elem)) target)
170                      (setq found-target-pos (point-marker)))
171                  (widget-create 'push-button
172                                 :notify #'notmuch-hello-widget-search
173                                 :notmuch-search-terms (cdr elem)
174                                 (format "%s " (car elem)))
175                  (insert (make-string (1+ (- widest (length (car elem)))) ? )))
176                (setq count (1+ count))
177                (if (eq (% count tags-per-line) 0)
178                    (widget-insert "\n"))))
179
180     ;; If the last line was not full (and hence did not include a
181     ;; carriage return), insert one now.
182     (if (not (eq (% count tags-per-line) 0))
183         (widget-insert "\n"))
184     found-target-pos))
185
186 (defun notmuch-hello-goto-search ()
187   "Put point inside the `search' widget."
188   (interactive)
189   (goto-char notmuch-hello-search-bar-marker))
190
191 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
192
193 (defun notmuch-hello-search-continuation()
194   (notmuch-hello-update t))
195
196 (defun notmuch-hello-update (&optional no-display)
197   ;; Lazy - rebuild everything.
198   (interactive)
199   (notmuch-hello no-display))
200
201 (defun notmuch-hello (&optional no-display)
202   (interactive)
203
204   ;; Provide support for the deprecated name of this variable
205   (if (not notmuch-saved-searches)
206       (setq notmuch-saved-searches notmuch-folders))
207
208   ;; And set a default if neither has been set by the user
209   (if (not notmuch-saved-searches)
210       (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
211                                      ("unread" . "tag:unread"))))
212
213   (if no-display
214       (set-buffer "*notmuch-hello*")
215     (switch-to-buffer "*notmuch-hello*"))
216
217   (let ((target (if (widget-at)
218                    (widget-value (widget-at))
219                  (condition-case nil
220                      (progn
221                        (widget-forward 1)
222                        (widget-value (widget-at)))
223                    (error nil)))))
224
225     (kill-all-local-variables)
226     (let ((inhibit-read-only t))
227       (erase-buffer))
228
229     (let ((all (overlay-lists)))
230       ;; Delete all the overlays.
231       (mapc 'delete-overlay (car all))
232       (mapc 'delete-overlay (cdr all)))
233
234     (when notmuch-show-logo
235       (let ((image notmuch-hello-logo))
236         ;; The notmuch logo uses transparency. That can display poorly
237         ;; when inserting the image into an emacs buffer (black logo on
238         ;; a black background), so force the background colour of the
239         ;; image. We use a face to represent the colour so that
240         ;; `defface' can be used to declare the different possible
241         ;; colours, which depend on whether the frame has a light or
242         ;; dark background.
243         (setq image (cons 'image
244                           (append (cdr image)
245                                   (list :background (face-background 'notmuch-hello-logo-background)))))
246         (insert-image image))
247       (widget-insert "  "))
248
249     (widget-insert "Welcome to ")
250     ;; Hack the display of the links used.
251     (let ((widget-link-prefix "")
252           (widget-link-suffix ""))
253       (widget-create 'link
254                      :notify (lambda (&rest ignore)
255                                (browse-url notmuch-hello-url))
256                      :help-echo "Visit the notmuch website."
257                      "notmuch")
258       (widget-insert ". ")
259       (widget-insert "You have ")
260       (widget-create 'link
261                      :notify (lambda (&rest ignore)
262                                (notmuch-hello-update))
263                      :help-echo "Refresh"
264                      (car (process-lines notmuch-command "count")))
265       (widget-insert " messages (that's not much mail).\n\n"))
266
267     (let ((found-target-pos nil)
268           (final-target-pos nil))
269       (let* ((saved-alist
270               ;; Filter out empty saved seaches if required.
271               (if notmuch-show-empty-saved-searches
272                   notmuch-saved-searches
273                 (loop for elem in notmuch-saved-searches
274                       if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
275                       collect elem)))
276              (saved-widest (notmuch-hello-longest-label saved-alist))
277              (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
278                                     (process-lines notmuch-command "search-tags")))
279              (alltags-widest (notmuch-hello-longest-label alltags-alist))
280              (widest (max saved-widest alltags-widest)))
281
282         (when saved-alist
283           (widget-insert "Saved searches: ")
284           (widget-create 'push-button
285                          :notify (lambda (&rest ignore)
286                                    (customize-variable 'notmuch-saved-searches))
287                          "edit")
288           (widget-insert "\n\n")
289           (setq final-target-pos (point-marker))
290           (let ((start (point)))
291             (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
292             (if found-target-pos
293                 (setq final-target-pos found-target-pos))
294             (indent-rigidly start (point) notmuch-hello-indent)))
295
296         (let ((start (point)))
297           (widget-insert "\nSearch: ")
298           (setq notmuch-hello-search-bar-marker (point-marker))
299           (widget-create 'editable-field
300                          ;; Leave some space at the start and end of the
301                          ;; search boxes.
302                          :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
303                                          (length "Search: ")))
304                          :action (lambda (widget &rest ignore)
305                                    (notmuch-hello-search (widget-value widget))))
306           (widget-insert "\n")
307           (indent-rigidly start (point) notmuch-hello-indent))
308
309         (when notmuch-hello-recent-searches
310           (widget-insert "\nRecent searches: ")
311           (widget-create 'push-button
312                          :notify (lambda (&rest ignore)
313                                    (setq notmuch-hello-recent-searches nil)
314                                    (notmuch-hello-update))
315                          "clear")
316           (widget-insert "\n\n")
317           (let ((start (point))
318                 (nth 0))
319             (mapc '(lambda (search)
320                      (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
321                        (set widget-symbol
322                             (widget-create 'editable-field
323                                        ;; Don't let the search boxes be
324                                        ;; less than 8 characters wide.
325                                        :size (max 8
326                                                   (- (window-width)
327                                                      ;; Leave some space
328                                                      ;; at the start and
329                                                      ;; end of the
330                                                      ;; boxes.
331                                                      (* 2 notmuch-hello-indent)
332                                                      ;; 1 for the space
333                                                      ;; before the
334                                                      ;; `[save]' button. 6
335                                                      ;; for the `[save]'
336                                                      ;; button.
337                                                      1 6))
338                                        :action (lambda (widget &rest ignore)
339                                                  (notmuch-hello-search (widget-value widget)))
340                                        search))
341                        (widget-insert " ")
342                        (widget-create 'push-button
343                                       :notify (lambda (widget &rest ignore)
344                                                 (notmuch-hello-add-saved-search widget))
345                                       :notmuch-saved-search-widget widget-symbol
346                                       "save"))
347                      (widget-insert "\n")
348                      (setq nth (1+ nth)))
349                   notmuch-hello-recent-searches)
350             (indent-rigidly start (point) notmuch-hello-indent)))
351
352         (when alltags-alist
353           (if notmuch-show-all-tags-list
354               (progn
355                 (widget-insert "\nAll tags: ")
356                 (widget-create 'push-button
357                                :notify (lambda (widget &rest ignore)
358                                          (setq notmuch-show-all-tags-list nil)
359                                          (notmuch-hello-update))
360                                "hide")
361                 (widget-insert "\n\n")
362                 (let ((start (point)))
363                   (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
364                   (if (not final-target-pos)
365                       (setq final-target-pos found-target-pos))
366                   (indent-rigidly start (point) notmuch-hello-indent)))
367             (widget-insert "\n")
368             (widget-create 'push-button
369                            :notify (lambda (widget &rest ignore)
370                                      (setq notmuch-show-all-tags-list t)
371                                      (notmuch-hello-update))
372                            "Show all tags"))))
373
374       (let ((start (point)))
375         (widget-insert "\n\n")
376         (widget-insert "Type a search query and hit RET to view matching threads.\n")
377         (when notmuch-hello-recent-searches
378           (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
379           (widget-insert "Save recent searches with the `save' button.\n"))
380         (when notmuch-saved-searches
381           (widget-insert "Edit saved searches with the `edit' button.\n"))
382         (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
383         (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
384         (let ((fill-column (- (window-width) notmuch-hello-indent)))
385           (center-region start (point))))
386
387       (use-local-map widget-keymap)
388       (local-set-key "=" 'notmuch-hello-update)
389       (local-set-key "m" 'notmuch-mua-mail)
390       (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
391       (local-set-key "s" 'notmuch-hello-goto-search)
392       (local-set-key "v" '(lambda () (interactive)
393                             (message "notmuch version %s" (notmuch-version))))
394
395       (widget-setup)
396
397       (goto-char final-target-pos)
398       (if (not (widget-at))
399           (widget-forward 1)))))
400
401 ;;
402
403 (provide 'notmuch-hello)