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