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