blob: 6e29a7602379cbaf6ae8805fce13e0bf04981a48 [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
Bram Moolenaar2b570782008-05-07 15:40:33 +00005# Usage: vimtutor [-g] [xx]
6# Where optional argument -g starts vimtutor in gvim (GUI) instead of vim.
7# and xx is a language code like "es" or "nl".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008# When an argument is given, it tries loading that tutor.
9# When this fails or no argument was given, it tries using 'v:lang'
10# When that also fails, it uses the English version.
11
Bram Moolenaar2b570782008-05-07 15:40:33 +000012# Vim could be called "vim" or "vi". Also check for "vimN", for people who
13# have Vim installed with its version number.
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010014seq="vim vim91 vim90 vim81 vim80 vim8 vim74 vim73 vim72 vim71 vim70 vim7 vim6 vi"
Bram Moolenaar2b570782008-05-07 15:40:33 +000015
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010016usage()
17{
18 echo "==USAGE========================================================================================="
19 echo "${0##*/} [-(-l)anguage ISO639] [-(-c)hapter) NUMBER] [-(-g)ui] | [-(-h)elp] | [--list]"
20 printf "\twhere:\n"
21 printf "\t\tISO639 (default=en) is a 2 or 3 character language code\n"
22 printf "\t\tNUMBER (default=01) is one or two digits representing the chapter number\n"
23 printf "\texamples:\n"
24 printf "\t\tvimtutor -l es -c 2 -g\n"
25 printf "\t\tvimtutor --language de --chapter 02\n"
26 printf "\t\tvimtutor fr\n"
27 echo "More information at 'man vimtutor'"
28 echo "================================================================================================"
29}
30
31listOptions()
32{
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010033 echo "==OPTIONS======================================================================================="
34 echo "Chapter: 1"
35 printf "\tLang: %-3s => %s\n" \
36bar Bavarian \
37bg Bulgarian \
38ca Catalan \
39cs Czech \
40da Danish \
41de German \
42el Greek \
43en English\(default\) \
44eo Esperanto \
45es Spanish \
46fr French \
47hr Croatian \
48hu Hungarian \
49it Italian \
50ja Japanese \
51ko Korean \
52lv Latvian \
53nb Bokmål \
54nl Dutch \
55no Norwegian \
56pl Polish \
57pt Portuguese \
58ru Russian \
59sk Slovak \
60sr Serbian \
61sv Swedish \
62tr Turkish \
63uk English \
64vi Vietnamese \
65zh Chinese
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010066
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010067 echo "Chapter: 2"
68 printf "\tLang: %-3s => %s\n" \
69en English\(default\)
70 echo "================================================================================================"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010071}
72
73validateLang()
74{
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010075 case "$xx" in
76 '' | *[!a-z]* )
77 echo "Error: iso639 code must contain only [a-z]"
78 exit 1
79 esac
80
81 case "${#xx}" in
82 [23] )
83 ;;
84 * )
85 echo "Error: iso639 code must be 2 or 3 characters only"
86 exit 1
87 esac
88
Paul Desmond Parker17c71da2024-11-03 20:47:53 +010089 export xx
90}
91
92validateChapter()
93{
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +010094 case "$cc" in
95 '' | *[!0-9]* )
96 echo "Error: chapter argument must contain digits only"
97 exit 1
98 ;;
99 0 | 00 )
100 echo "Error: chapter must be non-zero"
101 exit 1
102 esac
103
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100104 export CHAPTER="$cc"
105}
106
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100107while [ "$1" != "" ]; do
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100108 case "$1" in
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100109 -g | --gui ) seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
110 ;;
111 -l | --language ) shift
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100112 xx="$1"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100113 validateLang
114 ;;
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100115 -l[a-z][a-z][a-z] | -l[a-z][a-z] )
116 export xx="${1#*l}"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100117 ;;
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100118 --language[a-z][a-z][a-z] | --language[a-z][a-z] )
119 export xx="${1#*e}"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100120 ;;
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100121 [a-z][a-z][a-z] | [a-z][a-z] ) export xx="$1"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100122 ;;
123 -c | --chapter ) shift
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100124 cc="$1"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100125 validateChapter
126 ;;
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100127 -c[1-9][0-9] | -c[1-9] ) export CHAPTER="${1#*c}"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100128 ;;
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100129 --chapter[1-9][0-9] | --chapter[1-9] )
130 export CHAPTER="${1#*r}"
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100131 ;;
132 -h | --help ) usage
133 exit
134 ;;
135 --list ) listOptions
136 exit
137 ;;
138 "" ) ;;
139 * ) usage
140 exit 1
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100141 esac
142 shift
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100143done
144
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146# We need a temp file for the copy. First try using a standard command.
147tmp="${TMPDIR-/tmp}"
148TUTORCOPY=`mktemp $tmp/tutorXXXXXX || tempfile -p tutor || echo none`
149
150# If the standard commands failed then create a directory to put the copy in.
151# That is a secure way to make a temp file.
152if test "$TUTORCOPY" = none; then
153 tmpdir=$tmp/vimtutor$$
154 OLD_UMASK=`umask`
155 umask 077
156 getout=no
157 mkdir $tmpdir || getout=yes
158 umask $OLD_UMASK
159 if test $getout = yes; then
160 echo "Could not create directory for tutor copy, exiting."
161 exit 1
162 fi
163 TUTORCOPY=$tmpdir/tutorcopy
164 touch $TUTORCOPY
165 TODELETE=$tmpdir
166else
167 TODELETE=$TUTORCOPY
168fi
169
170export TUTORCOPY
171
172# remove the copy of the tutor on exit
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100173trap "rm -rf $TODELETE" EXIT HUP INT QUIT SEGV PIPE TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
Bram Moolenaar13153492007-07-17 12:33:46 +0000175for i in $seq; do
Aliaksei Budavei09cc8c92024-11-04 19:43:22 +0100176 testvim=$(command -v "$i" 2>/dev/null)
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200177 if test -f "$testvim"; then
178 VIM=$i
179 break
180 fi
Bram Moolenaar13153492007-07-17 12:33:46 +0000181done
182
183# When no Vim version was found fall back to "vim", you'll get an error message
184# below.
185if test -z "$VIM"; then
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200186 VIM=vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187fi
188
189# Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
190# The script tutor.vim tells Vim which file to copy
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100191
Bram Moolenaar2b570782008-05-07 15:40:33 +0000192$VIM -f -u NONE -c 'so $VIMRUNTIME/tutor/tutor.vim'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar527dec32018-04-12 20:36:43 +0200194# Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200195$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"