context-sensitive completion using readline.set_completer

E

Edvard Majakari

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top