| 1 | #!/bin/sh |
| 2 | # shellcheck disable=SC2059 |
| 3 | |
| 4 | # We don't depend on gettext-bin, so provide a LANG=C-equivalent shim |
| 5 | # see #728612 |
| 6 | command -v gettext > /dev/null || alias gettext='printf %s' |
| 7 | |
| 8 | # EASIEST editor's basename |
| 9 | EASIEST="nano" |
| 10 | |
| 11 | # Ensure that $HOME/.selected_editor is writeable |
| 12 | true >> ~/.selected_editor || exit 1 |
| 13 | |
| 14 | |
| 15 | # The query output is shaped like |
| 16 | # Name: editor |
| 17 | # Link: /usr/bin/editor |
| 18 | # Slaves: |
| 19 | # editor.1.gz /usr/share/man/man1/editor.1.gz |
| 20 | # Status: manual |
| 21 | # Best: /usr/bin/vim.nox |
| 22 | # Value: /usr/bin/vim.nox |
| 23 | # |
| 24 | # Alternative: /bin/ed |
| 25 | # Priority: -100 |
| 26 | # Slaves: |
| 27 | # editor.1.gz /usr/share/man/man1/ed.1.gz |
| 28 | # |
| 29 | # Alternative: /usr/bin/vim.nox |
| 30 | # Priority: 40 |
| 31 | # Slaves: |
| 32 | # editor.1.gz /usr/share/man/man1/vim.1.gz |
| 33 | # we care about getting {Alternative}s sorted by {Priority}, in this case |
| 34 | # /usr/bin/vim.nox |
| 35 | # /bin/ed |
| 36 | sorted_list_of_editors="$( |
| 37 | alternative= |
| 38 | priority= |
| 39 | update-alternatives --query editor | while read -r field value; do |
| 40 | case "$field" in |
| 41 | 'Alternative:') alternative="$value" ;; |
| 42 | 'Priority:') priority="$value" ;; |
| 43 | esac |
| 44 | if [ -n "$alternative" ] && [ -n "$priority" ]; then |
| 45 | printf '%s\t%s\n' "$priority" "$alternative" |
| 46 | alternative= |
| 47 | priority= |
| 48 | fi |
| 49 | done | sort -n -r | cut -f 2- |
| 50 | )" |
| 51 | IFS=' |
| 52 | ' |
| 53 | |
| 54 | editor_count="$(update-alternatives --list editor | wc -l)" |
| 55 | if [ "$editor_count" -gt 1 ]; then |
| 56 | TEXTDOMAIN=sensible-utils gettext 'Select an editor. To change later, run select-editor again. |
| 57 | ' |
| 58 | # shellcheck source=/dev/null |
| 59 | [ -r ~/.selected_editor ] && . ~/.selected_editor # Highlight the current selection with a * |
| 60 | |
| 61 | easiest=1 # If EASIEST not found, default to the best alternative |
| 62 | default= |
| 63 | i=0 |
| 64 | for e in $sorted_list_of_editors; do |
| 65 | i=$(( i + 1 )) |
| 66 | [ "$e" = "$SELECTED_EDITOR" ] && { ind='*'; default=$i; } || ind=' ' |
| 67 | if [ "${e##*/}" = "$EASIEST" ]; then |
| 68 | # %c=* for the currently-selected entry, space for others |
| 69 | printf "$(TEXTDOMAIN=sensible-utils gettext '%c %*u. %s <---- easiest\n')" "$ind" "${#editor_count}" $i "$e" |
| 70 | easiest=$i |
| 71 | else |
| 72 | printf "$(TEXTDOMAIN=sensible-utils gettext '%c %*u. %s\n')" "$ind" "${#editor_count}" $i "$e" |
| 73 | fi |
| 74 | done |
| 75 | echo |
| 76 | default="${default:-"$easiest"}" |
| 77 | |
| 78 | selected=x |
| 79 | while :; do |
| 80 | if [ -z "$selected" ]; then |
| 81 | selected="$default" |
| 82 | elif ! { [ "$selected" -gt 0 ] && [ "$selected" -le $i ]; } 2>/dev/null; then |
| 83 | printf "$(TEXTDOMAIN=sensible-utils gettext 'Choose 1-%u [%u]: ')" $i "$default" |
| 84 | read -r selected |
| 85 | else |
| 86 | break |
| 87 | fi |
| 88 | done |
| 89 | i=0 |
| 90 | for e in $sorted_list_of_editors; do |
| 91 | i=$(( i + 1 )) |
| 92 | if [ $i -eq "$selected" ]; then |
| 93 | printf '%s\n' "# Generated by $0" "SELECTED_EDITOR=\"$e\"" > ~/.selected_editor && exit 0 |
| 94 | fi |
| 95 | done |
| 96 | fi |
| 97 | exit 1 |