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