aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn <john@ankarstrom.se>2019-06-01 13:50:20 +0200
committerJohn <john@ankarstrom.se>2019-06-01 13:50:20 +0200
commit21ee94842b6e461b0cd4536557f9a5a2776132aa (patch)
tree63b98d77ba93bd49fc2a58c4fa3d2d620e48ae1a
downloaddwim-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-xdwim.sh39
1 files changed, 39 insertions, 0 deletions
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"