]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-jump.el
0193f8cd7064436efb73f13da4ea798668cbfef0
[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 (unless (fboundp 'window-body-width)
29   ;; Compatibility for Emacs pre-24
30   (defalias 'window-body-width 'window-width))
31
32 ;;;###autoload
33 (defun notmuch-jump-search ()
34   "Jump to a saved search by shortcut key.
35
36 This prompts for and performs a saved search using the shortcut
37 keys configured in the :key property of `notmuch-saved-searches'.
38 Typically these shortcuts are a single key long, so this is a
39 fast way to jump to a saved search from anywhere in Notmuch."
40   (interactive)
41
42   ;; Build the action map
43   (let (action-map)
44     (dolist (saved-search notmuch-saved-searches)
45       (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
46              (key (plist-get saved-search :key)))
47         (when key
48           (let ((name (plist-get saved-search :name))
49                 (query (plist-get saved-search :query))
50                 (oldest-first
51                  (case (plist-get saved-search :sort-order)
52                    (newest-first nil)
53                    (oldest-first t)
54                    (otherwise (default-value 'notmuch-search-oldest-first)))))
55             (push (list key name
56                         `(lambda () (notmuch-search ',query ',oldest-first)))
57                   action-map)))))
58     (setq action-map (nreverse action-map))
59
60     (if action-map
61         (notmuch-jump action-map "Search: ")
62       (error "To use notmuch-jump, please customize shortcut keys in notmuch-saved-searches."))))
63
64 (defvar notmuch-jump--action nil)
65
66 (defun notmuch-jump (action-map prompt)
67   "Interactively prompt for one of the keys in ACTION-MAP.
68
69 Displays a summary of all bindings in ACTION-MAP in the
70 minibuffer, reads a key from the minibuffer, and performs the
71 corresponding action.  The prompt can be canceled with C-g or
72 RET.  PROMPT must be a string to use for the prompt.  PROMPT
73 should include a space at the end.
74
75 ACTION-MAP must be a list of triples of the form
76   (KEY LABEL ACTION)
77 where KEY is a key binding, LABEL is a string label to display in
78 the buffer, and ACTION is a nullary function to call.  LABEL may
79 be null, in which case the action will still be bound, but will
80 not appear in the pop-up buffer.
81 "
82
83   (let* ((items (notmuch-jump--format-actions action-map))
84          ;; Format the table of bindings and the full prompt
85          (table
86           (with-temp-buffer
87             (notmuch-jump--insert-items (window-body-width) items)
88             (buffer-string)))
89          (full-prompt
90           (concat table "\n\n"
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
96          ;; own re-face-ing.
97          (minibuffer-prompt-properties
98           (notmuch-plist-delete
99            (copy-sequence minibuffer-prompt-properties)
100            'face))
101          ;; Build the keymap with our bindings
102          (minibuffer-map (notmuch-jump--make-keymap action-map))
103          ;; The bindings save the the action in notmuch-jump--action
104          (notmuch-jump--action nil))
105     ;; Read the action
106     (read-from-minibuffer full-prompt nil minibuffer-map)
107
108     ;; If we got an action, do it
109     (when notmuch-jump--action
110       (funcall notmuch-jump--action))))
111
112 (defun notmuch-jump--format-actions (action-map)
113   "Format the actions in ACTION-MAP.
114
115 Returns a list of strings, one for each item with a label in
116 ACTION-MAP.  These strings can be inserted into a tabular
117 buffer."
118
119   ;; Compute the maximum key description width
120   (let ((key-width 1))
121     (dolist (entry action-map)
122       (setq key-width
123             (max key-width
124                  (string-width (format-kbd-macro (first entry))))))
125     ;; Format each action
126     (mapcar (lambda (entry)
127               (let ((key (format-kbd-macro (first entry)))
128                     (desc (second entry)))
129                 (concat
130                  (propertize key 'face 'minibuffer-prompt)
131                  (make-string (- key-width (length key)) ? )
132                  " " desc)))
133             action-map)))
134
135 (defun notmuch-jump--insert-items (width items)
136   "Make a table of ITEMS up to WIDTH wide in the current buffer."
137   (let* ((nitems (length items))
138          (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
139          (ncols (if (> (* col-width nitems) width)
140                     (max 1 (/ width col-width))
141                   ;; Items fit on one line.  Space them out
142                   (setq col-width (/ width nitems))
143                   (length items))))
144     (while items
145       (dotimes (col ncols)
146         (when items
147           (let ((item (pop items)))
148             (insert item)
149             (when (and items (< col (- ncols 1)))
150               (insert (make-string (- col-width (string-width item)) ? ))))))
151       (when items
152         (insert "\n")))))
153
154 (defvar notmuch-jump-minibuffer-map
155   (let ((map (make-sparse-keymap)))
156     (set-keymap-parent map minibuffer-local-map)
157     ;; Make this like a special-mode keymap, with no self-insert-command
158     (suppress-keymap map)
159     map)
160   "Base keymap for notmuch-jump's minibuffer keymap.")
161
162 (defun notmuch-jump--make-keymap (action-map)
163   "Translate ACTION-MAP into a minibuffer keymap."
164   (let ((map (make-sparse-keymap)))
165     (set-keymap-parent map notmuch-jump-minibuffer-map)
166     (dolist (action action-map)
167       (define-key map (first action)
168         `(lambda () (interactive)
169            (setq notmuch-jump--action ',(third action))
170            (exit-minibuffer))))
171     map))