1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 /* 10 * normal.c: Contains the main routine for processing characters in command 11 * mode. Communicates closely with the code in ops.c to handle 12 * the operators. 13 */ 14 15 #include "vim.h" 16 17 static int VIsual_mode_orig = NUL; // saved Visual mode 18 19 #ifdef FEAT_EVAL 20 static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount); 21 #endif 22 static int nv_compare(const void *s1, const void *s2); 23 static void unshift_special(cmdarg_T *cap); 24 #ifdef FEAT_CMDL_INFO 25 static void del_from_showcmd(int); 26 #endif 27 28 /* 29 * nv_*(): functions called to handle Normal and Visual mode commands. 30 * n_*(): functions called to handle Normal mode commands. 31 * v_*(): functions called to handle Visual mode commands. 32 */ 33 static void nv_ignore(cmdarg_T *cap); 34 static void nv_nop(cmdarg_T *cap); 35 static void nv_error(cmdarg_T *cap); 36 static void nv_help(cmdarg_T *cap); 37 static void nv_addsub(cmdarg_T *cap); 38 static void nv_page(cmdarg_T *cap); 39 static void nv_zet(cmdarg_T *cap); 40 #ifdef FEAT_GUI 41 static void nv_ver_scrollbar(cmdarg_T *cap); 42 static void nv_hor_scrollbar(cmdarg_T *cap); 43 #endif 44 #ifdef FEAT_GUI_TABLINE 45 static void nv_tabline(cmdarg_T *cap); 46 static void nv_tabmenu(cmdarg_T *cap); 47 #endif 48 static void nv_exmode(cmdarg_T *cap); 49 static void nv_colon(cmdarg_T *cap); 50 static void nv_ctrlg(cmdarg_T *cap); 51 static void nv_ctrlh(cmdarg_T *cap); 52 static void nv_clear(cmdarg_T *cap); 53 static void nv_ctrlo(cmdarg_T *cap); 54 static void nv_hat(cmdarg_T *cap); 55 static void nv_Zet(cmdarg_T *cap); 56 static void nv_ident(cmdarg_T *cap); 57 static void nv_tagpop(cmdarg_T *cap); 58 static void nv_scroll(cmdarg_T *cap); 59 static void nv_right(cmdarg_T *cap); 60 static void nv_left(cmdarg_T *cap); 61 static void nv_up(cmdarg_T *cap); 62 static void nv_down(cmdarg_T *cap); 63 static void nv_end(cmdarg_T *cap); 64 static void nv_dollar(cmdarg_T *cap); 65 static void nv_search(cmdarg_T *cap); 66 static void nv_next(cmdarg_T *cap); 67 static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped); 68 static void nv_csearch(cmdarg_T *cap); 69 static void nv_brackets(cmdarg_T *cap); 70 static void nv_percent(cmdarg_T *cap); 71 static void nv_brace(cmdarg_T *cap); 72 static void nv_mark(cmdarg_T *cap); 73 static void nv_findpar(cmdarg_T *cap); 74 static void nv_undo(cmdarg_T *cap); 75 static void nv_kundo(cmdarg_T *cap); 76 static void nv_Replace(cmdarg_T *cap); 77 static void nv_replace(cmdarg_T *cap); 78 static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos); 79 static void v_visop(cmdarg_T *cap); 80 static void nv_subst(cmdarg_T *cap); 81 static void nv_abbrev(cmdarg_T *cap); 82 static void nv_optrans(cmdarg_T *cap); 83 static void nv_gomark(cmdarg_T *cap); 84 static void nv_pcmark(cmdarg_T *cap); 85 static void nv_regname(cmdarg_T *cap); 86 static void nv_visual(cmdarg_T *cap); 87 static void n_start_visual_mode(int c); 88 static void nv_window(cmdarg_T *cap); 89 static void nv_suspend(cmdarg_T *cap); 90 static void nv_g_cmd(cmdarg_T *cap); 91 static void nv_dot(cmdarg_T *cap); 92 static void nv_redo(cmdarg_T *cap); 93 static void nv_Undo(cmdarg_T *cap); 94 static void nv_tilde(cmdarg_T *cap); 95 static void nv_operator(cmdarg_T *cap); 96 #ifdef FEAT_EVAL 97 static void set_op_var(int optype); 98 #endif 99 static void nv_lineop(cmdarg_T *cap); 100 static void nv_home(cmdarg_T *cap); 101 static void nv_pipe(cmdarg_T *cap); 102 static void nv_bck_word(cmdarg_T *cap); 103 static void nv_wordcmd(cmdarg_T *cap); 104 static void nv_beginline(cmdarg_T *cap); 105 static void adjust_cursor(oparg_T *oap); 106 static void adjust_for_sel(cmdarg_T *cap); 107 static void nv_select(cmdarg_T *cap); 108 static void nv_goto(cmdarg_T *cap); 109 static void nv_normal(cmdarg_T *cap); 110 static void nv_esc(cmdarg_T *oap); 111 static void nv_edit(cmdarg_T *cap); 112 static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln); 113 #ifdef FEAT_TEXTOBJ 114 static void nv_object(cmdarg_T *cap); 115 #endif 116 static void nv_record(cmdarg_T *cap); 117 static void nv_at(cmdarg_T *cap); 118 static void nv_halfpage(cmdarg_T *cap); 119 static void nv_join(cmdarg_T *cap); 120 static void nv_put(cmdarg_T *cap); 121 static void nv_put_opt(cmdarg_T *cap, int fix_indent); 122 static void nv_open(cmdarg_T *cap); 123 #ifdef FEAT_NETBEANS_INTG 124 static void nv_nbcmd(cmdarg_T *cap); 125 #endif 126 #ifdef FEAT_DND 127 static void nv_drop(cmdarg_T *cap); 128 #endif 129 static void nv_cursorhold(cmdarg_T *cap); 130 131 static char *e_noident = N_("E349: No identifier under cursor"); 132 133 /* 134 * Function to be called for a Normal or Visual mode command. 135 * The argument is a cmdarg_T. 136 */ 137 typedef void (*nv_func_T)(cmdarg_T *cap); 138 139 // Values for cmd_flags. 140 #define NV_NCH 0x01 // may need to get a second char 141 #define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending 142 #define NV_NCH_ALW (0x04|NV_NCH) // always get a second char 143 #define NV_LANG 0x08 // second char needs language adjustment 144 145 #define NV_SS 0x10 // may start selection 146 #define NV_SSS 0x20 // may start selection with shift modifier 147 #define NV_STS 0x40 // may stop selection without shift modif. 148 #define NV_RL 0x80 // 'rightleft' modifies command 149 #define NV_KEEPREG 0x100 // don't clear regname 150 #define NV_NCW 0x200 // not allowed in command-line window 151 152 /* 153 * Generally speaking, every Normal mode command should either clear any 154 * pending operator (with *clearop*()), or set the motion type variable 155 * oap->motion_type. 156 * 157 * When a cursor motion command is made, it is marked as being a character or 158 * line oriented motion. Then, if an operator is in effect, the operation 159 * becomes character or line oriented accordingly. 160 */ 161 162 /* 163 * This table contains one entry for every Normal or Visual mode command. 164 * The order doesn't matter, init_normal_cmds() will create a sorted index. 165 * It is faster when all keys from zero to '~' are present. 166 */ 167 static const struct nv_cmd 168 { 169 int cmd_char; // (first) command character 170 nv_func_T cmd_func; // function for this command 171 short_u cmd_flags; // NV_ flags 172 short cmd_arg; // value for ca.arg 173 } nv_cmds[] = 174 { 175 {NUL, nv_error, 0, 0}, 176 {Ctrl_A, nv_addsub, 0, 0}, 177 {Ctrl_B, nv_page, NV_STS, BACKWARD}, 178 {Ctrl_C, nv_esc, 0, TRUE}, 179 {Ctrl_D, nv_halfpage, 0, 0}, 180 {Ctrl_E, nv_scroll_line, 0, TRUE}, 181 {Ctrl_F, nv_page, NV_STS, FORWARD}, 182 {Ctrl_G, nv_ctrlg, 0, 0}, 183 {Ctrl_H, nv_ctrlh, 0, 0}, 184 {Ctrl_I, nv_pcmark, 0, 0}, 185 {NL, nv_down, 0, FALSE}, 186 {Ctrl_K, nv_error, 0, 0}, 187 {Ctrl_L, nv_clear, 0, 0}, 188 {CAR, nv_down, 0, TRUE}, 189 {Ctrl_N, nv_down, NV_STS, FALSE}, 190 {Ctrl_O, nv_ctrlo, 0, 0}, 191 {Ctrl_P, nv_up, NV_STS, FALSE}, 192 {Ctrl_Q, nv_visual, 0, FALSE}, 193 {Ctrl_R, nv_redo, 0, 0}, 194 {Ctrl_S, nv_ignore, 0, 0}, 195 {Ctrl_T, nv_tagpop, NV_NCW, 0}, 196 {Ctrl_U, nv_halfpage, 0, 0}, 197 {Ctrl_V, nv_visual, 0, FALSE}, 198 {'V', nv_visual, 0, FALSE}, 199 {'v', nv_visual, 0, FALSE}, 200 {Ctrl_W, nv_window, 0, 0}, 201 {Ctrl_X, nv_addsub, 0, 0}, 202 {Ctrl_Y, nv_scroll_line, 0, FALSE}, 203 {Ctrl_Z, nv_suspend, 0, 0}, 204 {ESC, nv_esc, 0, FALSE}, 205 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0}, 206 {Ctrl_RSB, nv_ident, NV_NCW, 0}, 207 {Ctrl_HAT, nv_hat, NV_NCW, 0}, 208 {Ctrl__, nv_error, 0, 0}, 209 {' ', nv_right, 0, 0}, 210 {'!', nv_operator, 0, 0}, 211 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0}, 212 {'#', nv_ident, 0, 0}, 213 {'$', nv_dollar, 0, 0}, 214 {'%', nv_percent, 0, 0}, 215 {'&', nv_optrans, 0, 0}, 216 {'\'', nv_gomark, NV_NCH_ALW, TRUE}, 217 {'(', nv_brace, 0, BACKWARD}, 218 {')', nv_brace, 0, FORWARD}, 219 {'*', nv_ident, 0, 0}, 220 {'+', nv_down, 0, TRUE}, 221 {',', nv_csearch, 0, TRUE}, 222 {'-', nv_up, 0, TRUE}, 223 {'.', nv_dot, NV_KEEPREG, 0}, 224 {'/', nv_search, 0, FALSE}, 225 {'0', nv_beginline, 0, 0}, 226 {'1', nv_ignore, 0, 0}, 227 {'2', nv_ignore, 0, 0}, 228 {'3', nv_ignore, 0, 0}, 229 {'4', nv_ignore, 0, 0}, 230 {'5', nv_ignore, 0, 0}, 231 {'6', nv_ignore, 0, 0}, 232 {'7', nv_ignore, 0, 0}, 233 {'8', nv_ignore, 0, 0}, 234 {'9', nv_ignore, 0, 0}, 235 {':', nv_colon, 0, 0}, 236 {';', nv_csearch, 0, FALSE}, 237 {'<', nv_operator, NV_RL, 0}, 238 {'=', nv_operator, 0, 0}, 239 {'>', nv_operator, NV_RL, 0}, 240 {'?', nv_search, 0, FALSE}, 241 {'@', nv_at, NV_NCH_NOP, FALSE}, 242 {'A', nv_edit, 0, 0}, 243 {'B', nv_bck_word, 0, 1}, 244 {'C', nv_abbrev, NV_KEEPREG, 0}, 245 {'D', nv_abbrev, NV_KEEPREG, 0}, 246 {'E', nv_wordcmd, 0, TRUE}, 247 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD}, 248 {'G', nv_goto, 0, TRUE}, 249 {'H', nv_scroll, 0, 0}, 250 {'I', nv_edit, 0, 0}, 251 {'J', nv_join, 0, 0}, 252 {'K', nv_ident, 0, 0}, 253 {'L', nv_scroll, 0, 0}, 254 {'M', nv_scroll, 0, 0}, 255 {'N', nv_next, 0, SEARCH_REV}, 256 {'O', nv_open, 0, 0}, 257 {'P', nv_put, 0, 0}, 258 {'Q', nv_exmode, NV_NCW, 0}, 259 {'R', nv_Replace, 0, FALSE}, 260 {'S', nv_subst, NV_KEEPREG, 0}, 261 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD}, 262 {'U', nv_Undo, 0, 0}, 263 {'W', nv_wordcmd, 0, TRUE}, 264 {'X', nv_abbrev, NV_KEEPREG, 0}, 265 {'Y', nv_abbrev, NV_KEEPREG, 0}, 266 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0}, 267 {'[', nv_brackets, NV_NCH_ALW, BACKWARD}, 268 {'\\', nv_error, 0, 0}, 269 {']', nv_brackets, NV_NCH_ALW, FORWARD}, 270 {'^', nv_beginline, 0, BL_WHITE | BL_FIX}, 271 {'_', nv_lineop, 0, 0}, 272 {'`', nv_gomark, NV_NCH_ALW, FALSE}, 273 {'a', nv_edit, NV_NCH, 0}, 274 {'b', nv_bck_word, 0, 0}, 275 {'c', nv_operator, 0, 0}, 276 {'d', nv_operator, 0, 0}, 277 {'e', nv_wordcmd, 0, FALSE}, 278 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD}, 279 {'g', nv_g_cmd, NV_NCH_ALW, FALSE}, 280 {'h', nv_left, NV_RL, 0}, 281 {'i', nv_edit, NV_NCH, 0}, 282 {'j', nv_down, 0, FALSE}, 283 {'k', nv_up, 0, FALSE}, 284 {'l', nv_right, NV_RL, 0}, 285 {'m', nv_mark, NV_NCH_NOP, 0}, 286 {'n', nv_next, 0, 0}, 287 {'o', nv_open, 0, 0}, 288 {'p', nv_put, 0, 0}, 289 {'q', nv_record, NV_NCH, 0}, 290 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0}, 291 {'s', nv_subst, NV_KEEPREG, 0}, 292 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD}, 293 {'u', nv_undo, 0, 0}, 294 {'w', nv_wordcmd, 0, FALSE}, 295 {'x', nv_abbrev, NV_KEEPREG, 0}, 296 {'y', nv_operator, 0, 0}, 297 {'z', nv_zet, NV_NCH_ALW, 0}, 298 {'{', nv_findpar, 0, BACKWARD}, 299 {'|', nv_pipe, 0, 0}, 300 {'}', nv_findpar, 0, FORWARD}, 301 {'~', nv_tilde, 0, 0}, 302 303 // pound sign 304 {POUND, nv_ident, 0, 0}, 305 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP}, 306 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN}, 307 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT}, 308 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT}, 309 {K_LEFTMOUSE, nv_mouse, 0, 0}, 310 {K_LEFTMOUSE_NM, nv_mouse, 0, 0}, 311 {K_LEFTDRAG, nv_mouse, 0, 0}, 312 {K_LEFTRELEASE, nv_mouse, 0, 0}, 313 {K_LEFTRELEASE_NM, nv_mouse, 0, 0}, 314 {K_MOUSEMOVE, nv_mouse, 0, 0}, 315 {K_MIDDLEMOUSE, nv_mouse, 0, 0}, 316 {K_MIDDLEDRAG, nv_mouse, 0, 0}, 317 {K_MIDDLERELEASE, nv_mouse, 0, 0}, 318 {K_RIGHTMOUSE, nv_mouse, 0, 0}, 319 {K_RIGHTDRAG, nv_mouse, 0, 0}, 320 {K_RIGHTRELEASE, nv_mouse, 0, 0}, 321 {K_X1MOUSE, nv_mouse, 0, 0}, 322 {K_X1DRAG, nv_mouse, 0, 0}, 323 {K_X1RELEASE, nv_mouse, 0, 0}, 324 {K_X2MOUSE, nv_mouse, 0, 0}, 325 {K_X2DRAG, nv_mouse, 0, 0}, 326 {K_X2RELEASE, nv_mouse, 0, 0}, 327 {K_IGNORE, nv_ignore, NV_KEEPREG, 0}, 328 {K_NOP, nv_nop, 0, 0}, 329 {K_INS, nv_edit, 0, 0}, 330 {K_KINS, nv_edit, 0, 0}, 331 {K_BS, nv_ctrlh, 0, 0}, 332 {K_UP, nv_up, NV_SSS|NV_STS, FALSE}, 333 {K_S_UP, nv_page, NV_SS, BACKWARD}, 334 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE}, 335 {K_S_DOWN, nv_page, NV_SS, FORWARD}, 336 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0}, 337 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0}, 338 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1}, 339 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0}, 340 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE}, 341 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE}, 342 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD}, 343 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD}, 344 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD}, 345 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD}, 346 {K_END, nv_end, NV_SSS|NV_STS, FALSE}, 347 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE}, 348 {K_S_END, nv_end, NV_SS, FALSE}, 349 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE}, 350 {K_HOME, nv_home, NV_SSS|NV_STS, 0}, 351 {K_KHOME, nv_home, NV_SSS|NV_STS, 0}, 352 {K_S_HOME, nv_home, NV_SS, 0}, 353 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE}, 354 {K_DEL, nv_abbrev, 0, 0}, 355 {K_KDEL, nv_abbrev, 0, 0}, 356 {K_UNDO, nv_kundo, 0, 0}, 357 {K_HELP, nv_help, NV_NCW, 0}, 358 {K_F1, nv_help, NV_NCW, 0}, 359 {K_XF1, nv_help, NV_NCW, 0}, 360 {K_SELECT, nv_select, 0, 0}, 361 #ifdef FEAT_GUI 362 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0}, 363 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0}, 364 #endif 365 #ifdef FEAT_GUI_TABLINE 366 {K_TABLINE, nv_tabline, 0, 0}, 367 {K_TABMENU, nv_tabmenu, 0, 0}, 368 #endif 369 #ifdef FEAT_NETBEANS_INTG 370 {K_F21, nv_nbcmd, NV_NCH_ALW, 0}, 371 #endif 372 #ifdef FEAT_DND 373 {K_DROP, nv_drop, NV_STS, 0}, 374 #endif 375 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0}, 376 {K_PS, nv_edit, 0, 0}, 377 {K_COMMAND, nv_colon, 0, 0}, 378 }; 379 380 // Number of commands in nv_cmds[]. 381 #define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds) 382 383 // Sorted index of commands in nv_cmds[]. 384 static short nv_cmd_idx[NV_CMDS_SIZE]; 385 386 // The highest index for which 387 // nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] 388 static int nv_max_linear; 389 390 /* 391 * Compare functions for qsort() below, that checks the command character 392 * through the index in nv_cmd_idx[]. 393 */ 394 static int 395 nv_compare(const void *s1, const void *s2) 396 { 397 int c1, c2; 398 399 // The commands are sorted on absolute value. 400 c1 = nv_cmds[*(const short *)s1].cmd_char; 401 c2 = nv_cmds[*(const short *)s2].cmd_char; 402 if (c1 < 0) 403 c1 = -c1; 404 if (c2 < 0) 405 c2 = -c2; 406 return c1 - c2; 407 } 408 409 /* 410 * Initialize the nv_cmd_idx[] table. 411 */ 412 void 413 init_normal_cmds(void) 414 { 415 int i; 416 417 // Fill the index table with a one to one relation. 418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i) 419 nv_cmd_idx[i] = i; 420 421 // Sort the commands by the command character. 422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare); 423 424 // Find the first entry that can't be indexed by the command character. 425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i) 426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char) 427 break; 428 nv_max_linear = i - 1; 429 } 430 431 /* 432 * Search for a command in the commands table. 433 * Returns -1 for invalid command. 434 */ 435 static int 436 find_command(int cmdchar) 437 { 438 int i; 439 int idx; 440 int top, bot; 441 int c; 442 443 // A multi-byte character is never a command. 444 if (cmdchar >= 0x100) 445 return -1; 446 447 // We use the absolute value of the character. Special keys have a 448 // negative value, but are sorted on their absolute value. 449 if (cmdchar < 0) 450 cmdchar = -cmdchar; 451 452 // If the character is in the first part: The character is the index into 453 // nv_cmd_idx[]. 454 if (cmdchar <= nv_max_linear) 455 return nv_cmd_idx[cmdchar]; 456 457 // Perform a binary search. 458 bot = nv_max_linear + 1; 459 top = NV_CMDS_SIZE - 1; 460 idx = -1; 461 while (bot <= top) 462 { 463 i = (top + bot) / 2; 464 c = nv_cmds[nv_cmd_idx[i]].cmd_char; 465 if (c < 0) 466 c = -c; 467 if (cmdchar == c) 468 { 469 idx = nv_cmd_idx[i]; 470 break; 471 } 472 if (cmdchar > c) 473 bot = i + 1; 474 else 475 top = i - 1; 476 } 477 return idx; 478 } 479 480 /* 481 * Execute a command in Normal mode. 482 */ 483 void 484 normal_cmd( 485 oparg_T *oap, 486 int toplevel UNUSED) // TRUE when called from main() 487 { 488 cmdarg_T ca; // command arguments 489 int c; 490 int ctrl_w = FALSE; // got CTRL-W command 491 int old_col = curwin->w_curswant; 492 #ifdef FEAT_CMDL_INFO 493 int need_flushbuf; // need to call out_flush() 494 #endif 495 pos_T old_pos; // cursor position before command 496 int mapped_len; 497 static int old_mapped_len = 0; 498 int idx; 499 #ifdef FEAT_EVAL 500 int set_prevcount = FALSE; 501 #endif 502 int save_did_cursorhold = did_cursorhold; 503 504 CLEAR_FIELD(ca); // also resets ca.retval 505 ca.oap = oap; 506 507 // Use a count remembered from before entering an operator. After typing 508 // "3d" we return from normal_cmd() and come back here, the "3" is 509 // remembered in "opcount". 510 ca.opcount = opcount; 511 512 /* 513 * If there is an operator pending, then the command we take this time 514 * will terminate it. Finish_op tells us to finish the operation before 515 * returning this time (unless the operation was cancelled). 516 */ 517 #ifdef CURSOR_SHAPE 518 c = finish_op; 519 #endif 520 finish_op = (oap->op_type != OP_NOP); 521 #ifdef CURSOR_SHAPE 522 if (finish_op != c) 523 { 524 ui_cursor_shape(); // may show different cursor shape 525 # ifdef FEAT_MOUSESHAPE 526 update_mouseshape(-1); 527 # endif 528 } 529 #endif 530 531 // When not finishing an operator and no register name typed, reset the 532 // count. 533 if (!finish_op && !oap->regname) 534 { 535 ca.opcount = 0; 536 #ifdef FEAT_EVAL 537 set_prevcount = TRUE; 538 #endif 539 } 540 541 // Restore counts from before receiving K_CURSORHOLD. This means after 542 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not 543 // "3 * 2". 544 if (oap->prev_opcount > 0 || oap->prev_count0 > 0) 545 { 546 ca.opcount = oap->prev_opcount; 547 ca.count0 = oap->prev_count0; 548 oap->prev_opcount = 0; 549 oap->prev_count0 = 0; 550 } 551 552 mapped_len = typebuf_maplen(); 553 554 State = NORMAL_BUSY; 555 #ifdef USE_ON_FLY_SCROLL 556 dont_scroll = FALSE; // allow scrolling here 557 #endif 558 559 #ifdef FEAT_EVAL 560 // Set v:count here, when called from main() and not a stuffed 561 // command, so that v:count can be used in an expression mapping 562 // when there is no count. Do set it for redo. 563 if (toplevel && readbuf1_empty()) 564 set_vcount_ca(&ca, &set_prevcount); 565 #endif 566 567 /* 568 * Get the command character from the user. 569 */ 570 c = safe_vgetc(); 571 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE); 572 573 /* 574 * If a mapping was started in Visual or Select mode, remember the length 575 * of the mapping. This is used below to not return to Insert mode for as 576 * long as the mapping is being executed. 577 */ 578 if (restart_edit == 0) 579 old_mapped_len = 0; 580 else if (old_mapped_len 581 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0)) 582 old_mapped_len = typebuf_maplen(); 583 584 if (c == NUL) 585 c = K_ZERO; 586 587 /* 588 * In Select mode, typed text replaces the selection. 589 */ 590 if (VIsual_active 591 && VIsual_select 592 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER)) 593 { 594 // Fake a "c"hange command. When "restart_edit" is set (e.g., because 595 // 'insertmode' is set) fake a "d"elete command, Insert mode will 596 // restart automatically. 597 // Insert the typed character in the typeahead buffer, so that it can 598 // be mapped in Insert mode. Required for ":lmap" to work. 599 ins_char_typebuf(vgetc_char, vgetc_mod_mask); 600 if (restart_edit != 0) 601 c = 'd'; 602 else 603 c = 'c'; 604 msg_nowait = TRUE; // don't delay going to insert mode 605 old_mapped_len = 0; // do go to Insert mode 606 } 607 608 #ifdef FEAT_CMDL_INFO 609 need_flushbuf = add_to_showcmd(c); 610 #endif 611 612 getcount: 613 if (!(VIsual_active && VIsual_select)) 614 { 615 /* 616 * Handle a count before a command and compute ca.count0. 617 * Note that '0' is a command and not the start of a count, but it's 618 * part of a count after other digits. 619 */ 620 while ( (c >= '1' && c <= '9') 621 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0'))) 622 { 623 if (c == K_DEL || c == K_KDEL) 624 { 625 ca.count0 /= 10; 626 #ifdef FEAT_CMDL_INFO 627 del_from_showcmd(4); // delete the digit and ~@% 628 #endif 629 } 630 else 631 ca.count0 = ca.count0 * 10 + (c - '0'); 632 if (ca.count0 < 0) // overflow 633 ca.count0 = 999999999L; 634 #ifdef FEAT_EVAL 635 // Set v:count here, when called from main() and not a stuffed 636 // command, so that v:count can be used in an expression mapping 637 // right after the count. Do set it for redo. 638 if (toplevel && readbuf1_empty()) 639 set_vcount_ca(&ca, &set_prevcount); 640 #endif 641 if (ctrl_w) 642 { 643 ++no_mapping; 644 ++allow_keys; // no mapping for nchar, but keys 645 } 646 ++no_zero_mapping; // don't map zero here 647 c = plain_vgetc(); 648 LANGMAP_ADJUST(c, TRUE); 649 --no_zero_mapping; 650 if (ctrl_w) 651 { 652 --no_mapping; 653 --allow_keys; 654 } 655 #ifdef FEAT_CMDL_INFO 656 need_flushbuf |= add_to_showcmd(c); 657 #endif 658 } 659 660 /* 661 * If we got CTRL-W there may be a/another count 662 */ 663 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP) 664 { 665 ctrl_w = TRUE; 666 ca.opcount = ca.count0; // remember first count 667 ca.count0 = 0; 668 ++no_mapping; 669 ++allow_keys; // no mapping for nchar, but keys 670 c = plain_vgetc(); // get next character 671 LANGMAP_ADJUST(c, TRUE); 672 --no_mapping; 673 --allow_keys; 674 #ifdef FEAT_CMDL_INFO 675 need_flushbuf |= add_to_showcmd(c); 676 #endif 677 goto getcount; // jump back 678 } 679 } 680 681 if (c == K_CURSORHOLD) 682 { 683 // Save the count values so that ca.opcount and ca.count0 are exactly 684 // the same when coming back here after handling K_CURSORHOLD. 685 oap->prev_opcount = ca.opcount; 686 oap->prev_count0 = ca.count0; 687 } 688 else if (ca.opcount != 0) 689 { 690 /* 691 * If we're in the middle of an operator (including after entering a 692 * yank buffer with '"') AND we had a count before the operator, then 693 * that count overrides the current value of ca.count0. 694 * What this means effectively, is that commands like "3dw" get turned 695 * into "d3w" which makes things fall into place pretty neatly. 696 * If you give a count before AND after the operator, they are 697 * multiplied. 698 */ 699 if (ca.count0) 700 ca.count0 *= ca.opcount; 701 else 702 ca.count0 = ca.opcount; 703 if (ca.count0 < 0) // overflow 704 ca.count0 = 999999999L; 705 } 706 707 /* 708 * Always remember the count. It will be set to zero (on the next call, 709 * above) when there is no pending operator. 710 * When called from main(), save the count for use by the "count" built-in 711 * variable. 712 */ 713 ca.opcount = ca.count0; 714 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0); 715 716 #ifdef FEAT_EVAL 717 /* 718 * Only set v:count when called from main() and not a stuffed command. 719 * Do set it for redo. 720 */ 721 if (toplevel && readbuf1_empty()) 722 set_vcount(ca.count0, ca.count1, set_prevcount); 723 #endif 724 725 /* 726 * Find the command character in the table of commands. 727 * For CTRL-W we already got nchar when looking for a count. 728 */ 729 if (ctrl_w) 730 { 731 ca.nchar = c; 732 ca.cmdchar = Ctrl_W; 733 } 734 else 735 ca.cmdchar = c; 736 idx = find_command(ca.cmdchar); 737 if (idx < 0) 738 { 739 // Not a known command: beep. 740 clearopbeep(oap); 741 goto normal_end; 742 } 743 744 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW)) 745 { 746 // This command is not allowed while editing a cmdline: beep. 747 clearopbeep(oap); 748 text_locked_msg(); 749 goto normal_end; 750 } 751 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked()) 752 goto normal_end; 753 754 /* 755 * In Visual/Select mode, a few keys are handled in a special way. 756 */ 757 if (VIsual_active) 758 { 759 // when 'keymodel' contains "stopsel" may stop Select/Visual mode 760 if (km_stopsel 761 && (nv_cmds[idx].cmd_flags & NV_STS) 762 && !(mod_mask & MOD_MASK_SHIFT)) 763 { 764 end_visual_mode(); 765 redraw_curbuf_later(INVERTED); 766 } 767 768 // Keys that work different when 'keymodel' contains "startsel" 769 if (km_startsel) 770 { 771 if (nv_cmds[idx].cmd_flags & NV_SS) 772 { 773 unshift_special(&ca); 774 idx = find_command(ca.cmdchar); 775 if (idx < 0) 776 { 777 // Just in case 778 clearopbeep(oap); 779 goto normal_end; 780 } 781 } 782 else if ((nv_cmds[idx].cmd_flags & NV_SSS) 783 && (mod_mask & MOD_MASK_SHIFT)) 784 mod_mask &= ~MOD_MASK_SHIFT; 785 } 786 } 787 788 #ifdef FEAT_RIGHTLEFT 789 if (curwin->w_p_rl && KeyTyped && !KeyStuffed 790 && (nv_cmds[idx].cmd_flags & NV_RL)) 791 { 792 // Invert horizontal movements and operations. Only when typed by the 793 // user directly, not when the result of a mapping or "x" translated 794 // to "dl". 795 switch (ca.cmdchar) 796 { 797 case 'l': ca.cmdchar = 'h'; break; 798 case K_RIGHT: ca.cmdchar = K_LEFT; break; 799 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break; 800 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break; 801 case 'h': ca.cmdchar = 'l'; break; 802 case K_LEFT: ca.cmdchar = K_RIGHT; break; 803 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break; 804 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break; 805 case '>': ca.cmdchar = '<'; break; 806 case '<': ca.cmdchar = '>'; break; 807 } 808 idx = find_command(ca.cmdchar); 809 } 810 #endif 811 812 /* 813 * Get an additional character if we need one. 814 */ 815 if ((nv_cmds[idx].cmd_flags & NV_NCH) 816 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP 817 && oap->op_type == OP_NOP) 818 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW 819 || (ca.cmdchar == 'q' 820 && oap->op_type == OP_NOP 821 && reg_recording == 0 822 && reg_executing == 0) 823 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i') 824 && (oap->op_type != OP_NOP || VIsual_active)))) 825 { 826 int *cp; 827 int repl = FALSE; // get character for replace mode 828 int lit = FALSE; // get extra character literally 829 int langmap_active = FALSE; // using :lmap mappings 830 int lang; // getting a text character 831 #ifdef HAVE_INPUT_METHOD 832 int save_smd; // saved value of p_smd 833 #endif 834 835 ++no_mapping; 836 ++allow_keys; // no mapping for nchar, but allow key codes 837 // Don't generate a CursorHold event here, most commands can't handle 838 // it, e.g., nv_replace(), nv_csearch(). 839 did_cursorhold = TRUE; 840 if (ca.cmdchar == 'g') 841 { 842 /* 843 * For 'g' get the next character now, so that we can check for 844 * "gr", "g'" and "g`". 845 */ 846 ca.nchar = plain_vgetc(); 847 LANGMAP_ADJUST(ca.nchar, TRUE); 848 #ifdef FEAT_CMDL_INFO 849 need_flushbuf |= add_to_showcmd(ca.nchar); 850 #endif 851 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`' 852 || ca.nchar == Ctrl_BSL) 853 { 854 cp = &ca.extra_char; // need to get a third character 855 if (ca.nchar != 'r') 856 lit = TRUE; // get it literally 857 else 858 repl = TRUE; // get it in replace mode 859 } 860 else 861 cp = NULL; // no third character needed 862 } 863 else 864 { 865 if (ca.cmdchar == 'r') // get it in replace mode 866 repl = TRUE; 867 cp = &ca.nchar; 868 } 869 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG)); 870 871 /* 872 * Get a second or third character. 873 */ 874 if (cp != NULL) 875 { 876 if (repl) 877 { 878 State = REPLACE; // pretend Replace mode 879 #ifdef CURSOR_SHAPE 880 ui_cursor_shape(); // show different cursor shape 881 #endif 882 } 883 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP) 884 { 885 // Allow mappings defined with ":lmap". 886 --no_mapping; 887 --allow_keys; 888 if (repl) 889 State = LREPLACE; 890 else 891 State = LANGMAP; 892 langmap_active = TRUE; 893 } 894 #ifdef HAVE_INPUT_METHOD 895 save_smd = p_smd; 896 p_smd = FALSE; // Don't let the IM code show the mode here 897 if (lang && curbuf->b_p_iminsert == B_IMODE_IM) 898 im_set_active(TRUE); 899 #endif 900 if ((State & INSERT) && !p_ek) 901 { 902 #ifdef FEAT_JOB_CHANNEL 903 ch_log_output = TRUE; 904 #endif 905 // Disable bracketed paste and modifyOtherKeys here, we won't 906 // recognize the escape sequences with 'esckeys' off. 907 out_str(T_BD); 908 out_str(T_CTE); 909 } 910 911 *cp = plain_vgetc(); 912 913 if ((State & INSERT) && !p_ek) 914 { 915 #ifdef FEAT_JOB_CHANNEL 916 ch_log_output = TRUE; 917 #endif 918 // Re-enable bracketed paste mode and modifyOtherKeys 919 out_str(T_BE); 920 out_str(T_CTI); 921 } 922 923 if (langmap_active) 924 { 925 // Undo the decrement done above 926 ++no_mapping; 927 ++allow_keys; 928 State = NORMAL_BUSY; 929 } 930 #ifdef HAVE_INPUT_METHOD 931 if (lang) 932 { 933 if (curbuf->b_p_iminsert != B_IMODE_LMAP) 934 im_save_status(&curbuf->b_p_iminsert); 935 im_set_active(FALSE); 936 } 937 p_smd = save_smd; 938 #endif 939 State = NORMAL_BUSY; 940 #ifdef FEAT_CMDL_INFO 941 need_flushbuf |= add_to_showcmd(*cp); 942 #endif 943 944 if (!lit) 945 { 946 #ifdef FEAT_DIGRAPHS 947 // Typing CTRL-K gets a digraph. 948 if (*cp == Ctrl_K 949 && ((nv_cmds[idx].cmd_flags & NV_LANG) 950 || cp == &ca.extra_char) 951 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL) 952 { 953 c = get_digraph(FALSE); 954 if (c > 0) 955 { 956 *cp = c; 957 # ifdef FEAT_CMDL_INFO 958 // Guessing how to update showcmd here... 959 del_from_showcmd(3); 960 need_flushbuf |= add_to_showcmd(*cp); 961 # endif 962 } 963 } 964 #endif 965 966 // adjust chars > 127, except after "tTfFr" commands 967 LANGMAP_ADJUST(*cp, !lang); 968 #ifdef FEAT_RIGHTLEFT 969 // adjust Hebrew mapped char 970 if (p_hkmap && lang && KeyTyped) 971 *cp = hkmap(*cp); 972 #endif 973 } 974 975 /* 976 * When the next character is CTRL-\ a following CTRL-N means the 977 * command is aborted and we go to Normal mode. 978 */ 979 if (cp == &ca.extra_char 980 && ca.nchar == Ctrl_BSL 981 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G)) 982 { 983 ca.cmdchar = Ctrl_BSL; 984 ca.nchar = ca.extra_char; 985 idx = find_command(ca.cmdchar); 986 } 987 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g') 988 ca.oap->op_type = get_op_type(*cp, NUL); 989 else if (*cp == Ctrl_BSL) 990 { 991 long towait = (p_ttm >= 0 ? p_ttm : p_tm); 992 993 // There is a busy wait here when typing "f<C-\>" and then 994 // something different from CTRL-N. Can't be avoided. 995 while ((c = vpeekc()) <= 0 && towait > 0L) 996 { 997 do_sleep(towait > 50L ? 50L : towait, FALSE); 998 towait -= 50L; 999 } 1000 if (c > 0) 1001 { 1002 c = plain_vgetc(); 1003 if (c != Ctrl_N && c != Ctrl_G) 1004 vungetc(c); 1005 else 1006 { 1007 ca.cmdchar = Ctrl_BSL; 1008 ca.nchar = c; 1009 idx = find_command(ca.cmdchar); 1010 } 1011 } 1012 } 1013 1014 // When getting a text character and the next character is a 1015 // multi-byte character, it could be a composing character. 1016 // However, don't wait for it to arrive. Also, do enable mapping, 1017 // because if it's put back with vungetc() it's too late to apply 1018 // mapping. 1019 --no_mapping; 1020 while (enc_utf8 && lang && (c = vpeekc()) > 0 1021 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1)) 1022 { 1023 c = plain_vgetc(); 1024 if (!utf_iscomposing(c)) 1025 { 1026 vungetc(c); // it wasn't, put it back 1027 break; 1028 } 1029 else if (ca.ncharC1 == 0) 1030 ca.ncharC1 = c; 1031 else 1032 ca.ncharC2 = c; 1033 } 1034 ++no_mapping; 1035 } 1036 --no_mapping; 1037 --allow_keys; 1038 } 1039 1040 #ifdef FEAT_CMDL_INFO 1041 /* 1042 * Flush the showcmd characters onto the screen so we can see them while 1043 * the command is being executed. Only do this when the shown command was 1044 * actually displayed, otherwise this will slow down a lot when executing 1045 * mappings. 1046 */ 1047 if (need_flushbuf) 1048 out_flush(); 1049 #endif 1050 if (ca.cmdchar != K_IGNORE) 1051 { 1052 if (ex_normal_busy) 1053 did_cursorhold = save_did_cursorhold; 1054 else 1055 did_cursorhold = FALSE; 1056 } 1057 1058 State = NORMAL; 1059 1060 if (ca.nchar == ESC) 1061 { 1062 clearop(oap); 1063 if (restart_edit == 0 && goto_im()) 1064 restart_edit = 'a'; 1065 goto normal_end; 1066 } 1067 1068 if (ca.cmdchar != K_IGNORE) 1069 { 1070 msg_didout = FALSE; // don't scroll screen up for normal command 1071 msg_col = 0; 1072 } 1073 1074 old_pos = curwin->w_cursor; // remember where cursor was 1075 1076 // When 'keymodel' contains "startsel" some keys start Select/Visual 1077 // mode. 1078 if (!VIsual_active && km_startsel) 1079 { 1080 if (nv_cmds[idx].cmd_flags & NV_SS) 1081 { 1082 start_selection(); 1083 unshift_special(&ca); 1084 idx = find_command(ca.cmdchar); 1085 } 1086 else if ((nv_cmds[idx].cmd_flags & NV_SSS) 1087 && (mod_mask & MOD_MASK_SHIFT)) 1088 { 1089 start_selection(); 1090 mod_mask &= ~MOD_MASK_SHIFT; 1091 } 1092 } 1093 1094 /* 1095 * Execute the command! 1096 * Call the command function found in the commands table. 1097 */ 1098 ca.arg = nv_cmds[idx].cmd_arg; 1099 (nv_cmds[idx].cmd_func)(&ca); 1100 1101 /* 1102 * If we didn't start or finish an operator, reset oap->regname, unless we 1103 * need it later. 1104 */ 1105 if (!finish_op 1106 && !oap->op_type 1107 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG))) 1108 { 1109 clearop(oap); 1110 #ifdef FEAT_EVAL 1111 reset_reg_var(); 1112 #endif 1113 } 1114 1115 // Get the length of mapped chars again after typing a count, second 1116 // character or "z333<cr>". 1117 if (old_mapped_len > 0) 1118 old_mapped_len = typebuf_maplen(); 1119 1120 /* 1121 * If an operation is pending, handle it. But not for K_IGNORE or 1122 * K_MOUSEMOVE. 1123 */ 1124 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE) 1125 do_pending_operator(&ca, old_col, FALSE); 1126 1127 /* 1128 * Wait for a moment when a message is displayed that will be overwritten 1129 * by the mode message. 1130 * In Visual mode and with "^O" in Insert mode, a short message will be 1131 * overwritten by the mode message. Wait a bit, until a key is hit. 1132 * In Visual mode, it's more important to keep the Visual area updated 1133 * than keeping a message (e.g. from a /pat search). 1134 * Only do this if the command was typed, not from a mapping. 1135 * Don't wait when emsg_silent is non-zero. 1136 * Also wait a bit after an error message, e.g. for "^O:". 1137 * Don't redraw the screen, it would remove the message. 1138 */ 1139 if ( ((p_smd 1140 && msg_silent == 0 1141 && (restart_edit != 0 1142 || (VIsual_active 1143 && old_pos.lnum == curwin->w_cursor.lnum 1144 && old_pos.col == curwin->w_cursor.col) 1145 ) 1146 && (clear_cmdline 1147 || redraw_cmdline) 1148 && (msg_didout || (msg_didany && msg_scroll)) 1149 && !msg_nowait 1150 && KeyTyped) 1151 || (restart_edit != 0 1152 && !VIsual_active 1153 && (msg_scroll 1154 || emsg_on_display))) 1155 && oap->regname == 0 1156 && !(ca.retval & CA_COMMAND_BUSY) 1157 && stuff_empty() 1158 && typebuf_typed() 1159 && emsg_silent == 0 1160 && !in_assert_fails 1161 && !did_wait_return 1162 && oap->op_type == OP_NOP) 1163 { 1164 int save_State = State; 1165 1166 // Draw the cursor with the right shape here 1167 if (restart_edit != 0) 1168 State = INSERT; 1169 1170 // If need to redraw, and there is a "keep_msg", redraw before the 1171 // delay 1172 if (must_redraw && keep_msg != NULL && !emsg_on_display) 1173 { 1174 char_u *kmsg; 1175 1176 kmsg = keep_msg; 1177 keep_msg = NULL; 1178 // Showmode() will clear keep_msg, but we want to use it anyway. 1179 // First update w_topline. 1180 setcursor(); 1181 update_screen(0); 1182 // now reset it, otherwise it's put in the history again 1183 keep_msg = kmsg; 1184 1185 kmsg = vim_strsave(keep_msg); 1186 if (kmsg != NULL) 1187 { 1188 msg_attr((char *)kmsg, keep_msg_attr); 1189 vim_free(kmsg); 1190 } 1191 } 1192 setcursor(); 1193 #ifdef CURSOR_SHAPE 1194 ui_cursor_shape(); // may show different cursor shape 1195 #endif 1196 cursor_on(); 1197 out_flush(); 1198 if (msg_scroll || emsg_on_display) 1199 ui_delay(1003L, TRUE); // wait at least one second 1200 ui_delay(3003L, FALSE); // wait up to three seconds 1201 State = save_State; 1202 1203 msg_scroll = FALSE; 1204 emsg_on_display = FALSE; 1205 } 1206 1207 /* 1208 * Finish up after executing a Normal mode command. 1209 */ 1210 normal_end: 1211 1212 msg_nowait = FALSE; 1213 1214 #ifdef FEAT_EVAL 1215 if (finish_op) 1216 reset_reg_var(); 1217 #endif 1218 1219 // Reset finish_op, in case it was set 1220 #ifdef CURSOR_SHAPE 1221 c = finish_op; 1222 #endif 1223 finish_op = FALSE; 1224 #ifdef CURSOR_SHAPE 1225 // Redraw the cursor with another shape, if we were in Operator-pending 1226 // mode or did a replace command. 1227 if (c || ca.cmdchar == 'r') 1228 { 1229 ui_cursor_shape(); // may show different cursor shape 1230 # ifdef FEAT_MOUSESHAPE 1231 update_mouseshape(-1); 1232 # endif 1233 } 1234 #endif 1235 1236 #ifdef FEAT_CMDL_INFO 1237 if (oap->op_type == OP_NOP && oap->regname == 0 1238 && ca.cmdchar != K_CURSORHOLD) 1239 clear_showcmd(); 1240 #endif 1241 1242 checkpcmark(); // check if we moved since setting pcmark 1243 vim_free(ca.searchbuf); 1244 1245 if (has_mbyte) 1246 mb_adjust_cursor(); 1247 1248 if (curwin->w_p_scb && toplevel) 1249 { 1250 validate_cursor(); // may need to update w_leftcol 1251 do_check_scrollbind(TRUE); 1252 } 1253 1254 if (curwin->w_p_crb && toplevel) 1255 { 1256 validate_cursor(); // may need to update w_leftcol 1257 do_check_cursorbind(); 1258 } 1259 1260 #ifdef FEAT_TERMINAL 1261 // don't go to Insert mode if a terminal has a running job 1262 if (term_job_running(curbuf->b_term)) 1263 restart_edit = 0; 1264 #endif 1265 1266 /* 1267 * May restart edit(), if we got here with CTRL-O in Insert mode (but not 1268 * if still inside a mapping that started in Visual mode). 1269 * May switch from Visual to Select mode after CTRL-O command. 1270 */ 1271 if ( oap->op_type == OP_NOP 1272 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0) 1273 || restart_VIsual_select == 1) 1274 && !(ca.retval & CA_COMMAND_BUSY) 1275 && stuff_empty() 1276 && oap->regname == 0) 1277 { 1278 if (restart_VIsual_select == 1) 1279 { 1280 VIsual_select = TRUE; 1281 showmode(); 1282 restart_VIsual_select = 0; 1283 } 1284 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0) 1285 (void)edit(restart_edit, FALSE, 1L); 1286 } 1287 1288 if (restart_VIsual_select == 2) 1289 restart_VIsual_select = 1; 1290 1291 // Save count before an operator for next time. 1292 opcount = ca.opcount; 1293 } 1294 1295 #ifdef FEAT_EVAL 1296 /* 1297 * Set v:count and v:count1 according to "cap". 1298 * Set v:prevcount only when "set_prevcount" is TRUE. 1299 */ 1300 static void 1301 set_vcount_ca(cmdarg_T *cap, int *set_prevcount) 1302 { 1303 long count = cap->count0; 1304 1305 // multiply with cap->opcount the same way as above 1306 if (cap->opcount != 0) 1307 count = cap->opcount * (count == 0 ? 1 : count); 1308 set_vcount(count, count == 0 ? 1 : count, *set_prevcount); 1309 *set_prevcount = FALSE; // only set v:prevcount once 1310 } 1311 #endif 1312 1313 /* 1314 * Check if highlighting for Visual mode is possible, give a warning message 1315 * if not. 1316 */ 1317 void 1318 check_visual_highlight(void) 1319 { 1320 static int did_check = FALSE; 1321 1322 if (full_screen) 1323 { 1324 if (!did_check && HL_ATTR(HLF_V) == 0) 1325 msg(_("Warning: terminal cannot highlight")); 1326 did_check = TRUE; 1327 } 1328 } 1329 1330 #if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL) 1331 /* 1332 * Call yank_do_autocmd() for "regname". 1333 */ 1334 static void 1335 call_yank_do_autocmd(int regname) 1336 { 1337 oparg_T oa; 1338 yankreg_T *reg; 1339 1340 clear_oparg(&oa); 1341 oa.regname = regname; 1342 oa.op_type = OP_YANK; 1343 oa.is_VIsual = TRUE; 1344 reg = get_register(regname, TRUE); 1345 yank_do_autocmd(&oa, reg); 1346 free_register(reg); 1347 } 1348 #endif 1349 1350 /* 1351 * End Visual mode. 1352 * This function or the next should ALWAYS be called to end Visual mode, except 1353 * from do_pending_operator(). 1354 */ 1355 void 1356 end_visual_mode() 1357 { 1358 end_visual_mode_keep_button(); 1359 reset_held_button(); 1360 } 1361 1362 void 1363 end_visual_mode_keep_button() 1364 { 1365 #ifdef FEAT_CLIPBOARD 1366 /* 1367 * If we are using the clipboard, then remember what was selected in case 1368 * we need to paste it somewhere while we still own the selection. 1369 * Only do this when the clipboard is already owned. Don't want to grab 1370 * the selection when hitting ESC. 1371 */ 1372 if (clip_star.available && clip_star.owned) 1373 clip_auto_select(); 1374 1375 # if defined(FEAT_EVAL) 1376 // Emit a TextYankPost for the automatic copy of the selection into the 1377 // star and/or plus register. 1378 if (has_textyankpost()) 1379 { 1380 if (clip_isautosel_star()) 1381 call_yank_do_autocmd('*'); 1382 if (clip_isautosel_plus()) 1383 call_yank_do_autocmd('+'); 1384 } 1385 # endif 1386 #endif 1387 1388 VIsual_active = FALSE; 1389 setmouse(); 1390 mouse_dragging = 0; 1391 1392 // Save the current VIsual area for '< and '> marks, and "gv" 1393 curbuf->b_visual.vi_mode = VIsual_mode; 1394 curbuf->b_visual.vi_start = VIsual; 1395 curbuf->b_visual.vi_end = curwin->w_cursor; 1396 curbuf->b_visual.vi_curswant = curwin->w_curswant; 1397 #ifdef FEAT_EVAL 1398 curbuf->b_visual_mode_eval = VIsual_mode; 1399 #endif 1400 if (!virtual_active()) 1401 curwin->w_cursor.coladd = 0; 1402 may_clear_cmdline(); 1403 1404 adjust_cursor_eol(); 1405 } 1406 1407 /* 1408 * Reset VIsual_active and VIsual_reselect. 1409 */ 1410 void 1411 reset_VIsual_and_resel(void) 1412 { 1413 if (VIsual_active) 1414 { 1415 end_visual_mode(); 1416 redraw_curbuf_later(INVERTED); // delete the inversion later 1417 } 1418 VIsual_reselect = FALSE; 1419 } 1420 1421 /* 1422 * Reset VIsual_active and VIsual_reselect if it's set. 1423 */ 1424 void 1425 reset_VIsual(void) 1426 { 1427 if (VIsual_active) 1428 { 1429 end_visual_mode(); 1430 redraw_curbuf_later(INVERTED); // delete the inversion later 1431 VIsual_reselect = FALSE; 1432 } 1433 } 1434 1435 void 1436 restore_visual_mode(void) 1437 { 1438 if (VIsual_mode_orig != NUL) 1439 { 1440 curbuf->b_visual.vi_mode = VIsual_mode_orig; 1441 VIsual_mode_orig = NUL; 1442 } 1443 } 1444 1445 /* 1446 * Check for a balloon-eval special item to include when searching for an 1447 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid! 1448 * Returns TRUE if the character at "*ptr" should be included. 1449 * "dir" is FORWARD or BACKWARD, the direction of searching. 1450 * "*colp" is in/decremented if "ptr[-dir]" should also be included. 1451 * "bnp" points to a counter for square brackets. 1452 */ 1453 static int 1454 find_is_eval_item( 1455 char_u *ptr, 1456 int *colp, 1457 int *bnp, 1458 int dir) 1459 { 1460 // Accept everything inside []. 1461 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD)) 1462 ++*bnp; 1463 if (*bnp > 0) 1464 { 1465 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD)) 1466 --*bnp; 1467 return TRUE; 1468 } 1469 1470 // skip over "s.var" 1471 if (*ptr == '.') 1472 return TRUE; 1473 1474 // two-character item: s->var 1475 if (ptr[dir == BACKWARD ? 0 : 1] == '>' 1476 && ptr[dir == BACKWARD ? -1 : 0] == '-') 1477 { 1478 *colp += dir; 1479 return TRUE; 1480 } 1481 return FALSE; 1482 } 1483 1484 /* 1485 * Find the identifier under or to the right of the cursor. 1486 * "find_type" can have one of three values: 1487 * FIND_IDENT: find an identifier (keyword) 1488 * FIND_STRING: find any non-white text 1489 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred. 1490 * FIND_EVAL: find text useful for C program debugging 1491 * 1492 * There are three steps: 1493 * 1. Search forward for the start of an identifier/text. Doesn't move if 1494 * already on one. 1495 * 2. Search backward for the start of this identifier/text. 1496 * This doesn't match the real Vi but I like it a little better and it 1497 * shouldn't bother anyone. 1498 * 3. Search forward to the end of this identifier/text. 1499 * When FIND_IDENT isn't defined, we backup until a blank. 1500 * 1501 * Returns the length of the text, or zero if no text is found. 1502 * If text is found, a pointer to the text is put in "*text". This 1503 * points into the current buffer line and is not always NUL terminated. 1504 */ 1505 int 1506 find_ident_under_cursor(char_u **text, int find_type) 1507 { 1508 return find_ident_at_pos(curwin, curwin->w_cursor.lnum, 1509 curwin->w_cursor.col, text, NULL, find_type); 1510 } 1511 1512 /* 1513 * Like find_ident_under_cursor(), but for any window and any position. 1514 * However: Uses 'iskeyword' from the current window!. 1515 */ 1516 int 1517 find_ident_at_pos( 1518 win_T *wp, 1519 linenr_T lnum, 1520 colnr_T startcol, 1521 char_u **text, 1522 int *textcol, // column where "text" starts, can be NULL 1523 int find_type) 1524 { 1525 char_u *ptr; 1526 int col = 0; // init to shut up GCC 1527 int i; 1528 int this_class = 0; 1529 int prev_class; 1530 int prevcol; 1531 int bn = 0; // bracket nesting 1532 1533 /* 1534 * if i == 0: try to find an identifier 1535 * if i == 1: try to find any non-white text 1536 */ 1537 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE); 1538 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i) 1539 { 1540 /* 1541 * 1. skip to start of identifier/text 1542 */ 1543 col = startcol; 1544 if (has_mbyte) 1545 { 1546 while (ptr[col] != NUL) 1547 { 1548 // Stop at a ']' to evaluate "a[x]". 1549 if ((find_type & FIND_EVAL) && ptr[col] == ']') 1550 break; 1551 this_class = mb_get_class(ptr + col); 1552 if (this_class != 0 && (i == 1 || this_class != 1)) 1553 break; 1554 col += (*mb_ptr2len)(ptr + col); 1555 } 1556 } 1557 else 1558 while (ptr[col] != NUL 1559 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col])) 1560 && (!(find_type & FIND_EVAL) || ptr[col] != ']') 1561 ) 1562 ++col; 1563 1564 // When starting on a ']' count it, so that we include the '['. 1565 bn = ptr[col] == ']'; 1566 1567 /* 1568 * 2. Back up to start of identifier/text. 1569 */ 1570 if (has_mbyte) 1571 { 1572 // Remember class of character under cursor. 1573 if ((find_type & FIND_EVAL) && ptr[col] == ']') 1574 this_class = mb_get_class((char_u *)"a"); 1575 else 1576 this_class = mb_get_class(ptr + col); 1577 while (col > 0 && this_class != 0) 1578 { 1579 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1); 1580 prev_class = mb_get_class(ptr + prevcol); 1581 if (this_class != prev_class 1582 && (i == 0 1583 || prev_class == 0 1584 || (find_type & FIND_IDENT)) 1585 && (!(find_type & FIND_EVAL) 1586 || prevcol == 0 1587 || !find_is_eval_item(ptr + prevcol, &prevcol, 1588 &bn, BACKWARD)) 1589 ) 1590 break; 1591 col = prevcol; 1592 } 1593 1594 // If we don't want just any old text, or we've found an 1595 // identifier, stop searching. 1596 if (this_class > 2) 1597 this_class = 2; 1598 if (!(find_type & FIND_STRING) || this_class == 2) 1599 break; 1600 } 1601 else 1602 { 1603 while (col > 0 1604 && ((i == 0 1605 ? vim_iswordc(ptr[col - 1]) 1606 : (!VIM_ISWHITE(ptr[col - 1]) 1607 && (!(find_type & FIND_IDENT) 1608 || !vim_iswordc(ptr[col - 1])))) 1609 || ((find_type & FIND_EVAL) 1610 && col > 1 1611 && find_is_eval_item(ptr + col - 1, &col, 1612 &bn, BACKWARD)) 1613 )) 1614 --col; 1615 1616 // If we don't want just any old text, or we've found an 1617 // identifier, stop searching. 1618 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col])) 1619 break; 1620 } 1621 } 1622 1623 if (ptr[col] == NUL || (i == 0 1624 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col])))) 1625 { 1626 // didn't find an identifier or text 1627 if ((find_type & FIND_NOERROR) == 0) 1628 { 1629 if (find_type & FIND_STRING) 1630 emsg(_("E348: No string under cursor")); 1631 else 1632 emsg(_(e_noident)); 1633 } 1634 return 0; 1635 } 1636 ptr += col; 1637 *text = ptr; 1638 if (textcol != NULL) 1639 *textcol = col; 1640 1641 /* 1642 * 3. Find the end if the identifier/text. 1643 */ 1644 bn = 0; 1645 startcol -= col; 1646 col = 0; 1647 if (has_mbyte) 1648 { 1649 // Search for point of changing multibyte character class. 1650 this_class = mb_get_class(ptr); 1651 while (ptr[col] != NUL 1652 && ((i == 0 ? mb_get_class(ptr + col) == this_class 1653 : mb_get_class(ptr + col) != 0) 1654 || ((find_type & FIND_EVAL) 1655 && col <= (int)startcol 1656 && find_is_eval_item(ptr + col, &col, &bn, FORWARD)) 1657 )) 1658 col += (*mb_ptr2len)(ptr + col); 1659 } 1660 else 1661 while ((i == 0 ? vim_iswordc(ptr[col]) 1662 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col]))) 1663 || ((find_type & FIND_EVAL) 1664 && col <= (int)startcol 1665 && find_is_eval_item(ptr + col, &col, &bn, FORWARD)) 1666 ) 1667 ++col; 1668 1669 return col; 1670 } 1671 1672 /* 1673 * Prepare for redo of a normal command. 1674 */ 1675 static void 1676 prep_redo_cmd(cmdarg_T *cap) 1677 { 1678 prep_redo(cap->oap->regname, cap->count0, 1679 NUL, cap->cmdchar, NUL, NUL, cap->nchar); 1680 } 1681 1682 /* 1683 * Prepare for redo of any command. 1684 * Note that only the last argument can be a multi-byte char. 1685 */ 1686 void 1687 prep_redo( 1688 int regname, 1689 long num, 1690 int cmd1, 1691 int cmd2, 1692 int cmd3, 1693 int cmd4, 1694 int cmd5) 1695 { 1696 ResetRedobuff(); 1697 if (regname != 0) // yank from specified buffer 1698 { 1699 AppendCharToRedobuff('"'); 1700 AppendCharToRedobuff(regname); 1701 } 1702 if (num) 1703 AppendNumberToRedobuff(num); 1704 1705 if (cmd1 != NUL) 1706 AppendCharToRedobuff(cmd1); 1707 if (cmd2 != NUL) 1708 AppendCharToRedobuff(cmd2); 1709 if (cmd3 != NUL) 1710 AppendCharToRedobuff(cmd3); 1711 if (cmd4 != NUL) 1712 AppendCharToRedobuff(cmd4); 1713 if (cmd5 != NUL) 1714 AppendCharToRedobuff(cmd5); 1715 } 1716 1717 /* 1718 * check for operator active and clear it 1719 * 1720 * return TRUE if operator was active 1721 */ 1722 static int 1723 checkclearop(oparg_T *oap) 1724 { 1725 if (oap->op_type == OP_NOP) 1726 return FALSE; 1727 clearopbeep(oap); 1728 return TRUE; 1729 } 1730 1731 /* 1732 * Check for operator or Visual active. Clear active operator. 1733 * 1734 * Return TRUE if operator or Visual was active. 1735 */ 1736 static int 1737 checkclearopq(oparg_T *oap) 1738 { 1739 if (oap->op_type == OP_NOP && !VIsual_active) 1740 return FALSE; 1741 clearopbeep(oap); 1742 return TRUE; 1743 } 1744 1745 void 1746 clearop(oparg_T *oap) 1747 { 1748 oap->op_type = OP_NOP; 1749 oap->regname = 0; 1750 oap->motion_force = NUL; 1751 oap->use_reg_one = FALSE; 1752 motion_force = NUL; 1753 } 1754 1755 void 1756 clearopbeep(oparg_T *oap) 1757 { 1758 clearop(oap); 1759 beep_flush(); 1760 } 1761 1762 /* 1763 * Remove the shift modifier from a special key. 1764 */ 1765 static void 1766 unshift_special(cmdarg_T *cap) 1767 { 1768 switch (cap->cmdchar) 1769 { 1770 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break; 1771 case K_S_LEFT: cap->cmdchar = K_LEFT; break; 1772 case K_S_UP: cap->cmdchar = K_UP; break; 1773 case K_S_DOWN: cap->cmdchar = K_DOWN; break; 1774 case K_S_HOME: cap->cmdchar = K_HOME; break; 1775 case K_S_END: cap->cmdchar = K_END; break; 1776 } 1777 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask); 1778 } 1779 1780 /* 1781 * If the mode is currently displayed clear the command line or update the 1782 * command displayed. 1783 */ 1784 void 1785 may_clear_cmdline(void) 1786 { 1787 if (mode_displayed) 1788 clear_cmdline = TRUE; // unshow visual mode later 1789 #ifdef FEAT_CMDL_INFO 1790 else 1791 clear_showcmd(); 1792 #endif 1793 } 1794 1795 #if defined(FEAT_CMDL_INFO) || defined(PROTO) 1796 /* 1797 * Routines for displaying a partly typed command 1798 */ 1799 1800 #define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30 1801 static char_u showcmd_buf[SHOWCMD_BUFLEN]; 1802 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd() 1803 static int showcmd_is_clear = TRUE; 1804 static int showcmd_visual = FALSE; 1805 1806 static void display_showcmd(void); 1807 1808 void 1809 clear_showcmd(void) 1810 { 1811 if (!p_sc) 1812 return; 1813 1814 if (VIsual_active && !char_avail()) 1815 { 1816 int cursor_bot = LT_POS(VIsual, curwin->w_cursor); 1817 long lines; 1818 colnr_T leftcol, rightcol; 1819 linenr_T top, bot; 1820 1821 // Show the size of the Visual area. 1822 if (cursor_bot) 1823 { 1824 top = VIsual.lnum; 1825 bot = curwin->w_cursor.lnum; 1826 } 1827 else 1828 { 1829 top = curwin->w_cursor.lnum; 1830 bot = VIsual.lnum; 1831 } 1832 # ifdef FEAT_FOLDING 1833 // Include closed folds as a whole. 1834 (void)hasFolding(top, &top, NULL); 1835 (void)hasFolding(bot, NULL, &bot); 1836 # endif 1837 lines = bot - top + 1; 1838 1839 if (VIsual_mode == Ctrl_V) 1840 { 1841 # ifdef FEAT_LINEBREAK 1842 char_u *saved_sbr = p_sbr; 1843 char_u *saved_w_sbr = curwin->w_p_sbr; 1844 1845 // Make 'sbr' empty for a moment to get the correct size. 1846 p_sbr = empty_option; 1847 curwin->w_p_sbr = empty_option; 1848 # endif 1849 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol); 1850 # ifdef FEAT_LINEBREAK 1851 p_sbr = saved_sbr; 1852 curwin->w_p_sbr = saved_w_sbr; 1853 # endif 1854 sprintf((char *)showcmd_buf, "%ldx%ld", lines, 1855 (long)(rightcol - leftcol + 1)); 1856 } 1857 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum) 1858 sprintf((char *)showcmd_buf, "%ld", lines); 1859 else 1860 { 1861 char_u *s, *e; 1862 int l; 1863 int bytes = 0; 1864 int chars = 0; 1865 1866 if (cursor_bot) 1867 { 1868 s = ml_get_pos(&VIsual); 1869 e = ml_get_cursor(); 1870 } 1871 else 1872 { 1873 s = ml_get_cursor(); 1874 e = ml_get_pos(&VIsual); 1875 } 1876 while ((*p_sel != 'e') ? s <= e : s < e) 1877 { 1878 l = (*mb_ptr2len)(s); 1879 if (l == 0) 1880 { 1881 ++bytes; 1882 ++chars; 1883 break; // end of line 1884 } 1885 bytes += l; 1886 ++chars; 1887 s += l; 1888 } 1889 if (bytes == chars) 1890 sprintf((char *)showcmd_buf, "%d", chars); 1891 else 1892 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes); 1893 } 1894 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate 1895 showcmd_visual = TRUE; 1896 } 1897 else 1898 { 1899 showcmd_buf[0] = NUL; 1900 showcmd_visual = FALSE; 1901 1902 // Don't actually display something if there is nothing to clear. 1903 if (showcmd_is_clear) 1904 return; 1905 } 1906 1907 display_showcmd(); 1908 } 1909 1910 /* 1911 * Add 'c' to string of shown command chars. 1912 * Return TRUE if output has been written (and setcursor() has been called). 1913 */ 1914 int 1915 add_to_showcmd(int c) 1916 { 1917 char_u *p; 1918 int old_len; 1919 int extra_len; 1920 int overflow; 1921 int i; 1922 static int ignore[] = 1923 { 1924 #ifdef FEAT_GUI 1925 K_VER_SCROLLBAR, K_HOR_SCROLLBAR, 1926 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM, 1927 #endif 1928 K_IGNORE, K_PS, 1929 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE, 1930 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE, 1931 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE, 1932 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT, 1933 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE, 1934 K_CURSORHOLD, 1935 0 1936 }; 1937 1938 if (!p_sc || msg_silent != 0) 1939 return FALSE; 1940 1941 if (showcmd_visual) 1942 { 1943 showcmd_buf[0] = NUL; 1944 showcmd_visual = FALSE; 1945 } 1946 1947 // Ignore keys that are scrollbar updates and mouse clicks 1948 if (IS_SPECIAL(c)) 1949 for (i = 0; ignore[i] != 0; ++i) 1950 if (ignore[i] == c) 1951 return FALSE; 1952 1953 p = transchar(c); 1954 if (*p == ' ') 1955 STRCPY(p, "<20>"); 1956 old_len = (int)STRLEN(showcmd_buf); 1957 extra_len = (int)STRLEN(p); 1958 overflow = old_len + extra_len - SHOWCMD_COLS; 1959 if (overflow > 0) 1960 mch_memmove(showcmd_buf, showcmd_buf + overflow, 1961 old_len - overflow + 1); 1962 STRCAT(showcmd_buf, p); 1963 1964 if (char_avail()) 1965 return FALSE; 1966 1967 display_showcmd(); 1968 1969 return TRUE; 1970 } 1971 1972 void 1973 add_to_showcmd_c(int c) 1974 { 1975 if (!add_to_showcmd(c)) 1976 setcursor(); 1977 } 1978 1979 /* 1980 * Delete 'len' characters from the end of the shown command. 1981 */ 1982 static void 1983 del_from_showcmd(int len) 1984 { 1985 int old_len; 1986 1987 if (!p_sc) 1988 return; 1989 1990 old_len = (int)STRLEN(showcmd_buf); 1991 if (len > old_len) 1992 len = old_len; 1993 showcmd_buf[old_len - len] = NUL; 1994 1995 if (!char_avail()) 1996 display_showcmd(); 1997 } 1998 1999 /* 2000 * push_showcmd() and pop_showcmd() are used when waiting for the user to type 2001 * something and there is a partial mapping. 2002 */ 2003 void 2004 push_showcmd(void) 2005 { 2006 if (p_sc) 2007 STRCPY(old_showcmd_buf, showcmd_buf); 2008 } 2009 2010 void 2011 pop_showcmd(void) 2012 { 2013 if (!p_sc) 2014 return; 2015 2016 STRCPY(showcmd_buf, old_showcmd_buf); 2017 2018 display_showcmd(); 2019 } 2020 2021 static void 2022 display_showcmd(void) 2023 { 2024 int len; 2025 2026 cursor_off(); 2027 2028 len = (int)STRLEN(showcmd_buf); 2029 if (len == 0) 2030 showcmd_is_clear = TRUE; 2031 else 2032 { 2033 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0); 2034 showcmd_is_clear = FALSE; 2035 } 2036 2037 /* 2038 * clear the rest of an old message by outputting up to SHOWCMD_COLS 2039 * spaces 2040 */ 2041 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0); 2042 2043 setcursor(); // put cursor back where it belongs 2044 } 2045 #endif 2046 2047 /* 2048 * When "check" is FALSE, prepare for commands that scroll the window. 2049 * When "check" is TRUE, take care of scroll-binding after the window has 2050 * scrolled. Called from normal_cmd() and edit(). 2051 */ 2052 void 2053 do_check_scrollbind(int check) 2054 { 2055 static win_T *old_curwin = NULL; 2056 static linenr_T old_topline = 0; 2057 #ifdef FEAT_DIFF 2058 static int old_topfill = 0; 2059 #endif 2060 static buf_T *old_buf = NULL; 2061 static colnr_T old_leftcol = 0; 2062 2063 if (check && curwin->w_p_scb) 2064 { 2065 // If a ":syncbind" command was just used, don't scroll, only reset 2066 // the values. 2067 if (did_syncbind) 2068 did_syncbind = FALSE; 2069 else if (curwin == old_curwin) 2070 { 2071 /* 2072 * Synchronize other windows, as necessary according to 2073 * 'scrollbind'. Don't do this after an ":edit" command, except 2074 * when 'diff' is set. 2075 */ 2076 if ((curwin->w_buffer == old_buf 2077 #ifdef FEAT_DIFF 2078 || curwin->w_p_diff 2079 #endif 2080 ) 2081 && (curwin->w_topline != old_topline 2082 #ifdef FEAT_DIFF 2083 || curwin->w_topfill != old_topfill 2084 #endif 2085 || curwin->w_leftcol != old_leftcol)) 2086 { 2087 check_scrollbind(curwin->w_topline - old_topline, 2088 (long)(curwin->w_leftcol - old_leftcol)); 2089 } 2090 } 2091 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt' 2092 { 2093 /* 2094 * When switching between windows, make sure that the relative 2095 * vertical offset is valid for the new window. The relative 2096 * offset is invalid whenever another 'scrollbind' window has 2097 * scrolled to a point that would force the current window to 2098 * scroll past the beginning or end of its buffer. When the 2099 * resync is performed, some of the other 'scrollbind' windows may 2100 * need to jump so that the current window's relative position is 2101 * visible on-screen. 2102 */ 2103 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L); 2104 } 2105 curwin->w_scbind_pos = curwin->w_topline; 2106 } 2107 2108 old_curwin = curwin; 2109 old_topline = curwin->w_topline; 2110 #ifdef FEAT_DIFF 2111 old_topfill = curwin->w_topfill; 2112 #endif 2113 old_buf = curwin->w_buffer; 2114 old_leftcol = curwin->w_leftcol; 2115 } 2116 2117 /* 2118 * Synchronize any windows that have "scrollbind" set, based on the 2119 * number of rows by which the current window has changed 2120 * (1998-11-02 16:21:01 R. Edward Ralston <[email protected]>) 2121 */ 2122 void 2123 check_scrollbind(linenr_T topline_diff, long leftcol_diff) 2124 { 2125 int want_ver; 2126 int want_hor; 2127 win_T *old_curwin = curwin; 2128 buf_T *old_curbuf = curbuf; 2129 int old_VIsual_select = VIsual_select; 2130 int old_VIsual_active = VIsual_active; 2131 colnr_T tgt_leftcol = curwin->w_leftcol; 2132 long topline; 2133 long y; 2134 2135 /* 2136 * check 'scrollopt' string for vertical and horizontal scroll options 2137 */ 2138 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0); 2139 #ifdef FEAT_DIFF 2140 want_ver |= old_curwin->w_p_diff; 2141 #endif 2142 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0)); 2143 2144 /* 2145 * loop through the scrollbound windows and scroll accordingly 2146 */ 2147 VIsual_select = VIsual_active = 0; 2148 FOR_ALL_WINDOWS(curwin) 2149 { 2150 curbuf = curwin->w_buffer; 2151 // skip original window and windows with 'noscrollbind' 2152 if (curwin != old_curwin && curwin->w_p_scb) 2153 { 2154 /* 2155 * do the vertical scroll 2156 */ 2157 if (want_ver) 2158 { 2159 #ifdef FEAT_DIFF 2160 if (old_curwin->w_p_diff && curwin->w_p_diff) 2161 { 2162 diff_set_topline(old_curwin, curwin); 2163 } 2164 else 2165 #endif 2166 { 2167 curwin->w_scbind_pos += topline_diff; 2168 topline = curwin->w_scbind_pos; 2169 if (topline > curbuf->b_ml.ml_line_count) 2170 topline = curbuf->b_ml.ml_line_count; 2171 if (topline < 1) 2172 topline = 1; 2173 2174 y = topline - curwin->w_topline; 2175 if (y > 0) 2176 scrollup(y, FALSE); 2177 else 2178 scrolldown(-y, FALSE); 2179 } 2180 2181 redraw_later(VALID); 2182 cursor_correct(); 2183 curwin->w_redr_status = TRUE; 2184 } 2185 2186 /* 2187 * do the horizontal scroll 2188 */ 2189 if (want_hor && curwin->w_leftcol != tgt_leftcol) 2190 { 2191 curwin->w_leftcol = tgt_leftcol; 2192 leftcol_changed(); 2193 } 2194 } 2195 } 2196 2197 /* 2198 * reset current-window 2199 */ 2200 VIsual_select = old_VIsual_select; 2201 VIsual_active = old_VIsual_active; 2202 curwin = old_curwin; 2203 curbuf = old_curbuf; 2204 } 2205 2206 /* 2207 * Command character that's ignored. 2208 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use 2209 * xon/xoff. 2210 */ 2211 static void 2212 nv_ignore(cmdarg_T *cap) 2213 { 2214 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now 2215 } 2216 2217 /* 2218 * Command character that doesn't do anything, but unlike nv_ignore() does 2219 * start edit(). Used for "startinsert" executed while starting up. 2220 */ 2221 static void 2222 nv_nop(cmdarg_T *cap UNUSED) 2223 { 2224 } 2225 2226 /* 2227 * Command character doesn't exist. 2228 */ 2229 static void 2230 nv_error(cmdarg_T *cap) 2231 { 2232 clearopbeep(cap->oap); 2233 } 2234 2235 /* 2236 * <Help> and <F1> commands. 2237 */ 2238 static void 2239 nv_help(cmdarg_T *cap) 2240 { 2241 if (!checkclearopq(cap->oap)) 2242 ex_help(NULL); 2243 } 2244 2245 /* 2246 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor. 2247 */ 2248 static void 2249 nv_addsub(cmdarg_T *cap) 2250 { 2251 #ifdef FEAT_JOB_CHANNEL 2252 if (bt_prompt(curbuf) && !prompt_curpos_editable()) 2253 clearopbeep(cap->oap); 2254 else 2255 #endif 2256 if (!VIsual_active && cap->oap->op_type == OP_NOP) 2257 { 2258 prep_redo_cmd(cap); 2259 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB; 2260 op_addsub(cap->oap, cap->count1, cap->arg); 2261 cap->oap->op_type = OP_NOP; 2262 } 2263 else if (VIsual_active) 2264 nv_operator(cap); 2265 else 2266 clearop(cap->oap); 2267 } 2268 2269 /* 2270 * CTRL-F, CTRL-B, etc: Scroll page up or down. 2271 */ 2272 static void 2273 nv_page(cmdarg_T *cap) 2274 { 2275 if (!checkclearop(cap->oap)) 2276 { 2277 if (mod_mask & MOD_MASK_CTRL) 2278 { 2279 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward 2280 if (cap->arg == BACKWARD) 2281 goto_tabpage(-(int)cap->count1); 2282 else 2283 goto_tabpage((int)cap->count0); 2284 } 2285 else 2286 (void)onepage(cap->arg, cap->count1); 2287 } 2288 } 2289 2290 /* 2291 * Implementation of "gd" and "gD" command. 2292 */ 2293 static void 2294 nv_gd( 2295 oparg_T *oap, 2296 int nchar, 2297 int thisblock) // 1 for "1gd" and "1gD" 2298 { 2299 int len; 2300 char_u *ptr; 2301 2302 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0 2303 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START) 2304 == FAIL) 2305 clearopbeep(oap); 2306 #ifdef FEAT_FOLDING 2307 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP) 2308 foldOpenCursor(); 2309 #endif 2310 } 2311 2312 /* 2313 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE 2314 * otherwise. 2315 */ 2316 static int 2317 is_ident(char_u *line, int offset) 2318 { 2319 int i; 2320 int incomment = FALSE; 2321 int instring = 0; 2322 int prev = 0; 2323 2324 for (i = 0; i < offset && line[i] != NUL; i++) 2325 { 2326 if (instring != 0) 2327 { 2328 if (prev != '\\' && line[i] == instring) 2329 instring = 0; 2330 } 2331 else if ((line[i] == '"' || line[i] == '\'') && !incomment) 2332 { 2333 instring = line[i]; 2334 } 2335 else 2336 { 2337 if (incomment) 2338 { 2339 if (prev == '*' && line[i] == '/') 2340 incomment = FALSE; 2341 } 2342 else if (prev == '/' && line[i] == '*') 2343 { 2344 incomment = TRUE; 2345 } 2346 else if (prev == '/' && line[i] == '/') 2347 { 2348 return FALSE; 2349 } 2350 } 2351 2352 prev = line[i]; 2353 } 2354 2355 return incomment == FALSE && instring == 0; 2356 } 2357 2358 /* 2359 * Search for variable declaration of "ptr[len]". 2360 * When "locally" is TRUE in the current function ("gd"), otherwise in the 2361 * current file ("gD"). 2362 * When "thisblock" is TRUE check the {} block scope. 2363 * Return FAIL when not found. 2364 */ 2365 int 2366 find_decl( 2367 char_u *ptr, 2368 int len, 2369 int locally, 2370 int thisblock, 2371 int flags_arg) // flags passed to searchit() 2372 { 2373 char_u *pat; 2374 pos_T old_pos; 2375 pos_T par_pos; 2376 pos_T found_pos; 2377 int t; 2378 int save_p_ws; 2379 int save_p_scs; 2380 int retval = OK; 2381 int incll; 2382 int searchflags = flags_arg; 2383 int valid; 2384 2385 if ((pat = alloc(len + 7)) == NULL) 2386 return FAIL; 2387 2388 // Put "\V" before the pattern to avoid that the special meaning of "." 2389 // and "~" causes trouble. 2390 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s", 2391 len, ptr); 2392 old_pos = curwin->w_cursor; 2393 save_p_ws = p_ws; 2394 save_p_scs = p_scs; 2395 p_ws = FALSE; // don't wrap around end of file now 2396 p_scs = FALSE; // don't switch ignorecase off now 2397 2398 /* 2399 * With "gD" go to line 1. 2400 * With "gd" Search back for the start of the current function, then go 2401 * back until a blank line. If this fails go to line 1. 2402 */ 2403 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE)) 2404 { 2405 setpcmark(); // Set in findpar() otherwise 2406 curwin->w_cursor.lnum = 1; 2407 par_pos = curwin->w_cursor; 2408 } 2409 else 2410 { 2411 par_pos = curwin->w_cursor; 2412 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL) 2413 --curwin->w_cursor.lnum; 2414 } 2415 curwin->w_cursor.col = 0; 2416 2417 // Search forward for the identifier, ignore comment lines. 2418 CLEAR_POS(&found_pos); 2419 for (;;) 2420 { 2421 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD, 2422 pat, 1L, searchflags, RE_LAST, NULL); 2423 if (curwin->w_cursor.lnum >= old_pos.lnum) 2424 t = FAIL; // match after start is failure too 2425 2426 if (thisblock && t != FAIL) 2427 { 2428 pos_T *pos; 2429 2430 // Check that the block the match is in doesn't end before the 2431 // position where we started the search from. 2432 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD, 2433 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL 2434 && pos->lnum < old_pos.lnum) 2435 { 2436 // There can't be a useful match before the end of this block. 2437 // Skip to the end. 2438 curwin->w_cursor = *pos; 2439 continue; 2440 } 2441 } 2442 2443 if (t == FAIL) 2444 { 2445 // If we previously found a valid position, use it. 2446 if (found_pos.lnum != 0) 2447 { 2448 curwin->w_cursor = found_pos; 2449 t = OK; 2450 } 2451 break; 2452 } 2453 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0) 2454 { 2455 // Ignore this line, continue at start of next line. 2456 ++curwin->w_cursor.lnum; 2457 curwin->w_cursor.col = 0; 2458 continue; 2459 } 2460 valid = is_ident(ml_get_curline(), curwin->w_cursor.col); 2461 2462 // If the current position is not a valid identifier and a previous 2463 // match is present, favor that one instead. 2464 if (!valid && found_pos.lnum != 0) 2465 { 2466 curwin->w_cursor = found_pos; 2467 break; 2468 } 2469 2470 // Global search: use first valid match found 2471 if (valid && !locally) 2472 break; 2473 if (valid && curwin->w_cursor.lnum >= par_pos.lnum) 2474 { 2475 // If we previously found a valid position, use it. 2476 if (found_pos.lnum != 0) 2477 curwin->w_cursor = found_pos; 2478 break; 2479 } 2480 2481 // For finding a local variable and the match is before the "{" or 2482 // inside a comment, continue searching. For K&R style function 2483 // declarations this skips the function header without types. 2484 if (!valid) 2485 CLEAR_POS(&found_pos); 2486 else 2487 found_pos = curwin->w_cursor; 2488 // Remove SEARCH_START from flags to avoid getting stuck at one 2489 // position. 2490 searchflags &= ~SEARCH_START; 2491 } 2492 2493 if (t == FAIL) 2494 { 2495 retval = FAIL; 2496 curwin->w_cursor = old_pos; 2497 } 2498 else 2499 { 2500 curwin->w_set_curswant = TRUE; 2501 // "n" searches forward now 2502 reset_search_dir(); 2503 } 2504 2505 vim_free(pat); 2506 p_ws = save_p_ws; 2507 p_scs = save_p_scs; 2508 2509 return retval; 2510 } 2511 2512 /* 2513 * Move 'dist' lines in direction 'dir', counting lines by *screen* 2514 * lines rather than lines in the file. 2515 * 'dist' must be positive. 2516 * 2517 * Return OK if able to move cursor, FAIL otherwise. 2518 */ 2519 static int 2520 nv_screengo(oparg_T *oap, int dir, long dist) 2521 { 2522 int linelen = linetabsize(ml_get_curline()); 2523 int retval = OK; 2524 int atend = FALSE; 2525 int n; 2526 int col_off1; // margin offset for first screen line 2527 int col_off2; // margin offset for wrapped screen line 2528 int width1; // text width for first screen line 2529 int width2; // test width for wrapped screen line 2530 2531 oap->motion_type = MCHAR; 2532 oap->inclusive = (curwin->w_curswant == MAXCOL); 2533 2534 col_off1 = curwin_col_off(); 2535 col_off2 = col_off1 - curwin_col_off2(); 2536 width1 = curwin->w_width - col_off1; 2537 width2 = curwin->w_width - col_off2; 2538 if (width2 == 0) 2539 width2 = 1; // avoid divide by zero 2540 2541 if (curwin->w_width != 0) 2542 { 2543 /* 2544 * Instead of sticking at the last character of the buffer line we 2545 * try to stick in the last column of the screen. 2546 */ 2547 if (curwin->w_curswant == MAXCOL) 2548 { 2549 atend = TRUE; 2550 validate_virtcol(); 2551 if (width1 <= 0) 2552 curwin->w_curswant = 0; 2553 else 2554 { 2555 curwin->w_curswant = width1 - 1; 2556 if (curwin->w_virtcol > curwin->w_curswant) 2557 curwin->w_curswant += ((curwin->w_virtcol 2558 - curwin->w_curswant - 1) / width2 + 1) * width2; 2559 } 2560 } 2561 else 2562 { 2563 if (linelen > width1) 2564 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1; 2565 else 2566 n = width1; 2567 if (curwin->w_curswant >= (colnr_T)n) 2568 curwin->w_curswant = n - 1; 2569 } 2570 2571 while (dist--) 2572 { 2573 if (dir == BACKWARD) 2574 { 2575 if ((long)curwin->w_curswant >= width1 2576 #ifdef FEAT_FOLDING 2577 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL) 2578 #endif 2579 ) 2580 // Move back within the line. This can give a negative value 2581 // for w_curswant if width1 < width2 (with cpoptions+=n), 2582 // which will get clipped to column 0. 2583 curwin->w_curswant -= width2; 2584 else 2585 { 2586 // to previous line 2587 #ifdef FEAT_FOLDING 2588 // Move to the start of a closed fold. Don't do that when 2589 // 'foldopen' contains "all": it will open in a moment. 2590 if (!(fdo_flags & FDO_ALL)) 2591 (void)hasFolding(curwin->w_cursor.lnum, 2592 &curwin->w_cursor.lnum, NULL); 2593 #endif 2594 if (curwin->w_cursor.lnum == 1) 2595 { 2596 retval = FAIL; 2597 break; 2598 } 2599 --curwin->w_cursor.lnum; 2600 2601 linelen = linetabsize(ml_get_curline()); 2602 if (linelen > width1) 2603 curwin->w_curswant += (((linelen - width1 - 1) / width2) 2604 + 1) * width2; 2605 } 2606 } 2607 else // dir == FORWARD 2608 { 2609 if (linelen > width1) 2610 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1; 2611 else 2612 n = width1; 2613 if (curwin->w_curswant + width2 < (colnr_T)n 2614 #ifdef FEAT_FOLDING 2615 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL) 2616 #endif 2617 ) 2618 // move forward within line 2619 curwin->w_curswant += width2; 2620 else 2621 { 2622 // to next line 2623 #ifdef FEAT_FOLDING 2624 // Move to the end of a closed fold. 2625 (void)hasFolding(curwin->w_cursor.lnum, NULL, 2626 &curwin->w_cursor.lnum); 2627 #endif 2628 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) 2629 { 2630 retval = FAIL; 2631 break; 2632 } 2633 curwin->w_cursor.lnum++; 2634 curwin->w_curswant %= width2; 2635 // Check if the cursor has moved below the number display 2636 // when width1 < width2 (with cpoptions+=n). Subtract width2 2637 // to get a negative value for w_curswant, which will get 2638 // clipped to column 0. 2639 if (curwin->w_curswant >= width1) 2640 curwin->w_curswant -= width2; 2641 linelen = linetabsize(ml_get_curline()); 2642 } 2643 } 2644 } 2645 } 2646 2647 if (virtual_active() && atend) 2648 coladvance(MAXCOL); 2649 else 2650 coladvance(curwin->w_curswant); 2651 2652 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) 2653 { 2654 colnr_T virtcol; 2655 2656 /* 2657 * Check for landing on a character that got split at the end of the 2658 * last line. We want to advance a screenline, not end up in the same 2659 * screenline or move two screenlines. 2660 */ 2661 validate_virtcol(); 2662 virtcol = curwin->w_virtcol; 2663 #if defined(FEAT_LINEBREAK) 2664 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL) 2665 virtcol -= vim_strsize(get_showbreak_value(curwin)); 2666 #endif 2667 2668 if (virtcol > curwin->w_curswant 2669 && (curwin->w_curswant < (colnr_T)width1 2670 ? (curwin->w_curswant > (colnr_T)width1 / 2) 2671 : ((curwin->w_curswant - width1) % width2 2672 > (colnr_T)width2 / 2))) 2673 --curwin->w_cursor.col; 2674 } 2675 2676 if (atend) 2677 curwin->w_curswant = MAXCOL; // stick in the last column 2678 2679 return retval; 2680 } 2681 2682 /* 2683 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down. 2684 * cap->arg must be TRUE for CTRL-E. 2685 */ 2686 void 2687 nv_scroll_line(cmdarg_T *cap) 2688 { 2689 if (!checkclearop(cap->oap)) 2690 scroll_redraw(cap->arg, cap->count1); 2691 } 2692 2693 /* 2694 * Scroll "count" lines up or down, and redraw. 2695 */ 2696 void 2697 scroll_redraw(int up, long count) 2698 { 2699 linenr_T prev_topline = curwin->w_topline; 2700 #ifdef FEAT_DIFF 2701 int prev_topfill = curwin->w_topfill; 2702 #endif 2703 linenr_T prev_lnum = curwin->w_cursor.lnum; 2704 2705 if (up) 2706 scrollup(count, TRUE); 2707 else 2708 scrolldown(count, TRUE); 2709 if (get_scrolloff_value()) 2710 { 2711 // Adjust the cursor position for 'scrolloff'. Mark w_topline as 2712 // valid, otherwise the screen jumps back at the end of the file. 2713 cursor_correct(); 2714 check_cursor_moved(curwin); 2715 curwin->w_valid |= VALID_TOPLINE; 2716 2717 // If moved back to where we were, at least move the cursor, otherwise 2718 // we get stuck at one position. Don't move the cursor up if the 2719 // first line of the buffer is already on the screen 2720 while (curwin->w_topline == prev_topline 2721 #ifdef FEAT_DIFF 2722 && curwin->w_topfill == prev_topfill 2723 #endif 2724 ) 2725 { 2726 if (up) 2727 { 2728 if (curwin->w_cursor.lnum > prev_lnum 2729 || cursor_down(1L, FALSE) == FAIL) 2730 break; 2731 } 2732 else 2733 { 2734 if (curwin->w_cursor.lnum < prev_lnum 2735 || prev_topline == 1L 2736 || cursor_up(1L, FALSE) == FAIL) 2737 break; 2738 } 2739 // Mark w_topline as valid, otherwise the screen jumps back at the 2740 // end of the file. 2741 check_cursor_moved(curwin); 2742 curwin->w_valid |= VALID_TOPLINE; 2743 } 2744 } 2745 if (curwin->w_cursor.lnum != prev_lnum) 2746 coladvance(curwin->w_curswant); 2747 redraw_later(VALID); 2748 } 2749 2750 /* 2751 * Commands that start with "z". 2752 */ 2753 static void 2754 nv_zet(cmdarg_T *cap) 2755 { 2756 long n; 2757 colnr_T col; 2758 int nchar = cap->nchar; 2759 #ifdef FEAT_FOLDING 2760 long old_fdl = curwin->w_p_fdl; 2761 int old_fen = curwin->w_p_fen; 2762 #endif 2763 #ifdef FEAT_SPELL 2764 int undo = FALSE; 2765 #endif 2766 long siso = get_sidescrolloff_value(); 2767 2768 if (VIM_ISDIGIT(nchar)) 2769 { 2770 /* 2771 * "z123{nchar}": edit the count before obtaining {nchar} 2772 */ 2773 if (checkclearop(cap->oap)) 2774 return; 2775 n = nchar - '0'; 2776 for (;;) 2777 { 2778 #ifdef USE_ON_FLY_SCROLL 2779 dont_scroll = TRUE; // disallow scrolling here 2780 #endif 2781 ++no_mapping; 2782 ++allow_keys; // no mapping for nchar, but allow key codes 2783 nchar = plain_vgetc(); 2784 LANGMAP_ADJUST(nchar, TRUE); 2785 --no_mapping; 2786 --allow_keys; 2787 #ifdef FEAT_CMDL_INFO 2788 (void)add_to_showcmd(nchar); 2789 #endif 2790 if (nchar == K_DEL || nchar == K_KDEL) 2791 n /= 10; 2792 else if (VIM_ISDIGIT(nchar)) 2793 n = n * 10 + (nchar - '0'); 2794 else if (nchar == CAR) 2795 { 2796 #ifdef FEAT_GUI 2797 need_mouse_correct = TRUE; 2798 #endif 2799 win_setheight((int)n); 2800 break; 2801 } 2802 else if (nchar == 'l' 2803 || nchar == 'h' 2804 || nchar == K_LEFT 2805 || nchar == K_RIGHT) 2806 { 2807 cap->count1 = n ? n * cap->count1 : cap->count1; 2808 goto dozet; 2809 } 2810 else 2811 { 2812 clearopbeep(cap->oap); 2813 break; 2814 } 2815 } 2816 cap->oap->op_type = OP_NOP; 2817 return; 2818 } 2819 2820 dozet: 2821 if ( 2822 #ifdef FEAT_FOLDING 2823 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc" 2824 // and "zC" only in Visual mode. "zj" and "zk" are motion 2825 // commands. 2826 cap->nchar != 'f' && cap->nchar != 'F' 2827 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar)) 2828 && cap->nchar != 'j' && cap->nchar != 'k' 2829 && 2830 #endif 2831 checkclearop(cap->oap)) 2832 return; 2833 2834 /* 2835 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb": 2836 * If line number given, set cursor. 2837 */ 2838 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL) 2839 && cap->count0 2840 && cap->count0 != curwin->w_cursor.lnum) 2841 { 2842 setpcmark(); 2843 if (cap->count0 > curbuf->b_ml.ml_line_count) 2844 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 2845 else 2846 curwin->w_cursor.lnum = cap->count0; 2847 check_cursor_col(); 2848 } 2849 2850 switch (nchar) 2851 { 2852 // "z+", "z<CR>" and "zt": put cursor at top of screen 2853 case '+': 2854 if (cap->count0 == 0) 2855 { 2856 // No count given: put cursor at the line below screen 2857 validate_botline(); // make sure w_botline is valid 2858 if (curwin->w_botline > curbuf->b_ml.ml_line_count) 2859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 2860 else 2861 curwin->w_cursor.lnum = curwin->w_botline; 2862 } 2863 // FALLTHROUGH 2864 case NL: 2865 case CAR: 2866 case K_KENTER: 2867 beginline(BL_WHITE | BL_FIX); 2868 // FALLTHROUGH 2869 2870 case 't': scroll_cursor_top(0, TRUE); 2871 redraw_later(VALID); 2872 set_fraction(curwin); 2873 break; 2874 2875 // "z." and "zz": put cursor in middle of screen 2876 case '.': beginline(BL_WHITE | BL_FIX); 2877 // FALLTHROUGH 2878 2879 case 'z': scroll_cursor_halfway(TRUE); 2880 redraw_later(VALID); 2881 set_fraction(curwin); 2882 break; 2883 2884 // "z^", "z-" and "zb": put cursor at bottom of screen 2885 case '^': // Strange Vi behavior: <count>z^ finds line at top of window 2886 // when <count> is at bottom of window, and puts that one at 2887 // bottom of window. 2888 if (cap->count0 != 0) 2889 { 2890 scroll_cursor_bot(0, TRUE); 2891 curwin->w_cursor.lnum = curwin->w_topline; 2892 } 2893 else if (curwin->w_topline == 1) 2894 curwin->w_cursor.lnum = 1; 2895 else 2896 curwin->w_cursor.lnum = curwin->w_topline - 1; 2897 // FALLTHROUGH 2898 case '-': 2899 beginline(BL_WHITE | BL_FIX); 2900 // FALLTHROUGH 2901 2902 case 'b': scroll_cursor_bot(0, TRUE); 2903 redraw_later(VALID); 2904 set_fraction(curwin); 2905 break; 2906 2907 // "zH" - scroll screen right half-page 2908 case 'H': 2909 cap->count1 *= curwin->w_width / 2; 2910 // FALLTHROUGH 2911 2912 // "zh" - scroll screen to the right 2913 case 'h': 2914 case K_LEFT: 2915 if (!curwin->w_p_wrap) 2916 { 2917 if ((colnr_T)cap->count1 > curwin->w_leftcol) 2918 curwin->w_leftcol = 0; 2919 else 2920 curwin->w_leftcol -= (colnr_T)cap->count1; 2921 leftcol_changed(); 2922 } 2923 break; 2924 2925 // "zL" - scroll screen left half-page 2926 case 'L': cap->count1 *= curwin->w_width / 2; 2927 // FALLTHROUGH 2928 2929 // "zl" - scroll screen to the left 2930 case 'l': 2931 case K_RIGHT: 2932 if (!curwin->w_p_wrap) 2933 { 2934 // scroll the window left 2935 curwin->w_leftcol += (colnr_T)cap->count1; 2936 leftcol_changed(); 2937 } 2938 break; 2939 2940 // "zs" - scroll screen, cursor at the start 2941 case 's': if (!curwin->w_p_wrap) 2942 { 2943 #ifdef FEAT_FOLDING 2944 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) 2945 col = 0; // like the cursor is in col 0 2946 else 2947 #endif 2948 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL); 2949 if ((long)col > siso) 2950 col -= siso; 2951 else 2952 col = 0; 2953 if (curwin->w_leftcol != col) 2954 { 2955 curwin->w_leftcol = col; 2956 redraw_later(NOT_VALID); 2957 } 2958 } 2959 break; 2960 2961 // "ze" - scroll screen, cursor at the end 2962 case 'e': if (!curwin->w_p_wrap) 2963 { 2964 #ifdef FEAT_FOLDING 2965 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) 2966 col = 0; // like the cursor is in col 0 2967 else 2968 #endif 2969 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); 2970 n = curwin->w_width - curwin_col_off(); 2971 if ((long)col + siso < n) 2972 col = 0; 2973 else 2974 col = col + siso - n + 1; 2975 if (curwin->w_leftcol != col) 2976 { 2977 curwin->w_leftcol = col; 2978 redraw_later(NOT_VALID); 2979 } 2980 } 2981 break; 2982 2983 // "zp", "zP" in block mode put without addind trailing spaces 2984 case 'P': 2985 case 'p': nv_put(cap); 2986 break; 2987 // "zy" Yank without trailing spaces 2988 case 'y': nv_operator(cap); 2989 break; 2990 #ifdef FEAT_FOLDING 2991 // "zF": create fold command 2992 // "zf": create fold operator 2993 case 'F': 2994 case 'f': if (foldManualAllowed(TRUE)) 2995 { 2996 cap->nchar = 'f'; 2997 nv_operator(cap); 2998 curwin->w_p_fen = TRUE; 2999 3000 // "zF" is like "zfzf" 3001 if (nchar == 'F' && cap->oap->op_type == OP_FOLD) 3002 { 3003 nv_operator(cap); 3004 finish_op = TRUE; 3005 } 3006 } 3007 else 3008 clearopbeep(cap->oap); 3009 break; 3010 3011 // "zd": delete fold at cursor 3012 // "zD": delete fold at cursor recursively 3013 case 'd': 3014 case 'D': if (foldManualAllowed(FALSE)) 3015 { 3016 if (VIsual_active) 3017 nv_operator(cap); 3018 else 3019 deleteFold(curwin->w_cursor.lnum, 3020 curwin->w_cursor.lnum, nchar == 'D', FALSE); 3021 } 3022 break; 3023 3024 // "zE": erase all folds 3025 case 'E': if (foldmethodIsManual(curwin)) 3026 { 3027 clearFolding(curwin); 3028 changed_window_setting(); 3029 } 3030 else if (foldmethodIsMarker(curwin)) 3031 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count, 3032 TRUE, FALSE); 3033 else 3034 emsg(_("E352: Cannot erase folds with current 'foldmethod'")); 3035 break; 3036 3037 // "zn": fold none: reset 'foldenable' 3038 case 'n': curwin->w_p_fen = FALSE; 3039 break; 3040 3041 // "zN": fold Normal: set 'foldenable' 3042 case 'N': curwin->w_p_fen = TRUE; 3043 break; 3044 3045 // "zi": invert folding: toggle 'foldenable' 3046 case 'i': curwin->w_p_fen = !curwin->w_p_fen; 3047 break; 3048 3049 // "za": open closed fold or close open fold at cursor 3050 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) 3051 openFold(curwin->w_cursor.lnum, cap->count1); 3052 else 3053 { 3054 closeFold(curwin->w_cursor.lnum, cap->count1); 3055 curwin->w_p_fen = TRUE; 3056 } 3057 break; 3058 3059 // "zA": open fold at cursor recursively 3060 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL)) 3061 openFoldRecurse(curwin->w_cursor.lnum); 3062 else 3063 { 3064 closeFoldRecurse(curwin->w_cursor.lnum); 3065 curwin->w_p_fen = TRUE; 3066 } 3067 break; 3068 3069 // "zo": open fold at cursor or Visual area 3070 case 'o': if (VIsual_active) 3071 nv_operator(cap); 3072 else 3073 openFold(curwin->w_cursor.lnum, cap->count1); 3074 break; 3075 3076 // "zO": open fold recursively 3077 case 'O': if (VIsual_active) 3078 nv_operator(cap); 3079 else 3080 openFoldRecurse(curwin->w_cursor.lnum); 3081 break; 3082 3083 // "zc": close fold at cursor or Visual area 3084 case 'c': if (VIsual_active) 3085 nv_operator(cap); 3086 else 3087 closeFold(curwin->w_cursor.lnum, cap->count1); 3088 curwin->w_p_fen = TRUE; 3089 break; 3090 3091 // "zC": close fold recursively 3092 case 'C': if (VIsual_active) 3093 nv_operator(cap); 3094 else 3095 closeFoldRecurse(curwin->w_cursor.lnum); 3096 curwin->w_p_fen = TRUE; 3097 break; 3098 3099 // "zv": open folds at the cursor 3100 case 'v': foldOpenCursor(); 3101 break; 3102 3103 // "zx": re-apply 'foldlevel' and open folds at the cursor 3104 case 'x': curwin->w_p_fen = TRUE; 3105 curwin->w_foldinvalid = TRUE; // recompute folds 3106 newFoldLevel(); // update right now 3107 foldOpenCursor(); 3108 break; 3109 3110 // "zX": undo manual opens/closes, re-apply 'foldlevel' 3111 case 'X': curwin->w_p_fen = TRUE; 3112 curwin->w_foldinvalid = TRUE; // recompute folds 3113 old_fdl = -1; // force an update 3114 break; 3115 3116 // "zm": fold more 3117 case 'm': if (curwin->w_p_fdl > 0) 3118 { 3119 curwin->w_p_fdl -= cap->count1; 3120 if (curwin->w_p_fdl < 0) 3121 curwin->w_p_fdl = 0; 3122 } 3123 old_fdl = -1; // force an update 3124 curwin->w_p_fen = TRUE; 3125 break; 3126 3127 // "zM": close all folds 3128 case 'M': curwin->w_p_fdl = 0; 3129 old_fdl = -1; // force an update 3130 curwin->w_p_fen = TRUE; 3131 break; 3132 3133 // "zr": reduce folding 3134 case 'r': curwin->w_p_fdl += cap->count1; 3135 { 3136 int d = getDeepestNesting(); 3137 3138 if (curwin->w_p_fdl >= d) 3139 curwin->w_p_fdl = d; 3140 } 3141 break; 3142 3143 // "zR": open all folds 3144 case 'R': curwin->w_p_fdl = getDeepestNesting(); 3145 old_fdl = -1; // force an update 3146 break; 3147 3148 case 'j': // "zj" move to next fold downwards 3149 case 'k': // "zk" move to next fold upwards 3150 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD, 3151 cap->count1) == FAIL) 3152 clearopbeep(cap->oap); 3153 break; 3154 3155 #endif // FEAT_FOLDING 3156 3157 #ifdef FEAT_SPELL 3158 case 'u': // "zug" and "zuw": undo "zg" and "zw" 3159 ++no_mapping; 3160 ++allow_keys; // no mapping for nchar, but allow key codes 3161 nchar = plain_vgetc(); 3162 LANGMAP_ADJUST(nchar, TRUE); 3163 --no_mapping; 3164 --allow_keys; 3165 #ifdef FEAT_CMDL_INFO 3166 (void)add_to_showcmd(nchar); 3167 #endif 3168 if (vim_strchr((char_u *)"gGwW", nchar) == NULL) 3169 { 3170 clearopbeep(cap->oap); 3171 break; 3172 } 3173 undo = TRUE; 3174 // FALLTHROUGH 3175 3176 case 'g': // "zg": add good word to word list 3177 case 'w': // "zw": add wrong word to word list 3178 case 'G': // "zG": add good word to temp word list 3179 case 'W': // "zW": add wrong word to temp word list 3180 { 3181 char_u *ptr = NULL; 3182 int len; 3183 3184 if (checkclearop(cap->oap)) 3185 break; 3186 if (VIsual_active && get_visual_text(cap, &ptr, &len) 3187 == FAIL) 3188 return; 3189 if (ptr == NULL) 3190 { 3191 pos_T pos = curwin->w_cursor; 3192 3193 // Find bad word under the cursor. When 'spell' is 3194 // off this fails and find_ident_under_cursor() is 3195 // used below. 3196 emsg_off++; 3197 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL); 3198 emsg_off--; 3199 if (len != 0 && curwin->w_cursor.col <= pos.col) 3200 ptr = ml_get_pos(&curwin->w_cursor); 3201 curwin->w_cursor = pos; 3202 } 3203 3204 if (ptr == NULL && (len = find_ident_under_cursor(&ptr, 3205 FIND_IDENT)) == 0) 3206 return; 3207 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W' 3208 ? SPELL_ADD_BAD : SPELL_ADD_GOOD, 3209 (nchar == 'G' || nchar == 'W') 3210 ? 0 : (int)cap->count1, 3211 undo); 3212 } 3213 break; 3214 3215 case '=': // "z=": suggestions for a badly spelled word 3216 if (!checkclearop(cap->oap)) 3217 spell_suggest((int)cap->count0); 3218 break; 3219 #endif 3220 3221 default: clearopbeep(cap->oap); 3222 } 3223 3224 #ifdef FEAT_FOLDING 3225 // Redraw when 'foldenable' changed 3226 if (old_fen != curwin->w_p_fen) 3227 { 3228 # ifdef FEAT_DIFF 3229 win_T *wp; 3230 3231 if (foldmethodIsDiff(curwin) && curwin->w_p_scb) 3232 { 3233 // Adjust 'foldenable' in diff-synced windows. 3234 FOR_ALL_WINDOWS(wp) 3235 { 3236 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb) 3237 { 3238 wp->w_p_fen = curwin->w_p_fen; 3239 changed_window_setting_win(wp); 3240 } 3241 } 3242 } 3243 # endif 3244 changed_window_setting(); 3245 } 3246 3247 // Redraw when 'foldlevel' changed. 3248 if (old_fdl != curwin->w_p_fdl) 3249 newFoldLevel(); 3250 #endif 3251 } 3252 3253 #ifdef FEAT_GUI 3254 /* 3255 * Vertical scrollbar movement. 3256 */ 3257 static void 3258 nv_ver_scrollbar(cmdarg_T *cap) 3259 { 3260 if (cap->oap->op_type != OP_NOP) 3261 clearopbeep(cap->oap); 3262 3263 // Even if an operator was pending, we still want to scroll 3264 gui_do_scroll(); 3265 } 3266 3267 /* 3268 * Horizontal scrollbar movement. 3269 */ 3270 static void 3271 nv_hor_scrollbar(cmdarg_T *cap) 3272 { 3273 if (cap->oap->op_type != OP_NOP) 3274 clearopbeep(cap->oap); 3275 3276 // Even if an operator was pending, we still want to scroll 3277 gui_do_horiz_scroll(scrollbar_value, FALSE); 3278 } 3279 #endif 3280 3281 #if defined(FEAT_GUI_TABLINE) || defined(PROTO) 3282 /* 3283 * Click in GUI tab. 3284 */ 3285 static void 3286 nv_tabline(cmdarg_T *cap) 3287 { 3288 if (cap->oap->op_type != OP_NOP) 3289 clearopbeep(cap->oap); 3290 3291 // Even if an operator was pending, we still want to jump tabs. 3292 goto_tabpage(current_tab); 3293 } 3294 3295 /* 3296 * Selected item in tab line menu. 3297 */ 3298 static void 3299 nv_tabmenu(cmdarg_T *cap) 3300 { 3301 if (cap->oap->op_type != OP_NOP) 3302 clearopbeep(cap->oap); 3303 3304 // Even if an operator was pending, we still want to jump tabs. 3305 handle_tabmenu(); 3306 } 3307 3308 /* 3309 * Handle selecting an item of the GUI tab line menu. 3310 * Used in Normal and Insert mode. 3311 */ 3312 void 3313 handle_tabmenu(void) 3314 { 3315 switch (current_tabmenu) 3316 { 3317 case TABLINE_MENU_CLOSE: 3318 if (current_tab == 0) 3319 do_cmdline_cmd((char_u *)"tabclose"); 3320 else 3321 { 3322 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d", 3323 current_tab); 3324 do_cmdline_cmd(IObuff); 3325 } 3326 break; 3327 3328 case TABLINE_MENU_NEW: 3329 if (current_tab == 0) 3330 do_cmdline_cmd((char_u *)"$tabnew"); 3331 else 3332 { 3333 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew", 3334 current_tab - 1); 3335 do_cmdline_cmd(IObuff); 3336 } 3337 break; 3338 3339 case TABLINE_MENU_OPEN: 3340 if (current_tab == 0) 3341 do_cmdline_cmd((char_u *)"browse $tabnew"); 3342 else 3343 { 3344 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew", 3345 current_tab - 1); 3346 do_cmdline_cmd(IObuff); 3347 } 3348 break; 3349 } 3350 } 3351 #endif 3352 3353 /* 3354 * "Q" command. 3355 */ 3356 static void 3357 nv_exmode(cmdarg_T *cap) 3358 { 3359 /* 3360 * Ignore 'Q' in Visual mode, just give a beep. 3361 */ 3362 if (VIsual_active) 3363 vim_beep(BO_EX); 3364 else if (!checkclearop(cap->oap)) 3365 do_exmode(FALSE); 3366 } 3367 3368 /* 3369 * Handle a ":" command. 3370 */ 3371 static void 3372 nv_colon(cmdarg_T *cap) 3373 { 3374 int old_p_im; 3375 int cmd_result; 3376 int is_cmdkey = cap->cmdchar == K_COMMAND; 3377 3378 if (VIsual_active && !is_cmdkey) 3379 nv_operator(cap); 3380 else 3381 { 3382 if (cap->oap->op_type != OP_NOP) 3383 { 3384 // Using ":" as a movement is characterwise exclusive. 3385 cap->oap->motion_type = MCHAR; 3386 cap->oap->inclusive = FALSE; 3387 } 3388 else if (cap->count0 && !is_cmdkey) 3389 { 3390 // translate "count:" into ":.,.+(count - 1)" 3391 stuffcharReadbuff('.'); 3392 if (cap->count0 > 1) 3393 { 3394 stuffReadbuff((char_u *)",.+"); 3395 stuffnumReadbuff((long)cap->count0 - 1L); 3396 } 3397 } 3398 3399 // When typing, don't type below an old message 3400 if (KeyTyped) 3401 compute_cmdrow(); 3402 3403 old_p_im = p_im; 3404 3405 // get a command line and execute it 3406 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL, 3407 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0); 3408 3409 // If 'insertmode' changed, enter or exit Insert mode 3410 if (p_im != old_p_im) 3411 { 3412 if (p_im) 3413 restart_edit = 'i'; 3414 else 3415 restart_edit = 0; 3416 } 3417 3418 if (cmd_result == FAIL) 3419 // The Ex command failed, do not execute the operator. 3420 clearop(cap->oap); 3421 else if (cap->oap->op_type != OP_NOP 3422 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count 3423 || cap->oap->start.col > 3424 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum)) 3425 || did_emsg 3426 )) 3427 // The start of the operator has become invalid by the Ex command. 3428 clearopbeep(cap->oap); 3429 } 3430 } 3431 3432 /* 3433 * Handle CTRL-G command. 3434 */ 3435 static void 3436 nv_ctrlg(cmdarg_T *cap) 3437 { 3438 if (VIsual_active) // toggle Selection/Visual mode 3439 { 3440 VIsual_select = !VIsual_select; 3441 showmode(); 3442 } 3443 else if (!checkclearop(cap->oap)) 3444 // print full name if count given or :cd used 3445 fileinfo((int)cap->count0, FALSE, TRUE); 3446 } 3447 3448 /* 3449 * Handle CTRL-H <Backspace> command. 3450 */ 3451 static void 3452 nv_ctrlh(cmdarg_T *cap) 3453 { 3454 if (VIsual_active && VIsual_select) 3455 { 3456 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode 3457 v_visop(cap); 3458 } 3459 else 3460 nv_left(cap); 3461 } 3462 3463 /* 3464 * CTRL-L: clear screen and redraw. 3465 */ 3466 static void 3467 nv_clear(cmdarg_T *cap) 3468 { 3469 if (!checkclearop(cap->oap)) 3470 { 3471 #ifdef FEAT_SYN_HL 3472 // Clear all syntax states to force resyncing. 3473 syn_stack_free_all(curwin->w_s); 3474 # ifdef FEAT_RELTIME 3475 { 3476 win_T *wp; 3477 3478 FOR_ALL_WINDOWS(wp) 3479 wp->w_s->b_syn_slow = FALSE; 3480 } 3481 # endif 3482 #endif 3483 redraw_later(CLEAR); 3484 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) 3485 # ifdef VIMDLL 3486 if (!gui.in_use) 3487 # endif 3488 resize_console_buf(); 3489 #endif 3490 } 3491 } 3492 3493 /* 3494 * CTRL-O: In Select mode: switch to Visual mode for one command. 3495 * Otherwise: Go to older pcmark. 3496 */ 3497 static void 3498 nv_ctrlo(cmdarg_T *cap) 3499 { 3500 if (VIsual_active && VIsual_select) 3501 { 3502 VIsual_select = FALSE; 3503 showmode(); 3504 restart_VIsual_select = 2; // restart Select mode later 3505 } 3506 else 3507 { 3508 cap->count1 = -cap->count1; 3509 nv_pcmark(cap); 3510 } 3511 } 3512 3513 /* 3514 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is 3515 * not named. 3516 */ 3517 static void 3518 nv_hat(cmdarg_T *cap) 3519 { 3520 if (!checkclearopq(cap->oap)) 3521 (void)buflist_getfile((int)cap->count0, (linenr_T)0, 3522 GETF_SETMARK|GETF_ALT, FALSE); 3523 } 3524 3525 /* 3526 * "Z" commands. 3527 */ 3528 static void 3529 nv_Zet(cmdarg_T *cap) 3530 { 3531 if (!checkclearopq(cap->oap)) 3532 { 3533 switch (cap->nchar) 3534 { 3535 // "ZZ": equivalent to ":x". 3536 case 'Z': do_cmdline_cmd((char_u *)"x"); 3537 break; 3538 3539 // "ZQ": equivalent to ":q!" (Elvis compatible). 3540 case 'Q': do_cmdline_cmd((char_u *)"q!"); 3541 break; 3542 3543 default: clearopbeep(cap->oap); 3544 } 3545 } 3546 } 3547 3548 /* 3549 * Call nv_ident() as if "c1" was used, with "c2" as next character. 3550 */ 3551 void 3552 do_nv_ident(int c1, int c2) 3553 { 3554 oparg_T oa; 3555 cmdarg_T ca; 3556 3557 clear_oparg(&oa); 3558 CLEAR_FIELD(ca); 3559 ca.oap = &oa; 3560 ca.cmdchar = c1; 3561 ca.nchar = c2; 3562 nv_ident(&ca); 3563 } 3564 3565 /* 3566 * Handle the commands that use the word under the cursor. 3567 * [g] CTRL-] :ta to current identifier 3568 * [g] 'K' run program for current identifier 3569 * [g] '*' / to current identifier or string 3570 * [g] '#' ? to current identifier or string 3571 * g ']' :tselect for current identifier 3572 */ 3573 static void 3574 nv_ident(cmdarg_T *cap) 3575 { 3576 char_u *ptr = NULL; 3577 char_u *buf; 3578 unsigned buflen; 3579 char_u *newbuf; 3580 char_u *p; 3581 char_u *kp; // value of 'keywordprg' 3582 int kp_help; // 'keywordprg' is ":he" 3583 int kp_ex; // 'keywordprg' starts with ":" 3584 int n = 0; // init for GCC 3585 int cmdchar; 3586 int g_cmd; // "g" command 3587 int tag_cmd = FALSE; 3588 char_u *aux_ptr; 3589 int isman; 3590 int isman_s; 3591 3592 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]" 3593 { 3594 cmdchar = cap->nchar; 3595 g_cmd = TRUE; 3596 } 3597 else 3598 { 3599 cmdchar = cap->cmdchar; 3600 g_cmd = FALSE; 3601 } 3602 3603 if (cmdchar == POUND) // the pound sign, '#' for English keyboards 3604 cmdchar = '#'; 3605 3606 /* 3607 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode. 3608 */ 3609 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K') 3610 { 3611 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL) 3612 return; 3613 if (checkclearopq(cap->oap)) 3614 return; 3615 } 3616 3617 if (ptr == NULL && (n = find_ident_under_cursor(&ptr, 3618 (cmdchar == '*' || cmdchar == '#') 3619 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0) 3620 { 3621 clearop(cap->oap); 3622 return; 3623 } 3624 3625 // Allocate buffer to put the command in. Inserting backslashes can 3626 // double the length of the word. p_kp / curbuf->b_p_kp could be added 3627 // and some numbers. 3628 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp); 3629 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0 3630 || STRCMP(kp, ":help") == 0); 3631 if (kp_help && *skipwhite(ptr) == NUL) 3632 { 3633 emsg(_(e_noident)); // found white space only 3634 return; 3635 } 3636 kp_ex = (*kp == ':'); 3637 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp)); 3638 buf = alloc(buflen); 3639 if (buf == NULL) 3640 return; 3641 buf[0] = NUL; 3642 3643 switch (cmdchar) 3644 { 3645 case '*': 3646 case '#': 3647 /* 3648 * Put cursor at start of word, makes search skip the word 3649 * under the cursor. 3650 * Call setpcmark() first, so "*``" puts the cursor back where 3651 * it was. 3652 */ 3653 setpcmark(); 3654 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline()); 3655 3656 if (!g_cmd && vim_iswordp(ptr)) 3657 STRCPY(buf, "\\<"); 3658 no_smartcase = TRUE; // don't use 'smartcase' now 3659 break; 3660 3661 case 'K': 3662 if (kp_help) 3663 STRCPY(buf, "he! "); 3664 else if (kp_ex) 3665 { 3666 if (cap->count0 != 0) 3667 vim_snprintf((char *)buf, buflen, "%s %ld", 3668 kp, cap->count0); 3669 else 3670 STRCPY(buf, kp); 3671 STRCAT(buf, " "); 3672 } 3673 else 3674 { 3675 // An external command will probably use an argument starting 3676 // with "-" as an option. To avoid trouble we skip the "-". 3677 while (*ptr == '-' && n > 0) 3678 { 3679 ++ptr; 3680 --n; 3681 } 3682 if (n == 0) 3683 { 3684 emsg(_(e_noident)); // found dashes only 3685 vim_free(buf); 3686 return; 3687 } 3688 3689 // When a count is given, turn it into a range. Is this 3690 // really what we want? 3691 isman = (STRCMP(kp, "man") == 0); 3692 isman_s = (STRCMP(kp, "man -s") == 0); 3693 if (cap->count0 != 0 && !(isman || isman_s)) 3694 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1); 3695 3696 STRCAT(buf, "! "); 3697 if (cap->count0 == 0 && isman_s) 3698 STRCAT(buf, "man"); 3699 else 3700 STRCAT(buf, kp); 3701 STRCAT(buf, " "); 3702 if (cap->count0 != 0 && (isman || isman_s)) 3703 { 3704 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0); 3705 STRCAT(buf, " "); 3706 } 3707 } 3708 break; 3709 3710 case ']': 3711 tag_cmd = TRUE; 3712 #ifdef FEAT_CSCOPE 3713 if (p_cst) 3714 STRCPY(buf, "cstag "); 3715 else 3716 #endif 3717 STRCPY(buf, "ts "); 3718 break; 3719 3720 default: 3721 tag_cmd = TRUE; 3722 if (curbuf->b_help) 3723 STRCPY(buf, "he! "); 3724 else 3725 { 3726 if (g_cmd) 3727 STRCPY(buf, "tj "); 3728 else if (cap->count0 == 0) 3729 STRCPY(buf, "ta "); 3730 else 3731 sprintf((char *)buf, ":%ldta ", cap->count0); 3732 } 3733 } 3734 3735 /* 3736 * Now grab the chars in the identifier 3737 */ 3738 if (cmdchar == 'K' && !kp_help) 3739 { 3740 ptr = vim_strnsave(ptr, n); 3741 if (kp_ex) 3742 // Escape the argument properly for an Ex command 3743 p = vim_strsave_fnameescape(ptr, FALSE); 3744 else 3745 // Escape the argument properly for a shell command 3746 p = vim_strsave_shellescape(ptr, TRUE, TRUE); 3747 vim_free(ptr); 3748 if (p == NULL) 3749 { 3750 vim_free(buf); 3751 return; 3752 } 3753 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1); 3754 if (newbuf == NULL) 3755 { 3756 vim_free(buf); 3757 vim_free(p); 3758 return; 3759 } 3760 buf = newbuf; 3761 STRCAT(buf, p); 3762 vim_free(p); 3763 } 3764 else 3765 { 3766 if (cmdchar == '*') 3767 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\"); 3768 else if (cmdchar == '#') 3769 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\"); 3770 else if (tag_cmd) 3771 { 3772 if (curbuf->b_help) 3773 // ":help" handles unescaped argument 3774 aux_ptr = (char_u *)""; 3775 else 3776 aux_ptr = (char_u *)"\\|\"\n["; 3777 } 3778 else 3779 aux_ptr = (char_u *)"\\|\"\n*?["; 3780 3781 p = buf + STRLEN(buf); 3782 while (n-- > 0) 3783 { 3784 // put a backslash before \ and some others 3785 if (vim_strchr(aux_ptr, *ptr) != NULL) 3786 *p++ = '\\'; 3787 // When current byte is a part of multibyte character, copy all 3788 // bytes of that character. 3789 if (has_mbyte) 3790 { 3791 int i; 3792 int len = (*mb_ptr2len)(ptr) - 1; 3793 3794 for (i = 0; i < len && n >= 1; ++i, --n) 3795 *p++ = *ptr++; 3796 } 3797 *p++ = *ptr++; 3798 } 3799 *p = NUL; 3800 } 3801 3802 /* 3803 * Execute the command. 3804 */ 3805 if (cmdchar == '*' || cmdchar == '#') 3806 { 3807 if (!g_cmd && (has_mbyte 3808 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) 3809 : vim_iswordc(ptr[-1]))) 3810 STRCAT(buf, "\\>"); 3811 3812 // put pattern in search history 3813 init_history(); 3814 add_to_history(HIST_SEARCH, buf, TRUE, NUL); 3815 3816 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL); 3817 } 3818 else 3819 { 3820 g_tag_at_cursor = TRUE; 3821 do_cmdline_cmd(buf); 3822 g_tag_at_cursor = FALSE; 3823 } 3824 3825 vim_free(buf); 3826 } 3827 3828 /* 3829 * Get visually selected text, within one line only. 3830 * Returns FAIL if more than one line selected. 3831 */ 3832 int 3833 get_visual_text( 3834 cmdarg_T *cap, 3835 char_u **pp, // return: start of selected text 3836 int *lenp) // return: length of selected text 3837 { 3838 if (VIsual_mode != 'V') 3839 unadjust_for_sel(); 3840 if (VIsual.lnum != curwin->w_cursor.lnum) 3841 { 3842 if (cap != NULL) 3843 clearopbeep(cap->oap); 3844 return FAIL; 3845 } 3846 if (VIsual_mode == 'V') 3847 { 3848 *pp = ml_get_curline(); 3849 *lenp = (int)STRLEN(*pp); 3850 } 3851 else 3852 { 3853 if (LT_POS(curwin->w_cursor, VIsual)) 3854 { 3855 *pp = ml_get_pos(&curwin->w_cursor); 3856 *lenp = VIsual.col - curwin->w_cursor.col + 1; 3857 } 3858 else 3859 { 3860 *pp = ml_get_pos(&VIsual); 3861 *lenp = curwin->w_cursor.col - VIsual.col + 1; 3862 } 3863 if (has_mbyte) 3864 // Correct the length to include the whole last character. 3865 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1; 3866 } 3867 reset_VIsual_and_resel(); 3868 return OK; 3869 } 3870 3871 /* 3872 * CTRL-T: backwards in tag stack 3873 */ 3874 static void 3875 nv_tagpop(cmdarg_T *cap) 3876 { 3877 if (!checkclearopq(cap->oap)) 3878 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE); 3879 } 3880 3881 /* 3882 * Handle scrolling command 'H', 'L' and 'M'. 3883 */ 3884 static void 3885 nv_scroll(cmdarg_T *cap) 3886 { 3887 int used = 0; 3888 long n; 3889 #ifdef FEAT_FOLDING 3890 linenr_T lnum; 3891 #endif 3892 int half; 3893 3894 cap->oap->motion_type = MLINE; 3895 setpcmark(); 3896 3897 if (cap->cmdchar == 'L') 3898 { 3899 validate_botline(); // make sure curwin->w_botline is valid 3900 curwin->w_cursor.lnum = curwin->w_botline - 1; 3901 if (cap->count1 - 1 >= curwin->w_cursor.lnum) 3902 curwin->w_cursor.lnum = 1; 3903 else 3904 { 3905 #ifdef FEAT_FOLDING 3906 if (hasAnyFolding(curwin)) 3907 { 3908 // Count a fold for one screen line. 3909 for (n = cap->count1 - 1; n > 0 3910 && curwin->w_cursor.lnum > curwin->w_topline; --n) 3911 { 3912 (void)hasFolding(curwin->w_cursor.lnum, 3913 &curwin->w_cursor.lnum, NULL); 3914 --curwin->w_cursor.lnum; 3915 } 3916 } 3917 else 3918 #endif 3919 curwin->w_cursor.lnum -= cap->count1 - 1; 3920 } 3921 } 3922 else 3923 { 3924 if (cap->cmdchar == 'M') 3925 { 3926 #ifdef FEAT_DIFF 3927 // Don't count filler lines above the window. 3928 used -= diff_check_fill(curwin, curwin->w_topline) 3929 - curwin->w_topfill; 3930 #endif 3931 validate_botline(); // make sure w_empty_rows is valid 3932 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2; 3933 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n) 3934 { 3935 #ifdef FEAT_DIFF 3936 // Count half he number of filler lines to be "below this 3937 // line" and half to be "above the next line". 3938 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline 3939 + n) / 2 >= half) 3940 { 3941 --n; 3942 break; 3943 } 3944 #endif 3945 used += plines(curwin->w_topline + n); 3946 if (used >= half) 3947 break; 3948 #ifdef FEAT_FOLDING 3949 if (hasFolding(curwin->w_topline + n, NULL, &lnum)) 3950 n = lnum - curwin->w_topline; 3951 #endif 3952 } 3953 if (n > 0 && used > curwin->w_height) 3954 --n; 3955 } 3956 else // (cap->cmdchar == 'H') 3957 { 3958 n = cap->count1 - 1; 3959 #ifdef FEAT_FOLDING 3960 if (hasAnyFolding(curwin)) 3961 { 3962 // Count a fold for one screen line. 3963 lnum = curwin->w_topline; 3964 while (n-- > 0 && lnum < curwin->w_botline - 1) 3965 { 3966 (void)hasFolding(lnum, NULL, &lnum); 3967 ++lnum; 3968 } 3969 n = lnum - curwin->w_topline; 3970 } 3971 #endif 3972 } 3973 curwin->w_cursor.lnum = curwin->w_topline + n; 3974 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 3975 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 3976 } 3977 3978 // Correct for 'so', except when an operator is pending. 3979 if (cap->oap->op_type == OP_NOP) 3980 cursor_correct(); 3981 beginline(BL_SOL | BL_FIX); 3982 } 3983 3984 /* 3985 * Cursor right commands. 3986 */ 3987 static void 3988 nv_right(cmdarg_T *cap) 3989 { 3990 long n; 3991 int past_line; 3992 3993 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) 3994 { 3995 // <C-Right> and <S-Right> move a word or WORD right 3996 if (mod_mask & MOD_MASK_CTRL) 3997 cap->arg = TRUE; 3998 nv_wordcmd(cap); 3999 return; 4000 } 4001 4002 cap->oap->motion_type = MCHAR; 4003 cap->oap->inclusive = FALSE; 4004 past_line = (VIsual_active && *p_sel != 'o'); 4005 4006 /* 4007 * In virtual edit mode, there's no such thing as "past_line", as lines 4008 * are (theoretically) infinitely long. 4009 */ 4010 if (virtual_active()) 4011 past_line = 0; 4012 4013 for (n = cap->count1; n > 0; --n) 4014 { 4015 if ((!past_line && oneright() == FAIL) 4016 || (past_line && *ml_get_cursor() == NUL) 4017 ) 4018 { 4019 /* 4020 * <Space> wraps to next line if 'whichwrap' has 's'. 4021 * 'l' wraps to next line if 'whichwrap' has 'l'. 4022 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'. 4023 */ 4024 if ( ((cap->cmdchar == ' ' 4025 && vim_strchr(p_ww, 's') != NULL) 4026 || (cap->cmdchar == 'l' 4027 && vim_strchr(p_ww, 'l') != NULL) 4028 || (cap->cmdchar == K_RIGHT 4029 && vim_strchr(p_ww, '>') != NULL)) 4030 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 4031 { 4032 // When deleting we also count the NL as a character. 4033 // Set cap->oap->inclusive when last char in the line is 4034 // included, move to next line after that 4035 if ( cap->oap->op_type != OP_NOP 4036 && !cap->oap->inclusive 4037 && !LINEEMPTY(curwin->w_cursor.lnum)) 4038 cap->oap->inclusive = TRUE; 4039 else 4040 { 4041 ++curwin->w_cursor.lnum; 4042 curwin->w_cursor.col = 0; 4043 curwin->w_cursor.coladd = 0; 4044 curwin->w_set_curswant = TRUE; 4045 cap->oap->inclusive = FALSE; 4046 } 4047 continue; 4048 } 4049 if (cap->oap->op_type == OP_NOP) 4050 { 4051 // Only beep and flush if not moved at all 4052 if (n == cap->count1) 4053 beep_flush(); 4054 } 4055 else 4056 { 4057 if (!LINEEMPTY(curwin->w_cursor.lnum)) 4058 cap->oap->inclusive = TRUE; 4059 } 4060 break; 4061 } 4062 else if (past_line) 4063 { 4064 curwin->w_set_curswant = TRUE; 4065 if (virtual_active()) 4066 oneright(); 4067 else 4068 { 4069 if (has_mbyte) 4070 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); 4071 else 4072 ++curwin->w_cursor.col; 4073 } 4074 } 4075 } 4076 #ifdef FEAT_FOLDING 4077 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped 4078 && cap->oap->op_type == OP_NOP) 4079 foldOpenCursor(); 4080 #endif 4081 } 4082 4083 /* 4084 * Cursor left commands. 4085 * 4086 * Returns TRUE when operator end should not be adjusted. 4087 */ 4088 static void 4089 nv_left(cmdarg_T *cap) 4090 { 4091 long n; 4092 4093 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) 4094 { 4095 // <C-Left> and <S-Left> move a word or WORD left 4096 if (mod_mask & MOD_MASK_CTRL) 4097 cap->arg = 1; 4098 nv_bck_word(cap); 4099 return; 4100 } 4101 4102 cap->oap->motion_type = MCHAR; 4103 cap->oap->inclusive = FALSE; 4104 for (n = cap->count1; n > 0; --n) 4105 { 4106 if (oneleft() == FAIL) 4107 { 4108 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'. 4109 // 'h' wraps to previous line if 'whichwrap' has 'h'. 4110 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'. 4111 if ( (((cap->cmdchar == K_BS 4112 || cap->cmdchar == Ctrl_H) 4113 && vim_strchr(p_ww, 'b') != NULL) 4114 || (cap->cmdchar == 'h' 4115 && vim_strchr(p_ww, 'h') != NULL) 4116 || (cap->cmdchar == K_LEFT 4117 && vim_strchr(p_ww, '<') != NULL)) 4118 && curwin->w_cursor.lnum > 1) 4119 { 4120 --(curwin->w_cursor.lnum); 4121 coladvance((colnr_T)MAXCOL); 4122 curwin->w_set_curswant = TRUE; 4123 4124 // When the NL before the first char has to be deleted we 4125 // put the cursor on the NUL after the previous line. 4126 // This is a very special case, be careful! 4127 // Don't adjust op_end now, otherwise it won't work. 4128 if ( (cap->oap->op_type == OP_DELETE 4129 || cap->oap->op_type == OP_CHANGE) 4130 && !LINEEMPTY(curwin->w_cursor.lnum)) 4131 { 4132 char_u *cp = ml_get_cursor(); 4133 4134 if (*cp != NUL) 4135 { 4136 if (has_mbyte) 4137 curwin->w_cursor.col += (*mb_ptr2len)(cp); 4138 else 4139 ++curwin->w_cursor.col; 4140 } 4141 cap->retval |= CA_NO_ADJ_OP_END; 4142 } 4143 continue; 4144 } 4145 // Only beep and flush if not moved at all 4146 else if (cap->oap->op_type == OP_NOP && n == cap->count1) 4147 beep_flush(); 4148 break; 4149 } 4150 } 4151 #ifdef FEAT_FOLDING 4152 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped 4153 && cap->oap->op_type == OP_NOP) 4154 foldOpenCursor(); 4155 #endif 4156 } 4157 4158 /* 4159 * Cursor up commands. 4160 * cap->arg is TRUE for "-": Move cursor to first non-blank. 4161 */ 4162 static void 4163 nv_up(cmdarg_T *cap) 4164 { 4165 if (mod_mask & MOD_MASK_SHIFT) 4166 { 4167 // <S-Up> is page up 4168 cap->arg = BACKWARD; 4169 nv_page(cap); 4170 } 4171 else 4172 { 4173 cap->oap->motion_type = MLINE; 4174 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL) 4175 clearopbeep(cap->oap); 4176 else if (cap->arg) 4177 beginline(BL_WHITE | BL_FIX); 4178 } 4179 } 4180 4181 /* 4182 * Cursor down commands. 4183 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank. 4184 */ 4185 static void 4186 nv_down(cmdarg_T *cap) 4187 { 4188 if (mod_mask & MOD_MASK_SHIFT) 4189 { 4190 // <S-Down> is page down 4191 cap->arg = FORWARD; 4192 nv_page(cap); 4193 } 4194 #if defined(FEAT_QUICKFIX) 4195 // Quickfix window only: view the result under the cursor. 4196 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR) 4197 qf_view_result(FALSE); 4198 #endif 4199 else 4200 { 4201 #ifdef FEAT_CMDWIN 4202 // In the cmdline window a <CR> executes the command. 4203 if (cmdwin_type != 0 && cap->cmdchar == CAR) 4204 cmdwin_result = CAR; 4205 else 4206 #endif 4207 #ifdef FEAT_JOB_CHANNEL 4208 // In a prompt buffer a <CR> in the last line invokes the callback. 4209 if (bt_prompt(curbuf) && cap->cmdchar == CAR 4210 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) 4211 { 4212 invoke_prompt_callback(); 4213 if (restart_edit == 0) 4214 restart_edit = 'a'; 4215 } 4216 else 4217 #endif 4218 { 4219 cap->oap->motion_type = MLINE; 4220 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL) 4221 clearopbeep(cap->oap); 4222 else if (cap->arg) 4223 beginline(BL_WHITE | BL_FIX); 4224 } 4225 } 4226 } 4227 4228 #ifdef FEAT_SEARCHPATH 4229 /* 4230 * Grab the file name under the cursor and edit it. 4231 */ 4232 static void 4233 nv_gotofile(cmdarg_T *cap) 4234 { 4235 char_u *ptr; 4236 linenr_T lnum = -1; 4237 4238 if (text_locked()) 4239 { 4240 clearopbeep(cap->oap); 4241 text_locked_msg(); 4242 return; 4243 } 4244 if (curbuf_locked()) 4245 { 4246 clearop(cap->oap); 4247 return; 4248 } 4249 #ifdef FEAT_PROP_POPUP 4250 if (ERROR_IF_TERM_POPUP_WINDOW) 4251 return; 4252 #endif 4253 4254 ptr = grab_file_name(cap->count1, &lnum); 4255 4256 if (ptr != NULL) 4257 { 4258 // do autowrite if necessary 4259 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf)) 4260 (void)autowrite(curbuf, FALSE); 4261 setpcmark(); 4262 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST, 4263 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK 4264 && cap->nchar == 'F' && lnum >= 0) 4265 { 4266 curwin->w_cursor.lnum = lnum; 4267 check_cursor_lnum(); 4268 beginline(BL_SOL | BL_FIX); 4269 } 4270 vim_free(ptr); 4271 } 4272 else 4273 clearop(cap->oap); 4274 } 4275 #endif 4276 4277 /* 4278 * <End> command: to end of current line or last line. 4279 */ 4280 static void 4281 nv_end(cmdarg_T *cap) 4282 { 4283 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line 4284 { 4285 cap->arg = TRUE; 4286 nv_goto(cap); 4287 cap->count1 = 1; // to end of current line 4288 } 4289 nv_dollar(cap); 4290 } 4291 4292 /* 4293 * Handle the "$" command. 4294 */ 4295 static void 4296 nv_dollar(cmdarg_T *cap) 4297 { 4298 cap->oap->motion_type = MCHAR; 4299 cap->oap->inclusive = TRUE; 4300 // In virtual mode when off the edge of a line and an operator 4301 // is pending (whew!) keep the cursor where it is. 4302 // Otherwise, send it to the end of the line. 4303 if (!virtual_active() || gchar_cursor() != NUL 4304 || cap->oap->op_type == OP_NOP) 4305 curwin->w_curswant = MAXCOL; // so we stay at the end 4306 if (cursor_down((long)(cap->count1 - 1), 4307 cap->oap->op_type == OP_NOP) == FAIL) 4308 clearopbeep(cap->oap); 4309 #ifdef FEAT_FOLDING 4310 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) 4311 foldOpenCursor(); 4312 #endif 4313 } 4314 4315 /* 4316 * Implementation of '?' and '/' commands. 4317 * If cap->arg is TRUE don't set PC mark. 4318 */ 4319 static void 4320 nv_search(cmdarg_T *cap) 4321 { 4322 oparg_T *oap = cap->oap; 4323 pos_T save_cursor = curwin->w_cursor; 4324 4325 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13) 4326 { 4327 // Translate "g??" to "g?g?" 4328 cap->cmdchar = 'g'; 4329 cap->nchar = '?'; 4330 nv_operator(cap); 4331 return; 4332 } 4333 4334 // When using 'incsearch' the cursor may be moved to set a different search 4335 // start position. 4336 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE); 4337 4338 if (cap->searchbuf == NULL) 4339 { 4340 clearop(oap); 4341 return; 4342 } 4343 4344 (void)normal_search(cap, cap->cmdchar, cap->searchbuf, 4345 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor)) 4346 ? 0 : SEARCH_MARK, NULL); 4347 } 4348 4349 /* 4350 * Handle "N" and "n" commands. 4351 * cap->arg is SEARCH_REV for "N", 0 for "n". 4352 */ 4353 static void 4354 nv_next(cmdarg_T *cap) 4355 { 4356 pos_T old = curwin->w_cursor; 4357 int wrapped = FALSE; 4358 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped); 4359 4360 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor)) 4361 { 4362 // Avoid getting stuck on the current cursor position, which can 4363 // happen when an offset is given and the cursor is on the last char 4364 // in the buffer: Repeat with count + 1. 4365 cap->count1 += 1; 4366 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL); 4367 cap->count1 -= 1; 4368 } 4369 } 4370 4371 /* 4372 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat). 4373 * Uses only cap->count1 and cap->oap from "cap". 4374 * Return 0 for failure, 1 for found, 2 for found and line offset added. 4375 */ 4376 static int 4377 normal_search( 4378 cmdarg_T *cap, 4379 int dir, 4380 char_u *pat, 4381 int opt, // extra flags for do_search() 4382 int *wrapped) 4383 { 4384 int i; 4385 searchit_arg_T sia; 4386 4387 cap->oap->motion_type = MCHAR; 4388 cap->oap->inclusive = FALSE; 4389 cap->oap->use_reg_one = TRUE; 4390 curwin->w_set_curswant = TRUE; 4391 4392 CLEAR_FIELD(sia); 4393 i = do_search(cap->oap, dir, dir, pat, cap->count1, 4394 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia); 4395 if (wrapped != NULL) 4396 *wrapped = sia.sa_wrapped; 4397 if (i == 0) 4398 clearop(cap->oap); 4399 else 4400 { 4401 if (i == 2) 4402 cap->oap->motion_type = MLINE; 4403 curwin->w_cursor.coladd = 0; 4404 #ifdef FEAT_FOLDING 4405 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) 4406 foldOpenCursor(); 4407 #endif 4408 } 4409 4410 // "/$" will put the cursor after the end of the line, may need to 4411 // correct that here 4412 check_cursor(); 4413 return i; 4414 } 4415 4416 /* 4417 * Character search commands. 4418 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for 4419 * ',' and FALSE for ';'. 4420 * cap->nchar is NUL for ',' and ';' (repeat the search) 4421 */ 4422 static void 4423 nv_csearch(cmdarg_T *cap) 4424 { 4425 int t_cmd; 4426 4427 if (cap->cmdchar == 't' || cap->cmdchar == 'T') 4428 t_cmd = TRUE; 4429 else 4430 t_cmd = FALSE; 4431 4432 cap->oap->motion_type = MCHAR; 4433 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL) 4434 clearopbeep(cap->oap); 4435 else 4436 { 4437 curwin->w_set_curswant = TRUE; 4438 // Include a Tab for "tx" and for "dfx". 4439 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD 4440 && (t_cmd || cap->oap->op_type != OP_NOP)) 4441 { 4442 colnr_T scol, ecol; 4443 4444 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); 4445 curwin->w_cursor.coladd = ecol - scol; 4446 } 4447 else 4448 curwin->w_cursor.coladd = 0; 4449 adjust_for_sel(cap); 4450 #ifdef FEAT_FOLDING 4451 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) 4452 foldOpenCursor(); 4453 #endif 4454 } 4455 } 4456 4457 /* 4458 * "[" and "]" commands. 4459 * cap->arg is BACKWARD for "[" and FORWARD for "]". 4460 */ 4461 static void 4462 nv_brackets(cmdarg_T *cap) 4463 { 4464 pos_T new_pos = {0, 0, 0}; 4465 pos_T prev_pos; 4466 pos_T *pos = NULL; // init for GCC 4467 pos_T old_pos; // cursor position before command 4468 int flag; 4469 long n; 4470 int findc; 4471 int c; 4472 4473 cap->oap->motion_type = MCHAR; 4474 cap->oap->inclusive = FALSE; 4475 old_pos = curwin->w_cursor; 4476 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error. 4477 4478 #ifdef FEAT_SEARCHPATH 4479 /* 4480 * "[f" or "]f" : Edit file under the cursor (same as "gf") 4481 */ 4482 if (cap->nchar == 'f') 4483 nv_gotofile(cap); 4484 else 4485 #endif 4486 4487 #ifdef FEAT_FIND_ID 4488 /* 4489 * Find the occurrence(s) of the identifier or define under cursor 4490 * in current and included files or jump to the first occurrence. 4491 * 4492 * search list jump 4493 * fwd bwd fwd bwd fwd bwd 4494 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I" 4495 * define "]d" "[d" "]D" "[D" "]^D" "[^D" 4496 */ 4497 if (vim_strchr((char_u *) 4498 # ifdef EBCDIC 4499 "iI\005dD\067", 4500 # else 4501 "iI\011dD\004", 4502 # endif 4503 cap->nchar) != NULL) 4504 { 4505 char_u *ptr; 4506 int len; 4507 4508 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) 4509 clearop(cap->oap); 4510 else 4511 { 4512 find_pattern_in_path(ptr, 0, len, TRUE, 4513 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE, 4514 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY, 4515 cap->count1, 4516 isupper(cap->nchar) ? ACTION_SHOW_ALL : 4517 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO, 4518 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1, 4519 (linenr_T)MAXLNUM); 4520 curwin->w_set_curswant = TRUE; 4521 } 4522 } 4523 else 4524 #endif 4525 4526 /* 4527 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')' 4528 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct. 4529 * "[/", "[*", "]/", "]*": go to Nth comment start/end. 4530 * "[m" or "]m" search for prev/next start of (Java) method. 4531 * "[M" or "]M" search for prev/next end of (Java) method. 4532 */ 4533 if ( (cap->cmdchar == '[' 4534 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL) 4535 || (cap->cmdchar == ']' 4536 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL)) 4537 { 4538 if (cap->nchar == '*') 4539 cap->nchar = '/'; 4540 prev_pos.lnum = 0; 4541 if (cap->nchar == 'm' || cap->nchar == 'M') 4542 { 4543 if (cap->cmdchar == '[') 4544 findc = '{'; 4545 else 4546 findc = '}'; 4547 n = 9999; 4548 } 4549 else 4550 { 4551 findc = cap->nchar; 4552 n = cap->count1; 4553 } 4554 for ( ; n > 0; --n) 4555 { 4556 if ((pos = findmatchlimit(cap->oap, findc, 4557 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL) 4558 { 4559 if (new_pos.lnum == 0) // nothing found 4560 { 4561 if (cap->nchar != 'm' && cap->nchar != 'M') 4562 clearopbeep(cap->oap); 4563 } 4564 else 4565 pos = &new_pos; // use last one found 4566 break; 4567 } 4568 prev_pos = new_pos; 4569 curwin->w_cursor = *pos; 4570 new_pos = *pos; 4571 } 4572 curwin->w_cursor = old_pos; 4573 4574 /* 4575 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only 4576 * brought us to the match for "[m" and "]M" when inside a method. 4577 * Try finding the '{' or '}' we want to be at. 4578 * Also repeat for the given count. 4579 */ 4580 if (cap->nchar == 'm' || cap->nchar == 'M') 4581 { 4582 // norm is TRUE for "]M" and "[m" 4583 int norm = ((findc == '{') == (cap->nchar == 'm')); 4584 4585 n = cap->count1; 4586 // found a match: we were inside a method 4587 if (prev_pos.lnum != 0) 4588 { 4589 pos = &prev_pos; 4590 curwin->w_cursor = prev_pos; 4591 if (norm) 4592 --n; 4593 } 4594 else 4595 pos = NULL; 4596 while (n > 0) 4597 { 4598 for (;;) 4599 { 4600 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0) 4601 { 4602 // if not found anything, that's an error 4603 if (pos == NULL) 4604 clearopbeep(cap->oap); 4605 n = 0; 4606 break; 4607 } 4608 c = gchar_cursor(); 4609 if (c == '{' || c == '}') 4610 { 4611 // Must have found end/start of class: use it. 4612 // Or found the place to be at. 4613 if ((c == findc && norm) || (n == 1 && !norm)) 4614 { 4615 new_pos = curwin->w_cursor; 4616 pos = &new_pos; 4617 n = 0; 4618 } 4619 // if no match found at all, we started outside of the 4620 // class and we're inside now. Just go on. 4621 else if (new_pos.lnum == 0) 4622 { 4623 new_pos = curwin->w_cursor; 4624 pos = &new_pos; 4625 } 4626 // found start/end of other method: go to match 4627 else if ((pos = findmatchlimit(cap->oap, findc, 4628 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 4629 0)) == NULL) 4630 n = 0; 4631 else 4632 curwin->w_cursor = *pos; 4633 break; 4634 } 4635 } 4636 --n; 4637 } 4638 curwin->w_cursor = old_pos; 4639 if (pos == NULL && new_pos.lnum != 0) 4640 clearopbeep(cap->oap); 4641 } 4642 if (pos != NULL) 4643 { 4644 setpcmark(); 4645 curwin->w_cursor = *pos; 4646 curwin->w_set_curswant = TRUE; 4647 #ifdef FEAT_FOLDING 4648 if ((fdo_flags & FDO_BLOCK) && KeyTyped 4649 && cap->oap->op_type == OP_NOP) 4650 foldOpenCursor(); 4651 #endif 4652 } 4653 } 4654 4655 /* 4656 * "[[", "[]", "]]" and "][": move to start or end of function 4657 */ 4658 else if (cap->nchar == '[' || cap->nchar == ']') 4659 { 4660 if (cap->nchar == cap->cmdchar) // "]]" or "[[" 4661 flag = '{'; 4662 else 4663 flag = '}'; // "][" or "[]" 4664 4665 curwin->w_set_curswant = TRUE; 4666 /* 4667 * Imitate strange Vi behaviour: When using "]]" with an operator 4668 * we also stop at '}'. 4669 */ 4670 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag, 4671 (cap->oap->op_type != OP_NOP 4672 && cap->arg == FORWARD && flag == '{'))) 4673 clearopbeep(cap->oap); 4674 else 4675 { 4676 if (cap->oap->op_type == OP_NOP) 4677 beginline(BL_WHITE | BL_FIX); 4678 #ifdef FEAT_FOLDING 4679 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) 4680 foldOpenCursor(); 4681 #endif 4682 } 4683 } 4684 4685 /* 4686 * "[p", "[P", "]P" and "]p": put with indent adjustment 4687 */ 4688 else if (cap->nchar == 'p' || cap->nchar == 'P') 4689 { 4690 nv_put_opt(cap, TRUE); 4691 } 4692 4693 /* 4694 * "['", "[`", "]'" and "]`": jump to next mark 4695 */ 4696 else if (cap->nchar == '\'' || cap->nchar == '`') 4697 { 4698 pos = &curwin->w_cursor; 4699 for (n = cap->count1; n > 0; --n) 4700 { 4701 prev_pos = *pos; 4702 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD, 4703 cap->nchar == '\''); 4704 if (pos == NULL) 4705 break; 4706 } 4707 if (pos == NULL) 4708 pos = &prev_pos; 4709 nv_cursormark(cap, cap->nchar == '\'', pos); 4710 } 4711 4712 /* 4713 * [ or ] followed by a middle mouse click: put selected text with 4714 * indent adjustment. Any other button just does as usual. 4715 */ 4716 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE) 4717 { 4718 (void)do_mouse(cap->oap, cap->nchar, 4719 (cap->cmdchar == ']') ? FORWARD : BACKWARD, 4720 cap->count1, PUT_FIXINDENT); 4721 } 4722 4723 #ifdef FEAT_FOLDING 4724 /* 4725 * "[z" and "]z": move to start or end of open fold. 4726 */ 4727 else if (cap->nchar == 'z') 4728 { 4729 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD, 4730 cap->count1) == FAIL) 4731 clearopbeep(cap->oap); 4732 } 4733 #endif 4734 4735 #ifdef FEAT_DIFF 4736 /* 4737 * "[c" and "]c": move to next or previous diff-change. 4738 */ 4739 else if (cap->nchar == 'c') 4740 { 4741 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD, 4742 cap->count1) == FAIL) 4743 clearopbeep(cap->oap); 4744 } 4745 #endif 4746 4747 #ifdef FEAT_SPELL 4748 /* 4749 * "[s", "[S", "]s" and "]S": move to next spell error. 4750 */ 4751 else if (cap->nchar == 's' || cap->nchar == 'S') 4752 { 4753 setpcmark(); 4754 for (n = 0; n < cap->count1; ++n) 4755 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD, 4756 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0) 4757 { 4758 clearopbeep(cap->oap); 4759 break; 4760 } 4761 else 4762 curwin->w_set_curswant = TRUE; 4763 # ifdef FEAT_FOLDING 4764 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped) 4765 foldOpenCursor(); 4766 # endif 4767 } 4768 #endif 4769 4770 // Not a valid cap->nchar. 4771 else 4772 clearopbeep(cap->oap); 4773 } 4774 4775 /* 4776 * Handle Normal mode "%" command. 4777 */ 4778 static void 4779 nv_percent(cmdarg_T *cap) 4780 { 4781 pos_T *pos; 4782 #if defined(FEAT_FOLDING) 4783 linenr_T lnum = curwin->w_cursor.lnum; 4784 #endif 4785 4786 cap->oap->inclusive = TRUE; 4787 if (cap->count0) // {cnt}% : goto {cnt} percentage in file 4788 { 4789 if (cap->count0 > 100) 4790 clearopbeep(cap->oap); 4791 else 4792 { 4793 cap->oap->motion_type = MLINE; 4794 setpcmark(); 4795 // Round up, so 'normal 100%' always jumps at the line line. 4796 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would 4797 // overflow on 32-bits, so use a formula with less accuracy 4798 // to avoid overflows. 4799 if (curbuf->b_ml.ml_line_count >= 21474836) 4800 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L) 4801 / 100L * cap->count0; 4802 else 4803 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count * 4804 cap->count0 + 99L) / 100L; 4805 if (curwin->w_cursor.lnum < 1) 4806 curwin->w_cursor.lnum = 1; 4807 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 4808 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 4809 beginline(BL_SOL | BL_FIX); 4810 } 4811 } 4812 else // "%" : go to matching paren 4813 { 4814 cap->oap->motion_type = MCHAR; 4815 cap->oap->use_reg_one = TRUE; 4816 if ((pos = findmatch(cap->oap, NUL)) == NULL) 4817 clearopbeep(cap->oap); 4818 else 4819 { 4820 setpcmark(); 4821 curwin->w_cursor = *pos; 4822 curwin->w_set_curswant = TRUE; 4823 curwin->w_cursor.coladd = 0; 4824 adjust_for_sel(cap); 4825 } 4826 } 4827 #ifdef FEAT_FOLDING 4828 if (cap->oap->op_type == OP_NOP 4829 && lnum != curwin->w_cursor.lnum 4830 && (fdo_flags & FDO_PERCENT) 4831 && KeyTyped) 4832 foldOpenCursor(); 4833 #endif 4834 } 4835 4836 /* 4837 * Handle "(" and ")" commands. 4838 * cap->arg is BACKWARD for "(" and FORWARD for ")". 4839 */ 4840 static void 4841 nv_brace(cmdarg_T *cap) 4842 { 4843 cap->oap->motion_type = MCHAR; 4844 cap->oap->use_reg_one = TRUE; 4845 // The motion used to be inclusive for "(", but that is not what Vi does. 4846 cap->oap->inclusive = FALSE; 4847 curwin->w_set_curswant = TRUE; 4848 4849 if (findsent(cap->arg, cap->count1) == FAIL) 4850 clearopbeep(cap->oap); 4851 else 4852 { 4853 // Don't leave the cursor on the NUL past end of line. 4854 adjust_cursor(cap->oap); 4855 curwin->w_cursor.coladd = 0; 4856 #ifdef FEAT_FOLDING 4857 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) 4858 foldOpenCursor(); 4859 #endif 4860 } 4861 } 4862 4863 /* 4864 * "m" command: Mark a position. 4865 */ 4866 static void 4867 nv_mark(cmdarg_T *cap) 4868 { 4869 if (!checkclearop(cap->oap)) 4870 { 4871 if (setmark(cap->nchar) == FAIL) 4872 clearopbeep(cap->oap); 4873 } 4874 } 4875 4876 /* 4877 * "{" and "}" commands. 4878 * cmd->arg is BACKWARD for "{" and FORWARD for "}". 4879 */ 4880 static void 4881 nv_findpar(cmdarg_T *cap) 4882 { 4883 cap->oap->motion_type = MCHAR; 4884 cap->oap->inclusive = FALSE; 4885 cap->oap->use_reg_one = TRUE; 4886 curwin->w_set_curswant = TRUE; 4887 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE)) 4888 clearopbeep(cap->oap); 4889 else 4890 { 4891 curwin->w_cursor.coladd = 0; 4892 #ifdef FEAT_FOLDING 4893 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) 4894 foldOpenCursor(); 4895 #endif 4896 } 4897 } 4898 4899 /* 4900 * "u" command: Undo or make lower case. 4901 */ 4902 static void 4903 nv_undo(cmdarg_T *cap) 4904 { 4905 if (cap->oap->op_type == OP_LOWER || VIsual_active) 4906 { 4907 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" 4908 cap->cmdchar = 'g'; 4909 cap->nchar = 'u'; 4910 nv_operator(cap); 4911 } 4912 else 4913 nv_kundo(cap); 4914 } 4915 4916 /* 4917 * <Undo> command. 4918 */ 4919 static void 4920 nv_kundo(cmdarg_T *cap) 4921 { 4922 if (!checkclearopq(cap->oap)) 4923 { 4924 #ifdef FEAT_JOB_CHANNEL 4925 if (bt_prompt(curbuf)) 4926 { 4927 clearopbeep(cap->oap); 4928 return; 4929 } 4930 #endif 4931 u_undo((int)cap->count1); 4932 curwin->w_set_curswant = TRUE; 4933 } 4934 } 4935 4936 /* 4937 * Handle the "r" command. 4938 */ 4939 static void 4940 nv_replace(cmdarg_T *cap) 4941 { 4942 char_u *ptr; 4943 int had_ctrl_v; 4944 long n; 4945 4946 if (checkclearop(cap->oap)) 4947 return; 4948 #ifdef FEAT_JOB_CHANNEL 4949 if (bt_prompt(curbuf) && !prompt_curpos_editable()) 4950 { 4951 clearopbeep(cap->oap); 4952 return; 4953 } 4954 #endif 4955 4956 // get another character 4957 if (cap->nchar == Ctrl_V) 4958 { 4959 had_ctrl_v = Ctrl_V; 4960 cap->nchar = get_literal(FALSE); 4961 // Don't redo a multibyte character with CTRL-V. 4962 if (cap->nchar > DEL) 4963 had_ctrl_v = NUL; 4964 } 4965 else 4966 had_ctrl_v = NUL; 4967 4968 // Abort if the character is a special key. 4969 if (IS_SPECIAL(cap->nchar)) 4970 { 4971 clearopbeep(cap->oap); 4972 return; 4973 } 4974 4975 // Visual mode "r" 4976 if (VIsual_active) 4977 { 4978 if (got_int) 4979 reset_VIsual(); 4980 if (had_ctrl_v) 4981 { 4982 // Use a special (negative) number to make a difference between a 4983 // literal CR or NL and a line break. 4984 if (cap->nchar == CAR) 4985 cap->nchar = REPLACE_CR_NCHAR; 4986 else if (cap->nchar == NL) 4987 cap->nchar = REPLACE_NL_NCHAR; 4988 } 4989 nv_operator(cap); 4990 return; 4991 } 4992 4993 // Break tabs, etc. 4994 if (virtual_active()) 4995 { 4996 if (u_save_cursor() == FAIL) 4997 return; 4998 if (gchar_cursor() == NUL) 4999 { 5000 // Add extra space and put the cursor on the first one. 5001 coladvance_force((colnr_T)(getviscol() + cap->count1)); 5002 curwin->w_cursor.col -= cap->count1; 5003 } 5004 else if (gchar_cursor() == TAB) 5005 coladvance_force(getviscol()); 5006 } 5007 5008 // Abort if not enough characters to replace. 5009 ptr = ml_get_cursor(); 5010 if (STRLEN(ptr) < (unsigned)cap->count1 5011 || (has_mbyte && mb_charlen(ptr) < cap->count1)) 5012 { 5013 clearopbeep(cap->oap); 5014 return; 5015 } 5016 5017 /* 5018 * Replacing with a TAB is done by edit() when it is complicated because 5019 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB. 5020 * Other characters are done below to avoid problems with things like 5021 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC). 5022 */ 5023 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta)) 5024 { 5025 stuffnumReadbuff(cap->count1); 5026 stuffcharReadbuff('R'); 5027 stuffcharReadbuff('\t'); 5028 stuffcharReadbuff(ESC); 5029 return; 5030 } 5031 5032 // save line for undo 5033 if (u_save_cursor() == FAIL) 5034 return; 5035 5036 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n')) 5037 { 5038 /* 5039 * Replace character(s) by a single newline. 5040 * Strange vi behaviour: Only one newline is inserted. 5041 * Delete the characters here. 5042 * Insert the newline with an insert command, takes care of 5043 * autoindent. The insert command depends on being on the last 5044 * character of a line or not. 5045 */ 5046 (void)del_chars(cap->count1, FALSE); // delete the characters 5047 stuffcharReadbuff('\r'); 5048 stuffcharReadbuff(ESC); 5049 5050 // Give 'r' to edit(), to get the redo command right. 5051 invoke_edit(cap, TRUE, 'r', FALSE); 5052 } 5053 else 5054 { 5055 prep_redo(cap->oap->regname, cap->count1, 5056 NUL, 'r', NUL, had_ctrl_v, cap->nchar); 5057 5058 curbuf->b_op_start = curwin->w_cursor; 5059 if (has_mbyte) 5060 { 5061 int old_State = State; 5062 5063 if (cap->ncharC1 != 0) 5064 AppendCharToRedobuff(cap->ncharC1); 5065 if (cap->ncharC2 != 0) 5066 AppendCharToRedobuff(cap->ncharC2); 5067 5068 // This is slow, but it handles replacing a single-byte with a 5069 // multi-byte and the other way around. Also handles adding 5070 // composing characters for utf-8. 5071 for (n = cap->count1; n > 0; --n) 5072 { 5073 State = REPLACE; 5074 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y) 5075 { 5076 int c = ins_copychar(curwin->w_cursor.lnum 5077 + (cap->nchar == Ctrl_Y ? -1 : 1)); 5078 if (c != NUL) 5079 ins_char(c); 5080 else 5081 // will be decremented further down 5082 ++curwin->w_cursor.col; 5083 } 5084 else 5085 ins_char(cap->nchar); 5086 State = old_State; 5087 if (cap->ncharC1 != 0) 5088 ins_char(cap->ncharC1); 5089 if (cap->ncharC2 != 0) 5090 ins_char(cap->ncharC2); 5091 } 5092 } 5093 else 5094 { 5095 /* 5096 * Replace the characters within one line. 5097 */ 5098 for (n = cap->count1; n > 0; --n) 5099 { 5100 /* 5101 * Get ptr again, because u_save and/or showmatch() will have 5102 * released the line. At the same time we let know that the 5103 * line will be changed. 5104 */ 5105 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE); 5106 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y) 5107 { 5108 int c = ins_copychar(curwin->w_cursor.lnum 5109 + (cap->nchar == Ctrl_Y ? -1 : 1)); 5110 if (c != NUL) 5111 ptr[curwin->w_cursor.col] = c; 5112 } 5113 else 5114 ptr[curwin->w_cursor.col] = cap->nchar; 5115 if (p_sm && msg_silent == 0) 5116 showmatch(cap->nchar); 5117 ++curwin->w_cursor.col; 5118 } 5119 #ifdef FEAT_NETBEANS_INTG 5120 if (netbeans_active()) 5121 { 5122 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1); 5123 5124 netbeans_removed(curbuf, curwin->w_cursor.lnum, start, 5125 (long)cap->count1); 5126 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start, 5127 &ptr[start], (int)cap->count1); 5128 } 5129 #endif 5130 5131 // mark the buffer as changed and prepare for displaying 5132 changed_bytes(curwin->w_cursor.lnum, 5133 (colnr_T)(curwin->w_cursor.col - cap->count1)); 5134 } 5135 --curwin->w_cursor.col; // cursor on the last replaced char 5136 // if the character on the left of the current cursor is a multi-byte 5137 // character, move two characters left 5138 if (has_mbyte) 5139 mb_adjust_cursor(); 5140 curbuf->b_op_end = curwin->w_cursor; 5141 curwin->w_set_curswant = TRUE; 5142 set_last_insert(cap->nchar); 5143 } 5144 } 5145 5146 /* 5147 * 'o': Exchange start and end of Visual area. 5148 * 'O': same, but in block mode exchange left and right corners. 5149 */ 5150 static void 5151 v_swap_corners(int cmdchar) 5152 { 5153 pos_T old_cursor; 5154 colnr_T left, right; 5155 5156 if (cmdchar == 'O' && VIsual_mode == Ctrl_V) 5157 { 5158 old_cursor = curwin->w_cursor; 5159 getvcols(curwin, &old_cursor, &VIsual, &left, &right); 5160 curwin->w_cursor.lnum = VIsual.lnum; 5161 coladvance(left); 5162 VIsual = curwin->w_cursor; 5163 5164 curwin->w_cursor.lnum = old_cursor.lnum; 5165 curwin->w_curswant = right; 5166 // 'selection "exclusive" and cursor at right-bottom corner: move it 5167 // right one column 5168 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e') 5169 ++curwin->w_curswant; 5170 coladvance(curwin->w_curswant); 5171 if (curwin->w_cursor.col == old_cursor.col 5172 && (!virtual_active() 5173 || curwin->w_cursor.coladd == old_cursor.coladd)) 5174 { 5175 curwin->w_cursor.lnum = VIsual.lnum; 5176 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e') 5177 ++right; 5178 coladvance(right); 5179 VIsual = curwin->w_cursor; 5180 5181 curwin->w_cursor.lnum = old_cursor.lnum; 5182 coladvance(left); 5183 curwin->w_curswant = left; 5184 } 5185 } 5186 else 5187 { 5188 old_cursor = curwin->w_cursor; 5189 curwin->w_cursor = VIsual; 5190 VIsual = old_cursor; 5191 curwin->w_set_curswant = TRUE; 5192 } 5193 } 5194 5195 /* 5196 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE). 5197 */ 5198 static void 5199 nv_Replace(cmdarg_T *cap) 5200 { 5201 if (VIsual_active) // "R" is replace lines 5202 { 5203 cap->cmdchar = 'c'; 5204 cap->nchar = NUL; 5205 VIsual_mode_orig = VIsual_mode; // remember original area for gv 5206 VIsual_mode = 'V'; 5207 nv_operator(cap); 5208 } 5209 else if (!checkclearopq(cap->oap)) 5210 { 5211 if (!curbuf->b_p_ma) 5212 emsg(_(e_cannot_make_changes_modifiable_is_off)); 5213 else 5214 { 5215 if (virtual_active()) 5216 coladvance(getviscol()); 5217 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE); 5218 } 5219 } 5220 } 5221 5222 /* 5223 * "gr". 5224 */ 5225 static void 5226 nv_vreplace(cmdarg_T *cap) 5227 { 5228 if (VIsual_active) 5229 { 5230 cap->cmdchar = 'r'; 5231 cap->nchar = cap->extra_char; 5232 nv_replace(cap); // Do same as "r" in Visual mode for now 5233 } 5234 else if (!checkclearopq(cap->oap)) 5235 { 5236 if (!curbuf->b_p_ma) 5237 emsg(_(e_cannot_make_changes_modifiable_is_off)); 5238 else 5239 { 5240 if (cap->extra_char == Ctrl_V) // get another character 5241 cap->extra_char = get_literal(FALSE); 5242 stuffcharReadbuff(cap->extra_char); 5243 stuffcharReadbuff(ESC); 5244 if (virtual_active()) 5245 coladvance(getviscol()); 5246 invoke_edit(cap, TRUE, 'v', FALSE); 5247 } 5248 } 5249 } 5250 5251 /* 5252 * Swap case for "~" command, when it does not work like an operator. 5253 */ 5254 static void 5255 n_swapchar(cmdarg_T *cap) 5256 { 5257 long n; 5258 pos_T startpos; 5259 int did_change = 0; 5260 #ifdef FEAT_NETBEANS_INTG 5261 pos_T pos; 5262 char_u *ptr; 5263 int count; 5264 #endif 5265 5266 if (checkclearopq(cap->oap)) 5267 return; 5268 5269 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL) 5270 { 5271 clearopbeep(cap->oap); 5272 return; 5273 } 5274 5275 prep_redo_cmd(cap); 5276 5277 if (u_save_cursor() == FAIL) 5278 return; 5279 5280 startpos = curwin->w_cursor; 5281 #ifdef FEAT_NETBEANS_INTG 5282 pos = startpos; 5283 #endif 5284 for (n = cap->count1; n > 0; --n) 5285 { 5286 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor); 5287 inc_cursor(); 5288 if (gchar_cursor() == NUL) 5289 { 5290 if (vim_strchr(p_ww, '~') != NULL 5291 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 5292 { 5293 #ifdef FEAT_NETBEANS_INTG 5294 if (netbeans_active()) 5295 { 5296 if (did_change) 5297 { 5298 ptr = ml_get(pos.lnum); 5299 count = (int)STRLEN(ptr) - pos.col; 5300 netbeans_removed(curbuf, pos.lnum, pos.col, 5301 (long)count); 5302 netbeans_inserted(curbuf, pos.lnum, pos.col, 5303 &ptr[pos.col], count); 5304 } 5305 pos.col = 0; 5306 pos.lnum++; 5307 } 5308 #endif 5309 ++curwin->w_cursor.lnum; 5310 curwin->w_cursor.col = 0; 5311 if (n > 1) 5312 { 5313 if (u_savesub(curwin->w_cursor.lnum) == FAIL) 5314 break; 5315 u_clearline(); 5316 } 5317 } 5318 else 5319 break; 5320 } 5321 } 5322 #ifdef FEAT_NETBEANS_INTG 5323 if (did_change && netbeans_active()) 5324 { 5325 ptr = ml_get(pos.lnum); 5326 count = curwin->w_cursor.col - pos.col; 5327 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); 5328 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count); 5329 } 5330 #endif 5331 5332 5333 check_cursor(); 5334 curwin->w_set_curswant = TRUE; 5335 if (did_change) 5336 { 5337 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1, 5338 0L); 5339 curbuf->b_op_start = startpos; 5340 curbuf->b_op_end = curwin->w_cursor; 5341 if (curbuf->b_op_end.col > 0) 5342 --curbuf->b_op_end.col; 5343 } 5344 } 5345 5346 /* 5347 * Move cursor to mark. 5348 */ 5349 static void 5350 nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos) 5351 { 5352 if (check_mark(pos) == FAIL) 5353 clearop(cap->oap); 5354 else 5355 { 5356 if (cap->cmdchar == '\'' 5357 || cap->cmdchar == '`' 5358 || cap->cmdchar == '[' 5359 || cap->cmdchar == ']') 5360 setpcmark(); 5361 curwin->w_cursor = *pos; 5362 if (flag) 5363 beginline(BL_WHITE | BL_FIX); 5364 else 5365 check_cursor(); 5366 } 5367 cap->oap->motion_type = flag ? MLINE : MCHAR; 5368 if (cap->cmdchar == '`') 5369 cap->oap->use_reg_one = TRUE; 5370 cap->oap->inclusive = FALSE; // ignored if not MCHAR 5371 curwin->w_set_curswant = TRUE; 5372 } 5373 5374 /* 5375 * Handle commands that are operators in Visual mode. 5376 */ 5377 static void 5378 v_visop(cmdarg_T *cap) 5379 { 5380 static char_u trans[] = "YyDdCcxdXdAAIIrr"; 5381 5382 // Uppercase means linewise, except in block mode, then "D" deletes till 5383 // the end of the line, and "C" replaces till EOL 5384 if (isupper(cap->cmdchar)) 5385 { 5386 if (VIsual_mode != Ctrl_V) 5387 { 5388 VIsual_mode_orig = VIsual_mode; 5389 VIsual_mode = 'V'; 5390 } 5391 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D') 5392 curwin->w_curswant = MAXCOL; 5393 } 5394 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1); 5395 nv_operator(cap); 5396 } 5397 5398 /* 5399 * "s" and "S" commands. 5400 */ 5401 static void 5402 nv_subst(cmdarg_T *cap) 5403 { 5404 #ifdef FEAT_TERMINAL 5405 // When showing output of term_dumpdiff() swap the top and bottom. 5406 if (term_swap_diff() == OK) 5407 return; 5408 #endif 5409 #ifdef FEAT_JOB_CHANNEL 5410 if (bt_prompt(curbuf) && !prompt_curpos_editable()) 5411 { 5412 clearopbeep(cap->oap); 5413 return; 5414 } 5415 #endif 5416 if (VIsual_active) // "vs" and "vS" are the same as "vc" 5417 { 5418 if (cap->cmdchar == 'S') 5419 { 5420 VIsual_mode_orig = VIsual_mode; 5421 VIsual_mode = 'V'; 5422 } 5423 cap->cmdchar = 'c'; 5424 nv_operator(cap); 5425 } 5426 else 5427 nv_optrans(cap); 5428 } 5429 5430 /* 5431 * Abbreviated commands. 5432 */ 5433 static void 5434 nv_abbrev(cmdarg_T *cap) 5435 { 5436 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL) 5437 cap->cmdchar = 'x'; // DEL key behaves like 'x' 5438 5439 // in Visual mode these commands are operators 5440 if (VIsual_active) 5441 v_visop(cap); 5442 else 5443 nv_optrans(cap); 5444 } 5445 5446 /* 5447 * Translate a command into another command. 5448 */ 5449 static void 5450 nv_optrans(cmdarg_T *cap) 5451 { 5452 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh", 5453 (char_u *)"d$", (char_u *)"c$", 5454 (char_u *)"cl", (char_u *)"cc", 5455 (char_u *)"yy", (char_u *)":s\r"}; 5456 static char_u *str = (char_u *)"xXDCsSY&"; 5457 5458 if (!checkclearopq(cap->oap)) 5459 { 5460 // In Vi "2D" doesn't delete the next line. Can't translate it 5461 // either, because "2." should also not use the count. 5462 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL) 5463 { 5464 cap->oap->start = curwin->w_cursor; 5465 cap->oap->op_type = OP_DELETE; 5466 #ifdef FEAT_EVAL 5467 set_op_var(OP_DELETE); 5468 #endif 5469 cap->count1 = 1; 5470 nv_dollar(cap); 5471 finish_op = TRUE; 5472 ResetRedobuff(); 5473 AppendCharToRedobuff('D'); 5474 } 5475 else 5476 { 5477 if (cap->count0) 5478 stuffnumReadbuff(cap->count0); 5479 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]); 5480 } 5481 } 5482 cap->opcount = 0; 5483 } 5484 5485 /* 5486 * "'" and "`" commands. Also for "g'" and "g`". 5487 * cap->arg is TRUE for "'" and "g'". 5488 */ 5489 static void 5490 nv_gomark(cmdarg_T *cap) 5491 { 5492 pos_T *pos; 5493 int c; 5494 #ifdef FEAT_FOLDING 5495 pos_T old_cursor = curwin->w_cursor; 5496 int old_KeyTyped = KeyTyped; // getting file may reset it 5497 #endif 5498 5499 if (cap->cmdchar == 'g') 5500 c = cap->extra_char; 5501 else 5502 c = cap->nchar; 5503 pos = getmark(c, (cap->oap->op_type == OP_NOP)); 5504 if (pos == (pos_T *)-1) // jumped to other file 5505 { 5506 if (cap->arg) 5507 { 5508 check_cursor_lnum(); 5509 beginline(BL_WHITE | BL_FIX); 5510 } 5511 else 5512 check_cursor(); 5513 } 5514 else 5515 nv_cursormark(cap, cap->arg, pos); 5516 5517 // May need to clear the coladd that a mark includes. 5518 if (!virtual_active()) 5519 curwin->w_cursor.coladd = 0; 5520 check_cursor_col(); 5521 #ifdef FEAT_FOLDING 5522 if (cap->oap->op_type == OP_NOP 5523 && pos != NULL 5524 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos)) 5525 && (fdo_flags & FDO_MARK) 5526 && old_KeyTyped) 5527 foldOpenCursor(); 5528 #endif 5529 } 5530 5531 /* 5532 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands. 5533 */ 5534 static void 5535 nv_pcmark(cmdarg_T *cap) 5536 { 5537 #ifdef FEAT_JUMPLIST 5538 pos_T *pos; 5539 # ifdef FEAT_FOLDING 5540 linenr_T lnum = curwin->w_cursor.lnum; 5541 int old_KeyTyped = KeyTyped; // getting file may reset it 5542 # endif 5543 5544 if (!checkclearopq(cap->oap)) 5545 { 5546 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL) 5547 { 5548 if (goto_tabpage_lastused() == FAIL) 5549 clearopbeep(cap->oap); 5550 return; 5551 } 5552 if (cap->cmdchar == 'g') 5553 pos = movechangelist((int)cap->count1); 5554 else 5555 pos = movemark((int)cap->count1); 5556 if (pos == (pos_T *)-1) // jump to other file 5557 { 5558 curwin->w_set_curswant = TRUE; 5559 check_cursor(); 5560 } 5561 else if (pos != NULL) // can jump 5562 nv_cursormark(cap, FALSE, pos); 5563 else if (cap->cmdchar == 'g') 5564 { 5565 if (curbuf->b_changelistlen == 0) 5566 emsg(_("E664: changelist is empty")); 5567 else if (cap->count1 < 0) 5568 emsg(_("E662: At start of changelist")); 5569 else 5570 emsg(_("E663: At end of changelist")); 5571 } 5572 else 5573 clearopbeep(cap->oap); 5574 # ifdef FEAT_FOLDING 5575 if (cap->oap->op_type == OP_NOP 5576 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum) 5577 && (fdo_flags & FDO_MARK) 5578 && old_KeyTyped) 5579 foldOpenCursor(); 5580 # endif 5581 } 5582 #else 5583 clearopbeep(cap->oap); 5584 #endif 5585 } 5586 5587 /* 5588 * Handle '"' command. 5589 */ 5590 static void 5591 nv_regname(cmdarg_T *cap) 5592 { 5593 if (checkclearop(cap->oap)) 5594 return; 5595 #ifdef FEAT_EVAL 5596 if (cap->nchar == '=') 5597 cap->nchar = get_expr_register(); 5598 #endif 5599 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE)) 5600 { 5601 cap->oap->regname = cap->nchar; 5602 cap->opcount = cap->count0; // remember count before '"' 5603 #ifdef FEAT_EVAL 5604 set_reg_var(cap->oap->regname); 5605 #endif 5606 } 5607 else 5608 clearopbeep(cap->oap); 5609 } 5610 5611 /* 5612 * Handle "v", "V" and "CTRL-V" commands. 5613 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg 5614 * is TRUE. 5615 * Handle CTRL-Q just like CTRL-V. 5616 */ 5617 static void 5618 nv_visual(cmdarg_T *cap) 5619 { 5620 if (cap->cmdchar == Ctrl_Q) 5621 cap->cmdchar = Ctrl_V; 5622 5623 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it 5624 // characterwise, linewise, or blockwise. 5625 if (cap->oap->op_type != OP_NOP) 5626 { 5627 motion_force = cap->oap->motion_force = cap->cmdchar; 5628 finish_op = FALSE; // operator doesn't finish now but later 5629 return; 5630 } 5631 5632 VIsual_select = cap->arg; 5633 if (VIsual_active) // change Visual mode 5634 { 5635 if (VIsual_mode == cap->cmdchar) // stop visual mode 5636 end_visual_mode(); 5637 else // toggle char/block mode 5638 { // or char/line mode 5639 VIsual_mode = cap->cmdchar; 5640 showmode(); 5641 } 5642 redraw_curbuf_later(INVERTED); // update the inversion 5643 } 5644 else // start Visual mode 5645 { 5646 check_visual_highlight(); 5647 if (cap->count0 > 0 && resel_VIsual_mode != NUL) 5648 { 5649 // use previously selected part 5650 VIsual = curwin->w_cursor; 5651 5652 VIsual_active = TRUE; 5653 VIsual_reselect = TRUE; 5654 if (!cap->arg) 5655 // start Select mode when 'selectmode' contains "cmd" 5656 may_start_select('c'); 5657 setmouse(); 5658 if (p_smd && msg_silent == 0) 5659 redraw_cmdline = TRUE; // show visual mode later 5660 /* 5661 * For V and ^V, we multiply the number of lines even if there 5662 * was only one -- webb 5663 */ 5664 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1) 5665 { 5666 curwin->w_cursor.lnum += 5667 resel_VIsual_line_count * cap->count0 - 1; 5668 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 5669 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 5670 } 5671 VIsual_mode = resel_VIsual_mode; 5672 if (VIsual_mode == 'v') 5673 { 5674 if (resel_VIsual_line_count <= 1) 5675 { 5676 validate_virtcol(); 5677 curwin->w_curswant = curwin->w_virtcol 5678 + resel_VIsual_vcol * cap->count0 - 1; 5679 } 5680 else 5681 curwin->w_curswant = resel_VIsual_vcol; 5682 coladvance(curwin->w_curswant); 5683 } 5684 if (resel_VIsual_vcol == MAXCOL) 5685 { 5686 curwin->w_curswant = MAXCOL; 5687 coladvance((colnr_T)MAXCOL); 5688 } 5689 else if (VIsual_mode == Ctrl_V) 5690 { 5691 validate_virtcol(); 5692 curwin->w_curswant = curwin->w_virtcol 5693 + resel_VIsual_vcol * cap->count0 - 1; 5694 coladvance(curwin->w_curswant); 5695 } 5696 else 5697 curwin->w_set_curswant = TRUE; 5698 redraw_curbuf_later(INVERTED); // show the inversion 5699 } 5700 else 5701 { 5702 if (!cap->arg) 5703 // start Select mode when 'selectmode' contains "cmd" 5704 may_start_select('c'); 5705 n_start_visual_mode(cap->cmdchar); 5706 if (VIsual_mode != 'V' && *p_sel == 'e') 5707 ++cap->count1; // include one more char 5708 if (cap->count0 > 0 && --cap->count1 > 0) 5709 { 5710 // With a count select that many characters or lines. 5711 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V) 5712 nv_right(cap); 5713 else if (VIsual_mode == 'V') 5714 nv_down(cap); 5715 } 5716 } 5717 } 5718 } 5719 5720 /* 5721 * Start selection for Shift-movement keys. 5722 */ 5723 void 5724 start_selection(void) 5725 { 5726 // if 'selectmode' contains "key", start Select mode 5727 may_start_select('k'); 5728 n_start_visual_mode('v'); 5729 } 5730 5731 /* 5732 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu. 5733 */ 5734 void 5735 may_start_select(int c) 5736 { 5737 VIsual_select = (stuff_empty() && typebuf_typed() 5738 && (vim_strchr(p_slm, c) != NULL)); 5739 } 5740 5741 /* 5742 * Start Visual mode "c". 5743 * Should set VIsual_select before calling this. 5744 */ 5745 static void 5746 n_start_visual_mode(int c) 5747 { 5748 #ifdef FEAT_CONCEAL 5749 int cursor_line_was_concealed = curwin->w_p_cole > 0 5750 && conceal_cursor_line(curwin); 5751 #endif 5752 5753 VIsual_mode = c; 5754 VIsual_active = TRUE; 5755 VIsual_reselect = TRUE; 5756 5757 // Corner case: the 0 position in a tab may change when going into 5758 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting. 5759 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB) 5760 { 5761 validate_virtcol(); 5762 coladvance(curwin->w_virtcol); 5763 } 5764 VIsual = curwin->w_cursor; 5765 5766 #ifdef FEAT_FOLDING 5767 foldAdjustVisual(); 5768 #endif 5769 5770 setmouse(); 5771 #ifdef FEAT_CONCEAL 5772 // Check if redraw is needed after changing the state. 5773 conceal_check_cursor_line(cursor_line_was_concealed); 5774 #endif 5775 5776 if (p_smd && msg_silent == 0) 5777 redraw_cmdline = TRUE; // show visual mode later 5778 #ifdef FEAT_CLIPBOARD 5779 // Make sure the clipboard gets updated. Needed because start and 5780 // end may still be the same, and the selection needs to be owned 5781 clip_star.vmode = NUL; 5782 #endif 5783 5784 // Only need to redraw this line, unless still need to redraw an old 5785 // Visual area (when 'lazyredraw' is set). 5786 if (curwin->w_redr_type < INVERTED) 5787 { 5788 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum; 5789 curwin->w_old_visual_lnum = curwin->w_cursor.lnum; 5790 } 5791 } 5792 5793 5794 /* 5795 * CTRL-W: Window commands 5796 */ 5797 static void 5798 nv_window(cmdarg_T *cap) 5799 { 5800 if (cap->nchar == ':') 5801 { 5802 // "CTRL-W :" is the same as typing ":"; useful in a terminal window 5803 cap->cmdchar = ':'; 5804 cap->nchar = NUL; 5805 nv_colon(cap); 5806 } 5807 else if (!checkclearop(cap->oap)) 5808 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c 5809 } 5810 5811 /* 5812 * CTRL-Z: Suspend 5813 */ 5814 static void 5815 nv_suspend(cmdarg_T *cap) 5816 { 5817 clearop(cap->oap); 5818 if (VIsual_active) 5819 end_visual_mode(); // stop Visual mode 5820 do_cmdline_cmd((char_u *)"stop"); 5821 } 5822 5823 /* 5824 * Commands starting with "g". 5825 */ 5826 static void 5827 nv_g_cmd(cmdarg_T *cap) 5828 { 5829 oparg_T *oap = cap->oap; 5830 pos_T tpos; 5831 int i; 5832 int flag = FALSE; 5833 5834 switch (cap->nchar) 5835 { 5836 case Ctrl_A: 5837 case Ctrl_X: 5838 #ifdef MEM_PROFILE 5839 /* 5840 * "g^A": dump log of used memory. 5841 */ 5842 if (!VIsual_active && cap->nchar == Ctrl_A) 5843 vim_mem_profile_dump(); 5844 else 5845 #endif 5846 /* 5847 * "g^A/g^X": sequentially increment visually selected region 5848 */ 5849 if (VIsual_active) 5850 { 5851 cap->arg = TRUE; 5852 cap->cmdchar = cap->nchar; 5853 cap->nchar = NUL; 5854 nv_addsub(cap); 5855 } 5856 else 5857 clearopbeep(oap); 5858 break; 5859 5860 /* 5861 * "gR": Enter virtual replace mode. 5862 */ 5863 case 'R': 5864 cap->arg = TRUE; 5865 nv_Replace(cap); 5866 break; 5867 5868 case 'r': 5869 nv_vreplace(cap); 5870 break; 5871 5872 case '&': 5873 do_cmdline_cmd((char_u *)"%s//~/&"); 5874 break; 5875 5876 /* 5877 * "gv": Reselect the previous Visual area. If Visual already active, 5878 * exchange previous and current Visual area. 5879 */ 5880 case 'v': 5881 if (checkclearop(oap)) 5882 break; 5883 5884 if ( curbuf->b_visual.vi_start.lnum == 0 5885 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count 5886 || curbuf->b_visual.vi_end.lnum == 0) 5887 beep_flush(); 5888 else 5889 { 5890 // set w_cursor to the start of the Visual area, tpos to the end 5891 if (VIsual_active) 5892 { 5893 i = VIsual_mode; 5894 VIsual_mode = curbuf->b_visual.vi_mode; 5895 curbuf->b_visual.vi_mode = i; 5896 # ifdef FEAT_EVAL 5897 curbuf->b_visual_mode_eval = i; 5898 # endif 5899 i = curwin->w_curswant; 5900 curwin->w_curswant = curbuf->b_visual.vi_curswant; 5901 curbuf->b_visual.vi_curswant = i; 5902 5903 tpos = curbuf->b_visual.vi_end; 5904 curbuf->b_visual.vi_end = curwin->w_cursor; 5905 curwin->w_cursor = curbuf->b_visual.vi_start; 5906 curbuf->b_visual.vi_start = VIsual; 5907 } 5908 else 5909 { 5910 VIsual_mode = curbuf->b_visual.vi_mode; 5911 curwin->w_curswant = curbuf->b_visual.vi_curswant; 5912 tpos = curbuf->b_visual.vi_end; 5913 curwin->w_cursor = curbuf->b_visual.vi_start; 5914 } 5915 5916 VIsual_active = TRUE; 5917 VIsual_reselect = TRUE; 5918 5919 // Set Visual to the start and w_cursor to the end of the Visual 5920 // area. Make sure they are on an existing character. 5921 check_cursor(); 5922 VIsual = curwin->w_cursor; 5923 curwin->w_cursor = tpos; 5924 check_cursor(); 5925 update_topline(); 5926 /* 5927 * When called from normal "g" command: start Select mode when 5928 * 'selectmode' contains "cmd". When called for K_SELECT, always 5929 * start Select mode. 5930 */ 5931 if (cap->arg) 5932 VIsual_select = TRUE; 5933 else 5934 may_start_select('c'); 5935 setmouse(); 5936 #ifdef FEAT_CLIPBOARD 5937 // Make sure the clipboard gets updated. Needed because start and 5938 // end are still the same, and the selection needs to be owned 5939 clip_star.vmode = NUL; 5940 #endif 5941 redraw_curbuf_later(INVERTED); 5942 showmode(); 5943 } 5944 break; 5945 /* 5946 * "gV": Don't reselect the previous Visual area after a Select mode 5947 * mapping of menu. 5948 */ 5949 case 'V': 5950 VIsual_reselect = FALSE; 5951 break; 5952 5953 /* 5954 * "gh": start Select mode. 5955 * "gH": start Select line mode. 5956 * "g^H": start Select block mode. 5957 */ 5958 case K_BS: 5959 cap->nchar = Ctrl_H; 5960 // FALLTHROUGH 5961 case 'h': 5962 case 'H': 5963 case Ctrl_H: 5964 # ifdef EBCDIC 5965 // EBCDIC: 'v'-'h' != '^v'-'^h' 5966 if (cap->nchar == Ctrl_H) 5967 cap->cmdchar = Ctrl_V; 5968 else 5969 # endif 5970 cap->cmdchar = cap->nchar + ('v' - 'h'); 5971 cap->arg = TRUE; 5972 nv_visual(cap); 5973 break; 5974 5975 // "gn", "gN" visually select next/previous search match 5976 // "gn" selects next match 5977 // "gN" selects previous match 5978 case 'N': 5979 case 'n': 5980 if (!current_search(cap->count1, cap->nchar == 'n')) 5981 clearopbeep(oap); 5982 break; 5983 5984 /* 5985 * "gj" and "gk" two new funny movement keys -- up and down 5986 * movement based on *screen* line rather than *file* line. 5987 */ 5988 case 'j': 5989 case K_DOWN: 5990 // with 'nowrap' it works just like the normal "j" command. 5991 if (!curwin->w_p_wrap) 5992 { 5993 oap->motion_type = MLINE; 5994 i = cursor_down(cap->count1, oap->op_type == OP_NOP); 5995 } 5996 else 5997 i = nv_screengo(oap, FORWARD, cap->count1); 5998 if (i == FAIL) 5999 clearopbeep(oap); 6000 break; 6001 6002 case 'k': 6003 case K_UP: 6004 // with 'nowrap' it works just like the normal "k" command. 6005 if (!curwin->w_p_wrap) 6006 { 6007 oap->motion_type = MLINE; 6008 i = cursor_up(cap->count1, oap->op_type == OP_NOP); 6009 } 6010 else 6011 i = nv_screengo(oap, BACKWARD, cap->count1); 6012 if (i == FAIL) 6013 clearopbeep(oap); 6014 break; 6015 6016 /* 6017 * "gJ": join two lines without inserting a space. 6018 */ 6019 case 'J': 6020 nv_join(cap); 6021 break; 6022 6023 /* 6024 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines. 6025 * "gm": middle of "g0" and "g$". 6026 */ 6027 case '^': 6028 flag = TRUE; 6029 // FALLTHROUGH 6030 6031 case '0': 6032 case 'm': 6033 case K_HOME: 6034 case K_KHOME: 6035 oap->motion_type = MCHAR; 6036 oap->inclusive = FALSE; 6037 if (curwin->w_p_wrap && curwin->w_width != 0) 6038 { 6039 int width1 = curwin->w_width - curwin_col_off(); 6040 int width2 = width1 + curwin_col_off2(); 6041 6042 validate_virtcol(); 6043 i = 0; 6044 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0) 6045 i = (curwin->w_virtcol - width1) / width2 * width2 + width1; 6046 } 6047 else 6048 i = curwin->w_leftcol; 6049 // Go to the middle of the screen line. When 'number' or 6050 // 'relativenumber' is on and lines are wrapping the middle can be more 6051 // to the left. 6052 if (cap->nchar == 'm') 6053 i += (curwin->w_width - curwin_col_off() 6054 + ((curwin->w_p_wrap && i > 0) 6055 ? curwin_col_off2() : 0)) / 2; 6056 coladvance((colnr_T)i); 6057 if (flag) 6058 { 6059 do 6060 i = gchar_cursor(); 6061 while (VIM_ISWHITE(i) && oneright() == OK); 6062 curwin->w_valid &= ~VALID_WCOL; 6063 } 6064 curwin->w_set_curswant = TRUE; 6065 break; 6066 6067 case 'M': 6068 { 6069 char_u *ptr = ml_get_curline(); 6070 6071 oap->motion_type = MCHAR; 6072 oap->inclusive = FALSE; 6073 if (has_mbyte) 6074 i = mb_string2cells(ptr, (int)STRLEN(ptr)); 6075 else 6076 i = (int)STRLEN(ptr); 6077 if (cap->count0 > 0 && cap->count0 <= 100) 6078 coladvance((colnr_T)(i * cap->count0 / 100)); 6079 else 6080 coladvance((colnr_T)(i / 2)); 6081 curwin->w_set_curswant = TRUE; 6082 } 6083 break; 6084 6085 case '_': 6086 // "g_": to the last non-blank character in the line or <count> lines 6087 // downward. 6088 cap->oap->motion_type = MCHAR; 6089 cap->oap->inclusive = TRUE; 6090 curwin->w_curswant = MAXCOL; 6091 if (cursor_down((long)(cap->count1 - 1), 6092 cap->oap->op_type == OP_NOP) == FAIL) 6093 clearopbeep(cap->oap); 6094 else 6095 { 6096 char_u *ptr = ml_get_curline(); 6097 6098 // In Visual mode we may end up after the line. 6099 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL) 6100 --curwin->w_cursor.col; 6101 6102 // Decrease the cursor column until it's on a non-blank. 6103 while (curwin->w_cursor.col > 0 6104 && VIM_ISWHITE(ptr[curwin->w_cursor.col])) 6105 --curwin->w_cursor.col; 6106 curwin->w_set_curswant = TRUE; 6107 adjust_for_sel(cap); 6108 } 6109 break; 6110 6111 case '$': 6112 case K_END: 6113 case K_KEND: 6114 { 6115 int col_off = curwin_col_off(); 6116 6117 oap->motion_type = MCHAR; 6118 oap->inclusive = TRUE; 6119 if (curwin->w_p_wrap && curwin->w_width != 0) 6120 { 6121 curwin->w_curswant = MAXCOL; // so we stay at the end 6122 if (cap->count1 == 1) 6123 { 6124 int width1 = curwin->w_width - col_off; 6125 int width2 = width1 + curwin_col_off2(); 6126 6127 validate_virtcol(); 6128 i = width1 - 1; 6129 if (curwin->w_virtcol >= (colnr_T)width1) 6130 i += ((curwin->w_virtcol - width1) / width2 + 1) 6131 * width2; 6132 coladvance((colnr_T)i); 6133 6134 // Make sure we stick in this column. 6135 validate_virtcol(); 6136 curwin->w_curswant = curwin->w_virtcol; 6137 curwin->w_set_curswant = FALSE; 6138 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap) 6139 { 6140 /* 6141 * Check for landing on a character that got split at 6142 * the end of the line. We do not want to advance to 6143 * the next screen line. 6144 */ 6145 if (curwin->w_virtcol > (colnr_T)i) 6146 --curwin->w_cursor.col; 6147 } 6148 } 6149 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL) 6150 clearopbeep(oap); 6151 } 6152 else 6153 { 6154 if (cap->count1 > 1) 6155 // if it fails, let the cursor still move to the last char 6156 (void)cursor_down(cap->count1 - 1, FALSE); 6157 6158 i = curwin->w_leftcol + curwin->w_width - col_off - 1; 6159 coladvance((colnr_T)i); 6160 6161 // if the character doesn't fit move one back 6162 if (curwin->w_cursor.col > 0 6163 && (*mb_ptr2cells)(ml_get_cursor()) > 1) 6164 { 6165 colnr_T vcol; 6166 6167 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol); 6168 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off) 6169 --curwin->w_cursor.col; 6170 } 6171 6172 // Make sure we stick in this column. 6173 validate_virtcol(); 6174 curwin->w_curswant = curwin->w_virtcol; 6175 curwin->w_set_curswant = FALSE; 6176 } 6177 } 6178 break; 6179 6180 /* 6181 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>" 6182 */ 6183 case '*': 6184 case '#': 6185 #if POUND != '#' 6186 case POUND: // pound sign (sometimes equal to '#') 6187 #endif 6188 case Ctrl_RSB: // :tag or :tselect for current identifier 6189 case ']': // :tselect for current identifier 6190 nv_ident(cap); 6191 break; 6192 6193 /* 6194 * ge and gE: go back to end of word 6195 */ 6196 case 'e': 6197 case 'E': 6198 oap->motion_type = MCHAR; 6199 curwin->w_set_curswant = TRUE; 6200 oap->inclusive = TRUE; 6201 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL) 6202 clearopbeep(oap); 6203 break; 6204 6205 /* 6206 * "g CTRL-G": display info about cursor position 6207 */ 6208 case Ctrl_G: 6209 cursor_pos_info(NULL); 6210 break; 6211 6212 /* 6213 * "gi": start Insert at the last position. 6214 */ 6215 case 'i': 6216 if (curbuf->b_last_insert.lnum != 0) 6217 { 6218 curwin->w_cursor = curbuf->b_last_insert; 6219 check_cursor_lnum(); 6220 i = (int)STRLEN(ml_get_curline()); 6221 if (curwin->w_cursor.col > (colnr_T)i) 6222 { 6223 if (virtual_active()) 6224 curwin->w_cursor.coladd += curwin->w_cursor.col - i; 6225 curwin->w_cursor.col = i; 6226 } 6227 } 6228 cap->cmdchar = 'i'; 6229 nv_edit(cap); 6230 break; 6231 6232 /* 6233 * "gI": Start insert in column 1. 6234 */ 6235 case 'I': 6236 beginline(0); 6237 if (!checkclearopq(oap)) 6238 invoke_edit(cap, FALSE, 'g', FALSE); 6239 break; 6240 6241 #ifdef FEAT_SEARCHPATH 6242 /* 6243 * "gf": goto file, edit file under cursor 6244 * "]f" and "[f": can also be used. 6245 */ 6246 case 'f': 6247 case 'F': 6248 nv_gotofile(cap); 6249 break; 6250 #endif 6251 6252 // "g'm" and "g`m": jump to mark without setting pcmark 6253 case '\'': 6254 cap->arg = TRUE; 6255 // FALLTHROUGH 6256 case '`': 6257 nv_gomark(cap); 6258 break; 6259 6260 /* 6261 * "gs": Goto sleep. 6262 */ 6263 case 's': 6264 do_sleep(cap->count1 * 1000L, FALSE); 6265 break; 6266 6267 /* 6268 * "ga": Display the ascii value of the character under the 6269 * cursor. It is displayed in decimal, hex, and octal. -- webb 6270 */ 6271 case 'a': 6272 do_ascii(NULL); 6273 break; 6274 6275 /* 6276 * "g8": Display the bytes used for the UTF-8 character under the 6277 * cursor. It is displayed in hex. 6278 * "8g8" finds illegal byte sequence. 6279 */ 6280 case '8': 6281 if (cap->count0 == 8) 6282 utf_find_illegal(); 6283 else 6284 show_utf8(); 6285 break; 6286 6287 // "g<": show scrollback text 6288 case '<': 6289 show_sb_text(); 6290 break; 6291 6292 /* 6293 * "gg": Goto the first line in file. With a count it goes to 6294 * that line number like for "G". -- webb 6295 */ 6296 case 'g': 6297 cap->arg = FALSE; 6298 nv_goto(cap); 6299 break; 6300 6301 /* 6302 * Two-character operators: 6303 * "gq" Format text 6304 * "gw" Format text and keep cursor position 6305 * "g~" Toggle the case of the text. 6306 * "gu" Change text to lower case. 6307 * "gU" Change text to upper case. 6308 * "g?" rot13 encoding 6309 * "g@" call 'operatorfunc' 6310 */ 6311 case 'q': 6312 case 'w': 6313 oap->cursor_start = curwin->w_cursor; 6314 // FALLTHROUGH 6315 case '~': 6316 case 'u': 6317 case 'U': 6318 case '?': 6319 case '@': 6320 nv_operator(cap); 6321 break; 6322 6323 /* 6324 * "gd": Find first occurrence of pattern under the cursor in the 6325 * current function 6326 * "gD": idem, but in the current file. 6327 */ 6328 case 'd': 6329 case 'D': 6330 nv_gd(oap, cap->nchar, (int)cap->count0); 6331 break; 6332 6333 /* 6334 * g<*Mouse> : <C-*mouse> 6335 */ 6336 case K_MIDDLEMOUSE: 6337 case K_MIDDLEDRAG: 6338 case K_MIDDLERELEASE: 6339 case K_LEFTMOUSE: 6340 case K_LEFTDRAG: 6341 case K_LEFTRELEASE: 6342 case K_MOUSEMOVE: 6343 case K_RIGHTMOUSE: 6344 case K_RIGHTDRAG: 6345 case K_RIGHTRELEASE: 6346 case K_X1MOUSE: 6347 case K_X1DRAG: 6348 case K_X1RELEASE: 6349 case K_X2MOUSE: 6350 case K_X2DRAG: 6351 case K_X2RELEASE: 6352 mod_mask = MOD_MASK_CTRL; 6353 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0); 6354 break; 6355 6356 case K_IGNORE: 6357 break; 6358 6359 /* 6360 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text 6361 */ 6362 case 'p': 6363 case 'P': 6364 nv_put(cap); 6365 break; 6366 6367 #ifdef FEAT_BYTEOFF 6368 // "go": goto byte count from start of buffer 6369 case 'o': 6370 goto_byte(cap->count0); 6371 break; 6372 #endif 6373 6374 // "gQ": improved Ex mode 6375 case 'Q': 6376 if (text_locked()) 6377 { 6378 clearopbeep(cap->oap); 6379 text_locked_msg(); 6380 break; 6381 } 6382 6383 if (!checkclearopq(oap)) 6384 do_exmode(TRUE); 6385 break; 6386 6387 #ifdef FEAT_JUMPLIST 6388 case ',': 6389 nv_pcmark(cap); 6390 break; 6391 6392 case ';': 6393 cap->count1 = -cap->count1; 6394 nv_pcmark(cap); 6395 break; 6396 #endif 6397 6398 case 't': 6399 if (!checkclearop(oap)) 6400 goto_tabpage((int)cap->count0); 6401 break; 6402 case 'T': 6403 if (!checkclearop(oap)) 6404 goto_tabpage(-(int)cap->count1); 6405 break; 6406 6407 case TAB: 6408 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL) 6409 clearopbeep(oap); 6410 break; 6411 6412 case '+': 6413 case '-': // "g+" and "g-": undo or redo along the timeline 6414 if (!checkclearopq(oap)) 6415 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1, 6416 FALSE, FALSE, FALSE); 6417 break; 6418 6419 default: 6420 clearopbeep(oap); 6421 break; 6422 } 6423 } 6424 6425 /* 6426 * Handle "o" and "O" commands. 6427 */ 6428 static void 6429 n_opencmd(cmdarg_T *cap) 6430 { 6431 #ifdef FEAT_CONCEAL 6432 linenr_T oldline = curwin->w_cursor.lnum; 6433 #endif 6434 6435 if (!checkclearopq(cap->oap)) 6436 { 6437 #ifdef FEAT_FOLDING 6438 if (cap->cmdchar == 'O') 6439 // Open above the first line of a folded sequence of lines 6440 (void)hasFolding(curwin->w_cursor.lnum, 6441 &curwin->w_cursor.lnum, NULL); 6442 else 6443 // Open below the last line of a folded sequence of lines 6444 (void)hasFolding(curwin->w_cursor.lnum, 6445 NULL, &curwin->w_cursor.lnum); 6446 #endif 6447 if (u_save((linenr_T)(curwin->w_cursor.lnum - 6448 (cap->cmdchar == 'O' ? 1 : 0)), 6449 (linenr_T)(curwin->w_cursor.lnum + 6450 (cap->cmdchar == 'o' ? 1 : 0)) 6451 ) == OK 6452 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD, 6453 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0, 6454 0) == OK) 6455 { 6456 #ifdef FEAT_CONCEAL 6457 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum) 6458 redrawWinline(curwin, oldline); 6459 #endif 6460 #ifdef FEAT_SYN_HL 6461 if (curwin->w_p_cul) 6462 // force redraw of cursorline 6463 curwin->w_valid &= ~VALID_CROW; 6464 #endif 6465 // When '#' is in 'cpoptions' ignore the count. 6466 if (vim_strchr(p_cpo, CPO_HASH) != NULL) 6467 cap->count1 = 1; 6468 invoke_edit(cap, FALSE, cap->cmdchar, TRUE); 6469 } 6470 } 6471 } 6472 6473 /* 6474 * "." command: redo last change. 6475 */ 6476 static void 6477 nv_dot(cmdarg_T *cap) 6478 { 6479 if (!checkclearopq(cap->oap)) 6480 { 6481 /* 6482 * If "restart_edit" is TRUE, the last but one command is repeated 6483 * instead of the last command (inserting text). This is used for 6484 * CTRL-O <.> in insert mode. 6485 */ 6486 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL) 6487 clearopbeep(cap->oap); 6488 } 6489 } 6490 6491 /* 6492 * CTRL-R: undo undo 6493 */ 6494 static void 6495 nv_redo(cmdarg_T *cap) 6496 { 6497 if (!checkclearopq(cap->oap)) 6498 { 6499 u_redo((int)cap->count1); 6500 curwin->w_set_curswant = TRUE; 6501 } 6502 } 6503 6504 /* 6505 * Handle "U" command. 6506 */ 6507 static void 6508 nv_Undo(cmdarg_T *cap) 6509 { 6510 // In Visual mode and typing "gUU" triggers an operator 6511 if (cap->oap->op_type == OP_UPPER || VIsual_active) 6512 { 6513 // translate "gUU" to "gUgU" 6514 cap->cmdchar = 'g'; 6515 cap->nchar = 'U'; 6516 nv_operator(cap); 6517 } 6518 else if (!checkclearopq(cap->oap)) 6519 { 6520 u_undoline(); 6521 curwin->w_set_curswant = TRUE; 6522 } 6523 } 6524 6525 /* 6526 * '~' command: If tilde is not an operator and Visual is off: swap case of a 6527 * single character. 6528 */ 6529 static void 6530 nv_tilde(cmdarg_T *cap) 6531 { 6532 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE) 6533 { 6534 #ifdef FEAT_JOB_CHANNEL 6535 if (bt_prompt(curbuf) && !prompt_curpos_editable()) 6536 { 6537 clearopbeep(cap->oap); 6538 return; 6539 } 6540 #endif 6541 n_swapchar(cap); 6542 } 6543 else 6544 nv_operator(cap); 6545 } 6546 6547 /* 6548 * Handle an operator command. 6549 * The actual work is done by do_pending_operator(). 6550 */ 6551 static void 6552 nv_operator(cmdarg_T *cap) 6553 { 6554 int op_type; 6555 6556 op_type = get_op_type(cap->cmdchar, cap->nchar); 6557 #ifdef FEAT_JOB_CHANNEL 6558 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable()) 6559 { 6560 clearopbeep(cap->oap); 6561 return; 6562 } 6563 #endif 6564 6565 if (op_type == cap->oap->op_type) // double operator works on lines 6566 nv_lineop(cap); 6567 else if (!checkclearop(cap->oap)) 6568 { 6569 cap->oap->start = curwin->w_cursor; 6570 cap->oap->op_type = op_type; 6571 #ifdef FEAT_EVAL 6572 set_op_var(op_type); 6573 #endif 6574 } 6575 } 6576 6577 #ifdef FEAT_EVAL 6578 /* 6579 * Set v:operator to the characters for "optype". 6580 */ 6581 static void 6582 set_op_var(int optype) 6583 { 6584 char_u opchars[3]; 6585 6586 if (optype == OP_NOP) 6587 set_vim_var_string(VV_OP, NULL, 0); 6588 else 6589 { 6590 opchars[0] = get_op_char(optype); 6591 opchars[1] = get_extra_op_char(optype); 6592 opchars[2] = NUL; 6593 set_vim_var_string(VV_OP, opchars, -1); 6594 } 6595 } 6596 #endif 6597 6598 /* 6599 * Handle linewise operator "dd", "yy", etc. 6600 * 6601 * "_" is is a strange motion command that helps make operators more logical. 6602 * It is actually implemented, but not documented in the real Vi. This motion 6603 * command actually refers to "the current line". Commands like "dd" and "yy" 6604 * are really an alternate form of "d_" and "y_". It does accept a count, so 6605 * "d3_" works to delete 3 lines. 6606 */ 6607 static void 6608 nv_lineop(cmdarg_T *cap) 6609 { 6610 cap->oap->motion_type = MLINE; 6611 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL) 6612 clearopbeep(cap->oap); 6613 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions 6614 && cap->oap->motion_force != 'v' 6615 && cap->oap->motion_force != Ctrl_V) 6616 || cap->oap->op_type == OP_LSHIFT 6617 || cap->oap->op_type == OP_RSHIFT) 6618 beginline(BL_SOL | BL_FIX); 6619 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor 6620 beginline(BL_WHITE | BL_FIX); 6621 } 6622 6623 /* 6624 * <Home> command. 6625 */ 6626 static void 6627 nv_home(cmdarg_T *cap) 6628 { 6629 // CTRL-HOME is like "gg" 6630 if (mod_mask & MOD_MASK_CTRL) 6631 nv_goto(cap); 6632 else 6633 { 6634 cap->count0 = 1; 6635 nv_pipe(cap); 6636 } 6637 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a 6638 // one-character line). 6639 } 6640 6641 /* 6642 * "|" command. 6643 */ 6644 static void 6645 nv_pipe(cmdarg_T *cap) 6646 { 6647 cap->oap->motion_type = MCHAR; 6648 cap->oap->inclusive = FALSE; 6649 beginline(0); 6650 if (cap->count0 > 0) 6651 { 6652 coladvance((colnr_T)(cap->count0 - 1)); 6653 curwin->w_curswant = (colnr_T)(cap->count0 - 1); 6654 } 6655 else 6656 curwin->w_curswant = 0; 6657 // keep curswant at the column where we wanted to go, not where 6658 // we ended; differs if line is too short 6659 curwin->w_set_curswant = FALSE; 6660 } 6661 6662 /* 6663 * Handle back-word command "b" and "B". 6664 * cap->arg is 1 for "B" 6665 */ 6666 static void 6667 nv_bck_word(cmdarg_T *cap) 6668 { 6669 cap->oap->motion_type = MCHAR; 6670 cap->oap->inclusive = FALSE; 6671 curwin->w_set_curswant = TRUE; 6672 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL) 6673 clearopbeep(cap->oap); 6674 #ifdef FEAT_FOLDING 6675 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) 6676 foldOpenCursor(); 6677 #endif 6678 } 6679 6680 /* 6681 * Handle word motion commands "e", "E", "w" and "W". 6682 * cap->arg is TRUE for "E" and "W". 6683 */ 6684 static void 6685 nv_wordcmd(cmdarg_T *cap) 6686 { 6687 int n; 6688 int word_end; 6689 int flag = FALSE; 6690 pos_T startpos = curwin->w_cursor; 6691 6692 /* 6693 * Set inclusive for the "E" and "e" command. 6694 */ 6695 if (cap->cmdchar == 'e' || cap->cmdchar == 'E') 6696 word_end = TRUE; 6697 else 6698 word_end = FALSE; 6699 cap->oap->inclusive = word_end; 6700 6701 /* 6702 * "cw" and "cW" are a special case. 6703 */ 6704 if (!word_end && cap->oap->op_type == OP_CHANGE) 6705 { 6706 n = gchar_cursor(); 6707 if (n != NUL) // not an empty line 6708 { 6709 if (VIM_ISWHITE(n)) 6710 { 6711 /* 6712 * Reproduce a funny Vi behaviour: "cw" on a blank only 6713 * changes one character, not all blanks until the start of 6714 * the next word. Only do this when the 'w' flag is included 6715 * in 'cpoptions'. 6716 */ 6717 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL) 6718 { 6719 cap->oap->inclusive = TRUE; 6720 cap->oap->motion_type = MCHAR; 6721 return; 6722 } 6723 } 6724 else 6725 { 6726 /* 6727 * This is a little strange. To match what the real Vi does, 6728 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided 6729 * that we are not on a space or a TAB. This seems impolite 6730 * at first, but it's really more what we mean when we say 6731 * 'cw'. 6732 * Another strangeness: When standing on the end of a word 6733 * "ce" will change until the end of the next word, but "cw" 6734 * will change only one character! This is done by setting 6735 * flag. 6736 */ 6737 cap->oap->inclusive = TRUE; 6738 word_end = TRUE; 6739 flag = TRUE; 6740 } 6741 } 6742 } 6743 6744 cap->oap->motion_type = MCHAR; 6745 curwin->w_set_curswant = TRUE; 6746 if (word_end) 6747 n = end_word(cap->count1, cap->arg, flag, FALSE); 6748 else 6749 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP); 6750 6751 // Don't leave the cursor on the NUL past the end of line. Unless we 6752 // didn't move it forward. 6753 if (LT_POS(startpos, curwin->w_cursor)) 6754 adjust_cursor(cap->oap); 6755 6756 if (n == FAIL && cap->oap->op_type == OP_NOP) 6757 clearopbeep(cap->oap); 6758 else 6759 { 6760 adjust_for_sel(cap); 6761 #ifdef FEAT_FOLDING 6762 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) 6763 foldOpenCursor(); 6764 #endif 6765 } 6766 } 6767 6768 /* 6769 * Used after a movement command: If the cursor ends up on the NUL after the 6770 * end of the line, may move it back to the last character and make the motion 6771 * inclusive. 6772 */ 6773 static void 6774 adjust_cursor(oparg_T *oap) 6775 { 6776 // The cursor cannot remain on the NUL when: 6777 // - the column is > 0 6778 // - not in Visual mode or 'selection' is "o" 6779 // - 'virtualedit' is not "all" and not "onemore". 6780 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL 6781 && (!VIsual_active || *p_sel == 'o') 6782 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0) 6783 { 6784 --curwin->w_cursor.col; 6785 // prevent cursor from moving on the trail byte 6786 if (has_mbyte) 6787 mb_adjust_cursor(); 6788 oap->inclusive = TRUE; 6789 } 6790 } 6791 6792 /* 6793 * "0" and "^" commands. 6794 * cap->arg is the argument for beginline(). 6795 */ 6796 static void 6797 nv_beginline(cmdarg_T *cap) 6798 { 6799 cap->oap->motion_type = MCHAR; 6800 cap->oap->inclusive = FALSE; 6801 beginline(cap->arg); 6802 #ifdef FEAT_FOLDING 6803 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP) 6804 foldOpenCursor(); 6805 #endif 6806 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a 6807 // one-character line). 6808 } 6809 6810 /* 6811 * In exclusive Visual mode, may include the last character. 6812 */ 6813 static void 6814 adjust_for_sel(cmdarg_T *cap) 6815 { 6816 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e' 6817 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor)) 6818 { 6819 if (has_mbyte) 6820 inc_cursor(); 6821 else 6822 ++curwin->w_cursor.col; 6823 cap->oap->inclusive = FALSE; 6824 } 6825 } 6826 6827 /* 6828 * Exclude last character at end of Visual area for 'selection' == "exclusive". 6829 * Should check VIsual_mode before calling this. 6830 * Returns TRUE when backed up to the previous line. 6831 */ 6832 int 6833 unadjust_for_sel(void) 6834 { 6835 pos_T *pp; 6836 6837 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor)) 6838 { 6839 if (LT_POS(VIsual, curwin->w_cursor)) 6840 pp = &curwin->w_cursor; 6841 else 6842 pp = &VIsual; 6843 if (pp->coladd > 0) 6844 --pp->coladd; 6845 else 6846 if (pp->col > 0) 6847 { 6848 --pp->col; 6849 mb_adjustpos(curbuf, pp); 6850 } 6851 else if (pp->lnum > 1) 6852 { 6853 --pp->lnum; 6854 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum)); 6855 return TRUE; 6856 } 6857 } 6858 return FALSE; 6859 } 6860 6861 /* 6862 * SELECT key in Normal or Visual mode: end of Select mode mapping. 6863 */ 6864 static void 6865 nv_select(cmdarg_T *cap) 6866 { 6867 if (VIsual_active) 6868 VIsual_select = TRUE; 6869 else if (VIsual_reselect) 6870 { 6871 cap->nchar = 'v'; // fake "gv" command 6872 cap->arg = TRUE; 6873 nv_g_cmd(cap); 6874 } 6875 } 6876 6877 6878 /* 6879 * "G", "gg", CTRL-END, CTRL-HOME. 6880 * cap->arg is TRUE for "G". 6881 */ 6882 static void 6883 nv_goto(cmdarg_T *cap) 6884 { 6885 linenr_T lnum; 6886 6887 if (cap->arg) 6888 lnum = curbuf->b_ml.ml_line_count; 6889 else 6890 lnum = 1L; 6891 cap->oap->motion_type = MLINE; 6892 setpcmark(); 6893 6894 // When a count is given, use it instead of the default lnum 6895 if (cap->count0 != 0) 6896 lnum = cap->count0; 6897 if (lnum < 1L) 6898 lnum = 1L; 6899 else if (lnum > curbuf->b_ml.ml_line_count) 6900 lnum = curbuf->b_ml.ml_line_count; 6901 curwin->w_cursor.lnum = lnum; 6902 beginline(BL_SOL | BL_FIX); 6903 #ifdef FEAT_FOLDING 6904 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP) 6905 foldOpenCursor(); 6906 #endif 6907 } 6908 6909 /* 6910 * CTRL-\ in Normal mode. 6911 */ 6912 static void 6913 nv_normal(cmdarg_T *cap) 6914 { 6915 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G) 6916 { 6917 clearop(cap->oap); 6918 if (restart_edit != 0 && mode_displayed) 6919 clear_cmdline = TRUE; // unshow mode later 6920 restart_edit = 0; 6921 #ifdef FEAT_CMDWIN 6922 if (cmdwin_type != 0) 6923 cmdwin_result = Ctrl_C; 6924 #endif 6925 if (VIsual_active) 6926 { 6927 end_visual_mode(); // stop Visual 6928 redraw_curbuf_later(INVERTED); 6929 } 6930 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. 6931 if (cap->nchar == Ctrl_G && p_im) 6932 restart_edit = 'a'; 6933 } 6934 else 6935 clearopbeep(cap->oap); 6936 } 6937 6938 /* 6939 * ESC in Normal mode: beep, but don't flush buffers. 6940 * Don't even beep if we are canceling a command. 6941 */ 6942 static void 6943 nv_esc(cmdarg_T *cap) 6944 { 6945 int no_reason; 6946 6947 no_reason = (cap->oap->op_type == OP_NOP 6948 && cap->opcount == 0 6949 && cap->count0 == 0 6950 && cap->oap->regname == 0 6951 && !p_im); 6952 6953 if (cap->arg) // TRUE for CTRL-C 6954 { 6955 if (restart_edit == 0 6956 #ifdef FEAT_CMDWIN 6957 && cmdwin_type == 0 6958 #endif 6959 && !VIsual_active 6960 && no_reason) 6961 { 6962 if (anyBufIsChanged()) 6963 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim")); 6964 else 6965 msg(_("Type :qa and press <Enter> to exit Vim")); 6966 } 6967 6968 // Don't reset "restart_edit" when 'insertmode' is set, it won't be 6969 // set again below when halfway a mapping. 6970 if (!p_im) 6971 restart_edit = 0; 6972 #ifdef FEAT_CMDWIN 6973 if (cmdwin_type != 0) 6974 { 6975 cmdwin_result = K_IGNORE; 6976 got_int = FALSE; // don't stop executing autocommands et al. 6977 return; 6978 } 6979 #endif 6980 } 6981 #ifdef FEAT_CMDWIN 6982 else if (cmdwin_type != 0 && ex_normal_busy) 6983 { 6984 // When :normal runs out of characters while in the command line window 6985 // vgetorpeek() will return ESC. Exit the cmdline window to break the 6986 // loop. 6987 cmdwin_result = K_IGNORE; 6988 return; 6989 } 6990 #endif 6991 6992 if (VIsual_active) 6993 { 6994 end_visual_mode(); // stop Visual 6995 check_cursor_col(); // make sure cursor is not beyond EOL 6996 curwin->w_set_curswant = TRUE; 6997 redraw_curbuf_later(INVERTED); 6998 } 6999 else if (no_reason) 7000 vim_beep(BO_ESC); 7001 clearop(cap->oap); 7002 7003 // A CTRL-C is often used at the start of a menu. When 'insertmode' is 7004 // set return to Insert mode afterwards. 7005 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0) 7006 restart_edit = 'a'; 7007 } 7008 7009 /* 7010 * Move the cursor for the "A" command. 7011 */ 7012 void 7013 set_cursor_for_append_to_line(void) 7014 { 7015 curwin->w_set_curswant = TRUE; 7016 if (get_ve_flags() == VE_ALL) 7017 { 7018 int save_State = State; 7019 7020 // Pretend Insert mode here to allow the cursor on the 7021 // character past the end of the line 7022 State = INSERT; 7023 coladvance((colnr_T)MAXCOL); 7024 State = save_State; 7025 } 7026 else 7027 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); 7028 } 7029 7030 /* 7031 * Handle "A", "a", "I", "i" and <Insert> commands. 7032 * Also handle K_PS, start bracketed paste. 7033 */ 7034 static void 7035 nv_edit(cmdarg_T *cap) 7036 { 7037 // <Insert> is equal to "i" 7038 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS) 7039 cap->cmdchar = 'i'; 7040 7041 // in Visual mode "A" and "I" are an operator 7042 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I')) 7043 { 7044 #ifdef FEAT_TERMINAL 7045 if (term_in_normal_mode()) 7046 { 7047 end_visual_mode(); 7048 clearop(cap->oap); 7049 term_enter_job_mode(); 7050 return; 7051 } 7052 #endif 7053 v_visop(cap); 7054 } 7055 7056 // in Visual mode and after an operator "a" and "i" are for text objects 7057 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i') 7058 && (cap->oap->op_type != OP_NOP || VIsual_active)) 7059 { 7060 #ifdef FEAT_TEXTOBJ 7061 nv_object(cap); 7062 #else 7063 clearopbeep(cap->oap); 7064 #endif 7065 } 7066 #ifdef FEAT_TERMINAL 7067 else if (term_in_normal_mode()) 7068 { 7069 clearop(cap->oap); 7070 term_enter_job_mode(); 7071 return; 7072 } 7073 #endif 7074 else if (!curbuf->b_p_ma && !p_im) 7075 { 7076 // Only give this error when 'insertmode' is off. 7077 emsg(_(e_cannot_make_changes_modifiable_is_off)); 7078 clearop(cap->oap); 7079 if (cap->cmdchar == K_PS) 7080 // drop the pasted text 7081 bracketed_paste(PASTE_INSERT, TRUE, NULL); 7082 } 7083 else if (cap->cmdchar == K_PS && VIsual_active) 7084 { 7085 pos_T old_pos = curwin->w_cursor; 7086 pos_T old_visual = VIsual; 7087 7088 // In Visual mode the selected text is deleted. 7089 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum) 7090 { 7091 shift_delete_registers(); 7092 cap->oap->regname = '1'; 7093 } 7094 else 7095 cap->oap->regname = '-'; 7096 cap->cmdchar = 'd'; 7097 cap->nchar = NUL; 7098 nv_operator(cap); 7099 do_pending_operator(cap, 0, FALSE); 7100 cap->cmdchar = K_PS; 7101 7102 // When the last char in the line was deleted then append. Detect this 7103 // by checking if the cursor moved to before the Visual area. 7104 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos) 7105 && LT_POS(curwin->w_cursor, old_visual)) 7106 inc_cursor(); 7107 7108 // Insert to replace the deleted text with the pasted text. 7109 invoke_edit(cap, FALSE, cap->cmdchar, FALSE); 7110 } 7111 else if (!checkclearopq(cap->oap)) 7112 { 7113 switch (cap->cmdchar) 7114 { 7115 case 'A': // "A"ppend after the line 7116 set_cursor_for_append_to_line(); 7117 break; 7118 7119 case 'I': // "I"nsert before the first non-blank 7120 if (vim_strchr(p_cpo, CPO_INSEND) == NULL) 7121 beginline(BL_WHITE); 7122 else 7123 beginline(BL_WHITE|BL_FIX); 7124 break; 7125 7126 case K_PS: 7127 // Bracketed paste works like "a"ppend, unless the cursor is in 7128 // the first column, then it inserts. 7129 if (curwin->w_cursor.col == 0) 7130 break; 7131 // FALLTHROUGH 7132 7133 case 'a': // "a"ppend is like "i"nsert on the next character. 7134 // increment coladd when in virtual space, increment the 7135 // column otherwise, also to append after an unprintable char 7136 if (virtual_active() 7137 && (curwin->w_cursor.coladd > 0 7138 || *ml_get_cursor() == NUL 7139 || *ml_get_cursor() == TAB)) 7140 curwin->w_cursor.coladd++; 7141 else if (*ml_get_cursor() != NUL) 7142 inc_cursor(); 7143 break; 7144 } 7145 7146 if (curwin->w_cursor.coladd && cap->cmdchar != 'A') 7147 { 7148 int save_State = State; 7149 7150 // Pretend Insert mode here to allow the cursor on the 7151 // character past the end of the line 7152 State = INSERT; 7153 coladvance(getviscol()); 7154 State = save_State; 7155 } 7156 7157 invoke_edit(cap, FALSE, cap->cmdchar, FALSE); 7158 } 7159 else if (cap->cmdchar == K_PS) 7160 // drop the pasted text 7161 bracketed_paste(PASTE_INSERT, TRUE, NULL); 7162 } 7163 7164 /* 7165 * Invoke edit() and take care of "restart_edit" and the return value. 7166 */ 7167 static void 7168 invoke_edit( 7169 cmdarg_T *cap, 7170 int repl, // "r" or "gr" command 7171 int cmd, 7172 int startln) 7173 { 7174 int restart_edit_save = 0; 7175 7176 // Complicated: When the user types "a<C-O>a" we don't want to do Insert 7177 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow 7178 // it. 7179 if (repl || !stuff_empty()) 7180 restart_edit_save = restart_edit; 7181 else 7182 restart_edit_save = 0; 7183 7184 // Always reset "restart_edit", this is not a restarted edit. 7185 restart_edit = 0; 7186 7187 if (edit(cmd, startln, cap->count1)) 7188 cap->retval |= CA_COMMAND_BUSY; 7189 7190 if (restart_edit == 0) 7191 restart_edit = restart_edit_save; 7192 } 7193 7194 #ifdef FEAT_TEXTOBJ 7195 /* 7196 * "a" or "i" while an operator is pending or in Visual mode: object motion. 7197 */ 7198 static void 7199 nv_object( 7200 cmdarg_T *cap) 7201 { 7202 int flag; 7203 int include; 7204 char_u *mps_save; 7205 7206 if (cap->cmdchar == 'i') 7207 include = FALSE; // "ix" = inner object: exclude white space 7208 else 7209 include = TRUE; // "ax" = an object: include white space 7210 7211 // Make sure (), [], {} and <> are in 'matchpairs' 7212 mps_save = curbuf->b_p_mps; 7213 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>"; 7214 7215 switch (cap->nchar) 7216 { 7217 case 'w': // "aw" = a word 7218 flag = current_word(cap->oap, cap->count1, include, FALSE); 7219 break; 7220 case 'W': // "aW" = a WORD 7221 flag = current_word(cap->oap, cap->count1, include, TRUE); 7222 break; 7223 case 'b': // "ab" = a braces block 7224 case '(': 7225 case ')': 7226 flag = current_block(cap->oap, cap->count1, include, '(', ')'); 7227 break; 7228 case 'B': // "aB" = a Brackets block 7229 case '{': 7230 case '}': 7231 flag = current_block(cap->oap, cap->count1, include, '{', '}'); 7232 break; 7233 case '[': // "a[" = a [] block 7234 case ']': 7235 flag = current_block(cap->oap, cap->count1, include, '[', ']'); 7236 break; 7237 case '<': // "a<" = a <> block 7238 case '>': 7239 flag = current_block(cap->oap, cap->count1, include, '<', '>'); 7240 break; 7241 case 't': // "at" = a tag block (xml and html) 7242 // Do not adjust oap->end in do_pending_operator() 7243 // otherwise there are different results for 'dit' 7244 // (note leading whitespace in last line): 7245 // 1) <b> 2) <b> 7246 // foobar foobar 7247 // </b> </b> 7248 cap->retval |= CA_NO_ADJ_OP_END; 7249 flag = current_tagblock(cap->oap, cap->count1, include); 7250 break; 7251 case 'p': // "ap" = a paragraph 7252 flag = current_par(cap->oap, cap->count1, include, 'p'); 7253 break; 7254 case 's': // "as" = a sentence 7255 flag = current_sent(cap->oap, cap->count1, include); 7256 break; 7257 case '"': // "a"" = a double quoted string 7258 case '\'': // "a'" = a single quoted string 7259 case '`': // "a`" = a backtick quoted string 7260 flag = current_quote(cap->oap, cap->count1, include, 7261 cap->nchar); 7262 break; 7263 #if 0 // TODO 7264 case 'S': // "aS" = a section 7265 case 'f': // "af" = a filename 7266 case 'u': // "au" = a URL 7267 #endif 7268 default: 7269 flag = FAIL; 7270 break; 7271 } 7272 7273 curbuf->b_p_mps = mps_save; 7274 if (flag == FAIL) 7275 clearopbeep(cap->oap); 7276 adjust_cursor_col(); 7277 curwin->w_set_curswant = TRUE; 7278 } 7279 #endif 7280 7281 /* 7282 * "q" command: Start/stop recording. 7283 * "q:", "q/", "q?": edit command-line in command-line window. 7284 */ 7285 static void 7286 nv_record(cmdarg_T *cap) 7287 { 7288 if (cap->oap->op_type == OP_FORMAT) 7289 { 7290 // "gqq" is the same as "gqgq": format line 7291 cap->cmdchar = 'g'; 7292 cap->nchar = 'q'; 7293 nv_operator(cap); 7294 } 7295 else if (!checkclearop(cap->oap)) 7296 { 7297 #ifdef FEAT_CMDWIN 7298 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?') 7299 { 7300 stuffcharReadbuff(cap->nchar); 7301 stuffcharReadbuff(K_CMDWIN); 7302 } 7303 else 7304 #endif 7305 // (stop) recording into a named register, unless executing a 7306 // register 7307 if (reg_executing == 0 && do_record(cap->nchar) == FAIL) 7308 clearopbeep(cap->oap); 7309 } 7310 } 7311 7312 /* 7313 * Handle the "@r" command. 7314 */ 7315 static void 7316 nv_at(cmdarg_T *cap) 7317 { 7318 if (checkclearop(cap->oap)) 7319 return; 7320 #ifdef FEAT_EVAL 7321 if (cap->nchar == '=') 7322 { 7323 if (get_expr_register() == NUL) 7324 return; 7325 } 7326 #endif 7327 while (cap->count1-- && !got_int) 7328 { 7329 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL) 7330 { 7331 clearopbeep(cap->oap); 7332 break; 7333 } 7334 line_breakcheck(); 7335 } 7336 } 7337 7338 /* 7339 * Handle the CTRL-U and CTRL-D commands. 7340 */ 7341 static void 7342 nv_halfpage(cmdarg_T *cap) 7343 { 7344 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1) 7345 || (cap->cmdchar == Ctrl_D 7346 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)) 7347 clearopbeep(cap->oap); 7348 else if (!checkclearop(cap->oap)) 7349 halfpage(cap->cmdchar == Ctrl_D, cap->count0); 7350 } 7351 7352 /* 7353 * Handle "J" or "gJ" command. 7354 */ 7355 static void 7356 nv_join(cmdarg_T *cap) 7357 { 7358 if (VIsual_active) // join the visual lines 7359 nv_operator(cap); 7360 else if (!checkclearop(cap->oap)) 7361 { 7362 if (cap->count0 <= 1) 7363 cap->count0 = 2; // default for join is two lines! 7364 if (curwin->w_cursor.lnum + cap->count0 - 1 > 7365 curbuf->b_ml.ml_line_count) 7366 { 7367 // can't join when on the last line 7368 if (cap->count0 <= 2) 7369 { 7370 clearopbeep(cap->oap); 7371 return; 7372 } 7373 cap->count0 = curbuf->b_ml.ml_line_count 7374 - curwin->w_cursor.lnum + 1; 7375 } 7376 7377 prep_redo(cap->oap->regname, cap->count0, 7378 NUL, cap->cmdchar, NUL, NUL, cap->nchar); 7379 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE); 7380 } 7381 } 7382 7383 /* 7384 * "P", "gP", "p" and "gp" commands. 7385 */ 7386 static void 7387 nv_put(cmdarg_T *cap) 7388 { 7389 nv_put_opt(cap, FALSE); 7390 } 7391 7392 /* 7393 * "P", "gP", "p" and "gp" commands. 7394 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P". 7395 */ 7396 static void 7397 nv_put_opt(cmdarg_T *cap, int fix_indent) 7398 { 7399 int regname = 0; 7400 void *reg1 = NULL, *reg2 = NULL; 7401 int empty = FALSE; 7402 int was_visual = FALSE; 7403 int dir; 7404 int flags = 0; 7405 7406 if (cap->oap->op_type != OP_NOP) 7407 { 7408 #ifdef FEAT_DIFF 7409 // "dp" is ":diffput" 7410 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p') 7411 { 7412 clearop(cap->oap); 7413 nv_diffgetput(TRUE, cap->opcount); 7414 } 7415 else 7416 #endif 7417 clearopbeep(cap->oap); 7418 } 7419 #ifdef FEAT_JOB_CHANNEL 7420 else if (bt_prompt(curbuf) && !prompt_curpos_editable()) 7421 { 7422 clearopbeep(cap->oap); 7423 } 7424 #endif 7425 else 7426 { 7427 if (fix_indent) 7428 { 7429 dir = (cap->cmdchar == ']' && cap->nchar == 'p') 7430 ? FORWARD : BACKWARD; 7431 flags |= PUT_FIXINDENT; 7432 } 7433 else 7434 dir = (cap->cmdchar == 'P' 7435 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z') 7436 && cap->nchar == 'P')) ? BACKWARD : FORWARD; 7437 prep_redo_cmd(cap); 7438 if (cap->cmdchar == 'g') 7439 flags |= PUT_CURSEND; 7440 else if (cap->cmdchar == 'z') 7441 flags |= PUT_BLOCK_INNER; 7442 7443 if (VIsual_active) 7444 { 7445 // Putting in Visual mode: The put text replaces the selected 7446 // text. First delete the selected text, then put the new text. 7447 // Need to save and restore the registers that the delete 7448 // overwrites if the old contents is being put. 7449 was_visual = TRUE; 7450 regname = cap->oap->regname; 7451 #ifdef FEAT_CLIPBOARD 7452 adjust_clip_reg(®name); 7453 #endif 7454 if (regname == 0 || regname == '"' 7455 || VIM_ISDIGIT(regname) || regname == '-' 7456 #ifdef FEAT_CLIPBOARD 7457 || (clip_unnamed && (regname == '*' || regname == '+')) 7458 #endif 7459 7460 ) 7461 { 7462 // The delete is going to overwrite the register we want to 7463 // put, save it first. 7464 reg1 = get_register(regname, TRUE); 7465 } 7466 7467 // Now delete the selected text. Avoid messages here. 7468 cap->cmdchar = 'd'; 7469 cap->nchar = NUL; 7470 cap->oap->regname = NUL; 7471 ++msg_silent; 7472 nv_operator(cap); 7473 do_pending_operator(cap, 0, FALSE); 7474 empty = (curbuf->b_ml.ml_flags & ML_EMPTY); 7475 --msg_silent; 7476 7477 // delete PUT_LINE_BACKWARD; 7478 cap->oap->regname = regname; 7479 7480 if (reg1 != NULL) 7481 { 7482 // Delete probably changed the register we want to put, save 7483 // it first. Then put back what was there before the delete. 7484 reg2 = get_register(regname, FALSE); 7485 put_register(regname, reg1); 7486 } 7487 7488 // When deleted a linewise Visual area, put the register as 7489 // lines to avoid it joined with the next line. When deletion was 7490 // characterwise, split a line when putting lines. 7491 if (VIsual_mode == 'V') 7492 flags |= PUT_LINE; 7493 else if (VIsual_mode == 'v') 7494 flags |= PUT_LINE_SPLIT; 7495 if (VIsual_mode == Ctrl_V && dir == FORWARD) 7496 flags |= PUT_LINE_FORWARD; 7497 dir = BACKWARD; 7498 if ((VIsual_mode != 'V' 7499 && curwin->w_cursor.col < curbuf->b_op_start.col) 7500 || (VIsual_mode == 'V' 7501 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum)) 7502 // cursor is at the end of the line or end of file, put 7503 // forward. 7504 dir = FORWARD; 7505 // May have been reset in do_put(). 7506 VIsual_active = TRUE; 7507 } 7508 do_put(cap->oap->regname, NULL, dir, cap->count1, flags); 7509 7510 // If a register was saved, put it back now. 7511 if (reg2 != NULL) 7512 put_register(regname, reg2); 7513 7514 // What to reselect with "gv"? Selecting the just put text seems to 7515 // be the most useful, since the original text was removed. 7516 if (was_visual) 7517 { 7518 curbuf->b_visual.vi_start = curbuf->b_op_start; 7519 curbuf->b_visual.vi_end = curbuf->b_op_end; 7520 // need to adjust cursor position 7521 if (*p_sel == 'e') 7522 inc(&curbuf->b_visual.vi_end); 7523 } 7524 7525 // When all lines were selected and deleted do_put() leaves an empty 7526 // line that needs to be deleted now. 7527 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL) 7528 { 7529 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE); 7530 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1); 7531 7532 // If the cursor was in that line, move it to the end of the last 7533 // line. 7534 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 7535 { 7536 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 7537 coladvance((colnr_T)MAXCOL); 7538 } 7539 } 7540 auto_format(FALSE, TRUE); 7541 } 7542 } 7543 7544 /* 7545 * "o" and "O" commands. 7546 */ 7547 static void 7548 nv_open(cmdarg_T *cap) 7549 { 7550 #ifdef FEAT_DIFF 7551 // "do" is ":diffget" 7552 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o') 7553 { 7554 clearop(cap->oap); 7555 nv_diffgetput(FALSE, cap->opcount); 7556 } 7557 else 7558 #endif 7559 if (VIsual_active) // switch start and end of visual 7560 v_swap_corners(cap->cmdchar); 7561 #ifdef FEAT_JOB_CHANNEL 7562 else if (bt_prompt(curbuf)) 7563 clearopbeep(cap->oap); 7564 #endif 7565 else 7566 n_opencmd(cap); 7567 } 7568 7569 #ifdef FEAT_NETBEANS_INTG 7570 static void 7571 nv_nbcmd(cmdarg_T *cap) 7572 { 7573 netbeans_keycommand(cap->nchar); 7574 } 7575 #endif 7576 7577 #ifdef FEAT_DND 7578 static void 7579 nv_drop(cmdarg_T *cap UNUSED) 7580 { 7581 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND); 7582 } 7583 #endif 7584 7585 /* 7586 * Trigger CursorHold event. 7587 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the 7588 * input buffer. "did_cursorhold" is set to avoid retriggering. 7589 */ 7590 static void 7591 nv_cursorhold(cmdarg_T *cap) 7592 { 7593 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf); 7594 did_cursorhold = TRUE; 7595 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now 7596 } 7597