branch: master
commit 3bb12cd9803e73984f7e6c9241bd2096193cace5
Author: Ryan C. Thompson <[email protected]>
Commit: Ryan C. Thompson <[email protected]>

    Better handling of nil default to ivy-completing-read
    
    Now "ivy-completing-read" itself does nothing special when DEF is nil,
    but there is a new function
    "ivy-completing-read-with-empty-string-def" that treats a nil DEF as
    equivalent to the empty string for compatibility with
    "completing-read-default". This new function can be used in
    "ivy-completing-read-handlers-alist" to override ivy behavior for
    specific commands.
---
 ivy-test.el | 38 ++++++++++++++++++++++++++++++--------
 ivy.el      | 25 +++++++++++++++----------
 2 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/ivy-test.el b/ivy-test.el
index 5385e09..f1d6d5d 100644
--- a/ivy-test.el
+++ b/ivy-test.el
@@ -518,33 +518,55 @@
               "bl C-p C-M-j")
              "bl"))))
 
-(ert-deftest ivy-completing-read-default ()
-  (should
+(ert-deftest ivy-completing-read-def-handling ()
    ;; DEF in COLLECTION
+  (should
    (equal "b"
           (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil "b")
                     "RET")))
+  ;; Also make sure that choosing a non-default item works
+  (should
+   (equal "c"
+          (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil "b")
+                    "c RET")))
   ;; DEF not in COLLECTION
   (should
    (equal "d"
           (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil "d")
                     "RET")))
+  (should
+   (equal "c"
+          (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil "d")
+                    "c RET")))
   ;; DEF list, some in COLLECTION
   (should
    (equal "e"
           (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil '("e" "b"))
                     "RET")))
+  (should
+   (equal "c"
+          (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil '("e" "b"))
+                    "c RET")))
   ;; DEF nil
   (should
-   (equal ""
+   (equal "a"
           (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil nil)
                     "RET")))
-  ;; DEF nil, and `ivy-completing-read-default-is-empty-string' also nil
   (should
-   (equal "a"
-          (let ((ivy-completing-read-default-is-empty-string nil))
-            (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil nil)
-                      "RET")))))
+   (equal "c"
+          (ivy-with '(ivy-completing-read "Pick: " '("a" "b" "c") nil t nil 
nil nil)
+                    "c RET")))
+  ;; DEF nil, and called via `ivy-completing-read-with-empty-string-def'
+  (should
+   (equal ""
+          (ivy-with '(ivy-completing-read-with-empty-string-def
+                      "Pick: " '("a" "b" "c") nil t nil nil nil)
+                    "RET")))
+  (should
+   (equal "c"
+          (ivy-with '(ivy-completing-read-with-empty-string-def
+                      "Pick: " '("a" "b" "c") nil t nil nil nil)
+                      "c RET"))))
 
 (provide 'ivy-test)
 
diff --git a/ivy.el b/ivy.el
index 0640cef..521a9a5 100644
--- a/ivy.el
+++ b/ivy.el
@@ -273,14 +273,6 @@ Example:
 This is a global variable that is set by ivy functions for use in
 action functions.")
 
-(defvar ivy-completing-read-default-is-empty-string t
-  "If non-nil, emulate default arg handling of `completing-read-default'.
-
-Specifically, if `ivy-completing-read' is called with DEF nil, it
-will be treated as equivalent to specifying the empty string for
-DEF, since that is what `completing-read-default' does. If this
-is nil, then a nil DEF will really mean no default.")
-
 ;;* Keymap
 (require 'delsel)
 (defvar ivy-minibuffer-map
@@ -1864,8 +1856,6 @@ INHERIT-INPUT-METHOD is currently ignored."
           (setq initial-input (nth (1- (cdr history))
                                    (symbol-value (car history)))))
         (setq history (car history)))
-      (when (and (null def) ivy-completing-read-default-is-empty-string)
-        (setq def ""))
       (ivy-read (replace-regexp-in-string "%" "%%" prompt)
                 collection
                 :predicate predicate
@@ -1889,6 +1879,21 @@ INHERIT-INPUT-METHOD is currently ignored."
                               ((and collection (symbolp collection))
                                collection))))))
 
+(defun ivy-completing-read-with-empty-string-def
+    (prompt collection
+            &optional predicate require-match initial-input
+            history def inherit-input-method)
+  "Same as `ivy-completing-read' but with different handling of DEF.
+
+Specifically, if DEF is nil, it is treated the same as if DEF was
+the empty string. This mimics the behavior of
+`completing-read-refault'. This function can therefore be used in
+place of `icy-completing-read' for commands that rely on this
+behavior."
+  (ivy-completing-read
+   prompt collection predicate require-match initial-input
+   history (or def "") inherit-input-method))
+
 (defvar ivy-completion-beg nil
   "Completion bounds start.")
 

Reply via email to