diff options
-rwxr-xr-x | dwim | 30 |
1 files changed, 20 insertions, 10 deletions
@@ -5,7 +5,7 @@ use v5.24; use warnings; use strict; -use subs qw/dir dirname env fail handle path run/; +use subs qw/cd dir dirname env fail handle path run/; my @OPENER = ('xtopen'); my @EDITOR = (@OPENER, '-putf8', 'vi'); @@ -17,7 +17,7 @@ my @FILE_BROWSER = ('rox'); my @MAILDIR_VIEWER = (@OPENER, 'mutt -f'); my $MAILROOT = env MAILROOT => '/home/john/mail/'; -my ($DEBUG, $handler, $phrase); +my ($DEBUG, $cwd, $handler, $phrase); while ($_ = shift @ARGV) { if (/^-d$/) { @@ -122,6 +122,18 @@ for ($phrase) { die "no handler matched by: $phrase\n" } +# absp -- return absolute path to relative file (after calling path) +sub absp { + return $_[0] if $_[0] =~ m,^/,; + return $cwd . "/$_[0]"; +} + +# cd -- chdir and set $cwd +sub cd { + chdir $_[0]; + $cwd = $_[0]; +} + # dir -- return directory of current window sub dir { state $dir; @@ -168,18 +180,16 @@ sub handle { # path -- return abbreviated path to file (and chdir to its directory) sub path { my ($path, $dir) = (shift, dir); + $path =~ s,^~([^/]+),/home/$1,; $path =~ s,^~,/home/$ENV{USER},; - $path = "$dir/$path" unless $path =~ m,^/, or $path =~ /^~/; - chdir dirname($path); + $path = "$dir/$path" if not $path =~ m,^/,; + + $dir = dirname($path); + cd $dir; $path =~ s,^\Q$dir/\E,,; - return $path; -} -# absp -- return absolute path to relative file -sub absp { - return $_[0] if $_[0] =~ m,^/,; - return dir . "/$_[0]"; + return $path; } # run -- launch program and quit |