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