#!/bin/sh -e
# Generated by patch2vi from unified diff
# Available separators: 0003-0010 0013-0014 0016-0037 0110 0112 0120-0121 0124 0126-0130 0132 0136 0175-0177 0201-0205 0207-0211 0216-0221 0223 0225-0227 0231-0232 0234-0236 0240 0242-0250 0252 0254-0256 0260-0272 0274-0275 0277-0301 0303-0327 0334-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: conf.c led.c vi.h
# 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> /\\* how to highlight text in the reverse direction \\*/
const int conf_hlrev = SYN_BGMK\\(8\\);

/\\* right-to-left characters \\*/
#define CR2L		\"ء-يپچژکگی‌-‍؛،»«؟ً-ْٔ\"
/\\* neutral characters \\*/${ESC}${SEP}1??${ESC}${SEP}${LB}
${ESC}${SEP}1??+2m 1${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}%f> /\\* how to highlight text in the reverse direction \\*/
const int conf_hlrev = SYN_BGMK\\(8\\);

${ESC}${SEP}3??${ESC}${SEP}${LB}
${ESC}${SEP}3??+2m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK conf.c:327:a3${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> 	\\{msg_ft, \"\\.\\+\", A\\(AY1 \\| SYN_BD\\)},
};
const int hlslen = LEN\\(hls\\);.*(struct dircontext dctxs\\[] = \\{)
	\\{\"\\^\\[\" CR2L \"]\", -1},
	\\{\"\\^\\[a-zA-Z_0-9]\", \\+1},${ESC}${SEP}8??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}8??-6m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK conf.c:327: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> 	\\{bar_ft, \"\\^\\(\\\\\"\\.\\*\\\\\"\\)\\.\\* \\(\\[0-9]\\{1,3}%\\) \\(L\\[0-9]\\+\\) \\(C\\[0-9]\\+\\) \\(B-\\?\\[0-9]\\+\\)\\?\\.\\*\\\$\",
		A\\(AY1 \\| SYN_BD, BL, RE1, BL, YE1, GR\\)},
	\\{bar_ft, \"\\^\\.\\*\\\$\", A\\(AY1 \\| SYN_BD\\)},.*(struct dirmark dmarks\\[] = \\{)
	\\{\"\\[\" CR2L \"]\\[\" CNEUT CR2L \"]\\*\\[\" CR2L \"]\", \\+1, \\{-1}},
	\\{\"\\^\\(\\[ \\\\t]\\+\\)\\?\\(\\[\" CNEUT \"]\\*\\[\\^\" CR2L \"]\\*\\[\\^\" CR2L CNEUT \"]\\(\\?:\\[\" CNEUT \"]\\+\\\$\\)\\?\\)\", -1, \\{0, 1, -1}},${ESC}${SEP}9??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}9??-12m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK conf.c:327:a9${ESC}${SEP}'0${SEP}${LB}
${SEP}1;3;8;9??!${DBG1:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL conf.c:327${ESC}${SEP}pr${INTR}${QF1}}${SEP}${LB}
${SEP}${LB}
${SEP}'1i /* cursor shape escapes (DECSCUSR): set on entering insert, reset on leaving */
char conf_curins[] = \"\\x1b[5 q\";	/* insert mode: vertical bar */
char conf_curnorm[] = \"\\x1b[2 q\";	/* normal mode: block */

${SEP}??!${DBG2:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL conf.c:327:m1${ESC}${SEP}pr${INTR}${QF2}}${SEP}b1${SEP}%ya 98${SEP}?${ESC}${SEP}${LB}
${ESC}${SEP}%f> 	int n, key, ps = 0, crow = xrow, ctop = xtop;
	char \\*postref = NULL;
	ins_state is;
	while \\(1\\) \\{
		ins_init\\(is\\)
		key = led_line\\(sb, ps, sb->s_n, &post, postn, &postref,${ESC}${SEP}1??${ESC}${SEP}${LB}
${ESC}${SEP}1??+2m 1${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}%f> 	int n, key, ps = 0, crow = xrow, ctop = xtop;
	char \\*postref = NULL;
	ins_state is;${ESC}${SEP}3??${ESC}${SEP}${LB}
${ESC}${SEP}3??+2m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:689:a3${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}grp 1${ESC}${SEP}%f> 	int n, key, ps = 0, crow = xrow, ctop = xtop;.*?
	char \\*postref = NULL;.*?
(	ins_state is;)${ESC}${SEP}7??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}7??m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:689:a7${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> int led_input\\(sbuf \\*sb, char \\*post, int postn, int row, int flg, int \\*pren\\)
\\{
	int ai_max = 128 \\* xai;.*(				sbufn_str\\(sb, post\\))
			} else
				sb->s\\[\\*pren] = \\*post;${ESC}${SEP}8??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}8??-9m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:689: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> 	}
	return key;
}.*(			free\\(postref\\);)
			xrow = crow;
			return key;${ESC}${SEP}9??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}9??-12m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:689:a9${ESC}${SEP}'0${SEP}${LB}
${SEP}1;3;7;8;9??!${DBG1:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL led.c:689${ESC}${SEP}pr${INTR}${QF1}}${SEP}${LB}
${SEP}?${ESC}${SEP}${LB}
${ESC}${SEP}%f+ 		key = led_line\\(sb, ps, sb->s_n, &post, postn, &postref,
			ai_max, &xoff, &xkmap, &is, row, crow, ctop, flg\\);
		if \\(key != '\\\\n'\\) \\{
			\\*pren = sb->s_n;
			if \\(!xled\\) \\{
				xoff = uc_slen\\(sb->s\\+ps\\);${ESC}${SEP}1??${ESC}${SEP}${LB}
${ESC}${SEP}1??+2m 2${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}%f+ 		key = led_line\\(sb, ps, sb->s_n, &post, postn, &postref,
			ai_max, &xoff, &xkmap, &is, row, crow, ctop, flg\\);
		if \\(key != '\\\\n'\\) \\{${ESC}${SEP}3??${ESC}${SEP}${LB}
${ESC}${SEP}3??+2m 2${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:694:a3${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}grp 1${ESC}${SEP}%f+ 		key = led_line\\(sb, ps, sb->s_n, &post, postn, &postref,.*?
			ai_max, &xoff, &xkmap, &is, row, crow, ctop, flg\\);.*?
(		if \\(key != '\\\\n'\\) \\{)${ESC}${SEP}7??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}7??m 2${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:694:a7${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> int led_input\\(sbuf \\*sb, char \\*post, int postn, int row, int flg, int \\*pren\\)
\\{
	int ai_max = 128 \\* xai;.*(				sbufn_str\\(sb, post\\))
			} else
				sb->s\\[\\*pren] = \\*post;${ESC}${SEP}8??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}8??-4m 2${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:694: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> 	}
	return key;
}.*(			free\\(postref\\);)
			xrow = crow;
			return key;${ESC}${SEP}9??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}9??-7m 2${ESC}${ESC}${ESC}${SEP}${OK1}p OK led.c:694:a9${ESC}${SEP}'0${SEP}${LB}
${SEP}1;3;7;8;9??!${DBG1:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL led.c:694${ESC}${SEP}pr${INTR}${QF1}}${SEP}${LB}
${SEP}${LB}
${SEP}'1i 	term_write(conf_curins, sizeof(conf_curins) - 1)
${SEP}??!${DBG2:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL led.c:689:m1${ESC}${SEP}pr${INTR}${QF2}}${SEP}${LB}
${SEP}'2i 			term_write(conf_curnorm, sizeof(conf_curnorm) - 1)
${SEP}??!${DBG2:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL led.c:694:m2${ESC}${SEP}pr${INTR}${QF2}}${SEP}b2${SEP}%ya 98${SEP}?${ESC}${SEP}${LB}
${ESC}${SEP}%f> extern struct placeholder \\*ph;
extern int phlen;
extern const int conf_hlrev;
char \\*\\*conf_kmap\\(int id\\);
int conf_kmapfind\\(char \\*name\\);
char \\*conf_digraph\\(int c1, int c2\\);${ESC}${SEP}1??${ESC}${SEP}${LB}
${ESC}${SEP}1??+2m 1${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}%f> extern struct placeholder \\*ph;
extern int phlen;
extern const int conf_hlrev;${ESC}${SEP}3??${ESC}${SEP}${LB}
${ESC}${SEP}3??+2m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.h:539:a3${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}grp 1${ESC}${SEP}%f> extern struct placeholder \\*ph;.*?
extern int phlen;.*?
(extern const int conf_hlrev;)${ESC}${SEP}7??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}7??m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.h:539:a7${ESC}${ESC}${ESC}${SEP}1q${ESC}${SEP}${LB}
${ESC}${SEP}m 0${ESC}${SEP}1;0${ESC}${SEP}grp 1${ESC}${SEP}%f> 	int l;		/\\* the length of the codepoint \\*/
};
extern struct placeholder _ph\\[];.*(/\\* vi\\.c: main \\*/)
void vi\\(int init\\);
extern int vi_hidch;${ESC}${SEP}8??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}8??-5m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.h:539: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> 	int cp\\[2];	/\\* the source character codepoint \\*/
	char d\\[8];	/\\* the placeholder \\*/
	int wid;	/\\* the width of the placeholder \\*/.*(extern int vi_lncol;)
/\\* filesystem \\*/
extern rset \\*fsincl;${ESC}${SEP}9??${ESC}${SEP}${LB}
${ESC}${SEP}grp 0${ESC}${SEP}9??-8m 1${ESC}${ESC}${ESC}${SEP}${OK1}p OK vi.h:539:a9${ESC}${SEP}'0${SEP}${LB}
${SEP}1;3;7;8;9??!${DBG1:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL vi.h:539${ESC}${SEP}pr${INTR}${QF1}}${SEP}${LB}
${SEP}${LB}
${SEP}'1i extern char conf_curins[];
extern char conf_curnorm[];
${SEP}??!${DBG2:-ya!112${ESC}${SEP}prp${ESC}${SEP}p FAIL vi.h:539:m1${ESC}${SEP}pr${INTR}${QF2}}${SEP}vis 2${SEP}b0${SEP}w${SEP}b1${SEP}w${SEP}b2${SEP}w${SEP}2q" > "$P2VIF"
EXINIT='%ya 97:? %@97' $VI -e 'conf.c' 'led.c' 'vi.h' "$P2VIF"

exit 0
=== PATCH2VI DELTA ===
=== PATCH2VI PATCH ===
diff --git a/conf.c b/conf.c
index 70157040..aa7c5648 100644
--- a/conf.c
+++ b/conf.c
@@ -325,6 +325,10 @@ const int hlslen = LEN(hls);
 /* how to highlight text in the reverse direction */
 const int conf_hlrev = SYN_BGMK(8);
 
+/* cursor shape escapes (DECSCUSR): set on entering insert, reset on leaving */
+char conf_curins[] = "\x1b[5 q";	/* insert mode: vertical bar */
+char conf_curnorm[] = "\x1b[2 q";	/* normal mode: block */
+
 /* right-to-left characters */
 #define CR2L		"ء-يپچژکگی‌-‍؛،»«؟ً-ْٔ"
 /* neutral characters */
diff --git a/led.c b/led.c
index 96d2e3ba..a9a1b347 100644
--- a/led.c
+++ b/led.c
@@ -687,11 +687,13 @@ int led_input(sbuf *sb, char *post, int postn, int row, int flg, int *pren)
 	int n, key, ps = 0, crow = xrow, ctop = xtop;
 	char *postref = NULL;
 	ins_state is;
+	term_write(conf_curins, sizeof(conf_curins) - 1)
 	while (1) {
 		ins_init(is)
 		key = led_line(sb, ps, sb->s_n, &post, postn, &postref,
 			ai_max, &xoff, &xkmap, &is, row, crow, ctop, flg);
 		if (key != '\n') {
+			term_write(conf_curnorm, sizeof(conf_curnorm) - 1)
 			*pren = sb->s_n;
 			if (!xled) {
 				xoff = uc_slen(sb->s+ps);
diff --git a/vi.h b/vi.h
index 6466711d..5d315610 100644
--- a/vi.h
+++ b/vi.h
@@ -537,6 +537,8 @@ extern struct placeholder _ph[];
 extern struct placeholder *ph;
 extern int phlen;
 extern const int conf_hlrev;
+extern char conf_curins[];
+extern char conf_curnorm[];
 char **conf_kmap(int id);
 int conf_kmapfind(char *name);
 char *conf_digraph(int c1, int c2);
