From 44f31f2c591f3ec1354665257cf8830b6df5548d Mon Sep 17 00:00:00 2001 From: John Ankarstrom Date: Fri, 2 Jul 2021 23:45:45 +0200 Subject: Handle man.section as file --- dwim | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/dwim b/dwim index 1b6b443..6353412 100755 --- a/dwim +++ b/dwim @@ -67,13 +67,6 @@ for ($phrase) { run @MAN_VIEWER, $2, $1; } - if (/^([-_A-Za-z]+)\.([1-9]):?$/) { - handle 'manual.section'; - my $p = path "$1.$2"; - run @MAN_VIEWER, $p if -e $p; - run @MAN_VIEWER, $2, $1; - } - if (/^([A-Za-z_][A-Za-z0-9_]*)\(/) { handle 'function(call'; my $dir = dir; @@ -117,10 +110,11 @@ for ($phrase) { if (/^(\S+)$/) { handle 'maildir / directory / file'; my $p = path $1; - run @MAILDIR_VIEWER, $p if $p =~ /^$MAILROOT/; # maildir - run @FILE_BROWSER, $p if -d $p; # directory - run @PDF_VIEWER, $p if -e $p and $p =~ /\.pdf$/; # pdf - run @EDITOR, $p if -e $p; # file + run @MAILDIR_VIEWER, $p if absp($p) =~ /^$MAILROOT/; + run @FILE_BROWSER, $p if -d absp($p); + run @MAN_VIEWER, absp($p) if -e absp($p) and $p =~ /\.[1-9]$/; + run @PDF_VIEWER, $p if -e absp($p) and $p =~ /\.pdf$/; + run @EDITOR, $p if -e absp($p); fail 'file not found' if $DEBUG; # FALLTHROUGH } @@ -130,6 +124,8 @@ for ($phrase) { # dir -- return directory of current window sub dir { + state $dir; + return $dir if defined $dir; for (`xtitle`) { chomp; s/.*\(([^(]+)\)$/$1/; @@ -137,7 +133,7 @@ sub dir { s,^~,/home/$ENV{USER},; die "couldn't retrieve current directory\n" if ! -d $_ and ! -d ($_ = dirname($_)); - return $_; + return $dir = $_; } die "couldn't retrieve current directory: xtitle not installed\n"; } @@ -169,15 +165,21 @@ sub handle { print STDERR "$handler MATCHED\n" if $DEBUG; } -# path -- return absolute path to file (and chdir to its directory) +# path -- return abbreviated 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; + 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 =~ s,^\Q$dir/\E,,; + return $path; +} + +# absp -- return absolute path to relative file +sub absp { + return $_[0] if $_[0] =~ m,^/,; + return dir . "/$_[0]"; } # run -- launch program and quit -- cgit v1.2.3