]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-jump.el
emacs: Remove excess empty lines
[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 <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21 ;;          David Edmondson <dme@dme.org>
22
23 ;;; Code:
24
25 (eval-when-compile
26   (require 'cl-lib)
27   (require 'pcase))
28
29 (require 'notmuch-lib)
30 (require 'notmuch-hello)
31
32 (eval-and-compile
33   (unless (fboundp 'window-body-width)
34     ;; Compatibility for Emacs pre-24
35     (defalias 'window-body-width 'window-width)))
36
37 ;;;###autoload
38 (defun notmuch-jump-search ()
39   "Jump to a saved search by shortcut key.
40
41 This prompts for and performs a saved search using the shortcut
42 keys configured in the :key property of `notmuch-saved-searches'.
43 Typically these shortcuts are a single key long, so this is a
44 fast way to jump to a saved search from anywhere in Notmuch."
45   (interactive)
46   ;; Build the action map
47   (let (action-map)
48     (dolist (saved-search notmuch-saved-searches)
49       (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
50              (key (plist-get saved-search :key)))
51         (when key
52           (let ((name (plist-get saved-search :name))
53                 (query (plist-get saved-search :query))
54                 (oldest-first
55                  (cl-case (plist-get saved-search :sort-order)
56                    (newest-first nil)
57                    (oldest-first t)
58                    (otherwise (default-value 'notmuch-search-oldest-first)))))
59             (push (list key name
60                         (cond
61                          ((eq (plist-get saved-search :search-type) 'tree)
62                           `(lambda () (notmuch-tree ',query)))
63                          ((eq (plist-get saved-search :search-type) 'unthreaded)
64                           `(lambda () (notmuch-unthreaded ',query)))
65                          (t
66                           `(lambda () (notmuch-search ',query ',oldest-first)))))
67                   action-map)))))
68     (setq action-map (nreverse action-map))
69     (if action-map
70         (notmuch-jump action-map "Search: ")
71       (error "To use notmuch-jump, \
72 please customize shortcut keys in notmuch-saved-searches."))))
73
74 (defvar notmuch-jump--action nil)
75
76 (defun notmuch-jump (action-map prompt)
77   "Interactively prompt for one of the keys in ACTION-MAP.
78
79 Displays a summary of all bindings in ACTION-MAP in the
80 minibuffer, reads a key from the minibuffer, and performs the
81 corresponding action.  The prompt can be canceled with C-g or
82 RET.  PROMPT must be a string to use for the prompt.  PROMPT
83 should include a space at the end.
84
85 ACTION-MAP must be a list of triples of the form
86   (KEY LABEL ACTION)
87 where KEY is a key binding, LABEL is a string label to display in
88 the buffer, and ACTION is a nullary function to call.  LABEL may
89 be null, in which case the action will still be bound, but will
90 not appear in the pop-up buffer."
91   (let* ((items (notmuch-jump--format-actions action-map))
92          ;; Format the table of bindings and the full prompt
93          (table
94           (with-temp-buffer
95             (notmuch-jump--insert-items (window-body-width) items)
96             (buffer-string)))
97          (full-prompt
98           (concat table "\n\n"
99                   (propertize prompt 'face 'minibuffer-prompt)))
100          ;; By default, the minibuffer applies the minibuffer face to
101          ;; the entire prompt.  However, we want to clearly
102          ;; distinguish bindings (which we put in the prompt face
103          ;; ourselves) from their labels, so disable the minibuffer's
104          ;; own re-face-ing.
105          (minibuffer-prompt-properties
106           (notmuch-plist-delete
107            (copy-sequence minibuffer-prompt-properties)
108            'face))
109          ;; Build the keymap with our bindings
110          (minibuffer-map (notmuch-jump--make-keymap action-map prompt))
111          ;; The bindings save the the action in notmuch-jump--action
112          (notmuch-jump--action nil))
113     ;; Read the action
114     (read-from-minibuffer full-prompt nil minibuffer-map)
115     ;; If we got an action, do it
116     (when notmuch-jump--action
117       (funcall notmuch-jump--action))))
118
119 (defun notmuch-jump--format-actions (action-map)
120   "Format the actions in ACTION-MAP.
121
122 Returns a list of strings, one for each item with a label in
123 ACTION-MAP.  These strings can be inserted into a tabular
124 buffer."
125   ;; Compute the maximum key description width
126   (let ((key-width 1))
127     (pcase-dolist (`(,key ,desc) action-map)
128       (setq key-width
129             (max key-width
130                  (string-width (format-kbd-macro key)))))
131     ;; Format each action
132     (mapcar (pcase-lambda (`(,key ,desc))
133               (setq key (format-kbd-macro key))
134               (concat (propertize key 'face 'minibuffer-prompt)
135                       (make-string (- key-width (length key)) ? )
136                       " " desc))
137             action-map)))
138
139 (defun notmuch-jump--insert-items (width items)
140   "Make a table of ITEMS up to WIDTH wide in the current buffer."
141   (let* ((nitems (length items))
142          (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
143          (ncols (if (> (* col-width nitems) width)
144                     (max 1 (/ width col-width))
145                   ;; Items fit on one line.  Space them out
146                   (setq col-width (/ width nitems))
147                   (length items))))
148     (while items
149       (dotimes (col ncols)
150         (when items
151           (let ((item (pop items)))
152             (insert item)
153             (when (and items (< col (- ncols 1)))
154               (insert (make-string (- col-width (string-width item)) ? ))))))
155       (when items
156         (insert "\n")))))
157
158 (defvar notmuch-jump-minibuffer-map
159   (let ((map (make-sparse-keymap)))
160     (set-keymap-parent map minibuffer-local-map)
161     ;; Make this like a special-mode keymap, with no self-insert-command
162     (suppress-keymap map)
163     (define-key map (kbd "DEL") 'exit-minibuffer)
164     map)
165   "Base keymap for notmuch-jump's minibuffer keymap.")
166
167 (defun notmuch-jump--make-keymap (action-map prompt)
168   "Translate ACTION-MAP into a minibuffer keymap."
169   (let ((map (make-sparse-keymap)))
170     (set-keymap-parent map notmuch-jump-minibuffer-map)
171     (pcase-dolist (`(,key ,name ,fn) action-map)
172       (if (= (length key) 1)
173           (define-key map key
174             `(lambda () (interactive)
175                (setq notmuch-jump--action ',fn)
176                (exit-minibuffer)))))
177     ;; By doing this in two passes (and checking if we already have a
178     ;; binding) we avoid problems if the user specifies a binding which
179     ;; is a prefix of another binding.
180     (pcase-dolist (`(,key ,name ,fn) action-map)
181       (if (> (length key) 1)
182           (let* ((key (elt key 0))
183                  (keystr (string key))
184                  (new-prompt (concat prompt (format-kbd-macro keystr) " "))
185                  (action-submap nil))
186             (unless (lookup-key map keystr)
187               (pcase-dolist (`(,k ,n ,f) action-map)
188                 (when (= key (elt k 0))
189                   (push (list (substring k 1) n f) action-submap)))
190               ;; We deal with backspace specially
191               (push (list (kbd "DEL")
192                           "Backup"
193                           (apply-partially #'notmuch-jump action-map prompt))
194                     action-submap)
195               (setq action-submap (nreverse action-submap))
196               (define-key map keystr
197                 `(lambda () (interactive)
198                    (setq notmuch-jump--action
199                          ',(apply-partially #'notmuch-jump
200                                             action-submap
201                                             new-prompt))
202                    (exit-minibuffer)))))))
203     map))
204
205 ;;
206
207 (provide 'notmuch-jump)
208
209 ;;; notmuch-jump.el ends here