From 9dfb6b89bd055f2710996092983c0fcc3a80f788 Mon Sep 17 00:00:00 2001 From: Tom Feist Date: Thu, 2 Feb 2012 12:36:44 +0000 Subject: added grep.pl to fixery prior to adding some in-app help. --- fixery/grep.pl | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 fixery/grep.pl diff --git a/fixery/grep.pl b/fixery/grep.pl new file mode 100644 index 0000000..0215bca --- /dev/null +++ b/fixery/grep.pl @@ -0,0 +1,82 @@ +# /GREP [-i] [-w] [-v] [-F] +# +# -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] ", + 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'); -- cgit v1.2.3