From 47b775e21bfd158268eb8dcba066489484ac4900 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 4 Jun 2019 20:40:51 +0200 Subject: run() subroutine `run` is basically an alias for `exec`, except it prints the command being executed to STDERR when the -d option is specified. --- dwim | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/dwim b/dwim index e63b107..dd26e77 100755 --- a/dwim +++ b/dwim @@ -6,7 +6,7 @@ use v5.24; use warnings; use strict; use Path::ExpandTilde; -use subs qw/path env handle fail arguments/; +use subs qw/path env handle fail run arguments/; my ($phrase, %o) = arguments "usage: $0 parse\n", 1, d => 0; $phrase = `xsel -o` if not defined $phrase; @@ -24,30 +24,30 @@ our $handler; for ($phrase) { if (/^(https?:\/?\/?.+)$/) { handle "web address"; - exec "firefox", "$1" + run @BROWSER, "$1" } if (/^(mailto:.+)$/ or /^(.+@.+\.\w+)$/) { handle "e-mail address"; - exec @MAILER, "$1" + run @MAILER, "$1" } if (/^(.+):(\d+)(:.*?)?$/) { handle "{file}:{line} (like grep -n)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", ":$2", "$p" + run @LAUNCHER, @EDITOR, "-c", ":$2", "$p" } if (/^(.+) line (\d+)\.?$/) { handle "{file} line {line} (like perl)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", ":$2", "$p" + run @LAUNCHER, @EDITOR, "-c", ":$2", "$p" } if (/^(.+):(.+)$/) { handle "{file}:{query} (like grep)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", "/$2", "$p" if -e $p; + run @LAUNCHER, @EDITOR, "-c", "/$2", "$p" if -e $p; fail "file not found" if $o{d}; # otherwise fall through } @@ -55,8 +55,9 @@ for ($phrase) { if (/^([^\s]+)$/) { handle "maildir / directory / file"; my $p = path $1; - exec @LAUNCHER, @MAILDIR_VIEWER, "$p" if $p =~ /^$MAILROOT/; # maildir - exec @LAUNCHER, @EDITOR, "$p" if -e $p; #file + run @LAUNCHER, @MAILDIR_VIEWER, "$p" if $p =~ /^$MAILROOT/; # maildir + run @LAUNCHER, @FILE_BROWSER, "$p" if -d $p; # directory + run @LAUNCHER, @EDITOR, "$p" if -e $p; # file fail "file not found" if $o{d}; # otherwise fall through } @@ -93,6 +94,15 @@ sub fail { print STDERR "$handler FAILED: $msg\n"; } +sub run { + if ($o{d}) { + my @argv = @_; + s/(\s)/\\$1/g for @argv; # escape whitespace + print STDERR "@argv\n"; + } + exec @_; +} + # parse ARGV and return list of positionals and hash of option values sub arguments { my $usage = shift; # usage string -- cgit v1.2.3