diff options
-rwxr-xr-x | dwim | 63 |
1 files changed, 31 insertions, 32 deletions
@@ -7,6 +7,16 @@ use warnings; use strict; use subs qw/arguments dir env fail handle path run/; +my @OPENER = ('xtopen'); +my @EDITOR = (@OPENER, 'vi'); +my @PDF_VIEWER = ('xpdf'); +my @MAN_VIEWER = (@OPENER, 'man'); +my @MAILER = (@OPENER, 'mutt'); +my @BROWSER = (@OPENER, 'w3m -title'); +my @FILE_BROWSER = ('rox'); +my @MAILDIR_VIEWER = (@OPENER, 'mutt -f'); +my $MAILROOT = env MAILROOT => '/home/john/mail/'; + my ($DEBUG, $handler, $phrase); while ($_ = shift @ARGV) { @@ -21,16 +31,6 @@ while ($_ = shift @ARGV) { $phrase = `xsel -o` if not defined $phrase; -my @OPENER = ('xtopen'); -my @EDITOR = (@OPENER, 'vi'); -my @PDF_VIEWER = ('xpdf'); -my @MAN_VIEWER = (@OPENER, 'man'); -my @MAILER = (@OPENER, 'mutt'); -my @BROWSER = (@OPENER, 'w3m -title'); -my @FILE_BROWSER = ('rox'); -my @MAILDIR_VIEWER = (@OPENER, 'mutt -f'); -my $MAILROOT = env MAILROOT => '/home/john/mail/'; - for ($phrase) { if (/^(https?:\/?\/?\S+)$/) { handle 'web address'; @@ -134,17 +134,12 @@ sub dir { die "couldn't retrieve current directory: xtitle not installed\n"; } -# path -- return absolute path to file (and chdir to its directory) -sub path { - my $n = shift; - $n =~ s,^~([^/]+),/home/$1,; - $n =~ s,^~,/home/$ENV{USER},; - goto done if $n =~ /^\// or $n =~ /^~/; - $n = dir . "/$n"; -done: chdir dirname($n); - return $n; +# dirname -- return path without last part +sub dirname { + my $path = shift; + $path =~ s,/[^/]+$,,; + return $path; } - # env -- for (K => V), return environment variable K if defined, otherwise V sub env { my %h = @_; @@ -153,16 +148,27 @@ sub env { return $h{$k}; } -# handle -- print debug message when handler matches +# fail -- print debug message when matched handler fails +sub fail { + my $msg = shift; + print STDERR "$handler FAILED: $msg\n"; +} + +# handle -- save matched handler and print debug message sub handle { $handler = shift; print STDERR "$handler MATCHED\n" if $DEBUG; } -# fail -- print debug message when matched handler fails -sub fail { - my $msg = shift; - print STDERR "$handler FAILED: $msg\n"; +# path -- return absolute path to file (and chdir to its directory) +sub path { + my $n = shift; + $n =~ s,^~([^/]+),/home/$1,; + $n =~ s,^~,/home/$ENV{USER},; + goto done if $n =~ /^\// or $n =~ /^~/; + $n = dir . "/$n"; +done: chdir dirname($n); + return $n; } # run -- launch program and quit @@ -178,10 +184,3 @@ sub run { } exit; } - -# dirname -- return path without last part -sub dirname { - my $path = shift; - $path =~ s,/[^/]+$,,; - return $path; -} |