Browse Source

Fix C-j behavior when require-match is enabled

Fixes #93. Also adds tests for the expected behavior.
Ryan C. Thompson 9 years ago
parent
commit
4306d4ca2a
2 changed files with 67 additions and 7 deletions
  1. 26 0
      ido-completing-read+.el
  2. 41 7
      tests/ido-ubiquitous-test.el

+ 26 - 0
ido-completing-read+.el

@@ -270,6 +270,32 @@ shouldn't matter.")
     ;; point.
     (setq ido-context-switch-command #'ido-fallback-command)))
 
+;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/93
+
+(defadvice ido-select-text (around fix-require-match-behavior activate)
+  "Fix ido behavior when `require-match' is non-nil.
+
+Standard ido will allow C-j to exit with an incomplete completion
+even when `require-match' is non-nil. Ordinary completion does
+not allow this. In ordinary completion, RET on an incomplete
+match is equivalent to TAB, and C-j selects the first match.
+Since RET in ido already selects the first match, this advice
+sets up C-j to be equivalent to TAB in the same situation."
+  (if (and
+       ;; Only if using ico-cr+
+       ido-cr+-enable-this-call
+       ;; Only if require-match is non-nil
+       ido-require-match
+       ;; Only if current text is non-empty
+       (not (string= ido-text ""))
+       ;; Only if current text is not a complete choice
+       (not (member ido-text ido-cur-list)))
+      (progn
+        (ido-cr+--debug-message
+         "Overriding C-j behavior for require-match: performing completion instead of exiting.")
+        (ido-complete))
+    ad-do-it))
+
 ;;; Debug mode
 
 ;; This is defined at the end so it goes at the bottom of the

+ 41 - 7
tests/ido-ubiquitous-test.el

@@ -30,6 +30,7 @@
 ;; This is a series of macros to facilitate the testing of completion
 ;; non-interactively by simulating input.
 
+(require 'ido-completing-read+)
 (require 'ido-ubiquitous)
 (require 'ert)
 (require 'cl-macs)
@@ -305,14 +306,47 @@ See `should-with-tag'."
       (test-ido-ubiquitous-expected-mode-on-functional-collection 'enable-old
         :override-enable-old-colfunc))))
 
-(ert-deftest ido-ubiquitous-require-match ()
-  "Test whether require-match works.
-
-(Require match seems to be broken in ido at the moment)"
-  :expected-result :failed
+(ert-deftest ido-cr+-require-match ()
+  "Test whether require-match works."
+  (should-error
+   (with-simulated-input "b C-j C-j C-j"
+     (ido-completing-read+
+      "Prompt: "
+      '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t)))
+  (should-error
+   (with-simulated-input "b C-j b C-j C-j"
+     (ido-completing-read+
+      "Prompt: "
+      '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t)))
+  (should
+   (string=
+    "blueberry"
+    (with-simulated-input "b C-j b C-j e C-j C-j"
+      (ido-completing-read+
+       "Prompt: "
+       '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t))))
   (should-error
-   (with-simulated-input "b C-j"
-     (ido-completing-read "Prompt: " '("blue" "brown" "yellow" "green") nil t))))
+   (with-simulated-input "b l u e g C-j"
+     (ido-completing-read+
+      "Prompt: "
+      '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t)))
+  (should
+   (string=
+    "bluegrass"
+    (with-simulated-input "b l u e g C-j C-j"
+      (ido-completing-read+
+       "Prompt: "
+       '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t))))
+  ;; Finally, test for the expected wrong behavior without ido-cr+. If
+  ;; ido.el ever fixes this bug, it will cause this test to fail as a
+  ;; signal that the workaround can be phased out.
+  (should
+   (string=
+    "b"
+    (with-simulated-input "b C-j"
+     (ido-completing-read
+      "Prompt: "
+      '("bluebird" "blues" "bluegrass" "blueberry" "yellow ""green") nil t)))))
 
 ;; Functions to define overrides on for testing
 (defun idu-no-override-testfunc ()