]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: notmuch-hello: Move to first saved search item.
[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         (setq final-target-pos (point-marker))
276         (let ((start (point)))
277           (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
278           (if found-target-pos
279               (setq final-target-pos found-target-pos))
280           (indent-rigidly start (point) notmuch-hello-indent)))
281
282       (let ((start (point)))
283         (widget-insert "\nSearch: ")
284         (setq notmuch-hello-search-bar-marker (point-marker))
285         (widget-create 'editable-field
286                        ;; Leave some space at the start and end of the
287                        ;; search boxes.
288                        :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
289                                        (length "Search: ")))
290                        :action (lambda (widget &rest ignore)
291                                  (notmuch-hello-search (widget-value widget))))
292         (widget-insert "\n")
293         (indent-rigidly start (point) notmuch-hello-indent))
294
295       (when notmuch-hello-recent-searches
296         (widget-insert "\nRecent searches: ")
297         (widget-create 'push-button
298                        :notify (lambda (&rest ignore)
299                                  (setq notmuch-hello-recent-searches nil)
300                                  (notmuch-hello-update))
301                        "clear")
302         (widget-insert "\n\n")
303         (let ((start (point))
304               (nth 0))
305           (mapc '(lambda (search)
306                    (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
307                      (set widget-symbol
308                           (widget-create 'editable-field
309                                          ;; Don't let the search boxes be
310                                          ;; less than 8 characters wide.
311                                          :size (max 8
312                                                     (- (window-width)
313                                                        ;; Leave some space
314                                                        ;; at the start and
315                                                        ;; end of the
316                                                        ;; boxes.
317                                                        (* 2 notmuch-hello-indent)
318                                                        ;; 1 for the space
319                                                        ;; before the
320                                                        ;; `[save]' button. 6
321                                                        ;; for the `[save]'
322                                                        ;; button.
323                                                        1 6))
324                                          :action (lambda (widget &rest ignore)
325                                                    (notmuch-hello-search (widget-value widget)))
326                                          search))
327                      (widget-insert " ")
328                      (widget-create 'push-button
329                                     :notify (lambda (widget &rest ignore)
330                                               (notmuch-hello-add-saved-search widget))
331                                     :notmuch-saved-search-widget widget-symbol
332                                     "save"))
333                    (widget-insert "\n")
334                    (setq nth (1+ nth)))
335                 notmuch-hello-recent-searches)
336           (indent-rigidly start (point) notmuch-hello-indent)))
337
338       (when alltags-alist
339         (widget-insert "\nAll tags:\n\n")
340         (let ((start (point)))
341           (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
342           (if (not final-target-pos)
343               (setq final-target-pos found-target-pos))
344           (indent-rigidly start (point) notmuch-hello-indent))))
345
346     (let ((start (point)))
347       (widget-insert "\n\n")
348       (widget-insert "Type a search query and hit RET to view matching threads.\n")
349       (when notmuch-hello-recent-searches
350         (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
351         (widget-insert "Save recent searches with the `save' button.\n"))
352       (when notmuch-hello-saved-searches
353         (widget-insert "Edit saved searches with the `edit' button.\n"))
354       (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
355       (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
356       (let ((fill-column (- (window-width) notmuch-hello-indent)))
357         (center-region start (point))))
358
359     (use-local-map widget-keymap)
360     (local-set-key "=" 'notmuch-hello-update)
361     (local-set-key "m" 'notmuch-mua-mail)
362     (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
363     (local-set-key "s" 'notmuch-hello-goto-search)
364     (local-set-key "v" '(lambda () (interactive)
365                           (message "notmuch version %s" (notmuch-version))))
366
367     (widget-setup)
368
369     (goto-char final-target-pos)
370     (if (not (widget-at))
371         (widget-forward 1))))
372
373 ;;
374
375 (provide 'notmuch-hello)