From 21ee94842b6e461b0cd4536557f9a5a2776132aa Mon Sep 17 00:00:00 2001 From: John Date: Sat, 1 Jun 2019 13:50:20 +0200 Subject: 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. --- dwim.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 dwim.sh diff --git a/dwim.sh b/dwim.sh new file mode 100755 index 0000000..64f7773 --- /dev/null +++ b/dwim.sh @@ -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" -- cgit v1.2.3