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