blob: f53567bc104f7cb6011a61efc6c83d74ae14a65e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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 */
55static 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 Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061} y_regs[NUM_REGISTERS];
62
63static struct yankreg *y_current; /* ptr to current yankreg */
64static int y_append; /* TRUE when appending */
65static 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 */
71struct block_def
72{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000073 int startspaces; /* 'extra' cols before first char */
74 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000076 char_u *textstart; /* pointer to 1st char (partially) in block */
77 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 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
92static void shift_block __ARGS((oparg_T *oap, int amount));
93static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000096static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000097static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
98 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +000099static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
101static void mb_adjust_opend __ARGS((oparg_T *oap));
102#endif
103static void free_yank __ARGS((long));
104static void free_yank_all __ARGS((void));
105static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
106#ifdef FEAT_CLIPBOARD
107static void copy_yank_reg __ARGS((struct yankreg *reg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static void may_set_selection __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
112static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar0eac8282014-04-12 12:26:36 +0200116static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118static int ends_in_white __ARGS((linenr_T lnum));
119#ifdef FEAT_COMMENTS
120static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
121static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
122#else
123static 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 */
131static 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 Moolenaar83c465c2005-12-16 21:53:56 +0000160 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
168get_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 Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Return TRUE if operator "op" always works on whole lines.
186 */
187 int
188op_on_lines(op)
189 int op;
190{
191 return opchars[op][2];
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
195 * Get first operator command character.
196 * Returns 'g' or 'z' if there is another command character.
197 */
198 int
199get_op_char(optype)
200 int optype;
201{
202 return opchars[optype][0];
203}
204
205/*
206 * Get second operator command character.
207 */
208 int
209get_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
219op_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 Moolenaar071d4272004-06-13 20:20:40 +0000227 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 if (u_save((linenr_T)(oap->start.lnum - 1),
230 (linenr_T)(oap->end.lnum + 1)) == FAIL)
231 return;
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (oap->block_mode)
234 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
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 Moolenaar21b17e72008-01-16 19:03:13 +0000252 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254 ++curwin->w_cursor.lnum;
255 }
256
257 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200258#ifdef FEAT_FOLDING
259 /* The cursor line is not in a closed fold */
260 foldOpenCursor();
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (oap->block_mode)
264 {
265 curwin->w_cursor.lnum = oap->start.lnum;
266 curwin->w_cursor.col = block_col;
267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100268 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
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 Moolenaar21b17e72008-01-16 19:03:13 +0000316shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 int left;
318 int round;
319 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000320 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int count;
323 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100324 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
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 Moolenaar21b17e72008-01-16 19:03:13 +0000359 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000362 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
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
371shift_block(oap, amount)
372 oparg_T *oap;
373 int amount;
374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#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 Moolenaar0fa313a2005-08-10 21:07:57 +0000416 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 for ( ; vim_iswhite(*bd.textstart); )
422 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200423 /* 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 Moolenaar071d4272004-06-13 20:20:40 +0000426 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 Moolenaar2536d4f2015-07-17 13:22:51 +0200445 vim_memset(newp + bd.textcol, TAB, (size_t)i);
446 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 /* the end */
448 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
449 }
450 else /* left */
451 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 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 Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466 /*
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 Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /* 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 Moolenaar597a4222014-06-25 14:39:50 +0200485 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000486 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 }
488
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 block_space_width = non_white_col - oap->start_vcol;
490 /* We will shift by "total" or "block_space_width", whichever is less.
491 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000492 shift_amount = (block_space_width < (size_t)total
493 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494
495 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000496 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
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 Moolenaar597a4222014-06-25 14:39:50 +0200510 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 Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 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 Moolenaar071d4272004-06-13 20:20:40 +0000535 if (newp == NULL)
536 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200538 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000539 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
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
558block_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 Moolenaarb5cf6c32014-08-16 18:36:43 +0200612#ifdef FEAT_MBYTE
613 if (has_mbyte && spaces > 0)
614 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100615 int off;
616
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200617 /* Avoid starting halfway a multi-byte character. */
618 if (b_insert)
619 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100620 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200621 }
622 else
623 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100624 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200625 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200626 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100627 spaces -= off;
628 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200629 }
630#endif
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 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 Moolenaar2536d4f2015-07-17 13:22:51 +0200641 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
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 Moolenaar2536d4f2015-07-17 13:22:51 +0200650 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /* 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 Moolenaarc1a11ed2008-06-24 22:09:24 +0000659 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
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
683op_reindent(oap, how)
684 oparg_T *oap;
685 int (*how) __ARGS((void));
686{
687 long i;
688 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200689 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 linenr_T first_changed = 0;
691 linenr_T last_changed = 0;
692 linenr_T start_lnum = curwin->w_cursor.lnum;
693
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000694 /* Don't even try when 'modifiable' is off. */
695 if (!curbuf->b_p_ma)
696 {
697 EMSG(_(e_modifiable));
698 return;
699 }
700
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 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 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200722 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200724 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200726 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
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 Moolenaarddfc9782008-02-25 20:55:22 +0000735 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
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 Moolenaar071d4272004-06-13 20:20:40 +0000747 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 else if (oap->is_VIsual)
750 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
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 */
770static 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
777get_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
796set_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 *
808get_expr_line()
809{
810 char_u *expr_copy;
811 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000812 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
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 Moolenaar21bffa72006-10-06 21:33:16 +0000823 /* 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 Moolenaar362e1a32006-03-06 23:29:24 +0000829 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000830 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 vim_free(expr_copy);
832 return rv;
833}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000834
835/*
836 * Get the '=' register expression itself, without evaluating it.
837 */
838 char_u *
839get_expr_line_src()
840{
841 if (expr_line == NULL)
842 return NULL;
843 return vim_strsave(expr_line);
844}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#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
852valid_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 Moolenaar3b3a9492015-01-27 18:44:16 +0100859 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100861 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100864 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 || 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 Moolenaar677ee682005-01-27 14:41:15 +0000883 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 *
885 * If regname is 0 and writing, use register 0
886 * If regname is 0 and reading, use previous register
887 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000888 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889get_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 Moolenaar8299df92004-07-10 09:47:34 +0000932#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
934 * When "regname" is a clipboard register, obtain the selection. If it's not
935 * available return zero, otherwise return "regname".
936 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938may_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 Moolenaar071d4272004-06-13 20:20:40 +0000959/*
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 *
964get_register(name, copy)
965 int name;
966 int copy; /* make a copy, if FALSE make register empty. */
967{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000968 struct yankreg *reg;
969 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971#ifdef FEAT_CLIPBOARD
972 /* When Visual area changed, may have to update selection. Obtain the
973 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000974 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200976 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 Moolenaar071d4272004-06-13 20:20:40 +0000984 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 Moolenaar0a307462007-12-01 20:13:05 +00001014 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 */
1016 void
1017put_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 Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
1032 void
1033free_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 Moolenaar071d4272004-06-13 20:20:40 +00001044
1045#if defined(FEAT_MOUSE) || defined(PROTO)
1046/*
1047 * return TRUE if the current yank register has type MLINE
1048 */
1049 int
1050yank_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 Moolenaard55de222007-05-06 13:38:48 +00001063 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
1068do_record(c)
1069 int c;
1070{
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 char_u *p;
1072 static int regname;
1073 struct yankreg *old_y_previous, *old_y_current;
1074 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
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 Moolenaard55de222007-05-06 13:38:48 +00001092 * 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 Moolenaar071d4272004-06-13 20:20:40 +00001095 */
1096 Recording = FALSE;
1097 MSG("");
1098 p = get_recorded();
1099 if (p == NULL)
1100 retval = FAIL;
1101 else
1102 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001103 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1104 vim_unescape_csi(p);
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
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
1129stuff_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 Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * execute a yank register: copy it into the stuff buffer
1183 *
1184 * return FAIL for failure, OK otherwise
1185 */
1186 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001187do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 int regname;
1189 int colon; /* insert ':' before each line */
1190 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001191 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 long i;
1194 char_u *p;
1195 int retval = OK;
1196 int remap;
1197
1198 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001200 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001201 {
1202 EMSG(_("E748: No previously used register"));
1203 return FAIL;
1204 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001205 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 /* check for valid regname */
1208 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
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 Moolenaar2df6dcc2004-07-12 15:53:54 +00001232 /* Escape all control characters with a CTRL-V */
1233 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001234 (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 Moolenaar2df6dcc2004-07-12 15:53:54 +00001235 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001236 {
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 Moolenaar7f6ca072007-02-27 16:21:38 +00001240 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001241 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001242 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001243 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001244 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246#endif
1247#ifdef FEAT_EVAL
1248 else if (regname == '=')
1249 {
1250 p = get_expr_line();
1251 if (p == NULL)
1252 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001253 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 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 Moolenaar7f6ca072007-02-27 16:21:38 +00001265 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 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 Moolenaard333d1e2006-11-07 17:43:47 +00001280 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 for (i = y_current->y_size; --i >= 0; )
1282 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 char_u *escaped;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 /* 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 Moolenaard333d1e2006-11-07 17:43:47 +00001289 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return FAIL;
1291 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001292 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 Moolenaar071d4272004-06-13 20:20:40 +00001298 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001299 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 == 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 Moolenaard333d1e2006-11-07 17:43:47 +00001313put_reedit_in_typebuf(silent)
1314 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
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 Moolenaard333d1e2006-11-07 17:43:47 +00001331 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 restart_edit = NUL;
1333 }
1334}
1335
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001336/*
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 Moolenaar071d4272004-06-13 20:20:40 +00001342 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001343put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001345 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001347 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int retval = OK;
1350
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001351 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001355 {
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 Moolenaar38ef43b2010-01-27 16:31:13 +01001365 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1366 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001367 if (esc)
1368 vim_free(p);
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001371 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 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
1382insert_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
1446stuffescaped(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 Moolenaar3b1c4852010-07-31 17:59:29 +02001474 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 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 Moolenaard55de222007-05-06 13:38:48 +00001486 * If "regname" is a special register, return TRUE and store a pointer to its
1487 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001489 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490get_spec_reg(regname, argp, allocated, errmsg)
1491 int regname;
1492 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001493 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 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 Moolenaard1f56e62006-02-22 21:25:37 +00001544 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 *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 Moolenaar8299df92004-07-10 09:47:34 +00001568 * 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 Moolenaar071d4272004-06-13 20:20:40 +00001571 * 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 Moolenaar1769d5a2006-10-17 14:25:24 +00001577cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 int regname;
1579 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001580 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
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 Moolenaar1769d5a2006-10-17 14:25:24 +00001592 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00001599 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 Moolenaar071d4272004-06-13 20:20:40 +00001610#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
1616adjust_clip_reg(rp)
1617 int *rp;
1618{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001619 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1620 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001621 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 Moolenaarbf9680e2010-12-02 21:43:16 +01001625 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001626 else
1627 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1628 ? '+' : '*';
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!clip_star.available && *rp == '*')
1631 *rp = 0;
1632 if (!clip_plus.available && *rp == '+')
1633 *rp = 0;
1634}
1635#endif
1636
1637/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001638 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001640 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
1642 int
1643op_delete(oap)
1644 oparg_T *oap;
1645{
1646 int n;
1647 linenr_T lnum;
1648 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 char_u *newp, *oldp;
1650 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1652 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001653 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
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 Moolenaard04b7502010-07-08 22:27:55 +02001677 /*
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 Moolenaar071d4272004-06-13 20:20:40 +00001682 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001684 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001686 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && oap->op_type == OP_DELETE)
1688 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001689 ptr = ml_get(oap->end.lnum) + oap->end.col;
1690 if (*ptr != NUL)
1691 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ptr = skipwhite(ptr);
1693 if (*ptr == NUL && inindent(0))
1694 oap->motion_type = MLINE;
1695 }
1696
Bram Moolenaard04b7502010-07-08 22:27:55 +02001697 /*
1698 * Check for trying to delete (e.g. "D") in an empty line.
1699 * Note: For the change operator it is ok.
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 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 Moolenaar87e25fd2005-07-27 21:13:01 +00001707 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * 'cpoptions' (Vi compatible).
1709 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001710#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 Moolenaar071d4272004-06-13 20:20:40 +00001716 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1717 beep_flush();
1718 return OK;
1719 }
1720
Bram Moolenaard04b7502010-07-08 22:27:55 +02001721 /*
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 Moolenaar071d4272004-06-13 20:20:40 +00001726 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 Moolenaar7c821302012-09-05 14:18:45 +02001744 * Use the register name from before adjust_clip_reg() may have
1745 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001747 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 || 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 Moolenaar84298db2012-04-20 13:46:08 +02001760 /* 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 Moolenaarbfe3bf82012-06-13 17:28:55 +02001764 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1765 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001766#endif
1767 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && 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 Moolenaard04b7502010-07-08 22:27:55 +02001798 /*
1799 * block mode delete
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 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 Moolenaar2536d4f2015-07-17 13:22:51 +02001834 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 (size_t)(bd.startspaces + bd.endspaces));
1836 /* copy the part after the deleted part */
1837 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001838 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* 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 Moolenaarf7ff6e82014-03-23 15:13:05 +01001848 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
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 Moolenaarf7ff6e82014-03-23 15:13:05 +01001932 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 && oap->op_type == OP_CHANGE
1934 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001935 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 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 Moolenaard009e862015-06-09 20:20:03 +02001962 (void)del_bytes((long)n, !virtual_op,
1963 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
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 Moolenaard009e862015-06-09 20:20:03 +02001980 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001981 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001982 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 Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 }
1989
1990 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992#ifdef FEAT_VIRTUALEDIT
1993setmarks:
1994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 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 Moolenaar071d4272004-06-13 20:20:40 +00002001 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
2013mb_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
2031op_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 Moolenaard9820532013-11-04 01:41:17 +01002042 char_u *after_p = NULL;
2043 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2046 return OK; /* nothing to do */
2047
Bram Moolenaard9820532013-11-04 01:41:17 +01002048 if (had_ctrl_v_cr)
2049 c = (c == -1 ? '\r' : '\n');
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#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 Moolenaara1381de2009-11-03 15:44:21 +00002068 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 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 Moolenaara1381de2009-11-03 15:44:21 +00002084 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 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 Moolenaar2536d4f2015-07-17 13:22:51 +02002135 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002137 /* -1/-2 is used for entering CR literally. */
2138 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002140#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 Moolenaar2536d4f2015-07-17 13:22:51 +02002149 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002150 if (!bd.is_short)
2151 {
2152 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002153 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002154 /* copy the part after the changed part */
2155 STRMOVE(newp + STRLEN(newp), oldp);
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002160 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002161 after_p = alloc_check(
2162 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002163 if (after_p != NULL)
2164 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 /* replace the line */
2167 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002168 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 Moolenaar071d4272004-06-13 20:20:40 +00002175 }
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 Moolenaardb813952013-03-07 18:50:57 +01002203 if (curwin->w_cursor.lnum == oap->end.lnum)
2204 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 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 Moolenaarb44df0a2008-01-22 15:02:31 +00002276static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2280 */
2281 void
2282op_tilde(oap)
2283 oparg_T *oap;
2284{
2285 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002287 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
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 Moolenaar071d4272004-06-13 20:20:40 +00002294 if (oap->block_mode) /* Visual block mode */
2295 {
2296 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2297 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002298 int one_change;
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 block_prep(oap, &bd, pos.lnum, FALSE);
2301 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002302 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2303 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002304
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002305#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002306 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2309
Bram Moolenaar009b2592004-10-24 19:18:58 +00002310 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2311 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002313 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 if (did_change)
2318 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2319 }
2320 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
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 Moolenaare60c58d2008-02-06 13:44:43 +00002333 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 Moolenaar071d4272004-06-13 20:20:40 +00002345 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 Moolenaarb26e6322010-05-22 21:34:09 +02002350 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
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 Moolenaara93fa7e2006-04-17 22:14:47 +00002359 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002360 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002362 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 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 Moolenaar009b2592004-10-24 19:18:58 +00002368 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002370 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372#endif
2373 }
2374 }
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (!did_change && oap->is_VIsual)
2377 /* No change: need to remove the Visual selection */
2378 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
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 Moolenaarb44df0a2008-01-22 15:02:31 +00002396 * 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
2403swapchars(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 Moolenaarb44df0a2008-01-22 15:02:31 +00002414 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002415 {
2416 int len = (*mb_ptr2len)(ml_get_pos(pos));
2417
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002418 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002419 if (len > 0)
2420 todo -= len - 1;
2421 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002422# endif
2423 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002424 if (inc(pos) == -1) /* at end of file */
2425 break;
2426 }
2427 return did_change;
2428}
2429
2430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * 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
2438swapchar(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 Moolenaarb44df0a2008-01-22 15:02:31 +00002452 if (op_type == OP_UPPER && c == 0xdf
2453 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002454 {
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 Moolenaar071d4272004-06-13 20:20:40 +00002466 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 Moolenaard1cb65e2010-08-01 14:22:48 +02002492 /* don't use del_char(), it also removes composing chars */
2493 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 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
2510op_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 Moolenaarf2c03d72015-02-03 18:36:44 +01002518 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
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 Moolenaar6a584dc2006-06-20 18:29:34 +00002583 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
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 Moolenaarf2c03d72015-02-03 18:36:44 +01002592 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 edit(NUL, FALSE, (linenr_T)count1);
2594
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002595 /* 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 Moolenaaraacbb002008-01-03 15:34:40 +00002602 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00002606 return;
2607
2608 if (oap->block_mode)
2609 {
2610 struct block_def bd2;
2611
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002612 /* The user may have moved the cursor before inserting something, try
2613 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002614 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 {
2616 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002617 && 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 Moolenaar3f75e422013-11-11 01:29:22 +01002626 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002627 int t = getviscol2(curbuf->b_op_start_orig.col,
2628 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002629 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002630 pre_textlen -= t - oap->start_vcol;
2631 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002632 }
2633 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002634 && 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 Moolenaar3f75e422013-11-11 01:29:22 +01002643 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002644 int t = getviscol2(curbuf->b_op_start_orig.col,
2645 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002646 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002647 /* reset pre_textlen to the value of OP_INSERT */
2648 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002649 pre_textlen -= t - oap->start_vcol;
2650 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002651 oap->op_type = OP_INSERT;
2652 }
2653 }
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /*
2656 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002657 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * 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 Moolenaar5e4b9e92012-03-23 14:16:23 +01002680 if (pre_textlen >= 0
2681 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
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
2707op_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 Moolenaar53241da2007-09-13 20:41:32 +00002715 long ins_len;
2716 long pre_textlen = 0;
2717 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 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 Moolenaar53241da2007-09-13 20:41:32 +00002762 firstline = ml_get(oap->start.lnum);
2763 pre_textlen = (long)STRLEN(firstline);
2764 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 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 Moolenaar1cd871b2004-12-19 22:46:22 +00002778 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002780 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002782 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002784 /* Auto-indenting may have changed the indent. If the cursor was past
2785 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002787 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002789 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 Moolenaar071d4272004-06-13 20:20:40 +00002800 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2801 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002802 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 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 Moolenaara1381de2009-11-03 15:44:21 +00002816 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
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 Moolenaar2536d4f2015-07-17 13:22:51 +02002834 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 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 Moolenaarc1a11ed2008-06-24 22:09:24 +00002840 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 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
2860init_yank()
2861{
2862 int i;
2863
2864 for (i = 0; i < NUM_REGISTERS; ++i)
2865 y_regs[i].y_array = NULL;
2866}
2867
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002868#if defined(EXITFREE) || defined(PROTO)
2869 void
2870clear_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 Moolenaar071d4272004-06-13 20:20:40 +00002883/*
2884 * Free "n" lines from the current yank register.
2885 * Called for normal freeing and in case of error.
2886 */
2887 static void
2888free_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
2923free_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 Moolenaar1a4a75c2013-07-28 16:03:06 +02002934 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 int
2937op_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 Moolenaar9c52c3a2010-12-08 14:23:15 +01002954#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002955 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
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 Moolenaar2c7a29c2005-12-12 22:02:31 +00002984 /*
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 Moolenaar071d4272004-06-13 20:20:40 +00002988 if ( oap->motion_type == MCHAR
2989 && oap->start.col == 0
2990 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002992 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 && 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 Moolenaar071d4272004-06-13 20:20:40 +00003003 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 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 Moolenaar071d4272004-06-13 20:20:40 +00003016 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 Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3027 {
3028 switch (y_current->y_type)
3029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 case MBLOCK:
3031 block_prep(oap, &bd, lnum, FALSE);
3032 if (yank_copy_line(&bd, y_idx) == FAIL)
3033 goto fail;
3034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
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 Moolenaar18e00d22012-05-18 12:49:40 +02003107 if (endcol == MAXCOL)
3108 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (startcol > endcol
3110#ifdef FEAT_VIRTUALEDIT
3111 || is_oneChar
3112#endif
3113 )
3114 bd.textlen = 0;
3115 else
3116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 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 Moolenaar4399ef42005-02-12 14:29:27 +00003142 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00003145 {
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 Moolenaar7ec83432014-06-17 18:16:11 +02003168 if (curwin->w_p_rnu)
3169 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (mess) /* Display message about yank? */
3171 {
3172 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 && 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 Moolenaare2cc9702005-03-15 22:43:58 +00003182 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003183 if (oap->block_mode)
3184 MSG(_("block of 1 line yanked"));
3185 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003186 MSG(_("1 line yanked"));
3187 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003188 else if (oap->block_mode)
3189 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 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 Moolenaarf7ff6e82014-03-23 15:13:05 +01003200 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003201 {
3202 curbuf->b_op_start.col = 0;
3203 curbuf->b_op_end.col = MAXCOL;
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
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 Moolenaarbf9680e2010-12-02 21:43:16 +01003214 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003215 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
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 Moolenaar9c52c3a2010-12-08 14:23:15 +01003223# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003224 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
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 Moolenaarbf9680e2010-12-02 21:43:16 +01003233 if (clip_plus.available
3234 && (curr == &(y_regs[PLUS_REGISTER])
3235 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003236 && ((clip_unnamed | clip_unnamed_saved) &
3237 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003239 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 Moolenaar071d4272004-06-13 20:20:40 +00003243 clip_own_selection(&clip_plus);
3244 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003245 if (!clip_isautosel_star() && !did_star
3246 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
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
3258fail: /* free the allocated lines */
3259 free_yank(y_idx + 1);
3260 y_current = curr;
3261 return FAIL;
3262}
3263
3264 static int
3265yank_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 Moolenaar2536d4f2015-07-17 13:22:51 +02003275 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 pnew += bd->startspaces;
3277 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3278 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003279 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 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
3290copy_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 Moolenaar677ee682005-01-27 14:41:15 +00003316 * 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 Moolenaar071d4272004-06-13 20:20:40 +00003321 */
3322 void
3323do_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 Moolenaar071d4272004-06-13 20:20:40 +00003338 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 Moolenaar071d4272004-06-13 20:20:40 +00003345 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(&regname);
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 Moolenaar27356ad2012-12-12 16:11:36 +01003395#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 Moolenaar071d4272004-06-13 20:20:40 +00003401 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 Moolenaara554a192011-09-21 17:33:53 +02003408 * characters.
3409 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 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 Moolenaara554a192011-09-21 17:33:53 +02003425 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 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 Moolenaar071d4272004-06-13 20:20:40 +00003453 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 y_size = y_current->y_size;
3455 y_array = y_current->y_array;
3456 }
3457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (y_type == MLINE)
3459 {
3460 if (flags & PUT_LINE_SPLIT)
3461 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003462 char_u *p;
3463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 /* "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 Moolenaarc004bc22015-06-19 15:17:55 +02003468 p = ml_get_cursor();
3469 if (dir == FORWARD && *p != NUL)
3470 mb_ptr_adv(p);
3471 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (ptr == NULL)
3473 goto end;
3474 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3475 vim_free(ptr);
3476
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003477 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 Moolenaar071d4272004-06-13 20:20:40 +00003482 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 Moolenaara23ccb82006-02-27 00:08:02 +00003491 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 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 Moolenaar071d4272004-06-13 20:20:40 +00003497
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 Moolenaar071d4272004-06-13 20:20:40 +00003508 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 Moolenaarf7ff6e82014-03-23 15:13:05 +01003516 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
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 Moolenaar10315b12013-06-29 17:19:28 +02003529 /* In an empty buffer the empty line is going to be replaced, include
3530 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003531 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 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 Moolenaar071d4272004-06-13 20:20:40 +00003568 /*
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 Moolenaar0fa313a2005-08-10 21:07:57 +00003588 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 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 Moolenaare649ef02007-06-28 20:18:51 +00003602 if (ve_flags == VE_ALL
3603 && (curwin->w_cursor.coladd > 0
3604 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
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 Moolenaarfd2ac762006-03-01 22:09:21 +00003620 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 for (i = 0; i < y_size; ++i)
3622 {
3623 int spaces;
3624 char shortline;
3625
3626 bd.startspaces = 0;
3627 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 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 Moolenaar597a4222014-06-25 14:39:50 +02003645 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 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 Moolenaar597a4222014-06-25 14:39:50 +02003679 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 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 Moolenaar2536d4f2015-07-17 13:22:51 +02003693 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 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 Moolenaar2536d4f2015-07-17 13:22:51 +02003704 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 ptr += spaces;
3706 }
3707 }
3708 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003709 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 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 Moolenaar13fcaaf2005-04-15 21:13:42 +00003730# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (flags & PUT_CURSEND)
3734 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003735 colnr_T len;
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 curwin->w_cursor = curbuf->b_op_end;
3738 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003739
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 Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 else
3746 curwin->w_cursor.lnum = lnum;
3747 }
3748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
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 Moolenaar0fa313a2005-08-10 21:07:57 +00003762 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
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 Moolenaar071d4272004-06-13 20:20:40 +00003783 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 Moolenaarfd2ac762006-03-01 22:09:21 +00003790 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 /*
3793 * simple case: insert into current line
3794 */
3795 if (y_type == MCHAR && y_size == 1)
3796 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003797 do {
3798 totlen = count * yanklen;
3799 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003801 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 Moolenaar1e42f7a2013-11-21 13:24:41 +01003816 {
3817 /* make sure curwin->w_virtcol is updated */
3818 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003819 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003822 if (VIsual_active)
3823 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003824 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003825
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003826 if (VIsual_active) /* reset lnum to the last visual line */
3827 lnum--;
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 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
3916error:
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 Moolenaar26a60b42005-02-22 08:49:11 +00003944 if (flags & PUT_CURSLINE)
3945 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003946 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003947 curwin->w_cursor.lnum = lnum;
3948 beginline(BL_WHITE | BL_FIX);
3949 }
3950 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
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 Moolenaar26a60b42005-02-22 08:49:11 +00003969 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 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
3983end:
3984 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003986 if (regname == '=')
3987 vim_free(y_array);
3988
Bram Moolenaar033d8882013-09-25 23:24:57 +02003989 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003990
Bram Moolenaar677ee682005-01-27 14:41:15 +00003991 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003992 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
4000adjust_cursor_eol()
4001{
4002 if (curwin->w_cursor.col > 0
4003 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004004#ifdef FEAT_VIRTUALEDIT
4005 && (ve_flags & VE_ONEMORE) == 0
4006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 && !(restart_edit || (State & INSERT)))
4008 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004009 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004010 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004011
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#ifdef FEAT_VIRTUALEDIT
4013 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004014 {
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 Moolenaar071d4272004-06-13 20:20:40 +00004021#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
4030preprocs_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 Moolenaar6bcbcc52013-11-05 07:13:41 +01004041 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4042 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043# endif
4044 ;
4045}
4046#endif
4047
4048/* Return the character name of the register with the given number */
4049 int
4050get_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
4087ex_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 Moolenaard4755bb2004-09-02 19:12:26 +00004097#ifdef FEAT_MBYTE
4098 int clen;
4099#else
4100# define clen 1
4101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
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 Moolenaar0818b872010-11-24 14:28:58 +01004112 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 Moolenaar071d4272004-06-13 20:20:40 +00004118 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 Moolenaarcde547a2009-11-17 11:43:06 +00004137
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 Moolenaar071d4272004-06-13 20:20:40 +00004145 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 Moolenaar071d4272004-06-13 20:20:40 +00004162#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004163 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004164#endif
4165 msg_outtrans_len(p, clen);
4166#ifdef FEAT_MBYTE
4167 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#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
4251dis_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 Moolenaar0fa313a2005-08-10 21:07:57 +00004266 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
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 Moolenaar81340392012-06-06 16:12:59 +02004278#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 Moolenaarbfe3bf82012-06-13 17:28:55 +02004286 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004287 * 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 Moolenaarbfe3bf82012-06-13 17:28:55 +02004289 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004290 */
4291 static char_u *
4292skip_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 Moolenaar81340392012-06-06 16:12:59 +02004328 * - COM_END,
4329 * - colon,
4330 * whichever comes first.
4331 */
4332 while (*comment_flags)
4333 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004334 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004335 || *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 Moolenaare04a48f2012-06-13 14:01:41 +02004343 * 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 Moolenaar81340392012-06-06 16:12:59 +02004345 */
4346 if (*comment_flags == ':' || *comment_flags == NUL)
4347 line += lead_len;
4348
4349 return line;
4350}
4351#endif
4352
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004354 * Join 'count' lines (minimal 2) at cursor position.
4355 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004356 * 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 Moolenaar071d4272004-06-13 20:20:40 +00004360 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004361 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004364do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004365 long count;
4366 int insert_space;
4367 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004368 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004369 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004371 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004372 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004373 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004375 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004376 int endcurr1 = NUL;
4377 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004378 int currsize = 0; /* size of the current line */
4379 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004381 colnr_T col = 0;
4382 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004383#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004384 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004385 int remove_comments = (use_formatoptions == TRUE)
4386 && has_format_option(FO_REMOVE_COMS);
4387 int prev_was_comment;
4388#endif
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004391 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 Moolenaar071d4272004-06-13 20:20:40 +00004394
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004395 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004401#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 Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004414 * Don't move anything, just compute the final line length
4415 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004417 for (t = 0; t < count; ++t)
4418 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004419 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004420 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004421 {
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 Moolenaar81340392012-06-06 16:12:59 +02004426#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 Moolenaar27ba0882012-06-07 21:09:39 +02004436 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004437 curr = new_curr;
4438 }
4439 else
4440 curr = skip_comment(curr, FALSE, insert_space,
4441 &prev_was_comment);
4442 }
4443#endif
4444
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004445 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 Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004503 /* store the column position before last line */
4504 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004506 /* allocate the space for the new line */
4507 newp = alloc_check((unsigned)(sumsize + 1));
4508 cend = newp + sumsize;
4509 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004511 /*
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 Moolenaar2536d4f2015-07-17 13:22:51 +02004525 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004526 }
4527 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004528 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004529 if (t == 0)
4530 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004531 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004532#if defined(FEAT_COMMENTS) || defined(PROTO)
4533 if (remove_comments)
4534 curr += comments[t - 1];
4535#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004536 if (insert_space && t > 1)
4537 curr = skipwhite(curr);
4538 currsize = (int)STRLEN(curr);
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4541
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004542 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 Moolenaarf92d8a22014-02-11 19:33:07 +01004548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 /* 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 Moolenaar893eaab2010-07-10 17:51:46 +02004555 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 * 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 Moolenaar071d4272004-06-13 20:20:40 +00004558 */
4559 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004561 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 curwin->w_cursor.lnum = t;
4563
4564 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004565 * Set the cursor column:
4566 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004567 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004569 curwin->w_cursor.col =
4570 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#ifdef FEAT_VIRTUALEDIT
4574 curwin->w_cursor.coladd = 0;
4575#endif
4576 curwin->w_set_curswant = TRUE;
4577
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004578theend:
4579 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004580#if defined(FEAT_COMMENTS) || defined(PROTO)
4581 if (remove_comments)
4582 vim_free(comments);
4583#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004584 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585}
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
4594same_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 Moolenaar66accae2012-01-10 13:44:27 +01004666 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
4668 void
4669op_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 Moolenaar071d4272004-06-13 20:20:40 +00004684 if (oap->is_VIsual)
4685 /* When there is no change: need to remove the Visual selection */
4686 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
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 Moolenaar81a82092008-03-12 16:27:00 +00004696 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
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 Moolenaar071d4272004-06-13 20:20:40 +00004718 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 Moolenaar071d4272004-06-13 20:20:40 +00004735}
4736
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004737#if defined(FEAT_EVAL) || defined(PROTO)
4738/*
4739 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4740 */
4741 void
4742op_formatexpr(oap)
4743 oparg_T *oap;
4744{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004745 if (oap->is_VIsual)
4746 /* When there is no change: need to remove the Visual selection */
4747 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004748
Bram Moolenaar700303e2010-07-11 17:35:50 +02004749 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 Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753}
4754
4755 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004756fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004757 linenr_T lnum;
4758 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004759 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004760{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004761 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4762 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004763 int r;
4764
4765 /*
4766 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004767 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004768 */
4769 set_vim_var_nr(VV_LNUM, lnum);
4770 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004771 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004772
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004773 /*
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 Moolenaarf193fff2006-04-27 00:02:13 +00004781
4782 set_vim_var_string(VV_CHAR, NULL, -1);
4783
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004784 return r;
4785}
4786#endif
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788/*
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 Moolenaar81a82092008-03-12 16:27:00 +00004795format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004797 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
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 Moolenaarbfe3bf82012-06-13 17:28:55 +02004811 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#endif
4813 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004814 int second_indent = -1; /* indent for second line (comment
4815 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 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 Moolenaarbfe3bf82012-06-13 17:28:55 +02004918 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004920 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 Moolenaar071d4272004-06-13 20:20:40 +00004938 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004939 {
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 Moolenaar071d4272004-06-13 20:20:40 +00004958 }
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 Moolenaarbfe3bf82012-06-13 17:28:55 +02004996 + (do_comments && do_comments_list
4997 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004999 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 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 Moolenaar2c019c82013-10-06 17:46:56 +02005018 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 */
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 Moolenaar893eaab2010-07-10 17:51:46 +02005026 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005029 {
5030 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5032 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005033 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005035 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5036 {
5037 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005038 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005039
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 Moolenaar92c2db82013-11-02 23:59:35 +01005045 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005048 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
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
5069ends_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
5093fmt_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 Moolenaar81340392012-06-06 16:12:59 +02005104 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 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
5124fmt_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
5136paragraph_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 Moolenaar071d4272004-06-13 20:20:40 +00005187/*
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 Moolenaar9d77dcc2009-03-11 15:28:26 +00005194 * deleted.
5195 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 * - 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
5201block_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 Moolenaar597a4222014-06-25 14:39:50 +02005234 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 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 Moolenaar1cd871b2004-12-19 22:46:22 +00005249 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
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 Moolenaar1dc92332015-01-27 13:22:20 +01005303 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 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 Moolenaar2c7a29c2005-12-12 22:02:31 +00005320 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 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 Moolenaar071d4272004-06-13 20:20:40 +00005345
5346#ifdef FEAT_RIGHTLEFT
5347static void reverse_line __ARGS((char_u *s));
5348
5349 static void
5350reverse_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 Moolenaar3a304b22015-06-25 13:57:36 +02005378do_addsub(command, Prenum1, g_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 int command;
5380 linenr_T Prenum1;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005381 int g_cmd; /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
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 Moolenaara93fa7e2006-04-17 22:14:47 +00005388 unsigned long n;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005389 unsigned long offset = 0; /* line offset for Ctrl_V mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 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 Moolenaar071d4272004-06-13 20:20:40 +00005399 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005400 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005401 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005402 int visual = VIsual_active;
5403 int i;
5404 int lnum = curwin->w_cursor.lnum;
5405 int lnume = curwin->w_cursor.lnum;
Bram Moolenaar1db43b12015-07-12 16:21:23 +02005406 int startcol = 0;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005407 int did_change = FALSE;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005408 pos_T t = curwin->w_cursor;
5409 int maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
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 Moolenaar071d4272004-06-13 20:20:40 +00005415 /*
5416 * First check if we are on a hexadecimal number, after the "0x".
5417 */
5418 col = curwin->w_cursor.col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005419 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005421 if (lt(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005423 curwin->w_cursor = VIsual;
5424 VIsual = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005426
5427 ptr = ml_get(VIsual.lnum);
5428 RLADDSUBFIX(ptr);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005429 if (VIsual_mode == 'V')
5430 {
5431 VIsual.col = 0;
Bram Moolenaar33c3a692015-07-22 22:46:13 +02005432 curwin->w_cursor.col = (colnr_T)STRLEN(ptr);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005433 }
Bram Moolenaar33c3a692015-07-22 22:46:13 +02005434 else if (VIsual_mode == Ctrl_V && VIsual.col > curwin->w_cursor.col)
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005435 {
5436 t = VIsual;
5437 VIsual.col = curwin->w_cursor.col;
5438 curwin->w_cursor.col = t.col;
5439 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005440
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 Moolenaar5d1bc782015-07-17 13:03:48 +02005445 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005446
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005447 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 Moolenaar3a304b22015-06-25 13:57:36 +02005453 lnum = VIsual.lnum;
5454 lnume = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005456 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 Moolenaarcc218ab2015-08-04 18:23:22 +02005495 colnr_T stop = 0;
5496
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005497 t = curwin->w_cursor;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005498 curwin->w_cursor.lnum = i;
5499 ptr = ml_get_curline();
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005500 RLADDSUBFIX(ptr);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005501 if ((int)STRLEN(ptr) <= col)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005502 /* try again on next line */
5503 continue;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005504 if (visual)
5505 {
Bram Moolenaarcc218ab2015-08-04 18:23:22 +02005506 if (VIsual_mode == 'v'
5507 && i == lnume)
5508 stop = curwin->w_cursor.col;
5509 else if (VIsual_mode == Ctrl_V
5510 && curbuf->b_visual.vi_curswant != MAXCOL)
5511 stop = curwin->w_cursor.col;
5512
5513 while (ptr[col] != NUL
5514 && !vim_isdigit(ptr[col])
5515 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005516 {
Bram Moolenaarcc218ab2015-08-04 18:23:22 +02005517 if (col > 0 && col == stop)
5518 break;
5519 ++col;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005520 }
Bram Moolenaarcc218ab2015-08-04 18:23:22 +02005521
5522 if (col > startcol && ptr[col - 1] == '-')
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005523 {
Bram Moolenaarcc218ab2015-08-04 18:23:22 +02005524 negative = TRUE;
5525 was_positive = FALSE;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005526 }
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005527 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005528 /*
5529 * If a number was found, and saving for undo works, replace the number.
5530 */
5531 firstdigit = ptr[col];
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005532 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5533 || u_save_cursor() != OK)
5534 {
5535 if (lnum < lnume)
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005536 {
5537 if (visual && VIsual_mode != Ctrl_V)
5538 col = 0;
5539 else
5540 col = startcol;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005541 /* Try again on next line */
5542 continue;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005543 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005544 beep_flush();
5545 return FAIL;
5546 }
5547
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005548 if (doalp && ASCII_ISALPHA(firstdigit))
5549 {
5550 /* decrement or increment alphabetic character */
5551 if (command == Ctrl_X)
5552 {
5553 if (CharOrd(firstdigit) < Prenum1)
5554 {
5555 if (isupper(firstdigit))
5556 firstdigit = 'A';
5557 else
5558 firstdigit = 'a';
5559 }
5560 else
5561#ifdef EBCDIC
5562 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5563#else
5564 firstdigit -= Prenum1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005566 }
5567 else
5568 {
5569 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5570 {
5571 if (isupper(firstdigit))
5572 firstdigit = 'Z';
5573 else
5574 firstdigit = 'z';
5575 }
5576 else
5577#ifdef EBCDIC
5578 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5579#else
5580 firstdigit += Prenum1;
5581#endif
5582 }
5583 curwin->w_cursor.col = col;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005584 did_change = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005585 (void)del_char(FALSE);
5586 ins_char(firstdigit);
5587 }
5588 else
5589 {
5590 if (col > 0 && ptr[col - 1] == '-' && !visual)
5591 {
5592 /* negative number */
5593 --col;
5594 negative = TRUE;
5595 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005596 /* get the number value (unsigned) */
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005597 if (visual && VIsual_mode != 'V')
5598 {
5599 if (VIsual_mode == 'v')
5600 {
5601 if (i == lnum)
5602 maxlen = (lnum == lnume
5603 ? curwin->w_cursor.col - col + 1
5604 : (int)STRLEN(ptr) - col);
5605 else
5606 maxlen = (i == lnume ? curwin->w_cursor.col - col + 1
5607 : (int)STRLEN(ptr) - col);
5608 }
5609 else if (VIsual_mode == Ctrl_V)
5610 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5611 ? (int)STRLEN(ptr) - col
5612 : curwin->w_cursor.col - col + 1);
5613 }
5614
5615 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n,
5616 maxlen);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005617
5618 /* ignore leading '-' for hex and octal numbers */
5619 if (hex && negative)
5620 {
5621 ++col;
5622 --length;
5623 negative = FALSE;
5624 }
5625
5626 /* add or subtract */
5627 subtract = FALSE;
5628 if (command == Ctrl_X)
5629 subtract ^= TRUE;
5630 if (negative)
5631 subtract ^= TRUE;
5632
5633 oldn = n;
5634 if (subtract)
5635 n -= (unsigned long)Prenum1;
5636 else
5637 n += (unsigned long)Prenum1;
5638
5639 /* handle wraparound for decimal numbers */
5640 if (!hex)
5641 {
5642 if (subtract)
5643 {
5644 if (n > oldn)
5645 {
5646 n = 1 + (n ^ (unsigned long)-1);
5647 negative ^= TRUE;
5648 }
5649 }
5650 else
5651 {
5652 /* add */
5653 if (n < oldn)
5654 {
5655 n = (n ^ (unsigned long)-1);
5656 negative ^= TRUE;
5657 }
5658 }
5659 if (n == 0)
5660 negative = FALSE;
5661 }
5662
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005663 if (visual && !was_positive && !negative && col > 0)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005664 {
5665 /* need to remove the '-' */
5666 col--;
5667 length++;
5668 }
5669
5670
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005671 /*
5672 * Delete the old number.
5673 */
5674 curwin->w_cursor.col = col;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005675 did_change = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005676 todel = length;
5677 c = gchar_cursor();
5678
5679 /*
5680 * Don't include the '-' in the length, only the length of the
5681 * part after it is kept the same.
5682 */
5683 if (c == '-')
5684 --length;
5685 while (todel-- > 0)
5686 {
5687 if (c < 0x100 && isalpha(c))
5688 {
5689 if (isupper(c))
5690 hexupper = TRUE;
5691 else
5692 hexupper = FALSE;
5693 }
5694 /* del_char() will mark line needing displaying */
5695 (void)del_char(FALSE);
5696 c = gchar_cursor();
5697 }
5698
5699 /*
5700 * Prepare the leading characters in buf1[].
5701 * When there are many leading zeros it could be very long.
5702 * Allocate a bit too much.
5703 */
5704 buf1 = alloc((unsigned)length + NUMBUFLEN);
5705 if (buf1 == NULL)
5706 return FAIL;
5707 ptr = buf1;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005708 if (negative && (!visual || (visual && was_positive)))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005709 {
5710 *ptr++ = '-';
5711 }
5712 if (hex)
5713 {
5714 *ptr++ = '0';
5715 --length;
5716 }
5717 if (hex == 'x' || hex == 'X')
5718 {
5719 *ptr++ = hex;
5720 --length;
5721 }
5722
5723 /*
5724 * Put the number characters in buf2[].
5725 */
5726 if (hex == 0)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005727 sprintf((char *)buf2, "%lu", n);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005728 else if (hex == '0')
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005729 sprintf((char *)buf2, "%lo", n);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005730 else if (hex && hexupper)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005731 sprintf((char *)buf2, "%lX", n);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005732 else
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005733 sprintf((char *)buf2, "%lx", n);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005734 length -= (int)STRLEN(buf2);
5735
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005736 /*
5737 * Adjust number of zeros to the new number of digits, so the
5738 * total length of the number remains the same.
5739 * Don't do this when
5740 * the result may look like an octal number.
5741 */
5742 if (firstdigit == '0' && !(dooct && hex == 0))
5743 while (length-- > 0)
5744 *ptr++ = '0';
5745 *ptr = NUL;
5746 STRCAT(buf1, buf2);
5747 ins_str(buf1); /* insert the new number */
5748 vim_free(buf1);
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005749 if (lnum < lnume)
5750 curwin->w_cursor.col = t.col;
5751 else if (did_change && curwin->w_cursor.col)
5752 --curwin->w_cursor.col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005753 }
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005754
5755 if (g_cmd)
5756 {
5757 offset = (unsigned long)Prenum1;
5758 g_cmd = 0;
5759 }
5760 /* reset */
5761 subtract = FALSE;
5762 negative = FALSE;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005763 was_positive = TRUE;
Bram Moolenaarae2fe732015-07-10 22:38:00 +02005764 if (visual && VIsual_mode == Ctrl_V)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005765 col = startcol;
Bram Moolenaarae2fe732015-07-10 22:38:00 +02005766 else
5767 col = 0;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005768 Prenum1 += offset;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005769 curwin->w_set_curswant = TRUE;
5770#ifdef FEAT_RIGHTLEFT
5771 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5772 RLADDSUBFIX(ptr);
5773#endif
5774 }
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005775 if (visual)
5776 /* cursor at the top of the selection */
5777 curwin->w_cursor = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 return OK;
5779}
5780
5781#ifdef FEAT_VIMINFO
5782 int
5783read_viminfo_register(virp, force)
5784 vir_T *virp;
5785 int force;
5786{
5787 int eof;
5788 int do_it = TRUE;
5789 int size;
5790 int limit;
5791 int i;
5792 int set_prev = FALSE;
5793 char_u *str;
5794 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005795 int new_type = MCHAR; /* init to shut up compiler */
5796 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
5798 /* We only get here (hopefully) if line[0] == '"' */
5799 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005800
5801 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (*str == '"')
5803 {
5804 set_prev = TRUE;
5805 str++;
5806 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 if (!ASCII_ISALNUM(*str) && *str != '-')
5809 {
5810 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5811 return TRUE; /* too many errors, pretend end-of-file */
5812 do_it = FALSE;
5813 }
5814 get_yank_register(*str++, FALSE);
5815 if (!force && y_current->y_array != NULL)
5816 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005817
5818 if (*str == '@')
5819 {
5820 /* "x@: register x used for @@ */
5821 if (force || execreg_lastc == NUL)
5822 execreg_lastc = str[-1];
5823 }
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 size = 0;
5826 limit = 100; /* Optimized for registers containing <= 100 lines */
5827 if (do_it)
5828 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005829 /*
5830 * Build the new register in array[].
5831 * y_array is kept as-is until done.
5832 * The "do_it" flag is reset when something is wrong, in which case
5833 * array[] needs to be freed.
5834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (set_prev)
5836 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005837 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005838 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005840 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005842 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005844 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /* get the block width; if it's missing we get a zero, which is OK */
5846 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005847 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
5849
5850 while (!(eof = viminfo_readline(virp))
5851 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5852 {
5853 if (do_it)
5854 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005855 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005857 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005859
5860 if (new_array == NULL)
5861 {
5862 do_it = FALSE;
5863 break;
5864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005866 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005868 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
5871 str = viminfo_readstring(virp, 1, TRUE);
5872 if (str != NULL)
5873 array[size++] = str;
5874 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005875 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 do_it = FALSE;
5877 }
5878 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 if (do_it)
5881 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005882 /* free y_array[] */
5883 for (i = 0; i < y_current->y_size; i++)
5884 vim_free(y_current->y_array[i]);
5885 vim_free(y_current->y_array);
5886
5887 y_current->y_type = new_type;
5888 y_current->y_width = new_width;
5889 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 if (size == 0)
5891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 y_current->y_array = NULL;
5893 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005896 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 y_current->y_array =
5898 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5899 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005900 {
5901 if (y_current->y_array == NULL)
5902 vim_free(array[i]);
5903 else
5904 y_current->y_array[i] = array[i];
5905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005908 else
5909 {
5910 /* Free array[] if it was filled. */
5911 for (i = 0; i < size; i++)
5912 vim_free(array[i]);
5913 }
5914 vim_free(array);
5915
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 return eof;
5917}
5918
5919 void
5920write_viminfo_registers(fp)
5921 FILE *fp;
5922{
5923 int i, j;
5924 char_u *type;
5925 char_u c;
5926 int num_lines;
5927 int max_num_lines;
5928 int max_kbyte;
5929 long len;
5930
Bram Moolenaar64404472010-06-26 06:24:45 +02005931 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 /* Get '<' value, use old '"' value if '<' is not found. */
5934 max_num_lines = get_viminfo_parameter('<');
5935 if (max_num_lines < 0)
5936 max_num_lines = get_viminfo_parameter('"');
5937 if (max_num_lines == 0)
5938 return;
5939 max_kbyte = get_viminfo_parameter('s');
5940 if (max_kbyte == 0)
5941 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005942
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 for (i = 0; i < NUM_REGISTERS; i++)
5944 {
5945 if (y_regs[i].y_array == NULL)
5946 continue;
5947#ifdef FEAT_CLIPBOARD
5948 /* Skip '*'/'+' register, we don't want them back next time */
5949 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5950 continue;
5951#endif
5952#ifdef FEAT_DND
5953 /* Neither do we want the '~' register */
5954 if (i == TILDE_REGISTER)
5955 continue;
5956#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005957 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005959 if (num_lines == 0
5960 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005961 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005962 continue;
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (max_kbyte > 0)
5965 {
5966 /* Skip register if there is more text than the maximum size. */
5967 len = 0;
5968 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005969 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (len > (long)max_kbyte * 1024L)
5971 continue;
5972 }
5973
5974 switch (y_regs[i].y_type)
5975 {
5976 case MLINE:
5977 type = (char_u *)"LINE";
5978 break;
5979 case MCHAR:
5980 type = (char_u *)"CHAR";
5981 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 case MBLOCK:
5983 type = (char_u *)"BLOCK";
5984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 default:
5986 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005987 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 emsg(IObuff);
5989 type = (char_u *)"LINE";
5990 break;
5991 }
5992 if (y_previous == &y_regs[i])
5993 fprintf(fp, "\"");
5994 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005995 fprintf(fp, "\"%c", c);
5996 if (c == execreg_lastc)
5997 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005998 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000 /* If max_num_lines < 0, then we save ALL the lines in the register */
6001 if (max_num_lines > 0 && num_lines > max_num_lines)
6002 num_lines = max_num_lines;
6003 for (j = 0; j < num_lines; j++)
6004 {
6005 putc('\t', fp);
6006 viminfo_writestring(fp, y_regs[i].y_array[j]);
6007 }
6008 }
6009}
6010#endif /* FEAT_VIMINFO */
6011
6012#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6013/*
6014 * SELECTION / PRIMARY ('*')
6015 *
6016 * Text selection stuff that uses the GUI selection register '*'. When using a
6017 * GUI this may be text from another window, otherwise it is the last text we
6018 * had highlighted with VIsual mode. With mouse support, clicking the middle
6019 * button performs the paste, otherwise you will need to do <"*p>. "
6020 * If not under X, it is synonymous with the clipboard register '+'.
6021 *
6022 * X CLIPBOARD ('+')
6023 *
6024 * Text selection stuff that uses the GUI clipboard register '+'.
6025 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6026 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6027 * otherwise you will need to do <"+p>. "
6028 * If not under X, it is synonymous with the selection register '*'.
6029 */
6030
6031/*
6032 * Routine to export any final X selection we had to the environment
6033 * so that the text is still available after vim has exited. X selections
6034 * only exist while the owning application exists, so we write to the
6035 * permanent (while X runs) store CUT_BUFFER0.
6036 * Dump the CLIPBOARD selection if we own it (it's logically the more
6037 * 'permanent' of the two), otherwise the PRIMARY one.
6038 * For now, use a hard-coded sanity limit of 1Mb of data.
6039 */
6040#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
6041 void
6042x11_export_final_selection()
6043{
6044 Display *dpy;
6045 char_u *str = NULL;
6046 long_u len = 0;
6047 int motion_type = -1;
6048
6049# ifdef FEAT_GUI
6050 if (gui.in_use)
6051 dpy = X_DISPLAY;
6052 else
6053# endif
6054# ifdef FEAT_XCLIPBOARD
6055 dpy = xterm_dpy;
6056# else
6057 return;
6058# endif
6059
6060 /* Get selection to export */
6061 if (clip_plus.owned)
6062 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6063 else if (clip_star.owned)
6064 motion_type = clip_convert_selection(&str, &len, &clip_star);
6065
6066 /* Check it's OK */
6067 if (dpy != NULL && str != NULL && motion_type >= 0
6068 && len < 1024*1024 && len > 0)
6069 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006070#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006071 int ok = TRUE;
6072
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006073 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6074 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6075 * encoding conversion usually doesn't work, so keep the text as-is.
6076 */
6077 if (has_mbyte)
6078 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006079 vimconv_T vc;
6080
6081 vc.vc_type = CONV_NONE;
6082 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6083 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006084 int intlen = len;
6085 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006086
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006087 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006088 conv_str = string_convert(&vc, str, &intlen);
6089 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006090 if (conv_str != NULL)
6091 {
6092 vim_free(str);
6093 str = conv_str;
6094 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006095 else
6096 {
6097 ok = FALSE;
6098 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006099 convert_setup(&vc, NULL, NULL);
6100 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006101 else
6102 {
6103 ok = FALSE;
6104 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006105 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006106
6107 /* Do not store the string if conversion failed. Better to use any
6108 * other selection than garbled text. */
6109 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006110#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006111 {
6112 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6113 XFlush(dpy);
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116
6117 vim_free(str);
6118}
6119#endif
6120
6121 void
6122clip_free_selection(cbd)
6123 VimClipboard *cbd;
6124{
6125 struct yankreg *y_ptr = y_current;
6126
6127 if (cbd == &clip_plus)
6128 y_current = &y_regs[PLUS_REGISTER];
6129 else
6130 y_current = &y_regs[STAR_REGISTER];
6131 free_yank_all();
6132 y_current->y_size = 0;
6133 y_current = y_ptr;
6134}
6135
6136/*
6137 * Get the selected text and put it in the gui selection register '*' or '+'.
6138 */
6139 void
6140clip_get_selection(cbd)
6141 VimClipboard *cbd;
6142{
6143 struct yankreg *old_y_previous, *old_y_current;
6144 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 pos_T old_visual;
6146 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 colnr_T old_curswant;
6148 int old_set_curswant;
6149 pos_T old_op_start, old_op_end;
6150 oparg_T oa;
6151 cmdarg_T ca;
6152
6153 if (cbd->owned)
6154 {
6155 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6156 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6157 return;
6158
6159 /* Get the text between clip_star.start & clip_star.end */
6160 old_y_previous = y_previous;
6161 old_y_current = y_current;
6162 old_cursor = curwin->w_cursor;
6163 old_curswant = curwin->w_curswant;
6164 old_set_curswant = curwin->w_set_curswant;
6165 old_op_start = curbuf->b_op_start;
6166 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 old_visual = VIsual;
6168 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 clear_oparg(&oa);
6170 oa.regname = (cbd == &clip_plus ? '+' : '*');
6171 oa.op_type = OP_YANK;
6172 vim_memset(&ca, 0, sizeof(ca));
6173 ca.oap = &oa;
6174 ca.cmdchar = 'y';
6175 ca.count1 = 1;
6176 ca.retval = CA_NO_ADJ_OP_END;
6177 do_pending_operator(&ca, 0, TRUE);
6178 y_previous = old_y_previous;
6179 y_current = old_y_current;
6180 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006181 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 curwin->w_curswant = old_curswant;
6183 curwin->w_set_curswant = old_set_curswant;
6184 curbuf->b_op_start = old_op_start;
6185 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 VIsual = old_visual;
6187 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 }
6189 else
6190 {
6191 clip_free_selection(cbd);
6192
6193 /* Try to get selected text from another window */
6194 clip_gen_request_selection(cbd);
6195 }
6196}
6197
Bram Moolenaard44347f2011-06-19 01:14:29 +02006198/*
6199 * Convert from the GUI selection string into the '*'/'+' register.
6200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 void
6202clip_yank_selection(type, str, len, cbd)
6203 int type;
6204 char_u *str;
6205 long len;
6206 VimClipboard *cbd;
6207{
6208 struct yankreg *y_ptr;
6209
6210 if (cbd == &clip_plus)
6211 y_ptr = &y_regs[PLUS_REGISTER];
6212 else
6213 y_ptr = &y_regs[STAR_REGISTER];
6214
6215 clip_free_selection(cbd);
6216
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006217 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218}
6219
6220/*
6221 * Convert the '*'/'+' register into a GUI selection string returned in *str
6222 * with length *len.
6223 * Returns the motion type, or -1 for failure.
6224 */
6225 int
6226clip_convert_selection(str, len, cbd)
6227 char_u **str;
6228 long_u *len;
6229 VimClipboard *cbd;
6230{
6231 char_u *p;
6232 int lnum;
6233 int i, j;
6234 int_u eolsize;
6235 struct yankreg *y_ptr;
6236
6237 if (cbd == &clip_plus)
6238 y_ptr = &y_regs[PLUS_REGISTER];
6239 else
6240 y_ptr = &y_regs[STAR_REGISTER];
6241
6242#ifdef USE_CRNL
6243 eolsize = 2;
6244#else
6245 eolsize = 1;
6246#endif
6247
6248 *str = NULL;
6249 *len = 0;
6250 if (y_ptr->y_array == NULL)
6251 return -1;
6252
6253 for (i = 0; i < y_ptr->y_size; i++)
6254 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6255
6256 /*
6257 * Don't want newline character at end of last line if we're in MCHAR mode.
6258 */
6259 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6260 *len -= eolsize;
6261
6262 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6263 if (p == NULL)
6264 return -1;
6265 lnum = 0;
6266 for (i = 0, j = 0; i < (int)*len; i++, j++)
6267 {
6268 if (y_ptr->y_array[lnum][j] == '\n')
6269 p[i] = NUL;
6270 else if (y_ptr->y_array[lnum][j] == NUL)
6271 {
6272#ifdef USE_CRNL
6273 p[i++] = '\r';
6274#endif
6275#ifdef USE_CR
6276 p[i] = '\r';
6277#else
6278 p[i] = '\n';
6279#endif
6280 lnum++;
6281 j = -1;
6282 }
6283 else
6284 p[i] = y_ptr->y_array[lnum][j];
6285 }
6286 return y_ptr->y_type;
6287}
6288
6289
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290/*
6291 * If we have written to a clipboard register, send the text to the clipboard.
6292 */
6293 static void
6294may_set_selection()
6295{
6296 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6297 {
6298 clip_own_selection(&clip_star);
6299 clip_gen_set_selection(&clip_star);
6300 }
6301 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6302 {
6303 clip_own_selection(&clip_plus);
6304 clip_gen_set_selection(&clip_plus);
6305 }
6306}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308#endif /* FEAT_CLIPBOARD || PROTO */
6309
6310
6311#if defined(FEAT_DND) || defined(PROTO)
6312/*
6313 * Replace the contents of the '~' register with str.
6314 */
6315 void
6316dnd_yank_drag_data(str, len)
6317 char_u *str;
6318 long len;
6319{
6320 struct yankreg *curr;
6321
6322 curr = y_current;
6323 y_current = &y_regs[TILDE_REGISTER];
6324 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006325 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 y_current = curr;
6327}
6328#endif
6329
6330
6331#if defined(FEAT_EVAL) || defined(PROTO)
6332/*
6333 * Return the type of a register.
6334 * Used for getregtype()
6335 * Returns MAUTO for error.
6336 */
6337 char_u
6338get_reg_type(regname, reglen)
6339 int regname;
6340 long *reglen;
6341{
6342 switch (regname)
6343 {
6344 case '%': /* file name */
6345 case '#': /* alternate file name */
6346 case '=': /* expression */
6347 case ':': /* last command line */
6348 case '/': /* last search-pattern */
6349 case '.': /* last inserted text */
6350#ifdef FEAT_SEARCHPATH
6351 case Ctrl_F: /* Filename under cursor */
6352 case Ctrl_P: /* Path under cursor, expand via "path" */
6353#endif
6354 case Ctrl_W: /* word under cursor */
6355 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6356 case '_': /* black hole: always empty */
6357 return MCHAR;
6358 }
6359
6360#ifdef FEAT_CLIPBOARD
6361 regname = may_get_selection(regname);
6362#endif
6363
Bram Moolenaar32b92012014-01-14 12:33:36 +01006364 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6365 return MAUTO;
6366
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 get_yank_register(regname, FALSE);
6368
6369 if (y_current->y_array != NULL)
6370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (reglen != NULL && y_current->y_type == MBLOCK)
6372 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 return y_current->y_type;
6374 }
6375 return MAUTO;
6376}
6377
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006378static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6379
6380/*
6381 * When "flags" has GREG_LIST return a list with text "s".
6382 * Otherwise just return "s".
6383 */
6384 static char_u *
6385getreg_wrap_one_line(s, flags)
6386 char_u *s;
6387 int flags;
6388{
6389 if (flags & GREG_LIST)
6390 {
6391 list_T *list = list_alloc();
6392
6393 if (list != NULL)
6394 {
6395 if (list_append_string(list, NULL, -1) == FAIL)
6396 {
6397 list_free(list, TRUE);
6398 return NULL;
6399 }
6400 list->lv_first->li_tv.vval.v_string = s;
6401 }
6402 return (char_u *)list;
6403 }
6404 return s;
6405}
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407/*
6408 * Return the contents of a register as a single allocated string.
6409 * Used for "@r" in expressions and for getreg().
6410 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006411 * Flags:
6412 * GREG_NO_EXPR Do not allow expression register
6413 * GREG_EXPR_SRC For the expression register: return expression itself,
6414 * not the result of its evaluation.
6415 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 */
6417 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006418get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006420 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 long i;
6423 char_u *retval;
6424 int allocated;
6425 long len;
6426
6427 /* Don't allow using an expression register inside an expression */
6428 if (regname == '=')
6429 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006430 if (flags & GREG_NO_EXPR)
6431 return NULL;
6432 if (flags & GREG_EXPR_SRC)
6433 return getreg_wrap_one_line(get_expr_line_src(), flags);
6434 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 }
6436
6437 if (regname == '@') /* "@@" is used for unnamed register */
6438 regname = '"';
6439
6440 /* check for valid regname */
6441 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6442 return NULL;
6443
6444#ifdef FEAT_CLIPBOARD
6445 regname = may_get_selection(regname);
6446#endif
6447
6448 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6449 {
6450 if (retval == NULL)
6451 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006452 if (allocated)
6453 return getreg_wrap_one_line(retval, flags);
6454 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456
6457 get_yank_register(regname, FALSE);
6458 if (y_current->y_array == NULL)
6459 return NULL;
6460
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006461 if (flags & GREG_LIST)
6462 {
6463 list_T *list = list_alloc();
6464 int error = FALSE;
6465
6466 if (list == NULL)
6467 return NULL;
6468 for (i = 0; i < y_current->y_size; ++i)
6469 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6470 error = TRUE;
6471 if (error)
6472 {
6473 list_free(list, TRUE);
6474 return NULL;
6475 }
6476 return (char_u *)list;
6477 }
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 /*
6480 * Compute length of resulting string.
6481 */
6482 len = 0;
6483 for (i = 0; i < y_current->y_size; ++i)
6484 {
6485 len += (long)STRLEN(y_current->y_array[i]);
6486 /*
6487 * Insert a newline between lines and after last line if
6488 * y_type is MLINE.
6489 */
6490 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6491 ++len;
6492 }
6493
6494 retval = lalloc(len + 1, TRUE);
6495
6496 /*
6497 * Copy the lines of the yank register into the string.
6498 */
6499 if (retval != NULL)
6500 {
6501 len = 0;
6502 for (i = 0; i < y_current->y_size; ++i)
6503 {
6504 STRCPY(retval + len, y_current->y_array[i]);
6505 len += (long)STRLEN(retval + len);
6506
6507 /*
6508 * Insert a NL between lines and after the last line if y_type is
6509 * MLINE.
6510 */
6511 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6512 retval[len++] = '\n';
6513 }
6514 retval[len] = NUL;
6515 }
6516
6517 return retval;
6518}
6519
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006520 static int
6521init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6522 int name;
6523 struct yankreg **old_y_previous;
6524 struct yankreg **old_y_current;
6525 int must_append;
6526 int *yank_type UNUSED;
6527{
6528 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6529 {
6530 emsg_invreg(name);
6531 return FAIL;
6532 }
6533
6534 /* Don't want to change the current (unnamed) register */
6535 *old_y_previous = y_previous;
6536 *old_y_current = y_current;
6537
6538 get_yank_register(name, TRUE);
6539 if (!y_append && !must_append)
6540 free_yank_all();
6541 return OK;
6542}
6543
6544 static void
6545finish_write_reg(name, old_y_previous, old_y_current)
6546 int name;
6547 struct yankreg *old_y_previous;
6548 struct yankreg *old_y_current;
6549{
6550# ifdef FEAT_CLIPBOARD
6551 /* Send text of clipboard register to the clipboard. */
6552 may_set_selection();
6553# endif
6554
6555 /* ':let @" = "val"' should change the meaning of the "" register */
6556 if (name != '"')
6557 y_previous = old_y_previous;
6558 y_current = old_y_current;
6559}
6560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561/*
6562 * Store string "str" in register "name".
6563 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6564 * If "must_append" is TRUE, always append to the register. Otherwise append
6565 * if "name" is an uppercase letter.
6566 * Note: "maxlen" and "must_append" don't work for the "/" register.
6567 * Careful: 'str' is modified, you may have to use a copy!
6568 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6569 */
6570 void
6571write_reg_contents(name, str, maxlen, must_append)
6572 int name;
6573 char_u *str;
6574 int maxlen;
6575 int must_append;
6576{
6577 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6578}
6579
6580 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006581write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6582 int name;
6583 char_u **strings;
6584 int maxlen UNUSED;
6585 int must_append;
6586 int yank_type;
6587 long block_len;
6588{
6589 struct yankreg *old_y_previous, *old_y_current;
6590
6591 if (name == '/'
6592#ifdef FEAT_EVAL
6593 || name == '='
6594#endif
6595 )
6596 {
6597 char_u *s;
6598
6599 if (strings[0] == NULL)
6600 s = (char_u *)"";
6601 else if (strings[1] != NULL)
6602 {
6603 EMSG(_("E883: search pattern and expression register may not "
6604 "contain two or more lines"));
6605 return;
6606 }
6607 else
6608 s = strings[0];
6609 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6610 return;
6611 }
6612
6613 if (name == '_') /* black hole: nothing to do */
6614 return;
6615
6616 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6617 &yank_type) == FAIL)
6618 return;
6619
6620 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6621
6622 finish_write_reg(name, old_y_previous, old_y_current);
6623}
6624
6625 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6627 int name;
6628 char_u *str;
6629 int maxlen;
6630 int must_append;
6631 int yank_type;
6632 long block_len;
6633{
6634 struct yankreg *old_y_previous, *old_y_current;
6635 long len;
6636
Bram Moolenaare7566042005-06-17 22:00:15 +00006637 if (maxlen >= 0)
6638 len = maxlen;
6639 else
6640 len = (long)STRLEN(str);
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 /* Special case: '/' search pattern */
6643 if (name == '/')
6644 {
6645 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6646 return;
6647 }
6648
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006649 if (name == '#')
6650 {
6651 buf_T *buf;
6652
6653 if (VIM_ISDIGIT(*str))
6654 {
6655 int num = atoi((char *)str);
6656
6657 buf = buflist_findnr(num);
6658 if (buf == NULL)
6659 EMSGN(_(e_nobufnr), (long)num);
6660 }
6661 else
6662 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6663 TRUE, FALSE, FALSE));
6664 if (buf == NULL)
6665 return;
6666 curwin->w_alt_fnum = buf->b_fnum;
6667 return;
6668 }
6669
Bram Moolenaare7566042005-06-17 22:00:15 +00006670#ifdef FEAT_EVAL
6671 if (name == '=')
6672 {
6673 char_u *p, *s;
6674
6675 p = vim_strnsave(str, (int)len);
6676 if (p == NULL)
6677 return;
6678 if (must_append)
6679 {
6680 s = concat_str(get_expr_line_src(), p);
6681 vim_free(p);
6682 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006683 }
6684 set_expr_line(p);
6685 return;
6686 }
6687#endif
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (name == '_') /* black hole: nothing to do */
6690 return;
6691
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006692 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6693 &yank_type) == FAIL)
6694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006696 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006698 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699}
6700#endif /* FEAT_EVAL */
6701
6702#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6703/*
6704 * Put a string into a register. When the register is not empty, the string
6705 * is appended.
6706 */
6707 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006708str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006710 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 char_u *str; /* string to put in register */
6712 long len; /* length of string */
6713 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006714 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006716 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 int lnum;
6718 long start;
6719 long i;
6720 int extra;
6721 int newlines; /* number of lines added */
6722 int extraline = 0; /* extra line at the end */
6723 int append = FALSE; /* append to last line in register */
6724 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006725 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006729 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 y_ptr->y_size = 0;
6731
Bram Moolenaard44347f2011-06-19 01:14:29 +02006732 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006733 type = ((str_list || (len > 0 && (str[len - 1] == NL
6734 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006735 ? MLINE : MCHAR);
6736 else
6737 type = yank_type;
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /*
6740 * Count the number of lines within the string
6741 */
6742 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006743 if (str_list)
6744 {
6745 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006748 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006750 for (i = 0; i < len; i++)
6751 if (str[i] == '\n')
6752 ++newlines;
6753 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6754 {
6755 extraline = 1;
6756 ++newlines; /* count extra newline at the end */
6757 }
6758 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6759 {
6760 append = TRUE;
6761 --newlines; /* uncount newline when appending first line */
6762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006765 /* Without any lines make the register empty. */
6766 if (y_ptr->y_size + newlines == 0)
6767 {
6768 vim_free(y_ptr->y_array);
6769 y_ptr->y_array = NULL;
6770 return;
6771 }
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 /*
6774 * Allocate an array to hold the pointers to the new register lines.
6775 * If the register was not empty, move the existing lines to the new array.
6776 */
6777 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6778 * sizeof(char_u *), TRUE);
6779 if (pp == NULL) /* out of memory */
6780 return;
6781 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6782 pp[lnum] = y_ptr->y_array[lnum];
6783 vim_free(y_ptr->y_array);
6784 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 /*
6788 * Find the end of each line and save it into the array.
6789 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006790 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006792 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6793 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006794 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006795 pp[lnum] = vim_strnsave(*ss, i);
6796 if (i > maxlen)
6797 maxlen = i;
6798 }
6799 }
6800 else
6801 {
6802 for (start = 0; start < len + extraline; start += i + 1)
6803 {
6804 for (i = start; i < len; ++i) /* find the end of the line */
6805 if (str[i] == '\n')
6806 break;
6807 i -= start; /* i is now length of line */
6808 if (i > maxlen)
6809 maxlen = i;
6810 if (append)
6811 {
6812 --lnum;
6813 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6814 }
6815 else
6816 extra = 0;
6817 s = alloc((unsigned)(i + extra + 1));
6818 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006820 if (extra)
6821 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6822 if (append)
6823 vim_free(y_ptr->y_array[lnum]);
6824 if (i)
6825 mch_memmove(s + extra, str + start, (size_t)i);
6826 extra += i;
6827 s[extra] = NUL;
6828 y_ptr->y_array[lnum++] = s;
6829 while (--extra >= 0)
6830 {
6831 if (*s == NUL)
6832 *s = '\n'; /* replace NUL with newline */
6833 ++s;
6834 }
6835 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838 y_ptr->y_type = type;
6839 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (type == MBLOCK)
6841 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6842 else
6843 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844}
6845#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6846
6847 void
6848clear_oparg(oap)
6849 oparg_T *oap;
6850{
6851 vim_memset(oap, 0, sizeof(oparg_T));
6852}
6853
Bram Moolenaar7c626922005-02-07 22:01:03 +00006854static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006857 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 *
6859 * "Words" are counted by looking for boundaries between non-space and
6860 * space characters. (it seems to produce results that match 'wc'.)
6861 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006862 * Return value is byte count; word count for the line is added to "*wc".
6863 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 *
6865 * The function will only examine the first "limit" characters in the
6866 * line, stopping if it encounters an end-of-line (NUL byte). In that
6867 * case, eol_size will be added to the character count to account for
6868 * the size of the EOL character.
6869 */
6870 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006871line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 char_u *line;
6873 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006874 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 long limit;
6876 int eol_size;
6877{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006878 long i;
6879 long words = 0;
6880 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 int is_word = 0;
6882
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006883 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885 if (is_word)
6886 {
6887 if (vim_isspace(line[i]))
6888 {
6889 words++;
6890 is_word = 0;
6891 }
6892 }
6893 else if (!vim_isspace(line[i]))
6894 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006895 ++chars;
6896#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006897 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006898#else
6899 ++i;
6900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 }
6902
6903 if (is_word)
6904 words++;
6905 *wc += words;
6906
6907 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006908 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006911 chars += eol_size;
6912 }
6913 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 return i;
6915}
6916
6917/*
6918 * Give some info about the position of the cursor (for "g CTRL-G").
6919 * In Visual mode, give some info about the selected region. (In this case,
6920 * the *_count_cursor variables store running totals for the selection.)
6921 */
6922 void
6923cursor_pos_info()
6924{
6925 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006926 char_u buf1[50];
6927 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006929 long byte_count = 0;
6930 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 long char_count = 0;
6932 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 long word_count = 0;
6934 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006935 int eol_size;
6936 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 long line_count_selected = 0;
6938 pos_T min_pos, max_pos;
6939 oparg_T oparg;
6940 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 /*
6943 * Compute the length of the file in characters.
6944 */
6945 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6946 {
6947 MSG(_(no_lines_msg));
6948 }
6949 else
6950 {
6951 if (get_fileformat(curbuf) == EOL_DOS)
6952 eol_size = 2;
6953 else
6954 eol_size = 1;
6955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (VIsual_active)
6957 {
6958 if (lt(VIsual, curwin->w_cursor))
6959 {
6960 min_pos = VIsual;
6961 max_pos = curwin->w_cursor;
6962 }
6963 else
6964 {
6965 min_pos = curwin->w_cursor;
6966 max_pos = VIsual;
6967 }
6968 if (*p_sel == 'e' && max_pos.col > 0)
6969 --max_pos.col;
6970
6971 if (VIsual_mode == Ctrl_V)
6972 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006973#ifdef FEAT_LINEBREAK
6974 char_u * saved_sbr = p_sbr;
6975
6976 /* Make 'sbr' empty for a moment to get the correct size. */
6977 p_sbr = empty_option;
6978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 oparg.is_VIsual = 1;
6980 oparg.block_mode = TRUE;
6981 oparg.op_type = OP_NOP;
6982 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006983 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006984#ifdef FEAT_LINEBREAK
6985 p_sbr = saved_sbr;
6986#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006987 if (curwin->w_curswant == MAXCOL)
6988 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 /* Swap the start, end vcol if needed */
6990 if (oparg.end_vcol < oparg.start_vcol)
6991 {
6992 oparg.end_vcol += oparg.start_vcol;
6993 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6994 oparg.end_vcol -= oparg.start_vcol;
6995 }
6996 }
6997 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7001 {
7002 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007003 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
7005 ui_breakcheck();
7006 if (got_int)
7007 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007008 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 /* Do extra processing for VIsual mode. */
7012 if (VIsual_active
7013 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7014 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007015 char_u *s = NULL;
7016 long len = 0L;
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 switch (VIsual_mode)
7019 {
7020 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007021#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007025#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007027#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007028 s = bd.textstart;
7029 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 break;
7031 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007032 s = ml_get(lnum);
7033 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 break;
7035 case 'v':
7036 {
7037 colnr_T start_col = (lnum == min_pos.lnum)
7038 ? min_pos.col : 0;
7039 colnr_T end_col = (lnum == max_pos.lnum)
7040 ? max_pos.col - start_col + 1 : MAXCOL;
7041
Bram Moolenaardef9e822004-12-31 20:58:58 +00007042 s = ml_get(lnum) + start_col;
7043 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 }
7045 break;
7046 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007047 if (s != NULL)
7048 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007049 byte_count_cursor += line_count_info(s, &word_count_cursor,
7050 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007051 if (lnum == curbuf->b_ml.ml_line_count
7052 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007053 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007054 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007055 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
7058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 /* In non-visual mode, check for the line the cursor is on */
7061 if (lnum == curwin->w_cursor.lnum)
7062 {
7063 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007064 char_count_cursor += char_count;
7065 byte_count_cursor = byte_count +
7066 line_count_info(ml_get(lnum),
7067 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 (long)(curwin->w_cursor.col + 1), eol_size);
7069 }
7070 }
7071 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007072 byte_count += line_count_info(ml_get(lnum), &word_count,
7073 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
7075
7076 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007077 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007078 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (VIsual_active)
7081 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007082 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
7084 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007085 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007086 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7088 }
7089 else
7090 buf1[0] = NUL;
7091
Bram Moolenaar7c626922005-02-07 22:01:03 +00007092 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00007093 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007094 vim_snprintf((char *)IObuff, IOSIZE,
7095 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 buf1, line_count_selected,
7097 (long)curbuf->b_ml.ml_line_count,
7098 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007099 byte_count_cursor, byte_count);
7100 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007101 vim_snprintf((char *)IObuff, IOSIZE,
7102 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00007103 buf1, line_count_selected,
7104 (long)curbuf->b_ml.ml_line_count,
7105 word_count_cursor, word_count,
7106 char_count_cursor, char_count,
7107 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
7111 p = ml_get_curline();
7112 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007113 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007115 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7116 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar7c626922005-02-07 22:01:03 +00007118 if (char_count_cursor == byte_count_cursor
7119 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007120 vim_snprintf((char *)IObuff, IOSIZE,
7121 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 (char *)buf1, (char *)buf2,
7123 (long)curwin->w_cursor.lnum,
7124 (long)curbuf->b_ml.ml_line_count,
7125 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007126 byte_count_cursor, byte_count);
7127 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007128 vim_snprintf((char *)IObuff, IOSIZE,
7129 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00007130 (char *)buf1, (char *)buf2,
7131 (long)curwin->w_cursor.lnum,
7132 (long)curbuf->b_ml.ml_line_count,
7133 word_count_cursor, word_count,
7134 char_count_cursor, char_count,
7135 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
7137
7138#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00007139 byte_count = bomb_size();
7140 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00007142 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143#endif
7144 /* Don't shorten this message, the user asked for it. */
7145 p = p_shm;
7146 p_shm = (char_u *)"";
7147 msg(IObuff);
7148 p_shm = p;
7149 }
7150}