blob: 409c10c0474a70f3b5cbbc2339a8db65950dfa76 [file] [log] [blame]
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +01001#!/bin/sh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3# Start Vim on a copy of the tutor file.
4
Aliaksei Budavei715a58f2024-11-06 21:58:53 +01005# 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".
Bram Moolenaar071d4272004-06-13 20:20:40 +000012
Bram Moolenaar2b570782008-05-07 15:40:33 +000013# Vim could be called "vim" or "vi". Also check for "vimN", for people who
14# have Vim installed with its version number.
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010015seq="vim vim91 vim90 vim81 vim80 vim8 vim74 vim73 vim72 vim71 vim70 vim7 vim6 vi"
Bram Moolenaar2b570782008-05-07 15:40:33 +000016
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010017usage()
18{
19 echo "==USAGE========================================================================================="
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010020 echo "${0##*/} [[-(-l)anguage] ISO639] [-(-c)hapter NUMBER] [-(-g)ui] | [-(-h)elp] | [--list]"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010021 printf "\twhere:\n"
22 printf "\t\tISO639 (default=en) is a 2 or 3 character language code\n"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010023 printf "\t\tNUMBER (default=1) is a chapter number (1 or 2)\n"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010024 printf "\texamples:\n"
25 printf "\t\tvimtutor -l es -c 2 -g\n"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010026 printf "\t\tvimtutor --language de --chapter 2\n"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010027 printf "\t\tvimtutor fr\n"
28 echo "More information at 'man vimtutor'"
29 echo "================================================================================================"
30}
31
32listOptions()
33{
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010034 echo "==OPTIONS======================================================================================="
35 echo "Chapter: 1"
36 printf "\tLang: %-3s => %s\n" \
37bar Bavarian \
38bg Bulgarian \
39ca Catalan \
40cs Czech \
41da Danish \
42de German \
43el Greek \
44en English\(default\) \
45eo Esperanto \
46es Spanish \
47fr French \
48hr Croatian \
49hu Hungarian \
50it Italian \
51ja Japanese \
52ko Korean \
53lv Latvian \
54nb Bokmål \
55nl Dutch \
56no Norwegian \
57pl Polish \
58pt Portuguese \
59ru Russian \
60sk Slovak \
61sr Serbian \
62sv Swedish \
63tr Turkish \
64uk English \
65vi Vietnamese \
66zh Chinese
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010067
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010068 echo "Chapter: 2"
69 printf "\tLang: %-3s => %s\n" \
70en English\(default\)
71 echo "================================================================================================"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010072}
73
74validateLang()
75{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010076 case "$xx" in
77 '' | *[!a-z]* )
78 echo "Error: iso639 code must contain only [a-z]"
79 exit 1
80 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010081
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010082 case "${#xx}" in
83 [23] )
84 ;;
85 * )
86 echo "Error: iso639 code must be 2 or 3 characters only"
87 exit 1
88 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010089
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010090 export xx
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010091}
92
93validateChapter()
94{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010095 case "$cc" in
96 '' | *[!0-9]* )
97 echo "Error: chapter argument must contain digits only"
98 exit 1
99 ;;
100 [12] )
101 ;;
102 * )
103 echo "Error: invalid chapter number: [12]"
104 exit 1
105 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100106
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100107 export CHAPTER="$cc"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100108}
109
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100110while [ "$1" != "" ]; do
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100111 case "$1" in
112 -g | --gui )
113 seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
114 ;;
115 -l | --language )
116 shift
117 xx="$1"
118 validateLang
119 ;;
120 -l[a-z][a-z][a-z] | -l[a-z][a-z] )
121 export xx="${1#*l}"
122 ;;
123 --language[a-z][a-z][a-z] | --language[a-z][a-z] )
124 export xx="${1#*e}"
125 ;;
126 [a-z][a-z][a-z] | [a-z][a-z] )
127 export xx="$1"
128 ;;
129 -c | --chapter )
130 shift
131 cc="$1"
132 validateChapter
133 ;;
134 -c[12] )
135 export CHAPTER="${1#*c}"
136 ;;
137 --chapter[12] )
138 export CHAPTER="${1#*r}"
139 ;;
140 -h | --help )
141 usage
142 exit
143 ;;
144 --list )
145 listOptions
146 exit
147 ;;
148 "" )
149 ;;
150 * )
151 usage
152 exit 1
153 esac
154
155 shift
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100156done
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159# We need a temp file for the copy. First try using a standard command.
160tmp="${TMPDIR-/tmp}"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100161# shellcheck disable=SC2186
162TUTORCOPY=$(mktemp "$tmp/tutorXXXXXX" || tempfile -p tutor || echo none)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
164# If the standard commands failed then create a directory to put the copy in.
165# That is a secure way to make a temp file.
166if test "$TUTORCOPY" = none; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100167 tmpdir="$tmp/vimtutor$$"
168 OLD_UMASK=$(umask)
169 umask 077
170 getout=no
171 mkdir "$tmpdir" || getout=yes
172 umask "$OLD_UMASK"
173 if test "$getout" = yes; then
174 echo "Could not create directory for tutor copy, exiting."
175 exit 1
176 fi
177 TUTORCOPY="$tmpdir/tutorcopy"
178 touch "$TUTORCOPY"
179 TODELETE="$tmpdir"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180else
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100181 TODELETE="$TUTORCOPY"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182fi
183
184export TUTORCOPY
185
186# remove the copy of the tutor on exit
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100187trap 'rm -rf "$TODELETE"' EXIT HUP INT QUIT SEGV PIPE TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar13153492007-07-17 12:33:46 +0000189for i in $seq; do
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100190 testvim=$(command -v "$i" 2>/dev/null)
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200191 if test -f "$testvim"; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100192 VIM="$i"
193 break
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200194 fi
Bram Moolenaar13153492007-07-17 12:33:46 +0000195done
196
197# When no Vim version was found fall back to "vim", you'll get an error message
198# below.
199if test -z "$VIM"; then
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200200 VIM=vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201fi
202
203# Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
204# The script tutor.vim tells Vim which file to copy
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100205
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100206$VIM -f -u NONE -c "so \$VIMRUNTIME/tutor/tutor.vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaar527dec32018-04-12 20:36:43 +0200208# Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200209$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100210
211# vim:sw=4:ts=8:noet:nosta: