How to indent blocks when readline completion is on?

S

Steven D'Aprano

I have set up readline completion as described here:

http://docs.python.org/library/rlcompleter.html

Now how do I indent blocks in the interactive interpreter? If I press the
TAB key, the completer prompts me instead of indenting:
....
Display all 182 possibilities? (y or n)
....


Surely I'm not stuck with indenting by manually typing spaces?


I thought I could add a wrapper around the rlcompleter method, like this:
.... if not text or text.isspace():
.... return "\t"
.... else:
.... return completer(text, state)
....

Completion appears to work if I have something in the line to start with,
e.g. if I type "whi" TAB "Tr" TAB I get "while True", but if I press TAB
on an empty line (intending to get an actual tab character for
indentation), it plays merry havoc with my session. All keyboard input
appears to be dead, eventually I used Ctrl-Z to interrupt the process and
killed it from the shell.
 
C

Chris Angelico

I thought I could add a wrapper around the rlcompleter method, like this:

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

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?)
.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top