IDLE and rlcompleter [Was: Re: making the Python case to my dept.]

Jesse Sweeney jesse at
Wed Nov 10 08:07:29 EST 1999

On 08 Nov 1999 23:00:35 +0000, Michael Hudson <mwh21 at> wrote:

>jesse at (Jesse Sweeney) writes:
>> Coincidentally, I had the same idea just a few days ago, and after a
>> bit of tinkering, I came up with two different IDLE extensions. I've
>> put the files on the web at
>> .
>I had to change <Meta-.> to <Meta-period> and then to <Alt-period>
>(the latter is probably a misconiguration at this end, but the first
>may not be).

Thanks, Michael, I changed this, and fixed a few other things that I thought
needed fixing, and uploaded the files again.

>I'm very used to some other readline conventions such as Meta-. being
>yank-last-arg and C-r being reverse-search-history and some
>others. Would these be easy to add to your extension? (I know next to
>nothing about Tkinter).

I doubt I know much more, but that's no excuse! I'm going on the IDLE source
code, extend.txt, the several extension examples that come with IDLE and
Fredrik's Introduction to Tkinter whenever I need it (I had to look up the staff
about text marks, for instance).

Now, yank-last-arg requires knowing about the history, and extensions --  as far
as I can figure -- don't get told about the history. So, that means this
requires editing the IDLE source, rather than making an extension. Here's what I
came up with (sorry, I don't have a working copy of patch here): 

1. Add a yank_last_arg method to the History class in

    def yank_last_arg(self, event):
        if len(self.history) == 0:
            return "break"
        item = self.history[-1]
        wordchars = string.letters + string.digits + "_" + "."
        i = len(item)
        while i > 0 and item[i-1] in wordchars:
            i = i-1
        last_arg = item[i:]
        self._put_source("insert", last_arg)
        return "break"

2. Bind the event in History.__init__ by adding a line of the form:

        text.bind("<<yank-last-arg>>", self.yank_last_arg)

3. Bind a keystroke of your choosing to the function by adding a line to like:

	'<<yank-last-arg>>': ['<Alt-period>'],

(4. Edit (I think) if you want a menu item as well.)

That should do it. I'll put my copy of on my website (the address
is above) to make it more straightforward.

reverse-search-history shouldn't be *that* much harder to implement, except that
I haven't actually worked out how to capture the shell input yet. Nevertheless,
notice that I'm not putting my hand up for the job. I've just noticed that the
IDLE todo file (TODO.txt) mentions this:

- more emacsisms:
    - incremental search?

So perhaps Guido will save us all the trouble, and leave me to study for my
exams ;).

>> Obviously, unlike "Expand Word" these only work in the Python shell,
>> not in the file-editing windows. (Now that I think about it, this
>> would probably be pretty useful in those editing windows, but that
>> would take some partial compilation of some sort I suppose, which is
>> way too much work for me.)
>I think I'd settle for the completions being selected from the words
>known to the Python shell, if that helps.

Well, by a happy coincidence this is how it works at the moment. So works quite nicely in that regard, but is pretty
useless, because it just prints the possible completions to stdout, which is the
Python shell if you've got one open, and I suppose you could kinda put the
windows next to each other, but 'urk' is all I can say to that. A CallTips-style
thing could be hacked together I suppose, if someone was dying for this
functionality, but I doubt it's worth the effort.

>Good work!


	Cheers, Jesse.

More information about the Python-list mailing list