emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 5268d10 06/12: Add an option to call the completion action


From: Oleh Krehel
Subject: [elpa] master 5268d10 06/12: Add an option to call the completion action without exiting
Date: Tue, 21 Apr 2015 12:09:21 +0000

branch: master
commit 5268d100fbb0ac28eee3d61cae040ed28566718c
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Add an option to call the completion action without exiting
    
    * ivy.el (ivy--persistent-action): New defvar. This is a lambda that the
      caller sets if the caller has a plan for persistent actions.
    (ivy-next-line-and-call): Add and bind to "C-M-n". Identical to "C-n",
    except calls `ivy--persistent-action' when it's not nil.
    Add and bind to "C-M-p". Identical to "C-p",
    except calls `ivy--persistent-action' when it's not nil.
    (ivy-window): New defvar.
    (ivy-read): Store `ivy-window'.
    
    * counsel.el (counsel-git-grep): Use `ivy--persistent-action'.
    
    For `counsel-git-grep', as an example, it's possible to move to the
    matched place without exiting the completion with "C-M-n" and "C-M-p".
    This is a nice advantage, since it gives a full context to each one-line
    git grep match.
---
 counsel.el |   14 ++++++++------
 ivy.el     |   27 +++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/counsel.el b/counsel.el
index 6bbc17b..c1cc347 100644
--- a/counsel.el
+++ b/counsel.el
@@ -211,15 +211,17 @@
   (let* ((counsel--git-grep-count (counsel-git-grep-count ""))
          (ivy--dynamic-function (when (> counsel--git-grep-count 20000)
                                   'counsel-git-grep-function))
-         (default-directory (locate-dominating-file
-                             default-directory ".git"))
+         (git-dir (locate-dominating-file
+                   default-directory ".git"))
+         (ivy--persistent-action (lambda (x)
+                                   (setq lst (split-string x ":"))
+                                   (find-file (expand-file-name (car lst) 
git-dir))
+                                   (goto-char (point-min))
+                                   (forward-line (1- (string-to-number (cadr 
lst))))))
          (val (ivy-read "pattern: " 'counsel-git-grep-function))
          lst)
     (when val
-      (setq lst (split-string val ":"))
-      (find-file (car lst))
-      (goto-char (point-min))
-      (forward-line (1- (string-to-number (cadr lst)))))))
+      (funcall ivy--persistent-action val))))
 
 (defun counsel-locate-function (str &rest _u)
   (if (< (length str) 3)
diff --git a/ivy.el b/ivy.el
index d25fbce..65b68d9 100644
--- a/ivy.el
+++ b/ivy.el
@@ -99,6 +99,8 @@ Only \"./\" and \"../\" apply here. They appear in reverse 
order."
     (define-key map (kbd "C-g") 'minibuffer-keyboard-quit)
     (define-key map (kbd "C-v") 'ivy-scroll-up-command)
     (define-key map (kbd "M-v") 'ivy-scroll-down-command)
+    (define-key map (kbd "C-M-n") 'ivy-next-line-and-call)
+    (define-key map (kbd "C-M-p") 'ivy-previous-line-and-call)
     map)
   "Keymap used in the minibuffer.")
 
@@ -120,6 +122,9 @@ of `history-length', which see.")
 (defvar ivy-text ""
   "Store the user's string as it is typed in.")
 
+(defvar ivy-window nil
+  "Store the window in which `ivy-read' was called.")
+
 (defvar ivy--current ""
   "Current candidate.")
 
@@ -133,6 +138,9 @@ Otherwise, store nil.")
 (defvar ivy--action nil
   "Store a function to call at the end of `ivy--read'.")
 
+(defvar ivy--persistent-action nil
+  "Store a function to call for current candidate without exiting.")
+
 (defvar ivy--all-candidates nil
   "Store the candidates passed to `ivy-read'.")
 
@@ -256,6 +264,24 @@ If the input is empty, select the previous history element 
instead."
     (ivy-previous-history-element 1))
   (ivy-previous-line arg))
 
+(defun ivy-next-line-and-call (&optional arg)
+  "Move cursor vertically down ARG candidates."
+  (interactive "p")
+  (ivy-next-line arg)
+  (ivy--exhibit)
+  (when ivy--persistent-action
+    (with-selected-window ivy-window
+      (funcall ivy--persistent-action ivy--current))))
+
+(defun ivy-previous-line-and-call (&optional arg)
+  "Move cursor vertically down ARG candidates."
+  (interactive "p")
+  (ivy-previous-line arg)
+  (ivy--exhibit)
+  (when ivy--persistent-action
+    (with-selected-window ivy-window
+      (funcall ivy--persistent-action ivy--current))))
+
 (defun ivy-previous-history-element (arg)
   "Forward to `previous-history-element' with ARG."
   (interactive "p")
@@ -367,6 +393,7 @@ UPDATE-FN is called each time the current candidate(s) is 
changed.
 
 When SORT is t, refer to `ivy-sort-functions-alist' for sorting."
   (setq ivy--directory nil)
+  (setq ivy-window (selected-window))
   (let (coll sort-fn)
     (cond ((eq collection 'Info-read-node-name-1)
            (if (equal Info-current-file "dir")



reply via email to

[Prev in Thread] Current Thread [Next in Thread]