Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using ido-completions in other packages

3 views
Skip to first unread message

Andrea Crotti

unread,
Jun 4, 2010, 5:35:57 AM6/4/10
to help-gn...@gnu.org
I was wondering if it would be possible to use the nice ido-mode
completion elsewhere.

For example senator-jump is really cool, but I always have to type the
complete name of the function/class, which is a bit annoying, while the
ido-completion like is much faster and nicer.

I've looked in the code but I don't see what I could substitute, maybe
substituting some functions or advising something else would do the
trick?

Thanks


Tassilo Horn

unread,
Jun 4, 2010, 6:57:18 AM6/4/10
to help-gn...@gnu.org
Andrea Crotti <andrea....@gmail.com> writes:

Hi Andrea,

> I've looked in the code but I don't see what I could substitute, maybe
> substituting some functions or advising something else would do the
> trick?

Basically, if you want to use ido-completion in some code, you would use
`ido-comleting-read' instead of `completing-read'. For example, in some
home-brewn mode I have something like that:

--8<---------------cut here---------------start------------->8---
(if (and (featurep 'ido) ido-mode)
;; ido is available and enabled, so use it.
(ido-completing-read "Command: " commands)
;; fallback to normal completion with the
;; most frequently used command as default.
(completing-read
(concat "Command (defaults to `"
(car commands) "'): ")
commands
nil t nil nil (car commands)))
--8<---------------cut here---------------end--------------->8---

You could try to make `completing-read' point to `ido-completing-read':

(fset 'completing-read 'ido-completing-read)

But that might error in some cases, cause `completing-read' has one
optional parameter more than `ido-completing-read'. You might want to
create a function `andrea-completing-read' with the exact signature of
`completing-read' which just delegates to `ido-completing-read' and
throws away the additional parameter, and then do

(fset 'completing-read 'andrea-completing-read)

In any case: This method is a hammer, so be warned. ;-)

Bye,
Tassilo


Štěpán Němec

unread,
Jun 4, 2010, 8:06:47 AM6/4/10
to Tassilo Horn, help-gn...@gnu.org, Andrea Crotti
Tassilo Horn <tas...@member.fsf.org> writes:

> Andrea Crotti <andrea....@gmail.com> writes:
>
> Hi Andrea,
>
>> I've looked in the code but I don't see what I could substitute, maybe
>> substituting some functions or advising something else would do the
>> trick?

What I've been successfully using for some time is this, based on the
dysfunctional (for me, anyway) tip from the Emacs Wiki:

<http://www.emacswiki.org/emacs/InteractivelyDoThings#toc12>

(defvar ido-replace-completing-read t
"If non-nil, use `ido-completing-read' instead of `completing-read' if possible.

Set it to nil using `let' in around-advice for functions where the
original `completing-read' is required. For example, if a function
foo absolutely must use the original `completing-read', define some
advice like this:

\(defadvice foo (around original-completing-read-only activate)
\(let (ido-replace-completing-read) ad-do-it))")

(defvar ido-replace-completing-read-ignore-commands nil
"List of commands to... you get the point.")

(setq ido-replace-completing-read-ignore-commands
'(describe-function describe-variable find-function find-variable
w3m-goto-url w3m-goto-url-new-session))

;; replace completing-read wherever possible, unless directed otherwise
(defadvice completing-read
(around use-ido-when-possible activate)
(if (or (not ido-replace-completing-read)
ido-cur-list ; Avoid infinite loop from ido calling this
(memq this-command ido-replace-completing-read-ignore-commands))
ad-do-it
(let ((allcomp (all-completions "" collection predicate)))
(if allcomp
(setq ad-return-value
(ido-completing-read prompt
allcomp
nil require-match initial-input hist def))
ad-do-it))))


HTH,

Štěpán

>
> Basically, if you want to use ido-completion in some code, you would use
> `ido-comleting-read' instead of `completing-read'. For example, in some
> home-brewn mode I have something like that:
>

> (if (and (featurep 'ido) ido-mode)
> ;; ido is available and enabled, so use it.
> (ido-completing-read "Command: " commands)
> ;; fallback to normal completion with the
> ;; most frequently used command as default.
> (completing-read
> (concat "Command (defaults to `"
> (car commands) "'): ")
> commands
> nil t nil nil (car commands)))
>

Andrea Crotti

unread,
Jun 4, 2010, 8:12:16 AM6/4/10
to help-gn...@gnu.org
Štěpán Němec <ste...@gmail.com> writes:

> Tassilo Horn <tas...@member.fsf.org> writes:
>

> What I've been successfully using for some time is this, based on the
> dysfunctional (for me, anyway) tip from the Emacs Wiki:
>
> <http://www.emacswiki.org/emacs/InteractivelyDoThings#toc12>

That looks very nice thanks a lot, but I tried for example with
senator-jump and it doesn't work.

I guess that maybe not all the functions will use internally
read-completion, is that correct?


Thierry Volpiatto

unread,
Jun 4, 2010, 9:14:47 AM6/4/10
to help-gn...@gnu.org
Andrea Crotti <andrea....@gmail.com> writes:

> Štěpán Němec <ste...@gmail.com> writes:
>
>> Tassilo Horn <tas...@member.fsf.org> writes:
>>
>
>> What I've been successfully using for some time is this, based on the
>> dysfunctional (for me, anyway) tip from the Emacs Wiki:
>>
>> <http://www.emacswiki.org/emacs/InteractivelyDoThings#toc12>
>
> That looks very nice thanks a lot, but I tried for example with
> senator-jump and it doesn't work.

I don't know what is senator-jump, but i guess this function use a
simple read-string. To have completion you need a *-completing-read with a
collection as arg.

I think a better approach to enable a ido-completing-read is what DVC
does:

,----
| (defun dvc-completing-read (&rest args)
| "Read a string in the minibuffer, with completion.
| Set `dvc-completing-read-function' to determine which function to use.
|
| See `completing-read' for a description of ARGS."
| ;; Initialize dvc-completing-read-function on the first invocation of dvc-completing-read
| ;; This allows to enable ido-mode after loading DVC
| (when (eq dvc-completing-read-function 'auto)
| (setq dvc-completing-read-function (if (and (boundp 'ido-mode) ido-mode)
| 'ido-completing-read
| 'completing-read)))
| (apply dvc-completing-read-function args))
`----


> I guess that maybe not all the functions will use internally
> read-completion, is that correct?

Yes like read-string.

FYI you have also an anything implementation of completing-read that is
available in anything-config.el, it is called anything-comp-read.

--
Thierry Volpiatto
Gpg key: http://pgp.mit.edu/


Andrea Crotti

unread,
Jun 4, 2010, 10:41:32 AM6/4/10
to help-gn...@gnu.org
Thierry Volpiatto <thierry....@gmail.com> writes:
>
> I don't know what is senator-jump, but i guess this function use a
> simple read-string. To have completion you need a *-completing-read with a
> collection as arg.

Here is the part of the code that I think is in charge for showing me
the list of possibilities.
It does use completing-read, but maybe using apply it doesn't work?
(Just wild guessing).

--8<---------------cut here---------------start------------->8---
(let*
...
(completing-read-args
(list (if (and context (car context))
(format "%s(default: %s) " prompt (car context))
prompt)
(setq senator-jump-completion-list
(senator-completion-list in-context))
nil
require-match
""
'semantic-read-symbol-history)))
(list
(apply #'completing-read
(if (and context (car context))
(append completing-read-args context)
completing-read-args))
in-context no-default)))
--8<---------------cut here---------------end--------------->8---

>
> ,----
> | (defun dvc-completing-read (&rest args)
> | "Read a string in the minibuffer, with completion.
> | Set `dvc-completing-read-function' to determine which function to use.
> |
> | See `completing-read' for a description of ARGS."
> | ;; Initialize dvc-completing-read-function on the first invocation of dvc-completing-read
> | ;; This allows to enable ido-mode after loading DVC
> | (when (eq dvc-completing-read-function 'auto)
> | (setq dvc-completing-read-function (if (and (boundp 'ido-mode) ido-mode)
> | 'ido-completing-read
> | 'completing-read)))
> | (apply dvc-completing-read-function args))
> `----
>
>

Thanks a lot this looks alos cleaner...


Stefan Monnier

unread,
Jun 4, 2010, 11:05:51 AM6/4/10
to
> I was wondering if it would be possible to use the nice ido-mode
> completion elsewhere.

I'm not sure exactly which parts of "ido-mode completion" interest you,
but note that Emacs-23's default completion is a lot more powerful and
flexible than before. Some of that is enabled by default (M-x r-e-b RET
will call report-emacs-bug and C-x C-f /u/s/d will get you to
/usr/share/doc), while other parts are not.

E.g. additionally to the partial-completion style enabled in Emacs-23,
you can use substring completion (C-x b oba TAB will select the "foobar"
buffer (and/or other buffers whose name have "oba" somewhere)).
Take a look at `completion-styles' (`substring' was not in Emacs-23,
but can be added easily).

You can also have a list of completion candidates displayed "on the fly"
in the minibuffer with M-x icomplete-mode (that was already available
in earlier Emacsen).

And you can cycle through completion candidates with the command
minibuffer-force-complete (not bound by default, but I personally bind
it to M-TAB in minibuffer-local-completion-map). Or in Emacs-Bzr you
can make minibuffer-complete cycle if there are few enough candidates
(using completion-cycle-threshold).

If you want other parts of ido-style completion included in Emacs's
default completion, feel free to send such requests via M-x
report-emacs-bug (ideally accompanied by a patch, of course).


Stefan

Thierry Volpiatto

unread,
Jun 4, 2010, 11:15:59 AM6/4/10
to help-gn...@gnu.org
Andrea Crotti <andrea....@gmail.com> writes:

> Thierry Volpiatto <thierry....@gmail.com> writes:
>>
>> I don't know what is senator-jump, but i guess this function use a
>> simple read-string. To have completion you need a *-completing-read with a
>> collection as arg.
>
> Here is the part of the code that I think is in charge for showing me
> the list of possibilities.
> It does use completing-read, but maybe using apply it doesn't work?
> (Just wild guessing).
>

> (let*
> ...
> (completing-read-args
> (list (if (and context (car context))
> (format "%s(default: %s) " prompt (car context))
> prompt)
> (setq senator-jump-completion-list
> (senator-completion-list in-context))
> nil
> require-match
> ""
> 'semantic-read-symbol-history)))
> (list
> (apply #'completing-read
> (if (and context (car context))
> (append completing-read-args context)
> completing-read-args))
> in-context no-default)))
>

Try that:

copy/paste the function senator-jump in your .emacs and replace
(apply #'completing-read
by
(apply #'ido-completing-read

That will give you ido completion, but if
(senator-completion-list in-context) return nothing (nil)
the problem come from here.

To know that, use C-u C-M x on the function senator-jump and use it,
and then use "n" to step throught the code.

Do you have completion with the original code?(i.e using
completing-read)

Andrea Crotti

unread,
Jun 5, 2010, 10:15:57 AM6/5/10
to help-gn...@gnu.org
Thierry Volpiatto <thierry....@gmail.com> writes:

> Andrea Crotti <andrea....@gmail.com> writes:
>
> Try that:
>
> copy/paste the function senator-jump in your .emacs and replace
> (apply #'completing-read
> by
> (apply #'ido-completing-read
>
> That will give you ido completion, but if
> (senator-completion-list in-context) return nothing (nil)
> the problem come from here.
>
> To know that, use C-u C-M x on the function senator-jump and use it,
> and then use "n" to step throught the code.
>
> Do you have completion with the original code?(i.e using
> completing-read)

Great
- copy pasting
- modifying
- evaluating
and it works, even if not perfect at all I have to say...
But then I tried both the approaches you've sended me here and they
don't work, so what could be the problem?


William Xu

unread,
Jun 8, 2010, 12:39:17 AM6/8/10
to help-gn...@gnu.org
Andrea Crotti <andrea....@gmail.com> writes:

I was wondering if it would be possible to use the nice ido-mode
completion elsewhere.

For example senator-jump is really cool, but I always have to type the


complete name of the function/class, which is a bit annoying, while the
ido-completion like is much faster and nicer.

I've looked in the code but I don't see what I could substitute, maybe


substituting some functions or advising something else would do the
trick?

I use ido-hacks.el. Very nice.

http://permalink.gmane.org/gmane.emacs.help/61442

--
William

http://xwl.appspot.com


Andrea Crotti

unread,
Aug 16, 2010, 5:27:27 AM8/16/10
to help-gn...@gnu.org
William Xu <willi...@gmail.com> writes:

I answer a bit late sorry, just tried now ido-hacks but still semantic
doesn't care at all about it...

I guess it just doesn't work with an easy defadvice then...
Any other idea is welcome.

Now I'm too used to have flex matching that is always a pain when I
don't have it...


Richard Riley

unread,
Aug 16, 2010, 7:07:10 AM8/16/10
to help-gn...@gnu.org
Andrea Crotti <andrea....@gmail.com> writes:

> William Xu <willi...@gmail.com> writes:
>
>> Andrea Crotti <andrea....@gmail.com> writes:
>>
>> I was wondering if it would be possible to use the nice ido-mode
>> completion elsewhere.
>>
>> For example senator-jump is really cool, but I always have to type the
>> complete name of the function/class, which is a bit annoying, while the
>> ido-completion like is much faster and nicer.
>>
>> I've looked in the code but I don't see what I could substitute, maybe
>> substituting some functions or advising something else would do the
>> trick?
>>
>> I use ido-hacks.el. Very nice.
>>
>> http://permalink.gmane.org/gmane.emacs.help/61442
>
> I answer a bit late sorry, just tried now ido-hacks but still semantic
> doesn't care at all about it...

ido-hacks doesnt include any code for semantic afaik. Since ido-mode is
now part of emacs it might make sense to ask on the cedet mailing list
about ido integration.

Andrea Crotti

unread,
Aug 17, 2010, 5:49:22 PM8/17/10
to help-gn...@gnu.org
Richard Riley <ril...@gmail.com> writes:

> ido-hacks doesnt include any code for semantic afaik. Since ido-mode is
> now part of emacs it might make sense to ask on the cedet mailing list
> about ido integration.
>

Sure, but it advice the functions that normally every package use for
getting completion.
Also looking at the code I think it should work, but it doesn't...

Anyway I got ido working on many other things with those ido-hacks,
which is still very nice.


0 new messages