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