context-sensitive completion using readline.set_completer

Edvard Majakari edvard+news at
Tue Nov 18 09:28:49 CET 2003

Suppose you have a program containing commands and file name parameters.
A simple class providing completion could first read all the commands 
to list, and extend that list later using all file names in given
directory. This way a user could tab-complete (using
readline.parse_and_bind('tab: complete')) any command and any file. Eg. if
a command 'move' is implemented and there are files 'foo.txt' and
'bar.txt', the user could type

mo<tab> f<tab> b<tab>    

  expanding to

move foo.txt bar.txt

But the problem is that user is also able to type

"f<tab> m<tab>" getting "foo.txt move", which makes no sense.

The ideal completion would work as follows: when completing the first word
in command line, use set 1 (s1) as possible completion targets. When
completing anything else (second or any other word), use set 2 (s2) as
possible completion targets. Thus, having commands 'move', 'copy' and
'help' with previous two files the session would go as follows:

copy    move    help

c<tab> -> copy

foo.txt         bar.txt

f<tab> -> copy foo.txt

However I am not able to make it work, because I don't see how I could
store state information to my completer (it has no idea whether I'm
completing the first word or say, 4th word).

#!/usr/bin/perl -w
69,19,20,6,64,27,61,22,72};$_=join'',map{chr hex $h->{$_}}sort{$a<=>$b}
keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(join('',60,28,14,49),join'',
map{lc}($1,substr $2,4,1)),2,4)."\n"; print;

More information about the Python-list mailing list