Search code examples
pythonlinuxpython-2.7cygwinreadline

Deactivate readline autocompletion


I am trying to deactivate the readline autocompletion after I have used it. Therefore I want to write a decorator like this:

#!python
from contextlib import contextmanager
import readline

def main():
    with readline_autocompletion():
        raw_input('first: ')
    raw_input('second: ')

@contextmanager
def readline_autocompletion():
    readline.parse_and_bind('tab: complete')
    readline.set_completer_delims(' \t\n')
    readline.set_completer(None)

    yield

    # How to unload autocompletion without this hack??
    readline.set_completer(no_complete)

def no_complete(text,state):
    return None

if __name__ == '__main__':
    main()

How to deactivate the readline autocompletion again for the second raw_input()?

EDIT

I now managed to deactivate the completion by setting a completer that does only return None (see code above). Is this really the way to go in order to deactivate the autocompletion again? Feels like a hack.


Solution

  • After calling readline.parse_and_bind('tab: complete') the TAB is mapped on the function that performs completion. If custom completer is not set the default system file completion is used. There are better solutions than defining custom stub function to change that completion behavior.

    The TAB key can insert self (TAB symbol) as before 'tab: complete' binding:

    readline.parse_and_bind('tab: self-insert')
    

    For details take into account that the python readline module is based on the GNU Readline Library (in case of OS X it may be libedit). So, the documentation for the library can explain the function parse_and_bind. Here the most interesting part is "Readline Init File". The function parse_and_bind passes its input string as Readline Init File.

    It is possible to set values of various variables or bind different functions to keys.

    For example there is disable-completion variable that should work as 'tab: self-insert':

    If set to On, Readline will inhibit word completion. Completion characters will be inserted into the line as if they had been mapped to self-insert. The default is off.

    However, it is not completely true. It can be seen in the current library source code (readline-6.3). If the variable disable-completion is set to on the complete function (binded to tab) always inserts symbol of pressed key. However, self-insert inserts the symbol only in the insert mode. In the overwrite mode it overwrites symbol at cursor position. For example, bind to Ctrl-a keys the function toggle overwrite mode, set disable-completion on and keep complete binded to tab:

    readline.parse_and_bind('tab: complete')
    readline.parse_and_bind('set disable-completion on')
    readline.parse_and_bind('C-a: overwrite-mode')
    

    At first the tab key works as with 'tab: self-insert'. However, after pressing Ctrl-a the overwrite mode is enabled. Now the tab key still inserts tab symbol, but in case of self-insert the tab key overwrites symbols.