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