1
0
forked from aniani/vim

patch 9.1.0845: vimtutor shell script can be improved

Problem:  vimtutor shell script can be improved
Solution: further improve the vimtutor shell script
          (Aliaksei Budavei)

- Rewrite the script usage note.
- Reconcile the usage help output with the manual page entry
  that describes the implementation in that:
  * a language code argument can be used alone or with its
    option key, e.g. "vimtutor -l nl" or "vimtutor nl";
  * a chapter number argument cannot be used without its
    option key, e.g. "vimtutor -c 2".
- Accept only chapters 1 or 2 as valid chapter arguments.
- Double-quote instances of shell parameter expansion where
  neither pathname expansion nor field splitting is desired.
- Prefer "$(foo)" to "`foo`" for command substitution.
- Follow a single indentation style (see the modeline).

closes: #15992

Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Aliaksei Budavei
2024-11-06 21:58:53 +01:00
committed by Christian Brabandt
parent ea19e7856b
commit 715a58fda6
2 changed files with 104 additions and 86 deletions

View File

@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
845,
/**/ /**/
844, 844,
/**/ /**/

View File

@@ -2,12 +2,13 @@
# Start Vim on a copy of the tutor file. # Start Vim on a copy of the tutor file.
# Usage: vimtutor [-g] [xx] # Type "man vimtutor" (or "vimtutor --help") to learn more about the supported
# Where optional argument -g starts vimtutor in gvim (GUI) instead of vim. # command-line options.
# and xx is a language code like "es" or "nl". #
# When an argument is given, it tries loading that tutor. # Tutors in several human languages are distributed. Type "vimtutor" to use
# When this fails or no argument was given, it tries using 'v:lang' # a tutor in the language of the current locale (:help v:lang), if available;
# When that also fails, it uses the English version. # otherwise fall back to using the English tutor. To request any bundled
# tutor, specify its ISO639 name as an argument, e.g. "vimtutor nl".
# Vim could be called "vim" or "vi". Also check for "vimN", for people who # Vim could be called "vim" or "vi". Also check for "vimN", for people who
# have Vim installed with its version number. # have Vim installed with its version number.
@@ -16,13 +17,13 @@ seq="vim vim91 vim90 vim81 vim80 vim8 vim74 vim73 vim72 vim71 vim70 vim7 vim6 vi
usage() usage()
{ {
echo "==USAGE=========================================================================================" echo "==USAGE========================================================================================="
echo "${0##*/} [-(-l)anguage ISO639] [-(-c)hapter) NUMBER] [-(-g)ui] | [-(-h)elp] | [--list]" echo "${0##*/} [[-(-l)anguage] ISO639] [-(-c)hapter NUMBER] [-(-g)ui] | [-(-h)elp] | [--list]"
printf "\twhere:\n" printf "\twhere:\n"
printf "\t\tISO639 (default=en) is a 2 or 3 character language code\n" printf "\t\tISO639 (default=en) is a 2 or 3 character language code\n"
printf "\t\tNUMBER (default=01) is one or two digits representing the chapter number\n" printf "\t\tNUMBER (default=1) is a chapter number (1 or 2)\n"
printf "\texamples:\n" printf "\texamples:\n"
printf "\t\tvimtutor -l es -c 2 -g\n" printf "\t\tvimtutor -l es -c 2 -g\n"
printf "\t\tvimtutor --language de --chapter 02\n" printf "\t\tvimtutor --language de --chapter 2\n"
printf "\t\tvimtutor fr\n" printf "\t\tvimtutor fr\n"
echo "More information at 'man vimtutor'" echo "More information at 'man vimtutor'"
echo "================================================================================================" echo "================================================================================================"
@@ -96,8 +97,10 @@ validateChapter()
echo "Error: chapter argument must contain digits only" echo "Error: chapter argument must contain digits only"
exit 1 exit 1
;; ;;
0 | 00 ) [12] )
echo "Error: chapter must be non-zero" ;;
* )
echo "Error: invalid chapter number: [12]"
exit 1 exit 1
esac esac
@@ -106,9 +109,11 @@ validateChapter()
while [ "$1" != "" ]; do while [ "$1" != "" ]; do
case "$1" in case "$1" in
-g | --gui ) seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq" -g | --gui )
seq="gvim gvim91 gvim90 gvim81 gvim80 gvim8 gvim74 gvim73 gvim72 gvim71 gvim70 gvim7 gvim6 $seq"
;; ;;
-l | --language ) shift -l | --language )
shift
xx="$1" xx="$1"
validateLang validateLang
;; ;;
@@ -118,64 +123,73 @@ while [ "$1" != "" ]; do
--language[a-z][a-z][a-z] | --language[a-z][a-z] ) --language[a-z][a-z][a-z] | --language[a-z][a-z] )
export xx="${1#*e}" export xx="${1#*e}"
;; ;;
[a-z][a-z][a-z] | [a-z][a-z] ) export xx="$1" [a-z][a-z][a-z] | [a-z][a-z] )
export xx="$1"
;; ;;
-c | --chapter ) shift -c | --chapter )
shift
cc="$1" cc="$1"
validateChapter validateChapter
;; ;;
-c[1-9][0-9] | -c[1-9] ) export CHAPTER="${1#*c}" -c[12] )
export CHAPTER="${1#*c}"
;; ;;
--chapter[1-9][0-9] | --chapter[1-9] ) --chapter[12] )
export CHAPTER="${1#*r}" export CHAPTER="${1#*r}"
;; ;;
-h | --help ) usage -h | --help )
usage
exit exit
;; ;;
--list ) listOptions --list )
listOptions
exit exit
;; ;;
"" ) ;; "" )
* ) usage ;;
* )
usage
exit 1 exit 1
esac esac
shift shift
done done
# We need a temp file for the copy. First try using a standard command. # We need a temp file for the copy. First try using a standard command.
tmp="${TMPDIR-/tmp}" tmp="${TMPDIR-/tmp}"
TUTORCOPY=`mktemp $tmp/tutorXXXXXX || tempfile -p tutor || echo none` # shellcheck disable=SC2186
TUTORCOPY=$(mktemp "$tmp/tutorXXXXXX" || tempfile -p tutor || echo none)
# If the standard commands failed then create a directory to put the copy in. # If the standard commands failed then create a directory to put the copy in.
# That is a secure way to make a temp file. # That is a secure way to make a temp file.
if test "$TUTORCOPY" = none; then if test "$TUTORCOPY" = none; then
tmpdir=$tmp/vimtutor$$ tmpdir="$tmp/vimtutor$$"
OLD_UMASK=`umask` OLD_UMASK=$(umask)
umask 077 umask 077
getout=no getout=no
mkdir $tmpdir || getout=yes mkdir "$tmpdir" || getout=yes
umask $OLD_UMASK umask "$OLD_UMASK"
if test $getout = yes; then if test "$getout" = yes; then
echo "Could not create directory for tutor copy, exiting." echo "Could not create directory for tutor copy, exiting."
exit 1 exit 1
fi fi
TUTORCOPY=$tmpdir/tutorcopy TUTORCOPY="$tmpdir/tutorcopy"
touch $TUTORCOPY touch "$TUTORCOPY"
TODELETE=$tmpdir TODELETE="$tmpdir"
else else
TODELETE=$TUTORCOPY TODELETE="$TUTORCOPY"
fi fi
export TUTORCOPY export TUTORCOPY
# remove the copy of the tutor on exit # remove the copy of the tutor on exit
trap "rm -rf $TODELETE" EXIT HUP INT QUIT SEGV PIPE TERM trap 'rm -rf "$TODELETE"' EXIT HUP INT QUIT SEGV PIPE TERM
for i in $seq; do for i in $seq; do
testvim=$(command -v "$i" 2>/dev/null) testvim=$(command -v "$i" 2>/dev/null)
if test -f "$testvim"; then if test -f "$testvim"; then
VIM=$i VIM="$i"
break break
fi fi
done done
@@ -189,7 +203,9 @@ fi
# Use Vim to copy the tutor, it knows the value of $VIMRUNTIME # Use Vim to copy the tutor, it knows the value of $VIMRUNTIME
# The script tutor.vim tells Vim which file to copy # The script tutor.vim tells Vim which file to copy
$VIM -f -u NONE -c 'so $VIMRUNTIME/tutor/tutor.vim' $VIM -f -u NONE -c "so \$VIMRUNTIME/tutor/tutor.vim"
# Start vim without any .vimrc, set 'nocompatible' and 'showcmd' # Start vim without any .vimrc, set 'nocompatible' and 'showcmd'
$VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY" $VIM -f -u NONE -c "set nocp showcmd" "$TUTORCOPY"
# vim:sw=4:ts=8:noet:nosta: