Browse Source

Fix #19. Handle cons cell for initial-input arg

Instead of just nil or a string, the "initial-input" argument to
"completing-read" can also be a cons cell whose car is a string, and
my code didn't account for that, leading to spurious errors. Now the
code extracts the initial input string from the car of such a cons
cell.
Ryan C. Thompson 12 years ago
parent
commit
35afc08adb
1 changed files with 18 additions and 12 deletions
  1. 18 12
      ido-ubiquitous.el

+ 18 - 12
ido-ubiquitous.el

@@ -192,20 +192,26 @@ be used as the value of `completing-read-function'."
                  hist def inherit-input-method)))))
 
 (defadvice ido-completing-read (around detect-replacing-cr activate)
-  ;; Determine whether this call to `ido-completing-read' was done
-  ;; through the ido-ubiquitous wrapper `completing-read-ido'.
+  "Detect whether this call was done through `completing-read-ido'."
   (let* ((ido-this-call-replaces-completing-read ido-next-call-replaces-completing-read)
          (ido-next-call-replaces-completing-read nil))
-    ;; Work around a bug in ido when both INITIAL-INPUT and DEF are provided
-    ;; More info: https://github.com/technomancy/ido-ubiquitous/issues/18
-    (when (and ido-this-call-replaces-completing-read
-               def initial-input
-               (not (string= initial-input "")))
-      ;; Both default and initial input were provided. So keep the
-      ;; initial input and preprocess the choices list to put the
-      ;; default at the head, then proceed with default = nil.
-      (setq choices (cons def (remove def choices))
-            def nil))
+    (when ido-this-call-replaces-completing-read
+      ;; More info: https://github.com/technomancy/ido-ubiquitous/issues/18
+      (let ((initial (cond ((null initial-input) "")
+                           ((stringp initial-input) initial-input)
+                           ((consp initial-input) (car initial-input))
+                           (t initial-input)))
+            (deflist (if (listp def)
+                         def
+                       (list def))))
+        (when (and deflist initial
+                   (stringp initial)
+                   (not (string= initial "")))
+          ;; Both default and initial input were provided. So keep the
+          ;; initial input and preprocess the choices list to put the
+          ;; default at the head, then proceed with default = nil.
+          (setq choices (delete-dups (append deflist (remove def choices)))
+                def nil))))
     ad-do-it))
 
 (defmacro ido-ubiquitous-disable-in (func)