]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: Take advantage of position-remembering when returning to 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 (defcustom notmuch-hello-jump-to-search nil
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 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= (car elem) target)
170                      (progn
171                        (setq found-target-pos (point-marker))))
172                  (widget-create 'push-button
173                                 :notify #'notmuch-hello-widget-search
174                                 :notmuch-search-terms (cdr elem)
175                                 (car elem))
176                  (insert (make-string (- widest (length (car elem))) ? )))
177                (setq count (1+ count))
178                (if (eq (% count tags-per-line) 0)
179                    (widget-insert "\n"))))
180
181     ;; If the last line was not full (and hence did not include a
182     ;; carriage return), insert one now.
183     (if (not (eq (% count tags-per-line) 0))
184         (widget-insert "\n"))
185     found-target-pos))
186
187 (defun notmuch-hello-goto-search ()
188   "Put point inside the `search' widget, which we know is first."
189   (interactive)
190   (goto-char (point-min))
191   (widget-forward 3))
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   (let ((target (if (widget-at)
202                    (widget-value (widget-at))
203                  (progn
204                    (widget-forward 1)
205                    (widget-value (widget-at))))))
206     (notmuch-hello no-display target)))
207
208 (defun notmuch-hello (&optional no-display target)
209   (interactive)
210
211   (if no-display
212       (set-buffer "*notmuch-hello*")
213     (switch-to-buffer "*notmuch-hello*"))
214
215   (kill-all-local-variables)
216   (let ((inhibit-read-only t))
217     (erase-buffer))
218
219   (let ((all (overlay-lists)))
220     ;; Delete all the overlays.
221     (mapc 'delete-overlay (car all))
222     (mapc 'delete-overlay (cdr all)))
223
224   (when notmuch-hello-show-logo
225     (let ((image notmuch-hello-logo))
226       ;; The notmuch logo uses transparency. That can display poorly
227       ;; when inserting the image into an emacs buffer (black logo on
228       ;; a black background), so force the background colour of the
229       ;; image. We use a face to represent the colour so that
230       ;; `defface' can be used to declare the different possible
231       ;; colours, which depend on whether the frame has a light or
232       ;; dark background.
233       (setq image (cons 'image
234                         (append (cdr image)
235                                 (list :background (face-background 'notmuch-hello-logo-background)))))
236       (insert-image image))
237     (widget-insert "  "))
238
239   (widget-insert "Welcome to ")
240   ;; Hack the display of the links used.
241   (let ((widget-link-prefix "")
242         (widget-link-suffix ""))
243     (widget-create 'link
244                    :notify (lambda (&rest ignore)
245                              (browse-url notmuch-hello-url))
246                    :help-echo "Visit the notmuch website."
247                    "notmuch")
248     (widget-insert ". ")
249     (widget-insert "You have ")
250     (widget-create 'link
251                    :notify (lambda (&rest ignore)
252                              (notmuch-hello-update))
253                    :help-echo "Refresh"
254                    (car (process-lines notmuch-command "count")))
255     (widget-insert " messages (that's not much mail).\n\n"))
256
257   (let ((start (point)))
258     (widget-insert "Search: ")
259     (widget-create 'editable-field
260                    ;; Leave some space at the start and end of the
261                    ;; search boxes.
262                    :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
263                                    (length "Search: ")))
264                    :action (lambda (widget &rest ignore)
265                              (notmuch-hello-search (widget-value widget))))
266     (widget-insert "\n")
267     (indent-rigidly start (point) notmuch-hello-indent))
268
269   (when notmuch-hello-recent-searches
270     (widget-insert "\nRecent searches: ")
271     (widget-create 'push-button
272                    :notify (lambda (&rest ignore)
273                              (setq notmuch-hello-recent-searches nil)
274                              (notmuch-hello-update))
275                    "clear")
276     (widget-insert "\n\n")
277     (let ((start (point))
278           (nth 0))
279       (mapc '(lambda (search)
280                (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
281                  (set widget-symbol
282                       (widget-create 'editable-field
283                                      ;; Don't let the search boxes be
284                                      ;; less than 8 characters wide.
285                                      :size (max 8
286                                                 (- (window-width)
287                                                    ;; Leave some space
288                                                    ;; at the start and
289                                                    ;; end of the
290                                                    ;; boxes.
291                                                    (* 2 notmuch-hello-indent)
292                                                    ;; 1 for the space
293                                                    ;; before the
294                                                    ;; `[save]' button. 6
295                                                    ;; for the `[save]'
296                                                    ;; button.
297                                                    1 6))
298                                      :action (lambda (widget &rest ignore)
299                                                (notmuch-hello-search (widget-value widget)))
300                                      search))
301                  (widget-insert " ")
302                  (widget-create 'push-button
303                                 :notify (lambda (widget &rest ignore)
304                                           (notmuch-hello-add-saved-search widget))
305                                 :notmuch-saved-search-widget widget-symbol
306                                 "save"))
307                (widget-insert "\n")
308                (setq nth (1+ nth)))
309             notmuch-hello-recent-searches)
310       (indent-rigidly start (point) notmuch-hello-indent)))
311
312   (let ((found-target-pos nil)
313         (final-target-pos nil))
314     (let* ((saved-alist
315             ;; Filter out empty saved seaches if required.
316             (if notmuch-hello-show-empty-saved-searches
317                 notmuch-hello-saved-searches
318               (loop for elem in notmuch-hello-saved-searches
319                     if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
320                     collect elem)))
321            (saved-widest (notmuch-hello-longest-label saved-alist))
322            (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
323                                   (process-lines notmuch-command "search-tags")))
324            (alltags-widest (notmuch-hello-longest-label alltags-alist))
325            (widest (max saved-widest alltags-widest)))
326
327       (when saved-alist
328         (widget-insert "\nSaved searches: ")
329         (widget-create 'push-button
330                        :notify (lambda (&rest ignore)
331                                  (customize-variable 'notmuch-hello-saved-searches))
332                        "edit")
333         (widget-insert "\n\n")
334         (let ((start (point)))
335           (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
336           (if (not final-target-pos)
337               (setq final-target-pos found-target-pos))
338           (indent-rigidly start (point) notmuch-hello-indent)))
339
340       (when alltags-alist
341         (widget-insert "\nAll tags:\n\n")
342         (let ((start (point)))
343           (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
344           (if (not final-target-pos)
345               (setq final-target-pos found-target-pos))
346           (indent-rigidly start (point) notmuch-hello-indent))))
347
348     (let ((start (point)))
349       (widget-insert "\n\n")
350       (widget-insert "Type a search query and hit RET to view matching threads.\n")
351       (when notmuch-hello-recent-searches
352         (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
353         (widget-insert "Save recent searches with the `save' button.\n"))
354       (when notmuch-hello-saved-searches
355         (widget-insert "Edit saved searches with the `edit' button.\n"))
356       (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
357       (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
358       (let ((fill-column (- (window-width) notmuch-hello-indent)))
359         (center-region start (point))))
360
361     (use-local-map widget-keymap)
362     (local-set-key "=" 'notmuch-hello-update)
363     (local-set-key "m" 'notmuch-mua-mail)
364     (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
365     (local-set-key "s" 'notmuch-hello-goto-search)
366     (local-set-key "v" '(lambda () (interactive)
367                           (message "notmuch version %s" (notmuch-version))))
368
369     (widget-setup)
370
371     (if final-target-pos
372         (goto-char final-target-pos)
373       (if notmuch-hello-jump-to-search
374           (notmuch-hello-goto-search)
375         (goto-char (point-min))))))
376
377 ;;
378
379 (provide 'notmuch-hello)