aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstrom <john@ankarstrom.se>2021-07-04 00:48:19 +0200
committerJohn Ankarstrom <john@ankarstrom.se>2021-07-04 00:48:19 +0200
commit55b517b1d2321fff34b26659622147a3e38188d6 (patch)
tree23ab4b273004c481f2d58045ad31edb8e2f78c5f
parent949830adbcf5a2c526ce2ae84bcd5ca0adb803f0 (diff)
downloaddwim-55b517b1d2321fff34b26659622147a3e38188d6.tar.gz
Fix file path bug
-rwxr-xr-xdwim30
1 files changed, 20 insertions, 10 deletions
diff --git a/dwim b/dwim
index 1d86806..fee70ca 100755
--- a/dwim
+++ b/dwim
@@ -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