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