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