From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
Date: Mon, 14 Jul 2014 12:02:14 -0400 [thread overview]
Message-ID: <1405353735-26244-2-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1405353735-26244-1-git-send-email-amdragon@mit.edu>
This introduces notmuch-jump, which is like a user-friendly,
user-configurable global prefix map for saved searches. This provides
a non-modal and much faster way to access saved searches than
notmuch-hello.
A user configures shortcut keys in notmuch-saved-searches, which are
immediately accessible from anywhere in Notmuch under the "j" key (for
"jump"). When the user hits "j", the minibuffer immediately shows a
helpful table of bindings reminiscent of a completions buffer.
This code is a combination of work from myself (originally,
"notmuch-go"), David Edmondson, and modifications from Mark Walters.
---
emacs/Makefile.local | 3 +-
emacs/notmuch-hello.el | 2 +
emacs/notmuch-jump.el | 189 +++++++++++++++++++++++++++++++++++++++++++++++++
emacs/notmuch-lib.el | 3 +
4 files changed, 196 insertions(+), 1 deletion(-)
create mode 100644 emacs/notmuch-jump.el
diff --git a/emacs/Makefile.local b/emacs/Makefile.local
index c0d6b19..1109cfa 100644
--- a/emacs/Makefile.local
+++ b/emacs/Makefile.local
@@ -18,7 +18,8 @@ emacs_sources := \
$(dir)/notmuch-tag.el \
$(dir)/coolj.el \
$(dir)/notmuch-print.el \
- $(dir)/notmuch-version.el
+ $(dir)/notmuch-version.el \
+ $(dir)/notmuch-jump.el \
$(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp
$(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl
diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 3de5238..061b27d 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -85,6 +85,7 @@ (define-widget 'notmuch-saved-search-plist 'list
(group :format "%v" :inline t (const :format " Query: " :query) (string :format "%v")))
(checklist :inline t
:format "%v"
+ (group :format "%v" :inline t (const :format "Shortcut key: " :key) (key-sequence :format "%v"))
(group :format "%v" :inline t (const :format "Count-Query: " :count-query) (string :format "%v"))
(group :format "%v" :inline t (const :format "" :sort-order)
(choice :tag " Sort Order"
@@ -101,6 +102,7 @@ (defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
:name Name of the search (required).
:query Search to run (required).
+ :key Optional shortcut key for `notmuch-jump-search'.
:count-query Optional extra query to generate the count
shown. If not present then the :query property
is used.
diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
new file mode 100644
index 0000000..cb1ae10
--- /dev/null
+++ b/emacs/notmuch-jump.el
@@ -0,0 +1,189 @@
+;; notmuch-jump.el --- User-friendly shortcut keys
+;;
+;; 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
+;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>.
+;;
+;; Authors: Austin Clements <aclements@csail.mit.edu>
+;; David Edmondson <dme@dme.org>
+
+(eval-when-compile (require 'cl))
+
+(require 'notmuch-hello)
+
+;;;###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
+ (case (plist-get saved-search :sort-order)
+ (newest-first nil)
+ (oldest-first t)
+ (otherwise (default-value notmuch-search-oldest-first)))))
+ (push (list key name
+ `(lambda () (notmuch-search ',query ',oldest-first)))
+ action-map)))))
+ (setq action-map (nreverse action-map))
+
+ (if action-map
+ (notmuch-jump action-map "Search ")
+ (error "No shortcut keys for saved searches. Please customize notmuch-saved-searches."))))
+
+(defvar notmuch-jump--action nil)
+
+(defun notmuch-jump (action-map prompt)
+ "Interactively prompt for one of the keys in ACTION-MAP.
+
+Displays a pop-up temporary buffer with a summary of all bindings
+in ACTION-MAP, reads a key from the minibuffer, and performs the
+corresponding action. The prompt can be canceled with C-g.
+PROMPT must be a string to use for the prompt if this command was
+not invoked directly by a key binding (e.g., it was invoked
+through M-x). 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
+not appear in the pop-up buffer.
+"
+
+ (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)))
+ (prompt-text
+ (if (eq this-original-command this-command)
+ ;; Make it look like we're just part of any regular
+ ;; submap prompt (like C-x, C-c, etc.)
+ (concat (format-kbd-macro (this-command-keys)) "-")
+ ;; We were invoked through something like M-x
+ prompt))
+ (full-prompt
+ (concat table "\n\n"
+ (propertize prompt-text 'face 'minibuffer-prompt)))
+ ;; 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-jump--plist-delete
+ (copy-sequence minibuffer-prompt-properties)
+ 'face))
+ ;; Build the keymap with our bindings
+ (minibuffer-map (notmuch-jump--make-keymap action-map))
+ ;; 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))
+ (dolist (action action-map)
+ (setq key-width
+ (max key-width
+ (string-width (format-kbd-macro (first action))))))
+ ;; Format each action
+ (mapcar (lambda (action)
+ (let ((key (format-kbd-macro (first action)))
+ (desc (second action)))
+ (concat
+ (propertize key 'face 'minibuffer-prompt)
+ (make-string (- key-width (length key)) ? )
+ " " desc)))
+ 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)
+ map)
+ "Base keymap for notmuch-jump's minibuffer keymap.")
+
+(defun notmuch-jump--make-keymap (action-map)
+ "Translate ACTION-MAP into a minibuffer keymap."
+ (let ((map (make-sparse-keymap)))
+ (set-keymap-parent map notmuch-jump-minibuffer-map)
+ (dolist (action action-map)
+ (define-key map (first action)
+ `(lambda () (interactive)
+ (setq notmuch-jump--action ',(third action))
+ (exit-minibuffer))))
+ map))
+
+(defun notmuch-jump--plist-delete (plist property)
+ (let* ((xplist (cons nil plist))
+ (pred xplist))
+ (while (cdr pred)
+ (when (eq (cadr pred) property)
+ (setcdr pred (cdddr pred)))
+ (setq pred (cddr pred)))
+ (cdr xplist)))
+
+(unless (fboundp 'window-body-width)
+ ;; Compatibility for Emacs pre-24
+ (defun window-body-width (&optional window)
+ (let ((edges (window-inside-edges window)))
+ (- (caddr edges) (car edges)))))
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 2941da3..135422d 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -130,9 +130,12 @@ (defvar notmuch-common-keymap
(define-key map "m" 'notmuch-mua-new-mail)
(define-key map "=" 'notmuch-refresh-this-buffer)
(define-key map "G" 'notmuch-poll-and-refresh-this-buffer)
+ (define-key map "j" 'notmuch-jump-search)
map)
"Keymap shared by all notmuch modes.")
+(autoload 'notmuch-jump-search "notmuch-jump" "Jump to a saved search by shortcut key." t)
+
;; By default clicking on a button does not select the window
;; containing the button (as opposed to clicking on a widget which
;; does). This means that the button action is then executed in the
--
2.0.0
next prev parent reply other threads:[~2014-07-14 16:02 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-14 16:02 [PATCH 0/2] emacs: Shortcut keys to saved searches Austin Clements
2014-07-14 16:02 ` Austin Clements [this message]
2014-07-14 21:22 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Mark Walters
2014-07-15 3:46 ` Austin Clements
2014-07-15 7:02 ` Mark Walters
2014-07-14 16:02 ` [PATCH 2/2] emacs: Expand default saved searches and add shortcut keys Austin Clements
2014-07-15 14:06 ` [PATCH v2 0/2] emacs: Shortcut keys to saved searches Austin Clements
2014-07-15 14:06 ` [PATCH v2 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
2014-08-04 23:00 ` David Bremner
2014-08-05 1:44 ` Austin Clements
2014-08-05 1:45 ` [PATCH v3 " Austin Clements
2014-08-05 1:45 ` [PATCH v3 2/2] emacs: Expand default saved searches and add shortcut keys Austin Clements
2014-08-05 11:20 ` [PATCH v3 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches David Bremner
2014-07-15 14:06 ` [PATCH v2 2/2] emacs: Expand default saved searches and add shortcut keys Austin Clements
2014-07-15 22:06 ` David Bremner
2014-07-15 20:18 ` [PATCH v2 0/2] emacs: Shortcut keys to saved searches Mark Walters
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1405353735-26244-2-git-send-email-amdragon@mit.edu \
--to=amdragon@mit.edu \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).