File: select-editor

package info (click to toggle)
sensible-utils 0.0.26
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 604 kB
  • sloc: sh: 859; makefile: 122; sed: 88
file content (97 lines) | stat: -rwxr-xr-x 2,678 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
# shellcheck disable=SC2059

# We don't depend on gettext-bin, so provide a LANG=C-equivalent shim
# see #728612
command -v gettext > /dev/null || alias gettext='printf %s'

# EASIEST editor's basename
EASIEST="nano"

# Ensure that $HOME/.selected_editor is writeable
true >> ~/.selected_editor || exit 1


# The query output is shaped like
#   Name: editor
#   Link: /usr/bin/editor
#   Slaves:
#    editor.1.gz /usr/share/man/man1/editor.1.gz
#   Status: manual
#   Best: /usr/bin/vim.nox
#   Value: /usr/bin/vim.nox
#
#   Alternative: /bin/ed
#   Priority: -100
#   Slaves:
#    editor.1.gz /usr/share/man/man1/ed.1.gz
#
#   Alternative: /usr/bin/vim.nox
#   Priority: 40
#   Slaves:
#    editor.1.gz /usr/share/man/man1/vim.1.gz
# we care about getting {Alternative}s sorted by {Priority}, in this case
#   /usr/bin/vim.nox
#   /bin/ed
sorted_list_of_editors="$(
	alternative=
	priority=
	update-alternatives --query editor | while read -r field value; do
		case "$field" in
			'Alternative:') alternative="$value" ;;
			'Priority:')    priority="$value"    ;;
		esac
		if [ -n "$alternative" ] && [ -n "$priority" ]; then
			printf '%s\t%s\n' "$priority" "$alternative"
			alternative=
			priority=
		fi
	done | sort -n -r | cut -f 2-
)"
IFS='
'

editor_count="$(update-alternatives --list editor | wc -l)"
if [ "$editor_count" -gt 1 ]; then
	TEXTDOMAIN=sensible-utils gettext 'Select an editor.  To change later, run select-editor again.
'
	# shellcheck source=/dev/null
	[ -r ~/.selected_editor ] && . ~/.selected_editor  # Highlight the current selection with a *

	easiest=1 # If EASIEST not found, default to the best alternative
	default=
	i=0
	for e in $sorted_list_of_editors; do
		i=$(( i + 1 ))
		[ "$e" = "$SELECTED_EDITOR" ] && { ind='*'; default=$i; } || ind=' '
		if [ "${e##*/}" = "$EASIEST" ]; then
			# %c=* for the currently-selected entry, space for others
			printf "$(TEXTDOMAIN=sensible-utils gettext '%c %*u. %s        <---- easiest\n')" "$ind" "${#editor_count}" $i "$e"
			easiest=$i
		else
			printf "$(TEXTDOMAIN=sensible-utils gettext '%c %*u. %s\n')" "$ind" "${#editor_count}" $i "$e"
		fi
	done
	echo
	default="${default:-"$easiest"}"

	selected=x
	while :; do
		if [ -z "$selected" ]; then
			selected="$default"
		elif ! { [ "$selected" -gt 0 ] && [ "$selected" -le $i ]; } 2>/dev/null; then
			printf "$(TEXTDOMAIN=sensible-utils gettext 'Choose 1-%u [%u]: ')" $i "$default"
			read -r selected
		else
			break
		fi
	done
	i=0
	for e in $sorted_list_of_editors; do
		i=$(( i + 1 ))
		if [ $i -eq "$selected" ]; then
			printf '%s\n' "# Generated by $0" "SELECTED_EDITOR=\"$e\"" > ~/.selected_editor && exit 0
		fi
	done
fi
exit 1