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