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