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