#!/bin/sh -e
# Generated by patch2vi from unified diff
# Available separators: 0003-0010 0013-0014 0016-0037 0105 0107-0110 0112 0120-0127 0131-0132 0136 0172 0176-0205 0207-0217 0224-0341 0343-0377

# Pass any argument to use patch(1) instead of nextvi ex commands
if [ -n "$1" ]; then
    sed '1,/^=== PATCH2VI PATCH ===$/d' "$0" | patch -p1 --merge=diff3
    exit $?
fi

VI=${VI:-vi}
if ! $VI -? 2>&1 | grep -q 'Nextvi'; then
    echo "Error: $VI is not nextvi" >&2
    echo "Set VI environment variable to point to nextvi binary" >&2
    exit 1
fi

SEP="$(printf '\001')"
ESC="$(printf '\002')"
# Command that handles readability line breaks
LB="0?"
# Phase 1 (search/mark): errors disabled by default,
# DBG1=1 enables error reporting, QF1=1 quits on failure
# OK1: with DBG1=1 also report fallback anchor successes
[ "$DBG1" = "1" ] && OK1= || OK1="0?"
[ "$DBG1" = "1" ] && DBG1= || DBG1="0?"
[ "$QF1" = "1" ] && QF1="${ESC}${SEP}vis 2${ESC}${SEP}q!1" || QF1=
# Phase 2 (edits): DBG2=1 disables errors, QF2=1 ignores them
# OK2: with DBG2= also report fallback substitute successes
[ "$DBG2" = "1" ] && OK2="0?" || OK2=
[ "$DBG2" = "1" ] && DBG2="0?" || DBG2=
[ "$QF2" = "1" ] && QF2= || QF2="${ESC}${SEP}vis 2${ESC}${SEP}q!1"
# Enters vi at failing code line in this script
# Designed for state inspection mid execution
[ "$INTR" = "1" ] && INTR="${ESC}${SEP}|sc|${ESC}${SEP}vis 2:fr 0:e $0:83reg %@47:%f> %@112:&Q:b0:|sc! ${ESC}${ESC}${ESC}${SEP}|:vis 3${ESC}${SEP}q1" || INTR=

# Patch: vi.c
# Body too large for EXINIT/argv: stage it in a file
( : > /tmp/p2vi.$$ ) 2>/dev/null && P2VIF=/tmp/p2vi.$$ || P2VIF=./p2vi.$$
trap 'rm -f "$P2VIF"' EXIT
printf '%s\n' "|sc! ${ESC}${SEP}|:vis 3${SEP}fr 98${SEP}b0${SEP}%ya 98${SEP}?${ESC}${SEP}${LB}
${ESC}${SEP}%f> 
	mv = term_read\\(0\\);
	switch \\(mv\\) \\{
	case ',':
	case ';':
		if \\(!vi_charlast\\[0]\\)${ESC}${SEP}1??${ESC}${SEP}${LB}
${ESC}${SEP}1??+2m 1${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}%f> 
	mv = term_read\\(0\\);
	switch \\(mv\\) \\{${ESC}${SEP}3??${ESC}${SEP}${LB}
${ESC}${SEP}3??+2m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.c:527:a3${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}grp 1${ESC}${SEP}%f> .*?
	mv = term_read\\(0\\);.*?
(	switch \\(mv\\) \\{)${ESC}${SEP}7??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}7??m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.c:527:a7${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> 	char \\*cs;
	int cnt = vi_arg \\? vi_arg : 1;
	int mv, i, dir, var;.*(		if \\(mv == ','\\))
			mv = vi_charcmd == 'F' \\|\\| vi_charcmd == 'T'
				\\? tolower\\(vi_charcmd\\) : toupper\\(vi_charcmd\\);${ESC}${SEP}8??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}8??-5m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.c:527:a8${ESC}${SEP}'0${ESC}${SEP}8??${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> 	static rset \\*bre;
	static int srow\\[5], soff\\[5], lkwdcnt;
	static int cadir = 1;.*(			mv = vi_charcmd;)
		if \\(lbuf_findchar\\(xb, vi_charlast, mv, cnt, row, off\\)\\)
			return -1;${ESC}${SEP}9??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}9??-9m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.c:527:a9${ESC}${SEP}'0${SEP}${LB}
${SEP}1;3;7;8;9??!${DBG1:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL vi.c:527${ESC}${SEP}pr${INTR}${QF1}}${SEP}${LB}
${SEP}${LB}
${SEP}'1i 	case '\\033':	/* Arrow keys */
		mv = term_read(0);
		if (mv == '[') {
			mv = term_read(0);
			switch (mv) {
			case 'A':	/* ↑ */
				*row = MAX(*row - cnt, 0);
				mv = 'k';
				break;
			case 'B':	/* ↓ */
				*row = MIN(*row + cnt, lbuf_len(xb) - 1);
				mv = 'j';
				break;
			case 'D':	/* ← */
			case 'C':	/* → */
				if (!(cs = lbuf_get(xb, *row)))
					return -1;
				dir = dir_context(lbuf_get(xb, *row));
				if (mv == 'D')
					dir = -dir;
				for (i = 0; i < cnt; i++)
					if (vi_nextcol(cs, dir, off))
						break;
				mv = mv == 'D' ? 'h' : 'l';
				break;
			default:
				return 0;
			}
		} else	/* Not a 033[X command so we abort */
			return 0;
		break;
${SEP}??!${DBG2:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL vi.c:527:m1${ESC}${SEP}pr${INTR}${QF2}}${SEP}vis 2${SEP}b0${SEP}w${SEP}2q" > "$P2VIF"
EXINIT='%ya 97:? %@97' $VI -e 'vi.c' "$P2VIF"

exit 0
=== PATCH2VI DELTA ===
=== PATCH2VI PATCH ===
diff --git a/vi.c b/vi.c
index eecf2335..e25806bc 100644
--- a/vi.c
+++ b/vi.c
@@ -525,6 +525,37 @@ static int vi_region(int cmd, int *row, int *off)
 
 	mv = term_read(0);
 	switch (mv) {
+	case '\033':	/* Arrow keys */
+		mv = term_read(0);
+		if (mv == '[') {
+			mv = term_read(0);
+			switch (mv) {
+			case 'A':	/* ↑ */
+				*row = MAX(*row - cnt, 0);
+				mv = 'k';
+				break;
+			case 'B':	/* ↓ */
+				*row = MIN(*row + cnt, lbuf_len(xb) - 1);
+				mv = 'j';
+				break;
+			case 'D':	/* ← */
+			case 'C':	/* → */
+				if (!(cs = lbuf_get(xb, *row)))
+					return -1;
+				dir = dir_context(lbuf_get(xb, *row));
+				if (mv == 'D')
+					dir = -dir;
+				for (i = 0; i < cnt; i++)
+					if (vi_nextcol(cs, dir, off))
+						break;
+				mv = mv == 'D' ? 'h' : 'l';
+				break;
+			default:
+				return 0;
+			}
+		} else	/* Not a 033[X command so we abort */
+			return 0;
+		break;
 	case ',':
 	case ';':
 		if (!vi_charlast[0])
