diff options
author | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-06-06 11:08:43 +0200 |
---|---|---|
committer | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-06-06 11:09:41 +0200 |
commit | 29fdf68905dc8cbfc4f18e0cf508117a5839d2dc (patch) | |
tree | ab7f4837cfa22d4d03187be6e470e8a0f78602ae | |
parent | 1029436abacd6715a10e920f318ca0764a5e21bc (diff) | |
download | dwim-29fdf68905dc8cbfc4f18e0cf508117a5839d2dc.tar.gz |
Dynamically search include directories for header files
-rwxr-xr-x | dwim | 47 |
1 files changed, 29 insertions, 18 deletions
@@ -11,13 +11,15 @@ 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 @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 => "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/"; +my @OPENER = ("xtopen"); +my @EDITOR = (@OPENER, "vi"); +my @PDF_VIEWER = ("xpdf"); +my @MAN_VIEWER = (@OPENER, "man"); +my @MAILER = (@OPENER, "mutt"); +my @BROWSER = (@OPENER, "w3m -title"); +my @FILE_BROWSER = (@OPENER, "noice"); +my @MAILDIR_VIEWER = (@OPENER, "mutt -f"); +my $MAILROOT = env MAILROOT => "/home/john/mail/"; our $handler; @@ -35,39 +37,47 @@ for ($phrase) { if (/^(.+?):(\d+).*?$/) { handle "file:line (like grep -n)"; my $p = path $1; - run @OPENER, @EDITOR, "-c", ":$2", "$p" + run @EDITOR, "+$2", "$p" } if (/^(.+) line (\d+)\.?$/) { handle "FILE line LINE (like perl)"; my $p = path $1; - run @OPENER, @EDITOR, "-c", ":$2", "$p" + run @EDITOR, "+$2", "$p" } if (/^(.+):(.+)$/) { handle "file:query (like grep)"; my $p = path $1; - run @OPENER, @EDITOR, "-c", "/$2", "$p" if -e $p; + run @EDITOR, "+/$2", "$p" if -e $p; fail "file not found" if $o{d}; # otherwise fall through } if (/^(\S+)\((\S+)\)[,.]?/) { handle "manpage(section)"; - run @OPENER, "man", $2, $1; + run @MAN_VIEWER, $2, $1; } if (/^(\S+)\.([1-9])/) { handle "manpage.section"; my $p = path "$1.$2"; - run @OPENER, "man", $p if -e $p; - run @OPENER, "man", $2, $1; + run @MAN_VIEWER, $p if -e $p; + run @MAN_VIEWER, $2, $1; } if (/^<(\S+)>$/) { handle "<header.h>"; - run @OPENER, @EDITOR, "/usr/include/$1" if -e "/usr/include/$1"; - run @OPENER, @EDITOR, "/usr/local/include/$1" if -e "/usr/local/include/$1"; + open my $h, 'find / -maxdepth 3 -type d -name include 2>&-|' + or fail "could not search include directories"; + while (<$h>) { + chomp; + if (-e "$_/$1") { + close $h; + run @EDITOR, "$_/$1"; + } + } + close $h; fail "header file not found"; } @@ -79,9 +89,10 @@ for ($phrase) { if (/^(\S+)$/) { handle "maildir / directory / file"; my $p = path $1; - 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 + 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 fail "file not found" if $o{d}; # otherwise fall through } |