blob: beb95f0e55325d7e1d28751895161a12d9308abf [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" \
Damien Lejayd419fa22025-06-29 17:50:20 +020071en English\(default\) \
72fr French
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010073 echo "================================================================================================"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010074}
75
76validateLang()
77{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010078 case "$xx" in
79 '' | *[!a-z]* )
80 echo "Error: iso639 code must contain only [a-z]"
81 exit 1
82 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010083
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010084 case "${#xx}" in
85 [23] )
86 ;;
87 * )
88 echo "Error: iso639 code must be 2 or 3 characters only"
89 exit 1
90 esac
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010091
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010092 export xx
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010093}
94
95validateChapter()
96{
Aliaksei Budavei715a58f2024-11-06 21:58:53 +010097 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
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100108
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100109 export CHAPTER="$cc"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100110}
111
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100112while [ "$1" != "" ]; do
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100113 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
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100158done
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
161# We need a temp file for the copy. First try using a standard command.
162tmp="${TMPDIR-/tmp}"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100163# shellcheck disable=SC2186
164TUTORCOPY=$(mktemp "$tmp/tutorXXXXXX" || tempfile -p tutor || echo none)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
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.
168if test "$TUTORCOPY" = none; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100169 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"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182else
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100183 TODELETE="$TUTORCOPY"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184fi
185
186export TUTORCOPY
187
188# remove the copy of the tutor on exit
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100189trap 'rm -rf "$TODELETE"' EXIT HUP INT QUIT SEGV PIPE TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaar13153492007-07-17 12:33:46 +0000191for i in $seq; do
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100192 testvim=$(command -v "$i" 2>/dev/null)
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200193 if test -f "$testvim"; then
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100194 VIM="$i"
195 break
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200196 fi
Bram Moolenaar13153492007-07-17 12:33:46 +0000197done
198
199# When no Vim version was found fall back to "vim", you'll get an error message
200# below.
201if test -z "$VIM"; then
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200202 VIM=vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203fi
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
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100207
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100208$VIM -f -u NONE -c "so \$VIMRUNTIME/tutor/tutor.vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar527dec32018-04-12 20:36:43 +0200210# Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200211$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"
Aliaksei Budavei715a58f2024-11-06 21:58:53 +0100212
213# vim:sw=4:ts=8:noet:nosta: