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