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 (defvar notmuch-jump--action nil)
68 (defun notmuch-jump (action-map prompt)
69 "Interactively prompt for one of the keys in ACTION-MAP.
71 Displays a summary of all bindings in ACTION-MAP in the
72 minibuffer, reads a key from the minibuffer, and performs the
73 corresponding action. The prompt can be canceled with C-g or
74 RET. PROMPT must be a string to use for the prompt. PROMPT
75 should include a space at the end.
77 ACTION-MAP must be a list of triples of the form
79 where KEY is a key binding, LABEL is a string label to display in
80 the buffer, and ACTION is a nullary function to call. LABEL may
81 be null, in which case the action will still be bound, but will
82 not appear in the pop-up buffer."
83 (let* ((items (notmuch-jump--format-actions action-map))
84 ;; Format the table of bindings and the full prompt
87 (notmuch-jump--insert-items (window-body-width) items)
91 (propertize prompt 'face 'minibuffer-prompt)))
92 ;; By default, the minibuffer applies the minibuffer face to
93 ;; the entire prompt. However, we want to clearly
94 ;; distinguish bindings (which we put in the prompt face
95 ;; ourselves) from their labels, so disable the minibuffer's
97 (minibuffer-prompt-properties
99 (copy-sequence minibuffer-prompt-properties)
101 ;; Build the keymap with our bindings
102 (minibuffer-map (notmuch-jump--make-keymap action-map prompt))
103 ;; The bindings save the the action in notmuch-jump--action
104 (notmuch-jump--action nil))
106 (read-from-minibuffer full-prompt nil minibuffer-map)
107 ;; If we got an action, do it
108 (when notmuch-jump--action
109 (funcall notmuch-jump--action))))
111 (defun notmuch-jump--format-actions (action-map)
112 "Format the actions in ACTION-MAP.
114 Returns a list of strings, one for each item with a label in
115 ACTION-MAP. These strings can be inserted into a tabular
117 ;; Compute the maximum key description width
119 (pcase-dolist (`(,key ,_desc) action-map)
122 (string-width (format-kbd-macro key)))))
123 ;; Format each action
124 (mapcar (pcase-lambda (`(,key ,desc))
125 (setq key (format-kbd-macro key))
126 (concat (propertize key 'face 'minibuffer-prompt)
127 (make-string (- key-width (length key)) ? )
131 (defun notmuch-jump--insert-items (width items)
132 "Make a table of ITEMS up to WIDTH wide in the current buffer."
133 (let* ((nitems (length items))
134 (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
135 (ncols (if (> (* col-width nitems) width)
136 (max 1 (/ width col-width))
137 ;; Items fit on one line. Space them out
138 (setq col-width (/ width nitems))
143 (let ((item (pop items)))
145 (when (and items (< col (- ncols 1)))
146 (insert (make-string (- col-width (string-width item)) ? ))))))
150 (defvar notmuch-jump-minibuffer-map
151 (let ((map (make-sparse-keymap)))
152 (set-keymap-parent map minibuffer-local-map)
153 ;; Make this like a special-mode keymap, with no self-insert-command
154 (suppress-keymap map)
155 (define-key map (kbd "DEL") 'exit-minibuffer)
157 "Base keymap for notmuch-jump's minibuffer keymap.")
159 (defun notmuch-jump--make-keymap (action-map prompt)
160 "Translate ACTION-MAP into a minibuffer keymap."
161 (let ((map (make-sparse-keymap)))
162 (set-keymap-parent map notmuch-jump-minibuffer-map)
163 (pcase-dolist (`(,key ,_name ,fn) action-map)
164 (when (= (length key) 1)
166 `(lambda () (interactive)
167 (setq notmuch-jump--action ',fn)
168 (exit-minibuffer)))))
169 ;; By doing this in two passes (and checking if we already have a
170 ;; binding) we avoid problems if the user specifies a binding which
171 ;; is a prefix of another binding.
172 (pcase-dolist (`(,key ,_name ,_fn) action-map)
173 (when (> (length key) 1)
174 (let* ((key (elt key 0))
175 (keystr (string key))
176 (new-prompt (concat prompt (format-kbd-macro keystr) " "))
178 (unless (lookup-key map keystr)
179 (pcase-dolist (`(,k ,n ,f) action-map)
180 (when (= key (elt k 0))
181 (push (list (substring k 1) n f) action-submap)))
182 ;; We deal with backspace specially
183 (push (list (kbd "DEL")
185 (apply-partially #'notmuch-jump action-map prompt))
187 (setq action-submap (nreverse action-submap))
188 (define-key map keystr
189 `(lambda () (interactive)
190 (setq notmuch-jump--action
191 ',(apply-partially #'notmuch-jump
194 (exit-minibuffer)))))))
197 (provide 'notmuch-jump)
199 ;;; notmuch-jump.el ends here