blob: 1d530d491ed91c1f2f7c060bb8e0e6dc9fe90468 [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 \
RestorerZe7ea4ba2024-12-02 20:07:58 +010053lt Lithuanian \
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010054lv Latvian \
55nb Bokmål \
56nl Dutch \
57no Norwegian \
58pl Polish \
59pt Portuguese \
60ru Russian \
61sk Slovak \
62sr Serbian \
63sv Swedish \
64tr Turkish \
RestorerZe7ea4ba2024-12-02 20:07:58 +010065uk Ukrainian \
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010066vi Vietnamese \
67zh Chinese
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010068
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010069 echo "Chapter: 2"
70 printf "\tLang: %-3s => %s\n" \
71en English\(default\)
72 echo "================================================================================================"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010073}
74
75validateLang()
76{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010077 case "$xx" in
78 '' | *[!a-z]* )
79 echo "Error: iso639 code must contain only [a-z]"
80 exit 1
81 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010082
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010083 case "${#xx}" in
84 [23] )
85 ;;
86 * )
87 echo "Error: iso639 code must be 2 or 3 characters only"
88 exit 1
89 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010090
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010091 export xx
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010092}
93
94validateChapter()
95{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010096 case "$cc" in
97 '' | *[!0-9]* )
98 echo "Error: chapter argument must contain digits only"
99 exit 1
100 ;;
101 [12] )
102 ;;
103 * )
104 echo "Error: invalid chapter number: [12]"
105 exit 1
106 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100107
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100108 export CHAPTER="$cc"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100109}
110
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100111while [ "$1" != "" ]; do
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100112 case "$1" in
113 -g | --gui )
114 seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
115 ;;
116 -l | --language )
117 shift
118 xx="$1"
119 validateLang
120 ;;
121 -l[a-z][a-z][a-z] | -l[a-z][a-z] )
122 export xx="${1#*l}"
123 ;;
124 --language[a-z][a-z][a-z] | --language[a-z][a-z] )
125 export xx="${1#*e}"
126 ;;
127 [a-z][a-z][a-z] | [a-z][a-z] )
128 export xx="$1"
129 ;;
130 -c | --chapter )
131 shift
132 cc="$1"
133 validateChapter
134 ;;
135 -c[12] )
136 export CHAPTER="${1#*c}"
137 ;;
138 --chapter[12] )
139 export CHAPTER="${1#*r}"
140 ;;
141 -h | --help )
142 usage
143 exit
144 ;;
145 --list )
146 listOptions
147 exit
148 ;;
149 "" )
150 ;;
151 * )
152 usage
153 exit 1
154 esac
155
156 shift
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100157done
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160# We need a temp file for the copy. First try using a standard command.
161tmp="${TMPDIR-/tmp}"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100162# shellcheck disable=SC2186
163TUTORCOPY=$(mktemp "$tmp/tutorXXXXXX" || tempfile -p tutor || echo none)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165# If the standard commands failed then create a directory to put the copy in.
166# That is a secure way to make a temp file.
167if test "$TUTORCOPY" = none; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100168 tmpdir="$tmp/vimtutor$$"
169 OLD_UMASK=$(umask)
170 umask 077
171 getout=no
172 mkdir "$tmpdir" || getout=yes
173 umask "$OLD_UMASK"
174 if test "$getout" = yes; then
175 echo "Could not create directory for tutor copy, exiting."
176 exit 1
177 fi
178 TUTORCOPY="$tmpdir/tutorcopy"
179 touch "$TUTORCOPY"
180 TODELETE="$tmpdir"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181else
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100182 TODELETE="$TUTORCOPY"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183fi
184
185export TUTORCOPY
186
187# remove the copy of the tutor on exit
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100188trap 'rm -rf "$TODELETE"' EXIT HUP INT QUIT SEGV PIPE TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar13153492007-07-17 12:33:46 +0000190for i in $seq; do
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100191 testvim=$(command -v "$i" 2>/dev/null)
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200192 if test -f "$testvim"; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100193 VIM="$i"
194 break
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200195 fi
Bram Moolenaar13153492007-07-17 12:33:46 +0000196done
197
198# When no Vim version was found fall back to "vim", you'll get an error message
199# below.
200if test -z "$VIM"; then
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200201 VIM=vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202fi
203
204# Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
205# The script tutor.vim tells Vim which file to copy
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100206
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100207$VIM -f -u NONE -c "so \$VIMRUNTIME/tutor/tutor.vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaar527dec32018-04-12 20:36:43 +0200209# Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200210$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100211
212# vim:sw=4:ts=8:noet:nosta: