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