Customize rlcompleter

Eric Price eprice at tjhsst.edu
Mon Apr 7 12:28:29 EDT 2003


Gilles Lenfan wrote:
> I'd like to use "rlcompleter" with a custom vocabulary for a personal
> command line interpreter based on the "cmd" package. But the doc for
> rlcompleter is quite sparse, and I can't see how to tailor it to my own
> usage.

In rlcompleter.py, the Completer class has a method global_matches:

    def global_matches(self, text):
        """documentation"""
        import keyword
        matches = []
        n = len(text)
        for list in [keyword.kwlist,
                     __builtin__.__dict__.keys(),
                     __main__.__dict__.keys()]:
            for word in list:
                if word[:n] == text and word != "__builtins__":
                    matches.append(word)
        return matches

To edit the vocabulary for global matches, change the lists in the 
"for list in ..." loop to include the wanted vocabulary.

This will only change global matching, though.  Global matching is
called when there is no '.' in the string you are completing.  If you 
always want to complete using those lists, edit the complete method:

    def complete(self, text, state):
        """Docstring"""
        if state == 0:
            if "." in text:
                self.matches = self.attr_matches(text)
            else:
                self.matches = self.global_matches(text)
        [...]

and change it to

    def complete(self, text, state):
        """Docstring"""
        if state == 0:
            self.matches = self.global_matches(text)
        [...]

as well as editing global_matches as before.

Then, if tab-completion is enabled, the prompt will tab-complete to
any words in the lists in the global_matches function.

Hope this helps,
Eric




More information about the Python-list mailing list