Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | /* |
| 11 | * ops.c: implementation of various operators: op_shift, op_delete, op_tilde, |
| 12 | * op_change, op_yank, do_put, do_join |
| 13 | */ |
| 14 | |
| 15 | #include "vim.h" |
| 16 | |
| 17 | /* |
| 18 | * Number of registers. |
| 19 | * 0 = unnamed register, for normal yanks and puts |
| 20 | * 1..9 = registers '1' to '9', for deletes |
| 21 | * 10..35 = registers 'a' to 'z' |
| 22 | * 36 = delete register '-' |
| 23 | * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined |
| 24 | * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined |
| 25 | */ |
| 26 | /* |
| 27 | * Symbolic names for some registers. |
| 28 | */ |
| 29 | #define DELETION_REGISTER 36 |
| 30 | #ifdef FEAT_CLIPBOARD |
| 31 | # define STAR_REGISTER 37 |
| 32 | # ifdef FEAT_X11 |
| 33 | # define PLUS_REGISTER 38 |
| 34 | # else |
| 35 | # define PLUS_REGISTER STAR_REGISTER /* there is only one */ |
| 36 | # endif |
| 37 | #endif |
| 38 | #ifdef FEAT_DND |
| 39 | # define TILDE_REGISTER (PLUS_REGISTER + 1) |
| 40 | #endif |
| 41 | |
| 42 | #ifdef FEAT_CLIPBOARD |
| 43 | # ifdef FEAT_DND |
| 44 | # define NUM_REGISTERS (TILDE_REGISTER + 1) |
| 45 | # else |
| 46 | # define NUM_REGISTERS (PLUS_REGISTER + 1) |
| 47 | # endif |
| 48 | #else |
| 49 | # define NUM_REGISTERS 37 |
| 50 | #endif |
| 51 | |
| 52 | /* |
| 53 | * Each yank register is an array of pointers to lines. |
| 54 | */ |
| 55 | static struct yankreg |
| 56 | { |
| 57 | char_u **y_array; /* pointer to array of line pointers */ |
| 58 | linenr_T y_size; /* number of lines in y_array */ |
| 59 | char_u y_type; /* MLINE, MCHAR or MBLOCK */ |
| 60 | #ifdef FEAT_VISUAL |
| 61 | colnr_T y_width; /* only set if y_type == MBLOCK */ |
| 62 | #endif |
| 63 | } y_regs[NUM_REGISTERS]; |
| 64 | |
| 65 | static struct yankreg *y_current; /* ptr to current yankreg */ |
| 66 | static int y_append; /* TRUE when appending */ |
| 67 | static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */ |
| 68 | |
| 69 | /* |
| 70 | * structure used by block_prep, op_delete and op_yank for blockwise operators |
| 71 | * also op_change, op_shift, op_insert, op_replace - AKelly |
| 72 | */ |
| 73 | struct block_def |
| 74 | { |
| 75 | int startspaces; /* 'extra' cols of first char */ |
| 76 | int endspaces; /* 'extra' cols of first char */ |
| 77 | int textlen; /* chars in block */ |
| 78 | char_u *textstart; /* pointer to 1st char in block */ |
| 79 | colnr_T textcol; /* cols of chars (at least part.) in block */ |
| 80 | colnr_T start_vcol; /* start col of 1st char wholly inside block */ |
| 81 | colnr_T end_vcol; /* start col of 1st char wholly after block */ |
| 82 | #ifdef FEAT_VISUALEXTRA |
| 83 | int is_short; /* TRUE if line is too short to fit in block */ |
| 84 | int is_MAX; /* TRUE if curswant==MAXCOL when starting */ |
| 85 | int is_oneChar; /* TRUE if block within one character */ |
| 86 | int pre_whitesp; /* screen cols of ws before block */ |
| 87 | int pre_whitesp_c; /* chars of ws before block */ |
| 88 | colnr_T end_char_vcols; /* number of vcols of post-block char */ |
| 89 | #endif |
| 90 | colnr_T start_char_vcols; /* number of vcols of pre-block char */ |
| 91 | }; |
| 92 | |
| 93 | #ifdef FEAT_VISUALEXTRA |
| 94 | static void shift_block __ARGS((oparg_T *oap, int amount)); |
| 95 | static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp)); |
| 96 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | static int stuff_yank __ARGS((int, char_u *)); |
| 98 | static void put_reedit_in_typebuf __ARGS((void)); |
| 99 | static int put_in_typebuf __ARGS((char_u *s, int colon)); |
| 100 | static void stuffescaped __ARGS((char_u *arg, int literally)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | #ifdef FEAT_MBYTE |
| 102 | static void mb_adjust_opend __ARGS((oparg_T *oap)); |
| 103 | #endif |
| 104 | static void free_yank __ARGS((long)); |
| 105 | static void free_yank_all __ARGS((void)); |
| 106 | static int yank_copy_line __ARGS((struct block_def *bd, long y_idx)); |
| 107 | #ifdef FEAT_CLIPBOARD |
| 108 | static void copy_yank_reg __ARGS((struct yankreg *reg)); |
| 109 | # if defined(FEAT_VISUAL) || defined(FEAT_EVAL) |
| 110 | static void may_set_selection __ARGS((void)); |
| 111 | # endif |
| 112 | #endif |
| 113 | static void dis_msg __ARGS((char_u *p, int skip_esc)); |
| 114 | #ifdef FEAT_VISUAL |
| 115 | static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int)); |
| 116 | #endif |
| 117 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 118 | static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen)); |
| 119 | #endif |
| 120 | static int ends_in_white __ARGS((linenr_T lnum)); |
| 121 | #ifdef FEAT_COMMENTS |
| 122 | static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *)); |
| 123 | static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments)); |
| 124 | #else |
| 125 | static int fmt_check_par __ARGS((linenr_T)); |
| 126 | #endif |
| 127 | |
| 128 | /* |
| 129 | * The names of operators. |
| 130 | * IMPORTANT: Index must correspond with defines in vim.h!!! |
| 131 | * The third field indicates whether the operator always works on lines. |
| 132 | */ |
| 133 | static char opchars[][3] = |
| 134 | { |
| 135 | {NUL, NUL, FALSE}, /* OP_NOP */ |
| 136 | {'d', NUL, FALSE}, /* OP_DELETE */ |
| 137 | {'y', NUL, FALSE}, /* OP_YANK */ |
| 138 | {'c', NUL, FALSE}, /* OP_CHANGE */ |
| 139 | {'<', NUL, TRUE}, /* OP_LSHIFT */ |
| 140 | {'>', NUL, TRUE}, /* OP_RSHIFT */ |
| 141 | {'!', NUL, TRUE}, /* OP_FILTER */ |
| 142 | {'g', '~', FALSE}, /* OP_TILDE */ |
| 143 | {'=', NUL, TRUE}, /* OP_INDENT */ |
| 144 | {'g', 'q', TRUE}, /* OP_FORMAT */ |
| 145 | {':', NUL, TRUE}, /* OP_COLON */ |
| 146 | {'g', 'U', FALSE}, /* OP_UPPER */ |
| 147 | {'g', 'u', FALSE}, /* OP_LOWER */ |
| 148 | {'J', NUL, TRUE}, /* DO_JOIN */ |
| 149 | {'g', 'J', TRUE}, /* DO_JOIN_NS */ |
| 150 | {'g', '?', FALSE}, /* OP_ROT13 */ |
| 151 | {'r', NUL, FALSE}, /* OP_REPLACE */ |
| 152 | {'I', NUL, FALSE}, /* OP_INSERT */ |
| 153 | {'A', NUL, FALSE}, /* OP_APPEND */ |
| 154 | {'z', 'f', TRUE}, /* OP_FOLD */ |
| 155 | {'z', 'o', TRUE}, /* OP_FOLDOPEN */ |
| 156 | {'z', 'O', TRUE}, /* OP_FOLDOPENREC */ |
| 157 | {'z', 'c', TRUE}, /* OP_FOLDCLOSE */ |
| 158 | {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */ |
| 159 | {'z', 'd', TRUE}, /* OP_FOLDDEL */ |
| 160 | {'z', 'D', TRUE}, /* OP_FOLDDELREC */ |
| 161 | {'g', 'w', TRUE}, /* OP_FORMAT2 */ |
| 162 | }; |
| 163 | |
| 164 | /* |
| 165 | * Translate a command name into an operator type. |
| 166 | * Must only be called with a valid operator name! |
| 167 | */ |
| 168 | int |
| 169 | get_op_type(char1, char2) |
| 170 | int char1; |
| 171 | int char2; |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | if (char1 == 'r') /* ignore second character */ |
| 176 | return OP_REPLACE; |
| 177 | if (char1 == '~') /* when tilde is an operator */ |
| 178 | return OP_TILDE; |
| 179 | for (i = 0; ; ++i) |
| 180 | if (opchars[i][0] == char1 && opchars[i][1] == char2) |
| 181 | break; |
| 182 | return i; |
| 183 | } |
| 184 | |
| 185 | #if defined(FEAT_VISUAL) || defined(PROTO) |
| 186 | /* |
| 187 | * Return TRUE if operator "op" always works on whole lines. |
| 188 | */ |
| 189 | int |
| 190 | op_on_lines(op) |
| 191 | int op; |
| 192 | { |
| 193 | return opchars[op][2]; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | /* |
| 198 | * Get first operator command character. |
| 199 | * Returns 'g' or 'z' if there is another command character. |
| 200 | */ |
| 201 | int |
| 202 | get_op_char(optype) |
| 203 | int optype; |
| 204 | { |
| 205 | return opchars[optype][0]; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Get second operator command character. |
| 210 | */ |
| 211 | int |
| 212 | get_extra_op_char(optype) |
| 213 | int optype; |
| 214 | { |
| 215 | return opchars[optype][1]; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * op_shift - handle a shift operation |
| 220 | */ |
| 221 | void |
| 222 | op_shift(oap, curs_top, amount) |
| 223 | oparg_T *oap; |
| 224 | int curs_top; |
| 225 | int amount; |
| 226 | { |
| 227 | long i; |
| 228 | int first_char; |
| 229 | char_u *s; |
| 230 | #ifdef FEAT_VISUAL |
| 231 | int block_col = 0; |
| 232 | #endif |
| 233 | |
| 234 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 235 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 236 | return; |
| 237 | |
| 238 | #ifdef FEAT_VISUAL |
| 239 | if (oap->block_mode) |
| 240 | block_col = curwin->w_cursor.col; |
| 241 | #endif |
| 242 | |
| 243 | for (i = oap->line_count; --i >= 0; ) |
| 244 | { |
| 245 | first_char = *ml_get_curline(); |
| 246 | if (first_char == NUL) /* empty line */ |
| 247 | curwin->w_cursor.col = 0; |
| 248 | #ifdef FEAT_VISUALEXTRA |
| 249 | else if (oap->block_mode) |
| 250 | shift_block(oap, amount); |
| 251 | #endif |
| 252 | else |
| 253 | /* Move the line right if it doesn't start with '#', 'smartindent' |
| 254 | * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */ |
| 255 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 256 | if (first_char != '#' || !preprocs_left()) |
| 257 | #endif |
| 258 | { |
| 259 | shift_line(oap->op_type == OP_LSHIFT, p_sr, amount); |
| 260 | } |
| 261 | ++curwin->w_cursor.lnum; |
| 262 | } |
| 263 | |
| 264 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 265 | |
| 266 | #ifdef FEAT_VISUAL |
| 267 | if (oap->block_mode) |
| 268 | { |
| 269 | curwin->w_cursor.lnum = oap->start.lnum; |
| 270 | curwin->w_cursor.col = block_col; |
| 271 | } |
| 272 | else |
| 273 | #endif |
| 274 | if (curs_top) /* put cursor on first line, for ">>" */ |
| 275 | { |
| 276 | curwin->w_cursor.lnum = oap->start.lnum; |
| 277 | beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ |
| 278 | } |
| 279 | else |
| 280 | --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ |
| 281 | |
| 282 | if (oap->line_count > p_report) |
| 283 | { |
| 284 | if (oap->op_type == OP_RSHIFT) |
| 285 | s = (char_u *)">"; |
| 286 | else |
| 287 | s = (char_u *)"<"; |
| 288 | if (oap->line_count == 1) |
| 289 | { |
| 290 | if (amount == 1) |
| 291 | sprintf((char *)IObuff, _("1 line %sed 1 time"), s); |
| 292 | else |
| 293 | sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | if (amount == 1) |
| 298 | sprintf((char *)IObuff, _("%ld lines %sed 1 time"), |
| 299 | oap->line_count, s); |
| 300 | else |
| 301 | sprintf((char *)IObuff, _("%ld lines %sed %d times"), |
| 302 | oap->line_count, s, amount); |
| 303 | } |
| 304 | msg(IObuff); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Set "'[" and "']" marks. |
| 309 | */ |
| 310 | curbuf->b_op_start = oap->start; |
| 311 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 312 | curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 313 | if (curbuf->b_op_end.col > 0) |
| 314 | --curbuf->b_op_end.col; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * shift the current line one shiftwidth left (if left != 0) or right |
| 319 | * leaves cursor on first blank in the line |
| 320 | */ |
| 321 | void |
| 322 | shift_line(left, round, amount) |
| 323 | int left; |
| 324 | int round; |
| 325 | int amount; |
| 326 | { |
| 327 | int count; |
| 328 | int i, j; |
| 329 | int p_sw = (int)curbuf->b_p_sw; |
| 330 | |
| 331 | count = get_indent(); /* get current indent */ |
| 332 | |
| 333 | if (round) /* round off indent */ |
| 334 | { |
| 335 | i = count / p_sw; /* number of p_sw rounded down */ |
| 336 | j = count % p_sw; /* extra spaces */ |
| 337 | if (j && left) /* first remove extra spaces */ |
| 338 | --amount; |
| 339 | if (left) |
| 340 | { |
| 341 | i -= amount; |
| 342 | if (i < 0) |
| 343 | i = 0; |
| 344 | } |
| 345 | else |
| 346 | i += amount; |
| 347 | count = i * p_sw; |
| 348 | } |
| 349 | else /* original vi indent */ |
| 350 | { |
| 351 | if (left) |
| 352 | { |
| 353 | count -= p_sw * amount; |
| 354 | if (count < 0) |
| 355 | count = 0; |
| 356 | } |
| 357 | else |
| 358 | count += p_sw * amount; |
| 359 | } |
| 360 | |
| 361 | /* Set new indent */ |
| 362 | #ifdef FEAT_VREPLACE |
| 363 | if (State & VREPLACE_FLAG) |
| 364 | change_indent(INDENT_SET, count, FALSE, NUL); |
| 365 | else |
| 366 | #endif |
| 367 | (void)set_indent(count, SIN_CHANGED); |
| 368 | } |
| 369 | |
| 370 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 371 | /* |
| 372 | * Shift one line of the current block one shiftwidth right or left. |
| 373 | * Leaves cursor on first character in block. |
| 374 | */ |
| 375 | static void |
| 376 | shift_block(oap, amount) |
| 377 | oparg_T *oap; |
| 378 | int amount; |
| 379 | { |
| 380 | int left = (oap->op_type == OP_LSHIFT); |
| 381 | int oldstate = State; |
| 382 | int total, split; |
| 383 | char_u *newp, *oldp, *midp, *ptr; |
| 384 | int oldcol = curwin->w_cursor.col; |
| 385 | int p_sw = (int)curbuf->b_p_sw; |
| 386 | int p_ts = (int)curbuf->b_p_ts; |
| 387 | struct block_def bd; |
| 388 | int internal = 0; |
| 389 | int incr; |
| 390 | colnr_T vcol, col = 0, ws_vcol; |
| 391 | int i = 0, j = 0; |
| 392 | int len; |
| 393 | |
| 394 | #ifdef FEAT_RIGHTLEFT |
| 395 | int old_p_ri = p_ri; |
| 396 | |
| 397 | p_ri = 0; /* don't want revins in ident */ |
| 398 | #endif |
| 399 | |
| 400 | State = INSERT; /* don't want REPLACE for State */ |
| 401 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 402 | if (bd.is_short) |
| 403 | return; |
| 404 | |
| 405 | /* total is number of screen columns to be inserted/removed */ |
| 406 | total = amount * p_sw; |
| 407 | oldp = ml_get_curline(); |
| 408 | |
| 409 | if (!left) |
| 410 | { |
| 411 | /* |
| 412 | * 1. Get start vcol |
| 413 | * 2. Total ws vcols |
| 414 | * 3. Divvy into TABs & spp |
| 415 | * 4. Construct new string |
| 416 | */ |
| 417 | total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */ |
| 418 | ws_vcol = bd.start_vcol - bd.pre_whitesp; |
| 419 | if (bd.startspaces) |
| 420 | { |
| 421 | #ifdef FEAT_MBYTE |
| 422 | if (has_mbyte) |
| 423 | bd.textstart += (*mb_ptr2len_check)(bd.textstart); |
| 424 | #endif |
| 425 | ++bd.textstart; |
| 426 | } |
| 427 | for ( ; vim_iswhite(*bd.textstart); ) |
| 428 | { |
| 429 | incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol)); |
| 430 | total += incr; |
| 431 | bd.start_vcol += incr; |
| 432 | } |
| 433 | /* OK, now total=all the VWS reqd, and textstart points at the 1st |
| 434 | * non-ws char in the block. */ |
| 435 | if (!curbuf->b_p_et) |
| 436 | i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */ |
| 437 | if (i) |
| 438 | j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */ |
| 439 | else |
| 440 | j = total; |
| 441 | /* if we're splitting a TAB, allow for it */ |
| 442 | bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); |
| 443 | len = (int)STRLEN(bd.textstart) + 1; |
| 444 | newp = alloc_check((unsigned)(bd.textcol + i + j + len)); |
| 445 | if (newp == NULL) |
| 446 | return; |
| 447 | vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); |
| 448 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 449 | copy_chars(newp + bd.textcol, (size_t)i, TAB); |
| 450 | copy_spaces(newp + bd.textcol + i, (size_t)j); |
| 451 | /* the end */ |
| 452 | mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); |
| 453 | } |
| 454 | else /* left */ |
| 455 | { |
| 456 | vcol = oap->start_vcol; |
| 457 | /* walk vcol past ws to be removed */ |
| 458 | for (midp = oldp + bd.textcol; |
| 459 | vcol < (oap->start_vcol + total) && vim_iswhite(*midp); ) |
| 460 | { |
| 461 | incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol); |
| 462 | vcol += incr; |
| 463 | } |
| 464 | /* internal is the block-internal ws replacing a split TAB */ |
| 465 | if (vcol > (oap->start_vcol + total)) |
| 466 | { |
| 467 | /* we have to split the TAB *(midp-1) */ |
| 468 | internal = vcol - (oap->start_vcol + total); |
| 469 | } |
| 470 | /* if 'expandtab' is not set, use TABs */ |
| 471 | |
| 472 | split = bd.startspaces + internal; |
| 473 | if (split > 0) |
| 474 | { |
| 475 | if (!curbuf->b_p_et) |
| 476 | { |
| 477 | for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; ) |
| 478 | col += lbr_chartabsize_adv(&ptr, (colnr_T)col); |
| 479 | |
| 480 | /* col+1 now equals the start col of the first char of the |
| 481 | * block (may be < oap.start_vcol if we're splitting a TAB) */ |
| 482 | i = ((col % p_ts) + split) / p_ts; /* number of tabs */ |
| 483 | } |
| 484 | if (i) |
| 485 | j = ((col % p_ts) + split) % p_ts; /* number of spp */ |
| 486 | else |
| 487 | j = split; |
| 488 | } |
| 489 | |
| 490 | newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1); |
| 491 | if (newp == NULL) |
| 492 | return; |
| 493 | vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1)); |
| 494 | |
| 495 | /* copy first part we want to keep */ |
| 496 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 497 | /* Now copy any TABS and spp to ensure correct alignment! */ |
| 498 | while (vim_iswhite(*midp)) |
| 499 | { |
| 500 | if (*midp == TAB) |
| 501 | i++; |
| 502 | else /*space */ |
| 503 | j++; |
| 504 | midp++; |
| 505 | } |
| 506 | /* We might have an extra TAB worth of spp now! */ |
| 507 | if (j / p_ts && !curbuf->b_p_et) |
| 508 | { |
| 509 | i++; |
| 510 | j -= p_ts; |
| 511 | } |
| 512 | copy_chars(newp + bd.textcol, (size_t)i, TAB); |
| 513 | copy_spaces(newp + bd.textcol + i, (size_t)j); |
| 514 | |
| 515 | /* the end */ |
| 516 | mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1); |
| 517 | } |
| 518 | /* replace the line */ |
| 519 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 520 | changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol); |
| 521 | State = oldstate; |
| 522 | curwin->w_cursor.col = oldcol; |
| 523 | #ifdef FEAT_RIGHTLEFT |
| 524 | p_ri = old_p_ri; |
| 525 | #endif |
| 526 | } |
| 527 | #endif |
| 528 | |
| 529 | #ifdef FEAT_VISUALEXTRA |
| 530 | /* |
| 531 | * Insert string "s" (b_insert ? before : after) block :AKelly |
| 532 | * Caller must prepare for undo. |
| 533 | */ |
| 534 | static void |
| 535 | block_insert(oap, s, b_insert, bdp) |
| 536 | oparg_T *oap; |
| 537 | char_u *s; |
| 538 | int b_insert; |
| 539 | struct block_def *bdp; |
| 540 | { |
| 541 | int p_ts; |
| 542 | int count = 0; /* extra spaces to replace a cut TAB */ |
| 543 | int spaces = 0; /* non-zero if cutting a TAB */ |
| 544 | colnr_T offset; /* pointer along new line */ |
| 545 | unsigned s_len; /* STRLEN(s) */ |
| 546 | char_u *newp, *oldp; /* new, old lines */ |
| 547 | linenr_T lnum; /* loop var */ |
| 548 | int oldstate = State; |
| 549 | |
| 550 | State = INSERT; /* don't want REPLACE for State */ |
| 551 | s_len = (unsigned)STRLEN(s); |
| 552 | |
| 553 | for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) |
| 554 | { |
| 555 | block_prep(oap, bdp, lnum, TRUE); |
| 556 | if (bdp->is_short && b_insert) |
| 557 | continue; /* OP_INSERT, line ends before block start */ |
| 558 | |
| 559 | oldp = ml_get(lnum); |
| 560 | |
| 561 | if (b_insert) |
| 562 | { |
| 563 | p_ts = bdp->start_char_vcols; |
| 564 | spaces = bdp->startspaces; |
| 565 | if (spaces != 0) |
| 566 | count = p_ts - 1; /* we're cutting a TAB */ |
| 567 | offset = bdp->textcol; |
| 568 | } |
| 569 | else /* append */ |
| 570 | { |
| 571 | p_ts = bdp->end_char_vcols; |
| 572 | if (!bdp->is_short) /* spaces = padding after block */ |
| 573 | { |
| 574 | spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0); |
| 575 | if (spaces != 0) |
| 576 | count = p_ts - 1; /* we're cutting a TAB */ |
| 577 | offset = bdp->textcol + bdp->textlen - (spaces != 0); |
| 578 | } |
| 579 | else /* spaces = padding to block edge */ |
| 580 | { |
| 581 | /* if $ used, just append to EOL (ie spaces==0) */ |
| 582 | if (!bdp->is_MAX) |
| 583 | spaces = (oap->end_vcol - bdp->end_vcol) + 1; |
| 584 | count = spaces; |
| 585 | offset = bdp->textcol + bdp->textlen; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1); |
| 590 | if (newp == NULL) |
| 591 | continue; |
| 592 | |
| 593 | /* copy up to shifted part */ |
| 594 | mch_memmove(newp, oldp, (size_t)(offset)); |
| 595 | oldp += offset; |
| 596 | |
| 597 | /* insert pre-padding */ |
| 598 | copy_spaces(newp + offset, (size_t)spaces); |
| 599 | |
| 600 | /* copy the new text */ |
| 601 | mch_memmove(newp + offset + spaces, s, (size_t)s_len); |
| 602 | offset += s_len; |
| 603 | |
| 604 | if (spaces && !bdp->is_short) |
| 605 | { |
| 606 | /* insert post-padding */ |
| 607 | copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces)); |
| 608 | /* We're splitting a TAB, don't copy it. */ |
| 609 | oldp++; |
| 610 | /* We allowed for that TAB, remember this now */ |
| 611 | count++; |
| 612 | } |
| 613 | |
| 614 | if (spaces > 0) |
| 615 | offset += count; |
| 616 | mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1)); |
| 617 | |
| 618 | ml_replace(lnum, newp, FALSE); |
| 619 | |
| 620 | if (lnum == oap->end.lnum) |
| 621 | { |
| 622 | /* Set "']" mark to the end of the block instead of the end of |
| 623 | * the insert in the first line. */ |
| 624 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 625 | curbuf->b_op_end.col = offset; |
| 626 | } |
| 627 | } /* for all lnum */ |
| 628 | |
| 629 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 630 | |
| 631 | State = oldstate; |
| 632 | } |
| 633 | #endif |
| 634 | |
| 635 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 636 | /* |
| 637 | * op_reindent - handle reindenting a block of lines. |
| 638 | */ |
| 639 | void |
| 640 | op_reindent(oap, how) |
| 641 | oparg_T *oap; |
| 642 | int (*how) __ARGS((void)); |
| 643 | { |
| 644 | long i; |
| 645 | char_u *l; |
| 646 | int count; |
| 647 | linenr_T first_changed = 0; |
| 648 | linenr_T last_changed = 0; |
| 649 | linenr_T start_lnum = curwin->w_cursor.lnum; |
| 650 | |
| 651 | for (i = oap->line_count; --i >= 0 && !got_int; ) |
| 652 | { |
| 653 | /* it's a slow thing to do, so give feedback so there's no worry that |
| 654 | * the computer's just hung. */ |
| 655 | |
| 656 | if (i > 1 |
| 657 | && (i % 50 == 0 || i == oap->line_count - 1) |
| 658 | && oap->line_count > p_report) |
| 659 | smsg((char_u *)_("%ld lines to indent... "), i); |
| 660 | |
| 661 | /* |
| 662 | * Be vi-compatible: For lisp indenting the first line is not |
| 663 | * indented, unless there is only one line. |
| 664 | */ |
| 665 | #ifdef FEAT_LISP |
| 666 | if (i != oap->line_count - 1 || oap->line_count == 1 |
| 667 | || how != get_lisp_indent) |
| 668 | #endif |
| 669 | { |
| 670 | l = skipwhite(ml_get_curline()); |
| 671 | if (*l == NUL) /* empty or blank line */ |
| 672 | count = 0; |
| 673 | else |
| 674 | count = how(); /* get the indent for this line */ |
| 675 | |
| 676 | if (set_indent(count, SIN_UNDO)) |
| 677 | { |
| 678 | /* did change the indent, call changed_lines() later */ |
| 679 | if (first_changed == 0) |
| 680 | first_changed = curwin->w_cursor.lnum; |
| 681 | last_changed = curwin->w_cursor.lnum; |
| 682 | } |
| 683 | } |
| 684 | ++curwin->w_cursor.lnum; |
| 685 | } |
| 686 | |
| 687 | /* put cursor on first non-blank of indented line */ |
| 688 | curwin->w_cursor.lnum = start_lnum; |
| 689 | beginline(BL_SOL | BL_FIX); |
| 690 | |
| 691 | /* Mark changed lines so that they will be redrawn. When Visual |
| 692 | * highlighting was present, need to continue until the last line. When |
| 693 | * there is no change still need to remove the Visual highlighting. */ |
| 694 | if (last_changed != 0) |
| 695 | changed_lines(first_changed, 0, |
| 696 | #ifdef FEAT_VISUAL |
| 697 | oap->is_VIsual ? start_lnum + oap->line_count : |
| 698 | #endif |
| 699 | last_changed + 1, 0L); |
| 700 | #ifdef FEAT_VISUAL |
| 701 | else if (oap->is_VIsual) |
| 702 | redraw_curbuf_later(INVERTED); |
| 703 | #endif |
| 704 | |
| 705 | if (oap->line_count > p_report) |
| 706 | { |
| 707 | i = oap->line_count - (i + 1); |
| 708 | if (i == 1) |
| 709 | MSG(_("1 line indented ")); |
| 710 | else |
| 711 | smsg((char_u *)_("%ld lines indented "), i); |
| 712 | } |
| 713 | /* set '[ and '] marks */ |
| 714 | curbuf->b_op_start = oap->start; |
| 715 | curbuf->b_op_end = oap->end; |
| 716 | } |
| 717 | #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */ |
| 718 | |
| 719 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 720 | /* |
| 721 | * Keep the last expression line here, for repeating. |
| 722 | */ |
| 723 | static char_u *expr_line = NULL; |
| 724 | |
| 725 | /* |
| 726 | * Get an expression for the "\"=expr1" or "CTRL-R =expr1" |
| 727 | * Returns '=' when OK, NUL otherwise. |
| 728 | */ |
| 729 | int |
| 730 | get_expr_register() |
| 731 | { |
| 732 | char_u *new_line; |
| 733 | |
| 734 | new_line = getcmdline('=', 0L, 0); |
| 735 | if (new_line == NULL) |
| 736 | return NUL; |
| 737 | if (*new_line == NUL) /* use previous line */ |
| 738 | vim_free(new_line); |
| 739 | else |
| 740 | set_expr_line(new_line); |
| 741 | return '='; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Set the expression for the '=' register. |
| 746 | * Argument must be an allocated string. |
| 747 | */ |
| 748 | void |
| 749 | set_expr_line(new_line) |
| 750 | char_u *new_line; |
| 751 | { |
| 752 | vim_free(expr_line); |
| 753 | expr_line = new_line; |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * Get the result of the '=' register expression. |
| 758 | * Returns a pointer to allocated memory, or NULL for failure. |
| 759 | */ |
| 760 | char_u * |
| 761 | get_expr_line() |
| 762 | { |
| 763 | char_u *expr_copy; |
| 764 | char_u *rv; |
| 765 | |
| 766 | if (expr_line == NULL) |
| 767 | return NULL; |
| 768 | |
| 769 | /* Make a copy of the expression, because evaluating it may cause it to be |
| 770 | * changed. */ |
| 771 | expr_copy = vim_strsave(expr_line); |
| 772 | if (expr_copy == NULL) |
| 773 | return NULL; |
| 774 | |
| 775 | rv = eval_to_string(expr_copy, NULL); |
| 776 | vim_free(expr_copy); |
| 777 | return rv; |
| 778 | } |
| 779 | #endif /* FEAT_EVAL */ |
| 780 | |
| 781 | /* |
| 782 | * Check if 'regname' is a valid name of a yank register. |
| 783 | * Note: There is no check for 0 (default register), caller should do this |
| 784 | */ |
| 785 | int |
| 786 | valid_yank_reg(regname, writing) |
| 787 | int regname; |
| 788 | int writing; /* if TRUE check for writable registers */ |
| 789 | { |
| 790 | if ( (regname > 0 && ASCII_ISALNUM(regname)) |
| 791 | || (!writing && vim_strchr((char_u *) |
| 792 | #ifdef FEAT_EVAL |
| 793 | "/.%#:=" |
| 794 | #else |
| 795 | "/.%#:" |
| 796 | #endif |
| 797 | , regname) != NULL) |
| 798 | || regname == '"' |
| 799 | || regname == '-' |
| 800 | || regname == '_' |
| 801 | #ifdef FEAT_CLIPBOARD |
| 802 | || regname == '*' |
| 803 | || regname == '+' |
| 804 | #endif |
| 805 | #ifdef FEAT_DND |
| 806 | || (!writing && regname == '~') |
| 807 | #endif |
| 808 | ) |
| 809 | return TRUE; |
| 810 | return FALSE; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Set y_current and y_append, according to the value of "regname". |
| 815 | * Cannot handle the '_' register. |
| 816 | * |
| 817 | * If regname is 0 and writing, use register 0 |
| 818 | * If regname is 0 and reading, use previous register |
| 819 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 820 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 821 | get_yank_register(regname, writing) |
| 822 | int regname; |
| 823 | int writing; |
| 824 | { |
| 825 | int i; |
| 826 | |
| 827 | y_append = FALSE; |
| 828 | if ((regname == 0 || regname == '"') && !writing && y_previous != NULL) |
| 829 | { |
| 830 | y_current = y_previous; |
| 831 | return; |
| 832 | } |
| 833 | i = regname; |
| 834 | if (VIM_ISDIGIT(i)) |
| 835 | i -= '0'; |
| 836 | else if (ASCII_ISLOWER(i)) |
| 837 | i = CharOrdLow(i) + 10; |
| 838 | else if (ASCII_ISUPPER(i)) |
| 839 | { |
| 840 | i = CharOrdUp(i) + 10; |
| 841 | y_append = TRUE; |
| 842 | } |
| 843 | else if (regname == '-') |
| 844 | i = DELETION_REGISTER; |
| 845 | #ifdef FEAT_CLIPBOARD |
| 846 | /* When selection is not available, use register 0 instead of '*' */ |
| 847 | else if (clip_star.available && regname == '*') |
| 848 | i = STAR_REGISTER; |
| 849 | /* When clipboard is not available, use register 0 instead of '+' */ |
| 850 | else if (clip_plus.available && regname == '+') |
| 851 | i = PLUS_REGISTER; |
| 852 | #endif |
| 853 | #ifdef FEAT_DND |
| 854 | else if (!writing && regname == '~') |
| 855 | i = TILDE_REGISTER; |
| 856 | #endif |
| 857 | else /* not 0-9, a-z, A-Z or '-': use register 0 */ |
| 858 | i = 0; |
| 859 | y_current = &(y_regs[i]); |
| 860 | if (writing) /* remember the register we write into for do_put() */ |
| 861 | y_previous = y_current; |
| 862 | } |
| 863 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 864 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 865 | /* |
| 866 | * When "regname" is a clipboard register, obtain the selection. If it's not |
| 867 | * available return zero, otherwise return "regname". |
| 868 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 869 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 870 | may_get_selection(regname) |
| 871 | int regname; |
| 872 | { |
| 873 | if (regname == '*') |
| 874 | { |
| 875 | if (!clip_star.available) |
| 876 | regname = 0; |
| 877 | else |
| 878 | clip_get_selection(&clip_star); |
| 879 | } |
| 880 | else if (regname == '+') |
| 881 | { |
| 882 | if (!clip_plus.available) |
| 883 | regname = 0; |
| 884 | else |
| 885 | clip_get_selection(&clip_plus); |
| 886 | } |
| 887 | return regname; |
| 888 | } |
| 889 | #endif |
| 890 | |
| 891 | #if defined(FEAT_VISUAL) || defined(PROTO) |
| 892 | /* |
| 893 | * Obtain the contents of a "normal" register. The register is made empty. |
| 894 | * The returned pointer has allocated memory, use put_register() later. |
| 895 | */ |
| 896 | void * |
| 897 | get_register(name, copy) |
| 898 | int name; |
| 899 | int copy; /* make a copy, if FALSE make register empty. */ |
| 900 | { |
| 901 | static struct yankreg *reg; |
| 902 | int i; |
| 903 | |
| 904 | #ifdef FEAT_CLIPBOARD |
| 905 | /* When Visual area changed, may have to update selection. Obtain the |
| 906 | * selection too. */ |
| 907 | if (name == '*' && clip_star.available && clip_isautosel()) |
| 908 | { |
| 909 | clip_update_selection(); |
| 910 | may_get_selection(name); |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | get_yank_register(name, 0); |
| 915 | reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg)); |
| 916 | if (reg != NULL) |
| 917 | { |
| 918 | *reg = *y_current; |
| 919 | if (copy) |
| 920 | { |
| 921 | /* If we run out of memory some or all of the lines are empty. */ |
| 922 | if (reg->y_size == 0) |
| 923 | reg->y_array = NULL; |
| 924 | else |
| 925 | reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *) |
| 926 | * reg->y_size)); |
| 927 | if (reg->y_array != NULL) |
| 928 | { |
| 929 | for (i = 0; i < reg->y_size; ++i) |
| 930 | reg->y_array[i] = vim_strsave(y_current->y_array[i]); |
| 931 | } |
| 932 | } |
| 933 | else |
| 934 | y_current->y_array = NULL; |
| 935 | } |
| 936 | return (void *)reg; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Put "reg" into register "name". Free any previous contents. |
| 941 | */ |
| 942 | void |
| 943 | put_register(name, reg) |
| 944 | int name; |
| 945 | void *reg; |
| 946 | { |
| 947 | get_yank_register(name, 0); |
| 948 | free_yank_all(); |
| 949 | *y_current = *(struct yankreg *)reg; |
| 950 | |
| 951 | # ifdef FEAT_CLIPBOARD |
| 952 | /* Send text written to clipboard register to the clipboard. */ |
| 953 | may_set_selection(); |
| 954 | # endif |
| 955 | } |
| 956 | #endif |
| 957 | |
| 958 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 959 | /* |
| 960 | * return TRUE if the current yank register has type MLINE |
| 961 | */ |
| 962 | int |
| 963 | yank_register_mline(regname) |
| 964 | int regname; |
| 965 | { |
| 966 | if (regname != 0 && !valid_yank_reg(regname, FALSE)) |
| 967 | return FALSE; |
| 968 | if (regname == '_') /* black hole is always empty */ |
| 969 | return FALSE; |
| 970 | get_yank_register(regname, FALSE); |
| 971 | return (y_current->y_type == MLINE); |
| 972 | } |
| 973 | #endif |
| 974 | |
| 975 | /* |
| 976 | * start or stop recording into a yank register |
| 977 | * |
| 978 | * return FAIL for failure, OK otherwise |
| 979 | */ |
| 980 | int |
| 981 | do_record(c) |
| 982 | int c; |
| 983 | { |
| 984 | char_u *p; |
| 985 | static int regname; |
| 986 | struct yankreg *old_y_previous, *old_y_current; |
| 987 | int retval; |
| 988 | |
| 989 | if (Recording == FALSE) /* start recording */ |
| 990 | { |
| 991 | /* registers 0-9, a-z and " are allowed */ |
| 992 | if (c < 0 || (!ASCII_ISALNUM(c) && c != '"')) |
| 993 | retval = FAIL; |
| 994 | else |
| 995 | { |
| 996 | Recording = TRUE; |
| 997 | showmode(); |
| 998 | regname = c; |
| 999 | retval = OK; |
| 1000 | } |
| 1001 | } |
| 1002 | else /* stop recording */ |
| 1003 | { |
| 1004 | /* |
| 1005 | * Get the recorded key hits. K_SPECIAL and CSI will be escaped, so |
| 1006 | * that the register can be put into the typeahead buffer without |
| 1007 | * translation. |
| 1008 | */ |
| 1009 | Recording = FALSE; |
| 1010 | MSG(""); |
| 1011 | p = get_recorded(); |
| 1012 | if (p == NULL) |
| 1013 | retval = FAIL; |
| 1014 | else |
| 1015 | { |
| 1016 | /* |
| 1017 | * We don't want to change the default register here, so save and |
| 1018 | * restore the current register name. |
| 1019 | */ |
| 1020 | old_y_previous = y_previous; |
| 1021 | old_y_current = y_current; |
| 1022 | |
| 1023 | retval = stuff_yank(regname, p); |
| 1024 | |
| 1025 | y_previous = old_y_previous; |
| 1026 | y_current = old_y_current; |
| 1027 | } |
| 1028 | } |
| 1029 | return retval; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * Stuff string "p" into yank register "regname" as a single line (append if |
| 1034 | * uppercase). "p" must have been alloced. |
| 1035 | * |
| 1036 | * return FAIL for failure, OK otherwise |
| 1037 | */ |
| 1038 | static int |
| 1039 | stuff_yank(regname, p) |
| 1040 | int regname; |
| 1041 | char_u *p; |
| 1042 | { |
| 1043 | char_u *lp; |
| 1044 | char_u **pp; |
| 1045 | |
| 1046 | /* check for read-only register */ |
| 1047 | if (regname != 0 && !valid_yank_reg(regname, TRUE)) |
| 1048 | { |
| 1049 | vim_free(p); |
| 1050 | return FAIL; |
| 1051 | } |
| 1052 | if (regname == '_') /* black hole: don't do anything */ |
| 1053 | { |
| 1054 | vim_free(p); |
| 1055 | return OK; |
| 1056 | } |
| 1057 | get_yank_register(regname, TRUE); |
| 1058 | if (y_append && y_current->y_array != NULL) |
| 1059 | { |
| 1060 | pp = &(y_current->y_array[y_current->y_size - 1]); |
| 1061 | lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE); |
| 1062 | if (lp == NULL) |
| 1063 | { |
| 1064 | vim_free(p); |
| 1065 | return FAIL; |
| 1066 | } |
| 1067 | STRCPY(lp, *pp); |
| 1068 | STRCAT(lp, p); |
| 1069 | vim_free(p); |
| 1070 | vim_free(*pp); |
| 1071 | *pp = lp; |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | free_yank_all(); |
| 1076 | if ((y_current->y_array = |
| 1077 | (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL) |
| 1078 | { |
| 1079 | vim_free(p); |
| 1080 | return FAIL; |
| 1081 | } |
| 1082 | y_current->y_array[0] = p; |
| 1083 | y_current->y_size = 1; |
| 1084 | y_current->y_type = MCHAR; /* used to be MLINE, why? */ |
| 1085 | } |
| 1086 | return OK; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * execute a yank register: copy it into the stuff buffer |
| 1091 | * |
| 1092 | * return FAIL for failure, OK otherwise |
| 1093 | */ |
| 1094 | int |
| 1095 | do_execreg(regname, colon, addcr) |
| 1096 | int regname; |
| 1097 | int colon; /* insert ':' before each line */ |
| 1098 | int addcr; /* always add '\n' to end of line */ |
| 1099 | { |
| 1100 | static int lastc = NUL; |
| 1101 | long i; |
| 1102 | char_u *p; |
| 1103 | int retval = OK; |
| 1104 | int remap; |
| 1105 | |
| 1106 | if (regname == '@') /* repeat previous one */ |
| 1107 | regname = lastc; |
| 1108 | /* check for valid regname */ |
| 1109 | if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE)) |
| 1110 | return FAIL; |
| 1111 | lastc = regname; |
| 1112 | |
| 1113 | #ifdef FEAT_CLIPBOARD |
| 1114 | regname = may_get_selection(regname); |
| 1115 | #endif |
| 1116 | |
| 1117 | if (regname == '_') /* black hole: don't stuff anything */ |
| 1118 | return OK; |
| 1119 | |
| 1120 | #ifdef FEAT_CMDHIST |
| 1121 | if (regname == ':') /* use last command line */ |
| 1122 | { |
| 1123 | if (last_cmdline == NULL) |
| 1124 | { |
| 1125 | EMSG(_(e_nolastcmd)); |
| 1126 | return FAIL; |
| 1127 | } |
| 1128 | vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */ |
| 1129 | new_last_cmdline = NULL; |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1130 | /* Escape all control characters with a CTRL-V */ |
| 1131 | p = vim_strsave_escaped_ext(last_cmdline, |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 1132 | (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE); |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 1133 | if (p != NULL) |
| 1134 | retval = put_in_typebuf(p, TRUE); |
| 1135 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1136 | } |
| 1137 | #endif |
| 1138 | #ifdef FEAT_EVAL |
| 1139 | else if (regname == '=') |
| 1140 | { |
| 1141 | p = get_expr_line(); |
| 1142 | if (p == NULL) |
| 1143 | return FAIL; |
| 1144 | retval = put_in_typebuf(p, colon); |
| 1145 | vim_free(p); |
| 1146 | } |
| 1147 | #endif |
| 1148 | else if (regname == '.') /* use last inserted text */ |
| 1149 | { |
| 1150 | p = get_last_insert_save(); |
| 1151 | if (p == NULL) |
| 1152 | { |
| 1153 | EMSG(_(e_noinstext)); |
| 1154 | return FAIL; |
| 1155 | } |
| 1156 | retval = put_in_typebuf(p, colon); |
| 1157 | vim_free(p); |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | get_yank_register(regname, FALSE); |
| 1162 | if (y_current->y_array == NULL) |
| 1163 | return FAIL; |
| 1164 | |
| 1165 | /* Disallow remaping for ":@r". */ |
| 1166 | remap = colon ? REMAP_NONE : REMAP_YES; |
| 1167 | |
| 1168 | /* |
| 1169 | * Insert lines into typeahead buffer, from last one to first one. |
| 1170 | */ |
| 1171 | put_reedit_in_typebuf(); |
| 1172 | for (i = y_current->y_size; --i >= 0; ) |
| 1173 | { |
| 1174 | /* insert NL between lines and after last line if type is MLINE */ |
| 1175 | if (y_current->y_type == MLINE || i < y_current->y_size - 1 |
| 1176 | || addcr) |
| 1177 | { |
| 1178 | if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, FALSE) == FAIL) |
| 1179 | return FAIL; |
| 1180 | } |
| 1181 | if (ins_typebuf(y_current->y_array[i], remap, 0, TRUE, FALSE) |
| 1182 | == FAIL) |
| 1183 | return FAIL; |
| 1184 | if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, FALSE) |
| 1185 | == FAIL) |
| 1186 | return FAIL; |
| 1187 | } |
| 1188 | Exec_reg = TRUE; /* disable the 'q' command */ |
| 1189 | } |
| 1190 | return retval; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's |
| 1195 | * used only after other typeahead has been processed. |
| 1196 | */ |
| 1197 | static void |
| 1198 | put_reedit_in_typebuf() |
| 1199 | { |
| 1200 | char_u buf[3]; |
| 1201 | |
| 1202 | if (restart_edit != NUL) |
| 1203 | { |
| 1204 | if (restart_edit == 'V') |
| 1205 | { |
| 1206 | buf[0] = 'g'; |
| 1207 | buf[1] = 'R'; |
| 1208 | buf[2] = NUL; |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | buf[0] = restart_edit == 'I' ? 'i' : restart_edit; |
| 1213 | buf[1] = NUL; |
| 1214 | } |
| 1215 | if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE) == OK) |
| 1216 | restart_edit = NUL; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | static int |
| 1221 | put_in_typebuf(s, colon) |
| 1222 | char_u *s; |
| 1223 | int colon; /* add ':' before the line */ |
| 1224 | { |
| 1225 | int retval = OK; |
| 1226 | |
| 1227 | put_reedit_in_typebuf(); |
| 1228 | if (colon) |
| 1229 | retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, FALSE); |
| 1230 | if (retval == OK) |
| 1231 | retval = ins_typebuf(s, REMAP_YES, 0, TRUE, FALSE); |
| 1232 | if (colon && retval == OK) |
| 1233 | retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, FALSE); |
| 1234 | return retval; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Insert a yank register: copy it into the Read buffer. |
| 1239 | * Used by CTRL-R command and middle mouse button in insert mode. |
| 1240 | * |
| 1241 | * return FAIL for failure, OK otherwise |
| 1242 | */ |
| 1243 | int |
| 1244 | insert_reg(regname, literally) |
| 1245 | int regname; |
| 1246 | int literally; /* insert literally, not as if typed */ |
| 1247 | { |
| 1248 | long i; |
| 1249 | int retval = OK; |
| 1250 | char_u *arg; |
| 1251 | int allocated; |
| 1252 | |
| 1253 | /* |
| 1254 | * It is possible to get into an endless loop by having CTRL-R a in |
| 1255 | * register a and then, in insert mode, doing CTRL-R a. |
| 1256 | * If you hit CTRL-C, the loop will be broken here. |
| 1257 | */ |
| 1258 | ui_breakcheck(); |
| 1259 | if (got_int) |
| 1260 | return FAIL; |
| 1261 | |
| 1262 | /* check for valid regname */ |
| 1263 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 1264 | return FAIL; |
| 1265 | |
| 1266 | #ifdef FEAT_CLIPBOARD |
| 1267 | regname = may_get_selection(regname); |
| 1268 | #endif |
| 1269 | |
| 1270 | if (regname == '.') /* insert last inserted text */ |
| 1271 | retval = stuff_inserted(NUL, 1L, TRUE); |
| 1272 | else if (get_spec_reg(regname, &arg, &allocated, TRUE)) |
| 1273 | { |
| 1274 | if (arg == NULL) |
| 1275 | return FAIL; |
| 1276 | stuffescaped(arg, literally); |
| 1277 | if (allocated) |
| 1278 | vim_free(arg); |
| 1279 | } |
| 1280 | else /* name or number register */ |
| 1281 | { |
| 1282 | get_yank_register(regname, FALSE); |
| 1283 | if (y_current->y_array == NULL) |
| 1284 | retval = FAIL; |
| 1285 | else |
| 1286 | { |
| 1287 | for (i = 0; i < y_current->y_size; ++i) |
| 1288 | { |
| 1289 | stuffescaped(y_current->y_array[i], literally); |
| 1290 | /* |
| 1291 | * Insert a newline between lines and after last line if |
| 1292 | * y_type is MLINE. |
| 1293 | */ |
| 1294 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 1295 | stuffcharReadbuff('\n'); |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | return retval; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * Stuff a string into the typeahead buffer, such that edit() will insert it |
| 1305 | * literally ("literally" TRUE) or interpret is as typed characters. |
| 1306 | */ |
| 1307 | static void |
| 1308 | stuffescaped(arg, literally) |
| 1309 | char_u *arg; |
| 1310 | int literally; |
| 1311 | { |
| 1312 | int c; |
| 1313 | char_u *start; |
| 1314 | |
| 1315 | while (*arg != NUL) |
| 1316 | { |
| 1317 | /* Stuff a sequence of normal ASCII characters, that's fast. Also |
| 1318 | * stuff K_SPECIAL to get the effect of a special key when "literally" |
| 1319 | * is TRUE. */ |
| 1320 | start = arg; |
| 1321 | while ((*arg >= ' ' |
| 1322 | #ifndef EBCDIC |
| 1323 | && *arg < DEL /* EBCDIC: chars above space are normal */ |
| 1324 | #endif |
| 1325 | ) |
| 1326 | || (*arg == K_SPECIAL && !literally)) |
| 1327 | ++arg; |
| 1328 | if (arg > start) |
| 1329 | stuffReadbuffLen(start, (long)(arg - start)); |
| 1330 | |
| 1331 | /* stuff a single special character */ |
| 1332 | if (*arg != NUL) |
| 1333 | { |
| 1334 | #ifdef FEAT_MBYTE |
| 1335 | if (has_mbyte) |
| 1336 | c = mb_ptr2char_adv(&arg); |
| 1337 | else |
| 1338 | #endif |
| 1339 | c = *arg++; |
| 1340 | if (literally && ((c < ' ' && c != TAB) || c == DEL)) |
| 1341 | stuffcharReadbuff(Ctrl_V); |
| 1342 | stuffcharReadbuff(c); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * If "regname" is a special register, return a pointer to its value. |
| 1349 | */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1350 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1351 | get_spec_reg(regname, argp, allocated, errmsg) |
| 1352 | int regname; |
| 1353 | char_u **argp; |
| 1354 | int *allocated; |
| 1355 | int errmsg; /* give error message when failing */ |
| 1356 | { |
| 1357 | int cnt; |
| 1358 | |
| 1359 | *argp = NULL; |
| 1360 | *allocated = FALSE; |
| 1361 | switch (regname) |
| 1362 | { |
| 1363 | case '%': /* file name */ |
| 1364 | if (errmsg) |
| 1365 | check_fname(); /* will give emsg if not set */ |
| 1366 | *argp = curbuf->b_fname; |
| 1367 | return TRUE; |
| 1368 | |
| 1369 | case '#': /* alternate file name */ |
| 1370 | *argp = getaltfname(errmsg); /* may give emsg if not set */ |
| 1371 | return TRUE; |
| 1372 | |
| 1373 | #ifdef FEAT_EVAL |
| 1374 | case '=': /* result of expression */ |
| 1375 | *argp = get_expr_line(); |
| 1376 | *allocated = TRUE; |
| 1377 | return TRUE; |
| 1378 | #endif |
| 1379 | |
| 1380 | case ':': /* last command line */ |
| 1381 | if (last_cmdline == NULL && errmsg) |
| 1382 | EMSG(_(e_nolastcmd)); |
| 1383 | *argp = last_cmdline; |
| 1384 | return TRUE; |
| 1385 | |
| 1386 | case '/': /* last search-pattern */ |
| 1387 | if (last_search_pat() == NULL && errmsg) |
| 1388 | EMSG(_(e_noprevre)); |
| 1389 | *argp = last_search_pat(); |
| 1390 | return TRUE; |
| 1391 | |
| 1392 | case '.': /* last inserted text */ |
| 1393 | *argp = get_last_insert_save(); |
| 1394 | *allocated = TRUE; |
| 1395 | if (*argp == NULL && errmsg) |
| 1396 | EMSG(_(e_noinstext)); |
| 1397 | return TRUE; |
| 1398 | |
| 1399 | #ifdef FEAT_SEARCHPATH |
| 1400 | case Ctrl_F: /* Filename under cursor */ |
| 1401 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 1402 | if (!errmsg) |
| 1403 | return FALSE; |
| 1404 | *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP |
| 1405 | | (regname == Ctrl_P ? FNAME_EXP : 0), 1L); |
| 1406 | *allocated = TRUE; |
| 1407 | return TRUE; |
| 1408 | #endif |
| 1409 | |
| 1410 | case Ctrl_W: /* word under cursor */ |
| 1411 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 1412 | if (!errmsg) |
| 1413 | return FALSE; |
| 1414 | cnt = find_ident_under_cursor(argp, regname == Ctrl_W |
| 1415 | ? (FIND_IDENT|FIND_STRING) : FIND_STRING); |
| 1416 | *argp = cnt ? vim_strnsave(*argp, cnt) : NULL; |
| 1417 | *allocated = TRUE; |
| 1418 | return TRUE; |
| 1419 | |
| 1420 | case '_': /* black hole: always empty */ |
| 1421 | *argp = (char_u *)""; |
| 1422 | return TRUE; |
| 1423 | } |
| 1424 | |
| 1425 | return FALSE; |
| 1426 | } |
| 1427 | |
| 1428 | /* |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1429 | * Paste a yank register into the command line. |
| 1430 | * Only for non-special registers. |
| 1431 | * Used by CTRL-R command in command-line mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | * insert_reg() can't be used here, because special characters from the |
| 1433 | * register contents will be interpreted as commands. |
| 1434 | * |
| 1435 | * return FAIL for failure, OK otherwise |
| 1436 | */ |
| 1437 | int |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 1438 | cmdline_paste_reg(regname, literally) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | int regname; |
| 1440 | int literally; /* Insert text literally instead of "as typed" */ |
| 1441 | { |
| 1442 | long i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1443 | |
| 1444 | get_yank_register(regname, FALSE); |
| 1445 | if (y_current->y_array == NULL) |
| 1446 | return FAIL; |
| 1447 | |
| 1448 | for (i = 0; i < y_current->y_size; ++i) |
| 1449 | { |
| 1450 | cmdline_paste_str(y_current->y_array[i], literally); |
| 1451 | |
| 1452 | /* insert ^M between lines and after last line if type is MLINE */ |
| 1453 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 1454 | cmdline_paste_str((char_u *)"\r", literally); |
| 1455 | |
| 1456 | /* Check for CTRL-C, in case someone tries to paste a few thousand |
| 1457 | * lines and gets bored. */ |
| 1458 | ui_breakcheck(); |
| 1459 | if (got_int) |
| 1460 | return FAIL; |
| 1461 | } |
| 1462 | return OK; |
| 1463 | } |
| 1464 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1465 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 1466 | /* |
| 1467 | * Adjust the register name pointed to with "rp" for the clipboard being |
| 1468 | * used always and the clipboard being available. |
| 1469 | */ |
| 1470 | void |
| 1471 | adjust_clip_reg(rp) |
| 1472 | int *rp; |
| 1473 | { |
| 1474 | /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */ |
| 1475 | if (*rp == 0 && clip_unnamed) |
| 1476 | *rp = '*'; |
| 1477 | if (!clip_star.available && *rp == '*') |
| 1478 | *rp = 0; |
| 1479 | if (!clip_plus.available && *rp == '+') |
| 1480 | *rp = 0; |
| 1481 | } |
| 1482 | #endif |
| 1483 | |
| 1484 | /* |
| 1485 | * op_delete - handle a delete operation |
| 1486 | * |
| 1487 | * return FAIL if undo failed, OK otherwise. |
| 1488 | */ |
| 1489 | int |
| 1490 | op_delete(oap) |
| 1491 | oparg_T *oap; |
| 1492 | { |
| 1493 | int n; |
| 1494 | linenr_T lnum; |
| 1495 | char_u *ptr; |
| 1496 | #ifdef FEAT_VISUAL |
| 1497 | char_u *newp, *oldp; |
| 1498 | struct block_def bd; |
| 1499 | #endif |
| 1500 | linenr_T old_lcount = curbuf->b_ml.ml_line_count; |
| 1501 | int did_yank = FALSE; |
| 1502 | |
| 1503 | if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */ |
| 1504 | return OK; |
| 1505 | |
| 1506 | /* Nothing to delete, return here. Do prepare undo, for op_change(). */ |
| 1507 | if (oap->empty) |
| 1508 | return u_save_cursor(); |
| 1509 | |
| 1510 | if (!curbuf->b_p_ma) |
| 1511 | { |
| 1512 | EMSG(_(e_modifiable)); |
| 1513 | return FAIL; |
| 1514 | } |
| 1515 | |
| 1516 | #ifdef FEAT_CLIPBOARD |
| 1517 | adjust_clip_reg(&oap->regname); |
| 1518 | #endif |
| 1519 | |
| 1520 | #ifdef FEAT_MBYTE |
| 1521 | if (has_mbyte) |
| 1522 | mb_adjust_opend(oap); |
| 1523 | #endif |
| 1524 | |
| 1525 | /* |
| 1526 | * Imitate the strange Vi behaviour: If the delete spans more than one line |
| 1527 | * and motion_type == MCHAR and the result is a blank line, make the delete |
| 1528 | * linewise. Don't do this for the change command or Visual mode. |
| 1529 | */ |
| 1530 | if ( oap->motion_type == MCHAR |
| 1531 | #ifdef FEAT_VISUAL |
| 1532 | && !oap->is_VIsual |
| 1533 | #endif |
| 1534 | && oap->line_count > 1 |
| 1535 | && oap->op_type == OP_DELETE) |
| 1536 | { |
| 1537 | ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive; |
| 1538 | ptr = skipwhite(ptr); |
| 1539 | if (*ptr == NUL && inindent(0)) |
| 1540 | oap->motion_type = MLINE; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * Check for trying to delete (e.g. "D") in an empty line. |
| 1545 | * Note: For the change operator it is ok. |
| 1546 | */ |
| 1547 | if ( oap->motion_type == MCHAR |
| 1548 | && oap->line_count == 1 |
| 1549 | && oap->op_type == OP_DELETE |
| 1550 | && *ml_get(oap->start.lnum) == NUL) |
| 1551 | { |
| 1552 | /* |
| 1553 | * It's an error to operate on an empty region, when 'E' inclucded in |
| 1554 | * 'cpoptions' (Vi compatible). |
| 1555 | */ |
| 1556 | if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL) |
| 1557 | beep_flush(); |
| 1558 | return OK; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Do a yank of whatever we're about to delete. |
| 1563 | * If a yank register was specified, put the deleted text into that register. |
| 1564 | * For the black hole register '_' don't yank anything. |
| 1565 | */ |
| 1566 | if (oap->regname != '_') |
| 1567 | { |
| 1568 | if (oap->regname != 0) |
| 1569 | { |
| 1570 | /* check for read-only register */ |
| 1571 | if (!valid_yank_reg(oap->regname, TRUE)) |
| 1572 | { |
| 1573 | beep_flush(); |
| 1574 | return OK; |
| 1575 | } |
| 1576 | get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */ |
| 1577 | if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */ |
| 1578 | did_yank = TRUE; |
| 1579 | } |
| 1580 | |
| 1581 | /* |
| 1582 | * Put deleted text into register 1 and shift number registers if the |
| 1583 | * delete contains a line break, or when a regname has been specified. |
| 1584 | */ |
| 1585 | if (oap->regname != 0 || oap->motion_type == MLINE |
| 1586 | || oap->line_count > 1 || oap->use_reg_one) |
| 1587 | { |
| 1588 | y_current = &y_regs[9]; |
| 1589 | free_yank_all(); /* free register nine */ |
| 1590 | for (n = 9; n > 1; --n) |
| 1591 | y_regs[n] = y_regs[n - 1]; |
| 1592 | y_previous = y_current = &y_regs[1]; |
| 1593 | y_regs[1].y_array = NULL; /* set register one to empty */ |
| 1594 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1595 | did_yank = TRUE; |
| 1596 | } |
| 1597 | |
| 1598 | /* Yank into small delete register when no register specified and the |
| 1599 | * delete is within one line. */ |
| 1600 | if (oap->regname == 0 && oap->motion_type != MLINE |
| 1601 | && oap->line_count == 1) |
| 1602 | { |
| 1603 | oap->regname = '-'; |
| 1604 | get_yank_register(oap->regname, TRUE); |
| 1605 | if (op_yank(oap, TRUE, FALSE) == OK) |
| 1606 | did_yank = TRUE; |
| 1607 | oap->regname = 0; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * If there's too much stuff to fit in the yank register, then get a |
| 1612 | * confirmation before doing the delete. This is crude, but simple. |
| 1613 | * And it avoids doing a delete of something we can't put back if we |
| 1614 | * want. |
| 1615 | */ |
| 1616 | if (!did_yank) |
| 1617 | { |
| 1618 | int msg_silent_save = msg_silent; |
| 1619 | |
| 1620 | msg_silent = 0; /* must display the prompt */ |
| 1621 | n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE); |
| 1622 | msg_silent = msg_silent_save; |
| 1623 | if (n != 'y') |
| 1624 | { |
| 1625 | EMSG(_(e_abort)); |
| 1626 | return FAIL; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | #ifdef FEAT_VISUAL |
| 1632 | /* |
| 1633 | * block mode delete |
| 1634 | */ |
| 1635 | if (oap->block_mode) |
| 1636 | { |
| 1637 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1638 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1639 | return FAIL; |
| 1640 | |
| 1641 | for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum) |
| 1642 | { |
| 1643 | block_prep(oap, &bd, lnum, TRUE); |
| 1644 | if (bd.textlen == 0) /* nothing to delete */ |
| 1645 | continue; |
| 1646 | |
| 1647 | /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ |
| 1648 | if (lnum == curwin->w_cursor.lnum) |
| 1649 | { |
| 1650 | curwin->w_cursor.col = bd.textcol + bd.startspaces; |
| 1651 | # ifdef FEAT_VIRTUALEDIT |
| 1652 | curwin->w_cursor.coladd = 0; |
| 1653 | # endif |
| 1654 | } |
| 1655 | |
| 1656 | /* n == number of chars deleted |
| 1657 | * If we delete a TAB, it may be replaced by several characters. |
| 1658 | * Thus the number of characters may increase! |
| 1659 | */ |
| 1660 | n = bd.textlen - bd.startspaces - bd.endspaces; |
| 1661 | oldp = ml_get(lnum); |
| 1662 | newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n); |
| 1663 | if (newp == NULL) |
| 1664 | continue; |
| 1665 | /* copy up to deleted part */ |
| 1666 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1667 | /* insert spaces */ |
| 1668 | copy_spaces(newp + bd.textcol, |
| 1669 | (size_t)(bd.startspaces + bd.endspaces)); |
| 1670 | /* copy the part after the deleted part */ |
| 1671 | oldp += bd.textcol + bd.textlen; |
| 1672 | mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces, |
| 1673 | oldp, STRLEN(oldp) + 1); |
| 1674 | /* replace the line */ |
| 1675 | ml_replace(lnum, newp, FALSE); |
| 1676 | } |
| 1677 | |
| 1678 | check_cursor_col(); |
| 1679 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 1680 | oap->end.lnum + 1, 0L); |
| 1681 | oap->line_count = 0; /* no lines deleted */ |
| 1682 | } |
| 1683 | else |
| 1684 | #endif |
| 1685 | if (oap->motion_type == MLINE) |
| 1686 | { |
| 1687 | if (oap->op_type == OP_CHANGE) |
| 1688 | { |
| 1689 | /* Delete the lines except the first one. Temporarily move the |
| 1690 | * cursor to the next line. Save the current line number, if the |
| 1691 | * last line is deleted it may be changed. |
| 1692 | */ |
| 1693 | if (oap->line_count > 1) |
| 1694 | { |
| 1695 | lnum = curwin->w_cursor.lnum; |
| 1696 | ++curwin->w_cursor.lnum; |
| 1697 | del_lines((long)(oap->line_count - 1), TRUE); |
| 1698 | curwin->w_cursor.lnum = lnum; |
| 1699 | } |
| 1700 | if (u_save_cursor() == FAIL) |
| 1701 | return FAIL; |
| 1702 | if (curbuf->b_p_ai) /* don't delete indent */ |
| 1703 | { |
| 1704 | beginline(BL_WHITE); /* cursor on first non-white */ |
| 1705 | did_ai = TRUE; /* delete the indent when ESC hit */ |
| 1706 | ai_col = curwin->w_cursor.col; |
| 1707 | } |
| 1708 | else |
| 1709 | beginline(0); /* cursor in column 0 */ |
| 1710 | truncate_line(FALSE); /* delete the rest of the line */ |
| 1711 | /* leave cursor past last char in line */ |
| 1712 | if (oap->line_count > 1) |
| 1713 | u_clearline(); /* "U" command not possible after "2cc" */ |
| 1714 | } |
| 1715 | else |
| 1716 | { |
| 1717 | del_lines(oap->line_count, TRUE); |
| 1718 | beginline(BL_WHITE | BL_FIX); |
| 1719 | u_clearline(); /* "U" command not possible after "dd" */ |
| 1720 | } |
| 1721 | } |
| 1722 | else |
| 1723 | { |
| 1724 | #ifdef FEAT_VIRTUALEDIT |
| 1725 | if (virtual_op) |
| 1726 | { |
| 1727 | int endcol = 0; |
| 1728 | |
| 1729 | /* For virtualedit: break the tabs that are partly included. */ |
| 1730 | if (gchar_pos(&oap->start) == '\t') |
| 1731 | { |
| 1732 | if (u_save_cursor() == FAIL) /* save first line for undo */ |
| 1733 | return FAIL; |
| 1734 | if (oap->line_count == 1) |
| 1735 | endcol = getviscol2(oap->end.col, oap->end.coladd); |
| 1736 | coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); |
| 1737 | oap->start = curwin->w_cursor; |
| 1738 | if (oap->line_count == 1) |
| 1739 | { |
| 1740 | coladvance(endcol); |
| 1741 | oap->end.col = curwin->w_cursor.col; |
| 1742 | oap->end.coladd = curwin->w_cursor.coladd; |
| 1743 | curwin->w_cursor = oap->start; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | /* Break a tab only when it's included in the area. */ |
| 1748 | if (gchar_pos(&oap->end) == '\t' |
| 1749 | && (int)oap->end.coladd < oap->inclusive) |
| 1750 | { |
| 1751 | /* save last line for undo */ |
| 1752 | if (u_save((linenr_T)(oap->end.lnum - 1), |
| 1753 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1754 | return FAIL; |
| 1755 | curwin->w_cursor = oap->end; |
| 1756 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); |
| 1757 | oap->end = curwin->w_cursor; |
| 1758 | curwin->w_cursor = oap->start; |
| 1759 | } |
| 1760 | } |
| 1761 | #endif |
| 1762 | |
| 1763 | if (oap->line_count == 1) /* delete characters within one line */ |
| 1764 | { |
| 1765 | if (u_save_cursor() == FAIL) /* save line for undo */ |
| 1766 | return FAIL; |
| 1767 | |
| 1768 | /* if 'cpoptions' contains '$', display '$' at end of change */ |
| 1769 | if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL |
| 1770 | && oap->op_type == OP_CHANGE |
| 1771 | && oap->end.lnum == curwin->w_cursor.lnum |
| 1772 | #ifdef FEAT_VISUAL |
| 1773 | && !oap->is_VIsual |
| 1774 | #endif |
| 1775 | ) |
| 1776 | display_dollar(oap->end.col - !oap->inclusive); |
| 1777 | |
| 1778 | n = oap->end.col - oap->start.col + 1 - !oap->inclusive; |
| 1779 | |
| 1780 | #ifdef FEAT_VIRTUALEDIT |
| 1781 | if (virtual_op) |
| 1782 | { |
| 1783 | /* fix up things for virtualedit-delete: |
| 1784 | * break the tabs which are going to get in our way |
| 1785 | */ |
| 1786 | char_u *curline = ml_get_curline(); |
| 1787 | int len = (int)STRLEN(curline); |
| 1788 | |
| 1789 | if (oap->end.coladd != 0 |
| 1790 | && (int)oap->end.col >= len - 1 |
| 1791 | && !(oap->start.coladd && (int)oap->end.col >= len - 1)) |
| 1792 | n++; |
| 1793 | /* Delete at least one char (e.g, when on a control char). */ |
| 1794 | if (n == 0 && oap->start.coladd != oap->end.coladd) |
| 1795 | n = 1; |
| 1796 | |
| 1797 | /* When deleted a char in the line, reset coladd. */ |
| 1798 | if (gchar_cursor() != NUL) |
| 1799 | curwin->w_cursor.coladd = 0; |
| 1800 | } |
| 1801 | #endif |
| 1802 | (void)del_bytes((long)n, restart_edit == NUL && !virtual_op); |
| 1803 | } |
| 1804 | else /* delete characters between lines */ |
| 1805 | { |
| 1806 | pos_T curpos; |
| 1807 | |
| 1808 | /* save deleted and changed lines for undo */ |
| 1809 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 1810 | (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) |
| 1811 | return FAIL; |
| 1812 | |
| 1813 | truncate_line(TRUE); /* delete from cursor to end of line */ |
| 1814 | |
| 1815 | curpos = curwin->w_cursor; /* remember curwin->w_cursor */ |
| 1816 | ++curwin->w_cursor.lnum; |
| 1817 | del_lines((long)(oap->line_count - 2), FALSE); |
| 1818 | |
| 1819 | /* delete from start of line until op_end */ |
| 1820 | curwin->w_cursor.col = 0; |
| 1821 | (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive), |
| 1822 | restart_edit == NUL && !virtual_op); |
| 1823 | curwin->w_cursor = curpos; /* restore curwin->w_cursor */ |
| 1824 | |
| 1825 | (void)do_join(FALSE); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | msgmore(curbuf->b_ml.ml_line_count - old_lcount); |
| 1830 | |
| 1831 | #ifdef FEAT_VISUAL |
| 1832 | if (oap->block_mode) |
| 1833 | { |
| 1834 | curbuf->b_op_end.lnum = oap->end.lnum; |
| 1835 | curbuf->b_op_end.col = oap->start.col; |
| 1836 | } |
| 1837 | else |
| 1838 | #endif |
| 1839 | curbuf->b_op_end = oap->start; |
| 1840 | curbuf->b_op_start = oap->start; |
| 1841 | |
| 1842 | return OK; |
| 1843 | } |
| 1844 | |
| 1845 | #ifdef FEAT_MBYTE |
| 1846 | /* |
| 1847 | * Adjust end of operating area for ending on a multi-byte character. |
| 1848 | * Used for deletion. |
| 1849 | */ |
| 1850 | static void |
| 1851 | mb_adjust_opend(oap) |
| 1852 | oparg_T *oap; |
| 1853 | { |
| 1854 | char_u *p; |
| 1855 | |
| 1856 | if (oap->inclusive) |
| 1857 | { |
| 1858 | p = ml_get(oap->end.lnum); |
| 1859 | oap->end.col += mb_tail_off(p, p + oap->end.col); |
| 1860 | } |
| 1861 | } |
| 1862 | #endif |
| 1863 | |
| 1864 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 1865 | /* |
| 1866 | * Replace a whole area with one character. |
| 1867 | */ |
| 1868 | int |
| 1869 | op_replace(oap, c) |
| 1870 | oparg_T *oap; |
| 1871 | int c; |
| 1872 | { |
| 1873 | int n, numc; |
| 1874 | #ifdef FEAT_MBYTE |
| 1875 | int num_chars; |
| 1876 | #endif |
| 1877 | char_u *newp, *oldp; |
| 1878 | size_t oldlen; |
| 1879 | struct block_def bd; |
| 1880 | |
| 1881 | if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) |
| 1882 | return OK; /* nothing to do */ |
| 1883 | |
| 1884 | #ifdef FEAT_MBYTE |
| 1885 | if (has_mbyte) |
| 1886 | mb_adjust_opend(oap); |
| 1887 | #endif |
| 1888 | |
| 1889 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 1890 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 1891 | return FAIL; |
| 1892 | |
| 1893 | /* |
| 1894 | * block mode replace |
| 1895 | */ |
| 1896 | if (oap->block_mode) |
| 1897 | { |
| 1898 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 1899 | for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum) |
| 1900 | { |
| 1901 | block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); |
| 1902 | if (bd.textlen == 0 && (!virtual_op || bd.is_MAX)) |
| 1903 | continue; /* nothing to replace */ |
| 1904 | |
| 1905 | /* n == number of extra chars required |
| 1906 | * If we split a TAB, it may be replaced by several characters. |
| 1907 | * Thus the number of characters may increase! |
| 1908 | */ |
| 1909 | #ifdef FEAT_VIRTUALEDIT |
| 1910 | /* If the range starts in virtual space, count the initial |
| 1911 | * coladd offset as part of "startspaces" */ |
| 1912 | if (virtual_op && bd.is_short && *bd.textstart == NUL) |
| 1913 | { |
| 1914 | pos_T vpos; |
| 1915 | |
| 1916 | getvpos(&vpos, oap->start_vcol); |
| 1917 | bd.startspaces += vpos.coladd; |
| 1918 | n = bd.startspaces; |
| 1919 | } |
| 1920 | else |
| 1921 | #endif |
| 1922 | /* allow for pre spaces */ |
| 1923 | n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); |
| 1924 | |
| 1925 | /* allow for post spp */ |
| 1926 | n += (bd.endspaces |
| 1927 | #ifdef FEAT_VIRTUALEDIT |
| 1928 | && !bd.is_oneChar |
| 1929 | #endif |
| 1930 | && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; |
| 1931 | /* Figure out how many characters to replace. */ |
| 1932 | numc = oap->end_vcol - oap->start_vcol + 1; |
| 1933 | if (bd.is_short && (!virtual_op || bd.is_MAX)) |
| 1934 | numc -= (oap->end_vcol - bd.end_vcol) + 1; |
| 1935 | |
| 1936 | #ifdef FEAT_MBYTE |
| 1937 | /* A double-wide character can be replaced only up to half the |
| 1938 | * times. */ |
| 1939 | if ((*mb_char2cells)(c) > 1) |
| 1940 | { |
| 1941 | if ((numc & 1) && !bd.is_short) |
| 1942 | { |
| 1943 | ++bd.endspaces; |
| 1944 | ++n; |
| 1945 | } |
| 1946 | numc = numc / 2; |
| 1947 | } |
| 1948 | |
| 1949 | /* Compute bytes needed, move character count to num_chars. */ |
| 1950 | num_chars = numc; |
| 1951 | numc *= (*mb_char2len)(c); |
| 1952 | #endif |
| 1953 | /* oldlen includes textlen, so don't double count */ |
| 1954 | n += numc - bd.textlen; |
| 1955 | |
| 1956 | oldp = ml_get_curline(); |
| 1957 | oldlen = STRLEN(oldp); |
| 1958 | newp = alloc_check((unsigned)oldlen + 1 + n); |
| 1959 | if (newp == NULL) |
| 1960 | continue; |
| 1961 | vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); |
| 1962 | /* copy up to deleted part */ |
| 1963 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 1964 | oldp += bd.textcol + bd.textlen; |
| 1965 | /* insert pre-spaces */ |
| 1966 | copy_spaces(newp + bd.textcol, (size_t)bd.startspaces); |
| 1967 | /* insert replacement chars CHECK FOR ALLOCATED SPACE */ |
| 1968 | #ifdef FEAT_MBYTE |
| 1969 | if (has_mbyte) |
| 1970 | { |
| 1971 | n = STRLEN(newp); |
| 1972 | while (--num_chars >= 0) |
| 1973 | n += (*mb_char2bytes)(c, newp + n); |
| 1974 | } |
| 1975 | else |
| 1976 | #endif |
| 1977 | copy_chars(newp + STRLEN(newp), (size_t)numc, c); |
| 1978 | if (!bd.is_short) |
| 1979 | { |
| 1980 | /* insert post-spaces */ |
| 1981 | copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces); |
| 1982 | /* copy the part after the changed part */ |
| 1983 | mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1); |
| 1984 | } |
| 1985 | /* replace the line */ |
| 1986 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 1987 | } |
| 1988 | } |
| 1989 | else |
| 1990 | { |
| 1991 | /* |
| 1992 | * MCHAR and MLINE motion replace. |
| 1993 | */ |
| 1994 | if (oap->motion_type == MLINE) |
| 1995 | { |
| 1996 | oap->start.col = 0; |
| 1997 | curwin->w_cursor.col = 0; |
| 1998 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 1999 | if (oap->end.col) |
| 2000 | --oap->end.col; |
| 2001 | } |
| 2002 | else if (!oap->inclusive) |
| 2003 | dec(&(oap->end)); |
| 2004 | |
| 2005 | while (ltoreq(curwin->w_cursor, oap->end)) |
| 2006 | { |
| 2007 | n = gchar_cursor(); |
| 2008 | if (n != NUL) |
| 2009 | { |
| 2010 | #ifdef FEAT_MBYTE |
| 2011 | if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) |
| 2012 | { |
| 2013 | /* This is slow, but it handles replacing a single-byte |
| 2014 | * with a multi-byte and the other way around. */ |
| 2015 | oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); |
| 2016 | n = State; |
| 2017 | State = REPLACE; |
| 2018 | ins_char(c); |
| 2019 | State = n; |
| 2020 | /* Backup to the replaced character. */ |
| 2021 | dec_cursor(); |
| 2022 | } |
| 2023 | else |
| 2024 | #endif |
| 2025 | { |
| 2026 | #ifdef FEAT_VIRTUALEDIT |
| 2027 | if (n == TAB) |
| 2028 | { |
| 2029 | int end_vcol = 0; |
| 2030 | |
| 2031 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2032 | { |
| 2033 | /* oap->end has to be recalculated when |
| 2034 | * the tab breaks */ |
| 2035 | end_vcol = getviscol2(oap->end.col, |
| 2036 | oap->end.coladd); |
| 2037 | } |
| 2038 | coladvance_force(getviscol()); |
| 2039 | if (curwin->w_cursor.lnum == oap->end.lnum) |
| 2040 | getvpos(&oap->end, end_vcol); |
| 2041 | } |
| 2042 | #endif |
| 2043 | pchar(curwin->w_cursor, c); |
| 2044 | } |
| 2045 | } |
| 2046 | #ifdef FEAT_VIRTUALEDIT |
| 2047 | else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) |
| 2048 | { |
| 2049 | int virtcols = oap->end.coladd; |
| 2050 | |
| 2051 | if (curwin->w_cursor.lnum == oap->start.lnum |
| 2052 | && oap->start.col == oap->end.col && oap->start.coladd) |
| 2053 | virtcols -= oap->start.coladd; |
| 2054 | |
| 2055 | /* oap->end has been trimmed so it's effectively inclusive; |
| 2056 | * as a result an extra +1 must be counted so we don't |
| 2057 | * trample the NUL byte. */ |
| 2058 | coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1); |
| 2059 | curwin->w_cursor.col -= (virtcols + 1); |
| 2060 | for (; virtcols >= 0; virtcols--) |
| 2061 | { |
| 2062 | pchar(curwin->w_cursor, c); |
| 2063 | if (inc(&curwin->w_cursor) == -1) |
| 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | #endif |
| 2068 | |
| 2069 | /* Advance to next character, stop at the end of the file. */ |
| 2070 | if (inc_cursor() == -1) |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | curwin->w_cursor = oap->start; |
| 2076 | check_cursor(); |
| 2077 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L); |
| 2078 | |
| 2079 | /* Set "'[" and "']" marks. */ |
| 2080 | curbuf->b_op_start = oap->start; |
| 2081 | curbuf->b_op_end = oap->end; |
| 2082 | |
| 2083 | return OK; |
| 2084 | } |
| 2085 | #endif |
| 2086 | |
| 2087 | /* |
| 2088 | * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". |
| 2089 | */ |
| 2090 | void |
| 2091 | op_tilde(oap) |
| 2092 | oparg_T *oap; |
| 2093 | { |
| 2094 | pos_T pos; |
| 2095 | #ifdef FEAT_VISUAL |
| 2096 | struct block_def bd; |
| 2097 | int done; |
| 2098 | #endif |
| 2099 | int did_change = 0; |
| 2100 | #ifdef FEAT_MBYTE |
| 2101 | colnr_T col; |
| 2102 | #endif |
| 2103 | |
| 2104 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 2105 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 2106 | return; |
| 2107 | |
| 2108 | pos = oap->start; |
| 2109 | #ifdef FEAT_VISUAL |
| 2110 | if (oap->block_mode) /* Visual block mode */ |
| 2111 | { |
| 2112 | for (; pos.lnum <= oap->end.lnum; ++pos.lnum) |
| 2113 | { |
| 2114 | block_prep(oap, &bd, pos.lnum, FALSE); |
| 2115 | pos.col = bd.textcol; |
| 2116 | for (done = 0; done < bd.textlen; ++done) |
| 2117 | { |
| 2118 | did_change |= swapchar(oap->op_type, &pos); |
| 2119 | # ifdef FEAT_MBYTE |
| 2120 | col = pos.col + 1; |
| 2121 | # endif |
| 2122 | if (inc(&pos) == -1) /* at end of file */ |
| 2123 | break; |
| 2124 | # ifdef FEAT_MBYTE |
| 2125 | if (pos.col > col) |
| 2126 | /* Count extra bytes of a multi-byte character. */ |
| 2127 | done += pos.col - col; |
| 2128 | # endif |
| 2129 | } |
| 2130 | # ifdef FEAT_NETBEANS_INTG |
| 2131 | if (usingNetbeans && did_change) |
| 2132 | { |
| 2133 | char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2134 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2135 | netbeans_removed(curbuf, pos.lnum, bd.textcol, |
| 2136 | (long)bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | netbeans_inserted(curbuf, pos.lnum, bd.textcol, |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2138 | &ptr[bd.textcol], bd.textlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | } |
| 2140 | # endif |
| 2141 | } |
| 2142 | if (did_change) |
| 2143 | changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); |
| 2144 | } |
| 2145 | else /* not block mode */ |
| 2146 | #endif |
| 2147 | { |
| 2148 | if (oap->motion_type == MLINE) |
| 2149 | { |
| 2150 | oap->start.col = 0; |
| 2151 | pos.col = 0; |
| 2152 | oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); |
| 2153 | if (oap->end.col) |
| 2154 | --oap->end.col; |
| 2155 | } |
| 2156 | else if (!oap->inclusive) |
| 2157 | dec(&(oap->end)); |
| 2158 | |
| 2159 | while (ltoreq(pos, oap->end)) |
| 2160 | { |
| 2161 | did_change |= swapchar(oap->op_type, &pos); |
| 2162 | if (inc(&pos) == -1) /* at end of file */ |
| 2163 | break; |
| 2164 | } |
| 2165 | if (did_change) |
| 2166 | { |
| 2167 | changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, |
| 2168 | 0L); |
| 2169 | #ifdef FEAT_NETBEANS_INTG |
| 2170 | if (usingNetbeans && did_change) |
| 2171 | { |
| 2172 | char_u *ptr; |
| 2173 | int count; |
| 2174 | |
| 2175 | pos = oap->start; |
| 2176 | while (pos.lnum < oap->end.lnum) |
| 2177 | { |
| 2178 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2179 | count = STRLEN(ptr) - pos.col; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2180 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2182 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2183 | pos.col = 0; |
| 2184 | pos.lnum++; |
| 2185 | } |
| 2186 | ptr = ml_get_buf(curbuf, pos.lnum, FALSE); |
| 2187 | count = oap->end.col - pos.col + 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2188 | netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2189 | netbeans_inserted(curbuf, pos.lnum, pos.col, |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2190 | &ptr[pos.col], count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2191 | } |
| 2192 | #endif |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | #ifdef FEAT_VISUAL |
| 2197 | if (!did_change && oap->is_VIsual) |
| 2198 | /* No change: need to remove the Visual selection */ |
| 2199 | redraw_curbuf_later(INVERTED); |
| 2200 | #endif |
| 2201 | |
| 2202 | /* |
| 2203 | * Set '[ and '] marks. |
| 2204 | */ |
| 2205 | curbuf->b_op_start = oap->start; |
| 2206 | curbuf->b_op_end = oap->end; |
| 2207 | |
| 2208 | if (oap->line_count > p_report) |
| 2209 | { |
| 2210 | if (oap->line_count == 1) |
| 2211 | MSG(_("1 line changed")); |
| 2212 | else |
| 2213 | smsg((char_u *)_("%ld lines changed"), oap->line_count); |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | /* |
| 2218 | * If op_type == OP_UPPER: make uppercase, |
| 2219 | * if op_type == OP_LOWER: make lowercase, |
| 2220 | * if op_type == OP_ROT13: do rot13 encoding, |
| 2221 | * else swap case of character at 'pos' |
| 2222 | * returns TRUE when something actually changed. |
| 2223 | */ |
| 2224 | int |
| 2225 | swapchar(op_type, pos) |
| 2226 | int op_type; |
| 2227 | pos_T *pos; |
| 2228 | { |
| 2229 | int c; |
| 2230 | int nc; |
| 2231 | |
| 2232 | c = gchar_pos(pos); |
| 2233 | |
| 2234 | /* Only do rot13 encoding for ASCII characters. */ |
| 2235 | if (c >= 0x80 && op_type == OP_ROT13) |
| 2236 | return FALSE; |
| 2237 | |
| 2238 | #ifdef FEAT_MBYTE |
| 2239 | if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */ |
| 2240 | return FALSE; |
| 2241 | #endif |
| 2242 | nc = c; |
| 2243 | if (MB_ISLOWER(c)) |
| 2244 | { |
| 2245 | if (op_type == OP_ROT13) |
| 2246 | nc = ROT13(c, 'a'); |
| 2247 | else if (op_type != OP_LOWER) |
| 2248 | nc = MB_TOUPPER(c); |
| 2249 | } |
| 2250 | else if (MB_ISUPPER(c)) |
| 2251 | { |
| 2252 | if (op_type == OP_ROT13) |
| 2253 | nc = ROT13(c, 'A'); |
| 2254 | else if (op_type != OP_UPPER) |
| 2255 | nc = MB_TOLOWER(c); |
| 2256 | } |
| 2257 | if (nc != c) |
| 2258 | { |
| 2259 | #ifdef FEAT_MBYTE |
| 2260 | if (enc_utf8 && (c >= 0x80 || nc >= 0x80)) |
| 2261 | { |
| 2262 | pos_T sp = curwin->w_cursor; |
| 2263 | |
| 2264 | curwin->w_cursor = *pos; |
| 2265 | del_char(FALSE); |
| 2266 | ins_char(nc); |
| 2267 | curwin->w_cursor = sp; |
| 2268 | } |
| 2269 | else |
| 2270 | #endif |
| 2271 | pchar(*pos, nc); |
| 2272 | return TRUE; |
| 2273 | } |
| 2274 | return FALSE; |
| 2275 | } |
| 2276 | |
| 2277 | #if defined(FEAT_VISUALEXTRA) || defined(PROTO) |
| 2278 | /* |
| 2279 | * op_insert - Insert and append operators for Visual mode. |
| 2280 | */ |
| 2281 | void |
| 2282 | op_insert(oap, count1) |
| 2283 | oparg_T *oap; |
| 2284 | long count1; |
| 2285 | { |
| 2286 | long ins_len, pre_textlen = 0; |
| 2287 | char_u *firstline, *ins_text; |
| 2288 | struct block_def bd; |
| 2289 | int i; |
| 2290 | |
| 2291 | /* edit() changes this - record it for OP_APPEND */ |
| 2292 | bd.is_MAX = (curwin->w_curswant == MAXCOL); |
| 2293 | |
| 2294 | /* vis block is still marked. Get rid of it now. */ |
| 2295 | curwin->w_cursor.lnum = oap->start.lnum; |
| 2296 | update_screen(INVERTED); |
| 2297 | |
| 2298 | if (oap->block_mode) |
| 2299 | { |
| 2300 | #ifdef FEAT_VIRTUALEDIT |
| 2301 | /* When 'virtualedit' is used, need to insert the extra spaces before |
| 2302 | * doing block_prep(). When only "block" is used, virtual edit is |
| 2303 | * already disabled, but still need it when calling |
| 2304 | * coladvance_force(). */ |
| 2305 | if (curwin->w_cursor.coladd > 0) |
| 2306 | { |
| 2307 | int old_ve_flags = ve_flags; |
| 2308 | |
| 2309 | ve_flags = VE_ALL; |
| 2310 | if (u_save_cursor() == FAIL) |
| 2311 | return; |
| 2312 | coladvance_force(oap->op_type == OP_APPEND |
| 2313 | ? oap->end_vcol + 1 : getviscol()); |
| 2314 | if (oap->op_type == OP_APPEND) |
| 2315 | --curwin->w_cursor.col; |
| 2316 | ve_flags = old_ve_flags; |
| 2317 | } |
| 2318 | #endif |
| 2319 | /* Get the info about the block before entering the text */ |
| 2320 | block_prep(oap, &bd, oap->start.lnum, TRUE); |
| 2321 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
| 2322 | if (oap->op_type == OP_APPEND) |
| 2323 | firstline += bd.textlen; |
| 2324 | pre_textlen = (long)STRLEN(firstline); |
| 2325 | } |
| 2326 | |
| 2327 | if (oap->op_type == OP_APPEND) |
| 2328 | { |
| 2329 | if (oap->block_mode |
| 2330 | #ifdef FEAT_VIRTUALEDIT |
| 2331 | && curwin->w_cursor.coladd == 0 |
| 2332 | #endif |
| 2333 | ) |
| 2334 | { |
| 2335 | /* Move the cursor to the character right of the block. */ |
| 2336 | curwin->w_set_curswant = TRUE; |
| 2337 | while (*ml_get_cursor() != NUL |
| 2338 | && (curwin->w_cursor.col < bd.textcol + bd.textlen)) |
| 2339 | ++curwin->w_cursor.col; |
| 2340 | if (bd.is_short && !bd.is_MAX) |
| 2341 | { |
| 2342 | /* First line was too short, make it longer and adjust the |
| 2343 | * values in "bd". */ |
| 2344 | if (u_save_cursor() == FAIL) |
| 2345 | return; |
| 2346 | for (i = 0; i < bd.endspaces; ++i) |
| 2347 | ins_char(' '); |
| 2348 | bd.textlen += bd.endspaces; |
| 2349 | } |
| 2350 | } |
| 2351 | else |
| 2352 | { |
| 2353 | curwin->w_cursor = oap->end; |
| 2354 | |
| 2355 | /* Works just like an 'i'nsert on the next character. */ |
| 2356 | if (!lineempty(curwin->w_cursor.lnum) |
| 2357 | && oap->start_vcol != oap->end_vcol) |
| 2358 | inc_cursor(); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | edit(NUL, FALSE, (linenr_T)count1); |
| 2363 | |
| 2364 | /* if user has moved off this line, we don't know what to do, so do |
| 2365 | * nothing */ |
| 2366 | if (curwin->w_cursor.lnum != oap->start.lnum) |
| 2367 | return; |
| 2368 | |
| 2369 | if (oap->block_mode) |
| 2370 | { |
| 2371 | struct block_def bd2; |
| 2372 | |
| 2373 | /* |
| 2374 | * Spaces and tabs in the indent may have changed to other spaces and |
| 2375 | * tabs. Get the starting column again and correct the lenght. |
| 2376 | * Don't do this when "$" used, end-of-line will have changed. |
| 2377 | */ |
| 2378 | block_prep(oap, &bd2, oap->start.lnum, TRUE); |
| 2379 | if (!bd.is_MAX || bd2.textlen < bd.textlen) |
| 2380 | { |
| 2381 | if (oap->op_type == OP_APPEND) |
| 2382 | { |
| 2383 | pre_textlen += bd2.textlen - bd.textlen; |
| 2384 | if (bd2.endspaces) |
| 2385 | --bd2.textlen; |
| 2386 | } |
| 2387 | bd.textcol = bd2.textcol; |
| 2388 | bd.textlen = bd2.textlen; |
| 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Subsequent calls to ml_get() flush the firstline data - take a |
| 2393 | * copy of the required string. |
| 2394 | */ |
| 2395 | firstline = ml_get(oap->start.lnum) + bd.textcol; |
| 2396 | if (oap->op_type == OP_APPEND) |
| 2397 | firstline += bd.textlen; |
| 2398 | if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) |
| 2399 | { |
| 2400 | ins_text = vim_strnsave(firstline, (int)ins_len); |
| 2401 | if (ins_text != NULL) |
| 2402 | { |
| 2403 | /* block handled here */ |
| 2404 | if (u_save(oap->start.lnum, |
| 2405 | (linenr_T)(oap->end.lnum + 1)) == OK) |
| 2406 | block_insert(oap, ins_text, (oap->op_type == OP_INSERT), |
| 2407 | &bd); |
| 2408 | |
| 2409 | curwin->w_cursor.col = oap->start.col; |
| 2410 | check_cursor(); |
| 2411 | vim_free(ins_text); |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | #endif |
| 2417 | |
| 2418 | /* |
| 2419 | * op_change - handle a change operation |
| 2420 | * |
| 2421 | * return TRUE if edit() returns because of a CTRL-O command |
| 2422 | */ |
| 2423 | int |
| 2424 | op_change(oap) |
| 2425 | oparg_T *oap; |
| 2426 | { |
| 2427 | colnr_T l; |
| 2428 | int retval; |
| 2429 | #ifdef FEAT_VISUALEXTRA |
| 2430 | long offset; |
| 2431 | linenr_T linenr; |
| 2432 | long ins_len, pre_textlen = 0; |
| 2433 | char_u *firstline; |
| 2434 | char_u *ins_text, *newp, *oldp; |
| 2435 | struct block_def bd; |
| 2436 | #endif |
| 2437 | |
| 2438 | l = oap->start.col; |
| 2439 | if (oap->motion_type == MLINE) |
| 2440 | { |
| 2441 | l = 0; |
| 2442 | #ifdef FEAT_SMARTINDENT |
| 2443 | if (!p_paste && curbuf->b_p_si |
| 2444 | # ifdef FEAT_CINDENT |
| 2445 | && !curbuf->b_p_cin |
| 2446 | # endif |
| 2447 | ) |
| 2448 | can_si = TRUE; /* It's like opening a new line, do si */ |
| 2449 | #endif |
| 2450 | } |
| 2451 | |
| 2452 | /* First delete the text in the region. In an empty buffer only need to |
| 2453 | * save for undo */ |
| 2454 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 2455 | { |
| 2456 | if (u_save_cursor() == FAIL) |
| 2457 | return FALSE; |
| 2458 | } |
| 2459 | else if (op_delete(oap) == FAIL) |
| 2460 | return FALSE; |
| 2461 | |
| 2462 | if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum) |
| 2463 | && !virtual_op) |
| 2464 | inc_cursor(); |
| 2465 | |
| 2466 | #ifdef FEAT_VISUALEXTRA |
| 2467 | /* check for still on same line (<CR> in inserted text meaningless) */ |
| 2468 | /* skip blank lines too */ |
| 2469 | if (oap->block_mode) |
| 2470 | { |
| 2471 | # ifdef FEAT_VIRTUALEDIT |
| 2472 | /* Add spaces before getting the current line length. */ |
| 2473 | if (virtual_op && (curwin->w_cursor.coladd > 0 |
| 2474 | || gchar_cursor() == NUL)) |
| 2475 | coladvance_force(getviscol()); |
| 2476 | # endif |
| 2477 | pre_textlen = (long)STRLEN(ml_get(oap->start.lnum)); |
| 2478 | bd.textcol = curwin->w_cursor.col; |
| 2479 | } |
| 2480 | #endif |
| 2481 | |
| 2482 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) |
| 2483 | if (oap->motion_type == MLINE) |
| 2484 | fix_indent(); |
| 2485 | #endif |
| 2486 | |
| 2487 | retval = edit(NUL, FALSE, (linenr_T)1); |
| 2488 | |
| 2489 | #ifdef FEAT_VISUALEXTRA |
| 2490 | /* |
| 2491 | * In Visual block mode, handle copying the next text to all lines of the |
| 2492 | * block. |
| 2493 | */ |
| 2494 | if (oap->block_mode && oap->start.lnum != oap->end.lnum) |
| 2495 | { |
| 2496 | firstline = ml_get(oap->start.lnum); |
| 2497 | /* |
| 2498 | * Subsequent calls to ml_get() flush the firstline data - take a |
| 2499 | * copy of the required bit. |
| 2500 | */ |
| 2501 | if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) |
| 2502 | { |
| 2503 | if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) |
| 2504 | { |
| 2505 | STRNCPY(ins_text, firstline + bd.textcol, ins_len); |
| 2506 | ins_text[ins_len] = NUL; |
| 2507 | for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; |
| 2508 | linenr++) |
| 2509 | { |
| 2510 | block_prep(oap, &bd, linenr, TRUE); |
| 2511 | if (!bd.is_short || virtual_op) |
| 2512 | { |
| 2513 | # ifdef FEAT_VIRTUALEDIT |
| 2514 | pos_T vpos; |
| 2515 | |
| 2516 | /* If the block starts in virtual space, count the |
| 2517 | * initial coladd offset as part of "startspaces" */ |
| 2518 | if (bd.is_short) |
| 2519 | { |
| 2520 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2521 | |
| 2522 | curwin->w_cursor.lnum = linenr; |
| 2523 | (void)getvpos(&vpos, oap->start_vcol); |
| 2524 | curwin->w_cursor.lnum = lnum; |
| 2525 | } |
| 2526 | else |
| 2527 | vpos.coladd = 0; |
| 2528 | # endif |
| 2529 | oldp = ml_get(linenr); |
| 2530 | newp = alloc_check((unsigned)(STRLEN(oldp) |
| 2531 | # ifdef FEAT_VIRTUALEDIT |
| 2532 | + vpos.coladd |
| 2533 | # endif |
| 2534 | + ins_len + 1)); |
| 2535 | if (newp == NULL) |
| 2536 | continue; |
| 2537 | /* copy up to block start */ |
| 2538 | mch_memmove(newp, oldp, (size_t)bd.textcol); |
| 2539 | offset = bd.textcol; |
| 2540 | # ifdef FEAT_VIRTUALEDIT |
| 2541 | copy_spaces(newp + offset, (size_t)vpos.coladd); |
| 2542 | offset += vpos.coladd; |
| 2543 | # endif |
| 2544 | mch_memmove(newp + offset, ins_text, (size_t)ins_len); |
| 2545 | offset += ins_len; |
| 2546 | oldp += bd.textcol; |
| 2547 | mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1); |
| 2548 | ml_replace(linenr, newp, FALSE); |
| 2549 | } |
| 2550 | } |
| 2551 | check_cursor(); |
| 2552 | |
| 2553 | changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); |
| 2554 | } |
| 2555 | vim_free(ins_text); |
| 2556 | } |
| 2557 | } |
| 2558 | #endif |
| 2559 | |
| 2560 | return retval; |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * set all the yank registers to empty (called from main()) |
| 2565 | */ |
| 2566 | void |
| 2567 | init_yank() |
| 2568 | { |
| 2569 | int i; |
| 2570 | |
| 2571 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 2572 | y_regs[i].y_array = NULL; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * Free "n" lines from the current yank register. |
| 2577 | * Called for normal freeing and in case of error. |
| 2578 | */ |
| 2579 | static void |
| 2580 | free_yank(n) |
| 2581 | long n; |
| 2582 | { |
| 2583 | if (y_current->y_array != NULL) |
| 2584 | { |
| 2585 | long i; |
| 2586 | |
| 2587 | for (i = n; --i >= 0; ) |
| 2588 | { |
| 2589 | #ifdef AMIGA /* only for very slow machines */ |
| 2590 | if ((i & 1023) == 1023) /* this may take a while */ |
| 2591 | { |
| 2592 | /* |
| 2593 | * This message should never cause a hit-return message. |
| 2594 | * Overwrite this message with any next message. |
| 2595 | */ |
| 2596 | ++no_wait_return; |
| 2597 | smsg((char_u *)_("freeing %ld lines"), i + 1); |
| 2598 | --no_wait_return; |
| 2599 | msg_didout = FALSE; |
| 2600 | msg_col = 0; |
| 2601 | } |
| 2602 | #endif |
| 2603 | vim_free(y_current->y_array[i]); |
| 2604 | } |
| 2605 | vim_free(y_current->y_array); |
| 2606 | y_current->y_array = NULL; |
| 2607 | #ifdef AMIGA |
| 2608 | if (n >= 1000) |
| 2609 | MSG(""); |
| 2610 | #endif |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | static void |
| 2615 | free_yank_all() |
| 2616 | { |
| 2617 | free_yank(y_current->y_size); |
| 2618 | } |
| 2619 | |
| 2620 | /* |
| 2621 | * Yank the text between "oap->start" and "oap->end" into a yank register. |
| 2622 | * If we are to append (uppercase register), we first yank into a new yank |
| 2623 | * register and then concatenate the old and the new one (so we keep the old |
| 2624 | * one in case of out-of-memory). |
| 2625 | * |
| 2626 | * return FAIL for failure, OK otherwise |
| 2627 | */ |
| 2628 | int |
| 2629 | op_yank(oap, deleting, mess) |
| 2630 | oparg_T *oap; |
| 2631 | int deleting; |
| 2632 | int mess; |
| 2633 | { |
| 2634 | long y_idx; /* index in y_array[] */ |
| 2635 | struct yankreg *curr; /* copy of y_current */ |
| 2636 | struct yankreg newreg; /* new yank register when appending */ |
| 2637 | char_u **new_ptr; |
| 2638 | linenr_T lnum; /* current line number */ |
| 2639 | long j; |
| 2640 | int yanktype = oap->motion_type; |
| 2641 | long yanklines = oap->line_count; |
| 2642 | linenr_T yankendlnum = oap->end.lnum; |
| 2643 | char_u *p; |
| 2644 | char_u *pnew; |
| 2645 | struct block_def bd; |
| 2646 | |
| 2647 | /* check for read-only register */ |
| 2648 | if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE)) |
| 2649 | { |
| 2650 | beep_flush(); |
| 2651 | return FAIL; |
| 2652 | } |
| 2653 | if (oap->regname == '_') /* black hole: nothing to do */ |
| 2654 | return OK; |
| 2655 | |
| 2656 | #ifdef FEAT_CLIPBOARD |
| 2657 | if (!clip_star.available && oap->regname == '*') |
| 2658 | oap->regname = 0; |
| 2659 | else if (!clip_plus.available && oap->regname == '+') |
| 2660 | oap->regname = 0; |
| 2661 | #endif |
| 2662 | |
| 2663 | if (!deleting) /* op_delete() already set y_current */ |
| 2664 | get_yank_register(oap->regname, TRUE); |
| 2665 | |
| 2666 | curr = y_current; |
| 2667 | /* append to existing contents */ |
| 2668 | if (y_append && y_current->y_array != NULL) |
| 2669 | y_current = &newreg; |
| 2670 | else |
| 2671 | free_yank_all(); /* free previously yanked lines */ |
| 2672 | |
| 2673 | /* |
| 2674 | * If the cursor was in column 1 before and after the movement, and the |
| 2675 | * operator is not inclusive, the yank is always linewise. |
| 2676 | */ |
| 2677 | if ( oap->motion_type == MCHAR |
| 2678 | && oap->start.col == 0 |
| 2679 | && !oap->inclusive |
| 2680 | #ifdef FEAT_VISUAL |
| 2681 | && (!oap->is_VIsual || *p_sel == 'o') |
| 2682 | #endif |
| 2683 | && oap->end.col == 0 |
| 2684 | && yanklines > 1) |
| 2685 | { |
| 2686 | yanktype = MLINE; |
| 2687 | --yankendlnum; |
| 2688 | --yanklines; |
| 2689 | } |
| 2690 | |
| 2691 | y_current->y_size = yanklines; |
| 2692 | y_current->y_type = yanktype; /* set the yank register type */ |
| 2693 | #ifdef FEAT_VISUAL |
| 2694 | y_current->y_width = 0; |
| 2695 | #endif |
| 2696 | y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) * |
| 2697 | yanklines), TRUE); |
| 2698 | |
| 2699 | if (y_current->y_array == NULL) |
| 2700 | { |
| 2701 | y_current = curr; |
| 2702 | return FAIL; |
| 2703 | } |
| 2704 | |
| 2705 | y_idx = 0; |
| 2706 | lnum = oap->start.lnum; |
| 2707 | |
| 2708 | #ifdef FEAT_VISUAL |
| 2709 | if (oap->block_mode) |
| 2710 | { |
| 2711 | /* Visual block mode */ |
| 2712 | y_current->y_type = MBLOCK; /* set the yank register type */ |
| 2713 | y_current->y_width = oap->end_vcol - oap->start_vcol; |
| 2714 | |
| 2715 | if (curwin->w_curswant == MAXCOL && y_current->y_width > 0) |
| 2716 | y_current->y_width--; |
| 2717 | } |
| 2718 | #endif |
| 2719 | |
| 2720 | for ( ; lnum <= yankendlnum; lnum++, y_idx++) |
| 2721 | { |
| 2722 | switch (y_current->y_type) |
| 2723 | { |
| 2724 | #ifdef FEAT_VISUAL |
| 2725 | case MBLOCK: |
| 2726 | block_prep(oap, &bd, lnum, FALSE); |
| 2727 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 2728 | goto fail; |
| 2729 | break; |
| 2730 | #endif |
| 2731 | |
| 2732 | case MLINE: |
| 2733 | if ((y_current->y_array[y_idx] = |
| 2734 | vim_strsave(ml_get(lnum))) == NULL) |
| 2735 | goto fail; |
| 2736 | break; |
| 2737 | |
| 2738 | case MCHAR: |
| 2739 | { |
| 2740 | colnr_T startcol = 0, endcol = MAXCOL; |
| 2741 | #ifdef FEAT_VIRTUALEDIT |
| 2742 | int is_oneChar = FALSE; |
| 2743 | colnr_T cs, ce; |
| 2744 | #endif |
| 2745 | p = ml_get(lnum); |
| 2746 | bd.startspaces = 0; |
| 2747 | bd.endspaces = 0; |
| 2748 | |
| 2749 | if (lnum == oap->start.lnum) |
| 2750 | { |
| 2751 | startcol = oap->start.col; |
| 2752 | #ifdef FEAT_VIRTUALEDIT |
| 2753 | if (virtual_op) |
| 2754 | { |
| 2755 | getvcol(curwin, &oap->start, &cs, NULL, &ce); |
| 2756 | if (ce != cs && oap->start.coladd > 0) |
| 2757 | { |
| 2758 | /* Part of a tab selected -- but don't |
| 2759 | * double-count it. */ |
| 2760 | bd.startspaces = (ce - cs + 1) |
| 2761 | - oap->start.coladd; |
| 2762 | startcol++; |
| 2763 | } |
| 2764 | } |
| 2765 | #endif |
| 2766 | } |
| 2767 | |
| 2768 | if (lnum == oap->end.lnum) |
| 2769 | { |
| 2770 | endcol = oap->end.col; |
| 2771 | #ifdef FEAT_VIRTUALEDIT |
| 2772 | if (virtual_op) |
| 2773 | { |
| 2774 | getvcol(curwin, &oap->end, &cs, NULL, &ce); |
| 2775 | if (p[endcol] == NUL || (cs + oap->end.coladd < ce |
| 2776 | # ifdef FEAT_MBYTE |
| 2777 | /* Don't add space for double-wide |
| 2778 | * char; endcol will be on last byte |
| 2779 | * of multi-byte char. */ |
| 2780 | && (*mb_head_off)(p, p + endcol) == 0 |
| 2781 | # endif |
| 2782 | )) |
| 2783 | { |
| 2784 | if (oap->start.lnum == oap->end.lnum |
| 2785 | && oap->start.col == oap->end.col) |
| 2786 | { |
| 2787 | /* Special case: inside a single char */ |
| 2788 | is_oneChar = TRUE; |
| 2789 | bd.startspaces = oap->end.coladd |
| 2790 | - oap->start.coladd + oap->inclusive; |
| 2791 | endcol = startcol; |
| 2792 | } |
| 2793 | else |
| 2794 | { |
| 2795 | bd.endspaces = oap->end.coladd |
| 2796 | + oap->inclusive; |
| 2797 | endcol -= oap->inclusive; |
| 2798 | } |
| 2799 | } |
| 2800 | } |
| 2801 | #endif |
| 2802 | } |
| 2803 | if (startcol > endcol |
| 2804 | #ifdef FEAT_VIRTUALEDIT |
| 2805 | || is_oneChar |
| 2806 | #endif |
| 2807 | ) |
| 2808 | bd.textlen = 0; |
| 2809 | else |
| 2810 | { |
| 2811 | if (endcol == MAXCOL) |
| 2812 | endcol = STRLEN(p); |
| 2813 | bd.textlen = endcol - startcol + oap->inclusive; |
| 2814 | } |
| 2815 | bd.textstart = p + startcol; |
| 2816 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 2817 | goto fail; |
| 2818 | break; |
| 2819 | } |
| 2820 | /* NOTREACHED */ |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | if (curr != y_current) /* append the new block to the old block */ |
| 2825 | { |
| 2826 | new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) * |
| 2827 | (curr->y_size + y_current->y_size)), TRUE); |
| 2828 | if (new_ptr == NULL) |
| 2829 | goto fail; |
| 2830 | for (j = 0; j < curr->y_size; ++j) |
| 2831 | new_ptr[j] = curr->y_array[j]; |
| 2832 | vim_free(curr->y_array); |
| 2833 | curr->y_array = new_ptr; |
| 2834 | |
| 2835 | if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */ |
| 2836 | curr->y_type = MLINE; |
| 2837 | |
| 2838 | /* concatenate the last line of the old block with the first line of |
| 2839 | * the new block */ |
| 2840 | if (curr->y_type == MCHAR) |
| 2841 | { |
| 2842 | pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1]) |
| 2843 | + STRLEN(y_current->y_array[0]) + 1), TRUE); |
| 2844 | if (pnew == NULL) |
| 2845 | { |
| 2846 | y_idx = y_current->y_size - 1; |
| 2847 | goto fail; |
| 2848 | } |
| 2849 | STRCPY(pnew, curr->y_array[--j]); |
| 2850 | STRCAT(pnew, y_current->y_array[0]); |
| 2851 | vim_free(curr->y_array[j]); |
| 2852 | vim_free(y_current->y_array[0]); |
| 2853 | curr->y_array[j++] = pnew; |
| 2854 | y_idx = 1; |
| 2855 | } |
| 2856 | else |
| 2857 | y_idx = 0; |
| 2858 | while (y_idx < y_current->y_size) |
| 2859 | curr->y_array[j++] = y_current->y_array[y_idx++]; |
| 2860 | curr->y_size = j; |
| 2861 | vim_free(y_current->y_array); |
| 2862 | y_current = curr; |
| 2863 | } |
| 2864 | if (mess) /* Display message about yank? */ |
| 2865 | { |
| 2866 | if (yanktype == MCHAR |
| 2867 | #ifdef FEAT_VISUAL |
| 2868 | && !oap->block_mode |
| 2869 | #endif |
| 2870 | && yanklines == 1) |
| 2871 | yanklines = 0; |
| 2872 | /* Some versions of Vi use ">=" here, some don't... */ |
| 2873 | if (yanklines > p_report) |
| 2874 | { |
| 2875 | /* redisplay now, so message is not deleted */ |
| 2876 | update_topline_redraw(); |
| 2877 | if (yanklines == 1) |
| 2878 | MSG(_("1 line yanked")); |
| 2879 | else |
| 2880 | smsg((char_u *)_("%ld lines yanked"), yanklines); |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | /* |
| 2885 | * Set "'[" and "']" marks. |
| 2886 | */ |
| 2887 | curbuf->b_op_start = oap->start; |
| 2888 | curbuf->b_op_end = oap->end; |
| 2889 | |
| 2890 | #ifdef FEAT_CLIPBOARD |
| 2891 | /* |
| 2892 | * If we were yanking to the '*' register, send result to clipboard. |
| 2893 | * If no register was specified, and "unnamed" in 'clipboard', make a copy |
| 2894 | * to the '*' register. |
| 2895 | */ |
| 2896 | if (clip_star.available |
| 2897 | && (curr == &(y_regs[STAR_REGISTER]) |
| 2898 | || (!deleting && oap->regname == 0 && clip_unnamed))) |
| 2899 | { |
| 2900 | if (curr != &(y_regs[STAR_REGISTER])) |
| 2901 | /* Copy the text from register 0 to the clipboard register. */ |
| 2902 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 2903 | |
| 2904 | clip_own_selection(&clip_star); |
| 2905 | clip_gen_set_selection(&clip_star); |
| 2906 | } |
| 2907 | |
| 2908 | # ifdef FEAT_X11 |
| 2909 | /* |
| 2910 | * If we were yanking to the '+' register, send result to selection. |
| 2911 | * Also copy to the '*' register, in case auto-select is off. |
| 2912 | */ |
| 2913 | else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER])) |
| 2914 | { |
| 2915 | /* No need to copy to * register upon 'unnamed' now - see below */ |
| 2916 | clip_own_selection(&clip_plus); |
| 2917 | clip_gen_set_selection(&clip_plus); |
| 2918 | if (!clip_isautosel()) |
| 2919 | { |
| 2920 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 2921 | clip_own_selection(&clip_star); |
| 2922 | clip_gen_set_selection(&clip_star); |
| 2923 | } |
| 2924 | } |
| 2925 | # endif |
| 2926 | #endif |
| 2927 | |
| 2928 | return OK; |
| 2929 | |
| 2930 | fail: /* free the allocated lines */ |
| 2931 | free_yank(y_idx + 1); |
| 2932 | y_current = curr; |
| 2933 | return FAIL; |
| 2934 | } |
| 2935 | |
| 2936 | static int |
| 2937 | yank_copy_line(bd, y_idx) |
| 2938 | struct block_def *bd; |
| 2939 | long y_idx; |
| 2940 | { |
| 2941 | char_u *pnew; |
| 2942 | |
| 2943 | if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1)) |
| 2944 | == NULL) |
| 2945 | return FAIL; |
| 2946 | y_current->y_array[y_idx] = pnew; |
| 2947 | copy_spaces(pnew, (size_t)bd->startspaces); |
| 2948 | pnew += bd->startspaces; |
| 2949 | mch_memmove(pnew, bd->textstart, (size_t)bd->textlen); |
| 2950 | pnew += bd->textlen; |
| 2951 | copy_spaces(pnew, (size_t)bd->endspaces); |
| 2952 | pnew += bd->endspaces; |
| 2953 | *pnew = NUL; |
| 2954 | return OK; |
| 2955 | } |
| 2956 | |
| 2957 | #ifdef FEAT_CLIPBOARD |
| 2958 | /* |
| 2959 | * Make a copy of the y_current register to register "reg". |
| 2960 | */ |
| 2961 | static void |
| 2962 | copy_yank_reg(reg) |
| 2963 | struct yankreg *reg; |
| 2964 | { |
| 2965 | struct yankreg *curr = y_current; |
| 2966 | long j; |
| 2967 | |
| 2968 | y_current = reg; |
| 2969 | free_yank_all(); |
| 2970 | *y_current = *curr; |
| 2971 | y_current->y_array = (char_u **)lalloc_clear( |
| 2972 | (long_u)(sizeof(char_u *) * y_current->y_size), TRUE); |
| 2973 | if (y_current->y_array == NULL) |
| 2974 | y_current->y_size = 0; |
| 2975 | else |
| 2976 | for (j = 0; j < y_current->y_size; ++j) |
| 2977 | if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL) |
| 2978 | { |
| 2979 | free_yank(j); |
| 2980 | y_current->y_size = 0; |
| 2981 | break; |
| 2982 | } |
| 2983 | y_current = curr; |
| 2984 | } |
| 2985 | #endif |
| 2986 | |
| 2987 | /* |
| 2988 | * put contents of register "regname" into the text |
| 2989 | * flags: PUT_FIXINDENT make indent look nice |
| 2990 | * PUT_CURSEND leave cursor after end of new text |
| 2991 | * PUT_LINE force linewise put (":put") |
| 2992 | */ |
| 2993 | void |
| 2994 | do_put(regname, dir, count, flags) |
| 2995 | int regname; |
| 2996 | int dir; /* BACKWARD for 'P', FORWARD for 'p' */ |
| 2997 | long count; |
| 2998 | int flags; |
| 2999 | { |
| 3000 | char_u *ptr; |
| 3001 | char_u *newp, *oldp; |
| 3002 | int yanklen; |
| 3003 | int totlen = 0; /* init for gcc */ |
| 3004 | linenr_T lnum; |
| 3005 | colnr_T col; |
| 3006 | long i; /* index in y_array[] */ |
| 3007 | int y_type; |
| 3008 | long y_size; |
| 3009 | #ifdef FEAT_VISUAL |
| 3010 | int oldlen; |
| 3011 | long y_width = 0; |
| 3012 | colnr_T vcol; |
| 3013 | int delcount; |
| 3014 | int incr = 0; |
| 3015 | long j; |
| 3016 | struct block_def bd; |
| 3017 | #endif |
| 3018 | char_u **y_array = NULL; |
| 3019 | long nr_lines = 0; |
| 3020 | pos_T new_cursor; |
| 3021 | int indent; |
| 3022 | int orig_indent = 0; /* init for gcc */ |
| 3023 | int indent_diff = 0; /* init for gcc */ |
| 3024 | int first_indent = TRUE; |
| 3025 | int lendiff = 0; |
| 3026 | pos_T old_pos; |
| 3027 | char_u *insert_string = NULL; |
| 3028 | int allocated = FALSE; |
| 3029 | long cnt; |
| 3030 | |
| 3031 | #ifdef FEAT_CLIPBOARD |
| 3032 | /* Adjust register name for "unnamed" in 'clipboard'. */ |
| 3033 | adjust_clip_reg(®name); |
| 3034 | (void)may_get_selection(regname); |
| 3035 | #endif |
| 3036 | |
| 3037 | if (flags & PUT_FIXINDENT) |
| 3038 | orig_indent = get_indent(); |
| 3039 | |
| 3040 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3041 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3042 | |
| 3043 | /* |
| 3044 | * Using inserted text works differently, because the register includes |
| 3045 | * special characters (newlines, etc.). |
| 3046 | */ |
| 3047 | if (regname == '.') |
| 3048 | { |
| 3049 | (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') : |
| 3050 | (count == -1 ? 'O' : 'i')), count, FALSE); |
| 3051 | /* Putting the text is done later, so can't really move the cursor to |
| 3052 | * the next character. Use "l" to simulate it. */ |
| 3053 | if ((flags & PUT_CURSEND) && gchar_cursor() != NUL) |
| 3054 | stuffcharReadbuff('l'); |
| 3055 | return; |
| 3056 | } |
| 3057 | |
| 3058 | /* |
| 3059 | * For special registers '%' (file name), '#' (alternate file name) and |
| 3060 | * ':' (last command line), etc. we have to create a fake yank register. |
| 3061 | */ |
| 3062 | if (get_spec_reg(regname, &insert_string, &allocated, TRUE)) |
| 3063 | { |
| 3064 | if (insert_string == NULL) |
| 3065 | return; |
| 3066 | } |
| 3067 | |
| 3068 | if (insert_string != NULL) |
| 3069 | { |
| 3070 | y_type = MCHAR; |
| 3071 | #ifdef FEAT_EVAL |
| 3072 | if (regname == '=') |
| 3073 | { |
| 3074 | /* For the = register we need to split the string at NL |
| 3075 | * characters. */ |
| 3076 | /* Loop twice: count the number of lines and save them. */ |
| 3077 | for (;;) |
| 3078 | { |
| 3079 | y_size = 0; |
| 3080 | ptr = insert_string; |
| 3081 | while (ptr != NULL) |
| 3082 | { |
| 3083 | if (y_array != NULL) |
| 3084 | y_array[y_size] = ptr; |
| 3085 | ++y_size; |
| 3086 | ptr = vim_strchr(ptr, '\n'); |
| 3087 | if (ptr != NULL) |
| 3088 | { |
| 3089 | if (y_array != NULL) |
| 3090 | *ptr = NUL; |
| 3091 | ++ptr; |
| 3092 | /* A trailing '\n' makes the string linewise */ |
| 3093 | if (*ptr == NUL) |
| 3094 | { |
| 3095 | y_type = MLINE; |
| 3096 | break; |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | if (y_array != NULL) |
| 3101 | break; |
| 3102 | y_array = (char_u **)alloc((unsigned) |
| 3103 | (y_size * sizeof(char_u *))); |
| 3104 | if (y_array == NULL) |
| 3105 | goto end; |
| 3106 | } |
| 3107 | } |
| 3108 | else |
| 3109 | #endif |
| 3110 | { |
| 3111 | y_size = 1; /* use fake one-line yank register */ |
| 3112 | y_array = &insert_string; |
| 3113 | } |
| 3114 | } |
| 3115 | else |
| 3116 | { |
| 3117 | get_yank_register(regname, FALSE); |
| 3118 | |
| 3119 | y_type = y_current->y_type; |
| 3120 | #ifdef FEAT_VISUAL |
| 3121 | y_width = y_current->y_width; |
| 3122 | #endif |
| 3123 | y_size = y_current->y_size; |
| 3124 | y_array = y_current->y_array; |
| 3125 | } |
| 3126 | |
| 3127 | #ifdef FEAT_VISUAL |
| 3128 | if (y_type == MLINE) |
| 3129 | { |
| 3130 | if (flags & PUT_LINE_SPLIT) |
| 3131 | { |
| 3132 | /* "p" or "P" in Visual mode: split the lines to put the text in |
| 3133 | * between. */ |
| 3134 | if (u_save_cursor() == FAIL) |
| 3135 | goto end; |
| 3136 | ptr = vim_strsave(ml_get_cursor()); |
| 3137 | if (ptr == NULL) |
| 3138 | goto end; |
| 3139 | ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE); |
| 3140 | vim_free(ptr); |
| 3141 | |
| 3142 | ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col); |
| 3143 | if (ptr == NULL) |
| 3144 | goto end; |
| 3145 | ml_replace(curwin->w_cursor.lnum, ptr, FALSE); |
| 3146 | ++nr_lines; |
| 3147 | dir = FORWARD; |
| 3148 | } |
| 3149 | if (flags & PUT_LINE_FORWARD) |
| 3150 | { |
| 3151 | /* Must be "p" for a Visual block, put lines below the block. */ |
| 3152 | curwin->w_cursor = curbuf->b_visual_end; |
| 3153 | dir = FORWARD; |
| 3154 | } |
| 3155 | curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ |
| 3156 | curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ |
| 3157 | } |
| 3158 | #endif |
| 3159 | |
| 3160 | if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */ |
| 3161 | y_type = MLINE; |
| 3162 | |
| 3163 | if (y_size == 0 || y_array == NULL) |
| 3164 | { |
| 3165 | EMSG2(_("E353: Nothing in register %s"), |
| 3166 | regname == 0 ? (char_u *)"\"" : transchar(regname)); |
| 3167 | goto end; |
| 3168 | } |
| 3169 | |
| 3170 | #ifdef FEAT_VISUAL |
| 3171 | if (y_type == MBLOCK) |
| 3172 | { |
| 3173 | lnum = curwin->w_cursor.lnum + y_size + 1; |
| 3174 | if (lnum > curbuf->b_ml.ml_line_count) |
| 3175 | lnum = curbuf->b_ml.ml_line_count + 1; |
| 3176 | if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) |
| 3177 | goto end; |
| 3178 | } |
| 3179 | else |
| 3180 | #endif |
| 3181 | if (y_type == MLINE) |
| 3182 | { |
| 3183 | lnum = curwin->w_cursor.lnum; |
| 3184 | #ifdef FEAT_FOLDING |
| 3185 | /* Correct line number for closed fold. Don't move the cursor yet, |
| 3186 | * u_save() uses it. */ |
| 3187 | if (dir == BACKWARD) |
| 3188 | (void)hasFolding(lnum, &lnum, NULL); |
| 3189 | else |
| 3190 | (void)hasFolding(lnum, NULL, &lnum); |
| 3191 | #endif |
| 3192 | if (dir == FORWARD) |
| 3193 | ++lnum; |
| 3194 | if (u_save(lnum - 1, lnum) == FAIL) |
| 3195 | goto end; |
| 3196 | #ifdef FEAT_FOLDING |
| 3197 | if (dir == FORWARD) |
| 3198 | curwin->w_cursor.lnum = lnum - 1; |
| 3199 | else |
| 3200 | curwin->w_cursor.lnum = lnum; |
| 3201 | curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */ |
| 3202 | #endif |
| 3203 | } |
| 3204 | else if (u_save_cursor() == FAIL) |
| 3205 | goto end; |
| 3206 | |
| 3207 | yanklen = (int)STRLEN(y_array[0]); |
| 3208 | |
| 3209 | #ifdef FEAT_VIRTUALEDIT |
| 3210 | if (ve_flags == VE_ALL && y_type == MCHAR) |
| 3211 | { |
| 3212 | if (gchar_cursor() == TAB) |
| 3213 | { |
| 3214 | /* Don't need to insert spaces when "p" on the last position of a |
| 3215 | * tab or "P" on the first position. */ |
| 3216 | if (dir == FORWARD |
| 3217 | ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1 |
| 3218 | : curwin->w_cursor.coladd > 0) |
| 3219 | coladvance_force(getviscol()); |
| 3220 | else |
| 3221 | curwin->w_cursor.coladd = 0; |
| 3222 | } |
| 3223 | else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL) |
| 3224 | coladvance_force(getviscol() + (dir == FORWARD)); |
| 3225 | } |
| 3226 | #endif |
| 3227 | |
| 3228 | lnum = curwin->w_cursor.lnum; |
| 3229 | col = curwin->w_cursor.col; |
| 3230 | |
| 3231 | #ifdef FEAT_VISUAL |
| 3232 | /* |
| 3233 | * Block mode |
| 3234 | */ |
| 3235 | if (y_type == MBLOCK) |
| 3236 | { |
| 3237 | char c = gchar_cursor(); |
| 3238 | colnr_T endcol2 = 0; |
| 3239 | |
| 3240 | if (dir == FORWARD && c != NUL) |
| 3241 | { |
| 3242 | #ifdef FEAT_VIRTUALEDIT |
| 3243 | if (ve_flags == VE_ALL) |
| 3244 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3245 | else |
| 3246 | #endif |
| 3247 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); |
| 3248 | |
| 3249 | #ifdef FEAT_MBYTE |
| 3250 | if (has_mbyte) |
| 3251 | /* move to start of next multi-byte character */ |
| 3252 | curwin->w_cursor.col += (*mb_ptr2len_check)(ml_get_cursor()); |
| 3253 | else |
| 3254 | #endif |
| 3255 | #ifdef FEAT_VIRTUALEDIT |
| 3256 | if (c != TAB || ve_flags != VE_ALL) |
| 3257 | #endif |
| 3258 | ++curwin->w_cursor.col; |
| 3259 | ++col; |
| 3260 | } |
| 3261 | else |
| 3262 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 3263 | |
| 3264 | #ifdef FEAT_VIRTUALEDIT |
| 3265 | col += curwin->w_cursor.coladd; |
| 3266 | if (ve_flags == VE_ALL && curwin->w_cursor.coladd > 0) |
| 3267 | { |
| 3268 | if (dir == FORWARD && c == NUL) |
| 3269 | ++col; |
| 3270 | if (dir != FORWARD && c != NUL) |
| 3271 | ++curwin->w_cursor.col; |
| 3272 | if (c == TAB) |
| 3273 | { |
| 3274 | if (dir == BACKWARD && curwin->w_cursor.col) |
| 3275 | curwin->w_cursor.col--; |
| 3276 | if (dir == FORWARD && col - 1 == endcol2) |
| 3277 | curwin->w_cursor.col++; |
| 3278 | } |
| 3279 | } |
| 3280 | curwin->w_cursor.coladd = 0; |
| 3281 | #endif |
| 3282 | for (i = 0; i < y_size; ++i) |
| 3283 | { |
| 3284 | int spaces; |
| 3285 | char shortline; |
| 3286 | |
| 3287 | bd.startspaces = 0; |
| 3288 | bd.endspaces = 0; |
| 3289 | bd.textcol = 0; |
| 3290 | vcol = 0; |
| 3291 | delcount = 0; |
| 3292 | |
| 3293 | /* add a new line */ |
| 3294 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 3295 | { |
| 3296 | if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", |
| 3297 | (colnr_T)1, FALSE) == FAIL) |
| 3298 | break; |
| 3299 | ++nr_lines; |
| 3300 | } |
| 3301 | /* get the old line and advance to the position to insert at */ |
| 3302 | oldp = ml_get_curline(); |
| 3303 | oldlen = (int)STRLEN(oldp); |
| 3304 | for (ptr = oldp; vcol < col && *ptr; ) |
| 3305 | { |
| 3306 | /* Count a tab for what it's worth (if list mode not on) */ |
| 3307 | incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol); |
| 3308 | vcol += incr; |
| 3309 | } |
| 3310 | bd.textcol = (colnr_T)(ptr - oldp); |
| 3311 | |
| 3312 | shortline = (vcol < col) || (vcol == col && !*ptr) ; |
| 3313 | |
| 3314 | if (vcol < col) /* line too short, padd with spaces */ |
| 3315 | bd.startspaces = col - vcol; |
| 3316 | else if (vcol > col) |
| 3317 | { |
| 3318 | bd.endspaces = vcol - col; |
| 3319 | bd.startspaces = incr - bd.endspaces; |
| 3320 | --bd.textcol; |
| 3321 | delcount = 1; |
| 3322 | #ifdef FEAT_MBYTE |
| 3323 | if (has_mbyte) |
| 3324 | bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol); |
| 3325 | #endif |
| 3326 | if (oldp[bd.textcol] != TAB) |
| 3327 | { |
| 3328 | /* Only a Tab can be split into spaces. Other |
| 3329 | * characters will have to be moved to after the |
| 3330 | * block, causing misalignment. */ |
| 3331 | delcount = 0; |
| 3332 | bd.endspaces = 0; |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | yanklen = (int)STRLEN(y_array[i]); |
| 3337 | |
| 3338 | /* calculate number of spaces required to fill right side of block*/ |
| 3339 | spaces = y_width + 1; |
| 3340 | for (j = 0; j < yanklen; j++) |
| 3341 | spaces -= lbr_chartabsize(&y_array[i][j], 0); |
| 3342 | if (spaces < 0) |
| 3343 | spaces = 0; |
| 3344 | |
| 3345 | /* insert the new text */ |
| 3346 | totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; |
| 3347 | newp = alloc_check((unsigned)totlen + oldlen + 1); |
| 3348 | if (newp == NULL) |
| 3349 | break; |
| 3350 | /* copy part up to cursor to new line */ |
| 3351 | ptr = newp; |
| 3352 | mch_memmove(ptr, oldp, (size_t)bd.textcol); |
| 3353 | ptr += bd.textcol; |
| 3354 | /* may insert some spaces before the new text */ |
| 3355 | copy_spaces(ptr, (size_t)bd.startspaces); |
| 3356 | ptr += bd.startspaces; |
| 3357 | /* insert the new text */ |
| 3358 | for (j = 0; j < count; ++j) |
| 3359 | { |
| 3360 | mch_memmove(ptr, y_array[i], (size_t)yanklen); |
| 3361 | ptr += yanklen; |
| 3362 | |
| 3363 | /* insert block's trailing spaces only if there's text behind */ |
| 3364 | if ((j < count - 1 || !shortline) && spaces) |
| 3365 | { |
| 3366 | copy_spaces(ptr, (size_t)spaces); |
| 3367 | ptr += spaces; |
| 3368 | } |
| 3369 | } |
| 3370 | /* may insert some spaces after the new text */ |
| 3371 | copy_spaces(ptr, (size_t)bd.endspaces); |
| 3372 | ptr += bd.endspaces; |
| 3373 | /* move the text after the cursor to the end of the line. */ |
| 3374 | mch_memmove(ptr, oldp + bd.textcol + delcount, |
| 3375 | (size_t)(oldlen - bd.textcol - delcount + 1)); |
| 3376 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 3377 | |
| 3378 | ++curwin->w_cursor.lnum; |
| 3379 | if (i == 0) |
| 3380 | curwin->w_cursor.col += bd.startspaces; |
| 3381 | } |
| 3382 | |
| 3383 | changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines); |
| 3384 | |
| 3385 | /* Set '[ mark. */ |
| 3386 | curbuf->b_op_start = curwin->w_cursor; |
| 3387 | curbuf->b_op_start.lnum = lnum; |
| 3388 | |
| 3389 | /* adjust '] mark */ |
| 3390 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; |
| 3391 | curbuf->b_op_end.col = bd.textcol + totlen - 1; |
| 3392 | #ifdef FEAT_VIRTUALEDIT |
| 3393 | curbuf->b_op_end.coladd = 0; |
| 3394 | #endif |
| 3395 | if (flags & PUT_CURSEND) |
| 3396 | { |
| 3397 | curwin->w_cursor = curbuf->b_op_end; |
| 3398 | curwin->w_cursor.col++; |
| 3399 | } |
| 3400 | else |
| 3401 | curwin->w_cursor.lnum = lnum; |
| 3402 | } |
| 3403 | else |
| 3404 | #endif |
| 3405 | { |
| 3406 | /* |
| 3407 | * Character or Line mode |
| 3408 | */ |
| 3409 | if (y_type == MCHAR) |
| 3410 | { |
| 3411 | /* if type is MCHAR, FORWARD is the same as BACKWARD on the next |
| 3412 | * char */ |
| 3413 | if (dir == FORWARD && gchar_cursor() != NUL) |
| 3414 | { |
| 3415 | #ifdef FEAT_MBYTE |
| 3416 | if (has_mbyte) |
| 3417 | { |
| 3418 | int bytelen = (*mb_ptr2len_check)(ml_get_cursor()); |
| 3419 | |
| 3420 | /* put it on the next of the multi-byte character. */ |
| 3421 | col += bytelen; |
| 3422 | if (yanklen) |
| 3423 | { |
| 3424 | curwin->w_cursor.col += bytelen; |
| 3425 | curbuf->b_op_end.col += bytelen; |
| 3426 | } |
| 3427 | } |
| 3428 | else |
| 3429 | #endif |
| 3430 | { |
| 3431 | ++col; |
| 3432 | if (yanklen) |
| 3433 | { |
| 3434 | ++curwin->w_cursor.col; |
| 3435 | ++curbuf->b_op_end.col; |
| 3436 | } |
| 3437 | } |
| 3438 | } |
| 3439 | new_cursor = curwin->w_cursor; |
| 3440 | curbuf->b_op_start = curwin->w_cursor; |
| 3441 | } |
| 3442 | /* |
| 3443 | * Line mode: BACKWARD is the same as FORWARD on the previous line |
| 3444 | */ |
| 3445 | else if (dir == BACKWARD) |
| 3446 | --lnum; |
| 3447 | |
| 3448 | /* |
| 3449 | * simple case: insert into current line |
| 3450 | */ |
| 3451 | if (y_type == MCHAR && y_size == 1) |
| 3452 | { |
| 3453 | totlen = count * yanklen; |
| 3454 | if (totlen) |
| 3455 | { |
| 3456 | oldp = ml_get(lnum); |
| 3457 | newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1)); |
| 3458 | if (newp == NULL) |
| 3459 | goto end; /* alloc() will give error message */ |
| 3460 | mch_memmove(newp, oldp, (size_t)col); |
| 3461 | ptr = newp + col; |
| 3462 | for (i = 0; i < count; ++i) |
| 3463 | { |
| 3464 | mch_memmove(ptr, y_array[0], (size_t)yanklen); |
| 3465 | ptr += yanklen; |
| 3466 | } |
| 3467 | mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1); |
| 3468 | ml_replace(lnum, newp, FALSE); |
| 3469 | /* Put cursor on last putted char. */ |
| 3470 | curwin->w_cursor.col += (colnr_T)(totlen - 1); |
| 3471 | } |
| 3472 | curbuf->b_op_end = curwin->w_cursor; |
| 3473 | /* For "CTRL-O p" in Insert mode, put cursor after last char */ |
| 3474 | if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND))) |
| 3475 | ++curwin->w_cursor.col; |
| 3476 | changed_bytes(lnum, col); |
| 3477 | } |
| 3478 | else |
| 3479 | { |
| 3480 | /* |
| 3481 | * Insert at least one line. When y_type is MCHAR, break the first |
| 3482 | * line in two. |
| 3483 | */ |
| 3484 | for (cnt = 1; cnt <= count; ++cnt) |
| 3485 | { |
| 3486 | i = 0; |
| 3487 | if (y_type == MCHAR) |
| 3488 | { |
| 3489 | /* |
| 3490 | * Split the current line in two at the insert position. |
| 3491 | * First insert y_array[size - 1] in front of second line. |
| 3492 | * Then append y_array[0] to first line. |
| 3493 | */ |
| 3494 | lnum = new_cursor.lnum; |
| 3495 | ptr = ml_get(lnum) + col; |
| 3496 | totlen = (int)STRLEN(y_array[y_size - 1]); |
| 3497 | newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1)); |
| 3498 | if (newp == NULL) |
| 3499 | goto error; |
| 3500 | STRCPY(newp, y_array[y_size - 1]); |
| 3501 | STRCAT(newp, ptr); |
| 3502 | /* insert second line */ |
| 3503 | ml_append(lnum, newp, (colnr_T)0, FALSE); |
| 3504 | vim_free(newp); |
| 3505 | |
| 3506 | oldp = ml_get(lnum); |
| 3507 | newp = alloc_check((unsigned)(col + yanklen + 1)); |
| 3508 | if (newp == NULL) |
| 3509 | goto error; |
| 3510 | /* copy first part of line */ |
| 3511 | mch_memmove(newp, oldp, (size_t)col); |
| 3512 | /* append to first line */ |
| 3513 | mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1)); |
| 3514 | ml_replace(lnum, newp, FALSE); |
| 3515 | |
| 3516 | curwin->w_cursor.lnum = lnum; |
| 3517 | i = 1; |
| 3518 | } |
| 3519 | |
| 3520 | for (; i < y_size; ++i) |
| 3521 | { |
| 3522 | if ((y_type != MCHAR || i < y_size - 1) |
| 3523 | && ml_append(lnum, y_array[i], (colnr_T)0, FALSE) |
| 3524 | == FAIL) |
| 3525 | goto error; |
| 3526 | lnum++; |
| 3527 | ++nr_lines; |
| 3528 | if (flags & PUT_FIXINDENT) |
| 3529 | { |
| 3530 | old_pos = curwin->w_cursor; |
| 3531 | curwin->w_cursor.lnum = lnum; |
| 3532 | ptr = ml_get(lnum); |
| 3533 | if (cnt == count && i == y_size - 1) |
| 3534 | lendiff = (int)STRLEN(ptr); |
| 3535 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 3536 | if (*ptr == '#' && preprocs_left()) |
| 3537 | indent = 0; /* Leave # lines at start */ |
| 3538 | else |
| 3539 | #endif |
| 3540 | if (*ptr == NUL) |
| 3541 | indent = 0; /* Ignore empty lines */ |
| 3542 | else if (first_indent) |
| 3543 | { |
| 3544 | indent_diff = orig_indent - get_indent(); |
| 3545 | indent = orig_indent; |
| 3546 | first_indent = FALSE; |
| 3547 | } |
| 3548 | else if ((indent = get_indent() + indent_diff) < 0) |
| 3549 | indent = 0; |
| 3550 | (void)set_indent(indent, 0); |
| 3551 | curwin->w_cursor = old_pos; |
| 3552 | /* remember how many chars were removed */ |
| 3553 | if (cnt == count && i == y_size - 1) |
| 3554 | lendiff -= (int)STRLEN(ml_get(lnum)); |
| 3555 | } |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | error: |
| 3560 | /* Adjust marks. */ |
| 3561 | if (y_type == MLINE) |
| 3562 | { |
| 3563 | curbuf->b_op_start.col = 0; |
| 3564 | if (dir == FORWARD) |
| 3565 | curbuf->b_op_start.lnum++; |
| 3566 | } |
| 3567 | mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR), |
| 3568 | (linenr_T)MAXLNUM, nr_lines, 0L); |
| 3569 | |
| 3570 | /* note changed text for displaying and folding */ |
| 3571 | if (y_type == MCHAR) |
| 3572 | changed_lines(curwin->w_cursor.lnum, col, |
| 3573 | curwin->w_cursor.lnum + 1, nr_lines); |
| 3574 | else |
| 3575 | changed_lines(curbuf->b_op_start.lnum, 0, |
| 3576 | curbuf->b_op_start.lnum, nr_lines); |
| 3577 | |
| 3578 | /* put '] mark at last inserted character */ |
| 3579 | curbuf->b_op_end.lnum = lnum; |
| 3580 | /* correct length for change in indent */ |
| 3581 | col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; |
| 3582 | if (col > 1) |
| 3583 | curbuf->b_op_end.col = col - 1; |
| 3584 | else |
| 3585 | curbuf->b_op_end.col = 0; |
| 3586 | |
| 3587 | if (flags & PUT_CURSEND) |
| 3588 | { |
| 3589 | /* put cursor after inserted text */ |
| 3590 | if (y_type == MLINE) |
| 3591 | { |
| 3592 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 3593 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 3594 | else |
| 3595 | curwin->w_cursor.lnum = lnum + 1; |
| 3596 | curwin->w_cursor.col = 0; |
| 3597 | } |
| 3598 | else |
| 3599 | { |
| 3600 | curwin->w_cursor.lnum = lnum; |
| 3601 | curwin->w_cursor.col = col; |
| 3602 | } |
| 3603 | } |
| 3604 | else if (y_type == MLINE) |
| 3605 | { |
| 3606 | /* put cursor onfirst non-blank in first inserted line */ |
| 3607 | curwin->w_cursor.col = 0; |
| 3608 | if (dir == FORWARD) |
| 3609 | ++curwin->w_cursor.lnum; |
| 3610 | beginline(BL_WHITE | BL_FIX); |
| 3611 | } |
| 3612 | else /* put cursor on first inserted character */ |
| 3613 | curwin->w_cursor = new_cursor; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | msgmore(nr_lines); |
| 3618 | curwin->w_set_curswant = TRUE; |
| 3619 | |
| 3620 | end: |
| 3621 | if (allocated) |
| 3622 | { |
| 3623 | vim_free(insert_string); |
| 3624 | if (regname == '=') |
| 3625 | vim_free(y_array); |
| 3626 | } |
| 3627 | if (gchar_cursor() == NUL |
| 3628 | && curwin->w_cursor.col > 0 |
| 3629 | && !(restart_edit || (State & INSERT))) |
| 3630 | { |
| 3631 | --curwin->w_cursor.col; |
| 3632 | #ifdef FEAT_VIRTUALEDIT |
| 3633 | if (ve_flags == VE_ALL) |
| 3634 | ++curwin->w_cursor.coladd; |
| 3635 | #endif |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO) |
| 3640 | /* |
| 3641 | * Return TRUE if lines starting with '#' should be left aligned. |
| 3642 | */ |
| 3643 | int |
| 3644 | preprocs_left() |
| 3645 | { |
| 3646 | return |
| 3647 | # ifdef FEAT_SMARTINDENT |
| 3648 | # ifdef FEAT_CINDENT |
| 3649 | (curbuf->b_p_si && !curbuf->b_p_cin) || |
| 3650 | # else |
| 3651 | curbuf->b_p_si |
| 3652 | # endif |
| 3653 | # endif |
| 3654 | # ifdef FEAT_CINDENT |
| 3655 | (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)) |
| 3656 | # endif |
| 3657 | ; |
| 3658 | } |
| 3659 | #endif |
| 3660 | |
| 3661 | /* Return the character name of the register with the given number */ |
| 3662 | int |
| 3663 | get_register_name(num) |
| 3664 | int num; |
| 3665 | { |
| 3666 | if (num == -1) |
| 3667 | return '"'; |
| 3668 | else if (num < 10) |
| 3669 | return num + '0'; |
| 3670 | else if (num == DELETION_REGISTER) |
| 3671 | return '-'; |
| 3672 | #ifdef FEAT_CLIPBOARD |
| 3673 | else if (num == STAR_REGISTER) |
| 3674 | return '*'; |
| 3675 | else if (num == PLUS_REGISTER) |
| 3676 | return '+'; |
| 3677 | #endif |
| 3678 | else |
| 3679 | { |
| 3680 | #ifdef EBCDIC |
| 3681 | int i; |
| 3682 | |
| 3683 | /* EBCDIC is really braindead ... */ |
| 3684 | i = 'a' + (num - 10); |
| 3685 | if (i > 'i') |
| 3686 | i += 7; |
| 3687 | if (i > 'r') |
| 3688 | i += 8; |
| 3689 | return i; |
| 3690 | #else |
| 3691 | return num + 'a' - 10; |
| 3692 | #endif |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | /* |
| 3697 | * ":dis" and ":registers": Display the contents of the yank registers. |
| 3698 | */ |
| 3699 | void |
| 3700 | ex_display(eap) |
| 3701 | exarg_T *eap; |
| 3702 | { |
| 3703 | int i, n; |
| 3704 | long j; |
| 3705 | char_u *p; |
| 3706 | struct yankreg *yb; |
| 3707 | int name; |
| 3708 | int attr; |
| 3709 | char_u *arg = eap->arg; |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3710 | #ifdef FEAT_MBYTE |
| 3711 | int clen; |
| 3712 | #else |
| 3713 | # define clen 1 |
| 3714 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | |
| 3716 | if (arg != NULL && *arg == NUL) |
| 3717 | arg = NULL; |
| 3718 | attr = hl_attr(HLF_8); |
| 3719 | |
| 3720 | /* Highlight title */ |
| 3721 | MSG_PUTS_TITLE(_("\n--- Registers ---")); |
| 3722 | for (i = -1; i < NUM_REGISTERS && !got_int; ++i) |
| 3723 | { |
| 3724 | name = get_register_name(i); |
| 3725 | if (arg != NULL && vim_strchr(arg, name) == NULL) |
| 3726 | continue; /* did not ask for this register */ |
| 3727 | |
| 3728 | #ifdef FEAT_CLIPBOARD |
| 3729 | /* Adjust register name for "unnamed" in 'clipboard'. |
| 3730 | * When it's a clipboard register, fill it with the current contents |
| 3731 | * of the clipboard. */ |
| 3732 | adjust_clip_reg(&name); |
| 3733 | (void)may_get_selection(name); |
| 3734 | #endif |
| 3735 | |
| 3736 | if (i == -1) |
| 3737 | { |
| 3738 | if (y_previous != NULL) |
| 3739 | yb = y_previous; |
| 3740 | else |
| 3741 | yb = &(y_regs[0]); |
| 3742 | } |
| 3743 | else |
| 3744 | yb = &(y_regs[i]); |
| 3745 | if (yb->y_array != NULL) |
| 3746 | { |
| 3747 | msg_putchar('\n'); |
| 3748 | msg_putchar('"'); |
| 3749 | msg_putchar(name); |
| 3750 | MSG_PUTS(" "); |
| 3751 | |
| 3752 | n = (int)Columns - 6; |
| 3753 | for (j = 0; j < yb->y_size && n > 1; ++j) |
| 3754 | { |
| 3755 | if (j) |
| 3756 | { |
| 3757 | MSG_PUTS_ATTR("^J", attr); |
| 3758 | n -= 2; |
| 3759 | } |
| 3760 | for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p) |
| 3761 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3762 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3763 | clen = (*mb_ptr2len_check)(p); |
| 3764 | #endif |
| 3765 | msg_outtrans_len(p, clen); |
| 3766 | #ifdef FEAT_MBYTE |
| 3767 | p += clen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3768 | #endif |
| 3769 | } |
| 3770 | } |
| 3771 | if (n > 1 && yb->y_type == MLINE) |
| 3772 | MSG_PUTS_ATTR("^J", attr); |
| 3773 | out_flush(); /* show one line at a time */ |
| 3774 | } |
| 3775 | ui_breakcheck(); |
| 3776 | } |
| 3777 | |
| 3778 | /* |
| 3779 | * display last inserted text |
| 3780 | */ |
| 3781 | if ((p = get_last_insert()) != NULL |
| 3782 | && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) |
| 3783 | { |
| 3784 | MSG_PUTS("\n\". "); |
| 3785 | dis_msg(p, TRUE); |
| 3786 | } |
| 3787 | |
| 3788 | /* |
| 3789 | * display last command line |
| 3790 | */ |
| 3791 | if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) |
| 3792 | && !got_int) |
| 3793 | { |
| 3794 | MSG_PUTS("\n\": "); |
| 3795 | dis_msg(last_cmdline, FALSE); |
| 3796 | } |
| 3797 | |
| 3798 | /* |
| 3799 | * display current file name |
| 3800 | */ |
| 3801 | if (curbuf->b_fname != NULL |
| 3802 | && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 3803 | { |
| 3804 | MSG_PUTS("\n\"% "); |
| 3805 | dis_msg(curbuf->b_fname, FALSE); |
| 3806 | } |
| 3807 | |
| 3808 | /* |
| 3809 | * display alternate file name |
| 3810 | */ |
| 3811 | if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 3812 | { |
| 3813 | char_u *fname; |
| 3814 | linenr_T dummy; |
| 3815 | |
| 3816 | if (buflist_name_nr(0, &fname, &dummy) != FAIL) |
| 3817 | { |
| 3818 | MSG_PUTS("\n\"# "); |
| 3819 | dis_msg(fname, FALSE); |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | /* |
| 3824 | * display last search pattern |
| 3825 | */ |
| 3826 | if (last_search_pat() != NULL |
| 3827 | && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) |
| 3828 | { |
| 3829 | MSG_PUTS("\n\"/ "); |
| 3830 | dis_msg(last_search_pat(), FALSE); |
| 3831 | } |
| 3832 | |
| 3833 | #ifdef FEAT_EVAL |
| 3834 | /* |
| 3835 | * display last used expression |
| 3836 | */ |
| 3837 | if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) |
| 3838 | && !got_int) |
| 3839 | { |
| 3840 | MSG_PUTS("\n\"= "); |
| 3841 | dis_msg(expr_line, FALSE); |
| 3842 | } |
| 3843 | #endif |
| 3844 | } |
| 3845 | |
| 3846 | /* |
| 3847 | * display a string for do_dis() |
| 3848 | * truncate at end of screen line |
| 3849 | */ |
| 3850 | static void |
| 3851 | dis_msg(p, skip_esc) |
| 3852 | char_u *p; |
| 3853 | int skip_esc; /* if TRUE, ignore trailing ESC */ |
| 3854 | { |
| 3855 | int n; |
| 3856 | #ifdef FEAT_MBYTE |
| 3857 | int l; |
| 3858 | #endif |
| 3859 | |
| 3860 | n = (int)Columns - 6; |
| 3861 | while (*p != NUL |
| 3862 | && !(*p == ESC && skip_esc && *(p + 1) == NUL) |
| 3863 | && (n -= ptr2cells(p)) >= 0) |
| 3864 | { |
| 3865 | #ifdef FEAT_MBYTE |
| 3866 | if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1) |
| 3867 | { |
| 3868 | msg_outtrans_len(p, l); |
| 3869 | p += l; |
| 3870 | } |
| 3871 | else |
| 3872 | #endif |
| 3873 | msg_outtrans_len(p++, 1); |
| 3874 | } |
| 3875 | ui_breakcheck(); |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * join 'count' lines (minimal 2), including u_save() |
| 3880 | */ |
| 3881 | void |
| 3882 | do_do_join(count, insert_space) |
| 3883 | long count; |
| 3884 | int insert_space; |
| 3885 | { |
| 3886 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 3887 | (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL) |
| 3888 | return; |
| 3889 | |
| 3890 | while (--count > 0) |
| 3891 | { |
| 3892 | line_breakcheck(); |
| 3893 | if (got_int || do_join(insert_space) == FAIL) |
| 3894 | { |
| 3895 | beep_flush(); |
| 3896 | break; |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | #if 0 |
| 3901 | /* |
| 3902 | * Need to update the screen if the line where the cursor is became too |
| 3903 | * long to fit on the screen. |
| 3904 | */ |
| 3905 | update_topline_redraw(); |
| 3906 | #endif |
| 3907 | } |
| 3908 | |
| 3909 | /* |
| 3910 | * Join two lines at the cursor position. |
| 3911 | * "redraw" is TRUE when the screen should be updated. |
| 3912 | * Caller must have setup for undo. |
| 3913 | * |
| 3914 | * return FAIL for failure, OK ohterwise |
| 3915 | */ |
| 3916 | int |
| 3917 | do_join(insert_space) |
| 3918 | int insert_space; |
| 3919 | { |
| 3920 | char_u *curr; |
| 3921 | char_u *next, *next_start; |
| 3922 | char_u *newp; |
| 3923 | int endcurr1, endcurr2; |
| 3924 | int currsize; /* size of the current line */ |
| 3925 | int nextsize; /* size of the next line */ |
| 3926 | int spaces; /* number of spaces to insert */ |
| 3927 | linenr_T t; |
| 3928 | |
| 3929 | if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) |
| 3930 | return FAIL; /* can't join on last line */ |
| 3931 | |
| 3932 | curr = ml_get_curline(); |
| 3933 | currsize = (int)STRLEN(curr); |
| 3934 | endcurr1 = endcurr2 = NUL; |
| 3935 | if (insert_space && currsize > 0) |
| 3936 | { |
| 3937 | #ifdef FEAT_MBYTE |
| 3938 | if (has_mbyte) |
| 3939 | { |
| 3940 | next = curr + currsize - 1; |
| 3941 | next -= (*mb_head_off)(curr, next); |
| 3942 | endcurr1 = (*mb_ptr2char)(next); |
| 3943 | if (next > curr) |
| 3944 | { |
| 3945 | --next; |
| 3946 | next -= (*mb_head_off)(curr, next); |
| 3947 | endcurr2 = (*mb_ptr2char)(next); |
| 3948 | } |
| 3949 | } |
| 3950 | else |
| 3951 | #endif |
| 3952 | { |
| 3953 | endcurr1 = *(curr + currsize - 1); |
| 3954 | if (currsize > 1) |
| 3955 | endcurr2 = *(curr + currsize - 2); |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1)); |
| 3960 | spaces = 0; |
| 3961 | if (insert_space) |
| 3962 | { |
| 3963 | next = skipwhite(next); |
| 3964 | if (*next != ')' && currsize != 0 && endcurr1 != TAB |
| 3965 | #ifdef FEAT_MBYTE |
| 3966 | && (!has_format_option(FO_MBYTE_JOIN) |
| 3967 | || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100)) |
| 3968 | && (!has_format_option(FO_MBYTE_JOIN2) |
| 3969 | || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100) |
| 3970 | #endif |
| 3971 | ) |
| 3972 | { |
| 3973 | /* don't add a space if the line is ending in a space */ |
| 3974 | if (endcurr1 == ' ') |
| 3975 | endcurr1 = endcurr2; |
| 3976 | else |
| 3977 | ++spaces; |
| 3978 | /* extra space when 'joinspaces' set and line ends in '.' */ |
| 3979 | if ( p_js |
| 3980 | && (endcurr1 == '.' |
| 3981 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 3982 | && (endcurr1 == '?' || endcurr1 == '!')))) |
| 3983 | ++spaces; |
| 3984 | } |
| 3985 | } |
| 3986 | nextsize = (int)STRLEN(next); |
| 3987 | |
| 3988 | newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1)); |
| 3989 | if (newp == NULL) |
| 3990 | return FAIL; |
| 3991 | |
| 3992 | /* |
| 3993 | * Insert the next line first, because we already have that pointer. |
| 3994 | * Curr has to be obtained again, because getting next will have |
| 3995 | * invalidated it. |
| 3996 | */ |
| 3997 | mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1)); |
| 3998 | |
| 3999 | curr = ml_get_curline(); |
| 4000 | mch_memmove(newp, curr, (size_t)currsize); |
| 4001 | |
| 4002 | copy_spaces(newp + currsize, (size_t)spaces); |
| 4003 | |
| 4004 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 4005 | |
| 4006 | /* Only report the change in the first line here, del_lines() will report |
| 4007 | * the deleted line. */ |
| 4008 | changed_lines(curwin->w_cursor.lnum, currsize, |
| 4009 | curwin->w_cursor.lnum + 1, 0L); |
| 4010 | |
| 4011 | /* |
| 4012 | * Delete the following line. To do this we move the cursor there |
| 4013 | * briefly, and then move it back. After del_lines() the cursor may |
| 4014 | * have moved up (last line deleted), so the current lnum is kept in t. |
| 4015 | * |
| 4016 | * Move marks from the deleted line to the joined line, adjusting the |
| 4017 | * column. This is not Vi compatible, but Vi deletes the marks, thus that |
| 4018 | * should not really be a problem. |
| 4019 | */ |
| 4020 | t = curwin->w_cursor.lnum; |
| 4021 | mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1, |
| 4022 | (long)(currsize + spaces - (next - next_start))); |
| 4023 | ++curwin->w_cursor.lnum; |
| 4024 | del_lines(1L, FALSE); |
| 4025 | curwin->w_cursor.lnum = t; |
| 4026 | |
| 4027 | /* |
| 4028 | * go to first character of the joined line |
| 4029 | */ |
| 4030 | curwin->w_cursor.col = currsize; |
| 4031 | check_cursor_col(); |
| 4032 | #ifdef FEAT_VIRTUALEDIT |
| 4033 | curwin->w_cursor.coladd = 0; |
| 4034 | #endif |
| 4035 | curwin->w_set_curswant = TRUE; |
| 4036 | |
| 4037 | return OK; |
| 4038 | } |
| 4039 | |
| 4040 | #ifdef FEAT_COMMENTS |
| 4041 | /* |
| 4042 | * Return TRUE if the two comment leaders given are the same. "lnum" is |
| 4043 | * the first line. White-space is ignored. Note that the whole of |
| 4044 | * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb |
| 4045 | */ |
| 4046 | static int |
| 4047 | same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags) |
| 4048 | linenr_T lnum; |
| 4049 | int leader1_len; |
| 4050 | char_u *leader1_flags; |
| 4051 | int leader2_len; |
| 4052 | char_u *leader2_flags; |
| 4053 | { |
| 4054 | int idx1 = 0, idx2 = 0; |
| 4055 | char_u *p; |
| 4056 | char_u *line1; |
| 4057 | char_u *line2; |
| 4058 | |
| 4059 | if (leader1_len == 0) |
| 4060 | return (leader2_len == 0); |
| 4061 | |
| 4062 | /* |
| 4063 | * If first leader has 'f' flag, the lines can be joined only if the |
| 4064 | * second line does not have a leader. |
| 4065 | * If first leader has 'e' flag, the lines can never be joined. |
| 4066 | * If fist leader has 's' flag, the lines can only be joined if there is |
| 4067 | * some text after it and the second line has the 'm' flag. |
| 4068 | */ |
| 4069 | if (leader1_flags != NULL) |
| 4070 | { |
| 4071 | for (p = leader1_flags; *p && *p != ':'; ++p) |
| 4072 | { |
| 4073 | if (*p == COM_FIRST) |
| 4074 | return (leader2_len == 0); |
| 4075 | if (*p == COM_END) |
| 4076 | return FALSE; |
| 4077 | if (*p == COM_START) |
| 4078 | { |
| 4079 | if (*(ml_get(lnum) + leader1_len) == NUL) |
| 4080 | return FALSE; |
| 4081 | if (leader2_flags == NULL || leader2_len == 0) |
| 4082 | return FALSE; |
| 4083 | for (p = leader2_flags; *p && *p != ':'; ++p) |
| 4084 | if (*p == COM_MIDDLE) |
| 4085 | return TRUE; |
| 4086 | return FALSE; |
| 4087 | } |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | /* |
| 4092 | * Get current line and next line, compare the leaders. |
| 4093 | * The first line has to be saved, only one line can be locked at a time. |
| 4094 | */ |
| 4095 | line1 = vim_strsave(ml_get(lnum)); |
| 4096 | if (line1 != NULL) |
| 4097 | { |
| 4098 | for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1) |
| 4099 | ; |
| 4100 | line2 = ml_get(lnum + 1); |
| 4101 | for (idx2 = 0; idx2 < leader2_len; ++idx2) |
| 4102 | { |
| 4103 | if (!vim_iswhite(line2[idx2])) |
| 4104 | { |
| 4105 | if (line1[idx1++] != line2[idx2]) |
| 4106 | break; |
| 4107 | } |
| 4108 | else |
| 4109 | while (vim_iswhite(line1[idx1])) |
| 4110 | ++idx1; |
| 4111 | } |
| 4112 | vim_free(line1); |
| 4113 | } |
| 4114 | return (idx2 == leader2_len && idx1 == leader1_len); |
| 4115 | } |
| 4116 | #endif |
| 4117 | |
| 4118 | /* |
| 4119 | * implementation of the format operator 'gq' |
| 4120 | */ |
| 4121 | void |
| 4122 | op_format(oap, keep_cursor) |
| 4123 | oparg_T *oap; |
| 4124 | int keep_cursor; /* keep cursor on same text char */ |
| 4125 | { |
| 4126 | long old_line_count = curbuf->b_ml.ml_line_count; |
| 4127 | |
| 4128 | /* Place the cursor where the "gq" or "gw" command was given, so that "u" |
| 4129 | * can put it back there. */ |
| 4130 | curwin->w_cursor = oap->cursor_start; |
| 4131 | |
| 4132 | if (u_save((linenr_T)(oap->start.lnum - 1), |
| 4133 | (linenr_T)(oap->end.lnum + 1)) == FAIL) |
| 4134 | return; |
| 4135 | curwin->w_cursor = oap->start; |
| 4136 | |
| 4137 | #ifdef FEAT_VISUAL |
| 4138 | if (oap->is_VIsual) |
| 4139 | /* When there is no change: need to remove the Visual selection */ |
| 4140 | redraw_curbuf_later(INVERTED); |
| 4141 | #endif |
| 4142 | |
| 4143 | /* Set '[ mark at the start of the formatted area */ |
| 4144 | curbuf->b_op_start = oap->start; |
| 4145 | |
| 4146 | /* For "gw" remember the cursor position and put it back below (adjusted |
| 4147 | * for joined and split lines). */ |
| 4148 | if (keep_cursor) |
| 4149 | saved_cursor = oap->cursor_start; |
| 4150 | |
| 4151 | format_lines(oap->line_count); |
| 4152 | |
| 4153 | /* |
| 4154 | * Leave the cursor at the first non-blank of the last formatted line. |
| 4155 | * If the cursor was moved one line back (e.g. with "Q}") go to the next |
| 4156 | * line, so "." will do the next lines. |
| 4157 | */ |
| 4158 | if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 4159 | ++curwin->w_cursor.lnum; |
| 4160 | beginline(BL_WHITE | BL_FIX); |
| 4161 | old_line_count = curbuf->b_ml.ml_line_count - old_line_count; |
| 4162 | msgmore(old_line_count); |
| 4163 | |
| 4164 | /* put '] mark on the end of the formatted area */ |
| 4165 | curbuf->b_op_end = curwin->w_cursor; |
| 4166 | |
| 4167 | if (keep_cursor) |
| 4168 | { |
| 4169 | curwin->w_cursor = saved_cursor; |
| 4170 | saved_cursor.lnum = 0; |
| 4171 | } |
| 4172 | |
| 4173 | #ifdef FEAT_VISUAL |
| 4174 | if (oap->is_VIsual) |
| 4175 | { |
| 4176 | win_T *wp; |
| 4177 | |
| 4178 | FOR_ALL_WINDOWS(wp) |
| 4179 | { |
| 4180 | if (wp->w_old_cursor_lnum != 0) |
| 4181 | { |
| 4182 | /* When lines have been inserted or deleted, adjust the end of |
| 4183 | * the Visual area to be redrawn. */ |
| 4184 | if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) |
| 4185 | wp->w_old_cursor_lnum += old_line_count; |
| 4186 | else |
| 4187 | wp->w_old_visual_lnum += old_line_count; |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | #endif |
| 4192 | } |
| 4193 | |
| 4194 | /* |
| 4195 | * Format "line_count" lines, starting at the cursor position. |
| 4196 | * When "line_count" is negative, format until the end of the paragraph. |
| 4197 | * Lines after the cursor line are saved for undo, caller must have saved the |
| 4198 | * first line. |
| 4199 | */ |
| 4200 | void |
| 4201 | format_lines(line_count) |
| 4202 | linenr_T line_count; |
| 4203 | { |
| 4204 | int max_len; |
| 4205 | int is_not_par; /* current line not part of parag. */ |
| 4206 | int next_is_not_par; /* next line not part of paragraph */ |
| 4207 | int is_end_par; /* at end of paragraph */ |
| 4208 | int prev_is_end_par = FALSE;/* prev. line not part of parag. */ |
| 4209 | int next_is_start_par = FALSE; |
| 4210 | #ifdef FEAT_COMMENTS |
| 4211 | int leader_len = 0; /* leader len of current line */ |
| 4212 | int next_leader_len; /* leader len of next line */ |
| 4213 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 4214 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 4215 | int do_comments; /* format comments */ |
| 4216 | #endif |
| 4217 | int advance = TRUE; |
| 4218 | int second_indent = -1; |
| 4219 | int do_second_indent; |
| 4220 | int do_number_indent; |
| 4221 | int do_trail_white; |
| 4222 | int first_par_line = TRUE; |
| 4223 | int smd_save; |
| 4224 | long count; |
| 4225 | int need_set_indent = TRUE; /* set indent of next paragraph */ |
| 4226 | int force_format = FALSE; |
| 4227 | int old_State = State; |
| 4228 | |
| 4229 | /* length of a line to force formatting: 3 * 'tw' */ |
| 4230 | max_len = comp_textwidth(TRUE) * 3; |
| 4231 | |
| 4232 | /* check for 'q', '2' and '1' in 'formatoptions' */ |
| 4233 | #ifdef FEAT_COMMENTS |
| 4234 | do_comments = has_format_option(FO_Q_COMS); |
| 4235 | #endif |
| 4236 | do_second_indent = has_format_option(FO_Q_SECOND); |
| 4237 | do_number_indent = has_format_option(FO_Q_NUMBER); |
| 4238 | do_trail_white = has_format_option(FO_WHITE_PAR); |
| 4239 | |
| 4240 | /* |
| 4241 | * Get info about the previous and current line. |
| 4242 | */ |
| 4243 | if (curwin->w_cursor.lnum > 1) |
| 4244 | is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 |
| 4245 | #ifdef FEAT_COMMENTS |
| 4246 | , &leader_len, &leader_flags, do_comments |
| 4247 | #endif |
| 4248 | ); |
| 4249 | else |
| 4250 | is_not_par = TRUE; |
| 4251 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum |
| 4252 | #ifdef FEAT_COMMENTS |
| 4253 | , &next_leader_len, &next_leader_flags, do_comments |
| 4254 | #endif |
| 4255 | ); |
| 4256 | is_end_par = (is_not_par || next_is_not_par); |
| 4257 | if (!is_end_par && do_trail_white) |
| 4258 | is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); |
| 4259 | |
| 4260 | curwin->w_cursor.lnum--; |
| 4261 | for (count = line_count; count != 0 && !got_int; --count) |
| 4262 | { |
| 4263 | /* |
| 4264 | * Advance to next paragraph. |
| 4265 | */ |
| 4266 | if (advance) |
| 4267 | { |
| 4268 | curwin->w_cursor.lnum++; |
| 4269 | prev_is_end_par = is_end_par; |
| 4270 | is_not_par = next_is_not_par; |
| 4271 | #ifdef FEAT_COMMENTS |
| 4272 | leader_len = next_leader_len; |
| 4273 | leader_flags = next_leader_flags; |
| 4274 | #endif |
| 4275 | } |
| 4276 | |
| 4277 | /* |
| 4278 | * The last line to be formatted. |
| 4279 | */ |
| 4280 | if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) |
| 4281 | { |
| 4282 | next_is_not_par = TRUE; |
| 4283 | #ifdef FEAT_COMMENTS |
| 4284 | next_leader_len = 0; |
| 4285 | next_leader_flags = NULL; |
| 4286 | #endif |
| 4287 | } |
| 4288 | else |
| 4289 | { |
| 4290 | next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 |
| 4291 | #ifdef FEAT_COMMENTS |
| 4292 | , &next_leader_len, &next_leader_flags, do_comments |
| 4293 | #endif |
| 4294 | ); |
| 4295 | if (do_number_indent) |
| 4296 | next_is_start_par = |
| 4297 | (get_number_indent(curwin->w_cursor.lnum + 1) > 0); |
| 4298 | } |
| 4299 | advance = TRUE; |
| 4300 | is_end_par = (is_not_par || next_is_not_par || next_is_start_par); |
| 4301 | if (!is_end_par && do_trail_white) |
| 4302 | is_end_par = !ends_in_white(curwin->w_cursor.lnum); |
| 4303 | |
| 4304 | /* |
| 4305 | * Skip lines that are not in a paragraph. |
| 4306 | */ |
| 4307 | if (is_not_par) |
| 4308 | { |
| 4309 | if (line_count < 0) |
| 4310 | break; |
| 4311 | } |
| 4312 | else |
| 4313 | { |
| 4314 | /* |
| 4315 | * For the first line of a paragraph, check indent of second line. |
| 4316 | * Don't do this for comments and empty lines. |
| 4317 | */ |
| 4318 | if (first_par_line |
| 4319 | && (do_second_indent || do_number_indent) |
| 4320 | && prev_is_end_par |
| 4321 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 4322 | #ifdef FEAT_COMMENTS |
| 4323 | && leader_len == 0 |
| 4324 | && next_leader_len == 0 |
| 4325 | #endif |
| 4326 | ) |
| 4327 | { |
| 4328 | if (do_second_indent |
| 4329 | && !lineempty(curwin->w_cursor.lnum + 1)) |
| 4330 | second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1); |
| 4331 | else if (do_number_indent) |
| 4332 | second_indent = get_number_indent(curwin->w_cursor.lnum); |
| 4333 | } |
| 4334 | |
| 4335 | /* |
| 4336 | * When the comment leader changes, it's the end of the paragraph. |
| 4337 | */ |
| 4338 | if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count |
| 4339 | #ifdef FEAT_COMMENTS |
| 4340 | || !same_leader(curwin->w_cursor.lnum, |
| 4341 | leader_len, leader_flags, |
| 4342 | next_leader_len, next_leader_flags) |
| 4343 | #endif |
| 4344 | ) |
| 4345 | is_end_par = TRUE; |
| 4346 | |
| 4347 | /* |
| 4348 | * If we have got to the end of a paragraph, or the line is |
| 4349 | * getting long, format it. |
| 4350 | */ |
| 4351 | if (is_end_par || force_format) |
| 4352 | { |
| 4353 | if (need_set_indent) |
| 4354 | /* replace indent in first line with minimal number of |
| 4355 | * tabs and spaces, according to current options */ |
| 4356 | (void)set_indent(get_indent(), SIN_CHANGED); |
| 4357 | |
| 4358 | /* put cursor on last non-space */ |
| 4359 | State = NORMAL; /* don't go past end-of-line */ |
| 4360 | coladvance((colnr_T)MAXCOL); |
| 4361 | while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) |
| 4362 | dec_cursor(); |
| 4363 | |
| 4364 | /* do the formatting, without 'showmode' */ |
| 4365 | State = INSERT; /* for open_line() */ |
| 4366 | smd_save = p_smd; |
| 4367 | p_smd = FALSE; |
| 4368 | insertchar(NUL, INSCHAR_FORMAT |
| 4369 | #ifdef FEAT_COMMENTS |
| 4370 | + (do_comments ? INSCHAR_DO_COM : 0) |
| 4371 | #endif |
| 4372 | , second_indent); |
| 4373 | State = old_State; |
| 4374 | p_smd = smd_save; |
| 4375 | second_indent = -1; |
| 4376 | /* at end of par.: need to set indent of next par. */ |
| 4377 | need_set_indent = is_end_par; |
| 4378 | if (is_end_par) |
| 4379 | { |
| 4380 | /* When called with a negative line count, break at the |
| 4381 | * end of the paragraph. */ |
| 4382 | if (line_count < 0) |
| 4383 | break; |
| 4384 | first_par_line = TRUE; |
| 4385 | } |
| 4386 | force_format = FALSE; |
| 4387 | } |
| 4388 | |
| 4389 | /* |
| 4390 | * When still in same paragraph, join the lines together. But |
| 4391 | * first delete the comment leader from the second line. |
| 4392 | */ |
| 4393 | if (!is_end_par) |
| 4394 | { |
| 4395 | advance = FALSE; |
| 4396 | curwin->w_cursor.lnum++; |
| 4397 | curwin->w_cursor.col = 0; |
| 4398 | if (line_count < 0 && u_save_cursor() == FAIL) |
| 4399 | break; |
| 4400 | #ifdef FEAT_COMMENTS |
| 4401 | (void)del_bytes((long)next_leader_len, FALSE); |
| 4402 | if (next_leader_len > 0) |
| 4403 | mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, |
| 4404 | (long)-next_leader_len); |
| 4405 | #endif |
| 4406 | curwin->w_cursor.lnum--; |
| 4407 | if (do_join(TRUE) == FAIL) |
| 4408 | { |
| 4409 | beep_flush(); |
| 4410 | break; |
| 4411 | } |
| 4412 | first_par_line = FALSE; |
| 4413 | /* If the line is getting long, format it next time */ |
| 4414 | if (STRLEN(ml_get_curline()) > (size_t)max_len) |
| 4415 | force_format = TRUE; |
| 4416 | else |
| 4417 | force_format = FALSE; |
| 4418 | } |
| 4419 | } |
| 4420 | line_breakcheck(); |
| 4421 | } |
| 4422 | } |
| 4423 | |
| 4424 | /* |
| 4425 | * Return TRUE if line "lnum" ends in a white character. |
| 4426 | */ |
| 4427 | static int |
| 4428 | ends_in_white(lnum) |
| 4429 | linenr_T lnum; |
| 4430 | { |
| 4431 | char_u *s = ml_get(lnum); |
| 4432 | size_t l; |
| 4433 | |
| 4434 | if (*s == NUL) |
| 4435 | return FALSE; |
| 4436 | /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro |
| 4437 | * invocation may call function multiple times". */ |
| 4438 | l = STRLEN(s) - 1; |
| 4439 | return vim_iswhite(s[l]); |
| 4440 | } |
| 4441 | |
| 4442 | /* |
| 4443 | * Blank lines, and lines containing only the comment leader, are left |
| 4444 | * untouched by the formatting. The function returns TRUE in this |
| 4445 | * case. It also returns TRUE when a line starts with the end of a comment |
| 4446 | * ('e' in comment flags), so that this line is skipped, and not joined to the |
| 4447 | * previous line. A new paragraph starts after a blank line, or when the |
| 4448 | * comment leader changes -- webb. |
| 4449 | */ |
| 4450 | #ifdef FEAT_COMMENTS |
| 4451 | static int |
| 4452 | fmt_check_par(lnum, leader_len, leader_flags, do_comments) |
| 4453 | linenr_T lnum; |
| 4454 | int *leader_len; |
| 4455 | char_u **leader_flags; |
| 4456 | int do_comments; |
| 4457 | { |
| 4458 | char_u *flags = NULL; /* init for GCC */ |
| 4459 | char_u *ptr; |
| 4460 | |
| 4461 | ptr = ml_get(lnum); |
| 4462 | if (do_comments) |
| 4463 | *leader_len = get_leader_len(ptr, leader_flags, FALSE); |
| 4464 | else |
| 4465 | *leader_len = 0; |
| 4466 | |
| 4467 | if (*leader_len > 0) |
| 4468 | { |
| 4469 | /* |
| 4470 | * Search for 'e' flag in comment leader flags. |
| 4471 | */ |
| 4472 | flags = *leader_flags; |
| 4473 | while (*flags && *flags != ':' && *flags != COM_END) |
| 4474 | ++flags; |
| 4475 | } |
| 4476 | |
| 4477 | return (*skipwhite(ptr + *leader_len) == NUL |
| 4478 | || (*leader_len > 0 && *flags == COM_END) |
| 4479 | || startPS(lnum, NUL, FALSE)); |
| 4480 | } |
| 4481 | #else |
| 4482 | static int |
| 4483 | fmt_check_par(lnum) |
| 4484 | linenr_T lnum; |
| 4485 | { |
| 4486 | return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE)); |
| 4487 | } |
| 4488 | #endif |
| 4489 | |
| 4490 | /* |
| 4491 | * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the |
| 4492 | * previous line is in the same paragraph. Used for auto-formatting. |
| 4493 | */ |
| 4494 | int |
| 4495 | paragraph_start(lnum) |
| 4496 | linenr_T lnum; |
| 4497 | { |
| 4498 | char_u *p; |
| 4499 | #ifdef FEAT_COMMENTS |
| 4500 | int leader_len = 0; /* leader len of current line */ |
| 4501 | char_u *leader_flags = NULL; /* flags for leader of current line */ |
| 4502 | int next_leader_len; /* leader len of next line */ |
| 4503 | char_u *next_leader_flags; /* flags for leader of next line */ |
| 4504 | int do_comments; /* format comments */ |
| 4505 | #endif |
| 4506 | |
| 4507 | if (lnum <= 1) |
| 4508 | return TRUE; /* start of the file */ |
| 4509 | |
| 4510 | p = ml_get(lnum - 1); |
| 4511 | if (*p == NUL) |
| 4512 | return TRUE; /* after empty line */ |
| 4513 | |
| 4514 | #ifdef FEAT_COMMENTS |
| 4515 | do_comments = has_format_option(FO_Q_COMS); |
| 4516 | #endif |
| 4517 | if (fmt_check_par(lnum - 1 |
| 4518 | #ifdef FEAT_COMMENTS |
| 4519 | , &leader_len, &leader_flags, do_comments |
| 4520 | #endif |
| 4521 | )) |
| 4522 | return TRUE; /* after non-paragraph line */ |
| 4523 | |
| 4524 | if (fmt_check_par(lnum |
| 4525 | #ifdef FEAT_COMMENTS |
| 4526 | , &next_leader_len, &next_leader_flags, do_comments |
| 4527 | #endif |
| 4528 | )) |
| 4529 | return TRUE; /* "lnum" is not a paragraph line */ |
| 4530 | |
| 4531 | if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) |
| 4532 | return TRUE; /* missing trailing space in previous line. */ |
| 4533 | |
| 4534 | if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) |
| 4535 | return TRUE; /* numbered item starts in "lnum". */ |
| 4536 | |
| 4537 | #ifdef FEAT_COMMENTS |
| 4538 | if (!same_leader(lnum - 1, leader_len, leader_flags, |
| 4539 | next_leader_len, next_leader_flags)) |
| 4540 | return TRUE; /* change of comment leader. */ |
| 4541 | #endif |
| 4542 | |
| 4543 | return FALSE; |
| 4544 | } |
| 4545 | |
| 4546 | #ifdef FEAT_VISUAL |
| 4547 | /* |
| 4548 | * prepare a few things for block mode yank/delete/tilde |
| 4549 | * |
| 4550 | * for delete: |
| 4551 | * - textlen includes the first/last char to be (partly) deleted |
| 4552 | * - start/endspaces is the number of columns that are taken by the |
| 4553 | * first/last deleted char minus the number of columns that have to be |
| 4554 | * deleted. for yank and tilde: |
| 4555 | * - textlen includes the first/last char to be wholly yanked |
| 4556 | * - start/endspaces is the number of columns of the first/last yanked char |
| 4557 | * that are to be yanked. |
| 4558 | */ |
| 4559 | static void |
| 4560 | block_prep(oap, bdp, lnum, is_del) |
| 4561 | oparg_T *oap; |
| 4562 | struct block_def *bdp; |
| 4563 | linenr_T lnum; |
| 4564 | int is_del; |
| 4565 | { |
| 4566 | int incr = 0; |
| 4567 | char_u *pend; |
| 4568 | char_u *pstart; |
| 4569 | char_u *line; |
| 4570 | char_u *prev_pstart; |
| 4571 | char_u *prev_pend; |
| 4572 | |
| 4573 | bdp->startspaces = 0; |
| 4574 | bdp->endspaces = 0; |
| 4575 | bdp->textlen = 0; |
| 4576 | bdp->start_vcol = 0; |
| 4577 | bdp->end_vcol = 0; |
| 4578 | #ifdef FEAT_VISUALEXTRA |
| 4579 | bdp->is_short = FALSE; |
| 4580 | bdp->is_oneChar = FALSE; |
| 4581 | bdp->pre_whitesp = 0; |
| 4582 | bdp->pre_whitesp_c = 0; |
| 4583 | bdp->end_char_vcols = 0; |
| 4584 | #endif |
| 4585 | bdp->start_char_vcols = 0; |
| 4586 | |
| 4587 | line = ml_get(lnum); |
| 4588 | pstart = line; |
| 4589 | prev_pstart = line; |
| 4590 | while (bdp->start_vcol < oap->start_vcol && *pstart) |
| 4591 | { |
| 4592 | /* Count a tab for what it's worth (if list mode not on) */ |
| 4593 | incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol); |
| 4594 | bdp->start_vcol += incr; |
| 4595 | #ifdef FEAT_VISUALEXTRA |
| 4596 | if (vim_iswhite(*pstart)) |
| 4597 | { |
| 4598 | bdp->pre_whitesp += incr; |
| 4599 | bdp->pre_whitesp_c++; |
| 4600 | } |
| 4601 | else |
| 4602 | { |
| 4603 | bdp->pre_whitesp = 0; |
| 4604 | bdp->pre_whitesp_c = 0; |
| 4605 | } |
| 4606 | #endif |
| 4607 | prev_pstart = pstart; |
| 4608 | #ifdef FEAT_MBYTE |
| 4609 | if (has_mbyte) |
| 4610 | pstart += (*mb_ptr2len_check)(pstart); |
| 4611 | else |
| 4612 | #endif |
| 4613 | ++pstart; |
| 4614 | } |
| 4615 | bdp->start_char_vcols = incr; |
| 4616 | if (bdp->start_vcol < oap->start_vcol) /* line too short */ |
| 4617 | { |
| 4618 | bdp->end_vcol = bdp->start_vcol; |
| 4619 | #ifdef FEAT_VISUALEXTRA |
| 4620 | bdp->is_short = TRUE; |
| 4621 | #endif |
| 4622 | if (!is_del || oap->op_type == OP_APPEND) |
| 4623 | bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; |
| 4624 | } |
| 4625 | else |
| 4626 | { |
| 4627 | /* notice: this converts partly selected Multibyte characters to |
| 4628 | * spaces, too. */ |
| 4629 | bdp->startspaces = bdp->start_vcol - oap->start_vcol; |
| 4630 | if (is_del && bdp->startspaces) |
| 4631 | bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; |
| 4632 | pend = pstart; |
| 4633 | bdp->end_vcol = bdp->start_vcol; |
| 4634 | if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ |
| 4635 | { |
| 4636 | #ifdef FEAT_VISUALEXTRA |
| 4637 | bdp->is_oneChar = TRUE; |
| 4638 | #endif |
| 4639 | if (oap->op_type == OP_INSERT) |
| 4640 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 4641 | else if (oap->op_type == OP_APPEND) |
| 4642 | { |
| 4643 | bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; |
| 4644 | bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; |
| 4645 | } |
| 4646 | else |
| 4647 | { |
| 4648 | bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; |
| 4649 | if (is_del && oap->op_type != OP_LSHIFT) |
| 4650 | { |
| 4651 | /* just putting the sum of those two into |
| 4652 | * bdp->startspaces doesn't work for Visual replace, |
| 4653 | * so we have to split the tab in two */ |
| 4654 | bdp->startspaces = bdp->start_char_vcols |
| 4655 | - (bdp->start_vcol - oap->start_vcol); |
| 4656 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 4657 | } |
| 4658 | } |
| 4659 | } |
| 4660 | else |
| 4661 | { |
| 4662 | prev_pend = pend; |
| 4663 | while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) |
| 4664 | { |
| 4665 | /* Count a tab for what it's worth (if list mode not on) */ |
| 4666 | prev_pend = pend; |
| 4667 | incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol); |
| 4668 | bdp->end_vcol += incr; |
| 4669 | } |
| 4670 | if (bdp->end_vcol <= oap->end_vcol |
| 4671 | && (!is_del |
| 4672 | || oap->op_type == OP_APPEND |
| 4673 | || oap->op_type == OP_REPLACE)) /* line too short */ |
| 4674 | { |
| 4675 | #ifdef FEAT_VISUALEXTRA |
| 4676 | bdp->is_short = TRUE; |
| 4677 | #endif |
| 4678 | /* Alternative: include spaces to fill up the block. |
| 4679 | * Disadvantage: can lead to trailing spaces when the line is |
| 4680 | * short where the text is put */ |
| 4681 | /* if (!is_del || oap->op_type == OP_APPEND) */ |
| 4682 | if (oap->op_type == OP_APPEND || virtual_op) |
| 4683 | bdp->endspaces = oap->end_vcol - bdp->end_vcol |
| 4684 | + oap->inclusive; |
| 4685 | else |
| 4686 | bdp->endspaces = 0; /* replace doesn't add characters */ |
| 4687 | } |
| 4688 | else if (bdp->end_vcol > oap->end_vcol) |
| 4689 | { |
| 4690 | bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; |
| 4691 | if (!is_del && bdp->endspaces) |
| 4692 | { |
| 4693 | bdp->endspaces = incr - bdp->endspaces; |
| 4694 | if (pend != pstart) |
| 4695 | pend = prev_pend; |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | #ifdef FEAT_VISUALEXTRA |
| 4700 | bdp->end_char_vcols = incr; |
| 4701 | #endif |
| 4702 | if (is_del && bdp->startspaces) |
| 4703 | pstart = prev_pstart; |
| 4704 | bdp->textlen = (int)(pend - pstart); |
| 4705 | } |
| 4706 | bdp->textcol = (colnr_T) (pstart - line); |
| 4707 | bdp->textstart = pstart; |
| 4708 | } |
| 4709 | #endif /* FEAT_VISUAL */ |
| 4710 | |
| 4711 | #ifdef FEAT_RIGHTLEFT |
| 4712 | static void reverse_line __ARGS((char_u *s)); |
| 4713 | |
| 4714 | static void |
| 4715 | reverse_line(s) |
| 4716 | char_u *s; |
| 4717 | { |
| 4718 | int i, j; |
| 4719 | char_u c; |
| 4720 | |
| 4721 | if ((i = (int)STRLEN(s) - 1) <= 0) |
| 4722 | return; |
| 4723 | |
| 4724 | curwin->w_cursor.col = i - curwin->w_cursor.col; |
| 4725 | for (j = 0; j < i; j++, i--) |
| 4726 | { |
| 4727 | c = s[i]; s[i] = s[j]; s[j] = c; |
| 4728 | } |
| 4729 | } |
| 4730 | |
| 4731 | # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr); |
| 4732 | #else |
| 4733 | # define RLADDSUBFIX(ptr) |
| 4734 | #endif |
| 4735 | |
| 4736 | /* |
| 4737 | * add or subtract 'Prenum1' from a number in a line |
| 4738 | * 'command' is CTRL-A for add, CTRL-X for subtract |
| 4739 | * |
| 4740 | * return FAIL for failure, OK otherwise |
| 4741 | */ |
| 4742 | int |
| 4743 | do_addsub(command, Prenum1) |
| 4744 | int command; |
| 4745 | linenr_T Prenum1; |
| 4746 | { |
| 4747 | int col; |
| 4748 | char_u *buf1; |
| 4749 | char_u buf2[NUMBUFLEN]; |
| 4750 | int hex; /* 'X' or 'x': hex; '0': octal */ |
| 4751 | static int hexupper = FALSE; /* 0xABC */ |
| 4752 | long_u n; |
| 4753 | long_u oldn; |
| 4754 | char_u *ptr; |
| 4755 | int c; |
| 4756 | int length = 0; /* character length of the number */ |
| 4757 | int todel; |
| 4758 | int dohex; |
| 4759 | int dooct; |
| 4760 | int doalp; |
| 4761 | int firstdigit; |
| 4762 | int negative; |
| 4763 | int subtract; |
| 4764 | |
| 4765 | dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */ |
| 4766 | dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */ |
| 4767 | doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */ |
| 4768 | |
| 4769 | ptr = ml_get_curline(); |
| 4770 | RLADDSUBFIX(ptr); |
| 4771 | |
| 4772 | /* |
| 4773 | * First check if we are on a hexadecimal number, after the "0x". |
| 4774 | */ |
| 4775 | col = curwin->w_cursor.col; |
| 4776 | if (dohex) |
| 4777 | while (col > 0 && vim_isxdigit(ptr[col])) |
| 4778 | --col; |
| 4779 | if ( dohex |
| 4780 | && col > 0 |
| 4781 | && (ptr[col] == 'X' |
| 4782 | || ptr[col] == 'x') |
| 4783 | && ptr[col - 1] == '0' |
| 4784 | && vim_isxdigit(ptr[col + 1])) |
| 4785 | { |
| 4786 | /* |
| 4787 | * Found hexadecimal number, move to its start. |
| 4788 | */ |
| 4789 | --col; |
| 4790 | } |
| 4791 | else |
| 4792 | { |
| 4793 | /* |
| 4794 | * Search forward and then backward to find the start of number. |
| 4795 | */ |
| 4796 | col = curwin->w_cursor.col; |
| 4797 | |
| 4798 | while (ptr[col] != NUL |
| 4799 | && !vim_isdigit(ptr[col]) |
| 4800 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 4801 | ++col; |
| 4802 | |
| 4803 | while (col > 0 |
| 4804 | && vim_isdigit(ptr[col - 1]) |
| 4805 | && !(doalp && ASCII_ISALPHA(ptr[col]))) |
| 4806 | --col; |
| 4807 | } |
| 4808 | |
| 4809 | /* truncate to max length of a number */ |
| 4810 | if (length >= NUMBUFLEN - 1) |
| 4811 | length = NUMBUFLEN - 2; |
| 4812 | |
| 4813 | /* |
| 4814 | * If a number was found, and saving for undo works, replace the number. |
| 4815 | */ |
| 4816 | firstdigit = ptr[col]; |
| 4817 | RLADDSUBFIX(ptr); |
| 4818 | if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) |
| 4819 | || u_save_cursor() != OK) |
| 4820 | { |
| 4821 | beep_flush(); |
| 4822 | return FAIL; |
| 4823 | } |
| 4824 | |
| 4825 | /* get ptr again, because u_save() may have changed it */ |
| 4826 | ptr = ml_get_curline(); |
| 4827 | RLADDSUBFIX(ptr); |
| 4828 | |
| 4829 | if (doalp && ASCII_ISALPHA(firstdigit)) |
| 4830 | { |
| 4831 | /* decrement or increment alphabetic character */ |
| 4832 | if (command == Ctrl_X) |
| 4833 | { |
| 4834 | if (CharOrd(firstdigit) < Prenum1) |
| 4835 | { |
| 4836 | if (isupper(firstdigit)) |
| 4837 | firstdigit = 'A'; |
| 4838 | else |
| 4839 | firstdigit = 'a'; |
| 4840 | } |
| 4841 | else |
| 4842 | #ifdef EBCDIC |
| 4843 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); |
| 4844 | #else |
| 4845 | firstdigit -= Prenum1; |
| 4846 | #endif |
| 4847 | } |
| 4848 | else |
| 4849 | { |
| 4850 | if (26 - CharOrd(firstdigit) - 1 < Prenum1) |
| 4851 | { |
| 4852 | if (isupper(firstdigit)) |
| 4853 | firstdigit = 'Z'; |
| 4854 | else |
| 4855 | firstdigit = 'z'; |
| 4856 | } |
| 4857 | else |
| 4858 | #ifdef EBCDIC |
| 4859 | firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); |
| 4860 | #else |
| 4861 | firstdigit += Prenum1; |
| 4862 | #endif |
| 4863 | } |
| 4864 | curwin->w_cursor.col = col; |
| 4865 | (void)del_char(FALSE); |
| 4866 | ins_char(firstdigit); |
| 4867 | } |
| 4868 | else |
| 4869 | { |
| 4870 | negative = FALSE; |
| 4871 | if (col > 0 && ptr[col - 1] == '-') /* negative number */ |
| 4872 | { |
| 4873 | --col; |
| 4874 | negative = TRUE; |
| 4875 | } |
| 4876 | |
| 4877 | /* get the number value (unsigned) */ |
| 4878 | vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n); |
| 4879 | |
| 4880 | /* ignore leading '-' for hex and octal numbers */ |
| 4881 | if (hex && negative) |
| 4882 | { |
| 4883 | ++col; |
| 4884 | --length; |
| 4885 | negative = FALSE; |
| 4886 | } |
| 4887 | |
| 4888 | /* add or subtract */ |
| 4889 | subtract = FALSE; |
| 4890 | if (command == Ctrl_X) |
| 4891 | subtract ^= TRUE; |
| 4892 | if (negative) |
| 4893 | subtract ^= TRUE; |
| 4894 | |
| 4895 | oldn = n; |
| 4896 | if (subtract) |
| 4897 | n -= (unsigned long)Prenum1; |
| 4898 | else |
| 4899 | n += (unsigned long)Prenum1; |
| 4900 | |
| 4901 | /* handle wraparound for decimal numbers */ |
| 4902 | if (!hex) |
| 4903 | { |
| 4904 | if (subtract) |
| 4905 | { |
| 4906 | if (n > oldn) |
| 4907 | { |
| 4908 | n = 1 + (n ^ (unsigned long)-1); |
| 4909 | negative ^= TRUE; |
| 4910 | } |
| 4911 | } |
| 4912 | else /* add */ |
| 4913 | { |
| 4914 | if (n < oldn) |
| 4915 | { |
| 4916 | n = (n ^ (unsigned long)-1); |
| 4917 | negative ^= TRUE; |
| 4918 | } |
| 4919 | } |
| 4920 | if (n == 0) |
| 4921 | negative = FALSE; |
| 4922 | } |
| 4923 | |
| 4924 | /* |
| 4925 | * Delete the old number. |
| 4926 | */ |
| 4927 | curwin->w_cursor.col = col; |
| 4928 | todel = length; |
| 4929 | c = gchar_cursor(); |
| 4930 | /* |
| 4931 | * Don't include the '-' in the length, only the length of the part |
| 4932 | * after it is kept the same. |
| 4933 | */ |
| 4934 | if (c == '-') |
| 4935 | --length; |
| 4936 | while (todel-- > 0) |
| 4937 | { |
| 4938 | if (c < 0x100 && isalpha(c)) |
| 4939 | { |
| 4940 | if (isupper(c)) |
| 4941 | hexupper = TRUE; |
| 4942 | else |
| 4943 | hexupper = FALSE; |
| 4944 | } |
| 4945 | /* del_char() will mark line needing displaying */ |
| 4946 | (void)del_char(FALSE); |
| 4947 | c = gchar_cursor(); |
| 4948 | } |
| 4949 | |
| 4950 | /* |
| 4951 | * Prepare the leading characters in buf1[]. |
| 4952 | * When there are many leading zeros it could be very long. Allocate |
| 4953 | * a bit too much. |
| 4954 | */ |
| 4955 | buf1 = alloc((unsigned)length + NUMBUFLEN); |
| 4956 | if (buf1 == NULL) |
| 4957 | return FAIL; |
| 4958 | ptr = buf1; |
| 4959 | if (negative) |
| 4960 | { |
| 4961 | *ptr++ = '-'; |
| 4962 | } |
| 4963 | if (hex) |
| 4964 | { |
| 4965 | *ptr++ = '0'; |
| 4966 | --length; |
| 4967 | } |
| 4968 | if (hex == 'x' || hex == 'X') |
| 4969 | { |
| 4970 | *ptr++ = hex; |
| 4971 | --length; |
| 4972 | } |
| 4973 | |
| 4974 | /* |
| 4975 | * Put the number characters in buf2[]. |
| 4976 | */ |
| 4977 | if (hex == 0) |
| 4978 | sprintf((char *)buf2, "%lu", n); |
| 4979 | else if (hex == '0') |
| 4980 | sprintf((char *)buf2, "%lo", n); |
| 4981 | else if (hex && hexupper) |
| 4982 | sprintf((char *)buf2, "%lX", n); |
| 4983 | else |
| 4984 | sprintf((char *)buf2, "%lx", n); |
| 4985 | length -= (int)STRLEN(buf2); |
| 4986 | |
| 4987 | /* |
| 4988 | * adjust number of zeros to the new number of digits, so the |
| 4989 | * total length of the number remains the same |
| 4990 | */ |
| 4991 | if (firstdigit == '0') |
| 4992 | while (length-- > 0) |
| 4993 | *ptr++ = '0'; |
| 4994 | *ptr = NUL; |
| 4995 | STRCAT(buf1, buf2); |
| 4996 | ins_str(buf1); /* insert the new number */ |
| 4997 | vim_free(buf1); |
| 4998 | } |
| 4999 | --curwin->w_cursor.col; |
| 5000 | curwin->w_set_curswant = TRUE; |
| 5001 | #ifdef FEAT_RIGHTLEFT |
| 5002 | ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE); |
| 5003 | RLADDSUBFIX(ptr); |
| 5004 | #endif |
| 5005 | return OK; |
| 5006 | } |
| 5007 | |
| 5008 | #ifdef FEAT_VIMINFO |
| 5009 | int |
| 5010 | read_viminfo_register(virp, force) |
| 5011 | vir_T *virp; |
| 5012 | int force; |
| 5013 | { |
| 5014 | int eof; |
| 5015 | int do_it = TRUE; |
| 5016 | int size; |
| 5017 | int limit; |
| 5018 | int i; |
| 5019 | int set_prev = FALSE; |
| 5020 | char_u *str; |
| 5021 | char_u **array = NULL; |
| 5022 | |
| 5023 | /* We only get here (hopefully) if line[0] == '"' */ |
| 5024 | str = virp->vir_line + 1; |
| 5025 | if (*str == '"') |
| 5026 | { |
| 5027 | set_prev = TRUE; |
| 5028 | str++; |
| 5029 | } |
| 5030 | if (!ASCII_ISALNUM(*str) && *str != '-') |
| 5031 | { |
| 5032 | if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line)) |
| 5033 | return TRUE; /* too many errors, pretend end-of-file */ |
| 5034 | do_it = FALSE; |
| 5035 | } |
| 5036 | get_yank_register(*str++, FALSE); |
| 5037 | if (!force && y_current->y_array != NULL) |
| 5038 | do_it = FALSE; |
| 5039 | size = 0; |
| 5040 | limit = 100; /* Optimized for registers containing <= 100 lines */ |
| 5041 | if (do_it) |
| 5042 | { |
| 5043 | if (set_prev) |
| 5044 | y_previous = y_current; |
| 5045 | vim_free(y_current->y_array); |
| 5046 | array = y_current->y_array = |
| 5047 | (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); |
| 5048 | str = skipwhite(str); |
| 5049 | if (STRNCMP(str, "CHAR", 4) == 0) |
| 5050 | y_current->y_type = MCHAR; |
| 5051 | #ifdef FEAT_VISUAL |
| 5052 | else if (STRNCMP(str, "BLOCK", 5) == 0) |
| 5053 | y_current->y_type = MBLOCK; |
| 5054 | #endif |
| 5055 | else |
| 5056 | y_current->y_type = MLINE; |
| 5057 | /* get the block width; if it's missing we get a zero, which is OK */ |
| 5058 | str = skipwhite(skiptowhite(str)); |
| 5059 | #ifdef FEAT_VISUAL |
| 5060 | y_current->y_width = getdigits(&str); |
| 5061 | #else |
| 5062 | (void)getdigits(&str); |
| 5063 | #endif |
| 5064 | } |
| 5065 | |
| 5066 | while (!(eof = viminfo_readline(virp)) |
| 5067 | && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<')) |
| 5068 | { |
| 5069 | if (do_it) |
| 5070 | { |
| 5071 | if (size >= limit) |
| 5072 | { |
| 5073 | y_current->y_array = (char_u **) |
| 5074 | alloc((unsigned)(limit * 2 * sizeof(char_u *))); |
| 5075 | for (i = 0; i < limit; i++) |
| 5076 | y_current->y_array[i] = array[i]; |
| 5077 | vim_free(array); |
| 5078 | limit *= 2; |
| 5079 | array = y_current->y_array; |
| 5080 | } |
| 5081 | str = viminfo_readstring(virp, 1, TRUE); |
| 5082 | if (str != NULL) |
| 5083 | array[size++] = str; |
| 5084 | else |
| 5085 | do_it = FALSE; |
| 5086 | } |
| 5087 | } |
| 5088 | if (do_it) |
| 5089 | { |
| 5090 | if (size == 0) |
| 5091 | { |
| 5092 | vim_free(array); |
| 5093 | y_current->y_array = NULL; |
| 5094 | } |
| 5095 | else if (size < limit) |
| 5096 | { |
| 5097 | y_current->y_array = |
| 5098 | (char_u **)alloc((unsigned)(size * sizeof(char_u *))); |
| 5099 | for (i = 0; i < size; i++) |
| 5100 | y_current->y_array[i] = array[i]; |
| 5101 | vim_free(array); |
| 5102 | } |
| 5103 | y_current->y_size = size; |
| 5104 | } |
| 5105 | return eof; |
| 5106 | } |
| 5107 | |
| 5108 | void |
| 5109 | write_viminfo_registers(fp) |
| 5110 | FILE *fp; |
| 5111 | { |
| 5112 | int i, j; |
| 5113 | char_u *type; |
| 5114 | char_u c; |
| 5115 | int num_lines; |
| 5116 | int max_num_lines; |
| 5117 | int max_kbyte; |
| 5118 | long len; |
| 5119 | |
| 5120 | fprintf(fp, _("\n# Registers:\n")); |
| 5121 | |
| 5122 | /* Get '<' value, use old '"' value if '<' is not found. */ |
| 5123 | max_num_lines = get_viminfo_parameter('<'); |
| 5124 | if (max_num_lines < 0) |
| 5125 | max_num_lines = get_viminfo_parameter('"'); |
| 5126 | if (max_num_lines == 0) |
| 5127 | return; |
| 5128 | max_kbyte = get_viminfo_parameter('s'); |
| 5129 | if (max_kbyte == 0) |
| 5130 | return; |
| 5131 | for (i = 0; i < NUM_REGISTERS; i++) |
| 5132 | { |
| 5133 | if (y_regs[i].y_array == NULL) |
| 5134 | continue; |
| 5135 | #ifdef FEAT_CLIPBOARD |
| 5136 | /* Skip '*'/'+' register, we don't want them back next time */ |
| 5137 | if (i == STAR_REGISTER || i == PLUS_REGISTER) |
| 5138 | continue; |
| 5139 | #endif |
| 5140 | #ifdef FEAT_DND |
| 5141 | /* Neither do we want the '~' register */ |
| 5142 | if (i == TILDE_REGISTER) |
| 5143 | continue; |
| 5144 | #endif |
| 5145 | num_lines = y_regs[i].y_size; |
| 5146 | if (max_kbyte > 0) |
| 5147 | { |
| 5148 | /* Skip register if there is more text than the maximum size. */ |
| 5149 | len = 0; |
| 5150 | for (j = 0; j < num_lines; j++) |
| 5151 | len += STRLEN(y_regs[i].y_array[j]) + 1L; |
| 5152 | if (len > (long)max_kbyte * 1024L) |
| 5153 | continue; |
| 5154 | } |
| 5155 | |
| 5156 | switch (y_regs[i].y_type) |
| 5157 | { |
| 5158 | case MLINE: |
| 5159 | type = (char_u *)"LINE"; |
| 5160 | break; |
| 5161 | case MCHAR: |
| 5162 | type = (char_u *)"CHAR"; |
| 5163 | break; |
| 5164 | #ifdef FEAT_VISUAL |
| 5165 | case MBLOCK: |
| 5166 | type = (char_u *)"BLOCK"; |
| 5167 | break; |
| 5168 | #endif |
| 5169 | default: |
| 5170 | sprintf((char *)IObuff, _("E574: Unknown register type %d"), |
| 5171 | y_regs[i].y_type); |
| 5172 | emsg(IObuff); |
| 5173 | type = (char_u *)"LINE"; |
| 5174 | break; |
| 5175 | } |
| 5176 | if (y_previous == &y_regs[i]) |
| 5177 | fprintf(fp, "\""); |
| 5178 | c = get_register_name(i); |
| 5179 | fprintf(fp, "\"%c\t%s\t%d\n", c, type, |
| 5180 | #ifdef FEAT_VISUAL |
| 5181 | (int)y_regs[i].y_width |
| 5182 | #else |
| 5183 | 0 |
| 5184 | #endif |
| 5185 | ); |
| 5186 | |
| 5187 | /* If max_num_lines < 0, then we save ALL the lines in the register */ |
| 5188 | if (max_num_lines > 0 && num_lines > max_num_lines) |
| 5189 | num_lines = max_num_lines; |
| 5190 | for (j = 0; j < num_lines; j++) |
| 5191 | { |
| 5192 | putc('\t', fp); |
| 5193 | viminfo_writestring(fp, y_regs[i].y_array[j]); |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | #endif /* FEAT_VIMINFO */ |
| 5198 | |
| 5199 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 5200 | /* |
| 5201 | * SELECTION / PRIMARY ('*') |
| 5202 | * |
| 5203 | * Text selection stuff that uses the GUI selection register '*'. When using a |
| 5204 | * GUI this may be text from another window, otherwise it is the last text we |
| 5205 | * had highlighted with VIsual mode. With mouse support, clicking the middle |
| 5206 | * button performs the paste, otherwise you will need to do <"*p>. " |
| 5207 | * If not under X, it is synonymous with the clipboard register '+'. |
| 5208 | * |
| 5209 | * X CLIPBOARD ('+') |
| 5210 | * |
| 5211 | * Text selection stuff that uses the GUI clipboard register '+'. |
| 5212 | * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. |
| 5213 | * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", |
| 5214 | * otherwise you will need to do <"+p>. " |
| 5215 | * If not under X, it is synonymous with the selection register '*'. |
| 5216 | */ |
| 5217 | |
| 5218 | /* |
| 5219 | * Routine to export any final X selection we had to the environment |
| 5220 | * so that the text is still available after vim has exited. X selections |
| 5221 | * only exist while the owning application exists, so we write to the |
| 5222 | * permanent (while X runs) store CUT_BUFFER0. |
| 5223 | * Dump the CLIPBOARD selection if we own it (it's logically the more |
| 5224 | * 'permanent' of the two), otherwise the PRIMARY one. |
| 5225 | * For now, use a hard-coded sanity limit of 1Mb of data. |
| 5226 | */ |
| 5227 | #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) |
| 5228 | void |
| 5229 | x11_export_final_selection() |
| 5230 | { |
| 5231 | Display *dpy; |
| 5232 | char_u *str = NULL; |
| 5233 | long_u len = 0; |
| 5234 | int motion_type = -1; |
| 5235 | |
| 5236 | # ifdef FEAT_GUI |
| 5237 | if (gui.in_use) |
| 5238 | dpy = X_DISPLAY; |
| 5239 | else |
| 5240 | # endif |
| 5241 | # ifdef FEAT_XCLIPBOARD |
| 5242 | dpy = xterm_dpy; |
| 5243 | # else |
| 5244 | return; |
| 5245 | # endif |
| 5246 | |
| 5247 | /* Get selection to export */ |
| 5248 | if (clip_plus.owned) |
| 5249 | motion_type = clip_convert_selection(&str, &len, &clip_plus); |
| 5250 | else if (clip_star.owned) |
| 5251 | motion_type = clip_convert_selection(&str, &len, &clip_star); |
| 5252 | |
| 5253 | /* Check it's OK */ |
| 5254 | if (dpy != NULL && str != NULL && motion_type >= 0 |
| 5255 | && len < 1024*1024 && len > 0) |
| 5256 | { |
| 5257 | XStoreBuffer(dpy, (char *)str, (int)len, 0); |
| 5258 | XFlush(dpy); |
| 5259 | } |
| 5260 | |
| 5261 | vim_free(str); |
| 5262 | } |
| 5263 | #endif |
| 5264 | |
| 5265 | void |
| 5266 | clip_free_selection(cbd) |
| 5267 | VimClipboard *cbd; |
| 5268 | { |
| 5269 | struct yankreg *y_ptr = y_current; |
| 5270 | |
| 5271 | if (cbd == &clip_plus) |
| 5272 | y_current = &y_regs[PLUS_REGISTER]; |
| 5273 | else |
| 5274 | y_current = &y_regs[STAR_REGISTER]; |
| 5275 | free_yank_all(); |
| 5276 | y_current->y_size = 0; |
| 5277 | y_current = y_ptr; |
| 5278 | } |
| 5279 | |
| 5280 | /* |
| 5281 | * Get the selected text and put it in the gui selection register '*' or '+'. |
| 5282 | */ |
| 5283 | void |
| 5284 | clip_get_selection(cbd) |
| 5285 | VimClipboard *cbd; |
| 5286 | { |
| 5287 | struct yankreg *old_y_previous, *old_y_current; |
| 5288 | pos_T old_cursor; |
| 5289 | #ifdef FEAT_VISUAL |
| 5290 | pos_T old_visual; |
| 5291 | int old_visual_mode; |
| 5292 | #endif |
| 5293 | colnr_T old_curswant; |
| 5294 | int old_set_curswant; |
| 5295 | pos_T old_op_start, old_op_end; |
| 5296 | oparg_T oa; |
| 5297 | cmdarg_T ca; |
| 5298 | |
| 5299 | if (cbd->owned) |
| 5300 | { |
| 5301 | if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) |
| 5302 | || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) |
| 5303 | return; |
| 5304 | |
| 5305 | /* Get the text between clip_star.start & clip_star.end */ |
| 5306 | old_y_previous = y_previous; |
| 5307 | old_y_current = y_current; |
| 5308 | old_cursor = curwin->w_cursor; |
| 5309 | old_curswant = curwin->w_curswant; |
| 5310 | old_set_curswant = curwin->w_set_curswant; |
| 5311 | old_op_start = curbuf->b_op_start; |
| 5312 | old_op_end = curbuf->b_op_end; |
| 5313 | #ifdef FEAT_VISUAL |
| 5314 | old_visual = VIsual; |
| 5315 | old_visual_mode = VIsual_mode; |
| 5316 | #endif |
| 5317 | clear_oparg(&oa); |
| 5318 | oa.regname = (cbd == &clip_plus ? '+' : '*'); |
| 5319 | oa.op_type = OP_YANK; |
| 5320 | vim_memset(&ca, 0, sizeof(ca)); |
| 5321 | ca.oap = &oa; |
| 5322 | ca.cmdchar = 'y'; |
| 5323 | ca.count1 = 1; |
| 5324 | ca.retval = CA_NO_ADJ_OP_END; |
| 5325 | do_pending_operator(&ca, 0, TRUE); |
| 5326 | y_previous = old_y_previous; |
| 5327 | y_current = old_y_current; |
| 5328 | curwin->w_cursor = old_cursor; |
| 5329 | curwin->w_curswant = old_curswant; |
| 5330 | curwin->w_set_curswant = old_set_curswant; |
| 5331 | curbuf->b_op_start = old_op_start; |
| 5332 | curbuf->b_op_end = old_op_end; |
| 5333 | #ifdef FEAT_VISUAL |
| 5334 | VIsual = old_visual; |
| 5335 | VIsual_mode = old_visual_mode; |
| 5336 | #endif |
| 5337 | } |
| 5338 | else |
| 5339 | { |
| 5340 | clip_free_selection(cbd); |
| 5341 | |
| 5342 | /* Try to get selected text from another window */ |
| 5343 | clip_gen_request_selection(cbd); |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | /* Convert from the GUI selection string into the '*'/'+' register */ |
| 5348 | void |
| 5349 | clip_yank_selection(type, str, len, cbd) |
| 5350 | int type; |
| 5351 | char_u *str; |
| 5352 | long len; |
| 5353 | VimClipboard *cbd; |
| 5354 | { |
| 5355 | struct yankreg *y_ptr; |
| 5356 | |
| 5357 | if (cbd == &clip_plus) |
| 5358 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 5359 | else |
| 5360 | y_ptr = &y_regs[STAR_REGISTER]; |
| 5361 | |
| 5362 | clip_free_selection(cbd); |
| 5363 | |
| 5364 | str_to_reg(y_ptr, type, str, len, 0L); |
| 5365 | } |
| 5366 | |
| 5367 | /* |
| 5368 | * Convert the '*'/'+' register into a GUI selection string returned in *str |
| 5369 | * with length *len. |
| 5370 | * Returns the motion type, or -1 for failure. |
| 5371 | */ |
| 5372 | int |
| 5373 | clip_convert_selection(str, len, cbd) |
| 5374 | char_u **str; |
| 5375 | long_u *len; |
| 5376 | VimClipboard *cbd; |
| 5377 | { |
| 5378 | char_u *p; |
| 5379 | int lnum; |
| 5380 | int i, j; |
| 5381 | int_u eolsize; |
| 5382 | struct yankreg *y_ptr; |
| 5383 | |
| 5384 | if (cbd == &clip_plus) |
| 5385 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 5386 | else |
| 5387 | y_ptr = &y_regs[STAR_REGISTER]; |
| 5388 | |
| 5389 | #ifdef USE_CRNL |
| 5390 | eolsize = 2; |
| 5391 | #else |
| 5392 | eolsize = 1; |
| 5393 | #endif |
| 5394 | |
| 5395 | *str = NULL; |
| 5396 | *len = 0; |
| 5397 | if (y_ptr->y_array == NULL) |
| 5398 | return -1; |
| 5399 | |
| 5400 | for (i = 0; i < y_ptr->y_size; i++) |
| 5401 | *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; |
| 5402 | |
| 5403 | /* |
| 5404 | * Don't want newline character at end of last line if we're in MCHAR mode. |
| 5405 | */ |
| 5406 | if (y_ptr->y_type == MCHAR && *len >= eolsize) |
| 5407 | *len -= eolsize; |
| 5408 | |
| 5409 | p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */ |
| 5410 | if (p == NULL) |
| 5411 | return -1; |
| 5412 | lnum = 0; |
| 5413 | for (i = 0, j = 0; i < (int)*len; i++, j++) |
| 5414 | { |
| 5415 | if (y_ptr->y_array[lnum][j] == '\n') |
| 5416 | p[i] = NUL; |
| 5417 | else if (y_ptr->y_array[lnum][j] == NUL) |
| 5418 | { |
| 5419 | #ifdef USE_CRNL |
| 5420 | p[i++] = '\r'; |
| 5421 | #endif |
| 5422 | #ifdef USE_CR |
| 5423 | p[i] = '\r'; |
| 5424 | #else |
| 5425 | p[i] = '\n'; |
| 5426 | #endif |
| 5427 | lnum++; |
| 5428 | j = -1; |
| 5429 | } |
| 5430 | else |
| 5431 | p[i] = y_ptr->y_array[lnum][j]; |
| 5432 | } |
| 5433 | return y_ptr->y_type; |
| 5434 | } |
| 5435 | |
| 5436 | |
| 5437 | # if defined(FEAT_VISUAL) || defined(FEAT_EVAL) |
| 5438 | /* |
| 5439 | * If we have written to a clipboard register, send the text to the clipboard. |
| 5440 | */ |
| 5441 | static void |
| 5442 | may_set_selection() |
| 5443 | { |
| 5444 | if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) |
| 5445 | { |
| 5446 | clip_own_selection(&clip_star); |
| 5447 | clip_gen_set_selection(&clip_star); |
| 5448 | } |
| 5449 | else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) |
| 5450 | { |
| 5451 | clip_own_selection(&clip_plus); |
| 5452 | clip_gen_set_selection(&clip_plus); |
| 5453 | } |
| 5454 | } |
| 5455 | # endif |
| 5456 | |
| 5457 | #endif /* FEAT_CLIPBOARD || PROTO */ |
| 5458 | |
| 5459 | |
| 5460 | #if defined(FEAT_DND) || defined(PROTO) |
| 5461 | /* |
| 5462 | * Replace the contents of the '~' register with str. |
| 5463 | */ |
| 5464 | void |
| 5465 | dnd_yank_drag_data(str, len) |
| 5466 | char_u *str; |
| 5467 | long len; |
| 5468 | { |
| 5469 | struct yankreg *curr; |
| 5470 | |
| 5471 | curr = y_current; |
| 5472 | y_current = &y_regs[TILDE_REGISTER]; |
| 5473 | free_yank_all(); |
| 5474 | str_to_reg(y_current, MCHAR, str, len, 0L); |
| 5475 | y_current = curr; |
| 5476 | } |
| 5477 | #endif |
| 5478 | |
| 5479 | |
| 5480 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5481 | /* |
| 5482 | * Return the type of a register. |
| 5483 | * Used for getregtype() |
| 5484 | * Returns MAUTO for error. |
| 5485 | */ |
| 5486 | char_u |
| 5487 | get_reg_type(regname, reglen) |
| 5488 | int regname; |
| 5489 | long *reglen; |
| 5490 | { |
| 5491 | switch (regname) |
| 5492 | { |
| 5493 | case '%': /* file name */ |
| 5494 | case '#': /* alternate file name */ |
| 5495 | case '=': /* expression */ |
| 5496 | case ':': /* last command line */ |
| 5497 | case '/': /* last search-pattern */ |
| 5498 | case '.': /* last inserted text */ |
| 5499 | #ifdef FEAT_SEARCHPATH |
| 5500 | case Ctrl_F: /* Filename under cursor */ |
| 5501 | case Ctrl_P: /* Path under cursor, expand via "path" */ |
| 5502 | #endif |
| 5503 | case Ctrl_W: /* word under cursor */ |
| 5504 | case Ctrl_A: /* WORD (mnemonic All) under cursor */ |
| 5505 | case '_': /* black hole: always empty */ |
| 5506 | return MCHAR; |
| 5507 | } |
| 5508 | |
| 5509 | #ifdef FEAT_CLIPBOARD |
| 5510 | regname = may_get_selection(regname); |
| 5511 | #endif |
| 5512 | |
| 5513 | /* Should we check for a valid name? */ |
| 5514 | get_yank_register(regname, FALSE); |
| 5515 | |
| 5516 | if (y_current->y_array != NULL) |
| 5517 | { |
| 5518 | #ifdef FEAT_VISUAL |
| 5519 | if (reglen != NULL && y_current->y_type == MBLOCK) |
| 5520 | *reglen = y_current->y_width; |
| 5521 | #endif |
| 5522 | return y_current->y_type; |
| 5523 | } |
| 5524 | return MAUTO; |
| 5525 | } |
| 5526 | |
| 5527 | /* |
| 5528 | * Return the contents of a register as a single allocated string. |
| 5529 | * Used for "@r" in expressions and for getreg(). |
| 5530 | * Returns NULL for error. |
| 5531 | */ |
| 5532 | char_u * |
| 5533 | get_reg_contents(regname, allowexpr) |
| 5534 | int regname; |
| 5535 | int allowexpr; /* allow "=" register. */ |
| 5536 | { |
| 5537 | long i; |
| 5538 | char_u *retval; |
| 5539 | int allocated; |
| 5540 | long len; |
| 5541 | |
| 5542 | /* Don't allow using an expression register inside an expression */ |
| 5543 | if (regname == '=') |
| 5544 | { |
| 5545 | if (allowexpr) |
| 5546 | return get_expr_line(); |
| 5547 | return NULL; |
| 5548 | } |
| 5549 | |
| 5550 | if (regname == '@') /* "@@" is used for unnamed register */ |
| 5551 | regname = '"'; |
| 5552 | |
| 5553 | /* check for valid regname */ |
| 5554 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 5555 | return NULL; |
| 5556 | |
| 5557 | #ifdef FEAT_CLIPBOARD |
| 5558 | regname = may_get_selection(regname); |
| 5559 | #endif |
| 5560 | |
| 5561 | if (get_spec_reg(regname, &retval, &allocated, FALSE)) |
| 5562 | { |
| 5563 | if (retval == NULL) |
| 5564 | return NULL; |
| 5565 | if (!allocated) |
| 5566 | retval = vim_strsave(retval); |
| 5567 | return retval; |
| 5568 | } |
| 5569 | |
| 5570 | get_yank_register(regname, FALSE); |
| 5571 | if (y_current->y_array == NULL) |
| 5572 | return NULL; |
| 5573 | |
| 5574 | /* |
| 5575 | * Compute length of resulting string. |
| 5576 | */ |
| 5577 | len = 0; |
| 5578 | for (i = 0; i < y_current->y_size; ++i) |
| 5579 | { |
| 5580 | len += (long)STRLEN(y_current->y_array[i]); |
| 5581 | /* |
| 5582 | * Insert a newline between lines and after last line if |
| 5583 | * y_type is MLINE. |
| 5584 | */ |
| 5585 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 5586 | ++len; |
| 5587 | } |
| 5588 | |
| 5589 | retval = lalloc(len + 1, TRUE); |
| 5590 | |
| 5591 | /* |
| 5592 | * Copy the lines of the yank register into the string. |
| 5593 | */ |
| 5594 | if (retval != NULL) |
| 5595 | { |
| 5596 | len = 0; |
| 5597 | for (i = 0; i < y_current->y_size; ++i) |
| 5598 | { |
| 5599 | STRCPY(retval + len, y_current->y_array[i]); |
| 5600 | len += (long)STRLEN(retval + len); |
| 5601 | |
| 5602 | /* |
| 5603 | * Insert a NL between lines and after the last line if y_type is |
| 5604 | * MLINE. |
| 5605 | */ |
| 5606 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 5607 | retval[len++] = '\n'; |
| 5608 | } |
| 5609 | retval[len] = NUL; |
| 5610 | } |
| 5611 | |
| 5612 | return retval; |
| 5613 | } |
| 5614 | |
| 5615 | /* |
| 5616 | * Store string "str" in register "name". |
| 5617 | * "maxlen" is the maximum number of bytes to use, -1 for all bytes. |
| 5618 | * If "must_append" is TRUE, always append to the register. Otherwise append |
| 5619 | * if "name" is an uppercase letter. |
| 5620 | * Note: "maxlen" and "must_append" don't work for the "/" register. |
| 5621 | * Careful: 'str' is modified, you may have to use a copy! |
| 5622 | * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. |
| 5623 | */ |
| 5624 | void |
| 5625 | write_reg_contents(name, str, maxlen, must_append) |
| 5626 | int name; |
| 5627 | char_u *str; |
| 5628 | int maxlen; |
| 5629 | int must_append; |
| 5630 | { |
| 5631 | write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); |
| 5632 | } |
| 5633 | |
| 5634 | void |
| 5635 | write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len) |
| 5636 | int name; |
| 5637 | char_u *str; |
| 5638 | int maxlen; |
| 5639 | int must_append; |
| 5640 | int yank_type; |
| 5641 | long block_len; |
| 5642 | { |
| 5643 | struct yankreg *old_y_previous, *old_y_current; |
| 5644 | long len; |
| 5645 | |
| 5646 | /* Special case: '/' search pattern */ |
| 5647 | if (name == '/') |
| 5648 | { |
| 5649 | set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); |
| 5650 | return; |
| 5651 | } |
| 5652 | |
| 5653 | if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */ |
| 5654 | { |
| 5655 | EMSG2(_("E354: Invalid register name: '%s'"), transchar(name)); |
| 5656 | return; |
| 5657 | } |
| 5658 | |
| 5659 | if (name == '_') /* black hole: nothing to do */ |
| 5660 | return; |
| 5661 | |
| 5662 | /* Don't want to change the current (unnamed) register */ |
| 5663 | old_y_previous = y_previous; |
| 5664 | old_y_current = y_current; |
| 5665 | |
| 5666 | get_yank_register(name, TRUE); |
| 5667 | if (!y_append && !must_append) |
| 5668 | free_yank_all(); |
| 5669 | if (maxlen >= 0) |
| 5670 | len = maxlen; |
| 5671 | else |
| 5672 | len = (long)STRLEN(str); |
| 5673 | #ifndef FEAT_VISUAL |
| 5674 | /* Just in case - make sure we don't use MBLOCK */ |
| 5675 | if (yank_type == MBLOCK) |
| 5676 | yank_type = MAUTO; |
| 5677 | #endif |
| 5678 | if (yank_type == MAUTO) |
| 5679 | yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r')) |
| 5680 | ? MLINE : MCHAR); |
| 5681 | str_to_reg(y_current, yank_type, str, len, block_len); |
| 5682 | |
| 5683 | # ifdef FEAT_CLIPBOARD |
| 5684 | /* Send text of clipboard register to the clipboard. */ |
| 5685 | may_set_selection(); |
| 5686 | # endif |
| 5687 | |
| 5688 | /* ':let @" = "val"' should change the meaning of the "" register */ |
| 5689 | if (name != '"') |
| 5690 | y_previous = old_y_previous; |
| 5691 | y_current = old_y_current; |
| 5692 | } |
| 5693 | #endif /* FEAT_EVAL */ |
| 5694 | |
| 5695 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 5696 | /* |
| 5697 | * Put a string into a register. When the register is not empty, the string |
| 5698 | * is appended. |
| 5699 | */ |
| 5700 | static void |
| 5701 | str_to_reg(y_ptr, type, str, len, blocklen) |
| 5702 | struct yankreg *y_ptr; /* pointer to yank register */ |
| 5703 | int type; /* MCHAR, MLINE or MBLOCK */ |
| 5704 | char_u *str; /* string to put in register */ |
| 5705 | long len; /* length of string */ |
| 5706 | long blocklen; /* width of Visual block */ |
| 5707 | { |
| 5708 | int lnum; |
| 5709 | long start; |
| 5710 | long i; |
| 5711 | int extra; |
| 5712 | int newlines; /* number of lines added */ |
| 5713 | int extraline = 0; /* extra line at the end */ |
| 5714 | int append = FALSE; /* append to last line in register */ |
| 5715 | char_u *s; |
| 5716 | char_u **pp; |
| 5717 | #ifdef FEAT_VISUAL |
| 5718 | long maxlen; |
| 5719 | #endif |
| 5720 | |
| 5721 | if (y_ptr->y_array == NULL) /* NULL means emtpy register */ |
| 5722 | y_ptr->y_size = 0; |
| 5723 | |
| 5724 | /* |
| 5725 | * Count the number of lines within the string |
| 5726 | */ |
| 5727 | newlines = 0; |
| 5728 | for (i = 0; i < len; i++) |
| 5729 | if (str[i] == '\n') |
| 5730 | ++newlines; |
| 5731 | if (type == MCHAR || len == 0 || str[len - 1] != '\n') |
| 5732 | { |
| 5733 | extraline = 1; |
| 5734 | ++newlines; /* count extra newline at the end */ |
| 5735 | } |
| 5736 | if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) |
| 5737 | { |
| 5738 | append = TRUE; |
| 5739 | --newlines; /* uncount newline when appending first line */ |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * Allocate an array to hold the pointers to the new register lines. |
| 5744 | * If the register was not empty, move the existing lines to the new array. |
| 5745 | */ |
| 5746 | pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines) |
| 5747 | * sizeof(char_u *), TRUE); |
| 5748 | if (pp == NULL) /* out of memory */ |
| 5749 | return; |
| 5750 | for (lnum = 0; lnum < y_ptr->y_size; ++lnum) |
| 5751 | pp[lnum] = y_ptr->y_array[lnum]; |
| 5752 | vim_free(y_ptr->y_array); |
| 5753 | y_ptr->y_array = pp; |
| 5754 | #ifdef FEAT_VISUAL |
| 5755 | maxlen = 0; |
| 5756 | #endif |
| 5757 | |
| 5758 | /* |
| 5759 | * Find the end of each line and save it into the array. |
| 5760 | */ |
| 5761 | for (start = 0; start < len + extraline; start += i + 1) |
| 5762 | { |
| 5763 | for (i = start; i < len; ++i) /* find the end of the line */ |
| 5764 | if (str[i] == '\n') |
| 5765 | break; |
| 5766 | i -= start; /* i is now length of line */ |
| 5767 | #ifdef FEAT_VISUAL |
| 5768 | if (i > maxlen) |
| 5769 | maxlen = i; |
| 5770 | #endif |
| 5771 | if (append) |
| 5772 | { |
| 5773 | --lnum; |
| 5774 | extra = (int)STRLEN(y_ptr->y_array[lnum]); |
| 5775 | } |
| 5776 | else |
| 5777 | extra = 0; |
| 5778 | s = alloc((unsigned)(i + extra + 1)); |
| 5779 | if (s == NULL) |
| 5780 | break; |
| 5781 | if (extra) |
| 5782 | mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); |
| 5783 | if (append) |
| 5784 | vim_free(y_ptr->y_array[lnum]); |
| 5785 | if (i) |
| 5786 | mch_memmove(s + extra, str + start, (size_t)i); |
| 5787 | extra += i; |
| 5788 | s[extra] = NUL; |
| 5789 | y_ptr->y_array[lnum++] = s; |
| 5790 | while (--extra >= 0) |
| 5791 | { |
| 5792 | if (*s == NUL) |
| 5793 | *s = '\n'; /* replace NUL with newline */ |
| 5794 | ++s; |
| 5795 | } |
| 5796 | append = FALSE; /* only first line is appended */ |
| 5797 | } |
| 5798 | y_ptr->y_type = type; |
| 5799 | y_ptr->y_size = lnum; |
| 5800 | # ifdef FEAT_VISUAL |
| 5801 | if (type == MBLOCK) |
| 5802 | y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); |
| 5803 | else |
| 5804 | y_ptr->y_width = 0; |
| 5805 | # endif |
| 5806 | } |
| 5807 | #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */ |
| 5808 | |
| 5809 | void |
| 5810 | clear_oparg(oap) |
| 5811 | oparg_T *oap; |
| 5812 | { |
| 5813 | vim_memset(oap, 0, sizeof(oparg_T)); |
| 5814 | } |
| 5815 | |
| 5816 | static long line_count_info __ARGS((char_u *line, long *wc, long limit, int eol_size)); |
| 5817 | |
| 5818 | /* |
| 5819 | * Count the number of characters and "words" in a line. |
| 5820 | * |
| 5821 | * "Words" are counted by looking for boundaries between non-space and |
| 5822 | * space characters. (it seems to produce results that match 'wc'.) |
| 5823 | * |
| 5824 | * Return value is character count; word count for the line is ADDED |
| 5825 | * to "*wc". |
| 5826 | * |
| 5827 | * The function will only examine the first "limit" characters in the |
| 5828 | * line, stopping if it encounters an end-of-line (NUL byte). In that |
| 5829 | * case, eol_size will be added to the character count to account for |
| 5830 | * the size of the EOL character. |
| 5831 | */ |
| 5832 | static long |
| 5833 | line_count_info(line, wc, limit, eol_size) |
| 5834 | char_u *line; |
| 5835 | long *wc; |
| 5836 | long limit; |
| 5837 | int eol_size; |
| 5838 | { |
| 5839 | long i, words = 0; |
| 5840 | int is_word = 0; |
| 5841 | |
| 5842 | for (i = 0; line[i] && i < limit; i++) |
| 5843 | { |
| 5844 | if (is_word) |
| 5845 | { |
| 5846 | if (vim_isspace(line[i])) |
| 5847 | { |
| 5848 | words++; |
| 5849 | is_word = 0; |
| 5850 | } |
| 5851 | } |
| 5852 | else if (!vim_isspace(line[i])) |
| 5853 | is_word = 1; |
| 5854 | } |
| 5855 | |
| 5856 | if (is_word) |
| 5857 | words++; |
| 5858 | *wc += words; |
| 5859 | |
| 5860 | /* Add eol_size if the end of line was reached before hitting limit. */ |
| 5861 | if (!line[i] && i < limit) |
| 5862 | i += eol_size; |
| 5863 | return i; |
| 5864 | } |
| 5865 | |
| 5866 | /* |
| 5867 | * Give some info about the position of the cursor (for "g CTRL-G"). |
| 5868 | * In Visual mode, give some info about the selected region. (In this case, |
| 5869 | * the *_count_cursor variables store running totals for the selection.) |
| 5870 | */ |
| 5871 | void |
| 5872 | cursor_pos_info() |
| 5873 | { |
| 5874 | char_u *p; |
| 5875 | char_u buf1[20]; |
| 5876 | char_u buf2[20]; |
| 5877 | linenr_T lnum; |
| 5878 | long char_count = 0; |
| 5879 | long char_count_cursor = 0; |
| 5880 | int eol_size; |
| 5881 | long last_check = 100000L; |
| 5882 | long word_count = 0; |
| 5883 | long word_count_cursor = 0; |
| 5884 | #ifdef FEAT_VISUAL |
| 5885 | long line_count_selected = 0; |
| 5886 | pos_T min_pos, max_pos; |
| 5887 | oparg_T oparg; |
| 5888 | struct block_def bd; |
| 5889 | #endif |
| 5890 | |
| 5891 | /* |
| 5892 | * Compute the length of the file in characters. |
| 5893 | */ |
| 5894 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 5895 | { |
| 5896 | MSG(_(no_lines_msg)); |
| 5897 | } |
| 5898 | else |
| 5899 | { |
| 5900 | if (get_fileformat(curbuf) == EOL_DOS) |
| 5901 | eol_size = 2; |
| 5902 | else |
| 5903 | eol_size = 1; |
| 5904 | |
| 5905 | #ifdef FEAT_VISUAL |
| 5906 | if (VIsual_active) |
| 5907 | { |
| 5908 | if (lt(VIsual, curwin->w_cursor)) |
| 5909 | { |
| 5910 | min_pos = VIsual; |
| 5911 | max_pos = curwin->w_cursor; |
| 5912 | } |
| 5913 | else |
| 5914 | { |
| 5915 | min_pos = curwin->w_cursor; |
| 5916 | max_pos = VIsual; |
| 5917 | } |
| 5918 | if (*p_sel == 'e' && max_pos.col > 0) |
| 5919 | --max_pos.col; |
| 5920 | |
| 5921 | if (VIsual_mode == Ctrl_V) |
| 5922 | { |
| 5923 | oparg.is_VIsual = 1; |
| 5924 | oparg.block_mode = TRUE; |
| 5925 | oparg.op_type = OP_NOP; |
| 5926 | getvcols(curwin, &min_pos, &max_pos, |
| 5927 | &oparg.start_vcol, &oparg.end_vcol); |
| 5928 | /* Swap the start, end vcol if needed */ |
| 5929 | if (oparg.end_vcol < oparg.start_vcol) |
| 5930 | { |
| 5931 | oparg.end_vcol += oparg.start_vcol; |
| 5932 | oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; |
| 5933 | oparg.end_vcol -= oparg.start_vcol; |
| 5934 | } |
| 5935 | } |
| 5936 | line_count_selected = max_pos.lnum - min_pos.lnum + 1; |
| 5937 | } |
| 5938 | #endif |
| 5939 | |
| 5940 | for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) |
| 5941 | { |
| 5942 | /* Check for a CTRL-C every 100000 characters. */ |
| 5943 | if (char_count > last_check) |
| 5944 | { |
| 5945 | ui_breakcheck(); |
| 5946 | if (got_int) |
| 5947 | return; |
| 5948 | last_check = char_count + 100000L; |
| 5949 | } |
| 5950 | |
| 5951 | #ifdef FEAT_VISUAL |
| 5952 | /* Do extra processing for VIsual mode. */ |
| 5953 | if (VIsual_active |
| 5954 | && lnum >= min_pos.lnum && lnum <= max_pos.lnum) |
| 5955 | { |
| 5956 | switch (VIsual_mode) |
| 5957 | { |
| 5958 | case Ctrl_V: |
| 5959 | # ifdef FEAT_VIRTUALEDIT |
| 5960 | virtual_op = virtual_active(); |
| 5961 | # endif |
| 5962 | block_prep(&oparg, &bd, lnum, 0); |
| 5963 | # ifdef FEAT_VIRTUALEDIT |
| 5964 | virtual_op = MAYBE; |
| 5965 | # endif |
| 5966 | char_count_cursor += line_count_info(bd.textstart, |
| 5967 | &word_count_cursor, (long)bd.textlen, eol_size); |
| 5968 | break; |
| 5969 | case 'V': |
| 5970 | char_count_cursor += line_count_info(ml_get(lnum), |
| 5971 | &word_count_cursor, (long)MAXCOL, eol_size); |
| 5972 | break; |
| 5973 | case 'v': |
| 5974 | { |
| 5975 | colnr_T start_col = (lnum == min_pos.lnum) |
| 5976 | ? min_pos.col : 0; |
| 5977 | colnr_T end_col = (lnum == max_pos.lnum) |
| 5978 | ? max_pos.col - start_col + 1 : MAXCOL; |
| 5979 | |
| 5980 | char_count_cursor += |
| 5981 | line_count_info(ml_get(lnum) + start_col, |
| 5982 | &word_count_cursor, (long)end_col, eol_size); |
| 5983 | } |
| 5984 | break; |
| 5985 | } |
| 5986 | } |
| 5987 | else |
| 5988 | #endif |
| 5989 | { |
| 5990 | /* In non-visual mode, check for the line the cursor is on */ |
| 5991 | if (lnum == curwin->w_cursor.lnum) |
| 5992 | { |
| 5993 | word_count_cursor += word_count; |
| 5994 | char_count_cursor = char_count + |
| 5995 | line_count_info(ml_get(lnum), &word_count_cursor, |
| 5996 | (long)(curwin->w_cursor.col + 1), eol_size); |
| 5997 | } |
| 5998 | } |
| 5999 | /* Add to the running totals */ |
| 6000 | char_count += line_count_info(ml_get(lnum), &word_count, |
| 6001 | (long)MAXCOL, eol_size); |
| 6002 | } |
| 6003 | |
| 6004 | /* Correction for when last line doesn't have an EOL. */ |
| 6005 | if (!curbuf->b_p_eol && curbuf->b_p_bin) |
| 6006 | char_count -= eol_size; |
| 6007 | |
| 6008 | #ifdef FEAT_VISUAL |
| 6009 | if (VIsual_active) |
| 6010 | { |
| 6011 | if (VIsual_mode == Ctrl_V) |
| 6012 | { |
| 6013 | getvcols(curwin, &min_pos, &max_pos, &min_pos.col, |
| 6014 | &max_pos.col); |
| 6015 | sprintf((char *)buf1, _("%ld Cols; "), |
| 6016 | (long)(oparg.end_vcol - oparg.start_vcol + 1)); |
| 6017 | } |
| 6018 | else |
| 6019 | buf1[0] = NUL; |
| 6020 | |
| 6021 | sprintf((char *)IObuff, |
| 6022 | _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"), |
| 6023 | buf1, line_count_selected, |
| 6024 | (long)curbuf->b_ml.ml_line_count, |
| 6025 | word_count_cursor, word_count, |
| 6026 | char_count_cursor, char_count); |
| 6027 | } |
| 6028 | else |
| 6029 | #endif |
| 6030 | { |
| 6031 | p = ml_get_curline(); |
| 6032 | validate_virtcol(); |
| 6033 | col_print(buf1, (int)curwin->w_cursor.col + 1, |
| 6034 | (int)curwin->w_virtcol + 1); |
| 6035 | col_print(buf2, (int)STRLEN(p), linetabsize(p)); |
| 6036 | |
| 6037 | sprintf((char *)IObuff, |
| 6038 | _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"), |
| 6039 | (char *)buf1, (char *)buf2, |
| 6040 | (long)curwin->w_cursor.lnum, |
| 6041 | (long)curbuf->b_ml.ml_line_count, |
| 6042 | word_count_cursor, word_count, |
| 6043 | char_count_cursor, char_count); |
| 6044 | } |
| 6045 | |
| 6046 | #ifdef FEAT_MBYTE |
| 6047 | char_count = bomb_size(); |
| 6048 | if (char_count > 0) |
| 6049 | sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"), |
| 6050 | char_count); |
| 6051 | #endif |
| 6052 | /* Don't shorten this message, the user asked for it. */ |
| 6053 | p = p_shm; |
| 6054 | p_shm = (char_u *)""; |
| 6055 | msg(IObuff); |
| 6056 | p_shm = p; |
| 6057 | } |
| 6058 | } |