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