unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 0/2] emacs: Shortcut keys to saved searches
@ 2014-07-14 16:02 Austin Clements
  2014-07-14 16:02 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Austin Clements @ 2014-07-14 16:02 UTC (permalink / raw)
  To: notmuch

This series combines my original notmuch-go [1] with modifications
from Mark [2] and with dme's notmuch-jump [3].

Like dme's patch, this is bound to "j", is named notmuch-jump for
consistency, and uses shortcut keys configured through
notmuch-saved-searches.

I simplified the original notmuch-go code using an idea from dme's
patch to just show the bindings help in the minibuffer (rather than in
a separate buffer).  Unlike dme's patch, mine always shows the
bindings, rather than requiring the user to guess that "?" will do
something helpful.  This seems strictly more friendly to inexperienced
users and doesn't seem to have any real downside for experienced
users.  My patch also goes to more effort to display these bindings
nicely, since they're always visible, and suggests to the user what to
do when no bindings are configured.

[1] https://github.com/aclements/notmuch/blob/go-hack/emacs/notmuch-go.el
[2] http://www.maths.qmul.ac.uk/~walters/tmp/notmuch-go.el and http://www.maths.qmul.ac.uk/~walters/tmp/0001-defcustom.patch
[3] id:1399461694-25350-2-git-send-email-dme@dme.org

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-07-14 16:02 [PATCH 0/2] emacs: Shortcut keys to saved searches Austin Clements
@ 2014-07-14 16:02 ` Austin Clements
  2014-07-14 21:22   ` 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
  2 siblings, 1 reply; 16+ messages in thread
From: Austin Clements @ 2014-07-14 16:02 UTC (permalink / raw)
  To: notmuch

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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/2] emacs: Expand default saved searches and add shortcut keys
  2014-07-14 16:02 [PATCH 0/2] emacs: Shortcut keys to saved searches Austin Clements
  2014-07-14 16:02 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
@ 2014-07-14 16:02 ` Austin Clements
  2014-07-15 14:06 ` [PATCH v2 0/2] emacs: Shortcut keys to saved searches Austin Clements
  2 siblings, 0 replies; 16+ messages in thread
From: Austin Clements @ 2014-07-14 16:02 UTC (permalink / raw)
  To: notmuch

This should help new users off to a better start with the addition of
more sensible saved searches and default shortcut keys.  Most existing
users have probably customized this variable and won't be affected.
---
 emacs/notmuch-hello.el                              | 9 +++++++--
 test/emacs.expected-output/notmuch-hello            | 2 +-
 test/emacs.expected-output/notmuch-hello-long-names | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 061b27d..65d0627 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -93,8 +93,13 @@ (define-widget 'notmuch-saved-search-plist 'list
 				    (const :tag "Oldest-first" oldest-first)
 				    (const :tag "Newest-first" newest-first))))))
 
-(defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
-				    (:name "unread" :query "tag:unread"))
+(defcustom notmuch-saved-searches
+  `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
+    (:name "unread" :query "tag:unread" :key ,(kbd "u"))
+    (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
+    (:name "sent" :query "tag:sent" :key ,(kbd "t"))
+    (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
+    (:name "all mail" :query "*" :key ,(kbd "a")))
   "A list of saved searches to display.
 
 The saved search can be given in 3 forms. The preferred way is as
diff --git a/test/emacs.expected-output/notmuch-hello b/test/emacs.expected-output/notmuch-hello
index 2d69891..9ba4cfc 100644
--- a/test/emacs.expected-output/notmuch-hello
+++ b/test/emacs.expected-output/notmuch-hello
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
diff --git a/test/emacs.expected-output/notmuch-hello-long-names b/test/emacs.expected-output/notmuch-hello-long-names
index 486d0d9..1c8d6eb 100644
--- a/test/emacs.expected-output/notmuch-hello-long-names
+++ b/test/emacs.expected-output/notmuch-hello-long-names
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-07-14 16:02 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
@ 2014-07-14 21:22   ` Mark Walters
  2014-07-15  3:46     ` Austin Clements
  0 siblings, 1 reply; 16+ messages in thread
From: Mark Walters @ 2014-07-14 21:22 UTC (permalink / raw)
  To: Austin Clements, notmuch


On Mon, 14 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:
> 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.

I am basically happy with this: the only downside compared to dme's
patch is that this is quite substantially bigger. However, since this is
all in it's own file that is not really a problem.

I have a few comments below. In all cases I am happy to go with the
current code if you think it's better than my suggestion.

> 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)

I would probably not do the saved-search-to-plist bit and just use
notmuch-saved-search-get each time.

> +		   (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."))))

I would slightly rephrase the error: something more like "... To use
notmuch-jump please customize notmuch-saved-searches."  So that the user
who doesn't want to use notmuch-jump doesn't think they are at fault.

> +(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.

Maybe say the prompt can be canceled with RET too?

> +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.

I find the "j-" prompt a bit weird and would prefer something more like
"Jump to search: " to be used however the user enters the function.

> +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)

The name "action" is slightly unfortunate when you use ACTION as the
third item of each element of action-map when describing it
above. However, no better name springs to mind. (Maybe "triple"?)

> +      (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)

We don't normally seem to use autoload but instead use
declare-function. It might be worth being consistent (I am not very sure
of the pros and cons of autoload). May also be worth having it with the
other declare-functions to keep it clear how things are loaded.

Best wishes

Mark


> +
>  ;; 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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-07-14 21:22   ` Mark Walters
@ 2014-07-15  3:46     ` Austin Clements
  2014-07-15  7:02       ` Mark Walters
  0 siblings, 1 reply; 16+ messages in thread
From: Austin Clements @ 2014-07-15  3:46 UTC (permalink / raw)
  To: Mark Walters; +Cc: notmuch

Quoth Mark Walters on Jul 14 at 10:22 pm:
> 
> On Mon, 14 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:
> > 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.
> 
> I am basically happy with this: the only downside compared to dme's
> patch is that this is quite substantially bigger. However, since this is
> all in it's own file that is not really a problem.
> 
> I have a few comments below. In all cases I am happy to go with the
> current code if you think it's better than my suggestion.
> 
> > 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)
> 
> I would probably not do the saved-search-to-plist bit and just use
> notmuch-saved-search-get each time.

What's the downside of notmuch-hello-saved-search-to-plist?  Using
notmuch-saved-search-get everywhere is more verbose.

> > +		   (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."))))
> 
> I would slightly rephrase the error: something more like "... To use
> notmuch-jump please customize notmuch-saved-searches."  So that the user
> who doesn't want to use notmuch-jump doesn't think they are at fault.

Good idea.

> > +(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.
> 
> Maybe say the prompt can be canceled with RET too?

Done.  I also fixed the docstring's mention of a pop-up temporary
buffer, since that's not how the code works any more.

> > +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.
> 
> I find the "j-" prompt a bit weird and would prefer something more like
> "Jump to search: " to be used however the user enters the function.

The intent was to make it act like a prefix keymap.  For example, if
you hit "C-x", Emacs will show "C-x-" in the minibuffer until you hit
the next key.  OTOH, Emacs isn't exactly a paragon of usability, so
you're probably right and this should just use the provided prompt
string regardless.

> > +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)
> 
> The name "action" is slightly unfortunate when you use ACTION as the
> third item of each element of action-map when describing it
> above. However, no better name springs to mind. (Maybe "triple"?)

Good catch.  Changed to "entry" (for an entry in the 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)
> 
> We don't normally seem to use autoload but instead use
> declare-function. It might be worth being consistent (I am not very sure
> of the pros and cons of autoload). May also be worth having it with the
> other declare-functions to keep it clear how things are loaded.

I used autoload because it won't bother even reading
notmuch-jump.el(c) until the user hits "j" for the first time (and,
hence, won't load it at all if they don't use jump).  This is easy
with notmuch-jump because it's self-contained and has a single clear
entry-point and no customizable variables.

declare-function, on the other hand, still requires you to load the
source containing the function (either eagerly, which is what notmuch
usually does, or with an autoload).

I think we should do *more* autoloading, though maybe it's not
practical for the bulk of notmuch-emacs.

I'm happy to move the autoload call to somewhere else, though
notmuch-lib doesn't actually have any declare-functions (since it's
sort of a root of the dependency tree).  I could put it right below
the requires, since that's where we usually put declare-functions.

> Best wishes
> 
> Mark
> 
> 
> > +
> >  ;; 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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-07-15  3:46     ` Austin Clements
@ 2014-07-15  7:02       ` Mark Walters
  0 siblings, 0 replies; 16+ messages in thread
From: Mark Walters @ 2014-07-15  7:02 UTC (permalink / raw)
  To: Austin Clements; +Cc: notmuch

On Tue, 15 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:
> Quoth Mark Walters on Jul 14 at 10:22 pm:
>> 
>> On Mon, 14 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:
>> > 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.
>> 
>> I am basically happy with this: the only downside compared to dme's
>> patch is that this is quite substantially bigger. However, since this is
>> all in it's own file that is not really a problem.
>> 
>> I have a few comments below. In all cases I am happy to go with the
>> current code if you think it's better than my suggestion.
>> 
>> > 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)
>> 
>> I would probably not do the saved-search-to-plist bit and just use
>> notmuch-saved-search-get each time.
>
> What's the downside of notmuch-hello-saved-search-to-plist?  Using
> notmuch-saved-search-get everywhere is more verbose.

The only downside is that I sort of intended
notmuch-hello-saved-search-to-list to be a private function (I am not
sure why I gave notmuch-hello--saved-searches-to-plist the double dash
bit not this). But I am definitely happy with it as is: I wasn't sure
whether to make any comment when first reviewing.

>> > +		   (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."))))
>> 
>> I would slightly rephrase the error: something more like "... To use
>> notmuch-jump please customize notmuch-saved-searches."  So that the user
>> who doesn't want to use notmuch-jump doesn't think they are at fault.
>
> Good idea.
>
>> > +(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.
>> 
>> Maybe say the prompt can be canceled with RET too?
>
> Done.  I also fixed the docstring's mention of a pop-up temporary
> buffer, since that's not how the code works any more.
>
>> > +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.
>> 
>> I find the "j-" prompt a bit weird and would prefer something more like
>> "Jump to search: " to be used however the user enters the function.
>
> The intent was to make it act like a prefix keymap.  For example, if
> you hit "C-x", Emacs will show "C-x-" in the minibuffer until you hit
> the next key.  OTOH, Emacs isn't exactly a paragon of usability, so
> you're probably right and this should just use the provided prompt
> string regardless.
>
>> > +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)
>> 
>> The name "action" is slightly unfortunate when you use ACTION as the
>> third item of each element of action-map when describing it
>> above. However, no better name springs to mind. (Maybe "triple"?)
>
> Good catch.  Changed to "entry" (for an entry in the 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)
>> 
>> We don't normally seem to use autoload but instead use
>> declare-function. It might be worth being consistent (I am not very sure
>> of the pros and cons of autoload). May also be worth having it with the
>> other declare-functions to keep it clear how things are loaded.
>
> I used autoload because it won't bother even reading
> notmuch-jump.el(c) until the user hits "j" for the first time (and,
> hence, won't load it at all if they don't use jump).  This is easy
> with notmuch-jump because it's self-contained and has a single clear
> entry-point and no customizable variables.
>
> declare-function, on the other hand, still requires you to load the
> source containing the function (either eagerly, which is what notmuch
> usually does, or with an autoload).
>
> I think we should do *more* autoloading, though maybe it's not
> practical for the bulk of notmuch-emacs.
>
> I'm happy to move the autoload call to somewhere else, though
> notmuch-lib doesn't actually have any declare-functions (since it's
> sort of a root of the dependency tree).  I could put it right below
> the requires, since that's where we usually put declare-functions.

I think having the autoload  at the top sounds good. With the changes
you suggest it gets my +1.

Best wishes

Mark


>
>> Best wishes
>> 
>> Mark
>> 
>> 
>> > +
>> >  ;; 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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 0/2] emacs: Shortcut keys to saved searches
  2014-07-14 16:02 [PATCH 0/2] emacs: Shortcut keys to saved searches Austin Clements
  2014-07-14 16:02 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
  2014-07-14 16:02 ` [PATCH 2/2] emacs: Expand default saved searches and add shortcut keys Austin Clements
@ 2014-07-15 14:06 ` Austin Clements
  2014-07-15 14:06   ` [PATCH v2 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
                     ` (2 more replies)
  2 siblings, 3 replies; 16+ messages in thread
From: Austin Clements @ 2014-07-15 14:06 UTC (permalink / raw)
  To: notmuch

This is version 2 of
id:1405353735-26244-1-git-send-email-amdragon@mit.edu and addresses
Mark's comments in id:87egxnd4aq.fsf@qmul.ac.uk.

The diff from v1 is below.

diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
index cb1ae10..9cb1e6a 100644
--- a/emacs/notmuch-jump.el
+++ b/emacs/notmuch-jump.el
@@ -53,20 +53,19 @@ (defun notmuch-jump-search ()
     (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."))))
+	(notmuch-jump action-map "Search: ")
+      (error "To use notmuch-jump, please customize shortcut keys in 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.
+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)
@@ -82,16 +81,9 @@ (defun notmuch-jump (action-map prompt)
 	  (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)))
+		  (propertize prompt '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
@@ -121,14 +113,14 @@ (defun notmuch-jump--format-actions (action-map)
 
   ;; Compute the maximum key description width
   (let ((key-width 1))
-    (dolist (action action-map)
+    (dolist (entry action-map)
       (setq key-width
 	    (max key-width
-		 (string-width (format-kbd-macro (first action))))))
+		 (string-width (format-kbd-macro (first entry))))))
     ;; Format each action
-    (mapcar (lambda (action)
-	      (let ((key (format-kbd-macro (first action)))
-		    (desc (second action)))
+    (mapcar (lambda (entry)
+	      (let ((key (format-kbd-macro (first entry)))
+		    (desc (second entry)))
 		(concat
 		 (propertize key 'face 'minibuffer-prompt)
 		 (make-string (- key-width (length key)) ? )
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 135422d..b338aaa 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -25,6 +25,9 @@
 (require 'mm-decode)
 (require 'cl)
 
+(autoload 'notmuch-jump-search "notmuch-jump"
+  "Jump to a saved search by shortcut key." t)
+
 (defvar notmuch-command "notmuch"
   "Command to run the notmuch binary.")
 
@@ -134,8 +137,6 @@ (defvar notmuch-common-keymap
     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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-07-15 14:06 ` [PATCH v2 0/2] emacs: Shortcut keys to saved searches Austin Clements
@ 2014-07-15 14:06   ` Austin Clements
  2014-08-04 23:00     ` 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 20:18   ` [PATCH v2 0/2] emacs: Shortcut keys to saved searches Mark Walters
  2 siblings, 1 reply; 16+ messages in thread
From: Austin Clements @ 2014-07-15 14:06 UTC (permalink / raw)
  To: notmuch

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  | 181 +++++++++++++++++++++++++++++++++++++++++++++++++
 emacs/notmuch-lib.el   |   4 ++
 4 files changed, 189 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..9cb1e6a
--- /dev/null
+++ b/emacs/notmuch-jump.el
@@ -0,0 +1,181 @@
+;; 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 "To use notmuch-jump, please customize shortcut keys in 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 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
+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)))
+	 (full-prompt
+	  (concat table "\n\n"
+		  (propertize prompt '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 (entry action-map)
+      (setq key-width
+	    (max key-width
+		 (string-width (format-kbd-macro (first entry))))))
+    ;; Format each action
+    (mapcar (lambda (entry)
+	      (let ((key (format-kbd-macro (first entry)))
+		    (desc (second entry)))
+		(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..b338aaa 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -25,6 +25,9 @@
 (require 'mm-decode)
 (require 'cl)
 
+(autoload 'notmuch-jump-search "notmuch-jump"
+  "Jump to a saved search by shortcut key." t)
+
 (defvar notmuch-command "notmuch"
   "Command to run the notmuch binary.")
 
@@ -130,6 +133,7 @@ (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.")
 
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 2/2] emacs: Expand default saved searches and add shortcut keys
  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-07-15 14:06   ` 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
  2 siblings, 1 reply; 16+ messages in thread
From: Austin Clements @ 2014-07-15 14:06 UTC (permalink / raw)
  To: notmuch

This should help new users off to a better start with the addition of
more sensible saved searches and default shortcut keys.  Most existing
users have probably customized this variable and won't be affected.
---
 emacs/notmuch-hello.el                              | 9 +++++++--
 test/emacs.expected-output/notmuch-hello            | 2 +-
 test/emacs.expected-output/notmuch-hello-long-names | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 061b27d..65d0627 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -93,8 +93,13 @@ (define-widget 'notmuch-saved-search-plist 'list
 				    (const :tag "Oldest-first" oldest-first)
 				    (const :tag "Newest-first" newest-first))))))
 
-(defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
-				    (:name "unread" :query "tag:unread"))
+(defcustom notmuch-saved-searches
+  `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
+    (:name "unread" :query "tag:unread" :key ,(kbd "u"))
+    (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
+    (:name "sent" :query "tag:sent" :key ,(kbd "t"))
+    (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
+    (:name "all mail" :query "*" :key ,(kbd "a")))
   "A list of saved searches to display.
 
 The saved search can be given in 3 forms. The preferred way is as
diff --git a/test/emacs.expected-output/notmuch-hello b/test/emacs.expected-output/notmuch-hello
index 2d69891..9ba4cfc 100644
--- a/test/emacs.expected-output/notmuch-hello
+++ b/test/emacs.expected-output/notmuch-hello
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
diff --git a/test/emacs.expected-output/notmuch-hello-long-names b/test/emacs.expected-output/notmuch-hello-long-names
index 486d0d9..1c8d6eb 100644
--- a/test/emacs.expected-output/notmuch-hello-long-names
+++ b/test/emacs.expected-output/notmuch-hello-long-names
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 0/2] emacs: Shortcut keys to saved searches
  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-07-15 14:06   ` [PATCH v2 2/2] emacs: Expand default saved searches and add shortcut keys Austin Clements
@ 2014-07-15 20:18   ` Mark Walters
  2 siblings, 0 replies; 16+ messages in thread
From: Mark Walters @ 2014-07-15 20:18 UTC (permalink / raw)
  To: Austin Clements, notmuch


On Tue, 15 Jul 2014, Austin Clements <amdragon@MIT.EDU> wrote:
> This is version 2 of
> id:1405353735-26244-1-git-send-email-amdragon@mit.edu and addresses
> Mark's comments in id:87egxnd4aq.fsf@qmul.ac.uk.
>
> The diff from v1 is below.

This version gets a +1 from me. I would have a slight preference for
making the prompt "Jump to search: " rather than "Search: " to emphasise
that you can't type an actual search, but am definitely happy with this
version.

Best wishes

Mark




>
> diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
> index cb1ae10..9cb1e6a 100644
> --- a/emacs/notmuch-jump.el
> +++ b/emacs/notmuch-jump.el
> @@ -53,20 +53,19 @@ (defun notmuch-jump-search ()
>      (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."))))
> +	(notmuch-jump action-map "Search: ")
> +      (error "To use notmuch-jump, please customize shortcut keys in 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.
> +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)
> @@ -82,16 +81,9 @@ (defun notmuch-jump (action-map prompt)
>  	  (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)))
> +		  (propertize prompt '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
> @@ -121,14 +113,14 @@ (defun notmuch-jump--format-actions (action-map)
>  
>    ;; Compute the maximum key description width
>    (let ((key-width 1))
> -    (dolist (action action-map)
> +    (dolist (entry action-map)
>        (setq key-width
>  	    (max key-width
> -		 (string-width (format-kbd-macro (first action))))))
> +		 (string-width (format-kbd-macro (first entry))))))
>      ;; Format each action
> -    (mapcar (lambda (action)
> -	      (let ((key (format-kbd-macro (first action)))
> -		    (desc (second action)))
> +    (mapcar (lambda (entry)
> +	      (let ((key (format-kbd-macro (first entry)))
> +		    (desc (second entry)))
>  		(concat
>  		 (propertize key 'face 'minibuffer-prompt)
>  		 (make-string (- key-width (length key)) ? )
> diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
> index 135422d..b338aaa 100644
> --- a/emacs/notmuch-lib.el
> +++ b/emacs/notmuch-lib.el
> @@ -25,6 +25,9 @@
>  (require 'mm-decode)
>  (require 'cl)
>  
> +(autoload 'notmuch-jump-search "notmuch-jump"
> +  "Jump to a saved search by shortcut key." t)
> +
>  (defvar notmuch-command "notmuch"
>    "Command to run the notmuch binary.")
>  
> @@ -134,8 +137,6 @@ (defvar notmuch-common-keymap
>      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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 2/2] emacs: Expand default saved searches and add shortcut keys
  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
  0 siblings, 0 replies; 16+ messages in thread
From: David Bremner @ 2014-07-15 22:06 UTC (permalink / raw)
  To: Austin Clements, notmuch

Austin Clements <amdragon@MIT.EDU> writes:

> This should help new users off to a better start with the addition of
> more sensible saved searches and default shortcut keys.  Most existing
> users have probably customized this variable and won't be affected.

I didn't have a chance to review the code, but I like the interface.

d

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  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
  0 siblings, 1 reply; 16+ messages in thread
From: David Bremner @ 2014-08-04 23:00 UTC (permalink / raw)
  To: Austin Clements, notmuch

Austin Clements <amdragon@MIT.EDU> writes:

> This introduces notmuch-jump, which is like a user-friendly,
> user-configurable global prefix map for saved searches.  This provi

Unfortunately this patch doesn't apply anymore.

> @@ -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 \

Why the extra \ ?

> +(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)))

Should this function maybe be somewhere more generic?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-08-04 23:00     ` David Bremner
@ 2014-08-05  1:44       ` Austin Clements
  2014-08-05  1:45         ` [PATCH v3 " Austin Clements
  0 siblings, 1 reply; 16+ messages in thread
From: Austin Clements @ 2014-08-05  1:44 UTC (permalink / raw)
  To: David Bremner; +Cc: notmuch

Quoth David Bremner on Aug 04 at  8:00 pm:
> Austin Clements <amdragon@MIT.EDU> writes:
> 
> > This introduces notmuch-jump, which is like a user-friendly,
> > user-configurable global prefix map for saved searches.  This provi
> 
> Unfortunately this patch doesn't apply anymore.

Drat.  Thwarted by my own other patch!

Rebased series coming shortly (the resolution wasn't anything
interesting).

> > @@ -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 \
> 
> Why the extra \ ?

It means we can add more lines to this list without having the modify
an existing line, which cleans up future diffs and simplifies future
rebasing and handling of merge conflicts.

> > +(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)))
> 
> Should this function maybe be somewhere more generic?

Good idea.  I've moved it to notmuch-lib.  The diff from v2 is below:

diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el
index 9cb1e6a..05bbce5 100644
--- a/emacs/notmuch-jump.el
+++ b/emacs/notmuch-jump.el
@@ -22,6 +22,7 @@
 
 (eval-when-compile (require 'cl))
 
+(require 'notmuch-lib)
 (require 'notmuch-hello)
 
 ;;;###autoload
@@ -90,7 +91,7 @@ (defun notmuch-jump (action-map prompt)
 	 ;; ourselves) from their labels, so disable the minibuffer's
 	 ;; own re-face-ing.
 	 (minibuffer-prompt-properties
-	  (notmuch-jump--plist-delete
+	  (notmuch-plist-delete
 	   (copy-sequence minibuffer-prompt-properties)
 	   'face))
 	 ;; Build the keymap with our bindings
@@ -165,15 +166,6 @@ (defun notmuch-jump--make-keymap (action-map)
 	   (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)
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index a23c85d..19269e3 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -476,6 +476,15 @@ (defun notmuch-remove-if-not (predicate list)
       (setq list (cdr list)))
     (nreverse out)))
 
+(defun notmuch-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)))
+
 (defun notmuch-split-content-type (content-type)
   "Split content/type into 'content' and 'type'"
   (split-string content-type "/"))

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  2014-08-05  1:44       ` Austin Clements
@ 2014-08-05  1:45         ` 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
  0 siblings, 2 replies; 16+ messages in thread
From: Austin Clements @ 2014-08-05  1:45 UTC (permalink / raw)
  To: notmuch

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  | 173 +++++++++++++++++++++++++++++++++++++++++++++++++
 emacs/notmuch-lib.el   |  13 ++++
 4 files changed, 190 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..05bbce5
--- /dev/null
+++ b/emacs/notmuch-jump.el
@@ -0,0 +1,173 @@
+;; 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-lib)
+(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 "To use notmuch-jump, please customize shortcut keys in 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 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
+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)))
+	 (full-prompt
+	  (concat table "\n\n"
+		  (propertize prompt '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-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 (entry action-map)
+      (setq key-width
+	    (max key-width
+		 (string-width (format-kbd-macro (first entry))))))
+    ;; Format each action
+    (mapcar (lambda (entry)
+	      (let ((key (format-kbd-macro (first entry)))
+		    (desc (second entry)))
+		(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))
+
+(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 c06baac..19269e3 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -25,6 +25,9 @@
 (require 'mm-decode)
 (require 'cl)
 
+(autoload 'notmuch-jump-search "notmuch-jump"
+  "Jump to a saved search by shortcut key." t)
+
 (defgroup notmuch nil
   "Notmuch mail reader for Emacs."
   :group 'mail)
@@ -138,6 +141,7 @@ (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.")
 
@@ -472,6 +476,15 @@ (defun notmuch-remove-if-not (predicate list)
       (setq list (cdr list)))
     (nreverse out)))
 
+(defun notmuch-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)))
+
 (defun notmuch-split-content-type (content-type)
   "Split content/type into 'content' and 'type'"
   (split-string content-type "/"))
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 2/2] emacs: Expand default saved searches and add shortcut keys
  2014-08-05  1:45         ` [PATCH v3 " Austin Clements
@ 2014-08-05  1:45           ` Austin Clements
  2014-08-05 11:20           ` [PATCH v3 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches David Bremner
  1 sibling, 0 replies; 16+ messages in thread
From: Austin Clements @ 2014-08-05  1:45 UTC (permalink / raw)
  To: notmuch

This should help new users off to a better start with the addition of
more sensible saved searches and default shortcut keys.  Most existing
users have probably customized this variable and won't be affected.
---
 emacs/notmuch-hello.el                              | 9 +++++++--
 test/emacs.expected-output/notmuch-hello            | 2 +-
 test/emacs.expected-output/notmuch-hello-long-names | 2 +-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 061b27d..65d0627 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -93,8 +93,13 @@ (define-widget 'notmuch-saved-search-plist 'list
 				    (const :tag "Oldest-first" oldest-first)
 				    (const :tag "Newest-first" newest-first))))))
 
-(defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
-				    (:name "unread" :query "tag:unread"))
+(defcustom notmuch-saved-searches
+  `((:name "inbox" :query "tag:inbox" :key ,(kbd "i"))
+    (:name "unread" :query "tag:unread" :key ,(kbd "u"))
+    (:name "flagged" :query "tag:flagged" :key ,(kbd "f"))
+    (:name "sent" :query "tag:sent" :key ,(kbd "t"))
+    (:name "drafts" :query "tag:draft" :key ,(kbd "d"))
+    (:name "all mail" :query "*" :key ,(kbd "a")))
   "A list of saved searches to display.
 
 The saved search can be given in 3 forms. The preferred way is as
diff --git a/test/emacs.expected-output/notmuch-hello b/test/emacs.expected-output/notmuch-hello
index 2d69891..9ba4cfc 100644
--- a/test/emacs.expected-output/notmuch-hello
+++ b/test/emacs.expected-output/notmuch-hello
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
diff --git a/test/emacs.expected-output/notmuch-hello-long-names b/test/emacs.expected-output/notmuch-hello-long-names
index 486d0d9..1c8d6eb 100644
--- a/test/emacs.expected-output/notmuch-hello-long-names
+++ b/test/emacs.expected-output/notmuch-hello-long-names
@@ -2,7 +2,7 @@
 
 Saved searches: [edit]
 
-	  52 inbox           52 unread
+	  52 inbox           52 unread          52 all mail
 
 Search:                                                                     .
 
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 1/2] emacs: Introduce notmuch-jump: shortcut keys to saved searches
  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           ` David Bremner
  1 sibling, 0 replies; 16+ messages in thread
From: David Bremner @ 2014-08-05 11:20 UTC (permalink / raw)
  To: Austin Clements, notmuch

Austin Clements <amdragon@MIT.EDU> writes:

> 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.
>

pushed. This deserves a NEWS patch and ideally a patch to
doc/notmuch-emacs.rst

d

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2014-08-05 11:20 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-14 16:02 [PATCH 0/2] emacs: Shortcut keys to saved searches Austin Clements
2014-07-14 16:02 ` [PATCH 1/2] emacs: Introduce notmuch-jump: shortcut " Austin Clements
2014-07-14 21:22   ` 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

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).