context-sensitive completion using readline.set_completer

Edvard Majakari edvard+news at majakari.net
Tue Nov 18 03:28:49 EST 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:

<tab>
copy    move    help

c<tab> -> copy

<tab>
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
$h={23,69,28,'6e',2,64,3,76,7,20,13,61,8,'4d',24,73,10,'6a',12,'6b',21,68,14,
72,16,'2c',17,20,9,61,11,61,25,74,4,61,1,45,29,20,5,72,18,61,15,69,20,43,26,
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