Explore subroutine plugin with jump back

I've written a small plugin to quickly explore a subroutine. Simply press Ctrl-Down arrow on a subroutine use in your code to jump to that sub. Just press Ctrl-Down again to jump back.

Installation:
Simply copy&paste the below code and save it in your optiperl plugin folder. Recommended name is SubroutineExplorer.pl

Known bugs:
Sometimes the sub under cursor didnt get identified correctly when indenting with tabs (often happens with big indents). Simple workaround: use perltidy (recommended anyway).

note to Xarka:
I love to be able to write plugins in my favorite language -> perl. OLE implementation was a great idea!

#Name: Subroutine Explorer
#Description: Perl script to jump into a subroutine under cursor and easy jump back using ctrl-down arrow (\optiperl\plug-ins\SubroutineExplorer.pl)
#Icon: %opti%Tools.icl,148
##Extensions: Startup

#You can change the hotkey at line 23
#Remove second # above to load at startup

use Win32::OLE;

sub Initialization {
$optiperl = Win32::OLE->new('OptiPerl.Application');
$oldposY = -1;
$oldposX = -1;
if ( defined $valid_plugin ) {
$plug_id = $_[0];
my $menu = $optiperl->CreateToolItem( $plug_id, "menu" );
$menu->SetOptions( "Menu", "", "", "", "" );

$btn = $optiperl->CreateToolItem( $plug_id, "button" );
$btn->SetOptions(
"Explore Sub", "Explores a subroutine under cursor",
"%opti%tools.icl,157", "Ctrl+Down",
"do_exploresub"
);
$menu->ToolLinks->Add( $btn, 0 );

$optiperl->ToolBarLinks($plug_id)->AssignLinks( $menu->ToolLinks );
$optiperl->UpdateToolBars($plug_id);
$optiperl->ToolBarVisible( $plug_id, 1 );
}
}

sub Finalization {
}

sub do_exploresub {
if ( $oldposY == -1 ) {
my $line = $optiperl->ActiveDocument->Lines( $optiperl->ActiveDocument->CursorPosY );
my $cursor = $optiperl->ActiveDocument->CursorPosX;
my $subname = _getSubAtCursor( $cursor, $line );
if ( defined $subname ) {
my $pos = _checkSubExists($subname);
if ($pos) {
$oldposY = $optiperl->ActiveDocument->CursorPosY;
$oldposX = $optiperl->ActiveDocument->CursorPosX;
$optiperl->ActiveDocument->{CursorPosY} = $pos;
$optiperl->ActiveDocument->{CursorPosX} = 0;
$optiperl->ActiveDocument->TempHightlightLine($pos);
}
}
} else {
$optiperl->ActiveDocument->{CursorPosY} = $oldposY;
$optiperl->ActiveDocument->{CursorPosX} = $oldposX;
$oldposY = -1;
}
}

sub _getSubAtCursor {
my $cursor = shift;
my $line = shift;

my $subname = "";
my $i = $cursor;
while ( $i >= 0 ) {
my $c = substr( $line, $i, 1 );
last if $c =~ m/[>()&\s]/;
$subname = $c . $subname;
$i--;
}
$i = $cursor + 1;
while ( $i <= length($line) ) {
my $c = substr( $line, $i, 1 );
last if $c =~ m/[\s;(]/;
$subname .= $c;
$i++;
}
if ( $subname =~ m/^\w+$/ ) { return $subname; }
else { return undef; }
}

sub _checkSubExists {
my $sub = shift;

my $doc = $optiperl->ActiveDocument;
my $i = 0;
while ( $i < $doc->LineCount ) {
return $i if $doc->lines($i) =~ /^\s*sub ($sub)/;
$i++;
}
return -1;
}

if ( !defined $valid_plugin ) {
Initialization;
}

Ingo Bartel

Uploaded here:

http://www.xarka.com/optiperl/plugins/

Only modification is the default shortcut, changed to Ctrl - . because Ctrl-Down is used to scroll the editor one line down without moving the cursor.

21.08.2003. 23:01

This article hasn't been commented yet.

Write a comment

:

:

:


7 + 4 =