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