]> 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  -*- 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 (defvar notmuch-jump--action nil)
66
67 ;;;###autoload
68 (defun notmuch-jump (action-map prompt)
69   "Interactively prompt for one of the keys in ACTION-MAP.
70
71 Displays a summary of all bindings in ACTION-MAP in the
72 minibuffer, reads a key from the minibuffer, and performs the
73 corresponding action.  The prompt can be canceled with C-g or
74 RET.  PROMPT must be a string to use for the prompt.  PROMPT
75 should include a space at the end.
76
77 ACTION-MAP must be a list of triples of the form
78   (KEY LABEL ACTION)
79 where KEY is a key binding, LABEL is a string label to display in
80 the buffer, and ACTION is a nullary function to call.  LABEL may
81 be null, in which case the action will still be bound, but will
82 not appear in the pop-up buffer."
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 prompt))
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     ;; If we got an action, do it
108     (when notmuch-jump--action
109       (funcall notmuch-jump--action))))
110
111 (defun notmuch-jump--format-actions (action-map)
112   "Format the actions in ACTION-MAP.
113
114 Returns a list of strings, one for each item with a label in
115 ACTION-MAP.  These strings can be inserted into a tabular
116 buffer."
117   ;; Compute the maximum key description width
118   (let ((key-width 1))
119     (pcase-dolist (`(,key ,_desc) action-map)
120       (setq key-width
121             (max key-width
122                  (string-width (format-kbd-macro key)))))
123     ;; Format each action
124     (mapcar (pcase-lambda (`(,key ,desc))
125               (setq key (format-kbd-macro key))
126               (concat (propertize key 'face 'minibuffer-prompt)
127                       (make-string (- key-width (length key)) ? )
128                       " " desc))
129             action-map)))
130
131 (defun notmuch-jump--insert-items (width items)
132   "Make a table of ITEMS up to WIDTH wide in the current buffer."
133   (let* ((nitems (length items))
134          (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
135          (ncols (if (> (* col-width nitems) width)
136                     (max 1 (/ width col-width))
137                   ;; Items fit on one line.  Space them out
138                   (setq col-width (/ width nitems))
139                   (length items))))
140     (while items
141       (dotimes (col ncols)
142         (when items
143           (let ((item (pop items)))
144             (insert item)
145             (when (and items (< col (- ncols 1)))
146               (insert (make-string (- col-width (string-width item)) ? ))))))
147       (when items
148         (insert "\n")))))
149
150 (defvar notmuch-jump-minibuffer-map
151   (let ((map (make-sparse-keymap)))
152     (set-keymap-parent map minibuffer-local-map)
153     ;; Make this like a special-mode keymap, with no self-insert-command
154     (suppress-keymap map)
155     (define-key map (kbd "DEL") 'exit-minibuffer)
156     map)
157   "Base keymap for notmuch-jump's minibuffer keymap.")
158
159 (defun notmuch-jump--make-keymap (action-map prompt)
160   "Translate ACTION-MAP into a minibuffer keymap."
161   (let ((map (make-sparse-keymap)))
162     (set-keymap-parent map notmuch-jump-minibuffer-map)
163     (pcase-dolist (`(,key ,_name ,fn) action-map)
164       (when (= (length key) 1)
165         (define-key map key
166           `(lambda () (interactive)
167              (setq notmuch-jump--action ',fn)
168              (exit-minibuffer)))))
169     ;; By doing this in two passes (and checking if we already have a
170     ;; binding) we avoid problems if the user specifies a binding which
171     ;; is a prefix of another binding.
172     (pcase-dolist (`(,key ,_name ,_fn) action-map)
173       (when (> (length key) 1)
174         (let* ((key (elt key 0))
175                (keystr (string key))
176                (new-prompt (concat prompt (format-kbd-macro keystr) " "))
177                (action-submap nil))
178           (unless (lookup-key map keystr)
179             (pcase-dolist (`(,k ,n ,f) action-map)
180               (when (= key (elt k 0))
181                 (push (list (substring k 1) n f) action-submap)))
182             ;; We deal with backspace specially
183             (push (list (kbd "DEL")
184                         "Backup"
185                         (apply-partially #'notmuch-jump action-map prompt))
186                   action-submap)
187             (setq action-submap (nreverse action-submap))
188             (define-key map keystr
189               `(lambda () (interactive)
190                  (setq notmuch-jump--action
191                        ',(apply-partially #'notmuch-jump
192                                           action-submap
193                                           new-prompt))
194                  (exit-minibuffer)))))))
195     map))
196
197 (provide 'notmuch-jump)
198
199 ;;; notmuch-jump.el ends here