diff options
| author | Alexander Færøy | 2014-05-31 13:10:46 +0200 | 
|---|---|---|
| committer | Alexander Færøy | 2014-05-31 13:10:46 +0200 | 
| commit | 2d0759e6ca5767b48bcc85bf38c2c43d5f0b63b1 (patch) | |
| tree | 1c5e6d817c88e67b46e216a50e0aef5428bf63df /scripts/grep.pl | |
| parent | 2d080422d79d1fd49d6c5528593ccaaff9bfc583 (diff) | |
| download | scripts.irssi.org-2d0759e6ca5767b48bcc85bf38c2c43d5f0b63b1.tar.bz2 | |
Import scripts from scripts.irssi.org
Diffstat (limited to 'scripts/grep.pl')
| -rw-r--r-- | scripts/grep.pl | 82 | 
1 files changed, 82 insertions, 0 deletions
| diff --git a/scripts/grep.pl b/scripts/grep.pl new file mode 100644 index 0000000..976d867 --- /dev/null +++ b/scripts/grep.pl @@ -0,0 +1,82 @@ +# /GREP [-i] [-w] [-v] [-F] <perl-regexp> <command to run> +# +# -i: match case insensitive +# -w: only print matches that form whole words +# -v: Invert the sense of matching, to print non-matching lines. +# -F: match as a fixed string, not a regexp +# +# if you want /FGREP, do: /alias FGREP GREP -F + +use Irssi; +use strict; +use Text::ParseWords; +use vars qw($VERSION %IRSSI);  +$VERSION = "2.1"; +%IRSSI = ( +	authors	    => "Timo \'cras\' Sirainen, Wouter Coekaerts", +	contact	    => "tss\@iki.fi, wouter\@coekaerts.be",  +	name        => "grep", +	description => "/GREP [-i] [-w] [-v] [-F] <perl-regexp> <command to run>", +	license     => "Public Domain", +	url         => "http://wouter.coekaerts.be/irssi/", +	changed	    => "2008-01-13" +); + +my ($match, $v); + +sub sig_text { +	my ($dest, $text, $stripped_text) = @_; +	Irssi::signal_stop() if (($stripped_text =~ /$match/) == $v);	 +} + +sub cmd_grep { +	my ($data,$server,$item) = @_; +	my ($option,$cmd,$i,$w,$F); +	$v = 0; +	$F = 0; +   +	# split the arguments, keep quotes +	my (@args)  = "ewords(' ', 1, $data); + +	# search for options +	while ($args[0] =~ /^-/) { +		$option = shift(@args); +		if ($option eq '-i') {$i = 1;} +		elsif ($option eq '-v') {$v = 1;} +		elsif ($option eq '-w') {$w = 1;} +		elsif ($option eq '-F') {$F = 1;}	 +		else { +			Irssi::print("Unknown option: $option",MSGLEVEL_CLIENTERROR); +			return; +		} +	} + +	# match = first argument, but remove quotes +	($match) = "ewords(' ', 0, shift(@args)); +	# cmd = the rest (with quotes) +	$cmd = join(' ',@args); + +	# check if the regexp is valid +	eval("'' =~ /$match/"); +	if($@) { # there was an error +		chomp $@; +		Irssi::print($@,MSGLEVEL_CLIENTERROR); +		return; +	} +	 +	if ($F) { +		$match =~ s/(\(|\)|\[|\]|\{|\}|\\|\*|\.|\?|\|)/\\$1/g; +	} +	if ($w) { +		$match = '\b' . $match . '\b'; +	} +	if ($i) { +		$match = '(?i)' . $match; +	} + +	Irssi::signal_add_first('print text', 'sig_text'); +	Irssi::signal_emit('send command', $cmd, $server, $item); +	Irssi::signal_remove('print text', 'sig_text'); +} + +Irssi::command_bind('grep', 'cmd_grep'); | 
