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

#
# Test file for new Control-W and Control-U characters typed in insert mode
# to delete the last word of the current insertion, without wrapping logical
# lines, or the last line of the current insertion,
# both leaving autoindent chars if any were generated.
#
# Was https://github.com/martinwguy/xvi/issues/17

source scripts/term
start_vi

# Tests begin

# Check it started up OK
term_expect timeout { fail 100 } { screen_is 1 0 [list "" "~"] }

# Ctrl-U

test 101 "aabcde"		1 5 [list "abcde" "~"]

set r102 [list "" "~"]
if {$vi == "nvi"} {set r102 [list "abcde" "~"]} ;# With cursor at 1 0
test 102 [ctrl U]		1 0 $r102

# Ctrl-W

test 103 "abcde"		1 5 [list "abcde" "~"]
test 104 [ctrl W]		1 0 $r102

# Ctrl-W of one word should not take a space preceding what it gobbles
# but should take a trailing space.

test 105 "abc def"		1 7 [list "abc def" "~"]

set r106			    [list "abc" "~"]
if {$vi == "nvi"} { set r106	    [list "abc def" "~"] }
test 106 [ctrl W]		1 4 $r106

set r107			    [list "" "~"]
if {$vi == "nvi"} { set r107	    [list "abc def" "~"] }
test 107 [ctrl W]		1 0 $r107

# Check they only delete back to the insert point

test 108 "abc[esc]adef"		1 6 [list "abcdef" "~"]

set r109			    [list "abc" "~"]
if {$vi == "nvi"} { set r109	    [list "abcdef" "~"] }
test 109 [ctrl U]		1 3 $r109

test 110 "[esc]adef"		1 6 [list "abcdef" "~"]

set r111		  	    [list "abc" "~"]
if {$vi == "nvi"} { set r111	    [list "abcdef" "~"] }
test 111 [ctrl W]		1 3 $r111

# Check autoindent chars are kept

exp_send "[esc]:se ai\r"
test 112 "0Da  a[esc]o"		2 2 [list "  a" "" "~"]
test 113 "bb"			2 4 [list "  a" "  bb" "~"]

set r114			    [list "  a" "" "~"]
if {$vi == "nvi"} { set r114	    [list "  a" "  bb" "~"] }
test 114 [ctrl U]		2 2 $r114

# Check additional autoindents are kept. NVI fails both of these.
test 115 "[ctrl T]x" 		2 9 [list "  a" "        x" "~"]
test 116 "[ctrl W]b"		2 9 [list "  a" "        b" "~"]
test 117 "[ctrl U]c"		2 9 [list "  a" "        c" "~"]

exp_send "[esc]"

stop_vi

exit 0

