Debian Codegraph @

select-editor

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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/nullscip-shell filesystem . . `/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 truescip-shell system . . 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-alternativesscip-shell system . . update-alternatives. --list editor | wcscip-shell system . . wc. -l)"
55 if [scip-shell system . . `[`. "$editor_count" -gt 1 ]; then
56 TEXTDOMAIN=sensible-utils gettextscip-shell system . . gettext. 'Select an editor. To change later, run select-editor again.
57 '
58 # shellcheck source=/dev/null
59 [scip-shell system . . `[`. -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 [scip-shell system . . `[`. "$e" = "$SELECTED_EDITOR" ] && { ind='*'; default=$i; } || ind=' '
67 if [scip-shell system . . `[`. "${e##*/}" = "$EASIEST" ]; then
68 # %c=* for the currently-selected entry, space for others
69 printfscip-shell system . . printf. "$(TEXTDOMAIN=sensible-utils gettextscip-shell system . . gettext. '%c %*u. %s <---- easiest\n')" "$ind" "${#editor_count}" $i "$e"
70 easiest=$i
71 else
72 printfscip-shell system . . printf. "$(TEXTDOMAIN=sensible-utils gettextscip-shell system . . gettext. '%c %*u. %s\n')" "$ind" "${#editor_count}" $i "$e"
73 fi
74 done
75 echoscip-shell system . . echo.
76 default="${default:-"$easiest"}"
77
78 selected=x
79 while :; do
80 if [scip-shell system . . `[`. -z "$selected" ]; then
81 selected="$default"
82 elif ! { [scip-shell system . . `[`. "$selected" -gt 0 ] && [scip-shell system . . `[`. "$selected" -le $i ]; } 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then
83 printfscip-shell system . . printf. "$(TEXTDOMAIN=sensible-utils gettextscip-shell system . . 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 [scip-shell system . . `[`. $i -eq "$selected" ]; then
93 printfscip-shell system . . printf. '%s\n' "# Generated by $0" "SELECTED_EDITOR=\"$e\"" > ~/.selected_editor && exit 0
94 fi
95 done
96 fi
97 exit 1