# history-match
# This is basically an attempt to emulate the behavior of tcsh's ESC-p
# eg.. /partcom^Xp  looks for the completion to the command in the
#        command history
# and  /command partword^Xp  finds the match to the partial word in the
#      command history.. Try it twice as it may miss it the first time
# The other feature is that if there are several matches in the history
# you can hit ^Xp several times to move through the choices.. if you 
# modify the pattern or hit return it will reset the search to something
# new.. 

@ RCS.ircIId = [$$Header: /home/cvs/ircii/script/history-match,v 1.2 2001/08/12 15:44:17 mrg Exp $$]

bind meta2-p parse history.expn $L

# clearing out a null pattern is tough.. so I do it any time a line is
# sent
on #-input 765 * @ history.pat = [xxnomatchxx]

alias history.expn {
	if ([$0] != history.key)
	{
		# again, trying to get rid of a 'short' or null pattern
		@ history.pat = [xxnomatchxx]
		@ history.key = [$0]
	}
	if (history.nomatch == []) {@ history.nomatch = 1}
	if (!match($(history.pat)* $1)) {@ history.pat = [$1]}
	@ history.found = 0
	while (!history.found)
	{
		if ((history.search = [$(!$(0)!)]) != []) 
		{
			if (match($(history.pat)* $word(1 $history.search)))
			{
				parsekey erase_to_beg_of_line
				xtype -literal $history.search
				@ history.found = 1
				@ history.nomatch = 0
			}
		}
		{
			# we hit the top of the history.. drop out of the while loop
			@ history.found = 1
			@ history.nomatch = 1
		}
	}
}
