blob: 0c3d11b87ff79f1fdf5d7a818cdb090eb1066c83 [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);
445 copy_chars(newp + bd.textcol, (size_t)i, TAB);
446 copy_spaces(newp + bd.textcol + i, (size_t)j);
447 /* 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));
538 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
539 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 */
641 copy_spaces(newp + offset, (size_t)spaces);
642
643 /* copy the new text */
644 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
645 offset += s_len;
646
647 if (spaces && !bdp->is_short)
648 {
649 /* insert post-padding */
650 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
651 /* We're splitting a TAB, don't copy it. */
652 oldp++;
653 /* We allowed for that TAB, remember this now */
654 count++;
655 }
656
657 if (spaces > 0)
658 offset += count;
Bram 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;
689 int count;
690 linenr_T first_changed = 0;
691 linenr_T last_changed = 0;
692 linenr_T start_lnum = curwin->w_cursor.lnum;
693
Bram 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 */
722 count = 0;
723 else
724 count = how(); /* get the indent for this line */
725
726 if (set_indent(count, SIN_UNDO))
727 {
728 /* did change the indent, call changed_lines() later */
729 if (first_changed == 0)
730 first_changed = curwin->w_cursor.lnum;
731 last_changed = curwin->w_cursor.lnum;
732 }
733 }
734 ++curwin->w_cursor.lnum;
Bram 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
859 "/.%#:="
860#else
861 "/.%#:"
862#endif
863 , regname) != NULL)
864 || regname == '"'
865 || regname == '-'
866 || regname == '_'
867#ifdef FEAT_CLIPBOARD
868 || regname == '*'
869 || regname == '+'
870#endif
871#ifdef FEAT_DND
872 || (!writing && regname == '~')
873#endif
874 )
875 return TRUE;
876 return FALSE;
877}
878
879/*
880 * Set y_current and y_append, according to the value of "regname".
881 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000882 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 *
884 * If regname is 0 and writing, use register 0
885 * If regname is 0 and reading, use previous register
886 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000887 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888get_yank_register(regname, writing)
889 int regname;
890 int writing;
891{
892 int i;
893
894 y_append = FALSE;
895 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
896 {
897 y_current = y_previous;
898 return;
899 }
900 i = regname;
901 if (VIM_ISDIGIT(i))
902 i -= '0';
903 else if (ASCII_ISLOWER(i))
904 i = CharOrdLow(i) + 10;
905 else if (ASCII_ISUPPER(i))
906 {
907 i = CharOrdUp(i) + 10;
908 y_append = TRUE;
909 }
910 else if (regname == '-')
911 i = DELETION_REGISTER;
912#ifdef FEAT_CLIPBOARD
913 /* When selection is not available, use register 0 instead of '*' */
914 else if (clip_star.available && regname == '*')
915 i = STAR_REGISTER;
916 /* When clipboard is not available, use register 0 instead of '+' */
917 else if (clip_plus.available && regname == '+')
918 i = PLUS_REGISTER;
919#endif
920#ifdef FEAT_DND
921 else if (!writing && regname == '~')
922 i = TILDE_REGISTER;
923#endif
924 else /* not 0-9, a-z, A-Z or '-': use register 0 */
925 i = 0;
926 y_current = &(y_regs[i]);
927 if (writing) /* remember the register we write into for do_put() */
928 y_previous = y_current;
929}
930
Bram Moolenaar8299df92004-07-10 09:47:34 +0000931#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
933 * When "regname" is a clipboard register, obtain the selection. If it's not
934 * available return zero, otherwise return "regname".
935 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000936 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937may_get_selection(regname)
938 int regname;
939{
940 if (regname == '*')
941 {
942 if (!clip_star.available)
943 regname = 0;
944 else
945 clip_get_selection(&clip_star);
946 }
947 else if (regname == '+')
948 {
949 if (!clip_plus.available)
950 regname = 0;
951 else
952 clip_get_selection(&clip_plus);
953 }
954 return regname;
955}
956#endif
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Obtain the contents of a "normal" register. The register is made empty.
960 * The returned pointer has allocated memory, use put_register() later.
961 */
962 void *
963get_register(name, copy)
964 int name;
965 int copy; /* make a copy, if FALSE make register empty. */
966{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000967 struct yankreg *reg;
968 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970#ifdef FEAT_CLIPBOARD
971 /* When Visual area changed, may have to update selection. Obtain the
972 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000973 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200975 if (clip_isautosel_star())
976 clip_update_selection(&clip_star);
977 may_get_selection(name);
978 }
979 if (name == '+' && clip_plus.available)
980 {
981 if (clip_isautosel_plus())
982 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 may_get_selection(name);
984 }
985#endif
986
987 get_yank_register(name, 0);
988 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
989 if (reg != NULL)
990 {
991 *reg = *y_current;
992 if (copy)
993 {
994 /* If we run out of memory some or all of the lines are empty. */
995 if (reg->y_size == 0)
996 reg->y_array = NULL;
997 else
998 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
999 * reg->y_size));
1000 if (reg->y_array != NULL)
1001 {
1002 for (i = 0; i < reg->y_size; ++i)
1003 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1004 }
1005 }
1006 else
1007 y_current->y_array = NULL;
1008 }
1009 return (void *)reg;
1010}
1011
1012/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001013 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
1015 void
1016put_register(name, reg)
1017 int name;
1018 void *reg;
1019{
1020 get_yank_register(name, 0);
1021 free_yank_all();
1022 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001023 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001025#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* Send text written to clipboard register to the clipboard. */
1027 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001030
1031 void
1032free_register(reg)
1033 void *reg;
1034{
1035 struct yankreg tmp;
1036
1037 tmp = *y_current;
1038 *y_current = *(struct yankreg *)reg;
1039 free_yank_all();
1040 vim_free(reg);
1041 *y_current = tmp;
1042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044#if defined(FEAT_MOUSE) || defined(PROTO)
1045/*
1046 * return TRUE if the current yank register has type MLINE
1047 */
1048 int
1049yank_register_mline(regname)
1050 int regname;
1051{
1052 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1053 return FALSE;
1054 if (regname == '_') /* black hole is always empty */
1055 return FALSE;
1056 get_yank_register(regname, FALSE);
1057 return (y_current->y_type == MLINE);
1058}
1059#endif
1060
1061/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001062 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001064 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
1066 int
1067do_record(c)
1068 int c;
1069{
Bram Moolenaard55de222007-05-06 13:38:48 +00001070 char_u *p;
1071 static int regname;
1072 struct yankreg *old_y_previous, *old_y_current;
1073 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 if (Recording == FALSE) /* start recording */
1076 {
1077 /* registers 0-9, a-z and " are allowed */
1078 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1079 retval = FAIL;
1080 else
1081 {
1082 Recording = TRUE;
1083 showmode();
1084 regname = c;
1085 retval = OK;
1086 }
1087 }
1088 else /* stop recording */
1089 {
1090 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001091 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1092 * needs to be removed again to put it in a register. exec_reg then
1093 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
1095 Recording = FALSE;
1096 MSG("");
1097 p = get_recorded();
1098 if (p == NULL)
1099 retval = FAIL;
1100 else
1101 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001102 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1103 vim_unescape_csi(p);
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
1106 * We don't want to change the default register here, so save and
1107 * restore the current register name.
1108 */
1109 old_y_previous = y_previous;
1110 old_y_current = y_current;
1111
1112 retval = stuff_yank(regname, p);
1113
1114 y_previous = old_y_previous;
1115 y_current = old_y_current;
1116 }
1117 }
1118 return retval;
1119}
1120
1121/*
1122 * Stuff string "p" into yank register "regname" as a single line (append if
1123 * uppercase). "p" must have been alloced.
1124 *
1125 * return FAIL for failure, OK otherwise
1126 */
1127 static int
1128stuff_yank(regname, p)
1129 int regname;
1130 char_u *p;
1131{
1132 char_u *lp;
1133 char_u **pp;
1134
1135 /* check for read-only register */
1136 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1137 {
1138 vim_free(p);
1139 return FAIL;
1140 }
1141 if (regname == '_') /* black hole: don't do anything */
1142 {
1143 vim_free(p);
1144 return OK;
1145 }
1146 get_yank_register(regname, TRUE);
1147 if (y_append && y_current->y_array != NULL)
1148 {
1149 pp = &(y_current->y_array[y_current->y_size - 1]);
1150 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1151 if (lp == NULL)
1152 {
1153 vim_free(p);
1154 return FAIL;
1155 }
1156 STRCPY(lp, *pp);
1157 STRCAT(lp, p);
1158 vim_free(p);
1159 vim_free(*pp);
1160 *pp = lp;
1161 }
1162 else
1163 {
1164 free_yank_all();
1165 if ((y_current->y_array =
1166 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1167 {
1168 vim_free(p);
1169 return FAIL;
1170 }
1171 y_current->y_array[0] = p;
1172 y_current->y_size = 1;
1173 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1174 }
1175 return OK;
1176}
1177
Bram Moolenaar42b94362009-05-26 16:12:37 +00001178static int execreg_lastc = NUL;
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180/*
1181 * execute a yank register: copy it into the stuff buffer
1182 *
1183 * return FAIL for failure, OK otherwise
1184 */
1185 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001186do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 int regname;
1188 int colon; /* insert ':' before each line */
1189 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001190 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 long i;
1193 char_u *p;
1194 int retval = OK;
1195 int remap;
1196
1197 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001198 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001199 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001200 {
1201 EMSG(_("E748: No previously used register"));
1202 return FAIL;
1203 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001204 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 /* check for valid regname */
1207 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001208 {
1209 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001211 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001212 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214#ifdef FEAT_CLIPBOARD
1215 regname = may_get_selection(regname);
1216#endif
1217
1218 if (regname == '_') /* black hole: don't stuff anything */
1219 return OK;
1220
1221#ifdef FEAT_CMDHIST
1222 if (regname == ':') /* use last command line */
1223 {
1224 if (last_cmdline == NULL)
1225 {
1226 EMSG(_(e_nolastcmd));
1227 return FAIL;
1228 }
1229 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1230 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001231 /* Escape all control characters with a CTRL-V */
1232 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001233 (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 +00001234 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001235 {
1236 /* When in Visual mode "'<,'>" will be prepended to the command.
1237 * Remove it when it's already there. */
1238 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001239 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001240 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001241 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001242 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001243 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245#endif
1246#ifdef FEAT_EVAL
1247 else if (regname == '=')
1248 {
1249 p = get_expr_line();
1250 if (p == NULL)
1251 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001252 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 vim_free(p);
1254 }
1255#endif
1256 else if (regname == '.') /* use last inserted text */
1257 {
1258 p = get_last_insert_save();
1259 if (p == NULL)
1260 {
1261 EMSG(_(e_noinstext));
1262 return FAIL;
1263 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001264 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 vim_free(p);
1266 }
1267 else
1268 {
1269 get_yank_register(regname, FALSE);
1270 if (y_current->y_array == NULL)
1271 return FAIL;
1272
1273 /* Disallow remaping for ":@r". */
1274 remap = colon ? REMAP_NONE : REMAP_YES;
1275
1276 /*
1277 * Insert lines into typeahead buffer, from last one to first one.
1278 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001279 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 for (i = y_current->y_size; --i >= 0; )
1281 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 char_u *escaped;
1283
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 /* insert NL between lines and after last line if type is MLINE */
1285 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1286 || addcr)
1287 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 return FAIL;
1290 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001291 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1292 if (escaped == NULL)
1293 return FAIL;
1294 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1295 vim_free(escaped);
1296 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001298 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 == FAIL)
1300 return FAIL;
1301 }
1302 Exec_reg = TRUE; /* disable the 'q' command */
1303 }
1304 return retval;
1305}
1306
1307/*
1308 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1309 * used only after other typeahead has been processed.
1310 */
1311 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001312put_reedit_in_typebuf(silent)
1313 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 char_u buf[3];
1316
1317 if (restart_edit != NUL)
1318 {
1319 if (restart_edit == 'V')
1320 {
1321 buf[0] = 'g';
1322 buf[1] = 'R';
1323 buf[2] = NUL;
1324 }
1325 else
1326 {
1327 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1328 buf[1] = NUL;
1329 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 restart_edit = NUL;
1332 }
1333}
1334
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001335/*
1336 * Insert register contents "s" into the typeahead buffer, so that it will be
1337 * executed again.
1338 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1339 * no remapping.
1340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001342put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001346 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 int retval = OK;
1349
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001350 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001352 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001354 {
1355 char_u *p;
1356
1357 if (esc)
1358 p = vim_strsave_escape_csi(s);
1359 else
1360 p = s;
1361 if (p == NULL)
1362 retval = FAIL;
1363 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001364 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1365 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001366 if (esc)
1367 vim_free(p);
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 return retval;
1372}
1373
1374/*
1375 * Insert a yank register: copy it into the Read buffer.
1376 * Used by CTRL-R command and middle mouse button in insert mode.
1377 *
1378 * return FAIL for failure, OK otherwise
1379 */
1380 int
1381insert_reg(regname, literally)
1382 int regname;
1383 int literally; /* insert literally, not as if typed */
1384{
1385 long i;
1386 int retval = OK;
1387 char_u *arg;
1388 int allocated;
1389
1390 /*
1391 * It is possible to get into an endless loop by having CTRL-R a in
1392 * register a and then, in insert mode, doing CTRL-R a.
1393 * If you hit CTRL-C, the loop will be broken here.
1394 */
1395 ui_breakcheck();
1396 if (got_int)
1397 return FAIL;
1398
1399 /* check for valid regname */
1400 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1401 return FAIL;
1402
1403#ifdef FEAT_CLIPBOARD
1404 regname = may_get_selection(regname);
1405#endif
1406
1407 if (regname == '.') /* insert last inserted text */
1408 retval = stuff_inserted(NUL, 1L, TRUE);
1409 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1410 {
1411 if (arg == NULL)
1412 return FAIL;
1413 stuffescaped(arg, literally);
1414 if (allocated)
1415 vim_free(arg);
1416 }
1417 else /* name or number register */
1418 {
1419 get_yank_register(regname, FALSE);
1420 if (y_current->y_array == NULL)
1421 retval = FAIL;
1422 else
1423 {
1424 for (i = 0; i < y_current->y_size; ++i)
1425 {
1426 stuffescaped(y_current->y_array[i], literally);
1427 /*
1428 * Insert a newline between lines and after last line if
1429 * y_type is MLINE.
1430 */
1431 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1432 stuffcharReadbuff('\n');
1433 }
1434 }
1435 }
1436
1437 return retval;
1438}
1439
1440/*
1441 * Stuff a string into the typeahead buffer, such that edit() will insert it
1442 * literally ("literally" TRUE) or interpret is as typed characters.
1443 */
1444 static void
1445stuffescaped(arg, literally)
1446 char_u *arg;
1447 int literally;
1448{
1449 int c;
1450 char_u *start;
1451
1452 while (*arg != NUL)
1453 {
1454 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1455 * stuff K_SPECIAL to get the effect of a special key when "literally"
1456 * is TRUE. */
1457 start = arg;
1458 while ((*arg >= ' '
1459#ifndef EBCDIC
1460 && *arg < DEL /* EBCDIC: chars above space are normal */
1461#endif
1462 )
1463 || (*arg == K_SPECIAL && !literally))
1464 ++arg;
1465 if (arg > start)
1466 stuffReadbuffLen(start, (long)(arg - start));
1467
1468 /* stuff a single special character */
1469 if (*arg != NUL)
1470 {
1471#ifdef FEAT_MBYTE
1472 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001473 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 else
1475#endif
1476 c = *arg++;
1477 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1478 stuffcharReadbuff(Ctrl_V);
1479 stuffcharReadbuff(c);
1480 }
1481 }
1482}
1483
1484/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001485 * If "regname" is a special register, return TRUE and store a pointer to its
1486 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001488 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489get_spec_reg(regname, argp, allocated, errmsg)
1490 int regname;
1491 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001492 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 int errmsg; /* give error message when failing */
1494{
1495 int cnt;
1496
1497 *argp = NULL;
1498 *allocated = FALSE;
1499 switch (regname)
1500 {
1501 case '%': /* file name */
1502 if (errmsg)
1503 check_fname(); /* will give emsg if not set */
1504 *argp = curbuf->b_fname;
1505 return TRUE;
1506
1507 case '#': /* alternate file name */
1508 *argp = getaltfname(errmsg); /* may give emsg if not set */
1509 return TRUE;
1510
1511#ifdef FEAT_EVAL
1512 case '=': /* result of expression */
1513 *argp = get_expr_line();
1514 *allocated = TRUE;
1515 return TRUE;
1516#endif
1517
1518 case ':': /* last command line */
1519 if (last_cmdline == NULL && errmsg)
1520 EMSG(_(e_nolastcmd));
1521 *argp = last_cmdline;
1522 return TRUE;
1523
1524 case '/': /* last search-pattern */
1525 if (last_search_pat() == NULL && errmsg)
1526 EMSG(_(e_noprevre));
1527 *argp = last_search_pat();
1528 return TRUE;
1529
1530 case '.': /* last inserted text */
1531 *argp = get_last_insert_save();
1532 *allocated = TRUE;
1533 if (*argp == NULL && errmsg)
1534 EMSG(_(e_noinstext));
1535 return TRUE;
1536
1537#ifdef FEAT_SEARCHPATH
1538 case Ctrl_F: /* Filename under cursor */
1539 case Ctrl_P: /* Path under cursor, expand via "path" */
1540 if (!errmsg)
1541 return FALSE;
1542 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001543 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 *allocated = TRUE;
1545 return TRUE;
1546#endif
1547
1548 case Ctrl_W: /* word under cursor */
1549 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1550 if (!errmsg)
1551 return FALSE;
1552 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1553 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1554 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1555 *allocated = TRUE;
1556 return TRUE;
1557
1558 case '_': /* black hole: always empty */
1559 *argp = (char_u *)"";
1560 return TRUE;
1561 }
1562
1563 return FALSE;
1564}
1565
1566/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001567 * Paste a yank register into the command line.
1568 * Only for non-special registers.
1569 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 * insert_reg() can't be used here, because special characters from the
1571 * register contents will be interpreted as commands.
1572 *
1573 * return FAIL for failure, OK otherwise
1574 */
1575 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001576cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 int regname;
1578 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001579 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 get_yank_register(regname, FALSE);
1584 if (y_current->y_array == NULL)
1585 return FAIL;
1586
1587 for (i = 0; i < y_current->y_size; ++i)
1588 {
1589 cmdline_paste_str(y_current->y_array[i], literally);
1590
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001591 /* Insert ^M between lines and after last line if type is MLINE.
1592 * Don't do this when "remcr" is TRUE and the next line is empty. */
1593 if (y_current->y_type == MLINE
1594 || (i < y_current->y_size - 1
1595 && !(remcr
1596 && i == y_current->y_size - 2
1597 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 cmdline_paste_str((char_u *)"\r", literally);
1599
1600 /* Check for CTRL-C, in case someone tries to paste a few thousand
1601 * lines and gets bored. */
1602 ui_breakcheck();
1603 if (got_int)
1604 return FAIL;
1605 }
1606 return OK;
1607}
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1610/*
1611 * Adjust the register name pointed to with "rp" for the clipboard being
1612 * used always and the clipboard being available.
1613 */
1614 void
1615adjust_clip_reg(rp)
1616 int *rp;
1617{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001618 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1619 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001620 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1621 {
1622 if (clip_unnamed != 0)
1623 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001624 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001625 else
1626 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1627 ? '+' : '*';
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!clip_star.available && *rp == '*')
1630 *rp = 0;
1631 if (!clip_plus.available && *rp == '+')
1632 *rp = 0;
1633}
1634#endif
1635
1636/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001637 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001639 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
1641 int
1642op_delete(oap)
1643 oparg_T *oap;
1644{
1645 int n;
1646 linenr_T lnum;
1647 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 char_u *newp, *oldp;
1649 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1651 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001652 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1655 return OK;
1656
1657 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1658 if (oap->empty)
1659 return u_save_cursor();
1660
1661 if (!curbuf->b_p_ma)
1662 {
1663 EMSG(_(e_modifiable));
1664 return FAIL;
1665 }
1666
1667#ifdef FEAT_CLIPBOARD
1668 adjust_clip_reg(&oap->regname);
1669#endif
1670
1671#ifdef FEAT_MBYTE
1672 if (has_mbyte)
1673 mb_adjust_opend(oap);
1674#endif
1675
Bram Moolenaard04b7502010-07-08 22:27:55 +02001676 /*
1677 * Imitate the strange Vi behaviour: If the delete spans more than one
1678 * line and motion_type == MCHAR and the result is a blank line, make the
1679 * delete linewise. Don't do this for the change command or Visual mode.
1680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001683 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001685 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 && oap->op_type == OP_DELETE)
1687 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001688 ptr = ml_get(oap->end.lnum) + oap->end.col;
1689 if (*ptr != NUL)
1690 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 ptr = skipwhite(ptr);
1692 if (*ptr == NUL && inindent(0))
1693 oap->motion_type = MLINE;
1694 }
1695
Bram Moolenaard04b7502010-07-08 22:27:55 +02001696 /*
1697 * Check for trying to delete (e.g. "D") in an empty line.
1698 * Note: For the change operator it is ok.
1699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if ( oap->motion_type == MCHAR
1701 && oap->line_count == 1
1702 && oap->op_type == OP_DELETE
1703 && *ml_get(oap->start.lnum) == NUL)
1704 {
1705 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001706 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 * 'cpoptions' (Vi compatible).
1708 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001709#ifdef FEAT_VIRTUALEDIT
1710 if (virtual_op)
1711 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1712 * marks as if it happened. */
1713 goto setmarks;
1714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1716 beep_flush();
1717 return OK;
1718 }
1719
Bram Moolenaard04b7502010-07-08 22:27:55 +02001720 /*
1721 * Do a yank of whatever we're about to delete.
1722 * If a yank register was specified, put the deleted text into that
1723 * register. For the black hole register '_' don't yank anything.
1724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (oap->regname != '_')
1726 {
1727 if (oap->regname != 0)
1728 {
1729 /* check for read-only register */
1730 if (!valid_yank_reg(oap->regname, TRUE))
1731 {
1732 beep_flush();
1733 return OK;
1734 }
1735 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1736 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1737 did_yank = TRUE;
1738 }
1739
1740 /*
1741 * Put deleted text into register 1 and shift number registers if the
1742 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001743 * Use the register name from before adjust_clip_reg() may have
1744 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001746 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 || oap->line_count > 1 || oap->use_reg_one)
1748 {
1749 y_current = &y_regs[9];
1750 free_yank_all(); /* free register nine */
1751 for (n = 9; n > 1; --n)
1752 y_regs[n] = y_regs[n - 1];
1753 y_previous = y_current = &y_regs[1];
1754 y_regs[1].y_array = NULL; /* set register one to empty */
1755 if (op_yank(oap, TRUE, FALSE) == OK)
1756 did_yank = TRUE;
1757 }
1758
Bram Moolenaar84298db2012-04-20 13:46:08 +02001759 /* Yank into small delete register when no named register specified
1760 * and the delete is within one line. */
1761 if ((
1762#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001763 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1764 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001765#endif
1766 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 && oap->line_count == 1)
1768 {
1769 oap->regname = '-';
1770 get_yank_register(oap->regname, TRUE);
1771 if (op_yank(oap, TRUE, FALSE) == OK)
1772 did_yank = TRUE;
1773 oap->regname = 0;
1774 }
1775
1776 /*
1777 * If there's too much stuff to fit in the yank register, then get a
1778 * confirmation before doing the delete. This is crude, but simple.
1779 * And it avoids doing a delete of something we can't put back if we
1780 * want.
1781 */
1782 if (!did_yank)
1783 {
1784 int msg_silent_save = msg_silent;
1785
1786 msg_silent = 0; /* must display the prompt */
1787 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1788 msg_silent = msg_silent_save;
1789 if (n != 'y')
1790 {
1791 EMSG(_(e_abort));
1792 return FAIL;
1793 }
1794 }
1795 }
1796
Bram Moolenaard04b7502010-07-08 22:27:55 +02001797 /*
1798 * block mode delete
1799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (oap->block_mode)
1801 {
1802 if (u_save((linenr_T)(oap->start.lnum - 1),
1803 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1804 return FAIL;
1805
1806 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1807 {
1808 block_prep(oap, &bd, lnum, TRUE);
1809 if (bd.textlen == 0) /* nothing to delete */
1810 continue;
1811
1812 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1813 if (lnum == curwin->w_cursor.lnum)
1814 {
1815 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1816# ifdef FEAT_VIRTUALEDIT
1817 curwin->w_cursor.coladd = 0;
1818# endif
1819 }
1820
1821 /* n == number of chars deleted
1822 * If we delete a TAB, it may be replaced by several characters.
1823 * Thus the number of characters may increase!
1824 */
1825 n = bd.textlen - bd.startspaces - bd.endspaces;
1826 oldp = ml_get(lnum);
1827 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1828 if (newp == NULL)
1829 continue;
1830 /* copy up to deleted part */
1831 mch_memmove(newp, oldp, (size_t)bd.textcol);
1832 /* insert spaces */
1833 copy_spaces(newp + bd.textcol,
1834 (size_t)(bd.startspaces + bd.endspaces));
1835 /* copy the part after the deleted part */
1836 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001837 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 /* replace the line */
1839 ml_replace(lnum, newp, FALSE);
1840 }
1841
1842 check_cursor_col();
1843 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1844 oap->end.lnum + 1, 0L);
1845 oap->line_count = 0; /* no lines deleted */
1846 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001847 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 if (oap->op_type == OP_CHANGE)
1850 {
1851 /* Delete the lines except the first one. Temporarily move the
1852 * cursor to the next line. Save the current line number, if the
1853 * last line is deleted it may be changed.
1854 */
1855 if (oap->line_count > 1)
1856 {
1857 lnum = curwin->w_cursor.lnum;
1858 ++curwin->w_cursor.lnum;
1859 del_lines((long)(oap->line_count - 1), TRUE);
1860 curwin->w_cursor.lnum = lnum;
1861 }
1862 if (u_save_cursor() == FAIL)
1863 return FAIL;
1864 if (curbuf->b_p_ai) /* don't delete indent */
1865 {
1866 beginline(BL_WHITE); /* cursor on first non-white */
1867 did_ai = TRUE; /* delete the indent when ESC hit */
1868 ai_col = curwin->w_cursor.col;
1869 }
1870 else
1871 beginline(0); /* cursor in column 0 */
1872 truncate_line(FALSE); /* delete the rest of the line */
1873 /* leave cursor past last char in line */
1874 if (oap->line_count > 1)
1875 u_clearline(); /* "U" command not possible after "2cc" */
1876 }
1877 else
1878 {
1879 del_lines(oap->line_count, TRUE);
1880 beginline(BL_WHITE | BL_FIX);
1881 u_clearline(); /* "U" command not possible after "dd" */
1882 }
1883 }
1884 else
1885 {
1886#ifdef FEAT_VIRTUALEDIT
1887 if (virtual_op)
1888 {
1889 int endcol = 0;
1890
1891 /* For virtualedit: break the tabs that are partly included. */
1892 if (gchar_pos(&oap->start) == '\t')
1893 {
1894 if (u_save_cursor() == FAIL) /* save first line for undo */
1895 return FAIL;
1896 if (oap->line_count == 1)
1897 endcol = getviscol2(oap->end.col, oap->end.coladd);
1898 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1899 oap->start = curwin->w_cursor;
1900 if (oap->line_count == 1)
1901 {
1902 coladvance(endcol);
1903 oap->end.col = curwin->w_cursor.col;
1904 oap->end.coladd = curwin->w_cursor.coladd;
1905 curwin->w_cursor = oap->start;
1906 }
1907 }
1908
1909 /* Break a tab only when it's included in the area. */
1910 if (gchar_pos(&oap->end) == '\t'
1911 && (int)oap->end.coladd < oap->inclusive)
1912 {
1913 /* save last line for undo */
1914 if (u_save((linenr_T)(oap->end.lnum - 1),
1915 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1916 return FAIL;
1917 curwin->w_cursor = oap->end;
1918 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1919 oap->end = curwin->w_cursor;
1920 curwin->w_cursor = oap->start;
1921 }
1922 }
1923#endif
1924
1925 if (oap->line_count == 1) /* delete characters within one line */
1926 {
1927 if (u_save_cursor() == FAIL) /* save line for undo */
1928 return FAIL;
1929
1930 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001931 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 && oap->op_type == OP_CHANGE
1933 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001934 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 display_dollar(oap->end.col - !oap->inclusive);
1936
1937 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1938
1939#ifdef FEAT_VIRTUALEDIT
1940 if (virtual_op)
1941 {
1942 /* fix up things for virtualedit-delete:
1943 * break the tabs which are going to get in our way
1944 */
1945 char_u *curline = ml_get_curline();
1946 int len = (int)STRLEN(curline);
1947
1948 if (oap->end.coladd != 0
1949 && (int)oap->end.col >= len - 1
1950 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1951 n++;
1952 /* Delete at least one char (e.g, when on a control char). */
1953 if (n == 0 && oap->start.coladd != oap->end.coladd)
1954 n = 1;
1955
1956 /* When deleted a char in the line, reset coladd. */
1957 if (gchar_cursor() != NUL)
1958 curwin->w_cursor.coladd = 0;
1959 }
1960#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001961 if (oap->op_type == OP_DELETE
1962 && oap->inclusive
1963 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001964 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1965 {
1966 /* Special case: gH<Del> deletes the last line. */
1967 del_lines(1L, FALSE);
1968 }
1969 else
1970 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001971 (void)del_bytes((long)n, !virtual_op,
1972 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975 else /* delete characters between lines */
1976 {
1977 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001978 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 /* save deleted and changed lines for undo */
1981 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1982 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1983 return FAIL;
1984
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001985 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 truncate_line(TRUE); /* delete from cursor to end of line */
1987
1988 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1989 ++curwin->w_cursor.lnum;
1990 del_lines((long)(oap->line_count - 2), FALSE);
1991
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001992 if (delete_last_line)
1993 oap->end.lnum = curbuf->b_ml.ml_line_count;
1994
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001995 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001996 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001997 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1998 {
1999 /* Special case: gH<Del> deletes the last line. */
2000 del_lines(1L, FALSE);
2001 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002002 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2003 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002004 }
2005 else
2006 {
2007 /* delete from start of line until op_end */
2008 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002009 (void)del_bytes((long)n, !virtual_op,
2010 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002011 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2012 }
2013 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002014 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017
2018 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2019
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002020#ifdef FEAT_VIRTUALEDIT
2021setmarks:
2022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (oap->block_mode)
2024 {
2025 curbuf->b_op_end.lnum = oap->end.lnum;
2026 curbuf->b_op_end.col = oap->start.col;
2027 }
2028 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 curbuf->b_op_end = oap->start;
2030 curbuf->b_op_start = oap->start;
2031
2032 return OK;
2033}
2034
2035#ifdef FEAT_MBYTE
2036/*
2037 * Adjust end of operating area for ending on a multi-byte character.
2038 * Used for deletion.
2039 */
2040 static void
2041mb_adjust_opend(oap)
2042 oparg_T *oap;
2043{
2044 char_u *p;
2045
2046 if (oap->inclusive)
2047 {
2048 p = ml_get(oap->end.lnum);
2049 oap->end.col += mb_tail_off(p, p + oap->end.col);
2050 }
2051}
2052#endif
2053
2054#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2055/*
2056 * Replace a whole area with one character.
2057 */
2058 int
2059op_replace(oap, c)
2060 oparg_T *oap;
2061 int c;
2062{
2063 int n, numc;
2064#ifdef FEAT_MBYTE
2065 int num_chars;
2066#endif
2067 char_u *newp, *oldp;
2068 size_t oldlen;
2069 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002070 char_u *after_p = NULL;
2071 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2074 return OK; /* nothing to do */
2075
Bram Moolenaard9820532013-11-04 01:41:17 +01002076 if (had_ctrl_v_cr)
2077 c = (c == -1 ? '\r' : '\n');
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#ifdef FEAT_MBYTE
2080 if (has_mbyte)
2081 mb_adjust_opend(oap);
2082#endif
2083
2084 if (u_save((linenr_T)(oap->start.lnum - 1),
2085 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2086 return FAIL;
2087
2088 /*
2089 * block mode replace
2090 */
2091 if (oap->block_mode)
2092 {
2093 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2094 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2095 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002096 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2098 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2099 continue; /* nothing to replace */
2100
2101 /* n == number of extra chars required
2102 * If we split a TAB, it may be replaced by several characters.
2103 * Thus the number of characters may increase!
2104 */
2105#ifdef FEAT_VIRTUALEDIT
2106 /* If the range starts in virtual space, count the initial
2107 * coladd offset as part of "startspaces" */
2108 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2109 {
2110 pos_T vpos;
2111
Bram Moolenaara1381de2009-11-03 15:44:21 +00002112 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 getvpos(&vpos, oap->start_vcol);
2114 bd.startspaces += vpos.coladd;
2115 n = bd.startspaces;
2116 }
2117 else
2118#endif
2119 /* allow for pre spaces */
2120 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2121
2122 /* allow for post spp */
2123 n += (bd.endspaces
2124#ifdef FEAT_VIRTUALEDIT
2125 && !bd.is_oneChar
2126#endif
2127 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2128 /* Figure out how many characters to replace. */
2129 numc = oap->end_vcol - oap->start_vcol + 1;
2130 if (bd.is_short && (!virtual_op || bd.is_MAX))
2131 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2132
2133#ifdef FEAT_MBYTE
2134 /* A double-wide character can be replaced only up to half the
2135 * times. */
2136 if ((*mb_char2cells)(c) > 1)
2137 {
2138 if ((numc & 1) && !bd.is_short)
2139 {
2140 ++bd.endspaces;
2141 ++n;
2142 }
2143 numc = numc / 2;
2144 }
2145
2146 /* Compute bytes needed, move character count to num_chars. */
2147 num_chars = numc;
2148 numc *= (*mb_char2len)(c);
2149#endif
2150 /* oldlen includes textlen, so don't double count */
2151 n += numc - bd.textlen;
2152
2153 oldp = ml_get_curline();
2154 oldlen = STRLEN(oldp);
2155 newp = alloc_check((unsigned)oldlen + 1 + n);
2156 if (newp == NULL)
2157 continue;
2158 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2159 /* copy up to deleted part */
2160 mch_memmove(newp, oldp, (size_t)bd.textcol);
2161 oldp += bd.textcol + bd.textlen;
2162 /* insert pre-spaces */
2163 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2164 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002165 /* -1/-2 is used for entering CR literally. */
2166 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002168#ifdef FEAT_MBYTE
2169 if (has_mbyte)
2170 {
2171 n = (int)STRLEN(newp);
2172 while (--num_chars >= 0)
2173 n += (*mb_char2bytes)(c, newp + n);
2174 }
2175 else
2176#endif
2177 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2178 if (!bd.is_short)
2179 {
2180 /* insert post-spaces */
2181 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2182 /* copy the part after the changed part */
2183 STRMOVE(newp + STRLEN(newp), oldp);
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002188 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002189 after_p = alloc_check(
2190 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002191 if (after_p != NULL)
2192 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 /* replace the line */
2195 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002196 if (after_p != NULL)
2197 {
2198 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2199 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2200 oap->end.lnum++;
2201 vim_free(after_p);
2202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 }
2205 else
2206 {
2207 /*
2208 * MCHAR and MLINE motion replace.
2209 */
2210 if (oap->motion_type == MLINE)
2211 {
2212 oap->start.col = 0;
2213 curwin->w_cursor.col = 0;
2214 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2215 if (oap->end.col)
2216 --oap->end.col;
2217 }
2218 else if (!oap->inclusive)
2219 dec(&(oap->end));
2220
2221 while (ltoreq(curwin->w_cursor, oap->end))
2222 {
2223 n = gchar_cursor();
2224 if (n != NUL)
2225 {
2226#ifdef FEAT_MBYTE
2227 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2228 {
2229 /* This is slow, but it handles replacing a single-byte
2230 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002231 if (curwin->w_cursor.lnum == oap->end.lnum)
2232 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 n = State;
2234 State = REPLACE;
2235 ins_char(c);
2236 State = n;
2237 /* Backup to the replaced character. */
2238 dec_cursor();
2239 }
2240 else
2241#endif
2242 {
2243#ifdef FEAT_VIRTUALEDIT
2244 if (n == TAB)
2245 {
2246 int end_vcol = 0;
2247
2248 if (curwin->w_cursor.lnum == oap->end.lnum)
2249 {
2250 /* oap->end has to be recalculated when
2251 * the tab breaks */
2252 end_vcol = getviscol2(oap->end.col,
2253 oap->end.coladd);
2254 }
2255 coladvance_force(getviscol());
2256 if (curwin->w_cursor.lnum == oap->end.lnum)
2257 getvpos(&oap->end, end_vcol);
2258 }
2259#endif
2260 pchar(curwin->w_cursor, c);
2261 }
2262 }
2263#ifdef FEAT_VIRTUALEDIT
2264 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2265 {
2266 int virtcols = oap->end.coladd;
2267
2268 if (curwin->w_cursor.lnum == oap->start.lnum
2269 && oap->start.col == oap->end.col && oap->start.coladd)
2270 virtcols -= oap->start.coladd;
2271
2272 /* oap->end has been trimmed so it's effectively inclusive;
2273 * as a result an extra +1 must be counted so we don't
2274 * trample the NUL byte. */
2275 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2276 curwin->w_cursor.col -= (virtcols + 1);
2277 for (; virtcols >= 0; virtcols--)
2278 {
2279 pchar(curwin->w_cursor, c);
2280 if (inc(&curwin->w_cursor) == -1)
2281 break;
2282 }
2283 }
2284#endif
2285
2286 /* Advance to next character, stop at the end of the file. */
2287 if (inc_cursor() == -1)
2288 break;
2289 }
2290 }
2291
2292 curwin->w_cursor = oap->start;
2293 check_cursor();
2294 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2295
2296 /* Set "'[" and "']" marks. */
2297 curbuf->b_op_start = oap->start;
2298 curbuf->b_op_end = oap->end;
2299
2300 return OK;
2301}
2302#endif
2303
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002304static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2308 */
2309 void
2310op_tilde(oap)
2311 oparg_T *oap;
2312{
2313 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002315 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316
2317 if (u_save((linenr_T)(oap->start.lnum - 1),
2318 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2319 return;
2320
2321 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (oap->block_mode) /* Visual block mode */
2323 {
2324 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2325 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002326 int one_change;
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 block_prep(oap, &bd, pos.lnum, FALSE);
2329 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002330 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2331 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002332
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002333#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002334 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
2336 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2337
Bram Moolenaar009b2592004-10-24 19:18:58 +00002338 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2339 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002341 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345 if (did_change)
2346 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2347 }
2348 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 if (oap->motion_type == MLINE)
2351 {
2352 oap->start.col = 0;
2353 pos.col = 0;
2354 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2355 if (oap->end.col)
2356 --oap->end.col;
2357 }
2358 else if (!oap->inclusive)
2359 dec(&(oap->end));
2360
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002361 if (pos.lnum == oap->end.lnum)
2362 did_change = swapchars(oap->op_type, &pos,
2363 oap->end.col - pos.col + 1);
2364 else
2365 for (;;)
2366 {
2367 did_change |= swapchars(oap->op_type, &pos,
2368 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2369 (int)STRLEN(ml_get_pos(&pos)));
2370 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2371 break;
2372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (did_change)
2374 {
2375 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2376 0L);
2377#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002378 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
2380 char_u *ptr;
2381 int count;
2382
2383 pos = oap->start;
2384 while (pos.lnum < oap->end.lnum)
2385 {
2386 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002387 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002388 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002390 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 pos.col = 0;
2392 pos.lnum++;
2393 }
2394 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2395 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002396 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002398 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400#endif
2401 }
2402 }
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (!did_change && oap->is_VIsual)
2405 /* No change: need to remove the Visual selection */
2406 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /*
2409 * Set '[ and '] marks.
2410 */
2411 curbuf->b_op_start = oap->start;
2412 curbuf->b_op_end = oap->end;
2413
2414 if (oap->line_count > p_report)
2415 {
2416 if (oap->line_count == 1)
2417 MSG(_("1 line changed"));
2418 else
2419 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2420 }
2421}
2422
2423/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002424 * Invoke swapchar() on "length" bytes at position "pos".
2425 * "pos" is advanced to just after the changed characters.
2426 * "length" is rounded up to include the whole last multi-byte character.
2427 * Also works correctly when the number of bytes changes.
2428 * Returns TRUE if some character was changed.
2429 */
2430 static int
2431swapchars(op_type, pos, length)
2432 int op_type;
2433 pos_T *pos;
2434 int length;
2435{
2436 int todo;
2437 int did_change = 0;
2438
2439 for (todo = length; todo > 0; --todo)
2440 {
2441# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002442 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002443 {
2444 int len = (*mb_ptr2len)(ml_get_pos(pos));
2445
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002446 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002447 if (len > 0)
2448 todo -= len - 1;
2449 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002450# endif
2451 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002452 if (inc(pos) == -1) /* at end of file */
2453 break;
2454 }
2455 return did_change;
2456}
2457
2458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * If op_type == OP_UPPER: make uppercase,
2460 * if op_type == OP_LOWER: make lowercase,
2461 * if op_type == OP_ROT13: do rot13 encoding,
2462 * else swap case of character at 'pos'
2463 * returns TRUE when something actually changed.
2464 */
2465 int
2466swapchar(op_type, pos)
2467 int op_type;
2468 pos_T *pos;
2469{
2470 int c;
2471 int nc;
2472
2473 c = gchar_pos(pos);
2474
2475 /* Only do rot13 encoding for ASCII characters. */
2476 if (c >= 0x80 && op_type == OP_ROT13)
2477 return FALSE;
2478
2479#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002480 if (op_type == OP_UPPER && c == 0xdf
2481 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002482 {
2483 pos_T sp = curwin->w_cursor;
2484
2485 /* Special handling of German sharp s: change to "SS". */
2486 curwin->w_cursor = *pos;
2487 del_char(FALSE);
2488 ins_char('S');
2489 ins_char('S');
2490 curwin->w_cursor = sp;
2491 inc(pos);
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2495 return FALSE;
2496#endif
2497 nc = c;
2498 if (MB_ISLOWER(c))
2499 {
2500 if (op_type == OP_ROT13)
2501 nc = ROT13(c, 'a');
2502 else if (op_type != OP_LOWER)
2503 nc = MB_TOUPPER(c);
2504 }
2505 else if (MB_ISUPPER(c))
2506 {
2507 if (op_type == OP_ROT13)
2508 nc = ROT13(c, 'A');
2509 else if (op_type != OP_UPPER)
2510 nc = MB_TOLOWER(c);
2511 }
2512 if (nc != c)
2513 {
2514#ifdef FEAT_MBYTE
2515 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2516 {
2517 pos_T sp = curwin->w_cursor;
2518
2519 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002520 /* don't use del_char(), it also removes composing chars */
2521 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 ins_char(nc);
2523 curwin->w_cursor = sp;
2524 }
2525 else
2526#endif
2527 pchar(*pos, nc);
2528 return TRUE;
2529 }
2530 return FALSE;
2531}
2532
2533#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2534/*
2535 * op_insert - Insert and append operators for Visual mode.
2536 */
2537 void
2538op_insert(oap, count1)
2539 oparg_T *oap;
2540 long count1;
2541{
2542 long ins_len, pre_textlen = 0;
2543 char_u *firstline, *ins_text;
2544 struct block_def bd;
2545 int i;
2546
2547 /* edit() changes this - record it for OP_APPEND */
2548 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2549
2550 /* vis block is still marked. Get rid of it now. */
2551 curwin->w_cursor.lnum = oap->start.lnum;
2552 update_screen(INVERTED);
2553
2554 if (oap->block_mode)
2555 {
2556#ifdef FEAT_VIRTUALEDIT
2557 /* When 'virtualedit' is used, need to insert the extra spaces before
2558 * doing block_prep(). When only "block" is used, virtual edit is
2559 * already disabled, but still need it when calling
2560 * coladvance_force(). */
2561 if (curwin->w_cursor.coladd > 0)
2562 {
2563 int old_ve_flags = ve_flags;
2564
2565 ve_flags = VE_ALL;
2566 if (u_save_cursor() == FAIL)
2567 return;
2568 coladvance_force(oap->op_type == OP_APPEND
2569 ? oap->end_vcol + 1 : getviscol());
2570 if (oap->op_type == OP_APPEND)
2571 --curwin->w_cursor.col;
2572 ve_flags = old_ve_flags;
2573 }
2574#endif
2575 /* Get the info about the block before entering the text */
2576 block_prep(oap, &bd, oap->start.lnum, TRUE);
2577 firstline = ml_get(oap->start.lnum) + bd.textcol;
2578 if (oap->op_type == OP_APPEND)
2579 firstline += bd.textlen;
2580 pre_textlen = (long)STRLEN(firstline);
2581 }
2582
2583 if (oap->op_type == OP_APPEND)
2584 {
2585 if (oap->block_mode
2586#ifdef FEAT_VIRTUALEDIT
2587 && curwin->w_cursor.coladd == 0
2588#endif
2589 )
2590 {
2591 /* Move the cursor to the character right of the block. */
2592 curwin->w_set_curswant = TRUE;
2593 while (*ml_get_cursor() != NUL
2594 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2595 ++curwin->w_cursor.col;
2596 if (bd.is_short && !bd.is_MAX)
2597 {
2598 /* First line was too short, make it longer and adjust the
2599 * values in "bd". */
2600 if (u_save_cursor() == FAIL)
2601 return;
2602 for (i = 0; i < bd.endspaces; ++i)
2603 ins_char(' ');
2604 bd.textlen += bd.endspaces;
2605 }
2606 }
2607 else
2608 {
2609 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002610 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 /* Works just like an 'i'nsert on the next character. */
2613 if (!lineempty(curwin->w_cursor.lnum)
2614 && oap->start_vcol != oap->end_vcol)
2615 inc_cursor();
2616 }
2617 }
2618
2619 edit(NUL, FALSE, (linenr_T)count1);
2620
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002621 /* If user has moved off this line, we don't know what to do, so do
2622 * nothing.
2623 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2624 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return;
2626
2627 if (oap->block_mode)
2628 {
2629 struct block_def bd2;
2630
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002631 /* The user may have moved the cursor before inserting something, try
2632 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002633 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002634 {
2635 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002636 && oap->start.col
2637#ifdef FEAT_VIRTUALEDIT
2638 + oap->start.coladd
2639#endif
2640 != curbuf->b_op_start_orig.col
2641#ifdef FEAT_VIRTUALEDIT
2642 + curbuf->b_op_start_orig.coladd
2643#endif
2644 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002645 {
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 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2648 - oap->start_vcol;
2649 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2650 }
2651 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002652 && oap->end.col
2653#ifdef FEAT_VIRTUALEDIT
2654 + oap->end.coladd
2655#endif
2656 >= curbuf->b_op_start_orig.col
2657#ifdef FEAT_VIRTUALEDIT
2658 + curbuf->b_op_start_orig.coladd
2659#endif
2660 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002661 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002662 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002663 /* reset pre_textlen to the value of OP_INSERT */
2664 pre_textlen += bd.textlen;
2665 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2666 - oap->start_vcol;
2667 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2668 oap->op_type = OP_INSERT;
2669 }
2670 }
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /*
2673 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002674 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * Don't do this when "$" used, end-of-line will have changed.
2676 */
2677 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2678 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2679 {
2680 if (oap->op_type == OP_APPEND)
2681 {
2682 pre_textlen += bd2.textlen - bd.textlen;
2683 if (bd2.endspaces)
2684 --bd2.textlen;
2685 }
2686 bd.textcol = bd2.textcol;
2687 bd.textlen = bd2.textlen;
2688 }
2689
2690 /*
2691 * Subsequent calls to ml_get() flush the firstline data - take a
2692 * copy of the required string.
2693 */
2694 firstline = ml_get(oap->start.lnum) + bd.textcol;
2695 if (oap->op_type == OP_APPEND)
2696 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002697 if (pre_textlen >= 0
2698 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 ins_text = vim_strnsave(firstline, (int)ins_len);
2701 if (ins_text != NULL)
2702 {
2703 /* block handled here */
2704 if (u_save(oap->start.lnum,
2705 (linenr_T)(oap->end.lnum + 1)) == OK)
2706 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2707 &bd);
2708
2709 curwin->w_cursor.col = oap->start.col;
2710 check_cursor();
2711 vim_free(ins_text);
2712 }
2713 }
2714 }
2715}
2716#endif
2717
2718/*
2719 * op_change - handle a change operation
2720 *
2721 * return TRUE if edit() returns because of a CTRL-O command
2722 */
2723 int
2724op_change(oap)
2725 oparg_T *oap;
2726{
2727 colnr_T l;
2728 int retval;
2729#ifdef FEAT_VISUALEXTRA
2730 long offset;
2731 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002732 long ins_len;
2733 long pre_textlen = 0;
2734 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 char_u *firstline;
2736 char_u *ins_text, *newp, *oldp;
2737 struct block_def bd;
2738#endif
2739
2740 l = oap->start.col;
2741 if (oap->motion_type == MLINE)
2742 {
2743 l = 0;
2744#ifdef FEAT_SMARTINDENT
2745 if (!p_paste && curbuf->b_p_si
2746# ifdef FEAT_CINDENT
2747 && !curbuf->b_p_cin
2748# endif
2749 )
2750 can_si = TRUE; /* It's like opening a new line, do si */
2751#endif
2752 }
2753
2754 /* First delete the text in the region. In an empty buffer only need to
2755 * save for undo */
2756 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2757 {
2758 if (u_save_cursor() == FAIL)
2759 return FALSE;
2760 }
2761 else if (op_delete(oap) == FAIL)
2762 return FALSE;
2763
2764 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2765 && !virtual_op)
2766 inc_cursor();
2767
2768#ifdef FEAT_VISUALEXTRA
2769 /* check for still on same line (<CR> in inserted text meaningless) */
2770 /* skip blank lines too */
2771 if (oap->block_mode)
2772 {
2773# ifdef FEAT_VIRTUALEDIT
2774 /* Add spaces before getting the current line length. */
2775 if (virtual_op && (curwin->w_cursor.coladd > 0
2776 || gchar_cursor() == NUL))
2777 coladvance_force(getviscol());
2778# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002779 firstline = ml_get(oap->start.lnum);
2780 pre_textlen = (long)STRLEN(firstline);
2781 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 bd.textcol = curwin->w_cursor.col;
2783 }
2784#endif
2785
2786#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2787 if (oap->motion_type == MLINE)
2788 fix_indent();
2789#endif
2790
2791 retval = edit(NUL, FALSE, (linenr_T)1);
2792
2793#ifdef FEAT_VISUALEXTRA
2794 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002795 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002797 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002799 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002801 /* Auto-indenting may have changed the indent. If the cursor was past
2802 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002804 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002806 long new_indent = (long)(skipwhite(firstline) - firstline);
2807
2808 pre_textlen += new_indent - pre_indent;
2809 bd.textcol += new_indent - pre_indent;
2810 }
2811
2812 ins_len = (long)STRLEN(firstline) - pre_textlen;
2813 if (ins_len > 0)
2814 {
2815 /* Subsequent calls to ml_get() flush the firstline data - take a
2816 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002819 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2821 linenr++)
2822 {
2823 block_prep(oap, &bd, linenr, TRUE);
2824 if (!bd.is_short || virtual_op)
2825 {
2826# ifdef FEAT_VIRTUALEDIT
2827 pos_T vpos;
2828
2829 /* If the block starts in virtual space, count the
2830 * initial coladd offset as part of "startspaces" */
2831 if (bd.is_short)
2832 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002833 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836 else
2837 vpos.coladd = 0;
2838# endif
2839 oldp = ml_get(linenr);
2840 newp = alloc_check((unsigned)(STRLEN(oldp)
2841# ifdef FEAT_VIRTUALEDIT
2842 + vpos.coladd
2843# endif
2844 + ins_len + 1));
2845 if (newp == NULL)
2846 continue;
2847 /* copy up to block start */
2848 mch_memmove(newp, oldp, (size_t)bd.textcol);
2849 offset = bd.textcol;
2850# ifdef FEAT_VIRTUALEDIT
2851 copy_spaces(newp + offset, (size_t)vpos.coladd);
2852 offset += vpos.coladd;
2853# endif
2854 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2855 offset += ins_len;
2856 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002857 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 ml_replace(linenr, newp, FALSE);
2859 }
2860 }
2861 check_cursor();
2862
2863 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2864 }
2865 vim_free(ins_text);
2866 }
2867 }
2868#endif
2869
2870 return retval;
2871}
2872
2873/*
2874 * set all the yank registers to empty (called from main())
2875 */
2876 void
2877init_yank()
2878{
2879 int i;
2880
2881 for (i = 0; i < NUM_REGISTERS; ++i)
2882 y_regs[i].y_array = NULL;
2883}
2884
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002885#if defined(EXITFREE) || defined(PROTO)
2886 void
2887clear_registers()
2888{
2889 int i;
2890
2891 for (i = 0; i < NUM_REGISTERS; ++i)
2892 {
2893 y_current = &y_regs[i];
2894 if (y_current->y_array != NULL)
2895 free_yank_all();
2896 }
2897}
2898#endif
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900/*
2901 * Free "n" lines from the current yank register.
2902 * Called for normal freeing and in case of error.
2903 */
2904 static void
2905free_yank(n)
2906 long n;
2907{
2908 if (y_current->y_array != NULL)
2909 {
2910 long i;
2911
2912 for (i = n; --i >= 0; )
2913 {
2914#ifdef AMIGA /* only for very slow machines */
2915 if ((i & 1023) == 1023) /* this may take a while */
2916 {
2917 /*
2918 * This message should never cause a hit-return message.
2919 * Overwrite this message with any next message.
2920 */
2921 ++no_wait_return;
2922 smsg((char_u *)_("freeing %ld lines"), i + 1);
2923 --no_wait_return;
2924 msg_didout = FALSE;
2925 msg_col = 0;
2926 }
2927#endif
2928 vim_free(y_current->y_array[i]);
2929 }
2930 vim_free(y_current->y_array);
2931 y_current->y_array = NULL;
2932#ifdef AMIGA
2933 if (n >= 1000)
2934 MSG("");
2935#endif
2936 }
2937}
2938
2939 static void
2940free_yank_all()
2941{
2942 free_yank(y_current->y_size);
2943}
2944
2945/*
2946 * Yank the text between "oap->start" and "oap->end" into a yank register.
2947 * If we are to append (uppercase register), we first yank into a new yank
2948 * register and then concatenate the old and the new one (so we keep the old
2949 * one in case of out-of-memory).
2950 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002951 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
2953 int
2954op_yank(oap, deleting, mess)
2955 oparg_T *oap;
2956 int deleting;
2957 int mess;
2958{
2959 long y_idx; /* index in y_array[] */
2960 struct yankreg *curr; /* copy of y_current */
2961 struct yankreg newreg; /* new yank register when appending */
2962 char_u **new_ptr;
2963 linenr_T lnum; /* current line number */
2964 long j;
2965 int yanktype = oap->motion_type;
2966 long yanklines = oap->line_count;
2967 linenr_T yankendlnum = oap->end.lnum;
2968 char_u *p;
2969 char_u *pnew;
2970 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002971#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002972 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 /* check for read-only register */
2976 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2977 {
2978 beep_flush();
2979 return FAIL;
2980 }
2981 if (oap->regname == '_') /* black hole: nothing to do */
2982 return OK;
2983
2984#ifdef FEAT_CLIPBOARD
2985 if (!clip_star.available && oap->regname == '*')
2986 oap->regname = 0;
2987 else if (!clip_plus.available && oap->regname == '+')
2988 oap->regname = 0;
2989#endif
2990
2991 if (!deleting) /* op_delete() already set y_current */
2992 get_yank_register(oap->regname, TRUE);
2993
2994 curr = y_current;
2995 /* append to existing contents */
2996 if (y_append && y_current->y_array != NULL)
2997 y_current = &newreg;
2998 else
2999 free_yank_all(); /* free previously yanked lines */
3000
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003001 /*
3002 * If the cursor was in column 1 before and after the movement, and the
3003 * operator is not inclusive, the yank is always linewise.
3004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if ( oap->motion_type == MCHAR
3006 && oap->start.col == 0
3007 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003009 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 && oap->end.col == 0
3011 && yanklines > 1)
3012 {
3013 yanktype = MLINE;
3014 --yankendlnum;
3015 --yanklines;
3016 }
3017
3018 y_current->y_size = yanklines;
3019 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3022 yanklines), TRUE);
3023
3024 if (y_current->y_array == NULL)
3025 {
3026 y_current = curr;
3027 return FAIL;
3028 }
3029
3030 y_idx = 0;
3031 lnum = oap->start.lnum;
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (oap->block_mode)
3034 {
3035 /* Visual block mode */
3036 y_current->y_type = MBLOCK; /* set the yank register type */
3037 y_current->y_width = oap->end_vcol - oap->start_vcol;
3038
3039 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3040 y_current->y_width--;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3044 {
3045 switch (y_current->y_type)
3046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 case MBLOCK:
3048 block_prep(oap, &bd, lnum, FALSE);
3049 if (yank_copy_line(&bd, y_idx) == FAIL)
3050 goto fail;
3051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 case MLINE:
3054 if ((y_current->y_array[y_idx] =
3055 vim_strsave(ml_get(lnum))) == NULL)
3056 goto fail;
3057 break;
3058
3059 case MCHAR:
3060 {
3061 colnr_T startcol = 0, endcol = MAXCOL;
3062#ifdef FEAT_VIRTUALEDIT
3063 int is_oneChar = FALSE;
3064 colnr_T cs, ce;
3065#endif
3066 p = ml_get(lnum);
3067 bd.startspaces = 0;
3068 bd.endspaces = 0;
3069
3070 if (lnum == oap->start.lnum)
3071 {
3072 startcol = oap->start.col;
3073#ifdef FEAT_VIRTUALEDIT
3074 if (virtual_op)
3075 {
3076 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3077 if (ce != cs && oap->start.coladd > 0)
3078 {
3079 /* Part of a tab selected -- but don't
3080 * double-count it. */
3081 bd.startspaces = (ce - cs + 1)
3082 - oap->start.coladd;
3083 startcol++;
3084 }
3085 }
3086#endif
3087 }
3088
3089 if (lnum == oap->end.lnum)
3090 {
3091 endcol = oap->end.col;
3092#ifdef FEAT_VIRTUALEDIT
3093 if (virtual_op)
3094 {
3095 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3096 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3097# ifdef FEAT_MBYTE
3098 /* Don't add space for double-wide
3099 * char; endcol will be on last byte
3100 * of multi-byte char. */
3101 && (*mb_head_off)(p, p + endcol) == 0
3102# endif
3103 ))
3104 {
3105 if (oap->start.lnum == oap->end.lnum
3106 && oap->start.col == oap->end.col)
3107 {
3108 /* Special case: inside a single char */
3109 is_oneChar = TRUE;
3110 bd.startspaces = oap->end.coladd
3111 - oap->start.coladd + oap->inclusive;
3112 endcol = startcol;
3113 }
3114 else
3115 {
3116 bd.endspaces = oap->end.coladd
3117 + oap->inclusive;
3118 endcol -= oap->inclusive;
3119 }
3120 }
3121 }
3122#endif
3123 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003124 if (endcol == MAXCOL)
3125 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (startcol > endcol
3127#ifdef FEAT_VIRTUALEDIT
3128 || is_oneChar
3129#endif
3130 )
3131 bd.textlen = 0;
3132 else
3133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 bd.textlen = endcol - startcol + oap->inclusive;
3135 }
3136 bd.textstart = p + startcol;
3137 if (yank_copy_line(&bd, y_idx) == FAIL)
3138 goto fail;
3139 break;
3140 }
3141 /* NOTREACHED */
3142 }
3143 }
3144
3145 if (curr != y_current) /* append the new block to the old block */
3146 {
3147 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3148 (curr->y_size + y_current->y_size)), TRUE);
3149 if (new_ptr == NULL)
3150 goto fail;
3151 for (j = 0; j < curr->y_size; ++j)
3152 new_ptr[j] = curr->y_array[j];
3153 vim_free(curr->y_array);
3154 curr->y_array = new_ptr;
3155
3156 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3157 curr->y_type = MLINE;
3158
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003159 /* Concatenate the last line of the old block with the first line of
3160 * the new block, unless being Vi compatible. */
3161 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3164 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3165 if (pnew == NULL)
3166 {
3167 y_idx = y_current->y_size - 1;
3168 goto fail;
3169 }
3170 STRCPY(pnew, curr->y_array[--j]);
3171 STRCAT(pnew, y_current->y_array[0]);
3172 vim_free(curr->y_array[j]);
3173 vim_free(y_current->y_array[0]);
3174 curr->y_array[j++] = pnew;
3175 y_idx = 1;
3176 }
3177 else
3178 y_idx = 0;
3179 while (y_idx < y_current->y_size)
3180 curr->y_array[j++] = y_current->y_array[y_idx++];
3181 curr->y_size = j;
3182 vim_free(y_current->y_array);
3183 y_current = curr;
3184 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003185 if (curwin->w_p_rnu)
3186 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 if (mess) /* Display message about yank? */
3188 {
3189 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 && yanklines == 1)
3192 yanklines = 0;
3193 /* Some versions of Vi use ">=" here, some don't... */
3194 if (yanklines > p_report)
3195 {
3196 /* redisplay now, so message is not deleted */
3197 update_topline_redraw();
3198 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003199 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003200 if (oap->block_mode)
3201 MSG(_("block of 1 line yanked"));
3202 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003203 MSG(_("1 line yanked"));
3204 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003205 else if (oap->block_mode)
3206 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
3208 smsg((char_u *)_("%ld lines yanked"), yanklines);
3209 }
3210 }
3211
3212 /*
3213 * Set "'[" and "']" marks.
3214 */
3215 curbuf->b_op_start = oap->start;
3216 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003217 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003218 {
3219 curbuf->b_op_start.col = 0;
3220 curbuf->b_op_end.col = MAXCOL;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223#ifdef FEAT_CLIPBOARD
3224 /*
3225 * If we were yanking to the '*' register, send result to clipboard.
3226 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3227 * to the '*' register.
3228 */
3229 if (clip_star.available
3230 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003231 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003232 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
3234 if (curr != &(y_regs[STAR_REGISTER]))
3235 /* Copy the text from register 0 to the clipboard register. */
3236 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3237
3238 clip_own_selection(&clip_star);
3239 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003240# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003241 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244
3245# ifdef FEAT_X11
3246 /*
3247 * If we were yanking to the '+' register, send result to selection.
3248 * Also copy to the '*' register, in case auto-select is off.
3249 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003250 if (clip_plus.available
3251 && (curr == &(y_regs[PLUS_REGISTER])
3252 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003253 && ((clip_unnamed | clip_unnamed_saved) &
3254 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003256 if (curr != &(y_regs[PLUS_REGISTER]))
3257 /* Copy the text from register 0 to the clipboard register. */
3258 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 clip_own_selection(&clip_plus);
3261 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003262 if (!clip_isautosel_star() && !did_star
3263 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3266 clip_own_selection(&clip_star);
3267 clip_gen_set_selection(&clip_star);
3268 }
3269 }
3270# endif
3271#endif
3272
3273 return OK;
3274
3275fail: /* free the allocated lines */
3276 free_yank(y_idx + 1);
3277 y_current = curr;
3278 return FAIL;
3279}
3280
3281 static int
3282yank_copy_line(bd, y_idx)
3283 struct block_def *bd;
3284 long y_idx;
3285{
3286 char_u *pnew;
3287
3288 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3289 == NULL)
3290 return FAIL;
3291 y_current->y_array[y_idx] = pnew;
3292 copy_spaces(pnew, (size_t)bd->startspaces);
3293 pnew += bd->startspaces;
3294 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3295 pnew += bd->textlen;
3296 copy_spaces(pnew, (size_t)bd->endspaces);
3297 pnew += bd->endspaces;
3298 *pnew = NUL;
3299 return OK;
3300}
3301
3302#ifdef FEAT_CLIPBOARD
3303/*
3304 * Make a copy of the y_current register to register "reg".
3305 */
3306 static void
3307copy_yank_reg(reg)
3308 struct yankreg *reg;
3309{
3310 struct yankreg *curr = y_current;
3311 long j;
3312
3313 y_current = reg;
3314 free_yank_all();
3315 *y_current = *curr;
3316 y_current->y_array = (char_u **)lalloc_clear(
3317 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3318 if (y_current->y_array == NULL)
3319 y_current->y_size = 0;
3320 else
3321 for (j = 0; j < y_current->y_size; ++j)
3322 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3323 {
3324 free_yank(j);
3325 y_current->y_size = 0;
3326 break;
3327 }
3328 y_current = curr;
3329}
3330#endif
3331
3332/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003333 * Put contents of register "regname" into the text.
3334 * Caller must check "regname" to be valid!
3335 * "flags": PUT_FIXINDENT make indent look nice
3336 * PUT_CURSEND leave cursor after end of new text
3337 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 void
3340do_put(regname, dir, count, flags)
3341 int regname;
3342 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3343 long count;
3344 int flags;
3345{
3346 char_u *ptr;
3347 char_u *newp, *oldp;
3348 int yanklen;
3349 int totlen = 0; /* init for gcc */
3350 linenr_T lnum;
3351 colnr_T col;
3352 long i; /* index in y_array[] */
3353 int y_type;
3354 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int oldlen;
3356 long y_width = 0;
3357 colnr_T vcol;
3358 int delcount;
3359 int incr = 0;
3360 long j;
3361 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 char_u **y_array = NULL;
3363 long nr_lines = 0;
3364 pos_T new_cursor;
3365 int indent;
3366 int orig_indent = 0; /* init for gcc */
3367 int indent_diff = 0; /* init for gcc */
3368 int first_indent = TRUE;
3369 int lendiff = 0;
3370 pos_T old_pos;
3371 char_u *insert_string = NULL;
3372 int allocated = FALSE;
3373 long cnt;
3374
3375#ifdef FEAT_CLIPBOARD
3376 /* Adjust register name for "unnamed" in 'clipboard'. */
3377 adjust_clip_reg(&regname);
3378 (void)may_get_selection(regname);
3379#endif
3380
3381 if (flags & PUT_FIXINDENT)
3382 orig_indent = get_indent();
3383
3384 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3385 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3386
3387 /*
3388 * Using inserted text works differently, because the register includes
3389 * special characters (newlines, etc.).
3390 */
3391 if (regname == '.')
3392 {
3393 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3394 (count == -1 ? 'O' : 'i')), count, FALSE);
3395 /* Putting the text is done later, so can't really move the cursor to
3396 * the next character. Use "l" to simulate it. */
3397 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3398 stuffcharReadbuff('l');
3399 return;
3400 }
3401
3402 /*
3403 * For special registers '%' (file name), '#' (alternate file name) and
3404 * ':' (last command line), etc. we have to create a fake yank register.
3405 */
3406 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3407 {
3408 if (insert_string == NULL)
3409 return;
3410 }
3411
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003412#ifdef FEAT_AUTOCMD
3413 /* Autocommands may be executed when saving lines for undo, which may make
3414 * y_array invalid. Start undo now to avoid that. */
3415 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3416#endif
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (insert_string != NULL)
3419 {
3420 y_type = MCHAR;
3421#ifdef FEAT_EVAL
3422 if (regname == '=')
3423 {
3424 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003425 * characters.
3426 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 for (;;)
3428 {
3429 y_size = 0;
3430 ptr = insert_string;
3431 while (ptr != NULL)
3432 {
3433 if (y_array != NULL)
3434 y_array[y_size] = ptr;
3435 ++y_size;
3436 ptr = vim_strchr(ptr, '\n');
3437 if (ptr != NULL)
3438 {
3439 if (y_array != NULL)
3440 *ptr = NUL;
3441 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003442 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (*ptr == NUL)
3444 {
3445 y_type = MLINE;
3446 break;
3447 }
3448 }
3449 }
3450 if (y_array != NULL)
3451 break;
3452 y_array = (char_u **)alloc((unsigned)
3453 (y_size * sizeof(char_u *)));
3454 if (y_array == NULL)
3455 goto end;
3456 }
3457 }
3458 else
3459#endif
3460 {
3461 y_size = 1; /* use fake one-line yank register */
3462 y_array = &insert_string;
3463 }
3464 }
3465 else
3466 {
3467 get_yank_register(regname, FALSE);
3468
3469 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 y_size = y_current->y_size;
3472 y_array = y_current->y_array;
3473 }
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (y_type == MLINE)
3476 {
3477 if (flags & PUT_LINE_SPLIT)
3478 {
3479 /* "p" or "P" in Visual mode: split the lines to put the text in
3480 * between. */
3481 if (u_save_cursor() == FAIL)
3482 goto end;
3483 ptr = vim_strsave(ml_get_cursor());
3484 if (ptr == NULL)
3485 goto end;
3486 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3487 vim_free(ptr);
3488
3489 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3490 if (ptr == NULL)
3491 goto end;
3492 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3493 ++nr_lines;
3494 dir = FORWARD;
3495 }
3496 if (flags & PUT_LINE_FORWARD)
3497 {
3498 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003499 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 dir = FORWARD;
3501 }
3502 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3503 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3507 y_type = MLINE;
3508
3509 if (y_size == 0 || y_array == NULL)
3510 {
3511 EMSG2(_("E353: Nothing in register %s"),
3512 regname == 0 ? (char_u *)"\"" : transchar(regname));
3513 goto end;
3514 }
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (y_type == MBLOCK)
3517 {
3518 lnum = curwin->w_cursor.lnum + y_size + 1;
3519 if (lnum > curbuf->b_ml.ml_line_count)
3520 lnum = curbuf->b_ml.ml_line_count + 1;
3521 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3522 goto end;
3523 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003524 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 lnum = curwin->w_cursor.lnum;
3527#ifdef FEAT_FOLDING
3528 /* Correct line number for closed fold. Don't move the cursor yet,
3529 * u_save() uses it. */
3530 if (dir == BACKWARD)
3531 (void)hasFolding(lnum, &lnum, NULL);
3532 else
3533 (void)hasFolding(lnum, NULL, &lnum);
3534#endif
3535 if (dir == FORWARD)
3536 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003537 /* In an empty buffer the empty line is going to be replaced, include
3538 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003539 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 goto end;
3541#ifdef FEAT_FOLDING
3542 if (dir == FORWARD)
3543 curwin->w_cursor.lnum = lnum - 1;
3544 else
3545 curwin->w_cursor.lnum = lnum;
3546 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3547#endif
3548 }
3549 else if (u_save_cursor() == FAIL)
3550 goto end;
3551
3552 yanklen = (int)STRLEN(y_array[0]);
3553
3554#ifdef FEAT_VIRTUALEDIT
3555 if (ve_flags == VE_ALL && y_type == MCHAR)
3556 {
3557 if (gchar_cursor() == TAB)
3558 {
3559 /* Don't need to insert spaces when "p" on the last position of a
3560 * tab or "P" on the first position. */
3561 if (dir == FORWARD
3562 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3563 : curwin->w_cursor.coladd > 0)
3564 coladvance_force(getviscol());
3565 else
3566 curwin->w_cursor.coladd = 0;
3567 }
3568 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3569 coladvance_force(getviscol() + (dir == FORWARD));
3570 }
3571#endif
3572
3573 lnum = curwin->w_cursor.lnum;
3574 col = curwin->w_cursor.col;
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 /*
3577 * Block mode
3578 */
3579 if (y_type == MBLOCK)
3580 {
3581 char c = gchar_cursor();
3582 colnr_T endcol2 = 0;
3583
3584 if (dir == FORWARD && c != NUL)
3585 {
3586#ifdef FEAT_VIRTUALEDIT
3587 if (ve_flags == VE_ALL)
3588 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3589 else
3590#endif
3591 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3592
3593#ifdef FEAT_MBYTE
3594 if (has_mbyte)
3595 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003596 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 else
3598#endif
3599#ifdef FEAT_VIRTUALEDIT
3600 if (c != TAB || ve_flags != VE_ALL)
3601#endif
3602 ++curwin->w_cursor.col;
3603 ++col;
3604 }
3605 else
3606 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3607
3608#ifdef FEAT_VIRTUALEDIT
3609 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003610 if (ve_flags == VE_ALL
3611 && (curwin->w_cursor.coladd > 0
3612 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 if (dir == FORWARD && c == NUL)
3615 ++col;
3616 if (dir != FORWARD && c != NUL)
3617 ++curwin->w_cursor.col;
3618 if (c == TAB)
3619 {
3620 if (dir == BACKWARD && curwin->w_cursor.col)
3621 curwin->w_cursor.col--;
3622 if (dir == FORWARD && col - 1 == endcol2)
3623 curwin->w_cursor.col++;
3624 }
3625 }
3626 curwin->w_cursor.coladd = 0;
3627#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003628 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 for (i = 0; i < y_size; ++i)
3630 {
3631 int spaces;
3632 char shortline;
3633
3634 bd.startspaces = 0;
3635 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 vcol = 0;
3637 delcount = 0;
3638
3639 /* add a new line */
3640 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3641 {
3642 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3643 (colnr_T)1, FALSE) == FAIL)
3644 break;
3645 ++nr_lines;
3646 }
3647 /* get the old line and advance to the position to insert at */
3648 oldp = ml_get_curline();
3649 oldlen = (int)STRLEN(oldp);
3650 for (ptr = oldp; vcol < col && *ptr; )
3651 {
3652 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003653 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 vcol += incr;
3655 }
3656 bd.textcol = (colnr_T)(ptr - oldp);
3657
3658 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3659
3660 if (vcol < col) /* line too short, padd with spaces */
3661 bd.startspaces = col - vcol;
3662 else if (vcol > col)
3663 {
3664 bd.endspaces = vcol - col;
3665 bd.startspaces = incr - bd.endspaces;
3666 --bd.textcol;
3667 delcount = 1;
3668#ifdef FEAT_MBYTE
3669 if (has_mbyte)
3670 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3671#endif
3672 if (oldp[bd.textcol] != TAB)
3673 {
3674 /* Only a Tab can be split into spaces. Other
3675 * characters will have to be moved to after the
3676 * block, causing misalignment. */
3677 delcount = 0;
3678 bd.endspaces = 0;
3679 }
3680 }
3681
3682 yanklen = (int)STRLEN(y_array[i]);
3683
3684 /* calculate number of spaces required to fill right side of block*/
3685 spaces = y_width + 1;
3686 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003687 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (spaces < 0)
3689 spaces = 0;
3690
3691 /* insert the new text */
3692 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3693 newp = alloc_check((unsigned)totlen + oldlen + 1);
3694 if (newp == NULL)
3695 break;
3696 /* copy part up to cursor to new line */
3697 ptr = newp;
3698 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3699 ptr += bd.textcol;
3700 /* may insert some spaces before the new text */
3701 copy_spaces(ptr, (size_t)bd.startspaces);
3702 ptr += bd.startspaces;
3703 /* insert the new text */
3704 for (j = 0; j < count; ++j)
3705 {
3706 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3707 ptr += yanklen;
3708
3709 /* insert block's trailing spaces only if there's text behind */
3710 if ((j < count - 1 || !shortline) && spaces)
3711 {
3712 copy_spaces(ptr, (size_t)spaces);
3713 ptr += spaces;
3714 }
3715 }
3716 /* may insert some spaces after the new text */
3717 copy_spaces(ptr, (size_t)bd.endspaces);
3718 ptr += bd.endspaces;
3719 /* move the text after the cursor to the end of the line. */
3720 mch_memmove(ptr, oldp + bd.textcol + delcount,
3721 (size_t)(oldlen - bd.textcol - delcount + 1));
3722 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3723
3724 ++curwin->w_cursor.lnum;
3725 if (i == 0)
3726 curwin->w_cursor.col += bd.startspaces;
3727 }
3728
3729 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3730
3731 /* Set '[ mark. */
3732 curbuf->b_op_start = curwin->w_cursor;
3733 curbuf->b_op_start.lnum = lnum;
3734
3735 /* adjust '] mark */
3736 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3737 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003738# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003740# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (flags & PUT_CURSEND)
3742 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003743 colnr_T len;
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 curwin->w_cursor = curbuf->b_op_end;
3746 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003747
3748 /* in Insert mode we might be after the NUL, correct for that */
3749 len = (colnr_T)STRLEN(ml_get_curline());
3750 if (curwin->w_cursor.col > len)
3751 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 else
3754 curwin->w_cursor.lnum = lnum;
3755 }
3756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 /*
3759 * Character or Line mode
3760 */
3761 if (y_type == MCHAR)
3762 {
3763 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3764 * char */
3765 if (dir == FORWARD && gchar_cursor() != NUL)
3766 {
3767#ifdef FEAT_MBYTE
3768 if (has_mbyte)
3769 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003770 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /* put it on the next of the multi-byte character. */
3773 col += bytelen;
3774 if (yanklen)
3775 {
3776 curwin->w_cursor.col += bytelen;
3777 curbuf->b_op_end.col += bytelen;
3778 }
3779 }
3780 else
3781#endif
3782 {
3783 ++col;
3784 if (yanklen)
3785 {
3786 ++curwin->w_cursor.col;
3787 ++curbuf->b_op_end.col;
3788 }
3789 }
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 curbuf->b_op_start = curwin->w_cursor;
3792 }
3793 /*
3794 * Line mode: BACKWARD is the same as FORWARD on the previous line
3795 */
3796 else if (dir == BACKWARD)
3797 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003798 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 /*
3801 * simple case: insert into current line
3802 */
3803 if (y_type == MCHAR && y_size == 1)
3804 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003805 do {
3806 totlen = count * yanklen;
3807 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003809 oldp = ml_get(lnum);
3810 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3811 if (newp == NULL)
3812 goto end; /* alloc() gave an error message */
3813 mch_memmove(newp, oldp, (size_t)col);
3814 ptr = newp + col;
3815 for (i = 0; i < count; ++i)
3816 {
3817 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3818 ptr += yanklen;
3819 }
3820 STRMOVE(ptr, oldp + col);
3821 ml_replace(lnum, newp, FALSE);
3822 /* Place cursor on last putted char. */
3823 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003824 {
3825 /* make sure curwin->w_virtcol is updated */
3826 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003827 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003830 if (VIsual_active)
3831 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003832 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003833
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003834 if (VIsual_active) /* reset lnum to the last visual line */
3835 lnum--;
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 curbuf->b_op_end = curwin->w_cursor;
3838 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3839 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3840 ++curwin->w_cursor.col;
3841 changed_bytes(lnum, col);
3842 }
3843 else
3844 {
3845 /*
3846 * Insert at least one line. When y_type is MCHAR, break the first
3847 * line in two.
3848 */
3849 for (cnt = 1; cnt <= count; ++cnt)
3850 {
3851 i = 0;
3852 if (y_type == MCHAR)
3853 {
3854 /*
3855 * Split the current line in two at the insert position.
3856 * First insert y_array[size - 1] in front of second line.
3857 * Then append y_array[0] to first line.
3858 */
3859 lnum = new_cursor.lnum;
3860 ptr = ml_get(lnum) + col;
3861 totlen = (int)STRLEN(y_array[y_size - 1]);
3862 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3863 if (newp == NULL)
3864 goto error;
3865 STRCPY(newp, y_array[y_size - 1]);
3866 STRCAT(newp, ptr);
3867 /* insert second line */
3868 ml_append(lnum, newp, (colnr_T)0, FALSE);
3869 vim_free(newp);
3870
3871 oldp = ml_get(lnum);
3872 newp = alloc_check((unsigned)(col + yanklen + 1));
3873 if (newp == NULL)
3874 goto error;
3875 /* copy first part of line */
3876 mch_memmove(newp, oldp, (size_t)col);
3877 /* append to first line */
3878 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3879 ml_replace(lnum, newp, FALSE);
3880
3881 curwin->w_cursor.lnum = lnum;
3882 i = 1;
3883 }
3884
3885 for (; i < y_size; ++i)
3886 {
3887 if ((y_type != MCHAR || i < y_size - 1)
3888 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3889 == FAIL)
3890 goto error;
3891 lnum++;
3892 ++nr_lines;
3893 if (flags & PUT_FIXINDENT)
3894 {
3895 old_pos = curwin->w_cursor;
3896 curwin->w_cursor.lnum = lnum;
3897 ptr = ml_get(lnum);
3898 if (cnt == count && i == y_size - 1)
3899 lendiff = (int)STRLEN(ptr);
3900#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3901 if (*ptr == '#' && preprocs_left())
3902 indent = 0; /* Leave # lines at start */
3903 else
3904#endif
3905 if (*ptr == NUL)
3906 indent = 0; /* Ignore empty lines */
3907 else if (first_indent)
3908 {
3909 indent_diff = orig_indent - get_indent();
3910 indent = orig_indent;
3911 first_indent = FALSE;
3912 }
3913 else if ((indent = get_indent() + indent_diff) < 0)
3914 indent = 0;
3915 (void)set_indent(indent, 0);
3916 curwin->w_cursor = old_pos;
3917 /* remember how many chars were removed */
3918 if (cnt == count && i == y_size - 1)
3919 lendiff -= (int)STRLEN(ml_get(lnum));
3920 }
3921 }
3922 }
3923
3924error:
3925 /* Adjust marks. */
3926 if (y_type == MLINE)
3927 {
3928 curbuf->b_op_start.col = 0;
3929 if (dir == FORWARD)
3930 curbuf->b_op_start.lnum++;
3931 }
3932 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3933 (linenr_T)MAXLNUM, nr_lines, 0L);
3934
3935 /* note changed text for displaying and folding */
3936 if (y_type == MCHAR)
3937 changed_lines(curwin->w_cursor.lnum, col,
3938 curwin->w_cursor.lnum + 1, nr_lines);
3939 else
3940 changed_lines(curbuf->b_op_start.lnum, 0,
3941 curbuf->b_op_start.lnum, nr_lines);
3942
3943 /* put '] mark at last inserted character */
3944 curbuf->b_op_end.lnum = lnum;
3945 /* correct length for change in indent */
3946 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3947 if (col > 1)
3948 curbuf->b_op_end.col = col - 1;
3949 else
3950 curbuf->b_op_end.col = 0;
3951
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003952 if (flags & PUT_CURSLINE)
3953 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003954 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003955 curwin->w_cursor.lnum = lnum;
3956 beginline(BL_WHITE | BL_FIX);
3957 }
3958 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 /* put cursor after inserted text */
3961 if (y_type == MLINE)
3962 {
3963 if (lnum >= curbuf->b_ml.ml_line_count)
3964 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3965 else
3966 curwin->w_cursor.lnum = lnum + 1;
3967 curwin->w_cursor.col = 0;
3968 }
3969 else
3970 {
3971 curwin->w_cursor.lnum = lnum;
3972 curwin->w_cursor.col = col;
3973 }
3974 }
3975 else if (y_type == MLINE)
3976 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003977 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 curwin->w_cursor.col = 0;
3979 if (dir == FORWARD)
3980 ++curwin->w_cursor.lnum;
3981 beginline(BL_WHITE | BL_FIX);
3982 }
3983 else /* put cursor on first inserted character */
3984 curwin->w_cursor = new_cursor;
3985 }
3986 }
3987
3988 msgmore(nr_lines);
3989 curwin->w_set_curswant = TRUE;
3990
3991end:
3992 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003994 if (regname == '=')
3995 vim_free(y_array);
3996
Bram Moolenaar033d8882013-09-25 23:24:57 +02003997 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003998
Bram Moolenaar677ee682005-01-27 14:41:15 +00003999 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004000 adjust_cursor_eol();
4001}
4002
4003/*
4004 * When the cursor is on the NUL past the end of the line and it should not be
4005 * there move it left.
4006 */
4007 void
4008adjust_cursor_eol()
4009{
4010 if (curwin->w_cursor.col > 0
4011 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004012#ifdef FEAT_VIRTUALEDIT
4013 && (ve_flags & VE_ONEMORE) == 0
4014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 && !(restart_edit || (State & INSERT)))
4016 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004017 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004018 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_VIRTUALEDIT
4021 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004022 {
4023 colnr_T scol, ecol;
4024
4025 /* Coladd is set to the width of the last character. */
4026 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4027 curwin->w_cursor.coladd = ecol - scol + 1;
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#endif
4030 }
4031}
4032
4033#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4034/*
4035 * Return TRUE if lines starting with '#' should be left aligned.
4036 */
4037 int
4038preprocs_left()
4039{
4040 return
4041# ifdef FEAT_SMARTINDENT
4042# ifdef FEAT_CINDENT
4043 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4044# else
4045 curbuf->b_p_si
4046# endif
4047# endif
4048# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004049 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4050 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051# endif
4052 ;
4053}
4054#endif
4055
4056/* Return the character name of the register with the given number */
4057 int
4058get_register_name(num)
4059 int num;
4060{
4061 if (num == -1)
4062 return '"';
4063 else if (num < 10)
4064 return num + '0';
4065 else if (num == DELETION_REGISTER)
4066 return '-';
4067#ifdef FEAT_CLIPBOARD
4068 else if (num == STAR_REGISTER)
4069 return '*';
4070 else if (num == PLUS_REGISTER)
4071 return '+';
4072#endif
4073 else
4074 {
4075#ifdef EBCDIC
4076 int i;
4077
4078 /* EBCDIC is really braindead ... */
4079 i = 'a' + (num - 10);
4080 if (i > 'i')
4081 i += 7;
4082 if (i > 'r')
4083 i += 8;
4084 return i;
4085#else
4086 return num + 'a' - 10;
4087#endif
4088 }
4089}
4090
4091/*
4092 * ":dis" and ":registers": Display the contents of the yank registers.
4093 */
4094 void
4095ex_display(eap)
4096 exarg_T *eap;
4097{
4098 int i, n;
4099 long j;
4100 char_u *p;
4101 struct yankreg *yb;
4102 int name;
4103 int attr;
4104 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004105#ifdef FEAT_MBYTE
4106 int clen;
4107#else
4108# define clen 1
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 if (arg != NULL && *arg == NUL)
4112 arg = NULL;
4113 attr = hl_attr(HLF_8);
4114
4115 /* Highlight title */
4116 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4117 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4118 {
4119 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004120 if (arg != NULL && vim_strchr(arg, name) == NULL
4121#ifdef ONE_CLIPBOARD
4122 /* Star register and plus register contain the same thing. */
4123 && (name != '*' || vim_strchr(arg, '+') == NULL)
4124#endif
4125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 continue; /* did not ask for this register */
4127
4128#ifdef FEAT_CLIPBOARD
4129 /* Adjust register name for "unnamed" in 'clipboard'.
4130 * When it's a clipboard register, fill it with the current contents
4131 * of the clipboard. */
4132 adjust_clip_reg(&name);
4133 (void)may_get_selection(name);
4134#endif
4135
4136 if (i == -1)
4137 {
4138 if (y_previous != NULL)
4139 yb = y_previous;
4140 else
4141 yb = &(y_regs[0]);
4142 }
4143 else
4144 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004145
4146#ifdef FEAT_EVAL
4147 if (name == MB_TOLOWER(redir_reg)
4148 || (redir_reg == '"' && yb == y_previous))
4149 continue; /* do not list register being written to, the
4150 * pointer can be freed */
4151#endif
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (yb->y_array != NULL)
4154 {
4155 msg_putchar('\n');
4156 msg_putchar('"');
4157 msg_putchar(name);
4158 MSG_PUTS(" ");
4159
4160 n = (int)Columns - 6;
4161 for (j = 0; j < yb->y_size && n > 1; ++j)
4162 {
4163 if (j)
4164 {
4165 MSG_PUTS_ATTR("^J", attr);
4166 n -= 2;
4167 }
4168 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004171 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004172#endif
4173 msg_outtrans_len(p, clen);
4174#ifdef FEAT_MBYTE
4175 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#endif
4177 }
4178 }
4179 if (n > 1 && yb->y_type == MLINE)
4180 MSG_PUTS_ATTR("^J", attr);
4181 out_flush(); /* show one line at a time */
4182 }
4183 ui_breakcheck();
4184 }
4185
4186 /*
4187 * display last inserted text
4188 */
4189 if ((p = get_last_insert()) != NULL
4190 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4191 {
4192 MSG_PUTS("\n\". ");
4193 dis_msg(p, TRUE);
4194 }
4195
4196 /*
4197 * display last command line
4198 */
4199 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4200 && !got_int)
4201 {
4202 MSG_PUTS("\n\": ");
4203 dis_msg(last_cmdline, FALSE);
4204 }
4205
4206 /*
4207 * display current file name
4208 */
4209 if (curbuf->b_fname != NULL
4210 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4211 {
4212 MSG_PUTS("\n\"% ");
4213 dis_msg(curbuf->b_fname, FALSE);
4214 }
4215
4216 /*
4217 * display alternate file name
4218 */
4219 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4220 {
4221 char_u *fname;
4222 linenr_T dummy;
4223
4224 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4225 {
4226 MSG_PUTS("\n\"# ");
4227 dis_msg(fname, FALSE);
4228 }
4229 }
4230
4231 /*
4232 * display last search pattern
4233 */
4234 if (last_search_pat() != NULL
4235 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4236 {
4237 MSG_PUTS("\n\"/ ");
4238 dis_msg(last_search_pat(), FALSE);
4239 }
4240
4241#ifdef FEAT_EVAL
4242 /*
4243 * display last used expression
4244 */
4245 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4246 && !got_int)
4247 {
4248 MSG_PUTS("\n\"= ");
4249 dis_msg(expr_line, FALSE);
4250 }
4251#endif
4252}
4253
4254/*
4255 * display a string for do_dis()
4256 * truncate at end of screen line
4257 */
4258 static void
4259dis_msg(p, skip_esc)
4260 char_u *p;
4261 int skip_esc; /* if TRUE, ignore trailing ESC */
4262{
4263 int n;
4264#ifdef FEAT_MBYTE
4265 int l;
4266#endif
4267
4268 n = (int)Columns - 6;
4269 while (*p != NUL
4270 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4271 && (n -= ptr2cells(p)) >= 0)
4272 {
4273#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004274 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276 msg_outtrans_len(p, l);
4277 p += l;
4278 }
4279 else
4280#endif
4281 msg_outtrans_len(p++, 1);
4282 }
4283 ui_breakcheck();
4284}
4285
Bram Moolenaar81340392012-06-06 16:12:59 +02004286#if defined(FEAT_COMMENTS) || defined(PROTO)
4287/*
4288 * If "process" is TRUE and the line begins with a comment leader (possibly
4289 * after some white space), return a pointer to the text after it. Put a boolean
4290 * value indicating whether the line ends with an unclosed comment in
4291 * "is_comment".
4292 * line - line to be processed,
4293 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004294 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004295 * include_space - whether to also skip space following the comment leader,
4296 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004297 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004298 */
4299 static char_u *
4300skip_comment(line, process, include_space, is_comment)
4301 char_u *line;
4302 int process;
4303 int include_space;
4304 int *is_comment;
4305{
4306 char_u *comment_flags = NULL;
4307 int lead_len;
4308 int leader_offset = get_last_leader_offset(line, &comment_flags);
4309
4310 *is_comment = FALSE;
4311 if (leader_offset != -1)
4312 {
4313 /* Let's check whether the line ends with an unclosed comment.
4314 * If the last comment leader has COM_END in flags, there's no comment.
4315 */
4316 while (*comment_flags)
4317 {
4318 if (*comment_flags == COM_END
4319 || *comment_flags == ':')
4320 break;
4321 ++comment_flags;
4322 }
4323 if (*comment_flags != COM_END)
4324 *is_comment = TRUE;
4325 }
4326
4327 if (process == FALSE)
4328 return line;
4329
4330 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4331
4332 if (lead_len == 0)
4333 return line;
4334
4335 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004336 * - COM_END,
4337 * - colon,
4338 * whichever comes first.
4339 */
4340 while (*comment_flags)
4341 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004342 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004343 || *comment_flags == ':')
4344 {
4345 break;
4346 }
4347 ++comment_flags;
4348 }
4349
4350 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004351 * starting with a closing part of a three-part comment. That's good,
4352 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004353 */
4354 if (*comment_flags == ':' || *comment_flags == NUL)
4355 line += lead_len;
4356
4357 return line;
4358}
4359#endif
4360
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004362 * Join 'count' lines (minimal 2) at cursor position.
4363 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004364 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4365 * leaders should not be removed.
4366 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4367 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004369 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 */
4371 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004372do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004373 long count;
4374 int insert_space;
4375 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004376 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004377 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004379 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004380 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004381 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004383 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004384 int endcurr1 = NUL;
4385 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004386 int currsize = 0; /* size of the current line */
4387 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004389 colnr_T col = 0;
4390 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004391#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004392 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004393 int remove_comments = (use_formatoptions == TRUE)
4394 && has_format_option(FO_REMOVE_COMS);
4395 int prev_was_comment;
4396#endif
4397
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004399 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4400 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4401 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004403 /* Allocate an array to store the number of spaces inserted before each
4404 * line. We will use it to pre-compute the length of the new line and the
4405 * proper placement of each original line in the new one. */
4406 spaces = lalloc_clear((long_u)count, TRUE);
4407 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004409#if defined(FEAT_COMMENTS) || defined(PROTO)
4410 if (remove_comments)
4411 {
4412 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4413 if (comments == NULL)
4414 {
4415 vim_free(spaces);
4416 return FAIL;
4417 }
4418 }
4419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 * Don't move anything, just compute the final line length
4423 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004425 for (t = 0; t < count; ++t)
4426 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004427 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004428 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004429 {
4430 /* Set the '[ mark. */
4431 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4432 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4433 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004434#if defined(FEAT_COMMENTS) || defined(PROTO)
4435 if (remove_comments)
4436 {
4437 /* We don't want to remove the comment leader if the
4438 * previous line is not a comment. */
4439 if (t > 0 && prev_was_comment)
4440 {
4441
4442 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4443 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004444 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004445 curr = new_curr;
4446 }
4447 else
4448 curr = skip_comment(curr, FALSE, insert_space,
4449 &prev_was_comment);
4450 }
4451#endif
4452
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004453 if (insert_space && t > 0)
4454 {
4455 curr = skipwhite(curr);
4456 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4457#ifdef FEAT_MBYTE
4458 && (!has_format_option(FO_MBYTE_JOIN)
4459 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4460 && (!has_format_option(FO_MBYTE_JOIN2)
4461 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4462#endif
4463 )
4464 {
4465 /* don't add a space if the line is ending in a space */
4466 if (endcurr1 == ' ')
4467 endcurr1 = endcurr2;
4468 else
4469 ++spaces[t];
4470 /* extra space when 'joinspaces' set and line ends in '.' */
4471 if ( p_js
4472 && (endcurr1 == '.'
4473 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4474 && (endcurr1 == '?' || endcurr1 == '!'))))
4475 ++spaces[t];
4476 }
4477 }
4478 currsize = (int)STRLEN(curr);
4479 sumsize += currsize + spaces[t];
4480 endcurr1 = endcurr2 = NUL;
4481 if (insert_space && currsize > 0)
4482 {
4483#ifdef FEAT_MBYTE
4484 if (has_mbyte)
4485 {
4486 cend = curr + currsize;
4487 mb_ptr_back(curr, cend);
4488 endcurr1 = (*mb_ptr2char)(cend);
4489 if (cend > curr)
4490 {
4491 mb_ptr_back(curr, cend);
4492 endcurr2 = (*mb_ptr2char)(cend);
4493 }
4494 }
4495 else
4496#endif
4497 {
4498 endcurr1 = *(curr + currsize - 1);
4499 if (currsize > 1)
4500 endcurr2 = *(curr + currsize - 2);
4501 }
4502 }
4503 line_breakcheck();
4504 if (got_int)
4505 {
4506 ret = FAIL;
4507 goto theend;
4508 }
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004511 /* store the column position before last line */
4512 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004514 /* allocate the space for the new line */
4515 newp = alloc_check((unsigned)(sumsize + 1));
4516 cend = newp + sumsize;
4517 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004519 /*
4520 * Move affected lines to the new long one.
4521 *
4522 * Move marks from each deleted line to the joined line, adjusting the
4523 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4524 * should not really be a problem.
4525 */
4526 for (t = count - 1; ; --t)
4527 {
4528 cend -= currsize;
4529 mch_memmove(cend, curr, (size_t)currsize);
4530 if (spaces[t] > 0)
4531 {
4532 cend -= spaces[t];
4533 copy_spaces(cend, (size_t)(spaces[t]));
4534 }
4535 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004536 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004537 if (t == 0)
4538 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004539 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004540#if defined(FEAT_COMMENTS) || defined(PROTO)
4541 if (remove_comments)
4542 curr += comments[t - 1];
4543#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004544 if (insert_space && t > 1)
4545 curr = skipwhite(curr);
4546 currsize = (int)STRLEN(curr);
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4549
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004550 if (setmark)
4551 {
4552 /* Set the '] mark. */
4553 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4554 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4555 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 /* Only report the change in the first line here, del_lines() will report
4558 * the deleted line. */
4559 changed_lines(curwin->w_cursor.lnum, currsize,
4560 curwin->w_cursor.lnum + 1, 0L);
4561
4562 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004563 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 * briefly, and then move it back. After del_lines() the cursor may
4565 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 */
4567 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004569 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 curwin->w_cursor.lnum = t;
4571
4572 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004573 * Set the cursor column:
4574 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004575 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004577 curwin->w_cursor.col =
4578 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581#ifdef FEAT_VIRTUALEDIT
4582 curwin->w_cursor.coladd = 0;
4583#endif
4584 curwin->w_set_curswant = TRUE;
4585
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004586theend:
4587 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004588#if defined(FEAT_COMMENTS) || defined(PROTO)
4589 if (remove_comments)
4590 vim_free(comments);
4591#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004592 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593}
4594
4595#ifdef FEAT_COMMENTS
4596/*
4597 * Return TRUE if the two comment leaders given are the same. "lnum" is
4598 * the first line. White-space is ignored. Note that the whole of
4599 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4600 */
4601 static int
4602same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4603 linenr_T lnum;
4604 int leader1_len;
4605 char_u *leader1_flags;
4606 int leader2_len;
4607 char_u *leader2_flags;
4608{
4609 int idx1 = 0, idx2 = 0;
4610 char_u *p;
4611 char_u *line1;
4612 char_u *line2;
4613
4614 if (leader1_len == 0)
4615 return (leader2_len == 0);
4616
4617 /*
4618 * If first leader has 'f' flag, the lines can be joined only if the
4619 * second line does not have a leader.
4620 * If first leader has 'e' flag, the lines can never be joined.
4621 * If fist leader has 's' flag, the lines can only be joined if there is
4622 * some text after it and the second line has the 'm' flag.
4623 */
4624 if (leader1_flags != NULL)
4625 {
4626 for (p = leader1_flags; *p && *p != ':'; ++p)
4627 {
4628 if (*p == COM_FIRST)
4629 return (leader2_len == 0);
4630 if (*p == COM_END)
4631 return FALSE;
4632 if (*p == COM_START)
4633 {
4634 if (*(ml_get(lnum) + leader1_len) == NUL)
4635 return FALSE;
4636 if (leader2_flags == NULL || leader2_len == 0)
4637 return FALSE;
4638 for (p = leader2_flags; *p && *p != ':'; ++p)
4639 if (*p == COM_MIDDLE)
4640 return TRUE;
4641 return FALSE;
4642 }
4643 }
4644 }
4645
4646 /*
4647 * Get current line and next line, compare the leaders.
4648 * The first line has to be saved, only one line can be locked at a time.
4649 */
4650 line1 = vim_strsave(ml_get(lnum));
4651 if (line1 != NULL)
4652 {
4653 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4654 ;
4655 line2 = ml_get(lnum + 1);
4656 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4657 {
4658 if (!vim_iswhite(line2[idx2]))
4659 {
4660 if (line1[idx1++] != line2[idx2])
4661 break;
4662 }
4663 else
4664 while (vim_iswhite(line1[idx1]))
4665 ++idx1;
4666 }
4667 vim_free(line1);
4668 }
4669 return (idx2 == leader2_len && idx1 == leader1_len);
4670}
4671#endif
4672
4673/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004674 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 */
4676 void
4677op_format(oap, keep_cursor)
4678 oparg_T *oap;
4679 int keep_cursor; /* keep cursor on same text char */
4680{
4681 long old_line_count = curbuf->b_ml.ml_line_count;
4682
4683 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4684 * can put it back there. */
4685 curwin->w_cursor = oap->cursor_start;
4686
4687 if (u_save((linenr_T)(oap->start.lnum - 1),
4688 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4689 return;
4690 curwin->w_cursor = oap->start;
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 if (oap->is_VIsual)
4693 /* When there is no change: need to remove the Visual selection */
4694 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 /* Set '[ mark at the start of the formatted area */
4697 curbuf->b_op_start = oap->start;
4698
4699 /* For "gw" remember the cursor position and put it back below (adjusted
4700 * for joined and split lines). */
4701 if (keep_cursor)
4702 saved_cursor = oap->cursor_start;
4703
Bram Moolenaar81a82092008-03-12 16:27:00 +00004704 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 /*
4707 * Leave the cursor at the first non-blank of the last formatted line.
4708 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4709 * line, so "." will do the next lines.
4710 */
4711 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4712 ++curwin->w_cursor.lnum;
4713 beginline(BL_WHITE | BL_FIX);
4714 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4715 msgmore(old_line_count);
4716
4717 /* put '] mark on the end of the formatted area */
4718 curbuf->b_op_end = curwin->w_cursor;
4719
4720 if (keep_cursor)
4721 {
4722 curwin->w_cursor = saved_cursor;
4723 saved_cursor.lnum = 0;
4724 }
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (oap->is_VIsual)
4727 {
4728 win_T *wp;
4729
4730 FOR_ALL_WINDOWS(wp)
4731 {
4732 if (wp->w_old_cursor_lnum != 0)
4733 {
4734 /* When lines have been inserted or deleted, adjust the end of
4735 * the Visual area to be redrawn. */
4736 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4737 wp->w_old_cursor_lnum += old_line_count;
4738 else
4739 wp->w_old_visual_lnum += old_line_count;
4740 }
4741 }
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004745#if defined(FEAT_EVAL) || defined(PROTO)
4746/*
4747 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4748 */
4749 void
4750op_formatexpr(oap)
4751 oparg_T *oap;
4752{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753 if (oap->is_VIsual)
4754 /* When there is no change: need to remove the Visual selection */
4755 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004756
Bram Moolenaar700303e2010-07-11 17:35:50 +02004757 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4758 /* As documented: when 'formatexpr' returns non-zero fall back to
4759 * internal formatting. */
4760 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004761}
4762
4763 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004764fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004765 linenr_T lnum;
4766 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004767 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004768{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004769 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4770 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004771 int r;
4772
4773 /*
4774 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004775 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004776 */
4777 set_vim_var_nr(VV_LNUM, lnum);
4778 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004779 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004780
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004781 /*
4782 * Evaluate the function.
4783 */
4784 if (use_sandbox)
4785 ++sandbox;
4786 r = eval_to_number(curbuf->b_p_fex);
4787 if (use_sandbox)
4788 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004789
4790 set_vim_var_string(VV_CHAR, NULL, -1);
4791
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004792 return r;
4793}
4794#endif
4795
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796/*
4797 * Format "line_count" lines, starting at the cursor position.
4798 * When "line_count" is negative, format until the end of the paragraph.
4799 * Lines after the cursor line are saved for undo, caller must have saved the
4800 * first line.
4801 */
4802 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004803format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004805 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 int max_len;
4808 int is_not_par; /* current line not part of parag. */
4809 int next_is_not_par; /* next line not part of paragraph */
4810 int is_end_par; /* at end of paragraph */
4811 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4812 int next_is_start_par = FALSE;
4813#ifdef FEAT_COMMENTS
4814 int leader_len = 0; /* leader len of current line */
4815 int next_leader_len; /* leader len of next line */
4816 char_u *leader_flags = NULL; /* flags for leader of current line */
4817 char_u *next_leader_flags; /* flags for leader of next line */
4818 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004819 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004822 int second_indent = -1; /* indent for second line (comment
4823 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 int do_second_indent;
4825 int do_number_indent;
4826 int do_trail_white;
4827 int first_par_line = TRUE;
4828 int smd_save;
4829 long count;
4830 int need_set_indent = TRUE; /* set indent of next paragraph */
4831 int force_format = FALSE;
4832 int old_State = State;
4833
4834 /* length of a line to force formatting: 3 * 'tw' */
4835 max_len = comp_textwidth(TRUE) * 3;
4836
4837 /* check for 'q', '2' and '1' in 'formatoptions' */
4838#ifdef FEAT_COMMENTS
4839 do_comments = has_format_option(FO_Q_COMS);
4840#endif
4841 do_second_indent = has_format_option(FO_Q_SECOND);
4842 do_number_indent = has_format_option(FO_Q_NUMBER);
4843 do_trail_white = has_format_option(FO_WHITE_PAR);
4844
4845 /*
4846 * Get info about the previous and current line.
4847 */
4848 if (curwin->w_cursor.lnum > 1)
4849 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4850#ifdef FEAT_COMMENTS
4851 , &leader_len, &leader_flags, do_comments
4852#endif
4853 );
4854 else
4855 is_not_par = TRUE;
4856 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4857#ifdef FEAT_COMMENTS
4858 , &next_leader_len, &next_leader_flags, do_comments
4859#endif
4860 );
4861 is_end_par = (is_not_par || next_is_not_par);
4862 if (!is_end_par && do_trail_white)
4863 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4864
4865 curwin->w_cursor.lnum--;
4866 for (count = line_count; count != 0 && !got_int; --count)
4867 {
4868 /*
4869 * Advance to next paragraph.
4870 */
4871 if (advance)
4872 {
4873 curwin->w_cursor.lnum++;
4874 prev_is_end_par = is_end_par;
4875 is_not_par = next_is_not_par;
4876#ifdef FEAT_COMMENTS
4877 leader_len = next_leader_len;
4878 leader_flags = next_leader_flags;
4879#endif
4880 }
4881
4882 /*
4883 * The last line to be formatted.
4884 */
4885 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4886 {
4887 next_is_not_par = TRUE;
4888#ifdef FEAT_COMMENTS
4889 next_leader_len = 0;
4890 next_leader_flags = NULL;
4891#endif
4892 }
4893 else
4894 {
4895 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4896#ifdef FEAT_COMMENTS
4897 , &next_leader_len, &next_leader_flags, do_comments
4898#endif
4899 );
4900 if (do_number_indent)
4901 next_is_start_par =
4902 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4903 }
4904 advance = TRUE;
4905 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4906 if (!is_end_par && do_trail_white)
4907 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4908
4909 /*
4910 * Skip lines that are not in a paragraph.
4911 */
4912 if (is_not_par)
4913 {
4914 if (line_count < 0)
4915 break;
4916 }
4917 else
4918 {
4919 /*
4920 * For the first line of a paragraph, check indent of second line.
4921 * Don't do this for comments and empty lines.
4922 */
4923 if (first_par_line
4924 && (do_second_indent || do_number_indent)
4925 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004926 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004928 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4929 {
4930#ifdef FEAT_COMMENTS
4931 if (leader_len == 0 && next_leader_len == 0)
4932 {
4933 /* no comment found */
4934#endif
4935 second_indent =
4936 get_indent_lnum(curwin->w_cursor.lnum + 1);
4937#ifdef FEAT_COMMENTS
4938 }
4939 else
4940 {
4941 second_indent = next_leader_len;
4942 do_comments_list = 1;
4943 }
4944#endif
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004947 {
4948#ifdef FEAT_COMMENTS
4949 if (leader_len == 0 && next_leader_len == 0)
4950 {
4951 /* no comment found */
4952#endif
4953 second_indent =
4954 get_number_indent(curwin->w_cursor.lnum);
4955#ifdef FEAT_COMMENTS
4956 }
4957 else
4958 {
4959 /* get_number_indent() is now "comment aware"... */
4960 second_indent =
4961 get_number_indent(curwin->w_cursor.lnum);
4962 do_comments_list = 1;
4963 }
4964#endif
4965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967
4968 /*
4969 * When the comment leader changes, it's the end of the paragraph.
4970 */
4971 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4972#ifdef FEAT_COMMENTS
4973 || !same_leader(curwin->w_cursor.lnum,
4974 leader_len, leader_flags,
4975 next_leader_len, next_leader_flags)
4976#endif
4977 )
4978 is_end_par = TRUE;
4979
4980 /*
4981 * If we have got to the end of a paragraph, or the line is
4982 * getting long, format it.
4983 */
4984 if (is_end_par || force_format)
4985 {
4986 if (need_set_indent)
4987 /* replace indent in first line with minimal number of
4988 * tabs and spaces, according to current options */
4989 (void)set_indent(get_indent(), SIN_CHANGED);
4990
4991 /* put cursor on last non-space */
4992 State = NORMAL; /* don't go past end-of-line */
4993 coladvance((colnr_T)MAXCOL);
4994 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4995 dec_cursor();
4996
4997 /* do the formatting, without 'showmode' */
4998 State = INSERT; /* for open_line() */
4999 smd_save = p_smd;
5000 p_smd = FALSE;
5001 insertchar(NUL, INSCHAR_FORMAT
5002#ifdef FEAT_COMMENTS
5003 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005004 + (do_comments && do_comments_list
5005 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005007 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 State = old_State;
5009 p_smd = smd_save;
5010 second_indent = -1;
5011 /* at end of par.: need to set indent of next par. */
5012 need_set_indent = is_end_par;
5013 if (is_end_par)
5014 {
5015 /* When called with a negative line count, break at the
5016 * end of the paragraph. */
5017 if (line_count < 0)
5018 break;
5019 first_par_line = TRUE;
5020 }
5021 force_format = FALSE;
5022 }
5023
5024 /*
5025 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005026 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
5028 if (!is_end_par)
5029 {
5030 advance = FALSE;
5031 curwin->w_cursor.lnum++;
5032 curwin->w_cursor.col = 0;
5033 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005037 {
5038 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5040 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005041 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005043 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5044 {
5045 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005046 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005047
5048 if (indent > 0)
5049 {
5050 (void)del_bytes(indent, FALSE, FALSE);
5051 mark_col_adjust(curwin->w_cursor.lnum,
5052 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005053 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005056 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
5058 beep_flush();
5059 break;
5060 }
5061 first_par_line = FALSE;
5062 /* If the line is getting long, format it next time */
5063 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5064 force_format = TRUE;
5065 else
5066 force_format = FALSE;
5067 }
5068 }
5069 line_breakcheck();
5070 }
5071}
5072
5073/*
5074 * Return TRUE if line "lnum" ends in a white character.
5075 */
5076 static int
5077ends_in_white(lnum)
5078 linenr_T lnum;
5079{
5080 char_u *s = ml_get(lnum);
5081 size_t l;
5082
5083 if (*s == NUL)
5084 return FALSE;
5085 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5086 * invocation may call function multiple times". */
5087 l = STRLEN(s) - 1;
5088 return vim_iswhite(s[l]);
5089}
5090
5091/*
5092 * Blank lines, and lines containing only the comment leader, are left
5093 * untouched by the formatting. The function returns TRUE in this
5094 * case. It also returns TRUE when a line starts with the end of a comment
5095 * ('e' in comment flags), so that this line is skipped, and not joined to the
5096 * previous line. A new paragraph starts after a blank line, or when the
5097 * comment leader changes -- webb.
5098 */
5099#ifdef FEAT_COMMENTS
5100 static int
5101fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5102 linenr_T lnum;
5103 int *leader_len;
5104 char_u **leader_flags;
5105 int do_comments;
5106{
5107 char_u *flags = NULL; /* init for GCC */
5108 char_u *ptr;
5109
5110 ptr = ml_get(lnum);
5111 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005112 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 else
5114 *leader_len = 0;
5115
5116 if (*leader_len > 0)
5117 {
5118 /*
5119 * Search for 'e' flag in comment leader flags.
5120 */
5121 flags = *leader_flags;
5122 while (*flags && *flags != ':' && *flags != COM_END)
5123 ++flags;
5124 }
5125
5126 return (*skipwhite(ptr + *leader_len) == NUL
5127 || (*leader_len > 0 && *flags == COM_END)
5128 || startPS(lnum, NUL, FALSE));
5129}
5130#else
5131 static int
5132fmt_check_par(lnum)
5133 linenr_T lnum;
5134{
5135 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5136}
5137#endif
5138
5139/*
5140 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5141 * previous line is in the same paragraph. Used for auto-formatting.
5142 */
5143 int
5144paragraph_start(lnum)
5145 linenr_T lnum;
5146{
5147 char_u *p;
5148#ifdef FEAT_COMMENTS
5149 int leader_len = 0; /* leader len of current line */
5150 char_u *leader_flags = NULL; /* flags for leader of current line */
5151 int next_leader_len; /* leader len of next line */
5152 char_u *next_leader_flags; /* flags for leader of next line */
5153 int do_comments; /* format comments */
5154#endif
5155
5156 if (lnum <= 1)
5157 return TRUE; /* start of the file */
5158
5159 p = ml_get(lnum - 1);
5160 if (*p == NUL)
5161 return TRUE; /* after empty line */
5162
5163#ifdef FEAT_COMMENTS
5164 do_comments = has_format_option(FO_Q_COMS);
5165#endif
5166 if (fmt_check_par(lnum - 1
5167#ifdef FEAT_COMMENTS
5168 , &leader_len, &leader_flags, do_comments
5169#endif
5170 ))
5171 return TRUE; /* after non-paragraph line */
5172
5173 if (fmt_check_par(lnum
5174#ifdef FEAT_COMMENTS
5175 , &next_leader_len, &next_leader_flags, do_comments
5176#endif
5177 ))
5178 return TRUE; /* "lnum" is not a paragraph line */
5179
5180 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5181 return TRUE; /* missing trailing space in previous line. */
5182
5183 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5184 return TRUE; /* numbered item starts in "lnum". */
5185
5186#ifdef FEAT_COMMENTS
5187 if (!same_leader(lnum - 1, leader_len, leader_flags,
5188 next_leader_len, next_leader_flags))
5189 return TRUE; /* change of comment leader. */
5190#endif
5191
5192 return FALSE;
5193}
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195/*
5196 * prepare a few things for block mode yank/delete/tilde
5197 *
5198 * for delete:
5199 * - textlen includes the first/last char to be (partly) deleted
5200 * - start/endspaces is the number of columns that are taken by the
5201 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005202 * deleted.
5203 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 * - textlen includes the first/last char to be wholly yanked
5205 * - start/endspaces is the number of columns of the first/last yanked char
5206 * that are to be yanked.
5207 */
5208 static void
5209block_prep(oap, bdp, lnum, is_del)
5210 oparg_T *oap;
5211 struct block_def *bdp;
5212 linenr_T lnum;
5213 int is_del;
5214{
5215 int incr = 0;
5216 char_u *pend;
5217 char_u *pstart;
5218 char_u *line;
5219 char_u *prev_pstart;
5220 char_u *prev_pend;
5221
5222 bdp->startspaces = 0;
5223 bdp->endspaces = 0;
5224 bdp->textlen = 0;
5225 bdp->start_vcol = 0;
5226 bdp->end_vcol = 0;
5227#ifdef FEAT_VISUALEXTRA
5228 bdp->is_short = FALSE;
5229 bdp->is_oneChar = FALSE;
5230 bdp->pre_whitesp = 0;
5231 bdp->pre_whitesp_c = 0;
5232 bdp->end_char_vcols = 0;
5233#endif
5234 bdp->start_char_vcols = 0;
5235
5236 line = ml_get(lnum);
5237 pstart = line;
5238 prev_pstart = line;
5239 while (bdp->start_vcol < oap->start_vcol && *pstart)
5240 {
5241 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005242 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 bdp->start_vcol += incr;
5244#ifdef FEAT_VISUALEXTRA
5245 if (vim_iswhite(*pstart))
5246 {
5247 bdp->pre_whitesp += incr;
5248 bdp->pre_whitesp_c++;
5249 }
5250 else
5251 {
5252 bdp->pre_whitesp = 0;
5253 bdp->pre_whitesp_c = 0;
5254 }
5255#endif
5256 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005257 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 bdp->start_char_vcols = incr;
5260 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5261 {
5262 bdp->end_vcol = bdp->start_vcol;
5263#ifdef FEAT_VISUALEXTRA
5264 bdp->is_short = TRUE;
5265#endif
5266 if (!is_del || oap->op_type == OP_APPEND)
5267 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5268 }
5269 else
5270 {
5271 /* notice: this converts partly selected Multibyte characters to
5272 * spaces, too. */
5273 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5274 if (is_del && bdp->startspaces)
5275 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5276 pend = pstart;
5277 bdp->end_vcol = bdp->start_vcol;
5278 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5279 {
5280#ifdef FEAT_VISUALEXTRA
5281 bdp->is_oneChar = TRUE;
5282#endif
5283 if (oap->op_type == OP_INSERT)
5284 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5285 else if (oap->op_type == OP_APPEND)
5286 {
5287 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5288 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5289 }
5290 else
5291 {
5292 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5293 if (is_del && oap->op_type != OP_LSHIFT)
5294 {
5295 /* just putting the sum of those two into
5296 * bdp->startspaces doesn't work for Visual replace,
5297 * so we have to split the tab in two */
5298 bdp->startspaces = bdp->start_char_vcols
5299 - (bdp->start_vcol - oap->start_vcol);
5300 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5301 }
5302 }
5303 }
5304 else
5305 {
5306 prev_pend = pend;
5307 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5308 {
5309 /* Count a tab for what it's worth (if list mode not on) */
5310 prev_pend = pend;
Bram Moolenaar597a4222014-06-25 14:39:50 +02005311 /* TODO: is passing prev_pend for start of the line OK?
5312 * perhaps it should be "line". */
5313 incr = lbr_chartabsize_adv(prev_pend, &pend,
5314 (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 bdp->end_vcol += incr;
5316 }
5317 if (bdp->end_vcol <= oap->end_vcol
5318 && (!is_del
5319 || oap->op_type == OP_APPEND
5320 || oap->op_type == OP_REPLACE)) /* line too short */
5321 {
5322#ifdef FEAT_VISUALEXTRA
5323 bdp->is_short = TRUE;
5324#endif
5325 /* Alternative: include spaces to fill up the block.
5326 * Disadvantage: can lead to trailing spaces when the line is
5327 * short where the text is put */
5328 /* if (!is_del || oap->op_type == OP_APPEND) */
5329 if (oap->op_type == OP_APPEND || virtual_op)
5330 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005331 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 else
5333 bdp->endspaces = 0; /* replace doesn't add characters */
5334 }
5335 else if (bdp->end_vcol > oap->end_vcol)
5336 {
5337 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5338 if (!is_del && bdp->endspaces)
5339 {
5340 bdp->endspaces = incr - bdp->endspaces;
5341 if (pend != pstart)
5342 pend = prev_pend;
5343 }
5344 }
5345 }
5346#ifdef FEAT_VISUALEXTRA
5347 bdp->end_char_vcols = incr;
5348#endif
5349 if (is_del && bdp->startspaces)
5350 pstart = prev_pstart;
5351 bdp->textlen = (int)(pend - pstart);
5352 }
5353 bdp->textcol = (colnr_T) (pstart - line);
5354 bdp->textstart = pstart;
5355}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357#ifdef FEAT_RIGHTLEFT
5358static void reverse_line __ARGS((char_u *s));
5359
5360 static void
5361reverse_line(s)
5362 char_u *s;
5363{
5364 int i, j;
5365 char_u c;
5366
5367 if ((i = (int)STRLEN(s) - 1) <= 0)
5368 return;
5369
5370 curwin->w_cursor.col = i - curwin->w_cursor.col;
5371 for (j = 0; j < i; j++, i--)
5372 {
5373 c = s[i]; s[i] = s[j]; s[j] = c;
5374 }
5375}
5376
5377# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5378#else
5379# define RLADDSUBFIX(ptr)
5380#endif
5381
5382/*
5383 * add or subtract 'Prenum1' from a number in a line
5384 * 'command' is CTRL-A for add, CTRL-X for subtract
5385 *
5386 * return FAIL for failure, OK otherwise
5387 */
5388 int
5389do_addsub(command, Prenum1)
5390 int command;
5391 linenr_T Prenum1;
5392{
5393 int col;
5394 char_u *buf1;
5395 char_u buf2[NUMBUFLEN];
5396 int hex; /* 'X' or 'x': hex; '0': octal */
5397 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005398 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 long_u oldn;
5400 char_u *ptr;
5401 int c;
5402 int length = 0; /* character length of the number */
5403 int todel;
5404 int dohex;
5405 int dooct;
5406 int doalp;
5407 int firstdigit;
5408 int negative;
5409 int subtract;
5410
5411 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5412 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5413 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5414
5415 ptr = ml_get_curline();
5416 RLADDSUBFIX(ptr);
5417
5418 /*
5419 * First check if we are on a hexadecimal number, after the "0x".
5420 */
5421 col = curwin->w_cursor.col;
5422 if (dohex)
5423 while (col > 0 && vim_isxdigit(ptr[col]))
5424 --col;
5425 if ( dohex
5426 && col > 0
5427 && (ptr[col] == 'X'
5428 || ptr[col] == 'x')
5429 && ptr[col - 1] == '0'
5430 && vim_isxdigit(ptr[col + 1]))
5431 {
5432 /*
5433 * Found hexadecimal number, move to its start.
5434 */
5435 --col;
5436 }
5437 else
5438 {
5439 /*
5440 * Search forward and then backward to find the start of number.
5441 */
5442 col = curwin->w_cursor.col;
5443
5444 while (ptr[col] != NUL
5445 && !vim_isdigit(ptr[col])
5446 && !(doalp && ASCII_ISALPHA(ptr[col])))
5447 ++col;
5448
5449 while (col > 0
5450 && vim_isdigit(ptr[col - 1])
5451 && !(doalp && ASCII_ISALPHA(ptr[col])))
5452 --col;
5453 }
5454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 /*
5456 * If a number was found, and saving for undo works, replace the number.
5457 */
5458 firstdigit = ptr[col];
5459 RLADDSUBFIX(ptr);
5460 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5461 || u_save_cursor() != OK)
5462 {
5463 beep_flush();
5464 return FAIL;
5465 }
5466
5467 /* get ptr again, because u_save() may have changed it */
5468 ptr = ml_get_curline();
5469 RLADDSUBFIX(ptr);
5470
5471 if (doalp && ASCII_ISALPHA(firstdigit))
5472 {
5473 /* decrement or increment alphabetic character */
5474 if (command == Ctrl_X)
5475 {
5476 if (CharOrd(firstdigit) < Prenum1)
5477 {
5478 if (isupper(firstdigit))
5479 firstdigit = 'A';
5480 else
5481 firstdigit = 'a';
5482 }
5483 else
5484#ifdef EBCDIC
5485 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5486#else
5487 firstdigit -= Prenum1;
5488#endif
5489 }
5490 else
5491 {
5492 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5493 {
5494 if (isupper(firstdigit))
5495 firstdigit = 'Z';
5496 else
5497 firstdigit = 'z';
5498 }
5499 else
5500#ifdef EBCDIC
5501 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5502#else
5503 firstdigit += Prenum1;
5504#endif
5505 }
5506 curwin->w_cursor.col = col;
5507 (void)del_char(FALSE);
5508 ins_char(firstdigit);
5509 }
5510 else
5511 {
5512 negative = FALSE;
5513 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5514 {
5515 --col;
5516 negative = TRUE;
5517 }
5518
5519 /* get the number value (unsigned) */
5520 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5521
5522 /* ignore leading '-' for hex and octal numbers */
5523 if (hex && negative)
5524 {
5525 ++col;
5526 --length;
5527 negative = FALSE;
5528 }
5529
5530 /* add or subtract */
5531 subtract = FALSE;
5532 if (command == Ctrl_X)
5533 subtract ^= TRUE;
5534 if (negative)
5535 subtract ^= TRUE;
5536
5537 oldn = n;
5538 if (subtract)
5539 n -= (unsigned long)Prenum1;
5540 else
5541 n += (unsigned long)Prenum1;
5542
5543 /* handle wraparound for decimal numbers */
5544 if (!hex)
5545 {
5546 if (subtract)
5547 {
5548 if (n > oldn)
5549 {
5550 n = 1 + (n ^ (unsigned long)-1);
5551 negative ^= TRUE;
5552 }
5553 }
5554 else /* add */
5555 {
5556 if (n < oldn)
5557 {
5558 n = (n ^ (unsigned long)-1);
5559 negative ^= TRUE;
5560 }
5561 }
5562 if (n == 0)
5563 negative = FALSE;
5564 }
5565
5566 /*
5567 * Delete the old number.
5568 */
5569 curwin->w_cursor.col = col;
5570 todel = length;
5571 c = gchar_cursor();
5572 /*
5573 * Don't include the '-' in the length, only the length of the part
5574 * after it is kept the same.
5575 */
5576 if (c == '-')
5577 --length;
5578 while (todel-- > 0)
5579 {
5580 if (c < 0x100 && isalpha(c))
5581 {
5582 if (isupper(c))
5583 hexupper = TRUE;
5584 else
5585 hexupper = FALSE;
5586 }
5587 /* del_char() will mark line needing displaying */
5588 (void)del_char(FALSE);
5589 c = gchar_cursor();
5590 }
5591
5592 /*
5593 * Prepare the leading characters in buf1[].
5594 * When there are many leading zeros it could be very long. Allocate
5595 * a bit too much.
5596 */
5597 buf1 = alloc((unsigned)length + NUMBUFLEN);
5598 if (buf1 == NULL)
5599 return FAIL;
5600 ptr = buf1;
5601 if (negative)
5602 {
5603 *ptr++ = '-';
5604 }
5605 if (hex)
5606 {
5607 *ptr++ = '0';
5608 --length;
5609 }
5610 if (hex == 'x' || hex == 'X')
5611 {
5612 *ptr++ = hex;
5613 --length;
5614 }
5615
5616 /*
5617 * Put the number characters in buf2[].
5618 */
5619 if (hex == 0)
5620 sprintf((char *)buf2, "%lu", n);
5621 else if (hex == '0')
5622 sprintf((char *)buf2, "%lo", n);
5623 else if (hex && hexupper)
5624 sprintf((char *)buf2, "%lX", n);
5625 else
5626 sprintf((char *)buf2, "%lx", n);
5627 length -= (int)STRLEN(buf2);
5628
5629 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005630 * Adjust number of zeros to the new number of digits, so the
5631 * total length of the number remains the same.
5632 * Don't do this when
5633 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005635 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 while (length-- > 0)
5637 *ptr++ = '0';
5638 *ptr = NUL;
5639 STRCAT(buf1, buf2);
5640 ins_str(buf1); /* insert the new number */
5641 vim_free(buf1);
5642 }
5643 --curwin->w_cursor.col;
5644 curwin->w_set_curswant = TRUE;
5645#ifdef FEAT_RIGHTLEFT
5646 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5647 RLADDSUBFIX(ptr);
5648#endif
5649 return OK;
5650}
5651
5652#ifdef FEAT_VIMINFO
5653 int
5654read_viminfo_register(virp, force)
5655 vir_T *virp;
5656 int force;
5657{
5658 int eof;
5659 int do_it = TRUE;
5660 int size;
5661 int limit;
5662 int i;
5663 int set_prev = FALSE;
5664 char_u *str;
5665 char_u **array = NULL;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005666 int new_type;
5667 colnr_T new_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 /* We only get here (hopefully) if line[0] == '"' */
5670 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005671
5672 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (*str == '"')
5674 {
5675 set_prev = TRUE;
5676 str++;
5677 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (!ASCII_ISALNUM(*str) && *str != '-')
5680 {
5681 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5682 return TRUE; /* too many errors, pretend end-of-file */
5683 do_it = FALSE;
5684 }
5685 get_yank_register(*str++, FALSE);
5686 if (!force && y_current->y_array != NULL)
5687 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005688
5689 if (*str == '@')
5690 {
5691 /* "x@: register x used for @@ */
5692 if (force || execreg_lastc == NUL)
5693 execreg_lastc = str[-1];
5694 }
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 size = 0;
5697 limit = 100; /* Optimized for registers containing <= 100 lines */
5698 if (do_it)
5699 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005700 /*
5701 * Build the new register in array[].
5702 * y_array is kept as-is until done.
5703 * The "do_it" flag is reset when something is wrong, in which case
5704 * array[] needs to be freed.
5705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (set_prev)
5707 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005708 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005709 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005711 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005713 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005715 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 /* get the block width; if it's missing we get a zero, which is OK */
5717 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005718 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720
5721 while (!(eof = viminfo_readline(virp))
5722 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5723 {
5724 if (do_it)
5725 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005726 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005728 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005730
5731 if (new_array == NULL)
5732 {
5733 do_it = FALSE;
5734 break;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005737 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005739 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742 str = viminfo_readstring(virp, 1, TRUE);
5743 if (str != NULL)
5744 array[size++] = str;
5745 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005746 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 do_it = FALSE;
5748 }
5749 }
5750 if (do_it)
5751 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005752 /* free y_array[] */
5753 for (i = 0; i < y_current->y_size; i++)
5754 vim_free(y_current->y_array[i]);
5755 vim_free(y_current->y_array);
5756
5757 y_current->y_type = new_type;
5758 y_current->y_width = new_width;
5759 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (size == 0)
5761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 y_current->y_array = NULL;
5763 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005766 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 y_current->y_array =
5768 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5769 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005770 {
5771 if (y_current->y_array == NULL)
5772 vim_free(array[i]);
5773 else
5774 y_current->y_array[i] = array[i];
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005778 else
5779 {
5780 /* Free array[] if it was filled. */
5781 for (i = 0; i < size; i++)
5782 vim_free(array[i]);
5783 }
5784 vim_free(array);
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 return eof;
5787}
5788
5789 void
5790write_viminfo_registers(fp)
5791 FILE *fp;
5792{
5793 int i, j;
5794 char_u *type;
5795 char_u c;
5796 int num_lines;
5797 int max_num_lines;
5798 int max_kbyte;
5799 long len;
5800
Bram Moolenaar64404472010-06-26 06:24:45 +02005801 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 /* Get '<' value, use old '"' value if '<' is not found. */
5804 max_num_lines = get_viminfo_parameter('<');
5805 if (max_num_lines < 0)
5806 max_num_lines = get_viminfo_parameter('"');
5807 if (max_num_lines == 0)
5808 return;
5809 max_kbyte = get_viminfo_parameter('s');
5810 if (max_kbyte == 0)
5811 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 for (i = 0; i < NUM_REGISTERS; i++)
5814 {
5815 if (y_regs[i].y_array == NULL)
5816 continue;
5817#ifdef FEAT_CLIPBOARD
5818 /* Skip '*'/'+' register, we don't want them back next time */
5819 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5820 continue;
5821#endif
5822#ifdef FEAT_DND
5823 /* Neither do we want the '~' register */
5824 if (i == TILDE_REGISTER)
5825 continue;
5826#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005827 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005829 if (num_lines == 0
5830 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005831 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005832 continue;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (max_kbyte > 0)
5835 {
5836 /* Skip register if there is more text than the maximum size. */
5837 len = 0;
5838 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005839 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 if (len > (long)max_kbyte * 1024L)
5841 continue;
5842 }
5843
5844 switch (y_regs[i].y_type)
5845 {
5846 case MLINE:
5847 type = (char_u *)"LINE";
5848 break;
5849 case MCHAR:
5850 type = (char_u *)"CHAR";
5851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 case MBLOCK:
5853 type = (char_u *)"BLOCK";
5854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 default:
5856 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005857 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 emsg(IObuff);
5859 type = (char_u *)"LINE";
5860 break;
5861 }
5862 if (y_previous == &y_regs[i])
5863 fprintf(fp, "\"");
5864 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005865 fprintf(fp, "\"%c", c);
5866 if (c == execreg_lastc)
5867 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005868 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
5870 /* If max_num_lines < 0, then we save ALL the lines in the register */
5871 if (max_num_lines > 0 && num_lines > max_num_lines)
5872 num_lines = max_num_lines;
5873 for (j = 0; j < num_lines; j++)
5874 {
5875 putc('\t', fp);
5876 viminfo_writestring(fp, y_regs[i].y_array[j]);
5877 }
5878 }
5879}
5880#endif /* FEAT_VIMINFO */
5881
5882#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5883/*
5884 * SELECTION / PRIMARY ('*')
5885 *
5886 * Text selection stuff that uses the GUI selection register '*'. When using a
5887 * GUI this may be text from another window, otherwise it is the last text we
5888 * had highlighted with VIsual mode. With mouse support, clicking the middle
5889 * button performs the paste, otherwise you will need to do <"*p>. "
5890 * If not under X, it is synonymous with the clipboard register '+'.
5891 *
5892 * X CLIPBOARD ('+')
5893 *
5894 * Text selection stuff that uses the GUI clipboard register '+'.
5895 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5896 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5897 * otherwise you will need to do <"+p>. "
5898 * If not under X, it is synonymous with the selection register '*'.
5899 */
5900
5901/*
5902 * Routine to export any final X selection we had to the environment
5903 * so that the text is still available after vim has exited. X selections
5904 * only exist while the owning application exists, so we write to the
5905 * permanent (while X runs) store CUT_BUFFER0.
5906 * Dump the CLIPBOARD selection if we own it (it's logically the more
5907 * 'permanent' of the two), otherwise the PRIMARY one.
5908 * For now, use a hard-coded sanity limit of 1Mb of data.
5909 */
5910#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5911 void
5912x11_export_final_selection()
5913{
5914 Display *dpy;
5915 char_u *str = NULL;
5916 long_u len = 0;
5917 int motion_type = -1;
5918
5919# ifdef FEAT_GUI
5920 if (gui.in_use)
5921 dpy = X_DISPLAY;
5922 else
5923# endif
5924# ifdef FEAT_XCLIPBOARD
5925 dpy = xterm_dpy;
5926# else
5927 return;
5928# endif
5929
5930 /* Get selection to export */
5931 if (clip_plus.owned)
5932 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5933 else if (clip_star.owned)
5934 motion_type = clip_convert_selection(&str, &len, &clip_star);
5935
5936 /* Check it's OK */
5937 if (dpy != NULL && str != NULL && motion_type >= 0
5938 && len < 1024*1024 && len > 0)
5939 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005940#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005941 int ok = TRUE;
5942
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005943 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5944 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5945 * encoding conversion usually doesn't work, so keep the text as-is.
5946 */
5947 if (has_mbyte)
5948 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005949 vimconv_T vc;
5950
5951 vc.vc_type = CONV_NONE;
5952 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5953 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005954 int intlen = len;
5955 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005956
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005957 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005958 conv_str = string_convert(&vc, str, &intlen);
5959 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005960 if (conv_str != NULL)
5961 {
5962 vim_free(str);
5963 str = conv_str;
5964 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005965 else
5966 {
5967 ok = FALSE;
5968 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005969 convert_setup(&vc, NULL, NULL);
5970 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005971 else
5972 {
5973 ok = FALSE;
5974 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005975 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005976
5977 /* Do not store the string if conversion failed. Better to use any
5978 * other selection than garbled text. */
5979 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005980#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005981 {
5982 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5983 XFlush(dpy);
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986
5987 vim_free(str);
5988}
5989#endif
5990
5991 void
5992clip_free_selection(cbd)
5993 VimClipboard *cbd;
5994{
5995 struct yankreg *y_ptr = y_current;
5996
5997 if (cbd == &clip_plus)
5998 y_current = &y_regs[PLUS_REGISTER];
5999 else
6000 y_current = &y_regs[STAR_REGISTER];
6001 free_yank_all();
6002 y_current->y_size = 0;
6003 y_current = y_ptr;
6004}
6005
6006/*
6007 * Get the selected text and put it in the gui selection register '*' or '+'.
6008 */
6009 void
6010clip_get_selection(cbd)
6011 VimClipboard *cbd;
6012{
6013 struct yankreg *old_y_previous, *old_y_current;
6014 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 pos_T old_visual;
6016 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 colnr_T old_curswant;
6018 int old_set_curswant;
6019 pos_T old_op_start, old_op_end;
6020 oparg_T oa;
6021 cmdarg_T ca;
6022
6023 if (cbd->owned)
6024 {
6025 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6026 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6027 return;
6028
6029 /* Get the text between clip_star.start & clip_star.end */
6030 old_y_previous = y_previous;
6031 old_y_current = y_current;
6032 old_cursor = curwin->w_cursor;
6033 old_curswant = curwin->w_curswant;
6034 old_set_curswant = curwin->w_set_curswant;
6035 old_op_start = curbuf->b_op_start;
6036 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 old_visual = VIsual;
6038 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 clear_oparg(&oa);
6040 oa.regname = (cbd == &clip_plus ? '+' : '*');
6041 oa.op_type = OP_YANK;
6042 vim_memset(&ca, 0, sizeof(ca));
6043 ca.oap = &oa;
6044 ca.cmdchar = 'y';
6045 ca.count1 = 1;
6046 ca.retval = CA_NO_ADJ_OP_END;
6047 do_pending_operator(&ca, 0, TRUE);
6048 y_previous = old_y_previous;
6049 y_current = old_y_current;
6050 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006051 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 curwin->w_curswant = old_curswant;
6053 curwin->w_set_curswant = old_set_curswant;
6054 curbuf->b_op_start = old_op_start;
6055 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 VIsual = old_visual;
6057 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059 else
6060 {
6061 clip_free_selection(cbd);
6062
6063 /* Try to get selected text from another window */
6064 clip_gen_request_selection(cbd);
6065 }
6066}
6067
Bram Moolenaard44347f2011-06-19 01:14:29 +02006068/*
6069 * Convert from the GUI selection string into the '*'/'+' register.
6070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 void
6072clip_yank_selection(type, str, len, cbd)
6073 int type;
6074 char_u *str;
6075 long len;
6076 VimClipboard *cbd;
6077{
6078 struct yankreg *y_ptr;
6079
6080 if (cbd == &clip_plus)
6081 y_ptr = &y_regs[PLUS_REGISTER];
6082 else
6083 y_ptr = &y_regs[STAR_REGISTER];
6084
6085 clip_free_selection(cbd);
6086
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006087 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088}
6089
6090/*
6091 * Convert the '*'/'+' register into a GUI selection string returned in *str
6092 * with length *len.
6093 * Returns the motion type, or -1 for failure.
6094 */
6095 int
6096clip_convert_selection(str, len, cbd)
6097 char_u **str;
6098 long_u *len;
6099 VimClipboard *cbd;
6100{
6101 char_u *p;
6102 int lnum;
6103 int i, j;
6104 int_u eolsize;
6105 struct yankreg *y_ptr;
6106
6107 if (cbd == &clip_plus)
6108 y_ptr = &y_regs[PLUS_REGISTER];
6109 else
6110 y_ptr = &y_regs[STAR_REGISTER];
6111
6112#ifdef USE_CRNL
6113 eolsize = 2;
6114#else
6115 eolsize = 1;
6116#endif
6117
6118 *str = NULL;
6119 *len = 0;
6120 if (y_ptr->y_array == NULL)
6121 return -1;
6122
6123 for (i = 0; i < y_ptr->y_size; i++)
6124 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6125
6126 /*
6127 * Don't want newline character at end of last line if we're in MCHAR mode.
6128 */
6129 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6130 *len -= eolsize;
6131
6132 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6133 if (p == NULL)
6134 return -1;
6135 lnum = 0;
6136 for (i = 0, j = 0; i < (int)*len; i++, j++)
6137 {
6138 if (y_ptr->y_array[lnum][j] == '\n')
6139 p[i] = NUL;
6140 else if (y_ptr->y_array[lnum][j] == NUL)
6141 {
6142#ifdef USE_CRNL
6143 p[i++] = '\r';
6144#endif
6145#ifdef USE_CR
6146 p[i] = '\r';
6147#else
6148 p[i] = '\n';
6149#endif
6150 lnum++;
6151 j = -1;
6152 }
6153 else
6154 p[i] = y_ptr->y_array[lnum][j];
6155 }
6156 return y_ptr->y_type;
6157}
6158
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160/*
6161 * If we have written to a clipboard register, send the text to the clipboard.
6162 */
6163 static void
6164may_set_selection()
6165{
6166 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6167 {
6168 clip_own_selection(&clip_star);
6169 clip_gen_set_selection(&clip_star);
6170 }
6171 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6172 {
6173 clip_own_selection(&clip_plus);
6174 clip_gen_set_selection(&clip_plus);
6175 }
6176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178#endif /* FEAT_CLIPBOARD || PROTO */
6179
6180
6181#if defined(FEAT_DND) || defined(PROTO)
6182/*
6183 * Replace the contents of the '~' register with str.
6184 */
6185 void
6186dnd_yank_drag_data(str, len)
6187 char_u *str;
6188 long len;
6189{
6190 struct yankreg *curr;
6191
6192 curr = y_current;
6193 y_current = &y_regs[TILDE_REGISTER];
6194 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006195 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 y_current = curr;
6197}
6198#endif
6199
6200
6201#if defined(FEAT_EVAL) || defined(PROTO)
6202/*
6203 * Return the type of a register.
6204 * Used for getregtype()
6205 * Returns MAUTO for error.
6206 */
6207 char_u
6208get_reg_type(regname, reglen)
6209 int regname;
6210 long *reglen;
6211{
6212 switch (regname)
6213 {
6214 case '%': /* file name */
6215 case '#': /* alternate file name */
6216 case '=': /* expression */
6217 case ':': /* last command line */
6218 case '/': /* last search-pattern */
6219 case '.': /* last inserted text */
6220#ifdef FEAT_SEARCHPATH
6221 case Ctrl_F: /* Filename under cursor */
6222 case Ctrl_P: /* Path under cursor, expand via "path" */
6223#endif
6224 case Ctrl_W: /* word under cursor */
6225 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6226 case '_': /* black hole: always empty */
6227 return MCHAR;
6228 }
6229
6230#ifdef FEAT_CLIPBOARD
6231 regname = may_get_selection(regname);
6232#endif
6233
Bram Moolenaar32b92012014-01-14 12:33:36 +01006234 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6235 return MAUTO;
6236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 get_yank_register(regname, FALSE);
6238
6239 if (y_current->y_array != NULL)
6240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (reglen != NULL && y_current->y_type == MBLOCK)
6242 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return y_current->y_type;
6244 }
6245 return MAUTO;
6246}
6247
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006248static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6249
6250/*
6251 * When "flags" has GREG_LIST return a list with text "s".
6252 * Otherwise just return "s".
6253 */
6254 static char_u *
6255getreg_wrap_one_line(s, flags)
6256 char_u *s;
6257 int flags;
6258{
6259 if (flags & GREG_LIST)
6260 {
6261 list_T *list = list_alloc();
6262
6263 if (list != NULL)
6264 {
6265 if (list_append_string(list, NULL, -1) == FAIL)
6266 {
6267 list_free(list, TRUE);
6268 return NULL;
6269 }
6270 list->lv_first->li_tv.vval.v_string = s;
6271 }
6272 return (char_u *)list;
6273 }
6274 return s;
6275}
6276
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277/*
6278 * Return the contents of a register as a single allocated string.
6279 * Used for "@r" in expressions and for getreg().
6280 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006281 * Flags:
6282 * GREG_NO_EXPR Do not allow expression register
6283 * GREG_EXPR_SRC For the expression register: return expression itself,
6284 * not the result of its evaluation.
6285 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 */
6287 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006288get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006290 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 long i;
6293 char_u *retval;
6294 int allocated;
6295 long len;
6296
6297 /* Don't allow using an expression register inside an expression */
6298 if (regname == '=')
6299 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006300 if (flags & GREG_NO_EXPR)
6301 return NULL;
6302 if (flags & GREG_EXPR_SRC)
6303 return getreg_wrap_one_line(get_expr_line_src(), flags);
6304 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306
6307 if (regname == '@') /* "@@" is used for unnamed register */
6308 regname = '"';
6309
6310 /* check for valid regname */
6311 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6312 return NULL;
6313
6314#ifdef FEAT_CLIPBOARD
6315 regname = may_get_selection(regname);
6316#endif
6317
6318 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6319 {
6320 if (retval == NULL)
6321 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006322 if (allocated)
6323 return getreg_wrap_one_line(retval, flags);
6324 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
6326
6327 get_yank_register(regname, FALSE);
6328 if (y_current->y_array == NULL)
6329 return NULL;
6330
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006331 if (flags & GREG_LIST)
6332 {
6333 list_T *list = list_alloc();
6334 int error = FALSE;
6335
6336 if (list == NULL)
6337 return NULL;
6338 for (i = 0; i < y_current->y_size; ++i)
6339 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6340 error = TRUE;
6341 if (error)
6342 {
6343 list_free(list, TRUE);
6344 return NULL;
6345 }
6346 return (char_u *)list;
6347 }
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 /*
6350 * Compute length of resulting string.
6351 */
6352 len = 0;
6353 for (i = 0; i < y_current->y_size; ++i)
6354 {
6355 len += (long)STRLEN(y_current->y_array[i]);
6356 /*
6357 * Insert a newline between lines and after last line if
6358 * y_type is MLINE.
6359 */
6360 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6361 ++len;
6362 }
6363
6364 retval = lalloc(len + 1, TRUE);
6365
6366 /*
6367 * Copy the lines of the yank register into the string.
6368 */
6369 if (retval != NULL)
6370 {
6371 len = 0;
6372 for (i = 0; i < y_current->y_size; ++i)
6373 {
6374 STRCPY(retval + len, y_current->y_array[i]);
6375 len += (long)STRLEN(retval + len);
6376
6377 /*
6378 * Insert a NL between lines and after the last line if y_type is
6379 * MLINE.
6380 */
6381 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6382 retval[len++] = '\n';
6383 }
6384 retval[len] = NUL;
6385 }
6386
6387 return retval;
6388}
6389
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006390 static int
6391init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6392 int name;
6393 struct yankreg **old_y_previous;
6394 struct yankreg **old_y_current;
6395 int must_append;
6396 int *yank_type UNUSED;
6397{
6398 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6399 {
6400 emsg_invreg(name);
6401 return FAIL;
6402 }
6403
6404 /* Don't want to change the current (unnamed) register */
6405 *old_y_previous = y_previous;
6406 *old_y_current = y_current;
6407
6408 get_yank_register(name, TRUE);
6409 if (!y_append && !must_append)
6410 free_yank_all();
6411 return OK;
6412}
6413
6414 static void
6415finish_write_reg(name, old_y_previous, old_y_current)
6416 int name;
6417 struct yankreg *old_y_previous;
6418 struct yankreg *old_y_current;
6419{
6420# ifdef FEAT_CLIPBOARD
6421 /* Send text of clipboard register to the clipboard. */
6422 may_set_selection();
6423# endif
6424
6425 /* ':let @" = "val"' should change the meaning of the "" register */
6426 if (name != '"')
6427 y_previous = old_y_previous;
6428 y_current = old_y_current;
6429}
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431/*
6432 * Store string "str" in register "name".
6433 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6434 * If "must_append" is TRUE, always append to the register. Otherwise append
6435 * if "name" is an uppercase letter.
6436 * Note: "maxlen" and "must_append" don't work for the "/" register.
6437 * Careful: 'str' is modified, you may have to use a copy!
6438 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6439 */
6440 void
6441write_reg_contents(name, str, maxlen, must_append)
6442 int name;
6443 char_u *str;
6444 int maxlen;
6445 int must_append;
6446{
6447 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6448}
6449
6450 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006451write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6452 int name;
6453 char_u **strings;
6454 int maxlen UNUSED;
6455 int must_append;
6456 int yank_type;
6457 long block_len;
6458{
6459 struct yankreg *old_y_previous, *old_y_current;
6460
6461 if (name == '/'
6462#ifdef FEAT_EVAL
6463 || name == '='
6464#endif
6465 )
6466 {
6467 char_u *s;
6468
6469 if (strings[0] == NULL)
6470 s = (char_u *)"";
6471 else if (strings[1] != NULL)
6472 {
6473 EMSG(_("E883: search pattern and expression register may not "
6474 "contain two or more lines"));
6475 return;
6476 }
6477 else
6478 s = strings[0];
6479 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6480 return;
6481 }
6482
6483 if (name == '_') /* black hole: nothing to do */
6484 return;
6485
6486 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6487 &yank_type) == FAIL)
6488 return;
6489
6490 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6491
6492 finish_write_reg(name, old_y_previous, old_y_current);
6493}
6494
6495 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6497 int name;
6498 char_u *str;
6499 int maxlen;
6500 int must_append;
6501 int yank_type;
6502 long block_len;
6503{
6504 struct yankreg *old_y_previous, *old_y_current;
6505 long len;
6506
Bram Moolenaare7566042005-06-17 22:00:15 +00006507 if (maxlen >= 0)
6508 len = maxlen;
6509 else
6510 len = (long)STRLEN(str);
6511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 /* Special case: '/' search pattern */
6513 if (name == '/')
6514 {
6515 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6516 return;
6517 }
6518
Bram Moolenaare7566042005-06-17 22:00:15 +00006519#ifdef FEAT_EVAL
6520 if (name == '=')
6521 {
6522 char_u *p, *s;
6523
6524 p = vim_strnsave(str, (int)len);
6525 if (p == NULL)
6526 return;
6527 if (must_append)
6528 {
6529 s = concat_str(get_expr_line_src(), p);
6530 vim_free(p);
6531 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006532 }
6533 set_expr_line(p);
6534 return;
6535 }
6536#endif
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (name == '_') /* black hole: nothing to do */
6539 return;
6540
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006541 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6542 &yank_type) == FAIL)
6543 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006545 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006547 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548}
6549#endif /* FEAT_EVAL */
6550
6551#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6552/*
6553 * Put a string into a register. When the register is not empty, the string
6554 * is appended.
6555 */
6556 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006557str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006559 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 char_u *str; /* string to put in register */
6561 long len; /* length of string */
6562 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006563 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006565 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 int lnum;
6567 long start;
6568 long i;
6569 int extra;
6570 int newlines; /* number of lines added */
6571 int extraline = 0; /* extra line at the end */
6572 int append = FALSE; /* append to last line in register */
6573 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006574 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006578 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 y_ptr->y_size = 0;
6580
Bram Moolenaard44347f2011-06-19 01:14:29 +02006581 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006582 type = ((str_list || (len > 0 && (str[len - 1] == NL
6583 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006584 ? MLINE : MCHAR);
6585 else
6586 type = yank_type;
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 /*
6589 * Count the number of lines within the string
6590 */
6591 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006592 if (str_list)
6593 {
6594 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006599 for (i = 0; i < len; i++)
6600 if (str[i] == '\n')
6601 ++newlines;
6602 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6603 {
6604 extraline = 1;
6605 ++newlines; /* count extra newline at the end */
6606 }
6607 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6608 {
6609 append = TRUE;
6610 --newlines; /* uncount newline when appending first line */
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613
6614 /*
6615 * Allocate an array to hold the pointers to the new register lines.
6616 * If the register was not empty, move the existing lines to the new array.
6617 */
6618 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6619 * sizeof(char_u *), TRUE);
6620 if (pp == NULL) /* out of memory */
6621 return;
6622 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6623 pp[lnum] = y_ptr->y_array[lnum];
6624 vim_free(y_ptr->y_array);
6625 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628 /*
6629 * Find the end of each line and save it into the array.
6630 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006631 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006633 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6634 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006635 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006636 pp[lnum] = vim_strnsave(*ss, i);
6637 if (i > maxlen)
6638 maxlen = i;
6639 }
6640 }
6641 else
6642 {
6643 for (start = 0; start < len + extraline; start += i + 1)
6644 {
6645 for (i = start; i < len; ++i) /* find the end of the line */
6646 if (str[i] == '\n')
6647 break;
6648 i -= start; /* i is now length of line */
6649 if (i > maxlen)
6650 maxlen = i;
6651 if (append)
6652 {
6653 --lnum;
6654 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6655 }
6656 else
6657 extra = 0;
6658 s = alloc((unsigned)(i + extra + 1));
6659 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006661 if (extra)
6662 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6663 if (append)
6664 vim_free(y_ptr->y_array[lnum]);
6665 if (i)
6666 mch_memmove(s + extra, str + start, (size_t)i);
6667 extra += i;
6668 s[extra] = NUL;
6669 y_ptr->y_array[lnum++] = s;
6670 while (--extra >= 0)
6671 {
6672 if (*s == NUL)
6673 *s = '\n'; /* replace NUL with newline */
6674 ++s;
6675 }
6676 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 }
6679 y_ptr->y_type = type;
6680 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 if (type == MBLOCK)
6682 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6683 else
6684 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685}
6686#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6687
6688 void
6689clear_oparg(oap)
6690 oparg_T *oap;
6691{
6692 vim_memset(oap, 0, sizeof(oparg_T));
6693}
6694
Bram Moolenaar7c626922005-02-07 22:01:03 +00006695static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006698 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 *
6700 * "Words" are counted by looking for boundaries between non-space and
6701 * space characters. (it seems to produce results that match 'wc'.)
6702 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006703 * Return value is byte count; word count for the line is added to "*wc".
6704 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 *
6706 * The function will only examine the first "limit" characters in the
6707 * line, stopping if it encounters an end-of-line (NUL byte). In that
6708 * case, eol_size will be added to the character count to account for
6709 * the size of the EOL character.
6710 */
6711 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006712line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 char_u *line;
6714 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006715 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 long limit;
6717 int eol_size;
6718{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006719 long i;
6720 long words = 0;
6721 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 int is_word = 0;
6723
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006724 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
6726 if (is_word)
6727 {
6728 if (vim_isspace(line[i]))
6729 {
6730 words++;
6731 is_word = 0;
6732 }
6733 }
6734 else if (!vim_isspace(line[i]))
6735 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006736 ++chars;
6737#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006739#else
6740 ++i;
6741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743
6744 if (is_word)
6745 words++;
6746 *wc += words;
6747
6748 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006749 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006752 chars += eol_size;
6753 }
6754 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 return i;
6756}
6757
6758/*
6759 * Give some info about the position of the cursor (for "g CTRL-G").
6760 * In Visual mode, give some info about the selected region. (In this case,
6761 * the *_count_cursor variables store running totals for the selection.)
6762 */
6763 void
6764cursor_pos_info()
6765{
6766 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006767 char_u buf1[50];
6768 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006770 long byte_count = 0;
6771 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 long char_count = 0;
6773 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 long word_count = 0;
6775 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006776 int eol_size;
6777 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 long line_count_selected = 0;
6779 pos_T min_pos, max_pos;
6780 oparg_T oparg;
6781 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 /*
6784 * Compute the length of the file in characters.
6785 */
6786 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6787 {
6788 MSG(_(no_lines_msg));
6789 }
6790 else
6791 {
6792 if (get_fileformat(curbuf) == EOL_DOS)
6793 eol_size = 2;
6794 else
6795 eol_size = 1;
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (VIsual_active)
6798 {
6799 if (lt(VIsual, curwin->w_cursor))
6800 {
6801 min_pos = VIsual;
6802 max_pos = curwin->w_cursor;
6803 }
6804 else
6805 {
6806 min_pos = curwin->w_cursor;
6807 max_pos = VIsual;
6808 }
6809 if (*p_sel == 'e' && max_pos.col > 0)
6810 --max_pos.col;
6811
6812 if (VIsual_mode == Ctrl_V)
6813 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006814#ifdef FEAT_LINEBREAK
6815 char_u * saved_sbr = p_sbr;
6816
6817 /* Make 'sbr' empty for a moment to get the correct size. */
6818 p_sbr = empty_option;
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 oparg.is_VIsual = 1;
6821 oparg.block_mode = TRUE;
6822 oparg.op_type = OP_NOP;
6823 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006824 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006825#ifdef FEAT_LINEBREAK
6826 p_sbr = saved_sbr;
6827#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006828 if (curwin->w_curswant == MAXCOL)
6829 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 /* Swap the start, end vcol if needed */
6831 if (oparg.end_vcol < oparg.start_vcol)
6832 {
6833 oparg.end_vcol += oparg.start_vcol;
6834 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6835 oparg.end_vcol -= oparg.start_vcol;
6836 }
6837 }
6838 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
6841 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6842 {
6843 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006844 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
6846 ui_breakcheck();
6847 if (got_int)
6848 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006849 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 /* Do extra processing for VIsual mode. */
6853 if (VIsual_active
6854 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6855 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006856 char_u *s = NULL;
6857 long len = 0L;
6858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 switch (VIsual_mode)
6860 {
6861 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006862#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006866#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006868#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006869 s = bd.textstart;
6870 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 break;
6872 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006873 s = ml_get(lnum);
6874 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 break;
6876 case 'v':
6877 {
6878 colnr_T start_col = (lnum == min_pos.lnum)
6879 ? min_pos.col : 0;
6880 colnr_T end_col = (lnum == max_pos.lnum)
6881 ? max_pos.col - start_col + 1 : MAXCOL;
6882
Bram Moolenaardef9e822004-12-31 20:58:58 +00006883 s = ml_get(lnum) + start_col;
6884 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 break;
6887 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006888 if (s != NULL)
6889 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006890 byte_count_cursor += line_count_info(s, &word_count_cursor,
6891 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006892 if (lnum == curbuf->b_ml.ml_line_count
6893 && !curbuf->b_p_eol
6894 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006895 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006896 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
6899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 /* In non-visual mode, check for the line the cursor is on */
6902 if (lnum == curwin->w_cursor.lnum)
6903 {
6904 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006905 char_count_cursor += char_count;
6906 byte_count_cursor = byte_count +
6907 line_count_info(ml_get(lnum),
6908 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 (long)(curwin->w_cursor.col + 1), eol_size);
6910 }
6911 }
6912 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006913 byte_count += line_count_info(ml_get(lnum), &word_count,
6914 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
6917 /* Correction for when last line doesn't have an EOL. */
6918 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006919 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (VIsual_active)
6922 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006923 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006926 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006927 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6929 }
6930 else
6931 buf1[0] = NUL;
6932
Bram Moolenaar7c626922005-02-07 22:01:03 +00006933 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006934 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006935 vim_snprintf((char *)IObuff, IOSIZE,
6936 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 buf1, line_count_selected,
6938 (long)curbuf->b_ml.ml_line_count,
6939 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006940 byte_count_cursor, byte_count);
6941 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006942 vim_snprintf((char *)IObuff, IOSIZE,
6943 _("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 +00006944 buf1, line_count_selected,
6945 (long)curbuf->b_ml.ml_line_count,
6946 word_count_cursor, word_count,
6947 char_count_cursor, char_count,
6948 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 p = ml_get_curline();
6953 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006954 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006956 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6957 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar7c626922005-02-07 22:01:03 +00006959 if (char_count_cursor == byte_count_cursor
6960 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006961 vim_snprintf((char *)IObuff, IOSIZE,
6962 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 (char *)buf1, (char *)buf2,
6964 (long)curwin->w_cursor.lnum,
6965 (long)curbuf->b_ml.ml_line_count,
6966 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006967 byte_count_cursor, byte_count);
6968 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006969 vim_snprintf((char *)IObuff, IOSIZE,
6970 _("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 +00006971 (char *)buf1, (char *)buf2,
6972 (long)curwin->w_cursor.lnum,
6973 (long)curbuf->b_ml.ml_line_count,
6974 word_count_cursor, word_count,
6975 char_count_cursor, char_count,
6976 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978
6979#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006980 byte_count = bomb_size();
6981 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006983 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#endif
6985 /* Don't shorten this message, the user asked for it. */
6986 p = p_shm;
6987 p_shm = (char_u *)"";
6988 msg(IObuff);
6989 p_shm = p;
6990 }
6991}