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