]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
97db90e51a24875c17184363f0d2cfc03c2f0c92
[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-poll-and-update ()
202   "Invoke `notmuch-poll' to import mail, then refresh the current view."
203   (interactive)
204   (notmuch-poll)
205   (notmuch-hello-update))
206
207 (defun notmuch-hello (&optional no-display)
208   (interactive)
209
210   ;; Provide support for the deprecated name of this variable
211   (if (not notmuch-saved-searches)
212       (setq notmuch-saved-searches notmuch-folders))
213
214   ;; And set a default if neither has been set by the user
215   (if (not notmuch-saved-searches)
216       (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
217                                      ("unread" . "tag:unread"))))
218
219   (if no-display
220       (set-buffer "*notmuch-hello*")
221     (switch-to-buffer "*notmuch-hello*"))
222
223   (let ((target (if (widget-at)
224                    (widget-value (widget-at))
225                  (condition-case nil
226                      (progn
227                        (widget-forward 1)
228                        (widget-value (widget-at)))
229                    (error nil)))))
230
231     (kill-all-local-variables)
232     (let ((inhibit-read-only t))
233       (erase-buffer))
234
235     (let ((all (overlay-lists)))
236       ;; Delete all the overlays.
237       (mapc 'delete-overlay (car all))
238       (mapc 'delete-overlay (cdr all)))
239
240     (when notmuch-show-logo
241       (let ((image notmuch-hello-logo))
242         ;; The notmuch logo uses transparency. That can display poorly
243         ;; when inserting the image into an emacs buffer (black logo on
244         ;; a black background), so force the background colour of the
245         ;; image. We use a face to represent the colour so that
246         ;; `defface' can be used to declare the different possible
247         ;; colours, which depend on whether the frame has a light or
248         ;; dark background.
249         (setq image (cons 'image
250                           (append (cdr image)
251                                   (list :background (face-background 'notmuch-hello-logo-background)))))
252         (insert-image image))
253       (widget-insert "  "))
254
255     (widget-insert "Welcome to ")
256     ;; Hack the display of the links used.
257     (let ((widget-link-prefix "")
258           (widget-link-suffix ""))
259       (widget-create 'link
260                      :notify (lambda (&rest ignore)
261                                (browse-url notmuch-hello-url))
262                      :help-echo "Visit the notmuch website."
263                      "notmuch")
264       (widget-insert ". ")
265       (widget-insert "You have ")
266       (widget-create 'link
267                      :notify (lambda (&rest ignore)
268                                (notmuch-hello-update))
269                      :help-echo "Refresh"
270                      (car (process-lines notmuch-command "count")))
271       (widget-insert " messages (that's not much mail).\n\n"))
272
273     (let ((found-target-pos nil)
274           (final-target-pos nil))
275       (let* ((saved-alist
276               ;; Filter out empty saved seaches if required.
277               (if notmuch-show-empty-saved-searches
278                   notmuch-saved-searches
279                 (loop for elem in notmuch-saved-searches
280                       if (> (string-to-number (notmuch-folder-count (cdr elem))) 0)
281                       collect elem)))
282              (saved-widest (notmuch-hello-longest-label saved-alist))
283              (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
284                                     (process-lines notmuch-command "search-tags")))
285              (alltags-widest (notmuch-hello-longest-label alltags-alist))
286              (widest (max saved-widest alltags-widest)))
287
288         (when saved-alist
289           (widget-insert "Saved searches: ")
290           (widget-create 'push-button
291                          :notify (lambda (&rest ignore)
292                                    (customize-variable 'notmuch-saved-searches))
293                          "edit")
294           (widget-insert "\n\n")
295           (setq final-target-pos (point-marker))
296           (let ((start (point)))
297             (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
298             (if found-target-pos
299                 (setq final-target-pos found-target-pos))
300             (indent-rigidly start (point) notmuch-hello-indent)))
301
302         (let ((start (point)))
303           (widget-insert "\nSearch: ")
304           (setq notmuch-hello-search-bar-marker (point-marker))
305           (widget-create 'editable-field
306                          ;; Leave some space at the start and end of the
307                          ;; search boxes.
308                          :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
309                                          (length "Search: ")))
310                          :action (lambda (widget &rest ignore)
311                                    (notmuch-hello-search (widget-value widget))))
312           (widget-insert "\n")
313           (indent-rigidly start (point) notmuch-hello-indent))
314
315         (when notmuch-hello-recent-searches
316           (widget-insert "\nRecent searches: ")
317           (widget-create 'push-button
318                          :notify (lambda (&rest ignore)
319                                    (setq notmuch-hello-recent-searches nil)
320                                    (notmuch-hello-update))
321                          "clear")
322           (widget-insert "\n\n")
323           (let ((start (point))
324                 (nth 0))
325             (mapc '(lambda (search)
326                      (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
327                        (set widget-symbol
328                             (widget-create 'editable-field
329                                        ;; Don't let the search boxes be
330                                        ;; less than 8 characters wide.
331                                        :size (max 8
332                                                   (- (window-width)
333                                                      ;; Leave some space
334                                                      ;; at the start and
335                                                      ;; end of the
336                                                      ;; boxes.
337                                                      (* 2 notmuch-hello-indent)
338                                                      ;; 1 for the space
339                                                      ;; before the
340                                                      ;; `[save]' button. 6
341                                                      ;; for the `[save]'
342                                                      ;; button.
343                                                      1 6))
344                                        :action (lambda (widget &rest ignore)
345                                                  (notmuch-hello-search (widget-value widget)))
346                                        search))
347                        (widget-insert " ")
348                        (widget-create 'push-button
349                                       :notify (lambda (widget &rest ignore)
350                                                 (notmuch-hello-add-saved-search widget))
351                                       :notmuch-saved-search-widget widget-symbol
352                                       "save"))
353                      (widget-insert "\n")
354                      (setq nth (1+ nth)))
355                   notmuch-hello-recent-searches)
356             (indent-rigidly start (point) notmuch-hello-indent)))
357
358         (when alltags-alist
359           (if notmuch-show-all-tags-list
360               (progn
361                 (widget-insert "\nAll tags: ")
362                 (widget-create 'push-button
363                                :notify (lambda (widget &rest ignore)
364                                          (setq notmuch-show-all-tags-list nil)
365                                          (notmuch-hello-update))
366                                "hide")
367                 (widget-insert "\n\n")
368                 (let ((start (point)))
369                   (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
370                   (if (not final-target-pos)
371                       (setq final-target-pos found-target-pos))
372                   (indent-rigidly start (point) notmuch-hello-indent)))
373             (widget-insert "\n")
374             (widget-create 'push-button
375                            :notify (lambda (widget &rest ignore)
376                                      (setq notmuch-show-all-tags-list t)
377                                      (notmuch-hello-update))
378                            "Show all tags"))))
379
380       (let ((start (point)))
381         (widget-insert "\n\n")
382         (widget-insert "Type a search query and hit RET to view matching threads.\n")
383         (when notmuch-hello-recent-searches
384           (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
385           (widget-insert "Save recent searches with the `save' button.\n"))
386         (when notmuch-saved-searches
387           (widget-insert "Edit saved searches with the `edit' button.\n"))
388         (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
389         (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
390         (let ((fill-column (- (window-width) notmuch-hello-indent)))
391           (center-region start (point))))
392
393       (use-local-map widget-keymap)
394       (local-set-key "=" 'notmuch-hello-update)
395       (local-set-key "G" 'notmuch-hello-poll-and-update)
396       (local-set-key "m" 'notmuch-mua-mail)
397       (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
398       (local-set-key "s" 'notmuch-hello-goto-search)
399       (local-set-key "v" '(lambda () (interactive)
400                             (message "notmuch version %s" (notmuch-version))))
401
402       (widget-setup)
403
404       (goto-char final-target-pos)
405       (if (not (widget-at))
406           (widget-forward 1)))))
407
408 ;;
409
410 (provide 'notmuch-hello)