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