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