]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-jump.el
Merge branch 'release'
[notmuch] / emacs / notmuch-jump.el
1 ;;; notmuch-jump.el --- User-friendly shortcut keys
2 ;;
3 ;; Copyright © Austin Clements
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 <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21 ;;          David Edmondson <dme@dme.org>
22
23 ;;; Code:
24
25 (eval-when-compile
26   (require 'cl-lib)
27   (require 'pcase))
28
29 (require 'notmuch-lib)
30 (require 'notmuch-hello)
31
32 ;;;###autoload
33 (defun notmuch-jump-search ()
34   "Jump to a saved search by shortcut key.
35
36 This prompts for and performs a saved search using the shortcut
37 keys configured in the :key property of `notmuch-saved-searches'.
38 Typically these shortcuts are a single key long, so this is a
39 fast way to jump to a saved search from anywhere in Notmuch."
40   (interactive)
41   ;; Build the action map
42   (let (action-map)
43     (dolist (saved-search notmuch-saved-searches)
44       (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
45              (key (plist-get saved-search :key)))
46         (when key
47           (let ((name (plist-get saved-search :name))
48                 (query (plist-get saved-search :query))
49                 (oldest-first
50                  (cl-case (plist-get saved-search :sort-order)
51                    (newest-first nil)
52                    (oldest-first t)
53                    (otherwise (default-value 'notmuch-search-oldest-first)))))
54             (push (list key name
55                         (cond
56                          ((eq (plist-get saved-search :search-type) 'tree)
57                           `(lambda () (notmuch-tree ',query)))
58                          ((eq (plist-get saved-search :search-type) 'unthreaded)
59                           `(lambda () (notmuch-unthreaded ',query)))
60                          (t
61                           `(lambda () (notmuch-search ',query ',oldest-first)))))
62                   action-map)))))
63     (setq action-map (nreverse action-map))
64     (if action-map
65         (notmuch-jump action-map "Search: ")
66       (error "To use notmuch-jump, \
67 please customize shortcut keys in notmuch-saved-searches."))))
68
69 (defvar notmuch-jump--action nil)
70
71 ;;;###autoload
72 (defun notmuch-jump (action-map prompt)
73   "Interactively prompt for one of the keys in ACTION-MAP.
74
75 Displays a summary of all bindings in ACTION-MAP in the
76 minibuffer, reads a key from the minibuffer, and performs the
77 corresponding action.  The prompt can be canceled with C-g or
78 RET.  PROMPT must be a string to use for the prompt.  PROMPT
79 should include a space at the end.
80
81 ACTION-MAP must be a list of triples of the form
82   (KEY LABEL ACTION)
83 where KEY is a key binding, LABEL is a string label to display in
84 the buffer, and ACTION is a nullary function to call.  LABEL may
85 be null, in which case the action will still be bound, but will
86 not appear in the pop-up buffer."
87   (let* ((items (notmuch-jump--format-actions action-map))
88          ;; Format the table of bindings and the full prompt
89          (table
90           (with-temp-buffer
91             (notmuch-jump--insert-items (window-body-width) items)
92             (buffer-string)))
93          (full-prompt
94           (concat table "\n\n"
95                   (propertize prompt 'face 'minibuffer-prompt)))
96          ;; By default, the minibuffer applies the minibuffer face to
97          ;; the entire prompt.  However, we want to clearly
98          ;; distinguish bindings (which we put in the prompt face
99          ;; ourselves) from their labels, so disable the minibuffer's
100          ;; own re-face-ing.
101          (minibuffer-prompt-properties
102           (notmuch-plist-delete
103            (copy-sequence minibuffer-prompt-properties)
104            'face))
105          ;; Build the keymap with our bindings
106          (minibuffer-map (notmuch-jump--make-keymap action-map prompt))
107          ;; The bindings save the the action in notmuch-jump--action
108          (notmuch-jump--action nil))
109     ;; Read the action
110     (read-from-minibuffer full-prompt nil minibuffer-map)
111     ;; If we got an action, do it
112     (when notmuch-jump--action
113       (funcall notmuch-jump--action))))
114
115 (defun notmuch-jump--format-actions (action-map)
116   "Format the actions in ACTION-MAP.
117
118 Returns a list of strings, one for each item with a label in
119 ACTION-MAP.  These strings can be inserted into a tabular
120 buffer."
121   ;; Compute the maximum key description width
122   (let ((key-width 1))
123     (pcase-dolist (`(,key ,desc) action-map)
124       (setq key-width
125             (max key-width
126                  (string-width (format-kbd-macro key)))))
127     ;; Format each action
128     (mapcar (pcase-lambda (`(,key ,desc))
129               (setq key (format-kbd-macro key))
130               (concat (propertize key 'face 'minibuffer-prompt)
131                       (make-string (- key-width (length key)) ? )
132                       " " desc))
133             action-map)))
134
135 (defun notmuch-jump--insert-items (width items)
136   "Make a table of ITEMS up to WIDTH wide in the current buffer."
137   (let* ((nitems (length items))
138          (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
139          (ncols (if (> (* col-width nitems) width)
140                     (max 1 (/ width col-width))
141                   ;; Items fit on one line.  Space them out
142                   (setq col-width (/ width nitems))
143                   (length items))))
144     (while items
145       (dotimes (col ncols)
146         (when items
147           (let ((item (pop items)))
148             (insert item)
149             (when (and items (< col (- ncols 1)))
150               (insert (make-string (- col-width (string-width item)) ? ))))))
151       (when items
152         (insert "\n")))))
153
154 (defvar notmuch-jump-minibuffer-map
155   (let ((map (make-sparse-keymap)))
156     (set-keymap-parent map minibuffer-local-map)
157     ;; Make this like a special-mode keymap, with no self-insert-command
158     (suppress-keymap map)
159     (define-key map (kbd "DEL") 'exit-minibuffer)
160     map)
161   "Base keymap for notmuch-jump's minibuffer keymap.")
162
163 (defun notmuch-jump--make-keymap (action-map prompt)
164   "Translate ACTION-MAP into a minibuffer keymap."
165   (let ((map (make-sparse-keymap)))
166     (set-keymap-parent map notmuch-jump-minibuffer-map)
167     (pcase-dolist (`(,key ,name ,fn) action-map)
168       (when (= (length key) 1)
169         (define-key map key
170           `(lambda () (interactive)
171              (setq notmuch-jump--action ',fn)
172              (exit-minibuffer)))))
173     ;; By doing this in two passes (and checking if we already have a
174     ;; binding) we avoid problems if the user specifies a binding which
175     ;; is a prefix of another binding.
176     (pcase-dolist (`(,key ,name ,fn) action-map)
177       (when (> (length key) 1)
178         (let* ((key (elt key 0))
179                (keystr (string key))
180                (new-prompt (concat prompt (format-kbd-macro keystr) " "))
181                (action-submap nil))
182           (unless (lookup-key map keystr)
183             (pcase-dolist (`(,k ,n ,f) action-map)
184               (when (= key (elt k 0))
185                 (push (list (substring k 1) n f) action-submap)))
186             ;; We deal with backspace specially
187             (push (list (kbd "DEL")
188                         "Backup"
189                         (apply-partially #'notmuch-jump action-map prompt))
190                   action-submap)
191             (setq action-submap (nreverse action-submap))
192             (define-key map keystr
193               `(lambda () (interactive)
194                  (setq notmuch-jump--action
195                        ',(apply-partially #'notmuch-jump
196                                           action-submap
197                                           new-prompt))
198                  (exit-minibuffer)))))))
199     map))
200
201 ;;
202
203 (provide 'notmuch-jump)
204
205 ;;; notmuch-jump.el ends here