aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 15:35:57 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 15:35:57 +0200
commit1029436abacd6715a10e920f318ca0764a5e21bc (patch)
tree29bf36498555aed1e74f3e742f160dd5703f6985
parentc66633ff31d50caf89a24055063c85c71af17605 (diff)
downloaddwim-1029436abacd6715a10e920f318ca0764a5e21bc.tar.gz
Fix missing dirname function, handle 'manpage.section'
-rwxr-xr-xdwim56
1 files changed, 35 insertions, 21 deletions
diff --git a/dwim b/dwim
index 1548a50..7d3afcd 100755
--- a/dwim
+++ b/dwim
@@ -11,70 +11,77 @@ 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;
-my @LAUNCHER = split /\s/, env OPENER => "urxvtc -e";
+my @OPENER = split /\s/, env OPENER => "xterm -e";
my @EDITOR = split /\s/, env EDITOR => "vi";
my @MAILER = split /\s/, env MAILER => "mutt";
-my @BROWSER = split /\s/, env BROWSER => "firefox";
-my @FILE_BROWSER = split /\s/, env FILE_BROWSER => "ranger";
+my @BROWSER = split /\s/, env BROWSER => "w3m -title";
+my @FILE_BROWSER = split /\s/, env FILE_BROWSER => "noice";
my @MAILDIR_VIEWER = split /\s/, env MAILDIR_VIEWER => "mutt -f";
my $MAILROOT = env MAILROOT => "/home/john/mail/";
our $handler;
for ($phrase) {
- if (/^(https?:\/?\/?[^\s]+)$/) {
+ if (/^(https?:\/?\/?\S+)$/) {
handle "web address";
run @BROWSER, "$1"
}
- if (/^(mailto:[^\s]+)$/ or /^([^\s]+@.+\.\w+)$/) {
+ if (/^(mailto:\S+)$/ or /^(\S+@.+\.\w+)$/) {
handle "e-mail address";
run @MAILER, "$1"
}
- if (/^(.+):(\d+).*?$/) {
+ if (/^(.+?):(\d+).*?$/) {
handle "file:line (like grep -n)";
my $p = path $1;
- run @LAUNCHER, @EDITOR, "-c", ":$2", "$p"
+ run @OPENER, @EDITOR, "-c", ":$2", "$p"
}
if (/^(.+) line (\d+)\.?$/) {
handle "FILE line LINE (like perl)";
my $p = path $1;
- run @LAUNCHER, @EDITOR, "-c", ":$2", "$p"
+ run @OPENER, @EDITOR, "-c", ":$2", "$p"
}
if (/^(.+):(.+)$/) {
handle "file:query (like grep)";
my $p = path $1;
- run @LAUNCHER, @EDITOR, "-c", "/$2", "$p" if -e $p;
+ run @OPENER, @EDITOR, "-c", "/$2", "$p" if -e $p;
fail "file not found" if $o{d};
# otherwise fall through
}
- if (/^([^\s]+)\(([^\s]+)\)[,.]?/) {
+ if (/^(\S+)\((\S+)\)[,.]?/) {
handle "manpage(section)";
- run @LAUNCHER, "man", "$2", "$1";
+ run @OPENER, "man", $2, $1;
}
- if (/^<([^\s]+)>$/) {
+ if (/^(\S+)\.([1-9])/) {
+ handle "manpage.section";
+ my $p = path "$1.$2";
+ run @OPENER, "man", $p if -e $p;
+ run @OPENER, "man", $2, $1;
+ }
+
+ if (/^<(\S+)>$/) {
handle "<header.h>";
- run @LAUNCHER, @EDITOR, "/usr/include/$1" if -e "/usr/include/$1";
- run @LAUNCHER, @EDITOR, "/usr/local/include/$1" if -e "/usr/local/include/$1";
+ run @OPENER, @EDITOR, "/usr/include/$1" if -e "/usr/include/$1";
+ run @OPENER, @EDITOR, "/usr/local/include/$1" if -e "/usr/local/include/$1";
fail "header file not found";
}
- if (/^([^\s]+-[\d.]+_\d+)$/) {
+ if (/^(\S+-[\d.]+_\d+)$/) {
handle "xbps package";
- run @LAUNCHER, "in-shell", "sudo", "xbps-install", "-S", $1;
+ run @OPENER, "in-shell", "sudo", "xbps-install", "-S", $1;
}
- if (/^([^\s]+)$/) {
+ if (/^(\S+)$/) {
handle "maildir / directory / file";
my $p = path $1;
- 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
+ run @OPENER, @MAILDIR_VIEWER, "$p" if $p =~ /^$MAILROOT/; # maildir
+ run @OPENER, @FILE_BROWSER, "$p" if -d $p; # directory
+ run @OPENER, @EDITOR, "$p" if -e $p; # file
fail "file not found" if $o{d};
# otherwise fall through
}
@@ -89,7 +96,8 @@ sub path {
return $n if $n =~ /^\// or $n =~ /^~/;
my $d = `xtitle`;
chomp $d;
- die "couldn't retrieve current directory\n" if ! -d $d and ! -d ($d = dirname $d);
+ die "couldn't retrieve current directory\n"
+ if ! -d $d and ! -d ($d = dirname($d));
return "$d/$n";
}
@@ -120,6 +128,12 @@ sub run {
exec @_;
}
+sub dirname {
+ my $path = shift;
+ $path =~ s,/[^/]+,,;
+ return $path;
+}
+
# parse ARGV and return list of positionals and hash of option values
sub arguments {
my $usage = shift; # usage string