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