Debian Codegraph @

src/vimtutor

raw
1 #!/bin/sh
2
3 # Start Vim on a copy of the tutor file.
4
5 # Type "man vimtutor" (or "vimtutor --help") to learn more about the supported
6 # command-line options.
7 #
8 # Tutors in several human languages are distributed. Type "vimtutor" to use
9 # a tutor in the language of the current locale (:help v:lang), if available;
10 # otherwise fall back to using the English tutor. To request any bundled
11 # tutor, specify its ISO639 name as an argument, e.g. "vimtutor nl".
12
13 # Vim could be called "vim" or "vi". Also check for "vimN", for people who
14 # have Vim installed with its version number.
15 seq="vim vim91 vim90 vim81 vim80 vim8 vim74 vim73 vim72 vim71 vim70 vim7 vim6 vi"
16
17 usage()
18 {
19 echo "==USAGE========================================================================================="
20 echo "${0##*/} [[-(-l)anguage] ISO639] [-(-c)hapter NUMBER] [-(-g)ui] | [-(-h)elp] | [--list]"
21 printf "\twhere:\n"
22 printf "\t\tISO639 (default=en) is a 2 or 3 character language code\n"
23 printf "\t\tNUMBER (default=1) is a chapter number (1 or 2)\n"
24 printf "\texamples:\n"
25 printf "\t\tvimtutor -l es -c 2 -g\n"
26 printf "\t\tvimtutor --language de --chapter 2\n"
27 printf "\t\tvimtutor fr\n"
28 echo "More information at 'man vimtutor'"
29 echo "================================================================================================"
30 }
31
32 listOptions()
33 {
34 echo "==OPTIONS======================================================================================="
35 echo "Chapter: 1"
36 printf "\tLang: %-3s => %s\n" \
37 bar Bavarian \
38 bg Bulgarian \
39 ca Catalan \
40 cs Czech \
41 da Danish \
42 de German \
43 el Greek \
44 en English\(default\) \
45 eo Esperanto \
46 es Spanish \
47 fr French \
48 hr Croatian \
49 hu Hungarian \
50 it Italian \
51 ja Japanese \
52 ko Korean \
53 lt Lithuanian \
54 lv Latvian \
55 nb BokmÄl \
56 nl Dutch \
57 no Norwegian \
58 pl Polish \
59 pt Portuguese \
60 ru Russian \
61 sk Slovak \
62 sr Serbian \
63 sv Swedish \
64 tr Turkish \
65 uk Ukrainian \
66 vi Vietnamese \
67 zh Chinese
68
69 echo "Chapter: 2"
70 printf "\tLang: %-3s => %s\n" \
71 en English\(default\) \
72 fr French
73 echo "================================================================================================"
74 }
75
76 validateLang()
77 {
78 case "$xx" in
79 '' | *[!a-z]* )
80 echo "Error: iso639 code must contain only [a-z]"
81 exit 1
82 esac
83
84 case "${#xx}" in
85 [23] )
86 ;;
87 * )
88 echo "Error: iso639 code must be 2 or 3 characters only"
89 exit 1
90 esac
91
92 export xx
93 }
94
95 validateChapter()
96 {
97 case "$cc" in
98 '' | *[!0-9]* )
99 echo "Error: chapter argument must contain digits only"
100 exit 1
101 ;;
102 [12] )
103 ;;
104 * )
105 echo "Error: invalid chapter number: [12]"
106 exit 1
107 esac
108
109 export CHAPTER="$cc"
110 }
111
112 while [ "$1" != "" ]; do
113 case "$1" in
114 -g | --gui )
115 seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
116 ;;
117 -l | --language )
118 shift
119 xx="$1"
120 validateLang
121 ;;
122 -l[a-z][a-z][a-z] | -l[a-z][a-z] )
123 export xx="${1#*l}"
124 ;;
125 --language[a-z][a-z][a-z] | --language[a-z][a-z] )
126 export xx="${1#*e}"
127 ;;
128 [a-z][a-z][a-z] | [a-z][a-z] )
129 export xx="$1"
130 ;;
131 -c | --chapter )
132 shift
133 cc="$1"
134 validateChapter
135 ;;
136 -c[12] )
137 export CHAPTER="${1#*c}"
138 ;;
139 --chapter[12] )
140 export CHAPTER="${1#*r}"
141 ;;
142 -h | --help )
143 usage
144 exit
145 ;;
146 --list )
147 listOptions
148 exit
149 ;;
150 "" )
151 ;;
152 * )
153 usage
154 exit 1
155 esac
156
157 shift
158 done
159
160
161 # We need a temp file for the copy. First try using a standard command.
162 tmp="${TMPDIR-/tmp}"
163 # shellcheck disable=SC2186
164 TUTORCOPY=$(mktemp "$tmp/tutorXXXXXX" || tempfile -p tutor || echo none)
165
166 # If the standard commands failed then create a directory to put the copy in.
167 # That is a secure way to make a temp file.
168 if test "$TUTORCOPY" = none; then
169 tmpdir="$tmp/vimtutor$$"
170 OLD_UMASK=$(umask)
171 umask 077
172 getout=no
173 mkdir "$tmpdir" || getout=yes
174 umask "$OLD_UMASK"
175 if test "$getout" = yes; then
176 echo "Could not create directory for tutor copy, exiting."
177 exit 1
178 fi
179 TUTORCOPY="$tmpdir/tutorcopy"
180 touch "$TUTORCOPY"
181 TODELETE="$tmpdir"
182 else
183 TODELETE="$TUTORCOPY"
184 fi
185
186 export TUTORCOPY
187
188 # remove the copy of the tutor on exit
189 trap 'rm -rf "$TODELETE"' EXIT HUP INT QUIT SEGV PIPE TERM
190
191 for i in $seq; do
192 testvim=$(command -v "$i" 2>/dev/null)
193 if test -f "$testvim"; then
194 VIM="$i"
195 break
196 fi
197 done
198
199 # When no Vim version was found fall back to "vim", you'll get an error message
200 # below.
201 if test -z "$VIM"; then
202 VIM=vim
203 fi
204
205 # Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
206 # The script tutor.vim tells Vim which file to copy
207
208 $VIM -f -u NONE -c "so \$VIMRUNTIME/tutor/tutor.vim"
209
210 # Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
211 $VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"
212
213 # vim:sw=4:ts=8:noet:nosta: