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