#------------------------------------------------------ # simplegrep.irc # version 1.0 # blue-elf (belf@null.net) 11/May/2005 #------------------------------------------------------ # Desc: # # This script adds two aliases: # /whereis # /grep # # Just two simple commands, and not as flexible as their original counterparts. #------------------------------------------------------ # Syntax: # # /whereis [-s] # Searches for the file where is in. In this context, is an alias. # If [-s] switch is specified, it means that the is a subroutine. # This only applies to custom aliases and subroutines (as opposed to internal aliases). # Example: # /whereis massdeop This will search all loaded scripts for the massdeop alias. # /whereis -s clone This will search for the subroutine named 'clone'. # # /grep <[-r] pattern> [path and filename] # This finds the wildcard in the given path or filename. # If [-r] is specified, it means that pattern is a regular expression. # If only the path is specified, it will search for the in all the files of that path. # If only the filename is specified, it will search for the in all the files in the default jIRCii directory. # If only the is specified, it will search for the pattern in all the files in the default jIRCii directory. # Example: # /grep *version* myscript.irc This will find the line where *version* is inside myscript.irc # /grep -r ^#\sversion\s\d$ This will find all the files in the jIRCii directory # that have lines that matches the given expression. # In this example, anything matching "# version X" where X is a number. #------------------------------------------------------ # Notes: # # Right now, it will search ALL the files, regardless of the filetype. # The results will always show the file and the line number (for both aliases). # I'm not very good with regular expressions :/ # # There seems to be a problem if you specify #?string* as the wildcard pattern in /grep. Bug? =) # # If you have suggestions/comments, email me. I won't necessarily reply, but I'll read your mail. #------------------------------------------------------ # /whereis [-s] alias whereis { local('$pre $string $file $regex @list $result'); if ($1 eq '-s') { $pre = "sub"; $string = $2; } else { $pre = "alias"; $string = $1; } if (!$string) { return; } @list = getPropertyList('script.files'); foreach $file (@list) { # Originally, I used this but /grep displays too much info because it greps all scripts. # $regex = "$pre" . '\s' . "$string" . '\b'; # call("/grep -r $regex $file"); whereis_scan($pre, $string, $file); } } sub whereis_scan { local('$line @file $x $reg'); $line = openf($3); @file = readAll($line); closef($line); $reg = '^' . $1 . '\s' . $2 . '\b.*'; $x = 1; foreach $line (@file) { if ($line ismatch $reg || "$1 $2 $+ \{" eq $line) { echo("Found $2 in: " . getFileName($3) . " \(Line number: $x $+ \)"); } $x++; } } # /grep <-r pattern> [[path]file] alias grep { local('$regex $path $file $string'); if ($1 eq '-r') { $regex = $2; if ($3 ne '') { $path = getFilePath($3-); $file = getFileName($3-); } } else if ($2 ne '') { $string = $1; if ($2 ne '') { $path = getFilePath($2-); $file = getFileName($2-); } } else if ($1) { $string = $1; $path = getCurrentDirectory(); } if (!-isDir $path) { $path = getCurrentDirectory(); } echo("Searching for:"); echo(" Pattern: " . iff($regex, $regex, $string)); echo(" Path : $path"); echo(" File : $file"); if ($regex ne '') { grep_allr($regex, $path, $file); } else { grep_alln($string, $path, $file); } echo("End of search"); } sub grep_allr { local('$f @ls'); $f = getFileProper($2, $3); if (!-isFile $f) { @ls = ls($2); foreach $f (@ls) { grep_filer($1, $f); } } else { grep_filer($1, $f); } } # grep_filer(pattern, file); sub grep_filer { local('$l @c $x'); $l = openf($2); @c = readAll($l); closef($l); $x = 1; foreach $l (@c) { if ($l ismatch $1) { echo(" Line number $x $+ :"); echo(" $2"); } $x++; } } sub grep_alln { local('$f @c $x'); $f = getFileProper($2, $3); if (!-isFile $f) { foreach $f (ls($2)) { grep_filen($1, $f); } } else { grep_filen($1, $f); } } # grep_filen(pattern, file); sub grep_filen { local('$l @c $x'); $l = openf($2); @c = readAll($l); closef($l); $x = 1; foreach $l (@c) { if ($1 isin $l || $1 iswm $l) { echo(" Line number $x $+ :"); echo(" $2"); } $x++; } } # EOF