2021-01-10 15:00:47 +01:00
|
|
|
;;; notmuch-jump.el --- User-friendly shortcut keys -*- lexical-binding: t -*-
|
2014-08-05 03:45:20 +02:00
|
|
|
;;
|
|
|
|
;; Copyright © Austin Clements
|
|
|
|
;;
|
|
|
|
;; This file is part of Notmuch.
|
|
|
|
;;
|
|
|
|
;; Notmuch is free software: you can redistribute it and/or modify it
|
|
|
|
;; under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; Notmuch is distributed in the hope that it will be useful, but
|
|
|
|
;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;; General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
;; along with Notmuch. If not, see <https://www.gnu.org/licenses/>.
|
2014-08-05 03:45:20 +02:00
|
|
|
;;
|
|
|
|
;; Authors: Austin Clements <aclements@csail.mit.edu>
|
|
|
|
;; David Edmondson <dme@dme.org>
|
|
|
|
|
2016-04-13 09:58:47 +02:00
|
|
|
;;; Code:
|
|
|
|
|
2014-08-05 03:45:20 +02:00
|
|
|
(require 'notmuch-lib)
|
|
|
|
(require 'notmuch-hello)
|
|
|
|
|
2021-07-19 13:31:06 +02:00
|
|
|
(declare-function notmuch-search "notmuch")
|
|
|
|
(declare-function notmuch-tree "notmuch-tree")
|
|
|
|
(declare-function notmuch-unthreaded "notmuch-tree")
|
|
|
|
|
2014-08-05 03:45:20 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun notmuch-jump-search ()
|
|
|
|
"Jump to a saved search by shortcut key.
|
|
|
|
|
|
|
|
This prompts for and performs a saved search using the shortcut
|
|
|
|
keys configured in the :key property of `notmuch-saved-searches'.
|
|
|
|
Typically these shortcuts are a single key long, so this is a
|
|
|
|
fast way to jump to a saved search from anywhere in Notmuch."
|
|
|
|
(interactive)
|
|
|
|
;; Build the action map
|
|
|
|
(let (action-map)
|
|
|
|
(dolist (saved-search notmuch-saved-searches)
|
|
|
|
(let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search))
|
|
|
|
(key (plist-get saved-search :key)))
|
|
|
|
(when key
|
|
|
|
(let ((name (plist-get saved-search :name))
|
|
|
|
(query (plist-get saved-search :query))
|
|
|
|
(oldest-first
|
2020-04-25 22:18:07 +02:00
|
|
|
(cl-case (plist-get saved-search :sort-order)
|
2014-08-05 03:45:20 +02:00
|
|
|
(newest-first nil)
|
|
|
|
(oldest-first t)
|
2024-03-10 19:57:41 +01:00
|
|
|
(otherwise (default-value 'notmuch-search-oldest-first))))
|
|
|
|
(exclude (cl-case (plist-get saved-search :excluded)
|
|
|
|
(hide t)
|
|
|
|
(show nil)
|
|
|
|
(otherwise notmuch-search-hide-excluded))))
|
2014-08-05 03:45:20 +02:00
|
|
|
(push (list key name
|
2020-02-27 18:16:52 +01:00
|
|
|
(cond
|
|
|
|
((eq (plist-get saved-search :search-type) 'tree)
|
2024-03-10 19:57:41 +01:00
|
|
|
(lambda () (notmuch-tree query nil nil nil nil nil nil
|
|
|
|
oldest-first exclude)))
|
2020-02-27 18:16:52 +01:00
|
|
|
((eq (plist-get saved-search :search-type) 'unthreaded)
|
2024-03-10 19:57:41 +01:00
|
|
|
(lambda () (notmuch-unthreaded query nil nil nil nil
|
|
|
|
oldest-first exclude)))
|
2020-02-27 18:16:52 +01:00
|
|
|
(t
|
2024-03-10 19:57:41 +01:00
|
|
|
(lambda () (notmuch-search query oldest-first exclude)))))
|
2014-08-05 03:45:20 +02:00
|
|
|
action-map)))))
|
|
|
|
(setq action-map (nreverse action-map))
|
|
|
|
(if action-map
|
|
|
|
(notmuch-jump action-map "Search: ")
|
2021-01-13 18:37:50 +01:00
|
|
|
(error "To use notmuch-jump, %s"
|
|
|
|
"please customize shortcut keys in notmuch-saved-searches."))))
|
2014-08-05 03:45:20 +02:00
|
|
|
|
2021-05-21 18:55:31 +02:00
|
|
|
(defface notmuch-jump-key
|
|
|
|
'((t :inherit minibuffer-prompt))
|
|
|
|
"Default face used for keys in `notmuch-jump' and related."
|
|
|
|
:group 'notmuch-faces)
|
|
|
|
|
2014-08-05 03:45:20 +02:00
|
|
|
(defvar notmuch-jump--action nil)
|
|
|
|
|
2020-08-08 13:49:52 +02:00
|
|
|
;;;###autoload
|
2014-08-05 03:45:20 +02:00
|
|
|
(defun notmuch-jump (action-map prompt)
|
|
|
|
"Interactively prompt for one of the keys in ACTION-MAP.
|
|
|
|
|
|
|
|
Displays a summary of all bindings in ACTION-MAP in the
|
|
|
|
minibuffer, reads a key from the minibuffer, and performs the
|
|
|
|
corresponding action. The prompt can be canceled with C-g or
|
|
|
|
RET. PROMPT must be a string to use for the prompt. PROMPT
|
|
|
|
should include a space at the end.
|
|
|
|
|
|
|
|
ACTION-MAP must be a list of triples of the form
|
|
|
|
(KEY LABEL ACTION)
|
|
|
|
where KEY is a key binding, LABEL is a string label to display in
|
|
|
|
the buffer, and ACTION is a nullary function to call. LABEL may
|
|
|
|
be null, in which case the action will still be bound, but will
|
2020-05-24 21:17:56 +02:00
|
|
|
not appear in the pop-up buffer."
|
2014-08-05 03:45:20 +02:00
|
|
|
(let* ((items (notmuch-jump--format-actions action-map))
|
|
|
|
;; Format the table of bindings and the full prompt
|
|
|
|
(table
|
|
|
|
(with-temp-buffer
|
|
|
|
(notmuch-jump--insert-items (window-body-width) items)
|
|
|
|
(buffer-string)))
|
|
|
|
(full-prompt
|
|
|
|
(concat table "\n\n"
|
2021-07-17 10:30:18 +02:00
|
|
|
(propertize prompt 'face 'minibuffer-prompt)))
|
2014-08-05 03:45:20 +02:00
|
|
|
;; By default, the minibuffer applies the minibuffer face to
|
|
|
|
;; the entire prompt. However, we want to clearly
|
|
|
|
;; distinguish bindings (which we put in the prompt face
|
|
|
|
;; ourselves) from their labels, so disable the minibuffer's
|
|
|
|
;; own re-face-ing.
|
|
|
|
(minibuffer-prompt-properties
|
|
|
|
(notmuch-plist-delete
|
|
|
|
(copy-sequence minibuffer-prompt-properties)
|
|
|
|
'face))
|
|
|
|
;; Build the keymap with our bindings
|
2016-10-15 13:50:42 +02:00
|
|
|
(minibuffer-map (notmuch-jump--make-keymap action-map prompt))
|
2014-08-05 03:45:20 +02:00
|
|
|
;; The bindings save the the action in notmuch-jump--action
|
|
|
|
(notmuch-jump--action nil))
|
|
|
|
;; Read the action
|
|
|
|
(read-from-minibuffer full-prompt nil minibuffer-map)
|
|
|
|
;; If we got an action, do it
|
|
|
|
(when notmuch-jump--action
|
|
|
|
(funcall notmuch-jump--action))))
|
|
|
|
|
|
|
|
(defun notmuch-jump--format-actions (action-map)
|
|
|
|
"Format the actions in ACTION-MAP.
|
|
|
|
|
|
|
|
Returns a list of strings, one for each item with a label in
|
|
|
|
ACTION-MAP. These strings can be inserted into a tabular
|
|
|
|
buffer."
|
|
|
|
;; Compute the maximum key description width
|
|
|
|
(let ((key-width 1))
|
2021-01-10 15:00:48 +01:00
|
|
|
(pcase-dolist (`(,key ,_desc) action-map)
|
2014-08-05 03:45:20 +02:00
|
|
|
(setq key-width
|
|
|
|
(max key-width
|
2020-04-25 22:18:07 +02:00
|
|
|
(string-width (format-kbd-macro key)))))
|
2014-08-05 03:45:20 +02:00
|
|
|
;; Format each action
|
2020-04-25 22:18:07 +02:00
|
|
|
(mapcar (pcase-lambda (`(,key ,desc))
|
|
|
|
(setq key (format-kbd-macro key))
|
2021-05-21 18:55:31 +02:00
|
|
|
(concat (propertize key 'face 'notmuch-jump-key)
|
2020-04-25 22:18:07 +02:00
|
|
|
(make-string (- key-width (length key)) ? )
|
|
|
|
" " desc))
|
2014-08-05 03:45:20 +02:00
|
|
|
action-map)))
|
|
|
|
|
|
|
|
(defun notmuch-jump--insert-items (width items)
|
|
|
|
"Make a table of ITEMS up to WIDTH wide in the current buffer."
|
|
|
|
(let* ((nitems (length items))
|
|
|
|
(col-width (+ 3 (apply #'max (mapcar #'string-width items))))
|
|
|
|
(ncols (if (> (* col-width nitems) width)
|
|
|
|
(max 1 (/ width col-width))
|
|
|
|
;; Items fit on one line. Space them out
|
|
|
|
(setq col-width (/ width nitems))
|
|
|
|
(length items))))
|
|
|
|
(while items
|
|
|
|
(dotimes (col ncols)
|
|
|
|
(when items
|
|
|
|
(let ((item (pop items)))
|
|
|
|
(insert item)
|
|
|
|
(when (and items (< col (- ncols 1)))
|
|
|
|
(insert (make-string (- col-width (string-width item)) ? ))))))
|
|
|
|
(when items
|
|
|
|
(insert "\n")))))
|
|
|
|
|
|
|
|
(defvar notmuch-jump-minibuffer-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(set-keymap-parent map minibuffer-local-map)
|
|
|
|
;; Make this like a special-mode keymap, with no self-insert-command
|
|
|
|
(suppress-keymap map)
|
2016-10-15 13:50:42 +02:00
|
|
|
(define-key map (kbd "DEL") 'exit-minibuffer)
|
2014-08-05 03:45:20 +02:00
|
|
|
map)
|
|
|
|
"Base keymap for notmuch-jump's minibuffer keymap.")
|
|
|
|
|
2016-10-15 13:50:42 +02:00
|
|
|
(defun notmuch-jump--make-keymap (action-map prompt)
|
2014-08-05 03:45:20 +02:00
|
|
|
"Translate ACTION-MAP into a minibuffer keymap."
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(set-keymap-parent map notmuch-jump-minibuffer-map)
|
2021-01-10 15:00:48 +01:00
|
|
|
(pcase-dolist (`(,key ,_name ,fn) action-map)
|
2020-08-08 13:49:43 +02:00
|
|
|
(when (= (length key) 1)
|
|
|
|
(define-key map key
|
2021-07-19 13:31:05 +02:00
|
|
|
(lambda ()
|
|
|
|
(interactive)
|
|
|
|
(setq notmuch-jump--action fn)
|
|
|
|
(exit-minibuffer)))))
|
2016-10-15 13:50:42 +02:00
|
|
|
;; By doing this in two passes (and checking if we already have a
|
|
|
|
;; binding) we avoid problems if the user specifies a binding which
|
|
|
|
;; is a prefix of another binding.
|
2021-01-10 15:00:48 +01:00
|
|
|
(pcase-dolist (`(,key ,_name ,_fn) action-map)
|
2020-08-08 13:49:43 +02:00
|
|
|
(when (> (length key) 1)
|
|
|
|
(let* ((key (elt key 0))
|
|
|
|
(keystr (string key))
|
|
|
|
(new-prompt (concat prompt (format-kbd-macro keystr) " "))
|
|
|
|
(action-submap nil))
|
|
|
|
(unless (lookup-key map keystr)
|
|
|
|
(pcase-dolist (`(,k ,n ,f) action-map)
|
|
|
|
(when (= key (elt k 0))
|
|
|
|
(push (list (substring k 1) n f) action-submap)))
|
|
|
|
;; We deal with backspace specially
|
|
|
|
(push (list (kbd "DEL")
|
|
|
|
"Backup"
|
|
|
|
(apply-partially #'notmuch-jump action-map prompt))
|
|
|
|
action-submap)
|
|
|
|
(setq action-submap (nreverse action-submap))
|
|
|
|
(define-key map keystr
|
2021-07-19 13:31:05 +02:00
|
|
|
(lambda ()
|
|
|
|
(interactive)
|
|
|
|
(setq notmuch-jump--action
|
|
|
|
(apply-partially #'notmuch-jump
|
|
|
|
action-submap
|
|
|
|
new-prompt))
|
|
|
|
(exit-minibuffer)))))))
|
2014-08-05 03:45:20 +02:00
|
|
|
map))
|
2014-10-22 08:05:33 +02:00
|
|
|
|
|
|
|
(provide 'notmuch-jump)
|
2016-04-13 09:58:47 +02:00
|
|
|
|
|
|
|
;;; notmuch-jump.el ends here
|