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