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