| 1 | #!/bin/sh |
| 2 | # Copyright 2007 Jari Aalto; |
| 3 | # Copyright 2019-2022 Bastien Roucariès |
| 4 | # Copyright 2022 Richard Lewis |
| 5 | # Released under GNU GPL v2 or any later version |
| 6 | |
| 7 | # Prevent recursive loops, where environment variables are set to this script |
| 8 | [ -n "$__SENSIBLE_EDITOR" ] && exit 126 |
| 9 | __SENSIBLE_EDITOR="$0" |
| 10 | export __SENSIBLE_EDITOR |
| 11 | |
| 12 | |
| 13 | Try() |
| 14 | { |
| 15 | [ -z "$__sensible_candidate" ] && return |
| 16 | eval "$__sensible_candidate \"\$@\""; ret=$? |
| 17 | # 127=ENOENT, 126=couldn't exec for another reason |
| 18 | [ "$ret" -ne 126 ] && [ "$ret" -ne 127 ] && exit "$ret" |
| 19 | unset ret |
| 20 | } |
| 21 | |
| 22 | |
| 23 | # work around for #991982 |
| 24 | # shellcheck disable=SC2317 |
| 25 | nano () |
| 26 | { |
| 27 | if [ -z "$TERM" ]; then |
| 28 | return 126 |
| 29 | else |
| 30 | command nano "$@" |
| 31 | fi |
| 32 | } |
| 33 | |
| 34 | # shellcheck disable=SC2153 |
| 35 | for __sensible_candidate in "$VISUAL" "$EDITOR" "$SENSIBLE_EDITOR" "$SELECTED_EDITOR"; do |
| 36 | Try "$@" |
| 37 | done |
| 38 | |
| 39 | # fix #987675 |
| 40 | if [ -n "$HOME" ]; then |
| 41 | if [ -r ~/.selected_editor ]; then |
| 42 | # shellcheck source=/dev/null |
| 43 | . ~/.selected_editor |
| 44 | elif [ -z "$EDITOR" ] && [ -z "$SELECTED_EDITOR" ] && [ -t 0 ]; then |
| 45 | # shellcheck source=/dev/null |
| 46 | select-editor && [ -r ~/.selected_editor ] && . ~/.selected_editor |
| 47 | fi |
| 48 | fi |
| 49 | |
| 50 | for __sensible_candidate in "$EDITOR" "$SELECTED_EDITOR"; do |
| 51 | Try "$@" |
| 52 | done |
| 53 | |
| 54 | for __sensible_candidate in editor nano nano-tiny vi; do |
| 55 | Try "$@" |
| 56 | done |
| 57 | |
| 58 | command -v gettext > /dev/null || alias gettext='printf %s' |
| 59 | # shellcheck disable=SC2016 |
| 60 | TEXTDOMAIN=sensible-utils gettext 'Couldn'\''t find an editor! |
| 61 | Set the $EDITOR environment variable to your desired editor. |
| 62 | ' >&2 |
| 63 | exit 1 |