]> git.notmuchmail.org Git - notmuch/blobdiff - emacs/notmuch-jump.el
emacs docstrings: consistent indentation, newlines, periods
[notmuch] / emacs / notmuch-jump.el
index 05bbce5eaeadb8d8866749059c90b13b9d76e867..84fe2b9c2435b2cc4ac67760313e637c150ee202 100644 (file)
@@ -1,4 +1,4 @@
-;; notmuch-jump.el --- User-friendly shortcut keys
+;;; notmuch-jump.el --- User-friendly shortcut keys
 ;;
 ;; Copyright © Austin Clements
 ;;
 ;; 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/>.
+;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
 ;;
 ;; Authors: Austin Clements <aclements@csail.mit.edu>
 ;;          David Edmondson <dme@dme.org>
 
-(eval-when-compile (require 'cl))
+;;; Code:
+
+(eval-when-compile
+  (require 'cl-lib)
+  (require 'pcase))
 
 (require 'notmuch-lib)
 (require 'notmuch-hello)
 
+(eval-and-compile
+  (unless (fboundp 'window-body-width)
+    ;; Compatibility for Emacs pre-24
+    (defalias 'window-body-width 'window-width)))
+
 ;;;###autoload
 (defun notmuch-jump-search ()
   "Jump to a saved search by shortcut key.
@@ -44,12 +53,18 @@ fast way to jump to a saved search from anywhere in Notmuch."
          (let ((name (plist-get saved-search :name))
                (query (plist-get saved-search :query))
                (oldest-first
-                (case (plist-get saved-search :sort-order)
+                (cl-case (plist-get saved-search :sort-order)
                   (newest-first nil)
                   (oldest-first t)
-                  (otherwise (default-value notmuch-search-oldest-first)))))
+                  (otherwise (default-value 'notmuch-search-oldest-first)))))
            (push (list key name
-                       `(lambda () (notmuch-search ',query ',oldest-first)))
+                       (cond
+                        ((eq (plist-get saved-search :search-type) 'tree)
+                         `(lambda () (notmuch-tree ',query)))
+                        ((eq (plist-get saved-search :search-type) 'unthreaded)
+                         `(lambda () (notmuch-unthreaded ',query)))
+                        (t
+                         `(lambda () (notmuch-search ',query ',oldest-first)))))
                  action-map)))))
     (setq action-map (nreverse action-map))
 
@@ -73,8 +88,7 @@ ACTION-MAP must be a list of triples of the form
 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.
-"
+not appear in the pop-up buffer."
 
   (let* ((items (notmuch-jump--format-actions action-map))
         ;; Format the table of bindings and the full prompt
@@ -95,7 +109,7 @@ not appear in the pop-up buffer.
           (copy-sequence minibuffer-prompt-properties)
           'face))
         ;; Build the keymap with our bindings
-        (minibuffer-map (notmuch-jump--make-keymap action-map))
+        (minibuffer-map (notmuch-jump--make-keymap action-map prompt))
         ;; The bindings save the the action in notmuch-jump--action
         (notmuch-jump--action nil))
     ;; Read the action
@@ -114,18 +128,16 @@ buffer."
 
   ;; Compute the maximum key description width
   (let ((key-width 1))
-    (dolist (entry action-map)
+    (pcase-dolist (`(,key ,desc) action-map)
       (setq key-width
            (max key-width
-                (string-width (format-kbd-macro (first entry))))))
+                (string-width (format-kbd-macro key)))))
     ;; 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)))
+    (mapcar (pcase-lambda (`(,key ,desc))
+             (setq key (format-kbd-macro key))
+             (concat (propertize key 'face 'minibuffer-prompt)
+                     (make-string (- key-width (length key)) ? )
+                     " " desc))
            action-map)))
 
 (defun notmuch-jump--insert-items (width items)
@@ -152,22 +164,48 @@ buffer."
     (set-keymap-parent map minibuffer-local-map)
     ;; Make this like a special-mode keymap, with no self-insert-command
     (suppress-keymap map)
+    (define-key map (kbd "DEL") 'exit-minibuffer)
     map)
   "Base keymap for notmuch-jump's minibuffer keymap.")
 
-(defun notmuch-jump--make-keymap (action-map)
+(defun notmuch-jump--make-keymap (action-map prompt)
   "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))))
+    (pcase-dolist (`(,key ,name ,fn) action-map)
+      (if (= (length key) 1)
+         (define-key map key
+           `(lambda () (interactive)
+              (setq notmuch-jump--action ',fn)
+              (exit-minibuffer)))))
+    ;; By doing this in two passes (and checking if we already have a
+    ;; binding) we avoid problems if the user specifies a binding which
+    ;; is a prefix of another binding.
+    (pcase-dolist (`(,key ,name ,fn) action-map)
+      (if (> (length key) 1)
+         (let* ((key (elt key 0))
+                (keystr (string key))
+                (new-prompt (concat prompt (format-kbd-macro keystr) " "))
+                (action-submap nil))
+           (unless (lookup-key map keystr)
+             (pcase-dolist (`(,k ,n ,f) action-map)
+               (when (= key (elt k 0))
+                 (push (list (substring k 1) n f) action-submap)))
+             ;; We deal with backspace specially
+             (push (list (kbd "DEL")
+                         "Backup"
+                         (apply-partially #'notmuch-jump action-map prompt))
+                   action-submap)
+             (setq action-submap (nreverse action-submap))
+             (define-key map keystr
+               `(lambda () (interactive)
+                  (setq notmuch-jump--action
+                        ',(apply-partially #'notmuch-jump action-submap new-prompt))
+                  (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)))))
+;;
+
+(provide 'notmuch-jump)
+
+;;; notmuch-jump.el ends here