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