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

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

[elpa] externals/ivy-hydra 657e6b4 062/395: ivy-test.el (ivy-inhibit-act


From: Basil L. Contovounesios
Subject: [elpa] externals/ivy-hydra 657e6b4 062/395: ivy-test.el (ivy-inhibit-action): Better behavior with an alist collection
Date: Thu, 25 Feb 2021 08:31:32 -0500 (EST)

branch: externals/ivy-hydra
commit 657e6b4d3f2d9783dc87389f460e1145ce26fa70
Author: Oleh Krehel <ohwoeowho@gmail.com>
Commit: Oleh Krehel <ohwoeowho@gmail.com>

    ivy-test.el (ivy-inhibit-action): Better behavior with an alist collection
    
    Make it easily to extract the full cons cell.
    
    * ivy.el (ivy-call): In case `ivy-inhibit-action' is bound, the return
      result is the application of action on the current item.
    (ivy-read): Simplify by returning the return result of `ivy-call'.
---
 ivy-test.el |  12 +++++++
 ivy.el      | 115 +++++++++++++++++++++++++++++++-----------------------------
 2 files changed, 72 insertions(+), 55 deletions(-)

diff --git a/ivy-test.el b/ivy-test.el
index 6054e3b..f5868a7 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -1436,6 +1436,18 @@ a buffer visiting a file."
                      (ivy--handle-directory "/sudo::"))
                    "/sudo::/tmp/")))
 
+(ert-deftest ivy-inhibit-action ()
+  (should (equal (ivy-with
+                  '(let ((ivy-inhibit-action #'identity))
+                    (ivy-read "pattern: " '(("a" . 1) ("b" . 2))))
+                  "C-m")
+                 '("a" . 1)))
+  (should (equal (ivy-with
+                  '(let ((ivy-inhibit-action #'cdr))
+                    (ivy-read "pattern: " '(("a" . 1) ("b" . 2))))
+                  "C-n C-m")
+                 2)))
+
 (defun ivy-test-run-tests ()
   (let ((test-sets
          '(
diff --git a/ivy.el b/ivy.el
index d885515..d8e4083 100644
--- a/ivy.el
+++ b/ivy.el
@@ -1419,54 +1419,63 @@ will be called for each element of this list.")
                                ivy-alt-done
                                ivy-dispatching-done))
     (setq ivy-current-prefix-arg current-prefix-arg))
-  (let ((action
-         (if (functionp ivy-inhibit-action)
-             ivy-inhibit-action
-           (and (not ivy-inhibit-action)
-                (ivy--get-action ivy-last)))))
-    (when action
-      (let* ((collection (ivy-state-collection ivy-last))
-             (current (ivy-state-current ivy-last))
-             (x (cond
-                  ;; Alist type.
-                  ((and (consp (car-safe collection))
-                        ;; Previously, the cdr of the selected
-                        ;; candidate would be returned.  Now, the
-                        ;; whole candidate is returned.
-                        (let ((idx (get-text-property 0 'idx current)))
-                          (if idx
-                              (nth idx collection)
-                            (assoc current collection)))))
-                  (ivy--directory
-                   (expand-file-name current ivy--directory))
-                  ((equal current "")
-                   ivy-text)
-                  (t
-                   current))))
-        (if (eq action #'identity)
-            (prog1 x
-              (ivy-recursive-restore))
-          (select-window (ivy--get-window ivy-last))
-          (set-buffer (ivy-state-buffer ivy-last))
-          (prog1 (unwind-protect
-                      (if ivy-marked-candidates
-                          (let ((prefix-len (length ivy-mark-prefix)))
-                            (setq ivy-marked-candidates
-                                  (mapcar (lambda (s) (substring s prefix-len))
-                                          ivy-marked-candidates))
-                            (if (ivy-state-multi-action ivy-last)
-                                (funcall
-                                 (ivy-state-multi-action ivy-last)
-                                 ivy-marked-candidates)
-                              (dolist (c ivy-marked-candidates)
-                                (let ((default-directory (ivy-state-directory 
ivy-last)))
-                                  (funcall action c)))))
-                        (funcall action x))
-                   (ivy-recursive-restore))
-            (unless (or (eq ivy-exit 'done)
-                        (minibuffer-window-active-p (selected-window))
-                        (null (active-minibuffer-window)))
-              (select-window (active-minibuffer-window)))))))))
+  (let* ((action
+          (if (functionp ivy-inhibit-action)
+              ivy-inhibit-action
+            (and (not ivy-inhibit-action)
+                 (ivy--get-action ivy-last))))
+         (collection (ivy-state-collection ivy-last))
+         (current (ivy-state-current ivy-last))
+         (x (cond
+              ;; Alist type.
+              ((and (consp (car-safe collection))
+                    ;; Previously, the cdr of the selected
+                    ;; candidate would be returned.  Now, the
+                    ;; whole candidate is returned.
+                    (let ((idx (get-text-property 0 'idx current)))
+                      (if idx
+                          (progn
+                            (ivy--remove-props current 'idx)
+                            (nth idx collection))
+                        (assoc current collection)))))
+              (ivy--directory
+               (expand-file-name current ivy--directory))
+              ((equal current "")
+               ivy-text)
+              (t
+               current)))
+         (res
+          (cond
+            ((null action)
+             current)
+            ((eq action #'identity)
+             (prog1 x
+               (ivy-recursive-restore)))
+            (t
+             (select-window (ivy--get-window ivy-last))
+             (set-buffer (ivy-state-buffer ivy-last))
+             (prog1 (unwind-protect
+                         (if ivy-marked-candidates
+                             (let ((prefix-len (length ivy-mark-prefix)))
+                               (setq ivy-marked-candidates
+                                     (mapcar (lambda (s) (substring s 
prefix-len))
+                                             ivy-marked-candidates))
+                               (if (ivy-state-multi-action ivy-last)
+                                   (funcall
+                                    (ivy-state-multi-action ivy-last)
+                                    ivy-marked-candidates)
+                                 (dolist (c ivy-marked-candidates)
+                                   (let ((default-directory 
(ivy-state-directory ivy-last)))
+                                     (funcall action c)))))
+                           (funcall action x))
+                      (ivy-recursive-restore))
+               (unless (or (eq ivy-exit 'done)
+                           (minibuffer-window-active-p (selected-window))
+                           (null (active-minibuffer-window)))
+                 (select-window (active-minibuffer-window))))))))
+    (if ivy-inhibit-action
+        res
+      current)))
 
 (defun ivy-call-and-recenter ()
   "Call action and recenter window according to the selected candidate."
@@ -2085,8 +2094,7 @@ customizations apply to the current completion session."
          (ivy--display-function
           (when (or ivy-recursive-last
                     (not (window-minibuffer-p)))
-            (ivy-alist-setting ivy-display-functions-alist caller)))
-         result)
+            (ivy-alist-setting ivy-display-functions-alist caller))))
     (setq update-fn (or update-fn (ivy-alist-setting ivy-update-fns-alist 
caller)))
     (setq unwind (or unwind (ivy-alist-setting ivy-unwind-fns-alist caller)))
     (setq ivy-last
@@ -2149,12 +2157,9 @@ customizations apply to the current completion session."
                 hist)
                (pop (symbol-value hist)))
              (when (eq ivy-exit 'done)
-               (ivy--update-history hist))
-             (setq result (ivy-state-current ivy-last))))
+               (ivy--update-history hist))))
       (ivy--cleanup))
-    (ivy-call)
-    (ivy--remove-props (ivy-state-current ivy-last) 'idx)
-    result))
+    (ivy-call)))
 
 (defun ivy--update-history (hist)
   (let ((item (if ivy--directory



reply via email to

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