From 80076cd9ddfc5dae907ac10f3010925f181b028e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 21 Aug 2016 20:44:39 +1200 Subject: Restructure ed() to test,cache features on login --- sh/profile.d/ed.sh | 22 ++++++++++++++++++++++ sh/shrc.d/ed.sh | 38 ++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 20 deletions(-) create mode 100644 sh/profile.d/ed.sh 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 "$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 - &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 "$@" } -- cgit v1.2.3