blob: 3faf100b4d6dee42850881047484273e5208e1ee [file] [log] [blame]
Paul Desmond Parker17c71da2024-11-03 20:47:53 +01001#!/bin/bash
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.
Bram Moolenaarbbe917d2016-09-21 22:41:24 +020014# We anticipate up to a future Vim 8.1 version :-).
15seq="vim 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========================================================================================="
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=01) is one or two digits representing the chapter number\n"
24 printf "\texamples:\n"
25 printf "\t\tvimtutor -l es -c 2 -g\n"
26 printf "\t\tvimtutor --language de --chapter 02\n"
27 printf "\t\tvimtutor fr\n"
28 echo "More information at 'man vimtutor'"
29 echo "================================================================================================"
30}
31
32listOptions()
33{
34 declare -A language
35 language[bar]=Bavarian
36 language[bg]=Bulgarian
37 language[ca]=Catalan
38 language[cs]=Czech
39 language[da]=Danish
40 language[de]=German
41 language[el]=Greek
42 language[en]=English\(default\)
43 language[eo]=Esperanto
44 language[es]=Spanish
45 language[fr]=French
46 language[hr]=Croatian
47 language[hu]=Hungarian
48 language[it]=Italian
49 language[ja]=Japanese
50 language[ko]=Korean
51 language[lv]=Latvian
52 language[nb]=Bokmål
53 language[nl]=Dutch
54 language[no]=Norwegian
55 language[pl]=Polish
56 language[pt]=Portuguese
57 language[ru]=Russian
58 language[sk]=Slovak
59 language[sr]=Serbian
60 language[sv]=Swedish
61 language[tr]=Turkish
62 language[uk]=English
63 language[vi]=Vietnamese
64 language[zh]=Chinese
65
66 echo "==OPTIONS======================================================================================="
67 echo "Chapter: 1"
68 for code in bar bg ca cs da de el en eo es fr hr hu it ja ko lv nb nl no pl pt ru sk sr sv tr uk vi zh
69 do
70 printf "\tLang: %s => %s\n" ${code} ${language[${code}]}
71 done
72 echo "Chapter: 2"
73 for code in en
74 do
75 printf "\tLang: %s => %s\n" ${code} ${language[${code}]}
76 done
77 echo "================================================================================================"
78}
79
80validateLang()
81{
82 if [[ $xx =~ ^[^a-z]*$ ]]; then echo "Error: iso639 code must contain only [a-z]" && exit 1; fi
83 if [ ${#xx} == 2 ] || [ ${#xx} == 3 ]; then :; else echo "Error: iso639 code must be 2 or 3 characters only" && exit 1; fi
84 export xx
85}
86
87validateChapter()
88{
89 if [[ $cc =~ ^[0-9]*$ ]]; then :; else echo "Error: chapter argument must contain digits only" && exit 1; fi
90 if [ $cc == "0" ]; then echo "Error: chapter must be non-zero" && exit 1; fi
91 if [ $cc == "00" ]; then echo "Error: chapter must be non-zero" && exit 1; fi
92 export CHAPTER="$cc"
93}
94
95shopt -s extglob
96while [ "$1" != "" ]; do
97 case $1 in
98 -g | --gui ) seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
99 ;;
100 -l | --language ) shift
101 xx=$1
102 validateLang
103 ;;
104 -l[a-z][a-z]?([a-z]) ) xx=${1#*l}
105 validateLang
106 ;;
107 --language[a-z][a-z]?([a-z]) ) xx=${1#*e}
108 validateLang
109 ;;
110 [a-z][a-z]?([a-z]) ) xx=$1
111 validateLang
112 ;;
113 -c | --chapter ) shift
114 cc=$1
115 validateChapter
116 ;;
117 -c[0-9]?([0-9]) ) cc=${1#*c}
118 validateChapter
119 ;;
120 --chapter[0-9]?([0-9]) ) cc=${1#*r}
121 validateChapter
122 ;;
123 -h | --help ) usage
124 exit
125 ;;
126 --list ) listOptions
127 exit
128 ;;
129 "" ) ;;
130 * ) usage
131 exit 1
132 esac
133 shift
134done
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137# We need a temp file for the copy. First try using a standard command.
138tmp="${TMPDIR-/tmp}"
139TUTORCOPY=`mktemp $tmp/tutorXXXXXX || tempfile -p tutor || echo none`
140
141# If the standard commands failed then create a directory to put the copy in.
142# That is a secure way to make a temp file.
143if test "$TUTORCOPY" = none; then
144 tmpdir=$tmp/vimtutor$$
145 OLD_UMASK=`umask`
146 umask 077
147 getout=no
148 mkdir $tmpdir || getout=yes
149 umask $OLD_UMASK
150 if test $getout = yes; then
151 echo "Could not create directory for tutor copy, exiting."
152 exit 1
153 fi
154 TUTORCOPY=$tmpdir/tutorcopy
155 touch $TUTORCOPY
156 TODELETE=$tmpdir
157else
158 TODELETE=$TUTORCOPY
159fi
160
161export TUTORCOPY
162
163# remove the copy of the tutor on exit
164trap "rm -rf $TODELETE" 0 1 2 3 9 11 13 15
165
Bram Moolenaar13153492007-07-17 12:33:46 +0000166for i in $seq; do
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200167 testvim=$(which $i 2>/dev/null)
168 if test -f "$testvim"; then
169 VIM=$i
170 break
171 fi
Bram Moolenaar13153492007-07-17 12:33:46 +0000172done
173
174# When no Vim version was found fall back to "vim", you'll get an error message
175# below.
176if test -z "$VIM"; then
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200177 VIM=vim
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178fi
179
180# Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
181# The script tutor.vim tells Vim which file to copy
Paul Desmond Parker17c71da2024-11-03 20:47:53 +0100182
Bram Moolenaar2b570782008-05-07 15:40:33 +0000183$VIM -f -u NONE -c 'so $VIMRUNTIME/tutor/tutor.vim'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
Bram Moolenaar527dec32018-04-12 20:36:43 +0200185# Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
Bram Moolenaar89a9c152021-08-29 21:55:35 +0200186$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"