[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Review of new man page of sensible-editor



Justin B Rye <justin.byam.rye@gmail.com> writes:

> RL wrote:

>> #!/bin/sh
>> # Copyright 2007 Jari Aalto; Released under GNU GPL v2 or any later version
>> # Copyright 201
>
> (I wonder what's going on here?)

no idea

>> # Prevent recursive loops, where environment variables are set to this script
>> p="$(command -v "$0")"
>
> Of course this loop-detector won't catch VISUAL='sensible-editor --',
> but I suppose that's just another variety of "don't do that then".

maybe overkill, but i did find a way to catch that: and in
doing so, i realised some other things:

- there is no need to check for an exit status of 126 or 127 (which
might mean anything): can just use command -v
- the fix for  #991982 isnt going to work if editor == nano (or the user
has EDITOR='nano --')

#!/bin/sh
# Copyright 2007 Jari Aalto; Released under GNU GPL v2 or any later version
# Copyright 201

# Prevent recursive loops, where environment variables are set to this script
this_script="$(command -v "$0")"
is_this_script(){
		[ "$(command -v "$1")" = "$this_script" ]
}

# try editing using $candidate and exit.  But do nothing if
# $candidate's first word is not runnable, points to this script or
# will not start with current TERM.
Try()
{
		# $candidate must be unquoted as it may contain arguments
		is_this_script $candidate && return 1
		can_run $candidate || return 1
		term_is_ok_for $candidate || return 1
		exec $candidate "$@"
}

# exists and is executable
can_run(){
		command -v "$1" >/dev/null
}

# work around for #991982
term_is_ok_for(){
		# either TERM is set (to anything) or $1 can not run with an empty TERM
		[ -n "$TERM" ] && return 0

		prog="$(basename "$(realpath "$(command -v "$1")")")"
		[ "$prog" != "nano" ] && [ "$prog" != "nano-tiny" ] && [ "$prog" != "jed" ]
}

for candidate in "$VISUAL" "$EDITOR"; do
		Try "$@"
done

if [ -n "$HOME" ]; then
		# fix #987675: HOME should be set before using select-editor(1)
    if [ -r ~/.selected_editor ]; then
				. ~/.selected_editor 2>/dev/null
		fi
		# if we ended up with $this_script or nothing then run
		# select-editor(1)
		is_this_script $EDITOR && EDITOR=
		is_this_script $SELECTED_EDITOR && SELECTED_EDITOR=
    if [ -z "$EDITOR" ] && [ -z "$SELECTED_EDITOR" ] && [ -t 0 ]; then
				select-editor && . ~/.selected_editor 2>/dev/null
    fi
fi

if term_is_ok_for editor; then
		editor="editor"
else
		# editor(1) will not run with TERM's current setting
		editor=""
fi

for candidate in "$EDITOR" "$SELECTED_EDITOR" $editor nano nano-tiny vi "busybox vi"; do
		Try "$@"
done

echo "Couldn't find an editor!" 1>&2
echo 'Set the $EDITOR environment variable to your desired editor.' 1>&2
exit 1

> I suspect this *could* use
>
> 	: "${HOME:=$( printf '%s' ~ )}"
>
> but that's too cunning for its own good.

I dont think that works when HOME is empty (~ expands to the empty
$HOME i would thinkpad)

(other suggestions taken, including adding "busybox vi" to
the end of the fallback list - why not!)

Reply to: