1 ;; notmuch-jump.el --- User-friendly shortcut keys
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 <http://www.gnu.org/licenses/>.
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21 ;; David Edmondson <dme@dme.org>
23 (eval-when-compile (require 'cl))
25 (require 'notmuch-lib)
26 (require 'notmuch-hello)
29 (unless (fboundp 'window-body-width)
30 ;; Compatibility for Emacs pre-24
31 (defalias 'window-body-width 'window-width)))
34 (defun notmuch-jump-search ()
35 "Jump to a saved search by shortcut key.
37 This prompts for and performs a saved search using the shortcut
38 keys configured in the :key property of `notmuch-saved-searches'.
39 Typically these shortcuts are a single key long, so this is a
40 fast way to jump to a saved search from anywhere in Notmuch."
43 ;; Build the action map
45 (dolist (saved-search notmuch-saved-searches)
46 (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
47 (key (plist-get saved-search :key)))
49 (let ((name (plist-get saved-search :name))
50 (query (plist-get saved-search :query))
52 (case (plist-get saved-search :sort-order)
55 (otherwise (default-value 'notmuch-search-oldest-first)))))
57 (if (eq (plist-get saved-search :search-type) 'tree)
58 `(lambda () (notmuch-tree ',query))
59 `(lambda () (notmuch-search ',query ',oldest-first))))
61 (setq action-map (nreverse action-map))
64 (notmuch-jump action-map "Search: ")
65 (error "To use notmuch-jump, please customize shortcut keys in notmuch-saved-searches."))))
67 (defvar notmuch-jump--action nil)
69 (defun notmuch-jump (action-map prompt)
70 "Interactively prompt for one of the keys in ACTION-MAP.
72 Displays a summary of all bindings in ACTION-MAP in the
73 minibuffer, reads a key from the minibuffer, and performs the
74 corresponding action. The prompt can be canceled with C-g or
75 RET. PROMPT must be a string to use for the prompt. PROMPT
76 should include a space at the end.
78 ACTION-MAP must be a list of triples of the form
80 where KEY is a key binding, LABEL is a string label to display in
81 the buffer, and ACTION is a nullary function to call. LABEL may
82 be null, in which case the action will still be bound, but will
83 not appear in the pop-up buffer.
86 (let* ((items (notmuch-jump--format-actions action-map))
87 ;; Format the table of bindings and the full prompt
90 (notmuch-jump--insert-items (window-body-width) items)
94 (propertize prompt 'face 'minibuffer-prompt)))
95 ;; By default, the minibuffer applies the minibuffer face to
96 ;; the entire prompt. However, we want to clearly
97 ;; distinguish bindings (which we put in the prompt face
98 ;; ourselves) from their labels, so disable the minibuffer's
100 (minibuffer-prompt-properties
101 (notmuch-plist-delete
102 (copy-sequence minibuffer-prompt-properties)
104 ;; Build the keymap with our bindings
105 (minibuffer-map (notmuch-jump--make-keymap action-map))
106 ;; The bindings save the the action in notmuch-jump--action
107 (notmuch-jump--action nil))
109 (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))))
115 (defun notmuch-jump--format-actions (action-map)
116 "Format the actions in ACTION-MAP.
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
122 ;; Compute the maximum key description width
124 (dolist (entry action-map)
127 (string-width (format-kbd-macro (first entry))))))
128 ;; Format each action
129 (mapcar (lambda (entry)
130 (let ((key (format-kbd-macro (first entry)))
131 (desc (second entry)))
133 (propertize key 'face 'minibuffer-prompt)
134 (make-string (- key-width (length key)) ? )
138 (defun notmuch-jump--insert-items (width items)
139 "Make a table of ITEMS up to WIDTH wide in the current buffer."
140 (let* ((nitems (length items))
141 (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
142 (ncols (if (> (* col-width nitems) width)
143 (max 1 (/ width col-width))
144 ;; Items fit on one line. Space them out
145 (setq col-width (/ width nitems))
150 (let ((item (pop items)))
152 (when (and items (< col (- ncols 1)))
153 (insert (make-string (- col-width (string-width item)) ? ))))))
157 (defvar notmuch-jump-minibuffer-map
158 (let ((map (make-sparse-keymap)))
159 (set-keymap-parent map minibuffer-local-map)
160 ;; Make this like a special-mode keymap, with no self-insert-command
161 (suppress-keymap map)
163 "Base keymap for notmuch-jump's minibuffer keymap.")
165 (defun notmuch-jump--make-keymap (action-map)
166 "Translate ACTION-MAP into a minibuffer keymap."
167 (let ((map (make-sparse-keymap)))
168 (set-keymap-parent map notmuch-jump-minibuffer-map)
169 (dolist (action action-map)
170 (define-key map (first action)
171 `(lambda () (interactive)
172 (setq notmuch-jump--action ',(third action))
178 (provide 'notmuch-jump)