blob: a4261cb56e856fd5b9d17efc19b17392beb14ce8 [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
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100859 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100861 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100864 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 || regname == '"'
866 || regname == '-'
867 || regname == '_'
868#ifdef FEAT_CLIPBOARD
869 || regname == '*'
870 || regname == '+'
871#endif
872#ifdef FEAT_DND
873 || (!writing && regname == '~')
874#endif
875 )
876 return TRUE;
877 return FALSE;
878}
879
880/*
881 * Set y_current and y_append, according to the value of "regname".
882 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000883 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 *
885 * If regname is 0 and writing, use register 0
886 * If regname is 0 and reading, use previous register
887 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000888 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889get_yank_register(regname, writing)
890 int regname;
891 int writing;
892{
893 int i;
894
895 y_append = FALSE;
896 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
897 {
898 y_current = y_previous;
899 return;
900 }
901 i = regname;
902 if (VIM_ISDIGIT(i))
903 i -= '0';
904 else if (ASCII_ISLOWER(i))
905 i = CharOrdLow(i) + 10;
906 else if (ASCII_ISUPPER(i))
907 {
908 i = CharOrdUp(i) + 10;
909 y_append = TRUE;
910 }
911 else if (regname == '-')
912 i = DELETION_REGISTER;
913#ifdef FEAT_CLIPBOARD
914 /* When selection is not available, use register 0 instead of '*' */
915 else if (clip_star.available && regname == '*')
916 i = STAR_REGISTER;
917 /* When clipboard is not available, use register 0 instead of '+' */
918 else if (clip_plus.available && regname == '+')
919 i = PLUS_REGISTER;
920#endif
921#ifdef FEAT_DND
922 else if (!writing && regname == '~')
923 i = TILDE_REGISTER;
924#endif
925 else /* not 0-9, a-z, A-Z or '-': use register 0 */
926 i = 0;
927 y_current = &(y_regs[i]);
928 if (writing) /* remember the register we write into for do_put() */
929 y_previous = y_current;
930}
931
Bram Moolenaar8299df92004-07-10 09:47:34 +0000932#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
934 * When "regname" is a clipboard register, obtain the selection. If it's not
935 * available return zero, otherwise return "regname".
936 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938may_get_selection(regname)
939 int regname;
940{
941 if (regname == '*')
942 {
943 if (!clip_star.available)
944 regname = 0;
945 else
946 clip_get_selection(&clip_star);
947 }
948 else if (regname == '+')
949 {
950 if (!clip_plus.available)
951 regname = 0;
952 else
953 clip_get_selection(&clip_plus);
954 }
955 return regname;
956}
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959/*
960 * Obtain the contents of a "normal" register. The register is made empty.
961 * The returned pointer has allocated memory, use put_register() later.
962 */
963 void *
964get_register(name, copy)
965 int name;
966 int copy; /* make a copy, if FALSE make register empty. */
967{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000968 struct yankreg *reg;
969 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971#ifdef FEAT_CLIPBOARD
972 /* When Visual area changed, may have to update selection. Obtain the
973 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000974 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200976 if (clip_isautosel_star())
977 clip_update_selection(&clip_star);
978 may_get_selection(name);
979 }
980 if (name == '+' && clip_plus.available)
981 {
982 if (clip_isautosel_plus())
983 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 may_get_selection(name);
985 }
986#endif
987
988 get_yank_register(name, 0);
989 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
990 if (reg != NULL)
991 {
992 *reg = *y_current;
993 if (copy)
994 {
995 /* If we run out of memory some or all of the lines are empty. */
996 if (reg->y_size == 0)
997 reg->y_array = NULL;
998 else
999 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1000 * reg->y_size));
1001 if (reg->y_array != NULL)
1002 {
1003 for (i = 0; i < reg->y_size; ++i)
1004 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1005 }
1006 }
1007 else
1008 y_current->y_array = NULL;
1009 }
1010 return (void *)reg;
1011}
1012
1013/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001014 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 */
1016 void
1017put_register(name, reg)
1018 int name;
1019 void *reg;
1020{
1021 get_yank_register(name, 0);
1022 free_yank_all();
1023 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
1032 void
1033free_register(reg)
1034 void *reg;
1035{
1036 struct yankreg tmp;
1037
1038 tmp = *y_current;
1039 *y_current = *(struct yankreg *)reg;
1040 free_yank_all();
1041 vim_free(reg);
1042 *y_current = tmp;
1043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045#if defined(FEAT_MOUSE) || defined(PROTO)
1046/*
1047 * return TRUE if the current yank register has type MLINE
1048 */
1049 int
1050yank_register_mline(regname)
1051 int regname;
1052{
1053 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1054 return FALSE;
1055 if (regname == '_') /* black hole is always empty */
1056 return FALSE;
1057 get_yank_register(regname, FALSE);
1058 return (y_current->y_type == MLINE);
1059}
1060#endif
1061
1062/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001063 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
1068do_record(c)
1069 int c;
1070{
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 char_u *p;
1072 static int regname;
1073 struct yankreg *old_y_previous, *old_y_current;
1074 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (Recording == FALSE) /* start recording */
1077 {
1078 /* registers 0-9, a-z and " are allowed */
1079 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1080 retval = FAIL;
1081 else
1082 {
1083 Recording = TRUE;
1084 showmode();
1085 regname = c;
1086 retval = OK;
1087 }
1088 }
1089 else /* stop recording */
1090 {
1091 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1093 * needs to be removed again to put it in a register. exec_reg then
1094 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
1096 Recording = FALSE;
1097 MSG("");
1098 p = get_recorded();
1099 if (p == NULL)
1100 retval = FAIL;
1101 else
1102 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001103 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1104 vim_unescape_csi(p);
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * We don't want to change the default register here, so save and
1108 * restore the current register name.
1109 */
1110 old_y_previous = y_previous;
1111 old_y_current = y_current;
1112
1113 retval = stuff_yank(regname, p);
1114
1115 y_previous = old_y_previous;
1116 y_current = old_y_current;
1117 }
1118 }
1119 return retval;
1120}
1121
1122/*
1123 * Stuff string "p" into yank register "regname" as a single line (append if
1124 * uppercase). "p" must have been alloced.
1125 *
1126 * return FAIL for failure, OK otherwise
1127 */
1128 static int
1129stuff_yank(regname, p)
1130 int regname;
1131 char_u *p;
1132{
1133 char_u *lp;
1134 char_u **pp;
1135
1136 /* check for read-only register */
1137 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1138 {
1139 vim_free(p);
1140 return FAIL;
1141 }
1142 if (regname == '_') /* black hole: don't do anything */
1143 {
1144 vim_free(p);
1145 return OK;
1146 }
1147 get_yank_register(regname, TRUE);
1148 if (y_append && y_current->y_array != NULL)
1149 {
1150 pp = &(y_current->y_array[y_current->y_size - 1]);
1151 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1152 if (lp == NULL)
1153 {
1154 vim_free(p);
1155 return FAIL;
1156 }
1157 STRCPY(lp, *pp);
1158 STRCAT(lp, p);
1159 vim_free(p);
1160 vim_free(*pp);
1161 *pp = lp;
1162 }
1163 else
1164 {
1165 free_yank_all();
1166 if ((y_current->y_array =
1167 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1168 {
1169 vim_free(p);
1170 return FAIL;
1171 }
1172 y_current->y_array[0] = p;
1173 y_current->y_size = 1;
1174 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1175 }
1176 return OK;
1177}
1178
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * execute a yank register: copy it into the stuff buffer
1183 *
1184 * return FAIL for failure, OK otherwise
1185 */
1186 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001187do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 int regname;
1189 int colon; /* insert ':' before each line */
1190 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001191 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 long i;
1194 char_u *p;
1195 int retval = OK;
1196 int remap;
1197
1198 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001200 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001201 {
1202 EMSG(_("E748: No previously used register"));
1203 return FAIL;
1204 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001205 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 /* check for valid regname */
1208 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215#ifdef FEAT_CLIPBOARD
1216 regname = may_get_selection(regname);
1217#endif
1218
1219 if (regname == '_') /* black hole: don't stuff anything */
1220 return OK;
1221
1222#ifdef FEAT_CMDHIST
1223 if (regname == ':') /* use last command line */
1224 {
1225 if (last_cmdline == NULL)
1226 {
1227 EMSG(_(e_nolastcmd));
1228 return FAIL;
1229 }
1230 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1231 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001232 /* Escape all control characters with a CTRL-V */
1233 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001234 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001235 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001236 {
1237 /* When in Visual mode "'<,'>" will be prepended to the command.
1238 * Remove it when it's already there. */
1239 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001240 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001241 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001242 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001243 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001244 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246#endif
1247#ifdef FEAT_EVAL
1248 else if (regname == '=')
1249 {
1250 p = get_expr_line();
1251 if (p == NULL)
1252 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001253 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 vim_free(p);
1255 }
1256#endif
1257 else if (regname == '.') /* use last inserted text */
1258 {
1259 p = get_last_insert_save();
1260 if (p == NULL)
1261 {
1262 EMSG(_(e_noinstext));
1263 return FAIL;
1264 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001265 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 vim_free(p);
1267 }
1268 else
1269 {
1270 get_yank_register(regname, FALSE);
1271 if (y_current->y_array == NULL)
1272 return FAIL;
1273
1274 /* Disallow remaping for ":@r". */
1275 remap = colon ? REMAP_NONE : REMAP_YES;
1276
1277 /*
1278 * Insert lines into typeahead buffer, from last one to first one.
1279 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001280 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 for (i = y_current->y_size; --i >= 0; )
1282 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 char_u *escaped;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 /* insert NL between lines and after last line if type is MLINE */
1286 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1287 || addcr)
1288 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001289 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return FAIL;
1291 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001292 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1293 if (escaped == NULL)
1294 return FAIL;
1295 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1296 vim_free(escaped);
1297 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001299 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 == FAIL)
1301 return FAIL;
1302 }
1303 Exec_reg = TRUE; /* disable the 'q' command */
1304 }
1305 return retval;
1306}
1307
1308/*
1309 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1310 * used only after other typeahead has been processed.
1311 */
1312 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001313put_reedit_in_typebuf(silent)
1314 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 char_u buf[3];
1317
1318 if (restart_edit != NUL)
1319 {
1320 if (restart_edit == 'V')
1321 {
1322 buf[0] = 'g';
1323 buf[1] = 'R';
1324 buf[2] = NUL;
1325 }
1326 else
1327 {
1328 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1329 buf[1] = NUL;
1330 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001331 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 restart_edit = NUL;
1333 }
1334}
1335
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001336/*
1337 * Insert register contents "s" into the typeahead buffer, so that it will be
1338 * executed again.
1339 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1340 * no remapping.
1341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001343put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001345 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001347 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int retval = OK;
1350
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001351 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001355 {
1356 char_u *p;
1357
1358 if (esc)
1359 p = vim_strsave_escape_csi(s);
1360 else
1361 p = s;
1362 if (p == NULL)
1363 retval = FAIL;
1364 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001365 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1366 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001367 if (esc)
1368 vim_free(p);
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001371 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 return retval;
1373}
1374
1375/*
1376 * Insert a yank register: copy it into the Read buffer.
1377 * Used by CTRL-R command and middle mouse button in insert mode.
1378 *
1379 * return FAIL for failure, OK otherwise
1380 */
1381 int
1382insert_reg(regname, literally)
1383 int regname;
1384 int literally; /* insert literally, not as if typed */
1385{
1386 long i;
1387 int retval = OK;
1388 char_u *arg;
1389 int allocated;
1390
1391 /*
1392 * It is possible to get into an endless loop by having CTRL-R a in
1393 * register a and then, in insert mode, doing CTRL-R a.
1394 * If you hit CTRL-C, the loop will be broken here.
1395 */
1396 ui_breakcheck();
1397 if (got_int)
1398 return FAIL;
1399
1400 /* check for valid regname */
1401 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1402 return FAIL;
1403
1404#ifdef FEAT_CLIPBOARD
1405 regname = may_get_selection(regname);
1406#endif
1407
1408 if (regname == '.') /* insert last inserted text */
1409 retval = stuff_inserted(NUL, 1L, TRUE);
1410 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1411 {
1412 if (arg == NULL)
1413 return FAIL;
1414 stuffescaped(arg, literally);
1415 if (allocated)
1416 vim_free(arg);
1417 }
1418 else /* name or number register */
1419 {
1420 get_yank_register(regname, FALSE);
1421 if (y_current->y_array == NULL)
1422 retval = FAIL;
1423 else
1424 {
1425 for (i = 0; i < y_current->y_size; ++i)
1426 {
1427 stuffescaped(y_current->y_array[i], literally);
1428 /*
1429 * Insert a newline between lines and after last line if
1430 * y_type is MLINE.
1431 */
1432 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1433 stuffcharReadbuff('\n');
1434 }
1435 }
1436 }
1437
1438 return retval;
1439}
1440
1441/*
1442 * Stuff a string into the typeahead buffer, such that edit() will insert it
1443 * literally ("literally" TRUE) or interpret is as typed characters.
1444 */
1445 static void
1446stuffescaped(arg, literally)
1447 char_u *arg;
1448 int literally;
1449{
1450 int c;
1451 char_u *start;
1452
1453 while (*arg != NUL)
1454 {
1455 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1456 * stuff K_SPECIAL to get the effect of a special key when "literally"
1457 * is TRUE. */
1458 start = arg;
1459 while ((*arg >= ' '
1460#ifndef EBCDIC
1461 && *arg < DEL /* EBCDIC: chars above space are normal */
1462#endif
1463 )
1464 || (*arg == K_SPECIAL && !literally))
1465 ++arg;
1466 if (arg > start)
1467 stuffReadbuffLen(start, (long)(arg - start));
1468
1469 /* stuff a single special character */
1470 if (*arg != NUL)
1471 {
1472#ifdef FEAT_MBYTE
1473 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001474 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
1476#endif
1477 c = *arg++;
1478 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1479 stuffcharReadbuff(Ctrl_V);
1480 stuffcharReadbuff(c);
1481 }
1482 }
1483}
1484
1485/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001486 * If "regname" is a special register, return TRUE and store a pointer to its
1487 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001489 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490get_spec_reg(regname, argp, allocated, errmsg)
1491 int regname;
1492 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001493 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int errmsg; /* give error message when failing */
1495{
1496 int cnt;
1497
1498 *argp = NULL;
1499 *allocated = FALSE;
1500 switch (regname)
1501 {
1502 case '%': /* file name */
1503 if (errmsg)
1504 check_fname(); /* will give emsg if not set */
1505 *argp = curbuf->b_fname;
1506 return TRUE;
1507
1508 case '#': /* alternate file name */
1509 *argp = getaltfname(errmsg); /* may give emsg if not set */
1510 return TRUE;
1511
1512#ifdef FEAT_EVAL
1513 case '=': /* result of expression */
1514 *argp = get_expr_line();
1515 *allocated = TRUE;
1516 return TRUE;
1517#endif
1518
1519 case ':': /* last command line */
1520 if (last_cmdline == NULL && errmsg)
1521 EMSG(_(e_nolastcmd));
1522 *argp = last_cmdline;
1523 return TRUE;
1524
1525 case '/': /* last search-pattern */
1526 if (last_search_pat() == NULL && errmsg)
1527 EMSG(_(e_noprevre));
1528 *argp = last_search_pat();
1529 return TRUE;
1530
1531 case '.': /* last inserted text */
1532 *argp = get_last_insert_save();
1533 *allocated = TRUE;
1534 if (*argp == NULL && errmsg)
1535 EMSG(_(e_noinstext));
1536 return TRUE;
1537
1538#ifdef FEAT_SEARCHPATH
1539 case Ctrl_F: /* Filename under cursor */
1540 case Ctrl_P: /* Path under cursor, expand via "path" */
1541 if (!errmsg)
1542 return FALSE;
1543 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001544 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 *allocated = TRUE;
1546 return TRUE;
1547#endif
1548
1549 case Ctrl_W: /* word under cursor */
1550 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1551 if (!errmsg)
1552 return FALSE;
1553 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1554 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1555 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1556 *allocated = TRUE;
1557 return TRUE;
1558
1559 case '_': /* black hole: always empty */
1560 *argp = (char_u *)"";
1561 return TRUE;
1562 }
1563
1564 return FALSE;
1565}
1566
1567/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001568 * Paste a yank register into the command line.
1569 * Only for non-special registers.
1570 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * insert_reg() can't be used here, because special characters from the
1572 * register contents will be interpreted as commands.
1573 *
1574 * return FAIL for failure, OK otherwise
1575 */
1576 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001577cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 int regname;
1579 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001580 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 get_yank_register(regname, FALSE);
1585 if (y_current->y_array == NULL)
1586 return FAIL;
1587
1588 for (i = 0; i < y_current->y_size; ++i)
1589 {
1590 cmdline_paste_str(y_current->y_array[i], literally);
1591
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001592 /* Insert ^M between lines and after last line if type is MLINE.
1593 * Don't do this when "remcr" is TRUE and the next line is empty. */
1594 if (y_current->y_type == MLINE
1595 || (i < y_current->y_size - 1
1596 && !(remcr
1597 && i == y_current->y_size - 2
1598 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 cmdline_paste_str((char_u *)"\r", literally);
1600
1601 /* Check for CTRL-C, in case someone tries to paste a few thousand
1602 * lines and gets bored. */
1603 ui_breakcheck();
1604 if (got_int)
1605 return FAIL;
1606 }
1607 return OK;
1608}
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1611/*
1612 * Adjust the register name pointed to with "rp" for the clipboard being
1613 * used always and the clipboard being available.
1614 */
1615 void
1616adjust_clip_reg(rp)
1617 int *rp;
1618{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001619 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1620 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001621 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1622 {
1623 if (clip_unnamed != 0)
1624 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001625 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001626 else
1627 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1628 ? '+' : '*';
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!clip_star.available && *rp == '*')
1631 *rp = 0;
1632 if (!clip_plus.available && *rp == '+')
1633 *rp = 0;
1634}
1635#endif
1636
1637/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001638 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001640 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
1642 int
1643op_delete(oap)
1644 oparg_T *oap;
1645{
1646 int n;
1647 linenr_T lnum;
1648 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 char_u *newp, *oldp;
1650 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1652 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001653 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1656 return OK;
1657
1658 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1659 if (oap->empty)
1660 return u_save_cursor();
1661
1662 if (!curbuf->b_p_ma)
1663 {
1664 EMSG(_(e_modifiable));
1665 return FAIL;
1666 }
1667
1668#ifdef FEAT_CLIPBOARD
1669 adjust_clip_reg(&oap->regname);
1670#endif
1671
1672#ifdef FEAT_MBYTE
1673 if (has_mbyte)
1674 mb_adjust_opend(oap);
1675#endif
1676
Bram Moolenaard04b7502010-07-08 22:27:55 +02001677 /*
1678 * Imitate the strange Vi behaviour: If the delete spans more than one
1679 * line and motion_type == MCHAR and the result is a blank line, make the
1680 * delete linewise. Don't do this for the change command or Visual mode.
1681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001684 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001686 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && oap->op_type == OP_DELETE)
1688 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001689 ptr = ml_get(oap->end.lnum) + oap->end.col;
1690 if (*ptr != NUL)
1691 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ptr = skipwhite(ptr);
1693 if (*ptr == NUL && inindent(0))
1694 oap->motion_type = MLINE;
1695 }
1696
Bram Moolenaard04b7502010-07-08 22:27:55 +02001697 /*
1698 * Check for trying to delete (e.g. "D") in an empty line.
1699 * Note: For the change operator it is ok.
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if ( oap->motion_type == MCHAR
1702 && oap->line_count == 1
1703 && oap->op_type == OP_DELETE
1704 && *ml_get(oap->start.lnum) == NUL)
1705 {
1706 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001707 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * 'cpoptions' (Vi compatible).
1709 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001710#ifdef FEAT_VIRTUALEDIT
1711 if (virtual_op)
1712 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1713 * marks as if it happened. */
1714 goto setmarks;
1715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1717 beep_flush();
1718 return OK;
1719 }
1720
Bram Moolenaard04b7502010-07-08 22:27:55 +02001721 /*
1722 * Do a yank of whatever we're about to delete.
1723 * If a yank register was specified, put the deleted text into that
1724 * register. For the black hole register '_' don't yank anything.
1725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (oap->regname != '_')
1727 {
1728 if (oap->regname != 0)
1729 {
1730 /* check for read-only register */
1731 if (!valid_yank_reg(oap->regname, TRUE))
1732 {
1733 beep_flush();
1734 return OK;
1735 }
1736 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1737 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1738 did_yank = TRUE;
1739 }
1740
1741 /*
1742 * Put deleted text into register 1 and shift number registers if the
1743 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001744 * Use the register name from before adjust_clip_reg() may have
1745 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001747 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 || oap->line_count > 1 || oap->use_reg_one)
1749 {
1750 y_current = &y_regs[9];
1751 free_yank_all(); /* free register nine */
1752 for (n = 9; n > 1; --n)
1753 y_regs[n] = y_regs[n - 1];
1754 y_previous = y_current = &y_regs[1];
1755 y_regs[1].y_array = NULL; /* set register one to empty */
1756 if (op_yank(oap, TRUE, FALSE) == OK)
1757 did_yank = TRUE;
1758 }
1759
Bram Moolenaar84298db2012-04-20 13:46:08 +02001760 /* Yank into small delete register when no named register specified
1761 * and the delete is within one line. */
1762 if ((
1763#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001764 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1765 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001766#endif
1767 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && oap->line_count == 1)
1769 {
1770 oap->regname = '-';
1771 get_yank_register(oap->regname, TRUE);
1772 if (op_yank(oap, TRUE, FALSE) == OK)
1773 did_yank = TRUE;
1774 oap->regname = 0;
1775 }
1776
1777 /*
1778 * If there's too much stuff to fit in the yank register, then get a
1779 * confirmation before doing the delete. This is crude, but simple.
1780 * And it avoids doing a delete of something we can't put back if we
1781 * want.
1782 */
1783 if (!did_yank)
1784 {
1785 int msg_silent_save = msg_silent;
1786
1787 msg_silent = 0; /* must display the prompt */
1788 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1789 msg_silent = msg_silent_save;
1790 if (n != 'y')
1791 {
1792 EMSG(_(e_abort));
1793 return FAIL;
1794 }
1795 }
1796 }
1797
Bram Moolenaard04b7502010-07-08 22:27:55 +02001798 /*
1799 * block mode delete
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (oap->block_mode)
1802 {
1803 if (u_save((linenr_T)(oap->start.lnum - 1),
1804 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1805 return FAIL;
1806
1807 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1808 {
1809 block_prep(oap, &bd, lnum, TRUE);
1810 if (bd.textlen == 0) /* nothing to delete */
1811 continue;
1812
1813 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1814 if (lnum == curwin->w_cursor.lnum)
1815 {
1816 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1817# ifdef FEAT_VIRTUALEDIT
1818 curwin->w_cursor.coladd = 0;
1819# endif
1820 }
1821
1822 /* n == number of chars deleted
1823 * If we delete a TAB, it may be replaced by several characters.
1824 * Thus the number of characters may increase!
1825 */
1826 n = bd.textlen - bd.startspaces - bd.endspaces;
1827 oldp = ml_get(lnum);
1828 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1829 if (newp == NULL)
1830 continue;
1831 /* copy up to deleted part */
1832 mch_memmove(newp, oldp, (size_t)bd.textcol);
1833 /* insert spaces */
1834 copy_spaces(newp + bd.textcol,
1835 (size_t)(bd.startspaces + bd.endspaces));
1836 /* copy the part after the deleted part */
1837 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001838 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* replace the line */
1840 ml_replace(lnum, newp, FALSE);
1841 }
1842
1843 check_cursor_col();
1844 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1845 oap->end.lnum + 1, 0L);
1846 oap->line_count = 0; /* no lines deleted */
1847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001848 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 if (oap->op_type == OP_CHANGE)
1851 {
1852 /* Delete the lines except the first one. Temporarily move the
1853 * cursor to the next line. Save the current line number, if the
1854 * last line is deleted it may be changed.
1855 */
1856 if (oap->line_count > 1)
1857 {
1858 lnum = curwin->w_cursor.lnum;
1859 ++curwin->w_cursor.lnum;
1860 del_lines((long)(oap->line_count - 1), TRUE);
1861 curwin->w_cursor.lnum = lnum;
1862 }
1863 if (u_save_cursor() == FAIL)
1864 return FAIL;
1865 if (curbuf->b_p_ai) /* don't delete indent */
1866 {
1867 beginline(BL_WHITE); /* cursor on first non-white */
1868 did_ai = TRUE; /* delete the indent when ESC hit */
1869 ai_col = curwin->w_cursor.col;
1870 }
1871 else
1872 beginline(0); /* cursor in column 0 */
1873 truncate_line(FALSE); /* delete the rest of the line */
1874 /* leave cursor past last char in line */
1875 if (oap->line_count > 1)
1876 u_clearline(); /* "U" command not possible after "2cc" */
1877 }
1878 else
1879 {
1880 del_lines(oap->line_count, TRUE);
1881 beginline(BL_WHITE | BL_FIX);
1882 u_clearline(); /* "U" command not possible after "dd" */
1883 }
1884 }
1885 else
1886 {
1887#ifdef FEAT_VIRTUALEDIT
1888 if (virtual_op)
1889 {
1890 int endcol = 0;
1891
1892 /* For virtualedit: break the tabs that are partly included. */
1893 if (gchar_pos(&oap->start) == '\t')
1894 {
1895 if (u_save_cursor() == FAIL) /* save first line for undo */
1896 return FAIL;
1897 if (oap->line_count == 1)
1898 endcol = getviscol2(oap->end.col, oap->end.coladd);
1899 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1900 oap->start = curwin->w_cursor;
1901 if (oap->line_count == 1)
1902 {
1903 coladvance(endcol);
1904 oap->end.col = curwin->w_cursor.col;
1905 oap->end.coladd = curwin->w_cursor.coladd;
1906 curwin->w_cursor = oap->start;
1907 }
1908 }
1909
1910 /* Break a tab only when it's included in the area. */
1911 if (gchar_pos(&oap->end) == '\t'
1912 && (int)oap->end.coladd < oap->inclusive)
1913 {
1914 /* save last line for undo */
1915 if (u_save((linenr_T)(oap->end.lnum - 1),
1916 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1917 return FAIL;
1918 curwin->w_cursor = oap->end;
1919 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1920 oap->end = curwin->w_cursor;
1921 curwin->w_cursor = oap->start;
1922 }
1923 }
1924#endif
1925
1926 if (oap->line_count == 1) /* delete characters within one line */
1927 {
1928 if (u_save_cursor() == FAIL) /* save line for undo */
1929 return FAIL;
1930
1931 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001932 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 && oap->op_type == OP_CHANGE
1934 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001935 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 display_dollar(oap->end.col - !oap->inclusive);
1937
1938 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1939
1940#ifdef FEAT_VIRTUALEDIT
1941 if (virtual_op)
1942 {
1943 /* fix up things for virtualedit-delete:
1944 * break the tabs which are going to get in our way
1945 */
1946 char_u *curline = ml_get_curline();
1947 int len = (int)STRLEN(curline);
1948
1949 if (oap->end.coladd != 0
1950 && (int)oap->end.col >= len - 1
1951 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1952 n++;
1953 /* Delete at least one char (e.g, when on a control char). */
1954 if (n == 0 && oap->start.coladd != oap->end.coladd)
1955 n = 1;
1956
1957 /* When deleted a char in the line, reset coladd. */
1958 if (gchar_cursor() != NUL)
1959 curwin->w_cursor.coladd = 0;
1960 }
1961#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001962 if (oap->op_type == OP_DELETE
1963 && oap->inclusive
1964 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001965 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1966 {
1967 /* Special case: gH<Del> deletes the last line. */
1968 del_lines(1L, FALSE);
1969 }
1970 else
1971 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001972 (void)del_bytes((long)n, !virtual_op,
1973 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 else /* delete characters between lines */
1977 {
1978 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001979 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 /* save deleted and changed lines for undo */
1982 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1983 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1984 return FAIL;
1985
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001986 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 truncate_line(TRUE); /* delete from cursor to end of line */
1988
1989 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1990 ++curwin->w_cursor.lnum;
1991 del_lines((long)(oap->line_count - 2), FALSE);
1992
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001993 if (delete_last_line)
1994 oap->end.lnum = curbuf->b_ml.ml_line_count;
1995
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001996 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001997 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001998 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1999 {
2000 /* Special case: gH<Del> deletes the last line. */
2001 del_lines(1L, FALSE);
2002 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002003 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2004 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002005 }
2006 else
2007 {
2008 /* delete from start of line until op_end */
2009 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002010 (void)del_bytes((long)n, !virtual_op,
2011 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002012 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2013 }
2014 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002015 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 }
2018
2019 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2020
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002021#ifdef FEAT_VIRTUALEDIT
2022setmarks:
2023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (oap->block_mode)
2025 {
2026 curbuf->b_op_end.lnum = oap->end.lnum;
2027 curbuf->b_op_end.col = oap->start.col;
2028 }
2029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 curbuf->b_op_end = oap->start;
2031 curbuf->b_op_start = oap->start;
2032
2033 return OK;
2034}
2035
2036#ifdef FEAT_MBYTE
2037/*
2038 * Adjust end of operating area for ending on a multi-byte character.
2039 * Used for deletion.
2040 */
2041 static void
2042mb_adjust_opend(oap)
2043 oparg_T *oap;
2044{
2045 char_u *p;
2046
2047 if (oap->inclusive)
2048 {
2049 p = ml_get(oap->end.lnum);
2050 oap->end.col += mb_tail_off(p, p + oap->end.col);
2051 }
2052}
2053#endif
2054
2055#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2056/*
2057 * Replace a whole area with one character.
2058 */
2059 int
2060op_replace(oap, c)
2061 oparg_T *oap;
2062 int c;
2063{
2064 int n, numc;
2065#ifdef FEAT_MBYTE
2066 int num_chars;
2067#endif
2068 char_u *newp, *oldp;
2069 size_t oldlen;
2070 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002071 char_u *after_p = NULL;
2072 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2075 return OK; /* nothing to do */
2076
Bram Moolenaard9820532013-11-04 01:41:17 +01002077 if (had_ctrl_v_cr)
2078 c = (c == -1 ? '\r' : '\n');
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef FEAT_MBYTE
2081 if (has_mbyte)
2082 mb_adjust_opend(oap);
2083#endif
2084
2085 if (u_save((linenr_T)(oap->start.lnum - 1),
2086 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2087 return FAIL;
2088
2089 /*
2090 * block mode replace
2091 */
2092 if (oap->block_mode)
2093 {
2094 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2095 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2096 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002097 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2099 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2100 continue; /* nothing to replace */
2101
2102 /* n == number of extra chars required
2103 * If we split a TAB, it may be replaced by several characters.
2104 * Thus the number of characters may increase!
2105 */
2106#ifdef FEAT_VIRTUALEDIT
2107 /* If the range starts in virtual space, count the initial
2108 * coladd offset as part of "startspaces" */
2109 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2110 {
2111 pos_T vpos;
2112
Bram Moolenaara1381de2009-11-03 15:44:21 +00002113 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 getvpos(&vpos, oap->start_vcol);
2115 bd.startspaces += vpos.coladd;
2116 n = bd.startspaces;
2117 }
2118 else
2119#endif
2120 /* allow for pre spaces */
2121 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2122
2123 /* allow for post spp */
2124 n += (bd.endspaces
2125#ifdef FEAT_VIRTUALEDIT
2126 && !bd.is_oneChar
2127#endif
2128 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2129 /* Figure out how many characters to replace. */
2130 numc = oap->end_vcol - oap->start_vcol + 1;
2131 if (bd.is_short && (!virtual_op || bd.is_MAX))
2132 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2133
2134#ifdef FEAT_MBYTE
2135 /* A double-wide character can be replaced only up to half the
2136 * times. */
2137 if ((*mb_char2cells)(c) > 1)
2138 {
2139 if ((numc & 1) && !bd.is_short)
2140 {
2141 ++bd.endspaces;
2142 ++n;
2143 }
2144 numc = numc / 2;
2145 }
2146
2147 /* Compute bytes needed, move character count to num_chars. */
2148 num_chars = numc;
2149 numc *= (*mb_char2len)(c);
2150#endif
2151 /* oldlen includes textlen, so don't double count */
2152 n += numc - bd.textlen;
2153
2154 oldp = ml_get_curline();
2155 oldlen = STRLEN(oldp);
2156 newp = alloc_check((unsigned)oldlen + 1 + n);
2157 if (newp == NULL)
2158 continue;
2159 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2160 /* copy up to deleted part */
2161 mch_memmove(newp, oldp, (size_t)bd.textcol);
2162 oldp += bd.textcol + bd.textlen;
2163 /* insert pre-spaces */
2164 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2165 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002166 /* -1/-2 is used for entering CR literally. */
2167 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002169#ifdef FEAT_MBYTE
2170 if (has_mbyte)
2171 {
2172 n = (int)STRLEN(newp);
2173 while (--num_chars >= 0)
2174 n += (*mb_char2bytes)(c, newp + n);
2175 }
2176 else
2177#endif
2178 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2179 if (!bd.is_short)
2180 {
2181 /* insert post-spaces */
2182 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2183 /* copy the part after the changed part */
2184 STRMOVE(newp + STRLEN(newp), oldp);
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002189 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002190 after_p = alloc_check(
2191 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002192 if (after_p != NULL)
2193 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
2195 /* replace the line */
2196 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002197 if (after_p != NULL)
2198 {
2199 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2200 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2201 oap->end.lnum++;
2202 vim_free(after_p);
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 }
2206 else
2207 {
2208 /*
2209 * MCHAR and MLINE motion replace.
2210 */
2211 if (oap->motion_type == MLINE)
2212 {
2213 oap->start.col = 0;
2214 curwin->w_cursor.col = 0;
2215 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2216 if (oap->end.col)
2217 --oap->end.col;
2218 }
2219 else if (!oap->inclusive)
2220 dec(&(oap->end));
2221
2222 while (ltoreq(curwin->w_cursor, oap->end))
2223 {
2224 n = gchar_cursor();
2225 if (n != NUL)
2226 {
2227#ifdef FEAT_MBYTE
2228 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2229 {
2230 /* This is slow, but it handles replacing a single-byte
2231 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002232 if (curwin->w_cursor.lnum == oap->end.lnum)
2233 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 n = State;
2235 State = REPLACE;
2236 ins_char(c);
2237 State = n;
2238 /* Backup to the replaced character. */
2239 dec_cursor();
2240 }
2241 else
2242#endif
2243 {
2244#ifdef FEAT_VIRTUALEDIT
2245 if (n == TAB)
2246 {
2247 int end_vcol = 0;
2248
2249 if (curwin->w_cursor.lnum == oap->end.lnum)
2250 {
2251 /* oap->end has to be recalculated when
2252 * the tab breaks */
2253 end_vcol = getviscol2(oap->end.col,
2254 oap->end.coladd);
2255 }
2256 coladvance_force(getviscol());
2257 if (curwin->w_cursor.lnum == oap->end.lnum)
2258 getvpos(&oap->end, end_vcol);
2259 }
2260#endif
2261 pchar(curwin->w_cursor, c);
2262 }
2263 }
2264#ifdef FEAT_VIRTUALEDIT
2265 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2266 {
2267 int virtcols = oap->end.coladd;
2268
2269 if (curwin->w_cursor.lnum == oap->start.lnum
2270 && oap->start.col == oap->end.col && oap->start.coladd)
2271 virtcols -= oap->start.coladd;
2272
2273 /* oap->end has been trimmed so it's effectively inclusive;
2274 * as a result an extra +1 must be counted so we don't
2275 * trample the NUL byte. */
2276 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2277 curwin->w_cursor.col -= (virtcols + 1);
2278 for (; virtcols >= 0; virtcols--)
2279 {
2280 pchar(curwin->w_cursor, c);
2281 if (inc(&curwin->w_cursor) == -1)
2282 break;
2283 }
2284 }
2285#endif
2286
2287 /* Advance to next character, stop at the end of the file. */
2288 if (inc_cursor() == -1)
2289 break;
2290 }
2291 }
2292
2293 curwin->w_cursor = oap->start;
2294 check_cursor();
2295 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2296
2297 /* Set "'[" and "']" marks. */
2298 curbuf->b_op_start = oap->start;
2299 curbuf->b_op_end = oap->end;
2300
2301 return OK;
2302}
2303#endif
2304
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002305static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2309 */
2310 void
2311op_tilde(oap)
2312 oparg_T *oap;
2313{
2314 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002316 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 if (u_save((linenr_T)(oap->start.lnum - 1),
2319 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2320 return;
2321
2322 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (oap->block_mode) /* Visual block mode */
2324 {
2325 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2326 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002327 int one_change;
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 block_prep(oap, &bd, pos.lnum, FALSE);
2330 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002331 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2332 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002333
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002334#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002335 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2338
Bram Moolenaar009b2592004-10-24 19:18:58 +00002339 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2340 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002342 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 if (did_change)
2347 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2348 }
2349 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 if (oap->motion_type == MLINE)
2352 {
2353 oap->start.col = 0;
2354 pos.col = 0;
2355 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2356 if (oap->end.col)
2357 --oap->end.col;
2358 }
2359 else if (!oap->inclusive)
2360 dec(&(oap->end));
2361
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002362 if (pos.lnum == oap->end.lnum)
2363 did_change = swapchars(oap->op_type, &pos,
2364 oap->end.col - pos.col + 1);
2365 else
2366 for (;;)
2367 {
2368 did_change |= swapchars(oap->op_type, &pos,
2369 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2370 (int)STRLEN(ml_get_pos(&pos)));
2371 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2372 break;
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (did_change)
2375 {
2376 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2377 0L);
2378#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002379 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
2381 char_u *ptr;
2382 int count;
2383
2384 pos = oap->start;
2385 while (pos.lnum < oap->end.lnum)
2386 {
2387 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002389 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002391 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 pos.col = 0;
2393 pos.lnum++;
2394 }
2395 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2396 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002397 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002399 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401#endif
2402 }
2403 }
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (!did_change && oap->is_VIsual)
2406 /* No change: need to remove the Visual selection */
2407 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /*
2410 * Set '[ and '] marks.
2411 */
2412 curbuf->b_op_start = oap->start;
2413 curbuf->b_op_end = oap->end;
2414
2415 if (oap->line_count > p_report)
2416 {
2417 if (oap->line_count == 1)
2418 MSG(_("1 line changed"));
2419 else
2420 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2421 }
2422}
2423
2424/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002425 * Invoke swapchar() on "length" bytes at position "pos".
2426 * "pos" is advanced to just after the changed characters.
2427 * "length" is rounded up to include the whole last multi-byte character.
2428 * Also works correctly when the number of bytes changes.
2429 * Returns TRUE if some character was changed.
2430 */
2431 static int
2432swapchars(op_type, pos, length)
2433 int op_type;
2434 pos_T *pos;
2435 int length;
2436{
2437 int todo;
2438 int did_change = 0;
2439
2440 for (todo = length; todo > 0; --todo)
2441 {
2442# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002443 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002444 {
2445 int len = (*mb_ptr2len)(ml_get_pos(pos));
2446
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002447 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002448 if (len > 0)
2449 todo -= len - 1;
2450 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002451# endif
2452 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002453 if (inc(pos) == -1) /* at end of file */
2454 break;
2455 }
2456 return did_change;
2457}
2458
2459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 * If op_type == OP_UPPER: make uppercase,
2461 * if op_type == OP_LOWER: make lowercase,
2462 * if op_type == OP_ROT13: do rot13 encoding,
2463 * else swap case of character at 'pos'
2464 * returns TRUE when something actually changed.
2465 */
2466 int
2467swapchar(op_type, pos)
2468 int op_type;
2469 pos_T *pos;
2470{
2471 int c;
2472 int nc;
2473
2474 c = gchar_pos(pos);
2475
2476 /* Only do rot13 encoding for ASCII characters. */
2477 if (c >= 0x80 && op_type == OP_ROT13)
2478 return FALSE;
2479
2480#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002481 if (op_type == OP_UPPER && c == 0xdf
2482 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002483 {
2484 pos_T sp = curwin->w_cursor;
2485
2486 /* Special handling of German sharp s: change to "SS". */
2487 curwin->w_cursor = *pos;
2488 del_char(FALSE);
2489 ins_char('S');
2490 ins_char('S');
2491 curwin->w_cursor = sp;
2492 inc(pos);
2493 }
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2496 return FALSE;
2497#endif
2498 nc = c;
2499 if (MB_ISLOWER(c))
2500 {
2501 if (op_type == OP_ROT13)
2502 nc = ROT13(c, 'a');
2503 else if (op_type != OP_LOWER)
2504 nc = MB_TOUPPER(c);
2505 }
2506 else if (MB_ISUPPER(c))
2507 {
2508 if (op_type == OP_ROT13)
2509 nc = ROT13(c, 'A');
2510 else if (op_type != OP_UPPER)
2511 nc = MB_TOLOWER(c);
2512 }
2513 if (nc != c)
2514 {
2515#ifdef FEAT_MBYTE
2516 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2517 {
2518 pos_T sp = curwin->w_cursor;
2519
2520 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002521 /* don't use del_char(), it also removes composing chars */
2522 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 ins_char(nc);
2524 curwin->w_cursor = sp;
2525 }
2526 else
2527#endif
2528 pchar(*pos, nc);
2529 return TRUE;
2530 }
2531 return FALSE;
2532}
2533
2534#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2535/*
2536 * op_insert - Insert and append operators for Visual mode.
2537 */
2538 void
2539op_insert(oap, count1)
2540 oparg_T *oap;
2541 long count1;
2542{
2543 long ins_len, pre_textlen = 0;
2544 char_u *firstline, *ins_text;
2545 struct block_def bd;
2546 int i;
2547
2548 /* edit() changes this - record it for OP_APPEND */
2549 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2550
2551 /* vis block is still marked. Get rid of it now. */
2552 curwin->w_cursor.lnum = oap->start.lnum;
2553 update_screen(INVERTED);
2554
2555 if (oap->block_mode)
2556 {
2557#ifdef FEAT_VIRTUALEDIT
2558 /* When 'virtualedit' is used, need to insert the extra spaces before
2559 * doing block_prep(). When only "block" is used, virtual edit is
2560 * already disabled, but still need it when calling
2561 * coladvance_force(). */
2562 if (curwin->w_cursor.coladd > 0)
2563 {
2564 int old_ve_flags = ve_flags;
2565
2566 ve_flags = VE_ALL;
2567 if (u_save_cursor() == FAIL)
2568 return;
2569 coladvance_force(oap->op_type == OP_APPEND
2570 ? oap->end_vcol + 1 : getviscol());
2571 if (oap->op_type == OP_APPEND)
2572 --curwin->w_cursor.col;
2573 ve_flags = old_ve_flags;
2574 }
2575#endif
2576 /* Get the info about the block before entering the text */
2577 block_prep(oap, &bd, oap->start.lnum, TRUE);
2578 firstline = ml_get(oap->start.lnum) + bd.textcol;
2579 if (oap->op_type == OP_APPEND)
2580 firstline += bd.textlen;
2581 pre_textlen = (long)STRLEN(firstline);
2582 }
2583
2584 if (oap->op_type == OP_APPEND)
2585 {
2586 if (oap->block_mode
2587#ifdef FEAT_VIRTUALEDIT
2588 && curwin->w_cursor.coladd == 0
2589#endif
2590 )
2591 {
2592 /* Move the cursor to the character right of the block. */
2593 curwin->w_set_curswant = TRUE;
2594 while (*ml_get_cursor() != NUL
2595 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2596 ++curwin->w_cursor.col;
2597 if (bd.is_short && !bd.is_MAX)
2598 {
2599 /* First line was too short, make it longer and adjust the
2600 * values in "bd". */
2601 if (u_save_cursor() == FAIL)
2602 return;
2603 for (i = 0; i < bd.endspaces; ++i)
2604 ins_char(' ');
2605 bd.textlen += bd.endspaces;
2606 }
2607 }
2608 else
2609 {
2610 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002611 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /* Works just like an 'i'nsert on the next character. */
2614 if (!lineempty(curwin->w_cursor.lnum)
2615 && oap->start_vcol != oap->end_vcol)
2616 inc_cursor();
2617 }
2618 }
2619
2620 edit(NUL, FALSE, (linenr_T)count1);
2621
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002622 /* If user has moved off this line, we don't know what to do, so do
2623 * nothing.
2624 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2625 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return;
2627
2628 if (oap->block_mode)
2629 {
2630 struct block_def bd2;
2631
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002632 /* The user may have moved the cursor before inserting something, try
2633 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002634 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002635 {
2636 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002637 && oap->start.col
2638#ifdef FEAT_VIRTUALEDIT
2639 + oap->start.coladd
2640#endif
2641 != curbuf->b_op_start_orig.col
2642#ifdef FEAT_VIRTUALEDIT
2643 + curbuf->b_op_start_orig.coladd
2644#endif
2645 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002646 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002647 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002648 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2649 - oap->start_vcol;
2650 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2651 }
2652 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002653 && oap->end.col
2654#ifdef FEAT_VIRTUALEDIT
2655 + oap->end.coladd
2656#endif
2657 >= curbuf->b_op_start_orig.col
2658#ifdef FEAT_VIRTUALEDIT
2659 + curbuf->b_op_start_orig.coladd
2660#endif
2661 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002662 {
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002663 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002664 /* reset pre_textlen to the value of OP_INSERT */
2665 pre_textlen += bd.textlen;
2666 pre_textlen -= getviscol2(oap->start.col, oap->start.coladd)
2667 - oap->start_vcol;
2668 oap->start_vcol = getviscol2(oap->start.col, oap->start.coladd);
2669 oap->op_type = OP_INSERT;
2670 }
2671 }
2672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /*
2674 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002675 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 * Don't do this when "$" used, end-of-line will have changed.
2677 */
2678 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2679 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2680 {
2681 if (oap->op_type == OP_APPEND)
2682 {
2683 pre_textlen += bd2.textlen - bd.textlen;
2684 if (bd2.endspaces)
2685 --bd2.textlen;
2686 }
2687 bd.textcol = bd2.textcol;
2688 bd.textlen = bd2.textlen;
2689 }
2690
2691 /*
2692 * Subsequent calls to ml_get() flush the firstline data - take a
2693 * copy of the required string.
2694 */
2695 firstline = ml_get(oap->start.lnum) + bd.textcol;
2696 if (oap->op_type == OP_APPEND)
2697 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002698 if (pre_textlen >= 0
2699 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 ins_text = vim_strnsave(firstline, (int)ins_len);
2702 if (ins_text != NULL)
2703 {
2704 /* block handled here */
2705 if (u_save(oap->start.lnum,
2706 (linenr_T)(oap->end.lnum + 1)) == OK)
2707 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2708 &bd);
2709
2710 curwin->w_cursor.col = oap->start.col;
2711 check_cursor();
2712 vim_free(ins_text);
2713 }
2714 }
2715 }
2716}
2717#endif
2718
2719/*
2720 * op_change - handle a change operation
2721 *
2722 * return TRUE if edit() returns because of a CTRL-O command
2723 */
2724 int
2725op_change(oap)
2726 oparg_T *oap;
2727{
2728 colnr_T l;
2729 int retval;
2730#ifdef FEAT_VISUALEXTRA
2731 long offset;
2732 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002733 long ins_len;
2734 long pre_textlen = 0;
2735 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 char_u *firstline;
2737 char_u *ins_text, *newp, *oldp;
2738 struct block_def bd;
2739#endif
2740
2741 l = oap->start.col;
2742 if (oap->motion_type == MLINE)
2743 {
2744 l = 0;
2745#ifdef FEAT_SMARTINDENT
2746 if (!p_paste && curbuf->b_p_si
2747# ifdef FEAT_CINDENT
2748 && !curbuf->b_p_cin
2749# endif
2750 )
2751 can_si = TRUE; /* It's like opening a new line, do si */
2752#endif
2753 }
2754
2755 /* First delete the text in the region. In an empty buffer only need to
2756 * save for undo */
2757 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2758 {
2759 if (u_save_cursor() == FAIL)
2760 return FALSE;
2761 }
2762 else if (op_delete(oap) == FAIL)
2763 return FALSE;
2764
2765 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2766 && !virtual_op)
2767 inc_cursor();
2768
2769#ifdef FEAT_VISUALEXTRA
2770 /* check for still on same line (<CR> in inserted text meaningless) */
2771 /* skip blank lines too */
2772 if (oap->block_mode)
2773 {
2774# ifdef FEAT_VIRTUALEDIT
2775 /* Add spaces before getting the current line length. */
2776 if (virtual_op && (curwin->w_cursor.coladd > 0
2777 || gchar_cursor() == NUL))
2778 coladvance_force(getviscol());
2779# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002780 firstline = ml_get(oap->start.lnum);
2781 pre_textlen = (long)STRLEN(firstline);
2782 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 bd.textcol = curwin->w_cursor.col;
2784 }
2785#endif
2786
2787#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2788 if (oap->motion_type == MLINE)
2789 fix_indent();
2790#endif
2791
2792 retval = edit(NUL, FALSE, (linenr_T)1);
2793
2794#ifdef FEAT_VISUALEXTRA
2795 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002796 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002798 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002800 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002802 /* Auto-indenting may have changed the indent. If the cursor was past
2803 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002805 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002807 long new_indent = (long)(skipwhite(firstline) - firstline);
2808
2809 pre_textlen += new_indent - pre_indent;
2810 bd.textcol += new_indent - pre_indent;
2811 }
2812
2813 ins_len = (long)STRLEN(firstline) - pre_textlen;
2814 if (ins_len > 0)
2815 {
2816 /* Subsequent calls to ml_get() flush the firstline data - take a
2817 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2819 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002820 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2822 linenr++)
2823 {
2824 block_prep(oap, &bd, linenr, TRUE);
2825 if (!bd.is_short || virtual_op)
2826 {
2827# ifdef FEAT_VIRTUALEDIT
2828 pos_T vpos;
2829
2830 /* If the block starts in virtual space, count the
2831 * initial coladd offset as part of "startspaces" */
2832 if (bd.is_short)
2833 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002834 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837 else
2838 vpos.coladd = 0;
2839# endif
2840 oldp = ml_get(linenr);
2841 newp = alloc_check((unsigned)(STRLEN(oldp)
2842# ifdef FEAT_VIRTUALEDIT
2843 + vpos.coladd
2844# endif
2845 + ins_len + 1));
2846 if (newp == NULL)
2847 continue;
2848 /* copy up to block start */
2849 mch_memmove(newp, oldp, (size_t)bd.textcol);
2850 offset = bd.textcol;
2851# ifdef FEAT_VIRTUALEDIT
2852 copy_spaces(newp + offset, (size_t)vpos.coladd);
2853 offset += vpos.coladd;
2854# endif
2855 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2856 offset += ins_len;
2857 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002858 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 ml_replace(linenr, newp, FALSE);
2860 }
2861 }
2862 check_cursor();
2863
2864 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2865 }
2866 vim_free(ins_text);
2867 }
2868 }
2869#endif
2870
2871 return retval;
2872}
2873
2874/*
2875 * set all the yank registers to empty (called from main())
2876 */
2877 void
2878init_yank()
2879{
2880 int i;
2881
2882 for (i = 0; i < NUM_REGISTERS; ++i)
2883 y_regs[i].y_array = NULL;
2884}
2885
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002886#if defined(EXITFREE) || defined(PROTO)
2887 void
2888clear_registers()
2889{
2890 int i;
2891
2892 for (i = 0; i < NUM_REGISTERS; ++i)
2893 {
2894 y_current = &y_regs[i];
2895 if (y_current->y_array != NULL)
2896 free_yank_all();
2897 }
2898}
2899#endif
2900
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901/*
2902 * Free "n" lines from the current yank register.
2903 * Called for normal freeing and in case of error.
2904 */
2905 static void
2906free_yank(n)
2907 long n;
2908{
2909 if (y_current->y_array != NULL)
2910 {
2911 long i;
2912
2913 for (i = n; --i >= 0; )
2914 {
2915#ifdef AMIGA /* only for very slow machines */
2916 if ((i & 1023) == 1023) /* this may take a while */
2917 {
2918 /*
2919 * This message should never cause a hit-return message.
2920 * Overwrite this message with any next message.
2921 */
2922 ++no_wait_return;
2923 smsg((char_u *)_("freeing %ld lines"), i + 1);
2924 --no_wait_return;
2925 msg_didout = FALSE;
2926 msg_col = 0;
2927 }
2928#endif
2929 vim_free(y_current->y_array[i]);
2930 }
2931 vim_free(y_current->y_array);
2932 y_current->y_array = NULL;
2933#ifdef AMIGA
2934 if (n >= 1000)
2935 MSG("");
2936#endif
2937 }
2938}
2939
2940 static void
2941free_yank_all()
2942{
2943 free_yank(y_current->y_size);
2944}
2945
2946/*
2947 * Yank the text between "oap->start" and "oap->end" into a yank register.
2948 * If we are to append (uppercase register), we first yank into a new yank
2949 * register and then concatenate the old and the new one (so we keep the old
2950 * one in case of out-of-memory).
2951 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002952 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954 int
2955op_yank(oap, deleting, mess)
2956 oparg_T *oap;
2957 int deleting;
2958 int mess;
2959{
2960 long y_idx; /* index in y_array[] */
2961 struct yankreg *curr; /* copy of y_current */
2962 struct yankreg newreg; /* new yank register when appending */
2963 char_u **new_ptr;
2964 linenr_T lnum; /* current line number */
2965 long j;
2966 int yanktype = oap->motion_type;
2967 long yanklines = oap->line_count;
2968 linenr_T yankendlnum = oap->end.lnum;
2969 char_u *p;
2970 char_u *pnew;
2971 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002972#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002973 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /* check for read-only register */
2977 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2978 {
2979 beep_flush();
2980 return FAIL;
2981 }
2982 if (oap->regname == '_') /* black hole: nothing to do */
2983 return OK;
2984
2985#ifdef FEAT_CLIPBOARD
2986 if (!clip_star.available && oap->regname == '*')
2987 oap->regname = 0;
2988 else if (!clip_plus.available && oap->regname == '+')
2989 oap->regname = 0;
2990#endif
2991
2992 if (!deleting) /* op_delete() already set y_current */
2993 get_yank_register(oap->regname, TRUE);
2994
2995 curr = y_current;
2996 /* append to existing contents */
2997 if (y_append && y_current->y_array != NULL)
2998 y_current = &newreg;
2999 else
3000 free_yank_all(); /* free previously yanked lines */
3001
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003002 /*
3003 * If the cursor was in column 1 before and after the movement, and the
3004 * operator is not inclusive, the yank is always linewise.
3005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if ( oap->motion_type == MCHAR
3007 && oap->start.col == 0
3008 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003010 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 && oap->end.col == 0
3012 && yanklines > 1)
3013 {
3014 yanktype = MLINE;
3015 --yankendlnum;
3016 --yanklines;
3017 }
3018
3019 y_current->y_size = yanklines;
3020 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3023 yanklines), TRUE);
3024
3025 if (y_current->y_array == NULL)
3026 {
3027 y_current = curr;
3028 return FAIL;
3029 }
3030
3031 y_idx = 0;
3032 lnum = oap->start.lnum;
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (oap->block_mode)
3035 {
3036 /* Visual block mode */
3037 y_current->y_type = MBLOCK; /* set the yank register type */
3038 y_current->y_width = oap->end_vcol - oap->start_vcol;
3039
3040 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3041 y_current->y_width--;
3042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3045 {
3046 switch (y_current->y_type)
3047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 case MBLOCK:
3049 block_prep(oap, &bd, lnum, FALSE);
3050 if (yank_copy_line(&bd, y_idx) == FAIL)
3051 goto fail;
3052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 case MLINE:
3055 if ((y_current->y_array[y_idx] =
3056 vim_strsave(ml_get(lnum))) == NULL)
3057 goto fail;
3058 break;
3059
3060 case MCHAR:
3061 {
3062 colnr_T startcol = 0, endcol = MAXCOL;
3063#ifdef FEAT_VIRTUALEDIT
3064 int is_oneChar = FALSE;
3065 colnr_T cs, ce;
3066#endif
3067 p = ml_get(lnum);
3068 bd.startspaces = 0;
3069 bd.endspaces = 0;
3070
3071 if (lnum == oap->start.lnum)
3072 {
3073 startcol = oap->start.col;
3074#ifdef FEAT_VIRTUALEDIT
3075 if (virtual_op)
3076 {
3077 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3078 if (ce != cs && oap->start.coladd > 0)
3079 {
3080 /* Part of a tab selected -- but don't
3081 * double-count it. */
3082 bd.startspaces = (ce - cs + 1)
3083 - oap->start.coladd;
3084 startcol++;
3085 }
3086 }
3087#endif
3088 }
3089
3090 if (lnum == oap->end.lnum)
3091 {
3092 endcol = oap->end.col;
3093#ifdef FEAT_VIRTUALEDIT
3094 if (virtual_op)
3095 {
3096 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3097 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3098# ifdef FEAT_MBYTE
3099 /* Don't add space for double-wide
3100 * char; endcol will be on last byte
3101 * of multi-byte char. */
3102 && (*mb_head_off)(p, p + endcol) == 0
3103# endif
3104 ))
3105 {
3106 if (oap->start.lnum == oap->end.lnum
3107 && oap->start.col == oap->end.col)
3108 {
3109 /* Special case: inside a single char */
3110 is_oneChar = TRUE;
3111 bd.startspaces = oap->end.coladd
3112 - oap->start.coladd + oap->inclusive;
3113 endcol = startcol;
3114 }
3115 else
3116 {
3117 bd.endspaces = oap->end.coladd
3118 + oap->inclusive;
3119 endcol -= oap->inclusive;
3120 }
3121 }
3122 }
3123#endif
3124 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003125 if (endcol == MAXCOL)
3126 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (startcol > endcol
3128#ifdef FEAT_VIRTUALEDIT
3129 || is_oneChar
3130#endif
3131 )
3132 bd.textlen = 0;
3133 else
3134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 bd.textlen = endcol - startcol + oap->inclusive;
3136 }
3137 bd.textstart = p + startcol;
3138 if (yank_copy_line(&bd, y_idx) == FAIL)
3139 goto fail;
3140 break;
3141 }
3142 /* NOTREACHED */
3143 }
3144 }
3145
3146 if (curr != y_current) /* append the new block to the old block */
3147 {
3148 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3149 (curr->y_size + y_current->y_size)), TRUE);
3150 if (new_ptr == NULL)
3151 goto fail;
3152 for (j = 0; j < curr->y_size; ++j)
3153 new_ptr[j] = curr->y_array[j];
3154 vim_free(curr->y_array);
3155 curr->y_array = new_ptr;
3156
3157 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3158 curr->y_type = MLINE;
3159
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003160 /* Concatenate the last line of the old block with the first line of
3161 * the new block, unless being Vi compatible. */
3162 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3165 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3166 if (pnew == NULL)
3167 {
3168 y_idx = y_current->y_size - 1;
3169 goto fail;
3170 }
3171 STRCPY(pnew, curr->y_array[--j]);
3172 STRCAT(pnew, y_current->y_array[0]);
3173 vim_free(curr->y_array[j]);
3174 vim_free(y_current->y_array[0]);
3175 curr->y_array[j++] = pnew;
3176 y_idx = 1;
3177 }
3178 else
3179 y_idx = 0;
3180 while (y_idx < y_current->y_size)
3181 curr->y_array[j++] = y_current->y_array[y_idx++];
3182 curr->y_size = j;
3183 vim_free(y_current->y_array);
3184 y_current = curr;
3185 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003186 if (curwin->w_p_rnu)
3187 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (mess) /* Display message about yank? */
3189 {
3190 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 && yanklines == 1)
3193 yanklines = 0;
3194 /* Some versions of Vi use ">=" here, some don't... */
3195 if (yanklines > p_report)
3196 {
3197 /* redisplay now, so message is not deleted */
3198 update_topline_redraw();
3199 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003200 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003201 if (oap->block_mode)
3202 MSG(_("block of 1 line yanked"));
3203 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003204 MSG(_("1 line yanked"));
3205 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003206 else if (oap->block_mode)
3207 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209 smsg((char_u *)_("%ld lines yanked"), yanklines);
3210 }
3211 }
3212
3213 /*
3214 * Set "'[" and "']" marks.
3215 */
3216 curbuf->b_op_start = oap->start;
3217 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003218 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003219 {
3220 curbuf->b_op_start.col = 0;
3221 curbuf->b_op_end.col = MAXCOL;
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224#ifdef FEAT_CLIPBOARD
3225 /*
3226 * If we were yanking to the '*' register, send result to clipboard.
3227 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3228 * to the '*' register.
3229 */
3230 if (clip_star.available
3231 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003232 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003233 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 if (curr != &(y_regs[STAR_REGISTER]))
3236 /* Copy the text from register 0 to the clipboard register. */
3237 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3238
3239 clip_own_selection(&clip_star);
3240 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003241# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003242 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 }
3245
3246# ifdef FEAT_X11
3247 /*
3248 * If we were yanking to the '+' register, send result to selection.
3249 * Also copy to the '*' register, in case auto-select is off.
3250 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003251 if (clip_plus.available
3252 && (curr == &(y_regs[PLUS_REGISTER])
3253 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003254 && ((clip_unnamed | clip_unnamed_saved) &
3255 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003257 if (curr != &(y_regs[PLUS_REGISTER]))
3258 /* Copy the text from register 0 to the clipboard register. */
3259 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 clip_own_selection(&clip_plus);
3262 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003263 if (!clip_isautosel_star() && !did_star
3264 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3267 clip_own_selection(&clip_star);
3268 clip_gen_set_selection(&clip_star);
3269 }
3270 }
3271# endif
3272#endif
3273
3274 return OK;
3275
3276fail: /* free the allocated lines */
3277 free_yank(y_idx + 1);
3278 y_current = curr;
3279 return FAIL;
3280}
3281
3282 static int
3283yank_copy_line(bd, y_idx)
3284 struct block_def *bd;
3285 long y_idx;
3286{
3287 char_u *pnew;
3288
3289 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3290 == NULL)
3291 return FAIL;
3292 y_current->y_array[y_idx] = pnew;
3293 copy_spaces(pnew, (size_t)bd->startspaces);
3294 pnew += bd->startspaces;
3295 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3296 pnew += bd->textlen;
3297 copy_spaces(pnew, (size_t)bd->endspaces);
3298 pnew += bd->endspaces;
3299 *pnew = NUL;
3300 return OK;
3301}
3302
3303#ifdef FEAT_CLIPBOARD
3304/*
3305 * Make a copy of the y_current register to register "reg".
3306 */
3307 static void
3308copy_yank_reg(reg)
3309 struct yankreg *reg;
3310{
3311 struct yankreg *curr = y_current;
3312 long j;
3313
3314 y_current = reg;
3315 free_yank_all();
3316 *y_current = *curr;
3317 y_current->y_array = (char_u **)lalloc_clear(
3318 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3319 if (y_current->y_array == NULL)
3320 y_current->y_size = 0;
3321 else
3322 for (j = 0; j < y_current->y_size; ++j)
3323 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3324 {
3325 free_yank(j);
3326 y_current->y_size = 0;
3327 break;
3328 }
3329 y_current = curr;
3330}
3331#endif
3332
3333/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003334 * Put contents of register "regname" into the text.
3335 * Caller must check "regname" to be valid!
3336 * "flags": PUT_FIXINDENT make indent look nice
3337 * PUT_CURSEND leave cursor after end of new text
3338 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
3340 void
3341do_put(regname, dir, count, flags)
3342 int regname;
3343 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3344 long count;
3345 int flags;
3346{
3347 char_u *ptr;
3348 char_u *newp, *oldp;
3349 int yanklen;
3350 int totlen = 0; /* init for gcc */
3351 linenr_T lnum;
3352 colnr_T col;
3353 long i; /* index in y_array[] */
3354 int y_type;
3355 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int oldlen;
3357 long y_width = 0;
3358 colnr_T vcol;
3359 int delcount;
3360 int incr = 0;
3361 long j;
3362 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 char_u **y_array = NULL;
3364 long nr_lines = 0;
3365 pos_T new_cursor;
3366 int indent;
3367 int orig_indent = 0; /* init for gcc */
3368 int indent_diff = 0; /* init for gcc */
3369 int first_indent = TRUE;
3370 int lendiff = 0;
3371 pos_T old_pos;
3372 char_u *insert_string = NULL;
3373 int allocated = FALSE;
3374 long cnt;
3375
3376#ifdef FEAT_CLIPBOARD
3377 /* Adjust register name for "unnamed" in 'clipboard'. */
3378 adjust_clip_reg(&regname);
3379 (void)may_get_selection(regname);
3380#endif
3381
3382 if (flags & PUT_FIXINDENT)
3383 orig_indent = get_indent();
3384
3385 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3386 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3387
3388 /*
3389 * Using inserted text works differently, because the register includes
3390 * special characters (newlines, etc.).
3391 */
3392 if (regname == '.')
3393 {
3394 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3395 (count == -1 ? 'O' : 'i')), count, FALSE);
3396 /* Putting the text is done later, so can't really move the cursor to
3397 * the next character. Use "l" to simulate it. */
3398 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3399 stuffcharReadbuff('l');
3400 return;
3401 }
3402
3403 /*
3404 * For special registers '%' (file name), '#' (alternate file name) and
3405 * ':' (last command line), etc. we have to create a fake yank register.
3406 */
3407 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3408 {
3409 if (insert_string == NULL)
3410 return;
3411 }
3412
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003413#ifdef FEAT_AUTOCMD
3414 /* Autocommands may be executed when saving lines for undo, which may make
3415 * y_array invalid. Start undo now to avoid that. */
3416 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3417#endif
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (insert_string != NULL)
3420 {
3421 y_type = MCHAR;
3422#ifdef FEAT_EVAL
3423 if (regname == '=')
3424 {
3425 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003426 * characters.
3427 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 for (;;)
3429 {
3430 y_size = 0;
3431 ptr = insert_string;
3432 while (ptr != NULL)
3433 {
3434 if (y_array != NULL)
3435 y_array[y_size] = ptr;
3436 ++y_size;
3437 ptr = vim_strchr(ptr, '\n');
3438 if (ptr != NULL)
3439 {
3440 if (y_array != NULL)
3441 *ptr = NUL;
3442 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003443 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (*ptr == NUL)
3445 {
3446 y_type = MLINE;
3447 break;
3448 }
3449 }
3450 }
3451 if (y_array != NULL)
3452 break;
3453 y_array = (char_u **)alloc((unsigned)
3454 (y_size * sizeof(char_u *)));
3455 if (y_array == NULL)
3456 goto end;
3457 }
3458 }
3459 else
3460#endif
3461 {
3462 y_size = 1; /* use fake one-line yank register */
3463 y_array = &insert_string;
3464 }
3465 }
3466 else
3467 {
3468 get_yank_register(regname, FALSE);
3469
3470 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 y_size = y_current->y_size;
3473 y_array = y_current->y_array;
3474 }
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (y_type == MLINE)
3477 {
3478 if (flags & PUT_LINE_SPLIT)
3479 {
3480 /* "p" or "P" in Visual mode: split the lines to put the text in
3481 * between. */
3482 if (u_save_cursor() == FAIL)
3483 goto end;
3484 ptr = vim_strsave(ml_get_cursor());
3485 if (ptr == NULL)
3486 goto end;
3487 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3488 vim_free(ptr);
3489
3490 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3491 if (ptr == NULL)
3492 goto end;
3493 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3494 ++nr_lines;
3495 dir = FORWARD;
3496 }
3497 if (flags & PUT_LINE_FORWARD)
3498 {
3499 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003500 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 dir = FORWARD;
3502 }
3503 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3504 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3508 y_type = MLINE;
3509
3510 if (y_size == 0 || y_array == NULL)
3511 {
3512 EMSG2(_("E353: Nothing in register %s"),
3513 regname == 0 ? (char_u *)"\"" : transchar(regname));
3514 goto end;
3515 }
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (y_type == MBLOCK)
3518 {
3519 lnum = curwin->w_cursor.lnum + y_size + 1;
3520 if (lnum > curbuf->b_ml.ml_line_count)
3521 lnum = curbuf->b_ml.ml_line_count + 1;
3522 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3523 goto end;
3524 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003525 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 lnum = curwin->w_cursor.lnum;
3528#ifdef FEAT_FOLDING
3529 /* Correct line number for closed fold. Don't move the cursor yet,
3530 * u_save() uses it. */
3531 if (dir == BACKWARD)
3532 (void)hasFolding(lnum, &lnum, NULL);
3533 else
3534 (void)hasFolding(lnum, NULL, &lnum);
3535#endif
3536 if (dir == FORWARD)
3537 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003538 /* In an empty buffer the empty line is going to be replaced, include
3539 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003540 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 goto end;
3542#ifdef FEAT_FOLDING
3543 if (dir == FORWARD)
3544 curwin->w_cursor.lnum = lnum - 1;
3545 else
3546 curwin->w_cursor.lnum = lnum;
3547 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3548#endif
3549 }
3550 else if (u_save_cursor() == FAIL)
3551 goto end;
3552
3553 yanklen = (int)STRLEN(y_array[0]);
3554
3555#ifdef FEAT_VIRTUALEDIT
3556 if (ve_flags == VE_ALL && y_type == MCHAR)
3557 {
3558 if (gchar_cursor() == TAB)
3559 {
3560 /* Don't need to insert spaces when "p" on the last position of a
3561 * tab or "P" on the first position. */
3562 if (dir == FORWARD
3563 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3564 : curwin->w_cursor.coladd > 0)
3565 coladvance_force(getviscol());
3566 else
3567 curwin->w_cursor.coladd = 0;
3568 }
3569 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3570 coladvance_force(getviscol() + (dir == FORWARD));
3571 }
3572#endif
3573
3574 lnum = curwin->w_cursor.lnum;
3575 col = curwin->w_cursor.col;
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /*
3578 * Block mode
3579 */
3580 if (y_type == MBLOCK)
3581 {
3582 char c = gchar_cursor();
3583 colnr_T endcol2 = 0;
3584
3585 if (dir == FORWARD && c != NUL)
3586 {
3587#ifdef FEAT_VIRTUALEDIT
3588 if (ve_flags == VE_ALL)
3589 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3590 else
3591#endif
3592 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3593
3594#ifdef FEAT_MBYTE
3595 if (has_mbyte)
3596 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003597 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 else
3599#endif
3600#ifdef FEAT_VIRTUALEDIT
3601 if (c != TAB || ve_flags != VE_ALL)
3602#endif
3603 ++curwin->w_cursor.col;
3604 ++col;
3605 }
3606 else
3607 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3608
3609#ifdef FEAT_VIRTUALEDIT
3610 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003611 if (ve_flags == VE_ALL
3612 && (curwin->w_cursor.coladd > 0
3613 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 if (dir == FORWARD && c == NUL)
3616 ++col;
3617 if (dir != FORWARD && c != NUL)
3618 ++curwin->w_cursor.col;
3619 if (c == TAB)
3620 {
3621 if (dir == BACKWARD && curwin->w_cursor.col)
3622 curwin->w_cursor.col--;
3623 if (dir == FORWARD && col - 1 == endcol2)
3624 curwin->w_cursor.col++;
3625 }
3626 }
3627 curwin->w_cursor.coladd = 0;
3628#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003629 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 for (i = 0; i < y_size; ++i)
3631 {
3632 int spaces;
3633 char shortline;
3634
3635 bd.startspaces = 0;
3636 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 vcol = 0;
3638 delcount = 0;
3639
3640 /* add a new line */
3641 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3642 {
3643 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3644 (colnr_T)1, FALSE) == FAIL)
3645 break;
3646 ++nr_lines;
3647 }
3648 /* get the old line and advance to the position to insert at */
3649 oldp = ml_get_curline();
3650 oldlen = (int)STRLEN(oldp);
3651 for (ptr = oldp; vcol < col && *ptr; )
3652 {
3653 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003654 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 vcol += incr;
3656 }
3657 bd.textcol = (colnr_T)(ptr - oldp);
3658
3659 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3660
3661 if (vcol < col) /* line too short, padd with spaces */
3662 bd.startspaces = col - vcol;
3663 else if (vcol > col)
3664 {
3665 bd.endspaces = vcol - col;
3666 bd.startspaces = incr - bd.endspaces;
3667 --bd.textcol;
3668 delcount = 1;
3669#ifdef FEAT_MBYTE
3670 if (has_mbyte)
3671 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3672#endif
3673 if (oldp[bd.textcol] != TAB)
3674 {
3675 /* Only a Tab can be split into spaces. Other
3676 * characters will have to be moved to after the
3677 * block, causing misalignment. */
3678 delcount = 0;
3679 bd.endspaces = 0;
3680 }
3681 }
3682
3683 yanklen = (int)STRLEN(y_array[i]);
3684
3685 /* calculate number of spaces required to fill right side of block*/
3686 spaces = y_width + 1;
3687 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003688 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (spaces < 0)
3690 spaces = 0;
3691
3692 /* insert the new text */
3693 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3694 newp = alloc_check((unsigned)totlen + oldlen + 1);
3695 if (newp == NULL)
3696 break;
3697 /* copy part up to cursor to new line */
3698 ptr = newp;
3699 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3700 ptr += bd.textcol;
3701 /* may insert some spaces before the new text */
3702 copy_spaces(ptr, (size_t)bd.startspaces);
3703 ptr += bd.startspaces;
3704 /* insert the new text */
3705 for (j = 0; j < count; ++j)
3706 {
3707 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3708 ptr += yanklen;
3709
3710 /* insert block's trailing spaces only if there's text behind */
3711 if ((j < count - 1 || !shortline) && spaces)
3712 {
3713 copy_spaces(ptr, (size_t)spaces);
3714 ptr += spaces;
3715 }
3716 }
3717 /* may insert some spaces after the new text */
3718 copy_spaces(ptr, (size_t)bd.endspaces);
3719 ptr += bd.endspaces;
3720 /* move the text after the cursor to the end of the line. */
3721 mch_memmove(ptr, oldp + bd.textcol + delcount,
3722 (size_t)(oldlen - bd.textcol - delcount + 1));
3723 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3724
3725 ++curwin->w_cursor.lnum;
3726 if (i == 0)
3727 curwin->w_cursor.col += bd.startspaces;
3728 }
3729
3730 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3731
3732 /* Set '[ mark. */
3733 curbuf->b_op_start = curwin->w_cursor;
3734 curbuf->b_op_start.lnum = lnum;
3735
3736 /* adjust '] mark */
3737 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3738 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003739# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (flags & PUT_CURSEND)
3743 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003744 colnr_T len;
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 curwin->w_cursor = curbuf->b_op_end;
3747 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003748
3749 /* in Insert mode we might be after the NUL, correct for that */
3750 len = (colnr_T)STRLEN(ml_get_curline());
3751 if (curwin->w_cursor.col > len)
3752 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 else
3755 curwin->w_cursor.lnum = lnum;
3756 }
3757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759 /*
3760 * Character or Line mode
3761 */
3762 if (y_type == MCHAR)
3763 {
3764 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3765 * char */
3766 if (dir == FORWARD && gchar_cursor() != NUL)
3767 {
3768#ifdef FEAT_MBYTE
3769 if (has_mbyte)
3770 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /* put it on the next of the multi-byte character. */
3774 col += bytelen;
3775 if (yanklen)
3776 {
3777 curwin->w_cursor.col += bytelen;
3778 curbuf->b_op_end.col += bytelen;
3779 }
3780 }
3781 else
3782#endif
3783 {
3784 ++col;
3785 if (yanklen)
3786 {
3787 ++curwin->w_cursor.col;
3788 ++curbuf->b_op_end.col;
3789 }
3790 }
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 curbuf->b_op_start = curwin->w_cursor;
3793 }
3794 /*
3795 * Line mode: BACKWARD is the same as FORWARD on the previous line
3796 */
3797 else if (dir == BACKWARD)
3798 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003799 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 /*
3802 * simple case: insert into current line
3803 */
3804 if (y_type == MCHAR && y_size == 1)
3805 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003806 do {
3807 totlen = count * yanklen;
3808 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003810 oldp = ml_get(lnum);
3811 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3812 if (newp == NULL)
3813 goto end; /* alloc() gave an error message */
3814 mch_memmove(newp, oldp, (size_t)col);
3815 ptr = newp + col;
3816 for (i = 0; i < count; ++i)
3817 {
3818 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3819 ptr += yanklen;
3820 }
3821 STRMOVE(ptr, oldp + col);
3822 ml_replace(lnum, newp, FALSE);
3823 /* Place cursor on last putted char. */
3824 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003825 {
3826 /* make sure curwin->w_virtcol is updated */
3827 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003828 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003831 if (VIsual_active)
3832 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003833 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003834
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003835 if (VIsual_active) /* reset lnum to the last visual line */
3836 lnum--;
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 curbuf->b_op_end = curwin->w_cursor;
3839 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3840 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3841 ++curwin->w_cursor.col;
3842 changed_bytes(lnum, col);
3843 }
3844 else
3845 {
3846 /*
3847 * Insert at least one line. When y_type is MCHAR, break the first
3848 * line in two.
3849 */
3850 for (cnt = 1; cnt <= count; ++cnt)
3851 {
3852 i = 0;
3853 if (y_type == MCHAR)
3854 {
3855 /*
3856 * Split the current line in two at the insert position.
3857 * First insert y_array[size - 1] in front of second line.
3858 * Then append y_array[0] to first line.
3859 */
3860 lnum = new_cursor.lnum;
3861 ptr = ml_get(lnum) + col;
3862 totlen = (int)STRLEN(y_array[y_size - 1]);
3863 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3864 if (newp == NULL)
3865 goto error;
3866 STRCPY(newp, y_array[y_size - 1]);
3867 STRCAT(newp, ptr);
3868 /* insert second line */
3869 ml_append(lnum, newp, (colnr_T)0, FALSE);
3870 vim_free(newp);
3871
3872 oldp = ml_get(lnum);
3873 newp = alloc_check((unsigned)(col + yanklen + 1));
3874 if (newp == NULL)
3875 goto error;
3876 /* copy first part of line */
3877 mch_memmove(newp, oldp, (size_t)col);
3878 /* append to first line */
3879 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3880 ml_replace(lnum, newp, FALSE);
3881
3882 curwin->w_cursor.lnum = lnum;
3883 i = 1;
3884 }
3885
3886 for (; i < y_size; ++i)
3887 {
3888 if ((y_type != MCHAR || i < y_size - 1)
3889 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3890 == FAIL)
3891 goto error;
3892 lnum++;
3893 ++nr_lines;
3894 if (flags & PUT_FIXINDENT)
3895 {
3896 old_pos = curwin->w_cursor;
3897 curwin->w_cursor.lnum = lnum;
3898 ptr = ml_get(lnum);
3899 if (cnt == count && i == y_size - 1)
3900 lendiff = (int)STRLEN(ptr);
3901#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3902 if (*ptr == '#' && preprocs_left())
3903 indent = 0; /* Leave # lines at start */
3904 else
3905#endif
3906 if (*ptr == NUL)
3907 indent = 0; /* Ignore empty lines */
3908 else if (first_indent)
3909 {
3910 indent_diff = orig_indent - get_indent();
3911 indent = orig_indent;
3912 first_indent = FALSE;
3913 }
3914 else if ((indent = get_indent() + indent_diff) < 0)
3915 indent = 0;
3916 (void)set_indent(indent, 0);
3917 curwin->w_cursor = old_pos;
3918 /* remember how many chars were removed */
3919 if (cnt == count && i == y_size - 1)
3920 lendiff -= (int)STRLEN(ml_get(lnum));
3921 }
3922 }
3923 }
3924
3925error:
3926 /* Adjust marks. */
3927 if (y_type == MLINE)
3928 {
3929 curbuf->b_op_start.col = 0;
3930 if (dir == FORWARD)
3931 curbuf->b_op_start.lnum++;
3932 }
3933 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3934 (linenr_T)MAXLNUM, nr_lines, 0L);
3935
3936 /* note changed text for displaying and folding */
3937 if (y_type == MCHAR)
3938 changed_lines(curwin->w_cursor.lnum, col,
3939 curwin->w_cursor.lnum + 1, nr_lines);
3940 else
3941 changed_lines(curbuf->b_op_start.lnum, 0,
3942 curbuf->b_op_start.lnum, nr_lines);
3943
3944 /* put '] mark at last inserted character */
3945 curbuf->b_op_end.lnum = lnum;
3946 /* correct length for change in indent */
3947 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3948 if (col > 1)
3949 curbuf->b_op_end.col = col - 1;
3950 else
3951 curbuf->b_op_end.col = 0;
3952
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003953 if (flags & PUT_CURSLINE)
3954 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003955 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003956 curwin->w_cursor.lnum = lnum;
3957 beginline(BL_WHITE | BL_FIX);
3958 }
3959 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
3961 /* put cursor after inserted text */
3962 if (y_type == MLINE)
3963 {
3964 if (lnum >= curbuf->b_ml.ml_line_count)
3965 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3966 else
3967 curwin->w_cursor.lnum = lnum + 1;
3968 curwin->w_cursor.col = 0;
3969 }
3970 else
3971 {
3972 curwin->w_cursor.lnum = lnum;
3973 curwin->w_cursor.col = col;
3974 }
3975 }
3976 else if (y_type == MLINE)
3977 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003978 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 curwin->w_cursor.col = 0;
3980 if (dir == FORWARD)
3981 ++curwin->w_cursor.lnum;
3982 beginline(BL_WHITE | BL_FIX);
3983 }
3984 else /* put cursor on first inserted character */
3985 curwin->w_cursor = new_cursor;
3986 }
3987 }
3988
3989 msgmore(nr_lines);
3990 curwin->w_set_curswant = TRUE;
3991
3992end:
3993 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003995 if (regname == '=')
3996 vim_free(y_array);
3997
Bram Moolenaar033d8882013-09-25 23:24:57 +02003998 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003999
Bram Moolenaar677ee682005-01-27 14:41:15 +00004000 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004001 adjust_cursor_eol();
4002}
4003
4004/*
4005 * When the cursor is on the NUL past the end of the line and it should not be
4006 * there move it left.
4007 */
4008 void
4009adjust_cursor_eol()
4010{
4011 if (curwin->w_cursor.col > 0
4012 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004013#ifdef FEAT_VIRTUALEDIT
4014 && (ve_flags & VE_ONEMORE) == 0
4015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 && !(restart_edit || (State & INSERT)))
4017 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004018 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004019 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#ifdef FEAT_VIRTUALEDIT
4022 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004023 {
4024 colnr_T scol, ecol;
4025
4026 /* Coladd is set to the width of the last character. */
4027 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4028 curwin->w_cursor.coladd = ecol - scol + 1;
4029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#endif
4031 }
4032}
4033
4034#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4035/*
4036 * Return TRUE if lines starting with '#' should be left aligned.
4037 */
4038 int
4039preprocs_left()
4040{
4041 return
4042# ifdef FEAT_SMARTINDENT
4043# ifdef FEAT_CINDENT
4044 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4045# else
4046 curbuf->b_p_si
4047# endif
4048# endif
4049# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004050 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4051 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# endif
4053 ;
4054}
4055#endif
4056
4057/* Return the character name of the register with the given number */
4058 int
4059get_register_name(num)
4060 int num;
4061{
4062 if (num == -1)
4063 return '"';
4064 else if (num < 10)
4065 return num + '0';
4066 else if (num == DELETION_REGISTER)
4067 return '-';
4068#ifdef FEAT_CLIPBOARD
4069 else if (num == STAR_REGISTER)
4070 return '*';
4071 else if (num == PLUS_REGISTER)
4072 return '+';
4073#endif
4074 else
4075 {
4076#ifdef EBCDIC
4077 int i;
4078
4079 /* EBCDIC is really braindead ... */
4080 i = 'a' + (num - 10);
4081 if (i > 'i')
4082 i += 7;
4083 if (i > 'r')
4084 i += 8;
4085 return i;
4086#else
4087 return num + 'a' - 10;
4088#endif
4089 }
4090}
4091
4092/*
4093 * ":dis" and ":registers": Display the contents of the yank registers.
4094 */
4095 void
4096ex_display(eap)
4097 exarg_T *eap;
4098{
4099 int i, n;
4100 long j;
4101 char_u *p;
4102 struct yankreg *yb;
4103 int name;
4104 int attr;
4105 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004106#ifdef FEAT_MBYTE
4107 int clen;
4108#else
4109# define clen 1
4110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
4112 if (arg != NULL && *arg == NUL)
4113 arg = NULL;
4114 attr = hl_attr(HLF_8);
4115
4116 /* Highlight title */
4117 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4118 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4119 {
4120 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004121 if (arg != NULL && vim_strchr(arg, name) == NULL
4122#ifdef ONE_CLIPBOARD
4123 /* Star register and plus register contain the same thing. */
4124 && (name != '*' || vim_strchr(arg, '+') == NULL)
4125#endif
4126 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 continue; /* did not ask for this register */
4128
4129#ifdef FEAT_CLIPBOARD
4130 /* Adjust register name for "unnamed" in 'clipboard'.
4131 * When it's a clipboard register, fill it with the current contents
4132 * of the clipboard. */
4133 adjust_clip_reg(&name);
4134 (void)may_get_selection(name);
4135#endif
4136
4137 if (i == -1)
4138 {
4139 if (y_previous != NULL)
4140 yb = y_previous;
4141 else
4142 yb = &(y_regs[0]);
4143 }
4144 else
4145 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004146
4147#ifdef FEAT_EVAL
4148 if (name == MB_TOLOWER(redir_reg)
4149 || (redir_reg == '"' && yb == y_previous))
4150 continue; /* do not list register being written to, the
4151 * pointer can be freed */
4152#endif
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (yb->y_array != NULL)
4155 {
4156 msg_putchar('\n');
4157 msg_putchar('"');
4158 msg_putchar(name);
4159 MSG_PUTS(" ");
4160
4161 n = (int)Columns - 6;
4162 for (j = 0; j < yb->y_size && n > 1; ++j)
4163 {
4164 if (j)
4165 {
4166 MSG_PUTS_ATTR("^J", attr);
4167 n -= 2;
4168 }
4169 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004172 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004173#endif
4174 msg_outtrans_len(p, clen);
4175#ifdef FEAT_MBYTE
4176 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#endif
4178 }
4179 }
4180 if (n > 1 && yb->y_type == MLINE)
4181 MSG_PUTS_ATTR("^J", attr);
4182 out_flush(); /* show one line at a time */
4183 }
4184 ui_breakcheck();
4185 }
4186
4187 /*
4188 * display last inserted text
4189 */
4190 if ((p = get_last_insert()) != NULL
4191 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4192 {
4193 MSG_PUTS("\n\". ");
4194 dis_msg(p, TRUE);
4195 }
4196
4197 /*
4198 * display last command line
4199 */
4200 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4201 && !got_int)
4202 {
4203 MSG_PUTS("\n\": ");
4204 dis_msg(last_cmdline, FALSE);
4205 }
4206
4207 /*
4208 * display current file name
4209 */
4210 if (curbuf->b_fname != NULL
4211 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4212 {
4213 MSG_PUTS("\n\"% ");
4214 dis_msg(curbuf->b_fname, FALSE);
4215 }
4216
4217 /*
4218 * display alternate file name
4219 */
4220 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4221 {
4222 char_u *fname;
4223 linenr_T dummy;
4224
4225 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4226 {
4227 MSG_PUTS("\n\"# ");
4228 dis_msg(fname, FALSE);
4229 }
4230 }
4231
4232 /*
4233 * display last search pattern
4234 */
4235 if (last_search_pat() != NULL
4236 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4237 {
4238 MSG_PUTS("\n\"/ ");
4239 dis_msg(last_search_pat(), FALSE);
4240 }
4241
4242#ifdef FEAT_EVAL
4243 /*
4244 * display last used expression
4245 */
4246 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4247 && !got_int)
4248 {
4249 MSG_PUTS("\n\"= ");
4250 dis_msg(expr_line, FALSE);
4251 }
4252#endif
4253}
4254
4255/*
4256 * display a string for do_dis()
4257 * truncate at end of screen line
4258 */
4259 static void
4260dis_msg(p, skip_esc)
4261 char_u *p;
4262 int skip_esc; /* if TRUE, ignore trailing ESC */
4263{
4264 int n;
4265#ifdef FEAT_MBYTE
4266 int l;
4267#endif
4268
4269 n = (int)Columns - 6;
4270 while (*p != NUL
4271 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4272 && (n -= ptr2cells(p)) >= 0)
4273 {
4274#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004275 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
4277 msg_outtrans_len(p, l);
4278 p += l;
4279 }
4280 else
4281#endif
4282 msg_outtrans_len(p++, 1);
4283 }
4284 ui_breakcheck();
4285}
4286
Bram Moolenaar81340392012-06-06 16:12:59 +02004287#if defined(FEAT_COMMENTS) || defined(PROTO)
4288/*
4289 * If "process" is TRUE and the line begins with a comment leader (possibly
4290 * after some white space), return a pointer to the text after it. Put a boolean
4291 * value indicating whether the line ends with an unclosed comment in
4292 * "is_comment".
4293 * line - line to be processed,
4294 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004295 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004296 * include_space - whether to also skip space following the comment leader,
4297 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004298 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004299 */
4300 static char_u *
4301skip_comment(line, process, include_space, is_comment)
4302 char_u *line;
4303 int process;
4304 int include_space;
4305 int *is_comment;
4306{
4307 char_u *comment_flags = NULL;
4308 int lead_len;
4309 int leader_offset = get_last_leader_offset(line, &comment_flags);
4310
4311 *is_comment = FALSE;
4312 if (leader_offset != -1)
4313 {
4314 /* Let's check whether the line ends with an unclosed comment.
4315 * If the last comment leader has COM_END in flags, there's no comment.
4316 */
4317 while (*comment_flags)
4318 {
4319 if (*comment_flags == COM_END
4320 || *comment_flags == ':')
4321 break;
4322 ++comment_flags;
4323 }
4324 if (*comment_flags != COM_END)
4325 *is_comment = TRUE;
4326 }
4327
4328 if (process == FALSE)
4329 return line;
4330
4331 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4332
4333 if (lead_len == 0)
4334 return line;
4335
4336 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004337 * - COM_END,
4338 * - colon,
4339 * whichever comes first.
4340 */
4341 while (*comment_flags)
4342 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004343 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004344 || *comment_flags == ':')
4345 {
4346 break;
4347 }
4348 ++comment_flags;
4349 }
4350
4351 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004352 * starting with a closing part of a three-part comment. That's good,
4353 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004354 */
4355 if (*comment_flags == ':' || *comment_flags == NUL)
4356 line += lead_len;
4357
4358 return line;
4359}
4360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004363 * Join 'count' lines (minimal 2) at cursor position.
4364 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004365 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4366 * leaders should not be removed.
4367 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4368 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004370 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 */
4372 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004373do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004374 long count;
4375 int insert_space;
4376 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004377 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004378 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004380 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004381 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004382 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004384 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004385 int endcurr1 = NUL;
4386 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004387 int currsize = 0; /* size of the current line */
4388 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004390 colnr_T col = 0;
4391 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004392#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004393 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004394 int remove_comments = (use_formatoptions == TRUE)
4395 && has_format_option(FO_REMOVE_COMS);
4396 int prev_was_comment;
4397#endif
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004400 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4401 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004404 /* Allocate an array to store the number of spaces inserted before each
4405 * line. We will use it to pre-compute the length of the new line and the
4406 * proper placement of each original line in the new one. */
4407 spaces = lalloc_clear((long_u)count, TRUE);
4408 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004410#if defined(FEAT_COMMENTS) || defined(PROTO)
4411 if (remove_comments)
4412 {
4413 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4414 if (comments == NULL)
4415 {
4416 vim_free(spaces);
4417 return FAIL;
4418 }
4419 }
4420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004423 * Don't move anything, just compute the final line length
4424 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004426 for (t = 0; t < count; ++t)
4427 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004428 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004429 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004430 {
4431 /* Set the '[ mark. */
4432 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4433 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4434 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004435#if defined(FEAT_COMMENTS) || defined(PROTO)
4436 if (remove_comments)
4437 {
4438 /* We don't want to remove the comment leader if the
4439 * previous line is not a comment. */
4440 if (t > 0 && prev_was_comment)
4441 {
4442
4443 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4444 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004445 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004446 curr = new_curr;
4447 }
4448 else
4449 curr = skip_comment(curr, FALSE, insert_space,
4450 &prev_was_comment);
4451 }
4452#endif
4453
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004454 if (insert_space && t > 0)
4455 {
4456 curr = skipwhite(curr);
4457 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4458#ifdef FEAT_MBYTE
4459 && (!has_format_option(FO_MBYTE_JOIN)
4460 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4461 && (!has_format_option(FO_MBYTE_JOIN2)
4462 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4463#endif
4464 )
4465 {
4466 /* don't add a space if the line is ending in a space */
4467 if (endcurr1 == ' ')
4468 endcurr1 = endcurr2;
4469 else
4470 ++spaces[t];
4471 /* extra space when 'joinspaces' set and line ends in '.' */
4472 if ( p_js
4473 && (endcurr1 == '.'
4474 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4475 && (endcurr1 == '?' || endcurr1 == '!'))))
4476 ++spaces[t];
4477 }
4478 }
4479 currsize = (int)STRLEN(curr);
4480 sumsize += currsize + spaces[t];
4481 endcurr1 = endcurr2 = NUL;
4482 if (insert_space && currsize > 0)
4483 {
4484#ifdef FEAT_MBYTE
4485 if (has_mbyte)
4486 {
4487 cend = curr + currsize;
4488 mb_ptr_back(curr, cend);
4489 endcurr1 = (*mb_ptr2char)(cend);
4490 if (cend > curr)
4491 {
4492 mb_ptr_back(curr, cend);
4493 endcurr2 = (*mb_ptr2char)(cend);
4494 }
4495 }
4496 else
4497#endif
4498 {
4499 endcurr1 = *(curr + currsize - 1);
4500 if (currsize > 1)
4501 endcurr2 = *(curr + currsize - 2);
4502 }
4503 }
4504 line_breakcheck();
4505 if (got_int)
4506 {
4507 ret = FAIL;
4508 goto theend;
4509 }
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004512 /* store the column position before last line */
4513 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004515 /* allocate the space for the new line */
4516 newp = alloc_check((unsigned)(sumsize + 1));
4517 cend = newp + sumsize;
4518 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004520 /*
4521 * Move affected lines to the new long one.
4522 *
4523 * Move marks from each deleted line to the joined line, adjusting the
4524 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4525 * should not really be a problem.
4526 */
4527 for (t = count - 1; ; --t)
4528 {
4529 cend -= currsize;
4530 mch_memmove(cend, curr, (size_t)currsize);
4531 if (spaces[t] > 0)
4532 {
4533 cend -= spaces[t];
4534 copy_spaces(cend, (size_t)(spaces[t]));
4535 }
4536 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004537 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538 if (t == 0)
4539 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004540 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004541#if defined(FEAT_COMMENTS) || defined(PROTO)
4542 if (remove_comments)
4543 curr += comments[t - 1];
4544#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004545 if (insert_space && t > 1)
4546 curr = skipwhite(curr);
4547 currsize = (int)STRLEN(curr);
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4550
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004551 if (setmark)
4552 {
4553 /* Set the '] mark. */
4554 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4555 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4556 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 /* Only report the change in the first line here, del_lines() will report
4559 * the deleted line. */
4560 changed_lines(curwin->w_cursor.lnum, currsize,
4561 curwin->w_cursor.lnum + 1, 0L);
4562
4563 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004564 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 * briefly, and then move it back. After del_lines() the cursor may
4566 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 */
4568 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004570 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 curwin->w_cursor.lnum = t;
4572
4573 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004574 * Set the cursor column:
4575 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004576 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004578 curwin->w_cursor.col =
4579 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#ifdef FEAT_VIRTUALEDIT
4583 curwin->w_cursor.coladd = 0;
4584#endif
4585 curwin->w_set_curswant = TRUE;
4586
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004587theend:
4588 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004589#if defined(FEAT_COMMENTS) || defined(PROTO)
4590 if (remove_comments)
4591 vim_free(comments);
4592#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004593 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594}
4595
4596#ifdef FEAT_COMMENTS
4597/*
4598 * Return TRUE if the two comment leaders given are the same. "lnum" is
4599 * the first line. White-space is ignored. Note that the whole of
4600 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4601 */
4602 static int
4603same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4604 linenr_T lnum;
4605 int leader1_len;
4606 char_u *leader1_flags;
4607 int leader2_len;
4608 char_u *leader2_flags;
4609{
4610 int idx1 = 0, idx2 = 0;
4611 char_u *p;
4612 char_u *line1;
4613 char_u *line2;
4614
4615 if (leader1_len == 0)
4616 return (leader2_len == 0);
4617
4618 /*
4619 * If first leader has 'f' flag, the lines can be joined only if the
4620 * second line does not have a leader.
4621 * If first leader has 'e' flag, the lines can never be joined.
4622 * If fist leader has 's' flag, the lines can only be joined if there is
4623 * some text after it and the second line has the 'm' flag.
4624 */
4625 if (leader1_flags != NULL)
4626 {
4627 for (p = leader1_flags; *p && *p != ':'; ++p)
4628 {
4629 if (*p == COM_FIRST)
4630 return (leader2_len == 0);
4631 if (*p == COM_END)
4632 return FALSE;
4633 if (*p == COM_START)
4634 {
4635 if (*(ml_get(lnum) + leader1_len) == NUL)
4636 return FALSE;
4637 if (leader2_flags == NULL || leader2_len == 0)
4638 return FALSE;
4639 for (p = leader2_flags; *p && *p != ':'; ++p)
4640 if (*p == COM_MIDDLE)
4641 return TRUE;
4642 return FALSE;
4643 }
4644 }
4645 }
4646
4647 /*
4648 * Get current line and next line, compare the leaders.
4649 * The first line has to be saved, only one line can be locked at a time.
4650 */
4651 line1 = vim_strsave(ml_get(lnum));
4652 if (line1 != NULL)
4653 {
4654 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4655 ;
4656 line2 = ml_get(lnum + 1);
4657 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4658 {
4659 if (!vim_iswhite(line2[idx2]))
4660 {
4661 if (line1[idx1++] != line2[idx2])
4662 break;
4663 }
4664 else
4665 while (vim_iswhite(line1[idx1]))
4666 ++idx1;
4667 }
4668 vim_free(line1);
4669 }
4670 return (idx2 == leader2_len && idx1 == leader1_len);
4671}
4672#endif
4673
4674/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004675 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 */
4677 void
4678op_format(oap, keep_cursor)
4679 oparg_T *oap;
4680 int keep_cursor; /* keep cursor on same text char */
4681{
4682 long old_line_count = curbuf->b_ml.ml_line_count;
4683
4684 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4685 * can put it back there. */
4686 curwin->w_cursor = oap->cursor_start;
4687
4688 if (u_save((linenr_T)(oap->start.lnum - 1),
4689 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4690 return;
4691 curwin->w_cursor = oap->start;
4692
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (oap->is_VIsual)
4694 /* When there is no change: need to remove the Visual selection */
4695 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697 /* Set '[ mark at the start of the formatted area */
4698 curbuf->b_op_start = oap->start;
4699
4700 /* For "gw" remember the cursor position and put it back below (adjusted
4701 * for joined and split lines). */
4702 if (keep_cursor)
4703 saved_cursor = oap->cursor_start;
4704
Bram Moolenaar81a82092008-03-12 16:27:00 +00004705 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
4707 /*
4708 * Leave the cursor at the first non-blank of the last formatted line.
4709 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4710 * line, so "." will do the next lines.
4711 */
4712 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4713 ++curwin->w_cursor.lnum;
4714 beginline(BL_WHITE | BL_FIX);
4715 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4716 msgmore(old_line_count);
4717
4718 /* put '] mark on the end of the formatted area */
4719 curbuf->b_op_end = curwin->w_cursor;
4720
4721 if (keep_cursor)
4722 {
4723 curwin->w_cursor = saved_cursor;
4724 saved_cursor.lnum = 0;
4725 }
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (oap->is_VIsual)
4728 {
4729 win_T *wp;
4730
4731 FOR_ALL_WINDOWS(wp)
4732 {
4733 if (wp->w_old_cursor_lnum != 0)
4734 {
4735 /* When lines have been inserted or deleted, adjust the end of
4736 * the Visual area to be redrawn. */
4737 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4738 wp->w_old_cursor_lnum += old_line_count;
4739 else
4740 wp->w_old_visual_lnum += old_line_count;
4741 }
4742 }
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744}
4745
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004746#if defined(FEAT_EVAL) || defined(PROTO)
4747/*
4748 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4749 */
4750 void
4751op_formatexpr(oap)
4752 oparg_T *oap;
4753{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004754 if (oap->is_VIsual)
4755 /* When there is no change: need to remove the Visual selection */
4756 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004757
Bram Moolenaar700303e2010-07-11 17:35:50 +02004758 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4759 /* As documented: when 'formatexpr' returns non-zero fall back to
4760 * internal formatting. */
4761 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004762}
4763
4764 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004765fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004766 linenr_T lnum;
4767 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004768 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004769{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004770 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4771 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004772 int r;
4773
4774 /*
4775 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004776 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004777 */
4778 set_vim_var_nr(VV_LNUM, lnum);
4779 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004780 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004782 /*
4783 * Evaluate the function.
4784 */
4785 if (use_sandbox)
4786 ++sandbox;
4787 r = eval_to_number(curbuf->b_p_fex);
4788 if (use_sandbox)
4789 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004790
4791 set_vim_var_string(VV_CHAR, NULL, -1);
4792
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004793 return r;
4794}
4795#endif
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797/*
4798 * Format "line_count" lines, starting at the cursor position.
4799 * When "line_count" is negative, format until the end of the paragraph.
4800 * Lines after the cursor line are saved for undo, caller must have saved the
4801 * first line.
4802 */
4803 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004804format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004806 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 int max_len;
4809 int is_not_par; /* current line not part of parag. */
4810 int next_is_not_par; /* next line not part of paragraph */
4811 int is_end_par; /* at end of paragraph */
4812 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4813 int next_is_start_par = FALSE;
4814#ifdef FEAT_COMMENTS
4815 int leader_len = 0; /* leader len of current line */
4816 int next_leader_len; /* leader len of next line */
4817 char_u *leader_flags = NULL; /* flags for leader of current line */
4818 char_u *next_leader_flags; /* flags for leader of next line */
4819 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004820 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821#endif
4822 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004823 int second_indent = -1; /* indent for second line (comment
4824 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 int do_second_indent;
4826 int do_number_indent;
4827 int do_trail_white;
4828 int first_par_line = TRUE;
4829 int smd_save;
4830 long count;
4831 int need_set_indent = TRUE; /* set indent of next paragraph */
4832 int force_format = FALSE;
4833 int old_State = State;
4834
4835 /* length of a line to force formatting: 3 * 'tw' */
4836 max_len = comp_textwidth(TRUE) * 3;
4837
4838 /* check for 'q', '2' and '1' in 'formatoptions' */
4839#ifdef FEAT_COMMENTS
4840 do_comments = has_format_option(FO_Q_COMS);
4841#endif
4842 do_second_indent = has_format_option(FO_Q_SECOND);
4843 do_number_indent = has_format_option(FO_Q_NUMBER);
4844 do_trail_white = has_format_option(FO_WHITE_PAR);
4845
4846 /*
4847 * Get info about the previous and current line.
4848 */
4849 if (curwin->w_cursor.lnum > 1)
4850 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4851#ifdef FEAT_COMMENTS
4852 , &leader_len, &leader_flags, do_comments
4853#endif
4854 );
4855 else
4856 is_not_par = TRUE;
4857 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4858#ifdef FEAT_COMMENTS
4859 , &next_leader_len, &next_leader_flags, do_comments
4860#endif
4861 );
4862 is_end_par = (is_not_par || next_is_not_par);
4863 if (!is_end_par && do_trail_white)
4864 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4865
4866 curwin->w_cursor.lnum--;
4867 for (count = line_count; count != 0 && !got_int; --count)
4868 {
4869 /*
4870 * Advance to next paragraph.
4871 */
4872 if (advance)
4873 {
4874 curwin->w_cursor.lnum++;
4875 prev_is_end_par = is_end_par;
4876 is_not_par = next_is_not_par;
4877#ifdef FEAT_COMMENTS
4878 leader_len = next_leader_len;
4879 leader_flags = next_leader_flags;
4880#endif
4881 }
4882
4883 /*
4884 * The last line to be formatted.
4885 */
4886 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4887 {
4888 next_is_not_par = TRUE;
4889#ifdef FEAT_COMMENTS
4890 next_leader_len = 0;
4891 next_leader_flags = NULL;
4892#endif
4893 }
4894 else
4895 {
4896 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4897#ifdef FEAT_COMMENTS
4898 , &next_leader_len, &next_leader_flags, do_comments
4899#endif
4900 );
4901 if (do_number_indent)
4902 next_is_start_par =
4903 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4904 }
4905 advance = TRUE;
4906 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4907 if (!is_end_par && do_trail_white)
4908 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4909
4910 /*
4911 * Skip lines that are not in a paragraph.
4912 */
4913 if (is_not_par)
4914 {
4915 if (line_count < 0)
4916 break;
4917 }
4918 else
4919 {
4920 /*
4921 * For the first line of a paragraph, check indent of second line.
4922 * Don't do this for comments and empty lines.
4923 */
4924 if (first_par_line
4925 && (do_second_indent || do_number_indent)
4926 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004927 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004929 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4930 {
4931#ifdef FEAT_COMMENTS
4932 if (leader_len == 0 && next_leader_len == 0)
4933 {
4934 /* no comment found */
4935#endif
4936 second_indent =
4937 get_indent_lnum(curwin->w_cursor.lnum + 1);
4938#ifdef FEAT_COMMENTS
4939 }
4940 else
4941 {
4942 second_indent = next_leader_len;
4943 do_comments_list = 1;
4944 }
4945#endif
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004948 {
4949#ifdef FEAT_COMMENTS
4950 if (leader_len == 0 && next_leader_len == 0)
4951 {
4952 /* no comment found */
4953#endif
4954 second_indent =
4955 get_number_indent(curwin->w_cursor.lnum);
4956#ifdef FEAT_COMMENTS
4957 }
4958 else
4959 {
4960 /* get_number_indent() is now "comment aware"... */
4961 second_indent =
4962 get_number_indent(curwin->w_cursor.lnum);
4963 do_comments_list = 1;
4964 }
4965#endif
4966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968
4969 /*
4970 * When the comment leader changes, it's the end of the paragraph.
4971 */
4972 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4973#ifdef FEAT_COMMENTS
4974 || !same_leader(curwin->w_cursor.lnum,
4975 leader_len, leader_flags,
4976 next_leader_len, next_leader_flags)
4977#endif
4978 )
4979 is_end_par = TRUE;
4980
4981 /*
4982 * If we have got to the end of a paragraph, or the line is
4983 * getting long, format it.
4984 */
4985 if (is_end_par || force_format)
4986 {
4987 if (need_set_indent)
4988 /* replace indent in first line with minimal number of
4989 * tabs and spaces, according to current options */
4990 (void)set_indent(get_indent(), SIN_CHANGED);
4991
4992 /* put cursor on last non-space */
4993 State = NORMAL; /* don't go past end-of-line */
4994 coladvance((colnr_T)MAXCOL);
4995 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4996 dec_cursor();
4997
4998 /* do the formatting, without 'showmode' */
4999 State = INSERT; /* for open_line() */
5000 smd_save = p_smd;
5001 p_smd = FALSE;
5002 insertchar(NUL, INSCHAR_FORMAT
5003#ifdef FEAT_COMMENTS
5004 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005005 + (do_comments && do_comments_list
5006 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005008 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 State = old_State;
5010 p_smd = smd_save;
5011 second_indent = -1;
5012 /* at end of par.: need to set indent of next par. */
5013 need_set_indent = is_end_par;
5014 if (is_end_par)
5015 {
5016 /* When called with a negative line count, break at the
5017 * end of the paragraph. */
5018 if (line_count < 0)
5019 break;
5020 first_par_line = TRUE;
5021 }
5022 force_format = FALSE;
5023 }
5024
5025 /*
5026 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005027 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 */
5029 if (!is_end_par)
5030 {
5031 advance = FALSE;
5032 curwin->w_cursor.lnum++;
5033 curwin->w_cursor.col = 0;
5034 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005038 {
5039 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5041 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005042 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005044 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5045 {
5046 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005047 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005048
5049 if (indent > 0)
5050 {
5051 (void)del_bytes(indent, FALSE, FALSE);
5052 mark_col_adjust(curwin->w_cursor.lnum,
5053 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005054 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005057 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 beep_flush();
5060 break;
5061 }
5062 first_par_line = FALSE;
5063 /* If the line is getting long, format it next time */
5064 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5065 force_format = TRUE;
5066 else
5067 force_format = FALSE;
5068 }
5069 }
5070 line_breakcheck();
5071 }
5072}
5073
5074/*
5075 * Return TRUE if line "lnum" ends in a white character.
5076 */
5077 static int
5078ends_in_white(lnum)
5079 linenr_T lnum;
5080{
5081 char_u *s = ml_get(lnum);
5082 size_t l;
5083
5084 if (*s == NUL)
5085 return FALSE;
5086 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5087 * invocation may call function multiple times". */
5088 l = STRLEN(s) - 1;
5089 return vim_iswhite(s[l]);
5090}
5091
5092/*
5093 * Blank lines, and lines containing only the comment leader, are left
5094 * untouched by the formatting. The function returns TRUE in this
5095 * case. It also returns TRUE when a line starts with the end of a comment
5096 * ('e' in comment flags), so that this line is skipped, and not joined to the
5097 * previous line. A new paragraph starts after a blank line, or when the
5098 * comment leader changes -- webb.
5099 */
5100#ifdef FEAT_COMMENTS
5101 static int
5102fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5103 linenr_T lnum;
5104 int *leader_len;
5105 char_u **leader_flags;
5106 int do_comments;
5107{
5108 char_u *flags = NULL; /* init for GCC */
5109 char_u *ptr;
5110
5111 ptr = ml_get(lnum);
5112 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005113 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 else
5115 *leader_len = 0;
5116
5117 if (*leader_len > 0)
5118 {
5119 /*
5120 * Search for 'e' flag in comment leader flags.
5121 */
5122 flags = *leader_flags;
5123 while (*flags && *flags != ':' && *flags != COM_END)
5124 ++flags;
5125 }
5126
5127 return (*skipwhite(ptr + *leader_len) == NUL
5128 || (*leader_len > 0 && *flags == COM_END)
5129 || startPS(lnum, NUL, FALSE));
5130}
5131#else
5132 static int
5133fmt_check_par(lnum)
5134 linenr_T lnum;
5135{
5136 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5137}
5138#endif
5139
5140/*
5141 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5142 * previous line is in the same paragraph. Used for auto-formatting.
5143 */
5144 int
5145paragraph_start(lnum)
5146 linenr_T lnum;
5147{
5148 char_u *p;
5149#ifdef FEAT_COMMENTS
5150 int leader_len = 0; /* leader len of current line */
5151 char_u *leader_flags = NULL; /* flags for leader of current line */
5152 int next_leader_len; /* leader len of next line */
5153 char_u *next_leader_flags; /* flags for leader of next line */
5154 int do_comments; /* format comments */
5155#endif
5156
5157 if (lnum <= 1)
5158 return TRUE; /* start of the file */
5159
5160 p = ml_get(lnum - 1);
5161 if (*p == NUL)
5162 return TRUE; /* after empty line */
5163
5164#ifdef FEAT_COMMENTS
5165 do_comments = has_format_option(FO_Q_COMS);
5166#endif
5167 if (fmt_check_par(lnum - 1
5168#ifdef FEAT_COMMENTS
5169 , &leader_len, &leader_flags, do_comments
5170#endif
5171 ))
5172 return TRUE; /* after non-paragraph line */
5173
5174 if (fmt_check_par(lnum
5175#ifdef FEAT_COMMENTS
5176 , &next_leader_len, &next_leader_flags, do_comments
5177#endif
5178 ))
5179 return TRUE; /* "lnum" is not a paragraph line */
5180
5181 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5182 return TRUE; /* missing trailing space in previous line. */
5183
5184 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5185 return TRUE; /* numbered item starts in "lnum". */
5186
5187#ifdef FEAT_COMMENTS
5188 if (!same_leader(lnum - 1, leader_len, leader_flags,
5189 next_leader_len, next_leader_flags))
5190 return TRUE; /* change of comment leader. */
5191#endif
5192
5193 return FALSE;
5194}
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196/*
5197 * prepare a few things for block mode yank/delete/tilde
5198 *
5199 * for delete:
5200 * - textlen includes the first/last char to be (partly) deleted
5201 * - start/endspaces is the number of columns that are taken by the
5202 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005203 * deleted.
5204 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * - textlen includes the first/last char to be wholly yanked
5206 * - start/endspaces is the number of columns of the first/last yanked char
5207 * that are to be yanked.
5208 */
5209 static void
5210block_prep(oap, bdp, lnum, is_del)
5211 oparg_T *oap;
5212 struct block_def *bdp;
5213 linenr_T lnum;
5214 int is_del;
5215{
5216 int incr = 0;
5217 char_u *pend;
5218 char_u *pstart;
5219 char_u *line;
5220 char_u *prev_pstart;
5221 char_u *prev_pend;
5222
5223 bdp->startspaces = 0;
5224 bdp->endspaces = 0;
5225 bdp->textlen = 0;
5226 bdp->start_vcol = 0;
5227 bdp->end_vcol = 0;
5228#ifdef FEAT_VISUALEXTRA
5229 bdp->is_short = FALSE;
5230 bdp->is_oneChar = FALSE;
5231 bdp->pre_whitesp = 0;
5232 bdp->pre_whitesp_c = 0;
5233 bdp->end_char_vcols = 0;
5234#endif
5235 bdp->start_char_vcols = 0;
5236
5237 line = ml_get(lnum);
5238 pstart = line;
5239 prev_pstart = line;
5240 while (bdp->start_vcol < oap->start_vcol && *pstart)
5241 {
5242 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005243 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 bdp->start_vcol += incr;
5245#ifdef FEAT_VISUALEXTRA
5246 if (vim_iswhite(*pstart))
5247 {
5248 bdp->pre_whitesp += incr;
5249 bdp->pre_whitesp_c++;
5250 }
5251 else
5252 {
5253 bdp->pre_whitesp = 0;
5254 bdp->pre_whitesp_c = 0;
5255 }
5256#endif
5257 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005258 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260 bdp->start_char_vcols = incr;
5261 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5262 {
5263 bdp->end_vcol = bdp->start_vcol;
5264#ifdef FEAT_VISUALEXTRA
5265 bdp->is_short = TRUE;
5266#endif
5267 if (!is_del || oap->op_type == OP_APPEND)
5268 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5269 }
5270 else
5271 {
5272 /* notice: this converts partly selected Multibyte characters to
5273 * spaces, too. */
5274 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5275 if (is_del && bdp->startspaces)
5276 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5277 pend = pstart;
5278 bdp->end_vcol = bdp->start_vcol;
5279 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5280 {
5281#ifdef FEAT_VISUALEXTRA
5282 bdp->is_oneChar = TRUE;
5283#endif
5284 if (oap->op_type == OP_INSERT)
5285 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5286 else if (oap->op_type == OP_APPEND)
5287 {
5288 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5289 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5290 }
5291 else
5292 {
5293 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5294 if (is_del && oap->op_type != OP_LSHIFT)
5295 {
5296 /* just putting the sum of those two into
5297 * bdp->startspaces doesn't work for Visual replace,
5298 * so we have to split the tab in two */
5299 bdp->startspaces = bdp->start_char_vcols
5300 - (bdp->start_vcol - oap->start_vcol);
5301 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5302 }
5303 }
5304 }
5305 else
5306 {
5307 prev_pend = pend;
5308 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5309 {
5310 /* Count a tab for what it's worth (if list mode not on) */
5311 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005312 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 bdp->end_vcol += incr;
5314 }
5315 if (bdp->end_vcol <= oap->end_vcol
5316 && (!is_del
5317 || oap->op_type == OP_APPEND
5318 || oap->op_type == OP_REPLACE)) /* line too short */
5319 {
5320#ifdef FEAT_VISUALEXTRA
5321 bdp->is_short = TRUE;
5322#endif
5323 /* Alternative: include spaces to fill up the block.
5324 * Disadvantage: can lead to trailing spaces when the line is
5325 * short where the text is put */
5326 /* if (!is_del || oap->op_type == OP_APPEND) */
5327 if (oap->op_type == OP_APPEND || virtual_op)
5328 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005329 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 else
5331 bdp->endspaces = 0; /* replace doesn't add characters */
5332 }
5333 else if (bdp->end_vcol > oap->end_vcol)
5334 {
5335 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5336 if (!is_del && bdp->endspaces)
5337 {
5338 bdp->endspaces = incr - bdp->endspaces;
5339 if (pend != pstart)
5340 pend = prev_pend;
5341 }
5342 }
5343 }
5344#ifdef FEAT_VISUALEXTRA
5345 bdp->end_char_vcols = incr;
5346#endif
5347 if (is_del && bdp->startspaces)
5348 pstart = prev_pstart;
5349 bdp->textlen = (int)(pend - pstart);
5350 }
5351 bdp->textcol = (colnr_T) (pstart - line);
5352 bdp->textstart = pstart;
5353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355#ifdef FEAT_RIGHTLEFT
5356static void reverse_line __ARGS((char_u *s));
5357
5358 static void
5359reverse_line(s)
5360 char_u *s;
5361{
5362 int i, j;
5363 char_u c;
5364
5365 if ((i = (int)STRLEN(s) - 1) <= 0)
5366 return;
5367
5368 curwin->w_cursor.col = i - curwin->w_cursor.col;
5369 for (j = 0; j < i; j++, i--)
5370 {
5371 c = s[i]; s[i] = s[j]; s[j] = c;
5372 }
5373}
5374
5375# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5376#else
5377# define RLADDSUBFIX(ptr)
5378#endif
5379
5380/*
5381 * add or subtract 'Prenum1' from a number in a line
5382 * 'command' is CTRL-A for add, CTRL-X for subtract
5383 *
5384 * return FAIL for failure, OK otherwise
5385 */
5386 int
5387do_addsub(command, Prenum1)
5388 int command;
5389 linenr_T Prenum1;
5390{
5391 int col;
5392 char_u *buf1;
5393 char_u buf2[NUMBUFLEN];
5394 int hex; /* 'X' or 'x': hex; '0': octal */
5395 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005396 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 long_u oldn;
5398 char_u *ptr;
5399 int c;
5400 int length = 0; /* character length of the number */
5401 int todel;
5402 int dohex;
5403 int dooct;
5404 int doalp;
5405 int firstdigit;
5406 int negative;
5407 int subtract;
5408
5409 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5410 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5411 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5412
5413 ptr = ml_get_curline();
5414 RLADDSUBFIX(ptr);
5415
5416 /*
5417 * First check if we are on a hexadecimal number, after the "0x".
5418 */
5419 col = curwin->w_cursor.col;
5420 if (dohex)
5421 while (col > 0 && vim_isxdigit(ptr[col]))
5422 --col;
5423 if ( dohex
5424 && col > 0
5425 && (ptr[col] == 'X'
5426 || ptr[col] == 'x')
5427 && ptr[col - 1] == '0'
5428 && vim_isxdigit(ptr[col + 1]))
5429 {
5430 /*
5431 * Found hexadecimal number, move to its start.
5432 */
5433 --col;
5434 }
5435 else
5436 {
5437 /*
5438 * Search forward and then backward to find the start of number.
5439 */
5440 col = curwin->w_cursor.col;
5441
5442 while (ptr[col] != NUL
5443 && !vim_isdigit(ptr[col])
5444 && !(doalp && ASCII_ISALPHA(ptr[col])))
5445 ++col;
5446
5447 while (col > 0
5448 && vim_isdigit(ptr[col - 1])
5449 && !(doalp && ASCII_ISALPHA(ptr[col])))
5450 --col;
5451 }
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 /*
5454 * If a number was found, and saving for undo works, replace the number.
5455 */
5456 firstdigit = ptr[col];
5457 RLADDSUBFIX(ptr);
5458 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5459 || u_save_cursor() != OK)
5460 {
5461 beep_flush();
5462 return FAIL;
5463 }
5464
5465 /* get ptr again, because u_save() may have changed it */
5466 ptr = ml_get_curline();
5467 RLADDSUBFIX(ptr);
5468
5469 if (doalp && ASCII_ISALPHA(firstdigit))
5470 {
5471 /* decrement or increment alphabetic character */
5472 if (command == Ctrl_X)
5473 {
5474 if (CharOrd(firstdigit) < Prenum1)
5475 {
5476 if (isupper(firstdigit))
5477 firstdigit = 'A';
5478 else
5479 firstdigit = 'a';
5480 }
5481 else
5482#ifdef EBCDIC
5483 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5484#else
5485 firstdigit -= Prenum1;
5486#endif
5487 }
5488 else
5489 {
5490 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5491 {
5492 if (isupper(firstdigit))
5493 firstdigit = 'Z';
5494 else
5495 firstdigit = 'z';
5496 }
5497 else
5498#ifdef EBCDIC
5499 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5500#else
5501 firstdigit += Prenum1;
5502#endif
5503 }
5504 curwin->w_cursor.col = col;
5505 (void)del_char(FALSE);
5506 ins_char(firstdigit);
5507 }
5508 else
5509 {
5510 negative = FALSE;
5511 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5512 {
5513 --col;
5514 negative = TRUE;
5515 }
5516
5517 /* get the number value (unsigned) */
5518 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5519
5520 /* ignore leading '-' for hex and octal numbers */
5521 if (hex && negative)
5522 {
5523 ++col;
5524 --length;
5525 negative = FALSE;
5526 }
5527
5528 /* add or subtract */
5529 subtract = FALSE;
5530 if (command == Ctrl_X)
5531 subtract ^= TRUE;
5532 if (negative)
5533 subtract ^= TRUE;
5534
5535 oldn = n;
5536 if (subtract)
5537 n -= (unsigned long)Prenum1;
5538 else
5539 n += (unsigned long)Prenum1;
5540
5541 /* handle wraparound for decimal numbers */
5542 if (!hex)
5543 {
5544 if (subtract)
5545 {
5546 if (n > oldn)
5547 {
5548 n = 1 + (n ^ (unsigned long)-1);
5549 negative ^= TRUE;
5550 }
5551 }
5552 else /* add */
5553 {
5554 if (n < oldn)
5555 {
5556 n = (n ^ (unsigned long)-1);
5557 negative ^= TRUE;
5558 }
5559 }
5560 if (n == 0)
5561 negative = FALSE;
5562 }
5563
5564 /*
5565 * Delete the old number.
5566 */
5567 curwin->w_cursor.col = col;
5568 todel = length;
5569 c = gchar_cursor();
5570 /*
5571 * Don't include the '-' in the length, only the length of the part
5572 * after it is kept the same.
5573 */
5574 if (c == '-')
5575 --length;
5576 while (todel-- > 0)
5577 {
5578 if (c < 0x100 && isalpha(c))
5579 {
5580 if (isupper(c))
5581 hexupper = TRUE;
5582 else
5583 hexupper = FALSE;
5584 }
5585 /* del_char() will mark line needing displaying */
5586 (void)del_char(FALSE);
5587 c = gchar_cursor();
5588 }
5589
5590 /*
5591 * Prepare the leading characters in buf1[].
5592 * When there are many leading zeros it could be very long. Allocate
5593 * a bit too much.
5594 */
5595 buf1 = alloc((unsigned)length + NUMBUFLEN);
5596 if (buf1 == NULL)
5597 return FAIL;
5598 ptr = buf1;
5599 if (negative)
5600 {
5601 *ptr++ = '-';
5602 }
5603 if (hex)
5604 {
5605 *ptr++ = '0';
5606 --length;
5607 }
5608 if (hex == 'x' || hex == 'X')
5609 {
5610 *ptr++ = hex;
5611 --length;
5612 }
5613
5614 /*
5615 * Put the number characters in buf2[].
5616 */
5617 if (hex == 0)
5618 sprintf((char *)buf2, "%lu", n);
5619 else if (hex == '0')
5620 sprintf((char *)buf2, "%lo", n);
5621 else if (hex && hexupper)
5622 sprintf((char *)buf2, "%lX", n);
5623 else
5624 sprintf((char *)buf2, "%lx", n);
5625 length -= (int)STRLEN(buf2);
5626
5627 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005628 * Adjust number of zeros to the new number of digits, so the
5629 * total length of the number remains the same.
5630 * Don't do this when
5631 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005633 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 while (length-- > 0)
5635 *ptr++ = '0';
5636 *ptr = NUL;
5637 STRCAT(buf1, buf2);
5638 ins_str(buf1); /* insert the new number */
5639 vim_free(buf1);
5640 }
5641 --curwin->w_cursor.col;
5642 curwin->w_set_curswant = TRUE;
5643#ifdef FEAT_RIGHTLEFT
5644 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5645 RLADDSUBFIX(ptr);
5646#endif
5647 return OK;
5648}
5649
5650#ifdef FEAT_VIMINFO
5651 int
5652read_viminfo_register(virp, force)
5653 vir_T *virp;
5654 int force;
5655{
5656 int eof;
5657 int do_it = TRUE;
5658 int size;
5659 int limit;
5660 int i;
5661 int set_prev = FALSE;
5662 char_u *str;
5663 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005664 int new_type = MCHAR; /* init to shut up compiler */
5665 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
5667 /* We only get here (hopefully) if line[0] == '"' */
5668 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005669
5670 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (*str == '"')
5672 {
5673 set_prev = TRUE;
5674 str++;
5675 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005676
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (!ASCII_ISALNUM(*str) && *str != '-')
5678 {
5679 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5680 return TRUE; /* too many errors, pretend end-of-file */
5681 do_it = FALSE;
5682 }
5683 get_yank_register(*str++, FALSE);
5684 if (!force && y_current->y_array != NULL)
5685 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005686
5687 if (*str == '@')
5688 {
5689 /* "x@: register x used for @@ */
5690 if (force || execreg_lastc == NUL)
5691 execreg_lastc = str[-1];
5692 }
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 size = 0;
5695 limit = 100; /* Optimized for registers containing <= 100 lines */
5696 if (do_it)
5697 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005698 /*
5699 * Build the new register in array[].
5700 * y_array is kept as-is until done.
5701 * The "do_it" flag is reset when something is wrong, in which case
5702 * array[] needs to be freed.
5703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (set_prev)
5705 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005706 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005707 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005709 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005711 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005713 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 /* get the block width; if it's missing we get a zero, which is OK */
5715 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005716 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718
5719 while (!(eof = viminfo_readline(virp))
5720 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5721 {
5722 if (do_it)
5723 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005724 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005726 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005728
5729 if (new_array == NULL)
5730 {
5731 do_it = FALSE;
5732 break;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005735 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005737 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740 str = viminfo_readstring(virp, 1, TRUE);
5741 if (str != NULL)
5742 array[size++] = str;
5743 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005744 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 do_it = FALSE;
5746 }
5747 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (do_it)
5750 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005751 /* free y_array[] */
5752 for (i = 0; i < y_current->y_size; i++)
5753 vim_free(y_current->y_array[i]);
5754 vim_free(y_current->y_array);
5755
5756 y_current->y_type = new_type;
5757 y_current->y_width = new_width;
5758 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 if (size == 0)
5760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 y_current->y_array = NULL;
5762 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005765 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 y_current->y_array =
5767 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5768 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005769 {
5770 if (y_current->y_array == NULL)
5771 vim_free(array[i]);
5772 else
5773 y_current->y_array[i] = array[i];
5774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005777 else
5778 {
5779 /* Free array[] if it was filled. */
5780 for (i = 0; i < size; i++)
5781 vim_free(array[i]);
5782 }
5783 vim_free(array);
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 return eof;
5786}
5787
5788 void
5789write_viminfo_registers(fp)
5790 FILE *fp;
5791{
5792 int i, j;
5793 char_u *type;
5794 char_u c;
5795 int num_lines;
5796 int max_num_lines;
5797 int max_kbyte;
5798 long len;
5799
Bram Moolenaar64404472010-06-26 06:24:45 +02005800 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801
5802 /* Get '<' value, use old '"' value if '<' is not found. */
5803 max_num_lines = get_viminfo_parameter('<');
5804 if (max_num_lines < 0)
5805 max_num_lines = get_viminfo_parameter('"');
5806 if (max_num_lines == 0)
5807 return;
5808 max_kbyte = get_viminfo_parameter('s');
5809 if (max_kbyte == 0)
5810 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 for (i = 0; i < NUM_REGISTERS; i++)
5813 {
5814 if (y_regs[i].y_array == NULL)
5815 continue;
5816#ifdef FEAT_CLIPBOARD
5817 /* Skip '*'/'+' register, we don't want them back next time */
5818 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5819 continue;
5820#endif
5821#ifdef FEAT_DND
5822 /* Neither do we want the '~' register */
5823 if (i == TILDE_REGISTER)
5824 continue;
5825#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005826 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005828 if (num_lines == 0
5829 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005830 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005831 continue;
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (max_kbyte > 0)
5834 {
5835 /* Skip register if there is more text than the maximum size. */
5836 len = 0;
5837 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005838 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (len > (long)max_kbyte * 1024L)
5840 continue;
5841 }
5842
5843 switch (y_regs[i].y_type)
5844 {
5845 case MLINE:
5846 type = (char_u *)"LINE";
5847 break;
5848 case MCHAR:
5849 type = (char_u *)"CHAR";
5850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 case MBLOCK:
5852 type = (char_u *)"BLOCK";
5853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 default:
5855 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005856 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 emsg(IObuff);
5858 type = (char_u *)"LINE";
5859 break;
5860 }
5861 if (y_previous == &y_regs[i])
5862 fprintf(fp, "\"");
5863 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005864 fprintf(fp, "\"%c", c);
5865 if (c == execreg_lastc)
5866 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005867 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869 /* If max_num_lines < 0, then we save ALL the lines in the register */
5870 if (max_num_lines > 0 && num_lines > max_num_lines)
5871 num_lines = max_num_lines;
5872 for (j = 0; j < num_lines; j++)
5873 {
5874 putc('\t', fp);
5875 viminfo_writestring(fp, y_regs[i].y_array[j]);
5876 }
5877 }
5878}
5879#endif /* FEAT_VIMINFO */
5880
5881#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5882/*
5883 * SELECTION / PRIMARY ('*')
5884 *
5885 * Text selection stuff that uses the GUI selection register '*'. When using a
5886 * GUI this may be text from another window, otherwise it is the last text we
5887 * had highlighted with VIsual mode. With mouse support, clicking the middle
5888 * button performs the paste, otherwise you will need to do <"*p>. "
5889 * If not under X, it is synonymous with the clipboard register '+'.
5890 *
5891 * X CLIPBOARD ('+')
5892 *
5893 * Text selection stuff that uses the GUI clipboard register '+'.
5894 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5895 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5896 * otherwise you will need to do <"+p>. "
5897 * If not under X, it is synonymous with the selection register '*'.
5898 */
5899
5900/*
5901 * Routine to export any final X selection we had to the environment
5902 * so that the text is still available after vim has exited. X selections
5903 * only exist while the owning application exists, so we write to the
5904 * permanent (while X runs) store CUT_BUFFER0.
5905 * Dump the CLIPBOARD selection if we own it (it's logically the more
5906 * 'permanent' of the two), otherwise the PRIMARY one.
5907 * For now, use a hard-coded sanity limit of 1Mb of data.
5908 */
5909#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5910 void
5911x11_export_final_selection()
5912{
5913 Display *dpy;
5914 char_u *str = NULL;
5915 long_u len = 0;
5916 int motion_type = -1;
5917
5918# ifdef FEAT_GUI
5919 if (gui.in_use)
5920 dpy = X_DISPLAY;
5921 else
5922# endif
5923# ifdef FEAT_XCLIPBOARD
5924 dpy = xterm_dpy;
5925# else
5926 return;
5927# endif
5928
5929 /* Get selection to export */
5930 if (clip_plus.owned)
5931 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5932 else if (clip_star.owned)
5933 motion_type = clip_convert_selection(&str, &len, &clip_star);
5934
5935 /* Check it's OK */
5936 if (dpy != NULL && str != NULL && motion_type >= 0
5937 && len < 1024*1024 && len > 0)
5938 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005939#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005940 int ok = TRUE;
5941
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005942 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5943 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5944 * encoding conversion usually doesn't work, so keep the text as-is.
5945 */
5946 if (has_mbyte)
5947 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005948 vimconv_T vc;
5949
5950 vc.vc_type = CONV_NONE;
5951 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5952 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005953 int intlen = len;
5954 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005955
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005956 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005957 conv_str = string_convert(&vc, str, &intlen);
5958 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005959 if (conv_str != NULL)
5960 {
5961 vim_free(str);
5962 str = conv_str;
5963 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005964 else
5965 {
5966 ok = FALSE;
5967 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005968 convert_setup(&vc, NULL, NULL);
5969 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005970 else
5971 {
5972 ok = FALSE;
5973 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005974 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005975
5976 /* Do not store the string if conversion failed. Better to use any
5977 * other selection than garbled text. */
5978 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005979#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005980 {
5981 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5982 XFlush(dpy);
5983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
5985
5986 vim_free(str);
5987}
5988#endif
5989
5990 void
5991clip_free_selection(cbd)
5992 VimClipboard *cbd;
5993{
5994 struct yankreg *y_ptr = y_current;
5995
5996 if (cbd == &clip_plus)
5997 y_current = &y_regs[PLUS_REGISTER];
5998 else
5999 y_current = &y_regs[STAR_REGISTER];
6000 free_yank_all();
6001 y_current->y_size = 0;
6002 y_current = y_ptr;
6003}
6004
6005/*
6006 * Get the selected text and put it in the gui selection register '*' or '+'.
6007 */
6008 void
6009clip_get_selection(cbd)
6010 VimClipboard *cbd;
6011{
6012 struct yankreg *old_y_previous, *old_y_current;
6013 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 pos_T old_visual;
6015 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 colnr_T old_curswant;
6017 int old_set_curswant;
6018 pos_T old_op_start, old_op_end;
6019 oparg_T oa;
6020 cmdarg_T ca;
6021
6022 if (cbd->owned)
6023 {
6024 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6025 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6026 return;
6027
6028 /* Get the text between clip_star.start & clip_star.end */
6029 old_y_previous = y_previous;
6030 old_y_current = y_current;
6031 old_cursor = curwin->w_cursor;
6032 old_curswant = curwin->w_curswant;
6033 old_set_curswant = curwin->w_set_curswant;
6034 old_op_start = curbuf->b_op_start;
6035 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 old_visual = VIsual;
6037 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 clear_oparg(&oa);
6039 oa.regname = (cbd == &clip_plus ? '+' : '*');
6040 oa.op_type = OP_YANK;
6041 vim_memset(&ca, 0, sizeof(ca));
6042 ca.oap = &oa;
6043 ca.cmdchar = 'y';
6044 ca.count1 = 1;
6045 ca.retval = CA_NO_ADJ_OP_END;
6046 do_pending_operator(&ca, 0, TRUE);
6047 y_previous = old_y_previous;
6048 y_current = old_y_current;
6049 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006050 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 curwin->w_curswant = old_curswant;
6052 curwin->w_set_curswant = old_set_curswant;
6053 curbuf->b_op_start = old_op_start;
6054 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 VIsual = old_visual;
6056 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 }
6058 else
6059 {
6060 clip_free_selection(cbd);
6061
6062 /* Try to get selected text from another window */
6063 clip_gen_request_selection(cbd);
6064 }
6065}
6066
Bram Moolenaard44347f2011-06-19 01:14:29 +02006067/*
6068 * Convert from the GUI selection string into the '*'/'+' register.
6069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 void
6071clip_yank_selection(type, str, len, cbd)
6072 int type;
6073 char_u *str;
6074 long len;
6075 VimClipboard *cbd;
6076{
6077 struct yankreg *y_ptr;
6078
6079 if (cbd == &clip_plus)
6080 y_ptr = &y_regs[PLUS_REGISTER];
6081 else
6082 y_ptr = &y_regs[STAR_REGISTER];
6083
6084 clip_free_selection(cbd);
6085
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006086 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087}
6088
6089/*
6090 * Convert the '*'/'+' register into a GUI selection string returned in *str
6091 * with length *len.
6092 * Returns the motion type, or -1 for failure.
6093 */
6094 int
6095clip_convert_selection(str, len, cbd)
6096 char_u **str;
6097 long_u *len;
6098 VimClipboard *cbd;
6099{
6100 char_u *p;
6101 int lnum;
6102 int i, j;
6103 int_u eolsize;
6104 struct yankreg *y_ptr;
6105
6106 if (cbd == &clip_plus)
6107 y_ptr = &y_regs[PLUS_REGISTER];
6108 else
6109 y_ptr = &y_regs[STAR_REGISTER];
6110
6111#ifdef USE_CRNL
6112 eolsize = 2;
6113#else
6114 eolsize = 1;
6115#endif
6116
6117 *str = NULL;
6118 *len = 0;
6119 if (y_ptr->y_array == NULL)
6120 return -1;
6121
6122 for (i = 0; i < y_ptr->y_size; i++)
6123 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6124
6125 /*
6126 * Don't want newline character at end of last line if we're in MCHAR mode.
6127 */
6128 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6129 *len -= eolsize;
6130
6131 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6132 if (p == NULL)
6133 return -1;
6134 lnum = 0;
6135 for (i = 0, j = 0; i < (int)*len; i++, j++)
6136 {
6137 if (y_ptr->y_array[lnum][j] == '\n')
6138 p[i] = NUL;
6139 else if (y_ptr->y_array[lnum][j] == NUL)
6140 {
6141#ifdef USE_CRNL
6142 p[i++] = '\r';
6143#endif
6144#ifdef USE_CR
6145 p[i] = '\r';
6146#else
6147 p[i] = '\n';
6148#endif
6149 lnum++;
6150 j = -1;
6151 }
6152 else
6153 p[i] = y_ptr->y_array[lnum][j];
6154 }
6155 return y_ptr->y_type;
6156}
6157
6158
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159/*
6160 * If we have written to a clipboard register, send the text to the clipboard.
6161 */
6162 static void
6163may_set_selection()
6164{
6165 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6166 {
6167 clip_own_selection(&clip_star);
6168 clip_gen_set_selection(&clip_star);
6169 }
6170 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6171 {
6172 clip_own_selection(&clip_plus);
6173 clip_gen_set_selection(&clip_plus);
6174 }
6175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177#endif /* FEAT_CLIPBOARD || PROTO */
6178
6179
6180#if defined(FEAT_DND) || defined(PROTO)
6181/*
6182 * Replace the contents of the '~' register with str.
6183 */
6184 void
6185dnd_yank_drag_data(str, len)
6186 char_u *str;
6187 long len;
6188{
6189 struct yankreg *curr;
6190
6191 curr = y_current;
6192 y_current = &y_regs[TILDE_REGISTER];
6193 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006194 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 y_current = curr;
6196}
6197#endif
6198
6199
6200#if defined(FEAT_EVAL) || defined(PROTO)
6201/*
6202 * Return the type of a register.
6203 * Used for getregtype()
6204 * Returns MAUTO for error.
6205 */
6206 char_u
6207get_reg_type(regname, reglen)
6208 int regname;
6209 long *reglen;
6210{
6211 switch (regname)
6212 {
6213 case '%': /* file name */
6214 case '#': /* alternate file name */
6215 case '=': /* expression */
6216 case ':': /* last command line */
6217 case '/': /* last search-pattern */
6218 case '.': /* last inserted text */
6219#ifdef FEAT_SEARCHPATH
6220 case Ctrl_F: /* Filename under cursor */
6221 case Ctrl_P: /* Path under cursor, expand via "path" */
6222#endif
6223 case Ctrl_W: /* word under cursor */
6224 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6225 case '_': /* black hole: always empty */
6226 return MCHAR;
6227 }
6228
6229#ifdef FEAT_CLIPBOARD
6230 regname = may_get_selection(regname);
6231#endif
6232
Bram Moolenaar32b92012014-01-14 12:33:36 +01006233 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6234 return MAUTO;
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 get_yank_register(regname, FALSE);
6237
6238 if (y_current->y_array != NULL)
6239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (reglen != NULL && y_current->y_type == MBLOCK)
6241 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 return y_current->y_type;
6243 }
6244 return MAUTO;
6245}
6246
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006247static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6248
6249/*
6250 * When "flags" has GREG_LIST return a list with text "s".
6251 * Otherwise just return "s".
6252 */
6253 static char_u *
6254getreg_wrap_one_line(s, flags)
6255 char_u *s;
6256 int flags;
6257{
6258 if (flags & GREG_LIST)
6259 {
6260 list_T *list = list_alloc();
6261
6262 if (list != NULL)
6263 {
6264 if (list_append_string(list, NULL, -1) == FAIL)
6265 {
6266 list_free(list, TRUE);
6267 return NULL;
6268 }
6269 list->lv_first->li_tv.vval.v_string = s;
6270 }
6271 return (char_u *)list;
6272 }
6273 return s;
6274}
6275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276/*
6277 * Return the contents of a register as a single allocated string.
6278 * Used for "@r" in expressions and for getreg().
6279 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006280 * Flags:
6281 * GREG_NO_EXPR Do not allow expression register
6282 * GREG_EXPR_SRC For the expression register: return expression itself,
6283 * not the result of its evaluation.
6284 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 */
6286 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006287get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006289 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 long i;
6292 char_u *retval;
6293 int allocated;
6294 long len;
6295
6296 /* Don't allow using an expression register inside an expression */
6297 if (regname == '=')
6298 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006299 if (flags & GREG_NO_EXPR)
6300 return NULL;
6301 if (flags & GREG_EXPR_SRC)
6302 return getreg_wrap_one_line(get_expr_line_src(), flags);
6303 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 }
6305
6306 if (regname == '@') /* "@@" is used for unnamed register */
6307 regname = '"';
6308
6309 /* check for valid regname */
6310 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6311 return NULL;
6312
6313#ifdef FEAT_CLIPBOARD
6314 regname = may_get_selection(regname);
6315#endif
6316
6317 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6318 {
6319 if (retval == NULL)
6320 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006321 if (allocated)
6322 return getreg_wrap_one_line(retval, flags);
6323 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325
6326 get_yank_register(regname, FALSE);
6327 if (y_current->y_array == NULL)
6328 return NULL;
6329
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006330 if (flags & GREG_LIST)
6331 {
6332 list_T *list = list_alloc();
6333 int error = FALSE;
6334
6335 if (list == NULL)
6336 return NULL;
6337 for (i = 0; i < y_current->y_size; ++i)
6338 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6339 error = TRUE;
6340 if (error)
6341 {
6342 list_free(list, TRUE);
6343 return NULL;
6344 }
6345 return (char_u *)list;
6346 }
6347
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 /*
6349 * Compute length of resulting string.
6350 */
6351 len = 0;
6352 for (i = 0; i < y_current->y_size; ++i)
6353 {
6354 len += (long)STRLEN(y_current->y_array[i]);
6355 /*
6356 * Insert a newline between lines and after last line if
6357 * y_type is MLINE.
6358 */
6359 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6360 ++len;
6361 }
6362
6363 retval = lalloc(len + 1, TRUE);
6364
6365 /*
6366 * Copy the lines of the yank register into the string.
6367 */
6368 if (retval != NULL)
6369 {
6370 len = 0;
6371 for (i = 0; i < y_current->y_size; ++i)
6372 {
6373 STRCPY(retval + len, y_current->y_array[i]);
6374 len += (long)STRLEN(retval + len);
6375
6376 /*
6377 * Insert a NL between lines and after the last line if y_type is
6378 * MLINE.
6379 */
6380 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6381 retval[len++] = '\n';
6382 }
6383 retval[len] = NUL;
6384 }
6385
6386 return retval;
6387}
6388
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006389 static int
6390init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6391 int name;
6392 struct yankreg **old_y_previous;
6393 struct yankreg **old_y_current;
6394 int must_append;
6395 int *yank_type UNUSED;
6396{
6397 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6398 {
6399 emsg_invreg(name);
6400 return FAIL;
6401 }
6402
6403 /* Don't want to change the current (unnamed) register */
6404 *old_y_previous = y_previous;
6405 *old_y_current = y_current;
6406
6407 get_yank_register(name, TRUE);
6408 if (!y_append && !must_append)
6409 free_yank_all();
6410 return OK;
6411}
6412
6413 static void
6414finish_write_reg(name, old_y_previous, old_y_current)
6415 int name;
6416 struct yankreg *old_y_previous;
6417 struct yankreg *old_y_current;
6418{
6419# ifdef FEAT_CLIPBOARD
6420 /* Send text of clipboard register to the clipboard. */
6421 may_set_selection();
6422# endif
6423
6424 /* ':let @" = "val"' should change the meaning of the "" register */
6425 if (name != '"')
6426 y_previous = old_y_previous;
6427 y_current = old_y_current;
6428}
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430/*
6431 * Store string "str" in register "name".
6432 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6433 * If "must_append" is TRUE, always append to the register. Otherwise append
6434 * if "name" is an uppercase letter.
6435 * Note: "maxlen" and "must_append" don't work for the "/" register.
6436 * Careful: 'str' is modified, you may have to use a copy!
6437 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6438 */
6439 void
6440write_reg_contents(name, str, maxlen, must_append)
6441 int name;
6442 char_u *str;
6443 int maxlen;
6444 int must_append;
6445{
6446 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6447}
6448
6449 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006450write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6451 int name;
6452 char_u **strings;
6453 int maxlen UNUSED;
6454 int must_append;
6455 int yank_type;
6456 long block_len;
6457{
6458 struct yankreg *old_y_previous, *old_y_current;
6459
6460 if (name == '/'
6461#ifdef FEAT_EVAL
6462 || name == '='
6463#endif
6464 )
6465 {
6466 char_u *s;
6467
6468 if (strings[0] == NULL)
6469 s = (char_u *)"";
6470 else if (strings[1] != NULL)
6471 {
6472 EMSG(_("E883: search pattern and expression register may not "
6473 "contain two or more lines"));
6474 return;
6475 }
6476 else
6477 s = strings[0];
6478 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6479 return;
6480 }
6481
6482 if (name == '_') /* black hole: nothing to do */
6483 return;
6484
6485 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6486 &yank_type) == FAIL)
6487 return;
6488
6489 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6490
6491 finish_write_reg(name, old_y_previous, old_y_current);
6492}
6493
6494 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6496 int name;
6497 char_u *str;
6498 int maxlen;
6499 int must_append;
6500 int yank_type;
6501 long block_len;
6502{
6503 struct yankreg *old_y_previous, *old_y_current;
6504 long len;
6505
Bram Moolenaare7566042005-06-17 22:00:15 +00006506 if (maxlen >= 0)
6507 len = maxlen;
6508 else
6509 len = (long)STRLEN(str);
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 /* Special case: '/' search pattern */
6512 if (name == '/')
6513 {
6514 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6515 return;
6516 }
6517
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006518 if (name == '#')
6519 {
6520 buf_T *buf;
6521
6522 if (VIM_ISDIGIT(*str))
6523 {
6524 int num = atoi((char *)str);
6525
6526 buf = buflist_findnr(num);
6527 if (buf == NULL)
6528 EMSGN(_(e_nobufnr), (long)num);
6529 }
6530 else
6531 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6532 TRUE, FALSE, FALSE));
6533 if (buf == NULL)
6534 return;
6535 curwin->w_alt_fnum = buf->b_fnum;
6536 return;
6537 }
6538
Bram Moolenaare7566042005-06-17 22:00:15 +00006539#ifdef FEAT_EVAL
6540 if (name == '=')
6541 {
6542 char_u *p, *s;
6543
6544 p = vim_strnsave(str, (int)len);
6545 if (p == NULL)
6546 return;
6547 if (must_append)
6548 {
6549 s = concat_str(get_expr_line_src(), p);
6550 vim_free(p);
6551 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006552 }
6553 set_expr_line(p);
6554 return;
6555 }
6556#endif
6557
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (name == '_') /* black hole: nothing to do */
6559 return;
6560
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006561 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6562 &yank_type) == FAIL)
6563 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006565 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006567 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568}
6569#endif /* FEAT_EVAL */
6570
6571#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6572/*
6573 * Put a string into a register. When the register is not empty, the string
6574 * is appended.
6575 */
6576 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006577str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006579 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 char_u *str; /* string to put in register */
6581 long len; /* length of string */
6582 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006583 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006585 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 int lnum;
6587 long start;
6588 long i;
6589 int extra;
6590 int newlines; /* number of lines added */
6591 int extraline = 0; /* extra line at the end */
6592 int append = FALSE; /* append to last line in register */
6593 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006594 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006598 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 y_ptr->y_size = 0;
6600
Bram Moolenaard44347f2011-06-19 01:14:29 +02006601 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006602 type = ((str_list || (len > 0 && (str[len - 1] == NL
6603 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006604 ? MLINE : MCHAR);
6605 else
6606 type = yank_type;
6607
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 /*
6609 * Count the number of lines within the string
6610 */
6611 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006612 if (str_list)
6613 {
6614 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006619 for (i = 0; i < len; i++)
6620 if (str[i] == '\n')
6621 ++newlines;
6622 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6623 {
6624 extraline = 1;
6625 ++newlines; /* count extra newline at the end */
6626 }
6627 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6628 {
6629 append = TRUE;
6630 --newlines; /* uncount newline when appending first line */
6631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633
6634 /*
6635 * Allocate an array to hold the pointers to the new register lines.
6636 * If the register was not empty, move the existing lines to the new array.
6637 */
6638 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6639 * sizeof(char_u *), TRUE);
6640 if (pp == NULL) /* out of memory */
6641 return;
6642 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6643 pp[lnum] = y_ptr->y_array[lnum];
6644 vim_free(y_ptr->y_array);
6645 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648 /*
6649 * Find the end of each line and save it into the array.
6650 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006651 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006653 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6654 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006655 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006656 pp[lnum] = vim_strnsave(*ss, i);
6657 if (i > maxlen)
6658 maxlen = i;
6659 }
6660 }
6661 else
6662 {
6663 for (start = 0; start < len + extraline; start += i + 1)
6664 {
6665 for (i = start; i < len; ++i) /* find the end of the line */
6666 if (str[i] == '\n')
6667 break;
6668 i -= start; /* i is now length of line */
6669 if (i > maxlen)
6670 maxlen = i;
6671 if (append)
6672 {
6673 --lnum;
6674 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6675 }
6676 else
6677 extra = 0;
6678 s = alloc((unsigned)(i + extra + 1));
6679 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006681 if (extra)
6682 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6683 if (append)
6684 vim_free(y_ptr->y_array[lnum]);
6685 if (i)
6686 mch_memmove(s + extra, str + start, (size_t)i);
6687 extra += i;
6688 s[extra] = NUL;
6689 y_ptr->y_array[lnum++] = s;
6690 while (--extra >= 0)
6691 {
6692 if (*s == NUL)
6693 *s = '\n'; /* replace NUL with newline */
6694 ++s;
6695 }
6696 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 y_ptr->y_type = type;
6700 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (type == MBLOCK)
6702 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6703 else
6704 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6707
6708 void
6709clear_oparg(oap)
6710 oparg_T *oap;
6711{
6712 vim_memset(oap, 0, sizeof(oparg_T));
6713}
6714
Bram Moolenaar7c626922005-02-07 22:01:03 +00006715static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006718 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 *
6720 * "Words" are counted by looking for boundaries between non-space and
6721 * space characters. (it seems to produce results that match 'wc'.)
6722 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006723 * Return value is byte count; word count for the line is added to "*wc".
6724 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 *
6726 * The function will only examine the first "limit" characters in the
6727 * line, stopping if it encounters an end-of-line (NUL byte). In that
6728 * case, eol_size will be added to the character count to account for
6729 * the size of the EOL character.
6730 */
6731 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006732line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 char_u *line;
6734 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006735 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 long limit;
6737 int eol_size;
6738{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006739 long i;
6740 long words = 0;
6741 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 int is_word = 0;
6743
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006744 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {
6746 if (is_word)
6747 {
6748 if (vim_isspace(line[i]))
6749 {
6750 words++;
6751 is_word = 0;
6752 }
6753 }
6754 else if (!vim_isspace(line[i]))
6755 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006756 ++chars;
6757#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006758 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006759#else
6760 ++i;
6761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763
6764 if (is_word)
6765 words++;
6766 *wc += words;
6767
6768 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006769 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006772 chars += eol_size;
6773 }
6774 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 return i;
6776}
6777
6778/*
6779 * Give some info about the position of the cursor (for "g CTRL-G").
6780 * In Visual mode, give some info about the selected region. (In this case,
6781 * the *_count_cursor variables store running totals for the selection.)
6782 */
6783 void
6784cursor_pos_info()
6785{
6786 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006787 char_u buf1[50];
6788 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006790 long byte_count = 0;
6791 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 long char_count = 0;
6793 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 long word_count = 0;
6795 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006796 int eol_size;
6797 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 long line_count_selected = 0;
6799 pos_T min_pos, max_pos;
6800 oparg_T oparg;
6801 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 /*
6804 * Compute the length of the file in characters.
6805 */
6806 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6807 {
6808 MSG(_(no_lines_msg));
6809 }
6810 else
6811 {
6812 if (get_fileformat(curbuf) == EOL_DOS)
6813 eol_size = 2;
6814 else
6815 eol_size = 1;
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (VIsual_active)
6818 {
6819 if (lt(VIsual, curwin->w_cursor))
6820 {
6821 min_pos = VIsual;
6822 max_pos = curwin->w_cursor;
6823 }
6824 else
6825 {
6826 min_pos = curwin->w_cursor;
6827 max_pos = VIsual;
6828 }
6829 if (*p_sel == 'e' && max_pos.col > 0)
6830 --max_pos.col;
6831
6832 if (VIsual_mode == Ctrl_V)
6833 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006834#ifdef FEAT_LINEBREAK
6835 char_u * saved_sbr = p_sbr;
6836
6837 /* Make 'sbr' empty for a moment to get the correct size. */
6838 p_sbr = empty_option;
6839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 oparg.is_VIsual = 1;
6841 oparg.block_mode = TRUE;
6842 oparg.op_type = OP_NOP;
6843 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006844 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006845#ifdef FEAT_LINEBREAK
6846 p_sbr = saved_sbr;
6847#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006848 if (curwin->w_curswant == MAXCOL)
6849 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 /* Swap the start, end vcol if needed */
6851 if (oparg.end_vcol < oparg.start_vcol)
6852 {
6853 oparg.end_vcol += oparg.start_vcol;
6854 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6855 oparg.end_vcol -= oparg.start_vcol;
6856 }
6857 }
6858 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
6861 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6862 {
6863 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006864 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
6866 ui_breakcheck();
6867 if (got_int)
6868 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006869 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 /* Do extra processing for VIsual mode. */
6873 if (VIsual_active
6874 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6875 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006876 char_u *s = NULL;
6877 long len = 0L;
6878
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 switch (VIsual_mode)
6880 {
6881 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006882#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006886#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006888#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006889 s = bd.textstart;
6890 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 break;
6892 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006893 s = ml_get(lnum);
6894 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 break;
6896 case 'v':
6897 {
6898 colnr_T start_col = (lnum == min_pos.lnum)
6899 ? min_pos.col : 0;
6900 colnr_T end_col = (lnum == max_pos.lnum)
6901 ? max_pos.col - start_col + 1 : MAXCOL;
6902
Bram Moolenaardef9e822004-12-31 20:58:58 +00006903 s = ml_get(lnum) + start_col;
6904 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906 break;
6907 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006908 if (s != NULL)
6909 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006910 byte_count_cursor += line_count_info(s, &word_count_cursor,
6911 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006912 if (lnum == curbuf->b_ml.ml_line_count
6913 && !curbuf->b_p_eol
6914 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006915 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006916 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
6921 /* In non-visual mode, check for the line the cursor is on */
6922 if (lnum == curwin->w_cursor.lnum)
6923 {
6924 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006925 char_count_cursor += char_count;
6926 byte_count_cursor = byte_count +
6927 line_count_info(ml_get(lnum),
6928 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 (long)(curwin->w_cursor.col + 1), eol_size);
6930 }
6931 }
6932 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006933 byte_count += line_count_info(ml_get(lnum), &word_count,
6934 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936
6937 /* Correction for when last line doesn't have an EOL. */
6938 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006939 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (VIsual_active)
6942 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006943 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {
6945 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006946 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006947 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6949 }
6950 else
6951 buf1[0] = NUL;
6952
Bram Moolenaar7c626922005-02-07 22:01:03 +00006953 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006954 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006955 vim_snprintf((char *)IObuff, IOSIZE,
6956 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 buf1, line_count_selected,
6958 (long)curbuf->b_ml.ml_line_count,
6959 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006960 byte_count_cursor, byte_count);
6961 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006962 vim_snprintf((char *)IObuff, IOSIZE,
6963 _("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 +00006964 buf1, line_count_selected,
6965 (long)curbuf->b_ml.ml_line_count,
6966 word_count_cursor, word_count,
6967 char_count_cursor, char_count,
6968 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 }
6970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
6972 p = ml_get_curline();
6973 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006974 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006976 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6977 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
Bram Moolenaar7c626922005-02-07 22:01:03 +00006979 if (char_count_cursor == byte_count_cursor
6980 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006981 vim_snprintf((char *)IObuff, IOSIZE,
6982 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 (char *)buf1, (char *)buf2,
6984 (long)curwin->w_cursor.lnum,
6985 (long)curbuf->b_ml.ml_line_count,
6986 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006987 byte_count_cursor, byte_count);
6988 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006989 vim_snprintf((char *)IObuff, IOSIZE,
6990 _("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 +00006991 (char *)buf1, (char *)buf2,
6992 (long)curwin->w_cursor.lnum,
6993 (long)curbuf->b_ml.ml_line_count,
6994 word_count_cursor, word_count,
6995 char_count_cursor, char_count,
6996 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998
6999#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00007000 byte_count = bomb_size();
7001 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00007003 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#endif
7005 /* Don't shorten this message, the user asked for it. */
7006 p = p_shm;
7007 p_shm = (char_u *)"";
7008 msg(IObuff);
7009 p_shm = p;
7010 }
7011}