#
# grep 1.0 -- adds [e]grep support to the client, neat
# Author --  wd@anduril.org White_Dragon Chip Norkus
# Any deviation from the original is Jeremy Nelson's fault.
#
# Contributed to the EPIC project by Phoengold, on Fri, 14 Apr 2000.
#
# Usage: /grep [-w #] <text>
#	 /egrep [-w #] <text>
# Performs a text search on the lastlog of the current/specified window
#
# At the most basic level, /grep searches the lastlog of your current window
# for the specified text.  with the -w option you can specify the window
# whose lastlog you want to use. A regular expression of any sort can be
# used, as well.
#

alias grep 
{
	^local win,exp,re,i,x,l,s.,l.

	if (![$0]) 
	{
		echo Usage: /grep [-w #] <text>
		return
	}
        
	@ win = 1
        
        if ([$0] == [-w]) {
                if (![$2]) {
                        return
                }
                @ win = [$1]
                @ exp = [$2-]
        }{
                @ exp = [$*]
        }
        
        if (!winlevel($win)) {
		assign -win
        }
        
        @ re = regcomp($exp)
        
        ### grep here, and save the lines
        @ i = getset(LASTLOG)
        @ x = 0
	while (i) 
	{
		l = line($i $win)
                if (!regexec($re $l))
		{
                        @ s[$x] = l
                        @ l[$x] = i
                        @ x++
                }
                @ i--
        }
        @ regfree($re)
        
	echo ------------------ Results of grep: ----------------------
	@ i = 0
        while (i < x) {
                xecho -nolog $[4]l[$i]: $s[$i]
                @ i++
        }
	echo ------------------------ End -----------------------------
}

#WhiteDragon'Y2K
