]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-jump.el
emacs: allow saved searches to select tree-view
[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                         (if (eq (plist-get saved-search :search-type) 'tree)
58                             `(lambda () (notmuch-tree ',query))
59                           `(lambda () (notmuch-search ',query ',oldest-first))))
60                   action-map)))))
61     (setq action-map (nreverse action-map))
62
63     (if action-map
64         (notmuch-jump action-map "Search: ")
65       (error "To use notmuch-jump, please customize shortcut keys in notmuch-saved-searches."))))
66
67 (defvar notmuch-jump--action nil)
68
69 (defun notmuch-jump (action-map prompt)
70   "Interactively prompt for one of the keys in ACTION-MAP.
71
72 Displays a summary of all bindings in ACTION-MAP in the
73 minibuffer, reads a key from the minibuffer, and performs the
74 corresponding action.  The prompt can be canceled with C-g or
75 RET.  PROMPT must be a string to use for the prompt.  PROMPT
76 should include a space at the end.
77
78 ACTION-MAP must be a list of triples of the form
79   (KEY LABEL ACTION)
80 where KEY is a key binding, LABEL is a string label to display in
81 the buffer, and ACTION is a nullary function to call.  LABEL may
82 be null, in which case the action will still be bound, but will
83 not appear in the pop-up buffer.
84 "
85
86   (let* ((items (notmuch-jump--format-actions action-map))
87          ;; Format the table of bindings and the full prompt
88          (table
89           (with-temp-buffer
90             (notmuch-jump--insert-items (window-body-width) items)
91             (buffer-string)))
92          (full-prompt
93           (concat table "\n\n"
94                   (propertize prompt 'face 'minibuffer-prompt)))
95          ;; By default, the minibuffer applies the minibuffer face to
96          ;; the entire prompt.  However, we want to clearly
97          ;; distinguish bindings (which we put in the prompt face
98          ;; ourselves) from their labels, so disable the minibuffer's
99          ;; own re-face-ing.
100          (minibuffer-prompt-properties
101           (notmuch-plist-delete
102            (copy-sequence minibuffer-prompt-properties)
103            'face))
104          ;; Build the keymap with our bindings
105          (minibuffer-map (notmuch-jump--make-keymap action-map))
106          ;; The bindings save the the action in notmuch-jump--action
107          (notmuch-jump--action nil))
108     ;; Read the action
109     (read-from-minibuffer full-prompt nil minibuffer-map)
110
111     ;; If we got an action, do it
112     (when notmuch-jump--action
113       (funcall notmuch-jump--action))))
114
115 (defun notmuch-jump--format-actions (action-map)
116   "Format the actions in ACTION-MAP.
117
118 Returns a list of strings, one for each item with a label in
119 ACTION-MAP.  These strings can be inserted into a tabular
120 buffer."
121
122   ;; Compute the maximum key description width
123   (let ((key-width 1))
124     (dolist (entry action-map)
125       (setq key-width
126             (max key-width
127                  (string-width (format-kbd-macro (first entry))))))
128     ;; Format each action
129     (mapcar (lambda (entry)
130               (let ((key (format-kbd-macro (first entry)))
131                     (desc (second entry)))
132                 (concat
133                  (propertize key 'face 'minibuffer-prompt)
134                  (make-string (- key-width (length key)) ? )
135                  " " desc)))
136             action-map)))
137
138 (defun notmuch-jump--insert-items (width items)
139   "Make a table of ITEMS up to WIDTH wide in the current buffer."
140   (let* ((nitems (length items))
141          (col-width (+ 3 (apply #'max (mapcar #'string-width items))))
142          (ncols (if (> (* col-width nitems) width)
143                     (max 1 (/ width col-width))
144                   ;; Items fit on one line.  Space them out
145                   (setq col-width (/ width nitems))
146                   (length items))))
147     (while items
148       (dotimes (col ncols)
149         (when items
150           (let ((item (pop items)))
151             (insert item)
152             (when (and items (< col (- ncols 1)))
153               (insert (make-string (- col-width (string-width item)) ? ))))))
154       (when items
155         (insert "\n")))))
156
157 (defvar notmuch-jump-minibuffer-map
158   (let ((map (make-sparse-keymap)))
159     (set-keymap-parent map minibuffer-local-map)
160     ;; Make this like a special-mode keymap, with no self-insert-command
161     (suppress-keymap map)
162     map)
163   "Base keymap for notmuch-jump's minibuffer keymap.")
164
165 (defun notmuch-jump--make-keymap (action-map)
166   "Translate ACTION-MAP into a minibuffer keymap."
167   (let ((map (make-sparse-keymap)))
168     (set-keymap-parent map notmuch-jump-minibuffer-map)
169     (dolist (action action-map)
170       (define-key map (first action)
171         `(lambda () (interactive)
172            (setq notmuch-jump--action ',(third action))
173            (exit-minibuffer))))
174     map))
175
176 ;;
177
178 (provide 'notmuch-jump)