1 ;;; notmuch-jump.el --- User-friendly shortcut keys -*- lexical-binding: t -*-
3 ;; Copyright © Austin Clements
5 ;; This file is part of Notmuch.
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.
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.
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/>.
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21 ;; David Edmondson <dme@dme.org>
25 (require 'notmuch-lib)
26 (require 'notmuch-hello)
29 (defun notmuch-jump-search ()
30 "Jump to a saved search by shortcut key.
32 This prompts for and performs a saved search using the shortcut
33 keys configured in the :key property of `notmuch-saved-searches'.
34 Typically these shortcuts are a single key long, so this is a
35 fast way to jump to a saved search from anywhere in Notmuch."
37 ;; Build the action map
39 (dolist (saved-search notmuch-saved-searches)
40 (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
41 (key (plist-get saved-search :key)))
43 (let ((name (plist-get saved-search :name))
44 (query (plist-get saved-search :query))
46 (cl-case (plist-get saved-search :sort-order)
49 (otherwise (default-value 'notmuch-search-oldest-first)))))
52 ((eq (plist-get saved-search :search-type) 'tree)
53 `(lambda () (notmuch-tree ',query)))
54 ((eq (plist-get saved-search :search-type) 'unthreaded)
55 `(lambda () (notmuch-unthreaded ',query)))
57 `(lambda () (notmuch-search ',query ',oldest-first)))))
59 (setq action-map (nreverse action-map))
61 (notmuch-jump action-map "Search: ")
62 (error "To use notmuch-jump, %s"
63 "please customize shortcut keys in notmuch-saved-searches."))))
65 (defface notmuch-jump-key
66 '((t :inherit minibuffer-prompt))
67 "Default face used for keys in `notmuch-jump' and related."
68 :group 'notmuch-faces)
70 (defvar notmuch-jump--action nil)
73 (defun notmuch-jump (action-map prompt)
74 "Interactively prompt for one of the keys in ACTION-MAP.
76 Displays a summary of all bindings in ACTION-MAP in the
77 minibuffer, reads a key from the minibuffer, and performs the
78 corresponding action. The prompt can be canceled with C-g or
79 RET. PROMPT must be a string to use for the prompt. PROMPT
80 should include a space at the end.
82 ACTION-MAP must be a list of triples of the form
84 where KEY is a key binding, LABEL is a string label to display in
85 the buffer, and ACTION is a nullary function to call. LABEL may
86 be null, in which case the action will still be bound, but will
87 not appear in the pop-up buffer."
88 (let* ((items (notmuch-jump--format-actions action-map))
89 ;; Format the table of bindings and the full prompt
92 (notmuch-jump--insert-items (window-body-width) items)
96 (propertize prompt 'face 'notmuch-jump-key)))
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
102 (minibuffer-prompt-properties
103 (notmuch-plist-delete
104 (copy-sequence minibuffer-prompt-properties)
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))
111 (read-from-minibuffer full-prompt nil minibuffer-map)
112 ;; If we got an action, do it
113 (when notmuch-jump--action
114 (funcall notmuch-jump--action))))
116 (defun notmuch-jump--format-actions (action-map)
117 "Format the actions in ACTION-MAP.
119 Returns a list of strings, one for each item with a label in
120 ACTION-MAP. These strings can be inserted into a tabular
122 ;; Compute the maximum key description width
124 (pcase-dolist (`(,key ,_desc) action-map)
127 (string-width (format-kbd-macro key)))))
128 ;; Format each action
129 (mapcar (pcase-lambda (`(,key ,desc))
130 (setq key (format-kbd-macro key))
131 (concat (propertize key 'face 'notmuch-jump-key)
132 (make-string (- key-width (length key)) ? )
136 (defun notmuch-jump--insert-items (width items)
137 "Make a table of ITEMS up to WIDTH wide in the current buffer."
138 (let* ((nitems (length items))
139 (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
140 (ncols (if (> (* col-width nitems) width)
141 (max 1 (/ width col-width))
142 ;; Items fit on one line. Space them out
143 (setq col-width (/ width nitems))
148 (let ((item (pop items)))
150 (when (and items (< col (- ncols 1)))
151 (insert (make-string (- col-width (string-width item)) ? ))))))
155 (defvar notmuch-jump-minibuffer-map
156 (let ((map (make-sparse-keymap)))
157 (set-keymap-parent map minibuffer-local-map)
158 ;; Make this like a special-mode keymap, with no self-insert-command
159 (suppress-keymap map)
160 (define-key map (kbd "DEL") 'exit-minibuffer)
162 "Base keymap for notmuch-jump's minibuffer keymap.")
164 (defun notmuch-jump--make-keymap (action-map prompt)
165 "Translate ACTION-MAP into a minibuffer keymap."
166 (let ((map (make-sparse-keymap)))
167 (set-keymap-parent map notmuch-jump-minibuffer-map)
168 (pcase-dolist (`(,key ,_name ,fn) action-map)
169 (when (= (length key) 1)
171 `(lambda () (interactive)
172 (setq notmuch-jump--action ',fn)
173 (exit-minibuffer)))))
174 ;; By doing this in two passes (and checking if we already have a
175 ;; binding) we avoid problems if the user specifies a binding which
176 ;; is a prefix of another binding.
177 (pcase-dolist (`(,key ,_name ,_fn) action-map)
178 (when (> (length key) 1)
179 (let* ((key (elt key 0))
180 (keystr (string key))
181 (new-prompt (concat prompt (format-kbd-macro keystr) " "))
183 (unless (lookup-key map keystr)
184 (pcase-dolist (`(,k ,n ,f) action-map)
185 (when (= key (elt k 0))
186 (push (list (substring k 1) n f) action-submap)))
187 ;; We deal with backspace specially
188 (push (list (kbd "DEL")
190 (apply-partially #'notmuch-jump action-map prompt))
192 (setq action-submap (nreverse action-submap))
193 (define-key map keystr
194 `(lambda () (interactive)
195 (setq notmuch-jump--action
196 ',(apply-partially #'notmuch-jump
199 (exit-minibuffer)))))))
202 (provide 'notmuch-jump)
204 ;;; notmuch-jump.el ends here