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