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