]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
Merge branch 'release'
[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 (eval-when-compile (require 'cl))
23 (require 'widget)
24 (require 'wid-edit) ; For `widget-forward'.
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 (defun notmuch-sort-saved-searches (alist)
46   "Generate an alphabetically sorted saved searches alist."
47   (sort alist (lambda (a b) (string< (car a) (car b)))))
48
49 (defcustom notmuch-saved-search-sort-function nil
50   "Function used to sort the saved searches for the notmuch-hello view.
51
52 This variable controls how saved searches should be sorted. No
53 sorting (nil) displays the saved searches in the order they are
54 stored in `notmuch-saved-searches'. Sort alphabetically sorts the
55 saved searches in alphabetical order. Custom sort function should
56 be a function or a lambda expression that takes the saved
57 searches alist as a parameter, and returns a new saved searches
58 alist to be used."
59   :type '(choice (const :tag "No sorting" nil)
60                  (const :tag "Sort alphabetically" notmuch-sort-saved-searches)
61                  (function :tag "Custom sort function"
62                            :value notmuch-sort-saved-searches))
63   :group 'notmuch)
64
65 (defvar notmuch-hello-indent 4
66   "How much to indent non-headers.")
67
68 (defcustom notmuch-show-logo t
69   "Should the notmuch logo be shown?"
70   :type 'boolean
71   :group 'notmuch)
72
73 (defcustom notmuch-show-all-tags-list nil
74   "Should all tags be shown in the notmuch-hello view?"
75   :type 'boolean
76   :group 'notmuch)
77
78 (defcustom notmuch-hello-tag-list-make-query nil
79   "Function or string to generate queries for the all tags list.
80
81 This variable controls which query results are shown for each tag
82 in the \"all tags\" list. If nil, it will use all messages with
83 that tag. If this is set to a string, it is used as a filter for
84 messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
85 Finally this can be a function that will be called for each tag and
86 should return a filter for that tag, or nil to hide the tag."
87   :type '(choice (const :tag "All messages" nil)
88                  (const :tag "Unread messages" "tag:unread")
89                  (const :tag "Custom filter" string)
90                  (const :tag "Custom filter function" function))
91   :group 'notmuch)
92
93 (defcustom notmuch-hello-hide-tags nil
94   "List of tags to be hidden in the \"all tags\"-section."
95   :type '(repeat string)
96   :group 'notmuch)
97
98 (defface notmuch-hello-logo-background
99   '((((class color)
100       (background dark))
101      (:background "#5f5f5f"))
102     (((class color)
103       (background light))
104      (:background "white")))
105   "Background colour for the notmuch logo."
106   :group 'notmuch)
107
108 (defcustom notmuch-column-control t
109   "Controls the number of columns for saved searches/tags in notmuch view.
110
111 This variable has three potential sets of values:
112
113 - t: automatically calculate the number of columns possible based
114   on the tags to be shown and the window width,
115 - an integer: a lower bound on the number of characters that will
116   be used to display each column,
117 - a float: a fraction of the window width that is the lower bound
118   on the number of characters that should be used for each
119   column.
120
121 So:
122 - if you would like two columns of tags, set this to 0.5.
123 - if you would like a single column of tags, set this to 1.0.
124 - if you would like tags to be 30 characters wide, set this to
125   30.
126 - if you don't want to worry about all of this nonsense, leave
127   this set to `t'."
128   :group 'notmuch
129   :type '(choice
130           (const :tag "Automatically calculated" t)
131           (integer :tag "Number of characters")
132           (float :tag "Fraction of window")))
133
134 (defcustom notmuch-decimal-separator ","
135   "The string used as a decimal separator.
136
137 Typically \",\" in the US and UK and \".\" in Europe."
138   :group 'notmuch
139   :type 'string)
140
141 (defvar notmuch-hello-url "http://notmuchmail.org"
142   "The `notmuch' web site.")
143
144 (defvar notmuch-hello-recent-searches nil)
145
146 (defun notmuch-hello-remember-search (search)
147   (if (not (member search notmuch-hello-recent-searches))
148       (push search notmuch-hello-recent-searches))
149   (if (> (length notmuch-hello-recent-searches)
150          notmuch-recent-searches-max)
151       (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
152
153 (defun notmuch-hello-nice-number (n)
154   (let (result)
155     (while (> n 0)
156       (push (% n 1000) result)
157       (setq n (/ n 1000)))
158     (setq result (or result '(0)))
159     (apply #'concat
160      (number-to-string (car result))
161      (mapcar (lambda (elem)
162               (format "%s%03d" notmuch-decimal-separator elem))
163              (cdr result)))))
164
165 (defun notmuch-hello-trim (search)
166   "Trim whitespace."
167   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
168       (match-string 1 search)
169     search))
170
171 (defun notmuch-hello-search (search)
172   (let ((search (notmuch-hello-trim search)))
173     (notmuch-hello-remember-search search)
174     (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
175
176 (defun notmuch-hello-add-saved-search (widget)
177   (interactive)
178   (let ((search (widget-value
179                  (symbol-value
180                   (widget-get widget :notmuch-saved-search-widget))))
181         (name (completing-read "Name for saved search: "
182                                notmuch-saved-searches)))
183     ;; If an existing saved search with this name exists, remove it.
184     (setq notmuch-saved-searches
185           (loop for elem in notmuch-saved-searches
186                 if (not (equal name
187                                (car elem)))
188                 collect elem))
189     ;; Add the new one.
190     (customize-save-variable 'notmuch-saved-searches
191                              (add-to-list 'notmuch-saved-searches
192                                           (cons name search) t))
193     (message "Saved '%s' as '%s'." search name)
194     (notmuch-hello-update)))
195
196 (defun notmuch-hello-longest-label (tag-alist)
197   (or (loop for elem in tag-alist
198             maximize (length (car elem)))
199       0))
200
201 (defun notmuch-hello-reflect-generate-row (ncols nrows row list)
202   (let ((len (length list)))
203     (loop for col from 0 to (- ncols 1)
204           collect (let ((offset (+ (* nrows col) row)))
205                     (if (< offset len)
206                         (nth offset list)
207                       ;; Don't forget to insert an empty slot in the
208                       ;; output matrix if there is no corresponding
209                       ;; value in the input matrix.
210                       nil)))))
211
212 (defun notmuch-hello-reflect (list ncols)
213   "Reflect a `ncols' wide matrix represented by `list' along the
214 diagonal."
215   ;; Not very lispy...
216   (let ((nrows (ceiling (length list) ncols)))
217     (loop for row from 0 to (- nrows 1)
218           append (notmuch-hello-reflect-generate-row ncols nrows row list))))
219
220 (defun notmuch-hello-widget-search (widget &rest ignore)
221   (notmuch-search (widget-get widget
222                               :notmuch-search-terms)
223                   notmuch-search-oldest-first
224                   nil nil #'notmuch-hello-search-continuation))
225
226 (defun notmuch-saved-search-count (search)
227   (car (process-lines notmuch-command "count" search)))
228
229 (defun notmuch-hello-tags-per-line (widest)
230   "Determine how many tags to show per line and how wide they
231 should be. Returns a cons cell `(tags-per-line width)'."
232   (let ((tags-per-line
233          (cond
234           ((integerp notmuch-column-control)
235            (max 1
236                 (/ (- (window-width) notmuch-hello-indent)
237                    ;; Count is 9 wide (8 digits plus space), 1 for the space
238                    ;; after the name.
239                    (+ 9 1 (max notmuch-column-control widest)))))
240
241           ((floatp notmuch-column-control)
242            (let* ((available-width (- (window-width) notmuch-hello-indent))
243                   (proposed-width (max (* available-width notmuch-column-control) widest)))
244              (floor available-width proposed-width)))
245
246           (t
247            (max 1
248                 (/ (- (window-width) notmuch-hello-indent)
249                    ;; Count is 9 wide (8 digits plus space), 1 for the space
250                    ;; after the name.
251                    (+ 9 1 widest)))))))
252
253     (cons tags-per-line (/ (max 1
254                                 (- (window-width) notmuch-hello-indent
255                                    ;; Count is 9 wide (8 digits plus
256                                    ;; space), 1 for the space after the
257                                    ;; name.
258                                    (* tags-per-line (+ 9 1))))
259                            tags-per-line))))
260
261 (defun notmuch-hello-insert-tags (tag-alist widest target)
262   (let* ((tags-and-width (notmuch-hello-tags-per-line widest))
263          (tags-per-line (car tags-and-width))
264          (widest (cdr tags-and-width))
265          (count 0)
266          (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
267          ;; Hack the display of the buttons used.
268          (widget-push-button-prefix "")
269          (widget-push-button-suffix "")
270          (found-target-pos nil))
271     ;; dme: It feels as though there should be a better way to
272     ;; implement this loop than using an incrementing counter.
273     (mapc (lambda (elem)
274             ;; (not elem) indicates an empty slot in the matrix.
275             (when elem
276               (let* ((name (car elem))
277                      (query (cdr elem))
278                      (formatted-name (format "%s " name)))
279                 (widget-insert (format "%8s "
280                                        (notmuch-hello-nice-number
281                                         (string-to-number (notmuch-saved-search-count query)))))
282                 (if (string= formatted-name target)
283                     (setq found-target-pos (point-marker)))
284                 (widget-create 'push-button
285                                :notify #'notmuch-hello-widget-search
286                                :notmuch-search-terms query
287                                formatted-name)
288                 ;; Insert enough space to consume the rest of the
289                 ;; column.  Because the button for the name is `(1+
290                 ;; (length name))' long (due to the trailing space) we
291                 ;; can just insert `(- widest (length name))' spaces -
292                 ;; the column separator is included in the button if
293                 ;; `(equal widest (length name)'.
294                 (widget-insert (make-string (max 1
295                                                  (- widest (length name)))
296                                             ? ))))
297             (setq count (1+ count))
298             (if (eq (% count tags-per-line) 0)
299                 (widget-insert "\n")))
300           reordered-list)
301
302     ;; If the last line was not full (and hence did not include a
303     ;; carriage return), insert one now.
304     (if (not (eq (% count tags-per-line) 0))
305         (widget-insert "\n"))
306     found-target-pos))
307
308 (defun notmuch-hello-goto-search ()
309   "Put point inside the `search' widget."
310   (interactive)
311   (goto-char notmuch-hello-search-bar-marker))
312
313 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
314
315 (defun notmuch-hello-search-continuation()
316   (notmuch-hello-update t))
317
318 (defun notmuch-hello-update (&optional no-display)
319   "Update the current notmuch view."
320   ;; Lazy - rebuild everything.
321   (interactive)
322   (notmuch-hello no-display))
323
324 (defun notmuch-hello-poll-and-update ()
325   "Invoke `notmuch-poll' to import mail, then refresh the current view."
326   (interactive)
327   (notmuch-poll)
328   (notmuch-hello-update))
329
330
331 (defvar notmuch-hello-mode-map
332   (let ((map (make-sparse-keymap)))
333     (set-keymap-parent map widget-keymap)
334     (define-key map "v" '(lambda () "Display the notmuch version" (interactive)
335                            (message "notmuch version %s" (notmuch-version))))
336     (define-key map "?" 'notmuch-help)
337     (define-key map "q" 'notmuch-kill-this-buffer)
338     (define-key map "=" 'notmuch-hello-update)
339     (define-key map "G" 'notmuch-hello-poll-and-update)
340     (define-key map (kbd "<C-tab>") 'widget-backward)
341     (define-key map "m" 'notmuch-mua-new-mail)
342     (define-key map "s" 'notmuch-hello-goto-search)
343     map)
344   "Keymap for \"notmuch hello\" buffers.")
345 (fset 'notmuch-hello-mode-map notmuch-hello-mode-map)
346
347 (defun notmuch-hello-mode ()
348  "Major mode for convenient notmuch navigation. This is your entry portal into notmuch.
349
350 Complete list of currently available key bindings:
351
352 \\{notmuch-hello-mode-map}"
353  (interactive)
354  (kill-all-local-variables)
355  (use-local-map notmuch-hello-mode-map)
356  (setq major-mode 'notmuch-hello-mode
357        mode-name "notmuch-hello")
358  ;;(setq buffer-read-only t)
359 )
360
361 (defun notmuch-hello-generate-tag-alist ()
362   "Return an alist from tags to queries to display in the all-tags section."
363   (notmuch-remove-if-not
364    #'cdr
365    (mapcar (lambda (tag)
366              (cons tag
367                    (cond
368                     ((functionp notmuch-hello-tag-list-make-query)
369                      (concat "tag:" tag " and ("
370                              (funcall notmuch-hello-tag-list-make-query tag) ")"))
371                     ((stringp notmuch-hello-tag-list-make-query)
372                      (concat "tag:" tag " and ("
373                              notmuch-hello-tag-list-make-query ")"))
374                     (t (concat "tag:" tag)))))
375            (notmuch-remove-if-not
376             (lambda (tag)
377               (not (member tag notmuch-hello-hide-tags)))
378             (process-lines notmuch-command "search-tags")))))
379
380 ;;;###autoload
381 (defun notmuch-hello (&optional no-display)
382   "Run notmuch and display saved searches, known tags, etc."
383   (interactive)
384
385   ; Jump through a hoop to get this value from the deprecated variable
386   ; name (`notmuch-folders') or from the default value.
387   (if (not notmuch-saved-searches)
388     (setq notmuch-saved-searches (notmuch-saved-searches)))
389
390   (if no-display
391       (set-buffer "*notmuch-hello*")
392     (switch-to-buffer "*notmuch-hello*"))
393
394   (let ((target (if (widget-at)
395                    (widget-value (widget-at))
396                  (condition-case nil
397                      (progn
398                        (widget-forward 1)
399                        (widget-value (widget-at)))
400                    (error nil)))))
401
402     (kill-all-local-variables)
403     (let ((inhibit-read-only t))
404       (erase-buffer))
405
406     (unless (eq major-mode 'notmuch-hello-mode)
407       (notmuch-hello-mode))
408
409     (let ((all (overlay-lists)))
410       ;; Delete all the overlays.
411       (mapc 'delete-overlay (car all))
412       (mapc 'delete-overlay (cdr all)))
413
414     (when notmuch-show-logo
415       (let ((image notmuch-hello-logo))
416         ;; The notmuch logo uses transparency. That can display poorly
417         ;; when inserting the image into an emacs buffer (black logo on
418         ;; a black background), so force the background colour of the
419         ;; image. We use a face to represent the colour so that
420         ;; `defface' can be used to declare the different possible
421         ;; colours, which depend on whether the frame has a light or
422         ;; dark background.
423         (setq image (cons 'image
424                           (append (cdr image)
425                                   (list :background (face-background 'notmuch-hello-logo-background)))))
426         (insert-image image))
427       (widget-insert "  "))
428
429     (widget-insert "Welcome to ")
430     ;; Hack the display of the links used.
431     (let ((widget-link-prefix "")
432           (widget-link-suffix ""))
433       (widget-create 'link
434                      :notify (lambda (&rest ignore)
435                                (browse-url notmuch-hello-url))
436                      :help-echo "Visit the notmuch website."
437                      "notmuch")
438       (widget-insert ". ")
439       (widget-insert "You have ")
440       (widget-create 'link
441                      :notify (lambda (&rest ignore)
442                                (notmuch-hello-update))
443                      :help-echo "Refresh"
444                      (notmuch-hello-nice-number
445                       (string-to-number (car (process-lines notmuch-command "count")))))
446       (widget-insert " messages.\n"))
447
448     (let ((found-target-pos nil)
449           (final-target-pos nil))
450       (let* ((saved-alist
451               ;; Filter out empty saved searches if required.
452               (if notmuch-show-empty-saved-searches
453                   notmuch-saved-searches
454                 (loop for elem in notmuch-saved-searches
455                       if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
456                       collect elem)))
457              (saved-widest (notmuch-hello-longest-label saved-alist))
458              (alltags-alist (if notmuch-show-all-tags-list (notmuch-hello-generate-tag-alist)))
459              (alltags-widest (notmuch-hello-longest-label alltags-alist))
460              (widest (max saved-widest alltags-widest)))
461
462         (when saved-alist
463           ;; Sort saved searches if required.
464           (when notmuch-saved-search-sort-function
465             (setq saved-alist
466                   (funcall notmuch-saved-search-sort-function saved-alist)))
467           (widget-insert "\nSaved searches: ")
468           (widget-create 'push-button
469                          :notify (lambda (&rest ignore)
470                                    (customize-variable 'notmuch-saved-searches))
471                          "edit")
472           (widget-insert "\n\n")
473           (setq final-target-pos (point-marker))
474           (let ((start (point)))
475             (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
476             (if found-target-pos
477                 (setq final-target-pos found-target-pos))
478             (indent-rigidly start (point) notmuch-hello-indent)))
479
480         (widget-insert "\nSearch: ")
481         (setq notmuch-hello-search-bar-marker (point-marker))
482         (widget-create 'editable-field
483                        ;; Leave some space at the start and end of the
484                        ;; search boxes.
485                        :size (max 8 (- (window-width) notmuch-hello-indent
486                                        (length "Search: ")))
487                        :action (lambda (widget &rest ignore)
488                                  (notmuch-hello-search (widget-value widget))))
489         ;; add an invisible space to make `widget-end-of-line' ignore
490         ;; trailine spaces in the search widget field
491         (widget-insert " ")
492         (put-text-property (1- (point)) (point) 'invisible t)
493         (widget-insert "\n")
494
495         (when notmuch-hello-recent-searches
496           (widget-insert "\nRecent searches: ")
497           (widget-create 'push-button
498                          :notify (lambda (&rest ignore)
499                                    (setq notmuch-hello-recent-searches nil)
500                                    (notmuch-hello-update))
501                          "clear")
502           (widget-insert "\n\n")
503           (let ((start (point))
504                 (nth 0))
505             (mapc '(lambda (search)
506                      (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
507                        (set widget-symbol
508                             (widget-create 'editable-field
509                                        ;; Don't let the search boxes be
510                                        ;; less than 8 characters wide.
511                                        :size (max 8
512                                                   (- (window-width)
513                                                      ;; Leave some space
514                                                      ;; at the start and
515                                                      ;; end of the
516                                                      ;; boxes.
517                                                      (* 2 notmuch-hello-indent)
518                                                      ;; 1 for the space
519                                                      ;; before the
520                                                      ;; `[save]' button. 6
521                                                      ;; for the `[save]'
522                                                      ;; button.
523                                                      1 6))
524                                        :action (lambda (widget &rest ignore)
525                                                  (notmuch-hello-search (widget-value widget)))
526                                        search))
527                        (widget-insert " ")
528                        (widget-create 'push-button
529                                       :notify (lambda (widget &rest ignore)
530                                                 (notmuch-hello-add-saved-search widget))
531                                       :notmuch-saved-search-widget widget-symbol
532                                       "save"))
533                      (widget-insert "\n")
534                      (setq nth (1+ nth)))
535                   notmuch-hello-recent-searches)
536             (indent-rigidly start (point) notmuch-hello-indent)))
537
538         (when alltags-alist
539           (widget-insert "\nAll tags: ")
540           (widget-create 'push-button
541                          :notify (lambda (widget &rest ignore)
542                                    (setq notmuch-show-all-tags-list nil)
543                                    (notmuch-hello-update))
544                          "hide")
545           (widget-insert "\n\n")
546           (let ((start (point)))
547             (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
548             (if (not final-target-pos)
549                 (setq final-target-pos found-target-pos))
550             (indent-rigidly start (point) notmuch-hello-indent)))
551
552         (widget-insert "\n")
553
554         (if (not notmuch-show-all-tags-list)
555             (widget-create 'push-button
556                            :notify (lambda (widget &rest ignore)
557                                      (setq notmuch-show-all-tags-list t)
558                                      (notmuch-hello-update))
559                            "Show all tags")))
560
561       (let ((start (point)))
562         (widget-insert "\n\n")
563         (widget-insert "Type a search query and hit RET to view matching threads.\n")
564         (when notmuch-hello-recent-searches
565           (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
566           (widget-insert "Save recent searches with the `save' button.\n"))
567         (when notmuch-saved-searches
568           (widget-insert "Edit saved searches with the `edit' button.\n"))
569         (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
570         (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
571         (let ((fill-column (- (window-width) notmuch-hello-indent)))
572           (center-region start (point))))
573
574       (widget-setup)
575
576       (when final-target-pos
577         (goto-char final-target-pos)
578         (unless (widget-at)
579           (widget-forward 1)))
580
581       (unless (widget-at)
582         (notmuch-hello-goto-search)))))
583
584 (defun notmuch-folder ()
585   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
586   (interactive)
587   (notmuch-hello))
588
589 ;;
590
591 (provide 'notmuch-hello)