Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Auto expand when there is only a single candidate #2

Closed
davels opened this issue Oct 25, 2009 · 5 comments
Closed
Labels
invalid This doesn't seem right

Comments

@davels
Copy link

davels commented Oct 25, 2009

Here is a patch that adds the option of automatically expanding the completion when there is only a single candidate. This is particularly nice of ac-auto-start is nil and completion is started from a key binding.

There is a new defun: ac-start-and-expand-single. It functions exactly like ac-start, but if the initial completion list contains only a single candidate, it is automatically expanded and completion ends.

There is also a var: ac-always-expand-single-p. If non-nil, a candidate is automatically selected whenever the candidate list is reduced down to a single item.

diff -u d\:/emacs/emacs/site-lisp/auto-complete.el.orig d\:/emacs/emacs/site-lisp/auto-complete.el
--- d:/emacs/emacs/site-lisp/auto-complete.el.orig  2009-10-10 07:37:11.943250000 -0700
+++ d:/emacs/emacs/site-lisp/auto-complete.el   2009-10-20 22:33:37.308970300 -0700
@@ -283,6 +283,10 @@
   :group 'auto-complete)
 (defvaralias 'ac-candidate-max 'ac-candidate-limit)

+(defcustom ac-always-expand-single-p nil
+  "Non-nil to automatically complete whenever a single candidate remains."
+  :group 'auto-complete)
+
 (defcustom ac-modes
   '(emacs-lisp-mode lisp-interaction-mode
                     c-mode cc-mode c++-mode java-mode
@@ -732,7 +736,12 @@
                                    'ac-selection-face))
       (setq ac-selection selection))))

-(defun ac-start ()
+(defun ac-start-and-expand-single ()
+  "Start completion, automatically expanding if there is only a single candiate."
+  (interactive)
+  (ac-start t))
+
+(defun ac-start (&optional expand-single)
   "Start completion."
   (interactive)
   (let* ((point (save-excursion (funcall ac-prefix-function)))
@@ -762,17 +771,25 @@
                                (funcall ac-candidate-function))))
         (if ac-candidate-filter-function
             (setq candidates (funcall ac-candidate-filter-function candidates)))
-        (setq width (let ((w '(0)) s)
-                      (dotimes (i ac-candidate-menu-height)
-                        (setq s (nth i candidates))
-                        (if (stringp s) (push (string-width s) w)))
-                      (apply 'max w)))
-        (if (or reposition
-                (null ac-menu)
-                (> width current-width)
-                (< width (- current-width 10)))
-            (ac-setup (* (ceiling (/ width 10.0)) 10)))
-        (ac-update-candidates candidates)))))
+        (if (and (or expand-single ac-always-expand-single-p)
+                 (= 1 (length candidates)))
+            (let* ((candidate (car candidates))
+                   (action (ac-get-candidate-action candidate)))
+              (ac-expand-string candidate)
+              (if action
+                  (funcall action))
+              (ac-abort))
+          (setq width (let ((w '(0)) s)
+                        (dotimes (i ac-candidate-menu-height)
+                          (setq s (nth i candidates))
+                          (if (stringp s) (push (string-width s) w)))
+                        (apply 'max w)))
+          (if (or reposition
+                  (null ac-menu)
+                  (> width current-width)
+                  (< width (- current-width 10)))
+              (ac-setup (* (ceiling (/ width 10.0)) 10)))
+          (ac-update-candidates candidates))))))

 (defun ac-adaptive-candidate-filter (candidates)
   "Filter candidates according to length and history (not yet)."

Diff finished.  Sat Oct 24 19:20:04 2009
@m2ym
Copy link
Contributor

m2ym commented Oct 26, 2009

Could you please tell me how do you use this feature?

@davels
Copy link
Author

davels commented Oct 27, 2009

I normally have ac-auto-start set to nil. If I bind TAB to ac-start-and-expand-single then hitting TAB will either show a completion menu if there is >1 candidate or immediately do the expansion if there is only a single candidate.

I like this because it works kind of like normal TAB completion: complete if you can, otherwise show me a list to choose from.

This came up because I've been working on using auto-complete to do tab completion in ipython shell mode.

The ac-always-expand-single-p is something I added just to try out when ac-auto-start is 't. It's a bit jarring at times and I'm pretty neutral on this part of the patch. I'd be willing to re-submit the patch without that part if you'd like.

@m2ym
Copy link
Contributor

m2ym commented Oct 29, 2009

It seems very good idea.

I'll implement it in more general way.

@monsanto
Copy link
Contributor

Did this ever get implemented in a more general way?

@m2ym
Copy link
Contributor

m2ym commented Sep 2, 2012

The current implementation includes this feature already. So close this issue.

@m2ym m2ym closed this as completed Sep 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants