@@ -811,6 +811,14 @@ edit(
811811 do
812812 {
813813 c = safe_vgetc ();
814+
815+ if (stop_insert_mode )
816+ {
817+ // Insert mode ended, possibly from a callback.
818+ count = 0 ;
819+ nomove = TRUE;
820+ goto doESCkey ;
821+ }
814822 } while (c == K_IGNORE || c == K_NOP );
815823
816824 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
@@ -1165,6 +1173,18 @@ edit(
11651173 break ;
11661174
11671175 case Ctrl_W : /* delete word before the cursor */
1176+ #ifdef FEAT_JOB_CHANNEL
1177+ if (bt_prompt (curbuf ) && (mod_mask & MOD_MASK_SHIFT ) == 0 )
1178+ {
1179+ // In a prompt window CTRL-W is used for window commands.
1180+ // Use Shift-CTRL-W to delete a word.
1181+ stuffcharReadbuff (Ctrl_W );
1182+ restart_edit = 'i' ;
1183+ nomove = TRUE;
1184+ count = 0 ;
1185+ goto doESCkey ;
1186+ }
1187+ #endif
11681188 did_backspace = ins_bs (c , BACKSPACE_WORD , & inserted_space );
11691189 auto_format (FALSE, TRUE);
11701190 break ;
@@ -1400,11 +1420,10 @@ edit(
14001420#ifdef FEAT_JOB_CHANNEL
14011421 if (bt_prompt (curbuf ))
14021422 {
1403- buf_T * buf = curbuf ;
1404-
14051423 invoke_prompt_callback ();
1406- if (curbuf != buf )
1407- // buffer changed, get out of Insert mode
1424+ if (!bt_prompt (curbuf ))
1425+ // buffer changed to a non-prompt buffer, get out of
1426+ // Insert mode
14081427 goto doESCkey ;
14091428 break ;
14101429 }
@@ -1878,10 +1897,25 @@ init_prompt(int cmdchar_todo)
18781897 coladvance ((colnr_T )MAXCOL );
18791898 changed_bytes (curbuf -> b_ml .ml_line_count , 0 );
18801899 }
1900+
1901+ // Insert always starts after the prompt, allow editing text after it.
1902+ if (Insstart_orig .lnum != curwin -> w_cursor .lnum
1903+ || Insstart_orig .col != (int )STRLEN (prompt ))
1904+ {
1905+ Insstart .lnum = curwin -> w_cursor .lnum ;
1906+ Insstart .col = STRLEN (prompt );
1907+ Insstart_orig = Insstart ;
1908+ Insstart_textlen = Insstart .col ;
1909+ Insstart_blank_vcol = MAXCOL ;
1910+ arrow_used = FALSE;
1911+ }
1912+
18811913 if (cmdchar_todo == 'A' )
18821914 coladvance ((colnr_T )MAXCOL );
18831915 if (cmdchar_todo == 'I' || curwin -> w_cursor .col <= (int )STRLEN (prompt ))
18841916 curwin -> w_cursor .col = STRLEN (prompt );
1917+ /* Make sure the cursor is in a valid position. */
1918+ check_cursor ();
18851919}
18861920
18871921/*
@@ -9448,7 +9482,7 @@ ins_bs(
94489482
94499483 /* If deleted before the insertion point, adjust it */
94509484 if (curwin -> w_cursor .lnum == Insstart_orig .lnum
9451- && curwin -> w_cursor .col < Insstart_orig .col )
9485+ && curwin -> w_cursor .col < Insstart_orig .col )
94529486 Insstart_orig .col = curwin -> w_cursor .col ;
94539487
94549488 /* vi behaviour: the cursor moves backward but the character that
@@ -9498,6 +9532,11 @@ ins_mouse(int c)
94989532 * previous one to stop insert there properly. */
94999533 curwin = old_curwin ;
95009534 curbuf = curwin -> w_buffer ;
9535+ #ifdef FEAT_JOB_CHANNEL
9536+ if (bt_prompt (curbuf ))
9537+ // Restart Insert mode when re-entering the prompt buffer.
9538+ curbuf -> b_prompt_insert = 'A' ;
9539+ #endif
95019540 }
95029541 start_arrow (curwin == old_curwin ? & tpos : NULL );
95039542 if (curwin != new_curwin && win_valid (new_curwin ))
0 commit comments