#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

#
# Check that special keys work.
#
# The special keys that xvi recognises are listed in ascii.h:
#	K_UNDO K_UARROW K_DARROW K_LARROW K_RARROW
#	K_CGRAVE K_PGDOWN K_PGUP K_HOME K_END K_INSERT K_DELETE
# F1 is used to give help (K_HELP), F2 to F9 give #2 to #9 and
# F0 and F10 give #0.
#
# Check that they do something reasonable in each input mode:
#	normal, insert, replace, cmdline, display
#
# Was https://github.com/martinwguy/xvi/issues/115
#

source scripts/term
start_vi

# Tests begin

# Test pattern: a 5x5 grid that we sit in the middle of
# to be able to distinguish one-char motions from
# those that go to the start and end of line/screen.

set pattern [list "abcde" "fghij" "klmno" "pqrst" "uvwxy" "~" ]

test 100 "aabcde\rfghij\rklmno\rpqrst\ruvwxy[esc]kkhh" 3 2 $pattern

#
# Normal mode
#

test 10 "[left]"   3 1 $pattern
test 11 "[right]"  3 2 $pattern
test 12 "[up]"     2 2 $pattern
test 13 "[down]"   3 2 $pattern
test 14 "[home]"   3 0 $pattern
test 15 "[end]"    3 4 $pattern
test 16 "hh[esc]"  3 2 $pattern		;# Reset test pattern
test 17 "[insert]X" 3 3 [list "abcde" "fghij" "klXmno" "pqrst" "uvwxy" "~" ]
test 18 "[esc]x"   3 2 $pattern	;# Reset test pattern

#
# Insert mode
#

# In insert node, special keys should do nothing.

test 19 "i[left]"				3 2 $pattern
test 20 "[left][up][pgup][home][insert]"	3 2 $pattern
test 21 "[right][down][pgdown][end][delete]"	3 2 $pattern

# F1 to F10 insert #1 to #0
test 31 "\b[esc]1GdGaabc[esc]hi" 1 1 "abc"
test 39 "[f1][f2][f3][f4][f5][f6][f7][f8][f9][f10][esc]" 1 20 "a#1#2#3#4#5#6#7#8#9#0bc"

test 100 "1GdGaabcde\rfghij\rklmno\rpqrst\ruvwxy[esc]kkhh" 3 2 $pattern

#
# Replace mode
#

# In replace mode, special keys should do nothing.
test 40 "r[left][esc]" 3 2 $pattern
#test 41 "r[right][esc]\
r[up][esc]\
r[down][esc]\
r[pgdown][esc]\
r[pgup][esc]\
r[home][esc]\
r[end][esc]\
r[insert][esc]\
r[delete][esc]	3 2 $pattern
test 41 "r[right][esc]r[up][esc]r[down][esc]r[pgdown][esc]r[pgup][esc]r[home][esc]r[end][esc]r[insert][esc]r[delete][esc]" \
	3 2 $pattern
# Function keys in single-char replace mode say "#5", replace with # and poke a
# 5 into the input stream. Tough.

#
# Command line mode
#

# In cmdline mode, left and right arrow do simple line editing
# delete cancels the character under the cursor if not at end of line,
# home and end go to the start and the end of the line,
# other special keys should be ignored except for the function keys giving #N.
ctest 60 ":abc"		4 ":abc"
ctest 61 "[left]"	3 ":abc"	;# line editing motions
ctest 61 "[right]"	4 ":abc"
ctest 62 "[up]"	4 ":abc"	;# Does nothing
ctest 63 "[down]"	4 ":abc"	;# Does nothing
ctest 64 "[home]"	1 ":abc"
ctest 65 "[end]"	4 ":abc"
ctest 66 "[pgdown][pgup][insert]"	4 ":abc"	;# These do nothing

# Delete at end of line does nothing
ctest 70 "[delete]"	4 ":abc"
# Delete on the last char cancels it
ctest 71 "[left][delete]" 3 ":ab"
# Delete on the penultimate char leaves the last one
ctest 72 "[left][left][delete]" 1 ":b"

# Restore test pattern and sit on the b
ctest 79 "[left]a[right]c[left][left]" 	 2 ":abc"
# Check that function keys insert their magic strings
ctest 80 "[f1][f2][f3][f4][f5][f6][f7][f8][f9][f10]" 22 ":a#1#2#3#4#5#6#7#8#9#0bc"

stop_vi

exit 0

