]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-jump.el
emacs: Introduce notmuch-jump: shortcut keys to saved searches
[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 <http://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21 ;;          David Edmondson <dme@dme.org>
22
23 (eval-when-compile (require 'cl))
24
25 (require 'notmuch-lib)
26 (require 'notmuch-hello)
27
28 ;;;###autoload
29 (defun notmuch-jump-search ()
30   "Jump to a saved search by shortcut key.
31
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."
36   (interactive)
37
38   ;; Build the action map
39   (let (action-map)
40     (dolist (saved-search notmuch-saved-searches)
41       (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
42              (key (plist-get saved-search :key)))
43         (when key
44           (let ((name (plist-get saved-search :name))
45                 (query (plist-get saved-search :query))
46                 (oldest-first
47                  (case (plist-get saved-search :sort-order)
48                    (newest-first nil)
49                    (oldest-first t)
50                    (otherwise (default-value notmuch-search-oldest-first)))))
51             (push (list key name
52                         `(lambda () (notmuch-search ',query ',oldest-first)))
53                   action-map)))))
54     (setq action-map (nreverse action-map))
55
56     (if action-map
57         (notmuch-jump action-map "Search: ")
58       (error "To use notmuch-jump, please customize shortcut keys in notmuch-saved-searches."))))
59
60 (defvar notmuch-jump--action nil)
61
62 (defun notmuch-jump (action-map prompt)
63   "Interactively prompt for one of the keys in ACTION-MAP.
64
65 Displays a summary of all bindings in ACTION-MAP in the
66 minibuffer, reads a key from the minibuffer, and performs the
67 corresponding action.  The prompt can be canceled with C-g or
68 RET.  PROMPT must be a string to use for the prompt.  PROMPT
69 should include a space at the end.
70
71 ACTION-MAP must be a list of triples of the form
72   (KEY LABEL ACTION)
73 where KEY is a key binding, LABEL is a string label to display in
74 the buffer, and ACTION is a nullary function to call.  LABEL may
75 be null, in which case the action will still be bound, but will
76 not appear in the pop-up buffer.
77 "
78
79   (let* ((items (notmuch-jump--format-actions action-map))
80          ;; Format the table of bindings and the full prompt
81          (table
82           (with-temp-buffer
83             (notmuch-jump--insert-items (window-body-width) items)
84             (buffer-string)))
85          (full-prompt
86           (concat table "\n\n"
87                   (propertize prompt 'face 'minibuffer-prompt)))
88          ;; By default, the minibuffer applies the minibuffer face to
89          ;; the entire prompt.  However, we want to clearly
90          ;; distinguish bindings (which we put in the prompt face
91          ;; ourselves) from their labels, so disable the minibuffer's
92          ;; own re-face-ing.
93          (minibuffer-prompt-properties
94           (notmuch-plist-delete
95            (copy-sequence minibuffer-prompt-properties)
96            'face))
97          ;; Build the keymap with our bindings
98          (minibuffer-map (notmuch-jump--make-keymap action-map))
99          ;; The bindings save the the action in notmuch-jump--action
100          (notmuch-jump--action nil))
101     ;; Read the action
102     (read-from-minibuffer full-prompt nil minibuffer-map)
103
104     ;; If we got an action, do it
105     (when notmuch-jump--action
106       (funcall notmuch-jump--action))))
107
108 (defun notmuch-jump--format-actions (action-map)
109   "Format the actions in ACTION-MAP.
110
111 Returns a list of strings, one for each item with a label in
112 ACTION-MAP.  These strings can be inserted into a tabular
113 buffer."
114
115   ;; Compute the maximum key description width
116   (let ((key-width 1))
117     (dolist (entry action-map)
118       (setq key-width
119             (max key-width
120                  (string-width (format-kbd-macro (first entry))))))
121     ;; Format each action
122     (mapcar (lambda (entry)
123               (let ((key (format-kbd-macro (first entry)))
124                     (desc (second entry)))
125                 (concat
126                  (propertize key 'face 'minibuffer-prompt)
127                  (make-string (- key-width (length key)) ? )
128                  " " desc)))
129             action-map)))
130
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))
139                   (length items))))
140     (while items
141       (dotimes (col ncols)
142         (when items
143           (let ((item (pop items)))
144             (insert item)
145             (when (and items (< col (- ncols 1)))
146               (insert (make-string (- col-width (string-width item)) ? ))))))
147       (when items
148         (insert "\n")))))
149
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     map)
156   "Base keymap for notmuch-jump's minibuffer keymap.")
157
158 (defun notmuch-jump--make-keymap (action-map)
159   "Translate ACTION-MAP into a minibuffer keymap."
160   (let ((map (make-sparse-keymap)))
161     (set-keymap-parent map notmuch-jump-minibuffer-map)
162     (dolist (action action-map)
163       (define-key map (first action)
164         `(lambda () (interactive)
165            (setq notmuch-jump--action ',(third action))
166            (exit-minibuffer))))
167     map))
168
169 (unless (fboundp 'window-body-width)
170   ;; Compatibility for Emacs pre-24
171   (defun window-body-width (&optional window)
172     (let ((edges (window-inside-edges window)))
173       (- (caddr edges) (car edges)))))