]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-hello.el
emacs: Rip out all of the notmuch-folder code.
[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 (defvar notmuch-hello-url "http://notmuchmail.org"
69   "The `notmuch' web site.")
70
71 (defvar notmuch-hello-recent-searches nil)
72
73 (defun notmuch-hello-remember-search (search)
74   (if (not (member search notmuch-hello-recent-searches))
75       (push search notmuch-hello-recent-searches))
76   (if (> (length notmuch-hello-recent-searches)
77          notmuch-recent-searches-max)
78       (setq notmuch-hello-recent-searches (butlast notmuch-hello-recent-searches))))
79
80 (defun notmuch-hello-trim (search)
81   "Trim whitespace."
82   (if (string-match "^[[:space:]]*\\(.*[^[:space:]]\\)[[:space:]]*$" search)
83       (match-string 1 search)
84     search))
85
86 (defun notmuch-hello-search (search)
87   (let ((search (notmuch-hello-trim search)))
88     (notmuch-hello-remember-search search)
89     (notmuch-search search notmuch-search-oldest-first nil nil #'notmuch-hello-search-continuation)))
90
91 (defun notmuch-hello-add-saved-search (widget)
92   (interactive)
93   (let ((search (widget-value
94                  (symbol-value
95                   (widget-get widget :notmuch-saved-search-widget))))
96         (name (completing-read "Name for saved search: "
97                                notmuch-saved-searches)))
98     ;; If an existing saved search with this name exists, remove it.
99     (setq notmuch-saved-searches
100           (loop for elem in notmuch-saved-searches
101                 if (not (equal name
102                                (car elem)))
103                 collect elem))
104     ;; Add the new one.
105     (customize-save-variable 'notmuch-saved-searches
106                              (push (cons name search)
107                                    notmuch-saved-searches))
108     (message "Saved '%s' as '%s'." search name)
109     (notmuch-hello-update)))
110
111 (defun notmuch-hello-longest-label (tag-alist)
112   (or (loop for elem in tag-alist
113             maximize (length (car elem)))
114       0))
115
116 (defun notmuch-hello-roundup (dividend divisor)
117   "Return the rounded up value of dividing `dividend' by `divisor'."
118   (+ (/ dividend divisor)
119      (if (> (% dividend divisor) 0) 1 0)))
120
121 (defun notmuch-hello-reflect (list width)
122   "Reflect a `width' wide matrix represented by `list' along the
123 diagonal."
124   ;; Not very lispy...
125   (let* ((len (length list))
126          (nrows (notmuch-hello-roundup len width)))
127     (loop for row from 0 to (- nrows 1)
128           append (loop for col from 0 to (- width 1)
129                        ;; How could we calculate the offset just once
130                        ;; per inner-loop?
131                        if (< (+ (* nrows col) row) len)
132                        collect (nth (+ (* nrows col) row) list)
133                        else
134                        ;; Don't forget to insert an empty slot in the
135                        ;; output matrix if there is no corresponding
136                        ;; value in the input matrix.
137                        collect nil))))
138
139 (defun notmuch-hello-widget-search (widget &rest ignore)
140   (notmuch-search (widget-get widget
141                               :notmuch-search-terms)
142                   notmuch-search-oldest-first
143                   nil nil #'notmuch-hello-search-continuation))
144
145 (defun notmuch-saved-search-count (search)
146   (car (process-lines notmuch-command "count" search)))
147
148 (defun notmuch-hello-insert-tags (tag-alist widest target)
149   (let* ((tags-per-line (max 1
150                              (/ (- (window-width) notmuch-hello-indent)
151                                 ;; Count is 7 wide, 1 for the space
152                                 ;; after the name.
153                                 (+ 7 1 widest))))
154          (count 0)
155          (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
156          ;; Hack the display of the buttons used.
157          (widget-push-button-prefix "")
158          (widget-push-button-suffix "")
159          (found-target-pos nil))
160     ;; dme: It feels as though there should be a better way to
161     ;; implement this loop than using an incrementing counter.
162     (loop for elem in reordered-list
163           do (progn
164                ;; (not elem) indicates an empty slot in the matrix.
165                (when elem
166                  (widget-insert (format "%6s " (notmuch-saved-search-count (cdr elem))))
167                  (if (string= (format "%s " (car elem)) target)
168                      (setq found-target-pos (point-marker)))
169                  (widget-create 'push-button
170                                 :notify #'notmuch-hello-widget-search
171                                 :notmuch-search-terms (cdr elem)
172                                 (format "%s " (car elem)))
173                  (insert (make-string (1+ (- widest (length (car elem)))) ? )))
174                (setq count (1+ count))
175                (if (eq (% count tags-per-line) 0)
176                    (widget-insert "\n"))))
177
178     ;; If the last line was not full (and hence did not include a
179     ;; carriage return), insert one now.
180     (if (not (eq (% count tags-per-line) 0))
181         (widget-insert "\n"))
182     found-target-pos))
183
184 (defun notmuch-hello-goto-search ()
185   "Put point inside the `search' widget."
186   (interactive)
187   (goto-char notmuch-hello-search-bar-marker))
188
189 (defimage notmuch-hello-logo ((:type png :file "notmuch-logo.png")))
190
191 (defun notmuch-hello-search-continuation()
192   (notmuch-hello-update t))
193
194 (defun notmuch-hello-update (&optional no-display)
195   ;; Lazy - rebuild everything.
196   (interactive)
197   (notmuch-hello no-display))
198
199 (defun notmuch-hello-poll-and-update ()
200   "Invoke `notmuch-poll' to import mail, then refresh the current view."
201   (interactive)
202   (notmuch-poll)
203   (notmuch-hello-update))
204
205 (defun notmuch-hello (&optional no-display)
206   (interactive)
207
208   ; Jump through a hoop to get this value from the deprecated variable
209   ; name (`notmuch-folders') or from the default value.
210   (if (not notmuch-saved-searches)
211     (setq notmuch-saved-searches (notmuch-saved-searches)))
212
213   (if no-display
214       (set-buffer "*notmuch-hello*")
215     (switch-to-buffer "*notmuch-hello*"))
216
217   (let ((target (if (widget-at)
218                    (widget-value (widget-at))
219                  (condition-case nil
220                      (progn
221                        (widget-forward 1)
222                        (widget-value (widget-at)))
223                    (error nil)))))
224
225     (kill-all-local-variables)
226     (let ((inhibit-read-only t))
227       (erase-buffer))
228
229     (let ((all (overlay-lists)))
230       ;; Delete all the overlays.
231       (mapc 'delete-overlay (car all))
232       (mapc 'delete-overlay (cdr all)))
233
234     (when notmuch-show-logo
235       (let ((image notmuch-hello-logo))
236         ;; The notmuch logo uses transparency. That can display poorly
237         ;; when inserting the image into an emacs buffer (black logo on
238         ;; a black background), so force the background colour of the
239         ;; image. We use a face to represent the colour so that
240         ;; `defface' can be used to declare the different possible
241         ;; colours, which depend on whether the frame has a light or
242         ;; dark background.
243         (setq image (cons 'image
244                           (append (cdr image)
245                                   (list :background (face-background 'notmuch-hello-logo-background)))))
246         (insert-image image))
247       (widget-insert "  "))
248
249     (widget-insert "Welcome to ")
250     ;; Hack the display of the links used.
251     (let ((widget-link-prefix "")
252           (widget-link-suffix ""))
253       (widget-create 'link
254                      :notify (lambda (&rest ignore)
255                                (browse-url notmuch-hello-url))
256                      :help-echo "Visit the notmuch website."
257                      "notmuch")
258       (widget-insert ". ")
259       (widget-insert "You have ")
260       (widget-create 'link
261                      :notify (lambda (&rest ignore)
262                                (notmuch-hello-update))
263                      :help-echo "Refresh"
264                      (car (process-lines notmuch-command "count")))
265       (widget-insert " messages (that's not much mail).\n\n"))
266
267     (let ((found-target-pos nil)
268           (final-target-pos nil))
269       (let* ((saved-alist
270               ;; Filter out empty saved seaches if required.
271               (if notmuch-show-empty-saved-searches
272                   notmuch-saved-searches
273                 (loop for elem in notmuch-saved-searches
274                       if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
275                       collect elem)))
276              (saved-widest (notmuch-hello-longest-label saved-alist))
277              (alltags-alist (mapcar '(lambda (tag) (cons tag (concat "tag:" tag)))
278                                     (process-lines notmuch-command "search-tags")))
279              (alltags-widest (notmuch-hello-longest-label alltags-alist))
280              (widest (max saved-widest alltags-widest)))
281
282         (when saved-alist
283           (widget-insert "Saved searches: ")
284           (widget-create 'push-button
285                          :notify (lambda (&rest ignore)
286                                    (customize-variable 'notmuch-saved-searches))
287                          "edit")
288           (widget-insert "\n\n")
289           (setq final-target-pos (point-marker))
290           (let ((start (point)))
291             (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
292             (if found-target-pos
293                 (setq final-target-pos found-target-pos))
294             (indent-rigidly start (point) notmuch-hello-indent)))
295
296         (let ((start (point)))
297           (widget-insert "\nSearch: ")
298           (setq notmuch-hello-search-bar-marker (point-marker))
299           (widget-create 'editable-field
300                          ;; Leave some space at the start and end of the
301                          ;; search boxes.
302                          :size (max 8 (- (window-width) (* 2 notmuch-hello-indent)
303                                          (length "Search: ")))
304                          :action (lambda (widget &rest ignore)
305                                    (notmuch-hello-search (widget-value widget))))
306           (widget-insert "\n")
307           (indent-rigidly start (point) notmuch-hello-indent))
308
309         (when notmuch-hello-recent-searches
310           (widget-insert "\nRecent searches: ")
311           (widget-create 'push-button
312                          :notify (lambda (&rest ignore)
313                                    (setq notmuch-hello-recent-searches nil)
314                                    (notmuch-hello-update))
315                          "clear")
316           (widget-insert "\n\n")
317           (let ((start (point))
318                 (nth 0))
319             (mapc '(lambda (search)
320                      (let ((widget-symbol (intern (format "notmuch-hello-search-%d" nth))))
321                        (set widget-symbol
322                             (widget-create 'editable-field
323                                        ;; Don't let the search boxes be
324                                        ;; less than 8 characters wide.
325                                        :size (max 8
326                                                   (- (window-width)
327                                                      ;; Leave some space
328                                                      ;; at the start and
329                                                      ;; end of the
330                                                      ;; boxes.
331                                                      (* 2 notmuch-hello-indent)
332                                                      ;; 1 for the space
333                                                      ;; before the
334                                                      ;; `[save]' button. 6
335                                                      ;; for the `[save]'
336                                                      ;; button.
337                                                      1 6))
338                                        :action (lambda (widget &rest ignore)
339                                                  (notmuch-hello-search (widget-value widget)))
340                                        search))
341                        (widget-insert " ")
342                        (widget-create 'push-button
343                                       :notify (lambda (widget &rest ignore)
344                                                 (notmuch-hello-add-saved-search widget))
345                                       :notmuch-saved-search-widget widget-symbol
346                                       "save"))
347                      (widget-insert "\n")
348                      (setq nth (1+ nth)))
349                   notmuch-hello-recent-searches)
350             (indent-rigidly start (point) notmuch-hello-indent)))
351
352         (when alltags-alist
353           (if notmuch-show-all-tags-list
354               (progn
355                 (widget-insert "\nAll tags: ")
356                 (widget-create 'push-button
357                                :notify (lambda (widget &rest ignore)
358                                          (setq notmuch-show-all-tags-list nil)
359                                          (notmuch-hello-update))
360                                "hide")
361                 (widget-insert "\n\n")
362                 (let ((start (point)))
363                   (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
364                   (if (not final-target-pos)
365                       (setq final-target-pos found-target-pos))
366                   (indent-rigidly start (point) notmuch-hello-indent)))
367             (widget-insert "\n")
368             (widget-create 'push-button
369                            :notify (lambda (widget &rest ignore)
370                                      (setq notmuch-show-all-tags-list t)
371                                      (notmuch-hello-update))
372                            "Show all tags"))))
373
374       (let ((start (point)))
375         (widget-insert "\n\n")
376         (widget-insert "Type a search query and hit RET to view matching threads.\n")
377         (when notmuch-hello-recent-searches
378           (widget-insert "Hit RET to re-submit a previous search. Edit it first if you like.\n")
379           (widget-insert "Save recent searches with the `save' button.\n"))
380         (when notmuch-saved-searches
381           (widget-insert "Edit saved searches with the `edit' button.\n"))
382         (widget-insert "Hit RET or click on a saved search or tag name to view matching threads.\n")
383         (widget-insert "`=' refreshes this screen. `s' jumps to the search box. `q' to quit.\n")
384         (let ((fill-column (- (window-width) notmuch-hello-indent)))
385           (center-region start (point))))
386
387       (use-local-map widget-keymap)
388       (local-set-key "=" 'notmuch-hello-update)
389       (local-set-key "G" 'notmuch-hello-poll-and-update)
390       (local-set-key "m" 'notmuch-mua-mail)
391       (local-set-key "q" '(lambda () (interactive) (kill-buffer (current-buffer))))
392       (local-set-key "s" 'notmuch-hello-goto-search)
393       (local-set-key "v" '(lambda () (interactive)
394                             (message "notmuch version %s" (notmuch-version))))
395
396       (widget-setup)
397
398       (goto-char final-target-pos)
399       (if (not (widget-at))
400           (widget-forward 1)))))
401
402 ;;;###autoload
403 (defun notmuch-folder ()
404   "Deprecated function for invoking notmuch---calling `notmuch' is preferred now."
405   (interactive)
406   (notmuch-hello))
407
408 ;;
409
410 (provide 'notmuch-hello)