aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-21 20:44:39 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-21 20:44:39 +1200
commit80076cd9ddfc5dae907ac10f3010925f181b028e (patch)
treefbc7c4f4050534422bdda0bd0b0e71598f5a000d
parent2d392c27d78cce8c7b663a17abbd1c699e997b54 (diff)
downloaddotfiles-80076cd9ddfc5dae907ac10f3010925f181b028e.tar.gz
dotfiles-80076cd9ddfc5dae907ac10f3010925f181b028e.zip
Restructure ed() to test,cache features on login
-rw-r--r--sh/profile.d/ed.sh22
-rw-r--r--sh/shrc.d/ed.sh38
2 files changed, 40 insertions, 20 deletions
diff --git a/sh/profile.d/ed.sh b/sh/profile.d/ed.sh
new file mode 100644
index 00000000..abbd75c5
--- /dev/null
+++ b/sh/profile.d/ed.sh
@@ -0,0 +1,22 @@
+# Test that we have metadata about what options this system's ed(1) supports,
+# and try to create it if not
+(
+ # Create a directory to hold metadata about ed
+ ecd=$HOME/.cache/ed
+ if ! [ -d "$ecd" ] ; then
+ mkdir -p -- "$ecd" || exit
+ fi
+
+ # Write ed(1)'s --help output to a file, even if it's empty
+ if ! [ -f "$ecd"/help ] ; then
+ ed --help </dev/null >"$ecd"/help 2>/dev/null || exit
+
+ # Iterate through some useful options and create files to show they're
+ # available
+ set -- verbose
+ for opt ; do
+ grep -q -- --"$opt" "$ecd"/help || continue
+ touch -- "$ecd"/"$opt" || exit
+ done
+ fi
+)
diff --git a/sh/shrc.d/ed.sh b/sh/shrc.d/ed.sh
index 4638d2cb..243dcffc 100644
--- a/sh/shrc.d/ed.sh
+++ b/sh/shrc.d/ed.sh
@@ -1,26 +1,24 @@
-# Add a colon prompt to ed when a command is expected rather than text; makes
-# it feel a lot more like using ex. Only do this when stdin is a terminal,
-# however. Also try and use -v for more verbose error output, and rlwrap(1) if
-# it's available.
+# Define function proper
ed() {
- # We're only adding options if input is from a terminal
- if [ -t 0 ] ; then
+ # Don't mess with original call if input not a terminal
+ if ! [ -t 0 ] ; then
+ command ed "$@"
+ return
+ fi
- # Colon prompt (POSIX)
- set -- -p : "$@"
+ # Add --verbose to explain errors
+ [ -e "$HOME"/.cache/ed/verbose ] &&
+ set -- --verbose "$@"
- # Verbose if available (not POSIX)
- if ed -sv - </dev/null >&0 2>&0 ; then
- set -- -v "$@"
- fi
- fi
+ # Add a colon prompt (POSIX feature)
+ set -- -p: "$@"
- # Execute the ed(1) call, in a wrapper if appropriate and with the
- # concluded options
- if [ -t 0 ] && command -v rlwrap >/dev/null 2>&1 ; then
- command rlwrap ed "$@"
- else
- command ed "$@"
- fi
+ # Run in rlwrap(1) if available
+ set -- ed "$@"
+ command -v rlwrap >/dev/null 2>&1 &&
+ set -- rlwrap "$@"
+
+ # Run determined command
+ command "$@"
}