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