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