1

I recently discovered auto-completetion in the python interperter here: https://docs.python.org/2.7/tutorial/interactive.html . This is fantastic for speeding up tests that I do in the interactive interpreter. There are two things that complete does that are both useful.

If I simply put C+f: complete in my .inputrc (or use readline without rlcompleter), when I press Ctl+f, I get completion of files in the directory that I started the interpreter from. When I load the modules readline and rlcompleter and add readline.parse_and_bind('C-n: complete') to the .pystartup file, it converts both the Ctl+n and Ctl+f to auto completing python objects.

I would like to do both, but am not sure how to keep rlcompleter from overriding the standard complete. Is there a way to start up two instances of readline, one that does and one that doesn't use rlcompleter?

Here is my .pystartup file

import atexit
import os
import readline
import rlcompleter #removing this does file completion.

readline.parse_and_bind('C-n: complete')

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
jeffpkamp
  • 2,732
  • 2
  • 27
  • 51

1 Answers1

0

How it works:

When you import rlcompleter, it install rlcompleter.Completer().complete as readline's completer: readline.set_completer(Completer().complete).

Without rlcompleter, completer is None, so the underlying GNU readline lib use rl_filename_completion_function by default.

Binding key and completion logics are implemented by GNU readline lib so there is nothing to do in Python about start up two instances of readline...


I don't find a way to call default rl_filename_completion_function in Python(It is possible in C extension), so I guess you have to duplicate the logic of rl_filename_completion_function in Python.

Which means you should inherit Completer and build your custom complete. But you still cannot split these two logics to C-n and C-f :(

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • I was afraid of that. I wonder if there is a way to write a function to unload rcompleter when the right keys are pressed, then reload it... – jeffpkamp Aug 28 '18 at 04:36