-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_open.pl
executable file
·66 lines (62 loc) · 1.67 KB
/
short_open.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -w
# Aniruddha.A ([email protected])
# Given abbreviated name - expand and edit the file
# underscore/hyphen as seperator
#
# Usage: <script> <extension> <first-letters>
# Example: short_open.pl c mlp --> will open my_lisp_parser.c
#
# <script> <extension> <first-few-letters> [ <first-few-letters> ...]
# Example: short_open.pl h m list p --> will open my_list_parser.h
$editor = shift;
$ext = shift;
if ( scalar(@ARGV) == 1 ) {
# A few special cases - which I commonly edit
$in = shift;
if ((length($in) == 1) and ($in eq 'm')) {
system("$editor [Mm]akefile");
exit 0;
}
if ((length($in) == 1) and ($in eq 'R')) {
system("$editor README");
exit 0;
}
if ((length($in) == 1) and ($in eq 'w')) {
system("$editor wscript");
exit 0;
}
@p = split( //, $in ); # 1 arg - pick and split each char
}
else {
@p = (@ARGV); # multi arg - use them as is
}
$res = '^';
$res .= join( '[^_-]*[-_]', @p );
$res .= '[^_-]*[.]';
$res .= $ext . '$';
$re = qr/$res/;
foreach $a ( glob("*.$ext") ) {
if ( $a =~ m/$re/ ) {
print " [$a] \n";
system ("$editor $a");
exit 0;
}
}
# If it were not really a long name with underscore, then try
# a plain short name (assume 1 arg)
if ( scalar(@ARGV) > 1 ) {
exit 1;
}
$res = $in;
if ($res !~ /[.]$/ ) { # If user ended with a '.' lets use it
$res .= '.*[.]'; # match any part of the file name
}
$res .= $ext;
$re = qr/$res/;
foreach $a ( glob("*.$ext") ) {
if ( $a =~ m/$re/ ) {
print " [$a] \n";
system ("$editor $a");
exit 0;
}
}