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