diff options
author | John <john@ankarstrom.se> | 2019-06-01 13:50:20 +0200 |
---|---|---|
committer | John <john@ankarstrom.se> | 2019-06-01 13:50:20 +0200 |
commit | 21ee94842b6e461b0cd4536557f9a5a2776132aa (patch) | |
tree | 63b98d77ba93bd49fc2a58c4fa3d2d620e48ae1a | |
download | dwim-21ee94842b6e461b0cd4536557f9a5a2776132aa.tar.gz |
dwim.sh
This is the first version of my dwim script. The problem with POSIX
sh is that the case matching syntax isn't powerful enough. I need
something like Perl's regex matching capabilities.
-rwxr-xr-x | dwim.sh | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,39 @@ +#!/bin/sh + +# dwim (do what i mean) is my own plan9-like plumber + +err() { echo "$@" 1>&2; exit 1; } +usage="$0 phrase" + +[ $# -gt 1 ] && err "$usage" +[ $# -eq 1 ] && p=$1 || p=$(xsel -o) +[ -z "$p" ] && err "$usage" + +[ -z "$OPENER" ] && OPENER=u + +case "$p" in +*:*) + file=${p%%:*} + line=${p#*:}; line=${line%:} + + dir= + case "$file" in + /*) ;; + ~*) ;; + *) + dir=$(xtitle)/ + [ ! -d "$dir" ] && err "couldn't retrieve directory" + ;; + esac + + exec "$OPENER" "$EDITOR" -c ":$line" "$dir$file" + ;; +/*) + exec "$OPENER" "$EDITOR" "${p%:}" + ;; +~*) + exec "$OPENER" "$EDITOR" "${p%:}" + ;; +esac + +err "no handler matched" |