How to indent blocks when readline completion is on?

Chris Angelico rosuav at gmail.com
Sun Nov 13 09:40:28 EST 2011


On Mon, Nov 14, 2011 at 12:30 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I thought I could add a wrapper around the rlcompleter method, like this:
>
>>>> import readline
>>>> import rlcompleter
>>>> readline.parse_and_bind("tab: complete")
>>>> completer = readline.get_completer()
>>>> def wrapped_completer(text, state):
> ...     if not text or text.isspace():
> ...         return "\t"
> ...     else:
> ...         return completer(text, state)
> ...
>>>> readline.set_completer(wrapped_completer)

Attempting to duplicate this failed in my Py3 (no readline module -
probably I didn't have the dev version when I built that Python), but
in Py2, I can see the same issue. The wrapped_completer function is
called repeatedly with successive values for 'state', and it needs to
return None at some point to indicate that there are no more
possibilities. (Why isn't it specced to simply return an iterable?)

>>> def wrapped_completer(text, state):
...     if not text or text.isspace():
...         if state: return None
...         return "\t"
...     else:
...         return completer(text, state)
...

This function appears (to me!) to do what you're looking for; it
allows me to enter tabs while still using tab completion. However, I
seem to have some slightly different tab-completion settings somewhere
(for instance, typing "whi" <TAB> produces "while" with no space after
it, so I need to type " Tr" not "Tr"), so this may not work on your
setup.

ChrisA



More information about the Python-list mailing list