blob: 05b1e1cc2e43a9099312473e3a444a858712b043 [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 Moolenaard009e862015-06-09 20:20:03 +02001962 (void)del_bytes((long)n, !virtual_op,
1963 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 else /* delete characters between lines */
1966 {
1967 pos_T curpos;
1968
1969 /* save deleted and changed lines for undo */
1970 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1971 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1972 return FAIL;
1973
1974 truncate_line(TRUE); /* delete from cursor to end of line */
1975
1976 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1977 ++curwin->w_cursor.lnum;
1978 del_lines((long)(oap->line_count - 2), FALSE);
1979
Bram Moolenaard009e862015-06-09 20:20:03 +02001980 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001981 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001982 curwin->w_cursor.col = 0;
1983 (void)del_bytes((long)n, !virtual_op,
1984 oap->op_type == OP_DELETE && !oap->is_VIsual);
1985 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1986 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
1988 }
1989
1990 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1991
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001992#ifdef FEAT_VIRTUALEDIT
1993setmarks:
1994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (oap->block_mode)
1996 {
1997 curbuf->b_op_end.lnum = oap->end.lnum;
1998 curbuf->b_op_end.col = oap->start.col;
1999 }
2000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 curbuf->b_op_end = oap->start;
2002 curbuf->b_op_start = oap->start;
2003
2004 return OK;
2005}
2006
2007#ifdef FEAT_MBYTE
2008/*
2009 * Adjust end of operating area for ending on a multi-byte character.
2010 * Used for deletion.
2011 */
2012 static void
2013mb_adjust_opend(oap)
2014 oparg_T *oap;
2015{
2016 char_u *p;
2017
2018 if (oap->inclusive)
2019 {
2020 p = ml_get(oap->end.lnum);
2021 oap->end.col += mb_tail_off(p, p + oap->end.col);
2022 }
2023}
2024#endif
2025
2026#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2027/*
2028 * Replace a whole area with one character.
2029 */
2030 int
2031op_replace(oap, c)
2032 oparg_T *oap;
2033 int c;
2034{
2035 int n, numc;
2036#ifdef FEAT_MBYTE
2037 int num_chars;
2038#endif
2039 char_u *newp, *oldp;
2040 size_t oldlen;
2041 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002042 char_u *after_p = NULL;
2043 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2046 return OK; /* nothing to do */
2047
Bram Moolenaard9820532013-11-04 01:41:17 +01002048 if (had_ctrl_v_cr)
2049 c = (c == -1 ? '\r' : '\n');
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#ifdef FEAT_MBYTE
2052 if (has_mbyte)
2053 mb_adjust_opend(oap);
2054#endif
2055
2056 if (u_save((linenr_T)(oap->start.lnum - 1),
2057 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2058 return FAIL;
2059
2060 /*
2061 * block mode replace
2062 */
2063 if (oap->block_mode)
2064 {
2065 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2066 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2067 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002068 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2070 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2071 continue; /* nothing to replace */
2072
2073 /* n == number of extra chars required
2074 * If we split a TAB, it may be replaced by several characters.
2075 * Thus the number of characters may increase!
2076 */
2077#ifdef FEAT_VIRTUALEDIT
2078 /* If the range starts in virtual space, count the initial
2079 * coladd offset as part of "startspaces" */
2080 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2081 {
2082 pos_T vpos;
2083
Bram Moolenaara1381de2009-11-03 15:44:21 +00002084 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 getvpos(&vpos, oap->start_vcol);
2086 bd.startspaces += vpos.coladd;
2087 n = bd.startspaces;
2088 }
2089 else
2090#endif
2091 /* allow for pre spaces */
2092 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2093
2094 /* allow for post spp */
2095 n += (bd.endspaces
2096#ifdef FEAT_VIRTUALEDIT
2097 && !bd.is_oneChar
2098#endif
2099 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2100 /* Figure out how many characters to replace. */
2101 numc = oap->end_vcol - oap->start_vcol + 1;
2102 if (bd.is_short && (!virtual_op || bd.is_MAX))
2103 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2104
2105#ifdef FEAT_MBYTE
2106 /* A double-wide character can be replaced only up to half the
2107 * times. */
2108 if ((*mb_char2cells)(c) > 1)
2109 {
2110 if ((numc & 1) && !bd.is_short)
2111 {
2112 ++bd.endspaces;
2113 ++n;
2114 }
2115 numc = numc / 2;
2116 }
2117
2118 /* Compute bytes needed, move character count to num_chars. */
2119 num_chars = numc;
2120 numc *= (*mb_char2len)(c);
2121#endif
2122 /* oldlen includes textlen, so don't double count */
2123 n += numc - bd.textlen;
2124
2125 oldp = ml_get_curline();
2126 oldlen = STRLEN(oldp);
2127 newp = alloc_check((unsigned)oldlen + 1 + n);
2128 if (newp == NULL)
2129 continue;
2130 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2131 /* copy up to deleted part */
2132 mch_memmove(newp, oldp, (size_t)bd.textcol);
2133 oldp += bd.textcol + bd.textlen;
2134 /* insert pre-spaces */
2135 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2136 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002137 /* -1/-2 is used for entering CR literally. */
2138 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002140#ifdef FEAT_MBYTE
2141 if (has_mbyte)
2142 {
2143 n = (int)STRLEN(newp);
2144 while (--num_chars >= 0)
2145 n += (*mb_char2bytes)(c, newp + n);
2146 }
2147 else
2148#endif
2149 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2150 if (!bd.is_short)
2151 {
2152 /* insert post-spaces */
2153 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2154 /* copy the part after the changed part */
2155 STRMOVE(newp + STRLEN(newp), oldp);
2156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002160 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002161 after_p = alloc_check(
2162 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002163 if (after_p != NULL)
2164 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 /* replace the line */
2167 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002168 if (after_p != NULL)
2169 {
2170 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2171 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2172 oap->end.lnum++;
2173 vim_free(after_p);
2174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 }
2176 }
2177 else
2178 {
2179 /*
2180 * MCHAR and MLINE motion replace.
2181 */
2182 if (oap->motion_type == MLINE)
2183 {
2184 oap->start.col = 0;
2185 curwin->w_cursor.col = 0;
2186 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2187 if (oap->end.col)
2188 --oap->end.col;
2189 }
2190 else if (!oap->inclusive)
2191 dec(&(oap->end));
2192
2193 while (ltoreq(curwin->w_cursor, oap->end))
2194 {
2195 n = gchar_cursor();
2196 if (n != NUL)
2197 {
2198#ifdef FEAT_MBYTE
2199 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2200 {
2201 /* This is slow, but it handles replacing a single-byte
2202 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002203 if (curwin->w_cursor.lnum == oap->end.lnum)
2204 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 n = State;
2206 State = REPLACE;
2207 ins_char(c);
2208 State = n;
2209 /* Backup to the replaced character. */
2210 dec_cursor();
2211 }
2212 else
2213#endif
2214 {
2215#ifdef FEAT_VIRTUALEDIT
2216 if (n == TAB)
2217 {
2218 int end_vcol = 0;
2219
2220 if (curwin->w_cursor.lnum == oap->end.lnum)
2221 {
2222 /* oap->end has to be recalculated when
2223 * the tab breaks */
2224 end_vcol = getviscol2(oap->end.col,
2225 oap->end.coladd);
2226 }
2227 coladvance_force(getviscol());
2228 if (curwin->w_cursor.lnum == oap->end.lnum)
2229 getvpos(&oap->end, end_vcol);
2230 }
2231#endif
2232 pchar(curwin->w_cursor, c);
2233 }
2234 }
2235#ifdef FEAT_VIRTUALEDIT
2236 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2237 {
2238 int virtcols = oap->end.coladd;
2239
2240 if (curwin->w_cursor.lnum == oap->start.lnum
2241 && oap->start.col == oap->end.col && oap->start.coladd)
2242 virtcols -= oap->start.coladd;
2243
2244 /* oap->end has been trimmed so it's effectively inclusive;
2245 * as a result an extra +1 must be counted so we don't
2246 * trample the NUL byte. */
2247 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2248 curwin->w_cursor.col -= (virtcols + 1);
2249 for (; virtcols >= 0; virtcols--)
2250 {
2251 pchar(curwin->w_cursor, c);
2252 if (inc(&curwin->w_cursor) == -1)
2253 break;
2254 }
2255 }
2256#endif
2257
2258 /* Advance to next character, stop at the end of the file. */
2259 if (inc_cursor() == -1)
2260 break;
2261 }
2262 }
2263
2264 curwin->w_cursor = oap->start;
2265 check_cursor();
2266 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2267
2268 /* Set "'[" and "']" marks. */
2269 curbuf->b_op_start = oap->start;
2270 curbuf->b_op_end = oap->end;
2271
2272 return OK;
2273}
2274#endif
2275
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002276static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2280 */
2281 void
2282op_tilde(oap)
2283 oparg_T *oap;
2284{
2285 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002287 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 if (u_save((linenr_T)(oap->start.lnum - 1),
2290 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2291 return;
2292
2293 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (oap->block_mode) /* Visual block mode */
2295 {
2296 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2297 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002298 int one_change;
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 block_prep(oap, &bd, pos.lnum, FALSE);
2301 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002302 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2303 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002304
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002305#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002306 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2309
Bram Moolenaar009b2592004-10-24 19:18:58 +00002310 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2311 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002313 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 if (did_change)
2318 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2319 }
2320 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 if (oap->motion_type == MLINE)
2323 {
2324 oap->start.col = 0;
2325 pos.col = 0;
2326 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2327 if (oap->end.col)
2328 --oap->end.col;
2329 }
2330 else if (!oap->inclusive)
2331 dec(&(oap->end));
2332
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002333 if (pos.lnum == oap->end.lnum)
2334 did_change = swapchars(oap->op_type, &pos,
2335 oap->end.col - pos.col + 1);
2336 else
2337 for (;;)
2338 {
2339 did_change |= swapchars(oap->op_type, &pos,
2340 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2341 (int)STRLEN(ml_get_pos(&pos)));
2342 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2343 break;
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (did_change)
2346 {
2347 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2348 0L);
2349#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002350 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 char_u *ptr;
2353 int count;
2354
2355 pos = oap->start;
2356 while (pos.lnum < oap->end.lnum)
2357 {
2358 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002359 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002360 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002362 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 pos.col = 0;
2364 pos.lnum++;
2365 }
2366 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2367 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002368 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002370 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372#endif
2373 }
2374 }
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (!did_change && oap->is_VIsual)
2377 /* No change: need to remove the Visual selection */
2378 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 /*
2381 * Set '[ and '] marks.
2382 */
2383 curbuf->b_op_start = oap->start;
2384 curbuf->b_op_end = oap->end;
2385
2386 if (oap->line_count > p_report)
2387 {
2388 if (oap->line_count == 1)
2389 MSG(_("1 line changed"));
2390 else
2391 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2392 }
2393}
2394
2395/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002396 * Invoke swapchar() on "length" bytes at position "pos".
2397 * "pos" is advanced to just after the changed characters.
2398 * "length" is rounded up to include the whole last multi-byte character.
2399 * Also works correctly when the number of bytes changes.
2400 * Returns TRUE if some character was changed.
2401 */
2402 static int
2403swapchars(op_type, pos, length)
2404 int op_type;
2405 pos_T *pos;
2406 int length;
2407{
2408 int todo;
2409 int did_change = 0;
2410
2411 for (todo = length; todo > 0; --todo)
2412 {
2413# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002414 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002415 {
2416 int len = (*mb_ptr2len)(ml_get_pos(pos));
2417
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002418 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002419 if (len > 0)
2420 todo -= len - 1;
2421 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002422# endif
2423 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002424 if (inc(pos) == -1) /* at end of file */
2425 break;
2426 }
2427 return did_change;
2428}
2429
2430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * If op_type == OP_UPPER: make uppercase,
2432 * if op_type == OP_LOWER: make lowercase,
2433 * if op_type == OP_ROT13: do rot13 encoding,
2434 * else swap case of character at 'pos'
2435 * returns TRUE when something actually changed.
2436 */
2437 int
2438swapchar(op_type, pos)
2439 int op_type;
2440 pos_T *pos;
2441{
2442 int c;
2443 int nc;
2444
2445 c = gchar_pos(pos);
2446
2447 /* Only do rot13 encoding for ASCII characters. */
2448 if (c >= 0x80 && op_type == OP_ROT13)
2449 return FALSE;
2450
2451#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002452 if (op_type == OP_UPPER && c == 0xdf
2453 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002454 {
2455 pos_T sp = curwin->w_cursor;
2456
2457 /* Special handling of German sharp s: change to "SS". */
2458 curwin->w_cursor = *pos;
2459 del_char(FALSE);
2460 ins_char('S');
2461 ins_char('S');
2462 curwin->w_cursor = sp;
2463 inc(pos);
2464 }
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2467 return FALSE;
2468#endif
2469 nc = c;
2470 if (MB_ISLOWER(c))
2471 {
2472 if (op_type == OP_ROT13)
2473 nc = ROT13(c, 'a');
2474 else if (op_type != OP_LOWER)
2475 nc = MB_TOUPPER(c);
2476 }
2477 else if (MB_ISUPPER(c))
2478 {
2479 if (op_type == OP_ROT13)
2480 nc = ROT13(c, 'A');
2481 else if (op_type != OP_UPPER)
2482 nc = MB_TOLOWER(c);
2483 }
2484 if (nc != c)
2485 {
2486#ifdef FEAT_MBYTE
2487 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2488 {
2489 pos_T sp = curwin->w_cursor;
2490
2491 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002492 /* don't use del_char(), it also removes composing chars */
2493 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 ins_char(nc);
2495 curwin->w_cursor = sp;
2496 }
2497 else
2498#endif
2499 pchar(*pos, nc);
2500 return TRUE;
2501 }
2502 return FALSE;
2503}
2504
2505#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2506/*
2507 * op_insert - Insert and append operators for Visual mode.
2508 */
2509 void
2510op_insert(oap, count1)
2511 oparg_T *oap;
2512 long count1;
2513{
2514 long ins_len, pre_textlen = 0;
2515 char_u *firstline, *ins_text;
2516 struct block_def bd;
2517 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002518 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
2520 /* edit() changes this - record it for OP_APPEND */
2521 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2522
2523 /* vis block is still marked. Get rid of it now. */
2524 curwin->w_cursor.lnum = oap->start.lnum;
2525 update_screen(INVERTED);
2526
2527 if (oap->block_mode)
2528 {
2529#ifdef FEAT_VIRTUALEDIT
2530 /* When 'virtualedit' is used, need to insert the extra spaces before
2531 * doing block_prep(). When only "block" is used, virtual edit is
2532 * already disabled, but still need it when calling
2533 * coladvance_force(). */
2534 if (curwin->w_cursor.coladd > 0)
2535 {
2536 int old_ve_flags = ve_flags;
2537
2538 ve_flags = VE_ALL;
2539 if (u_save_cursor() == FAIL)
2540 return;
2541 coladvance_force(oap->op_type == OP_APPEND
2542 ? oap->end_vcol + 1 : getviscol());
2543 if (oap->op_type == OP_APPEND)
2544 --curwin->w_cursor.col;
2545 ve_flags = old_ve_flags;
2546 }
2547#endif
2548 /* Get the info about the block before entering the text */
2549 block_prep(oap, &bd, oap->start.lnum, TRUE);
2550 firstline = ml_get(oap->start.lnum) + bd.textcol;
2551 if (oap->op_type == OP_APPEND)
2552 firstline += bd.textlen;
2553 pre_textlen = (long)STRLEN(firstline);
2554 }
2555
2556 if (oap->op_type == OP_APPEND)
2557 {
2558 if (oap->block_mode
2559#ifdef FEAT_VIRTUALEDIT
2560 && curwin->w_cursor.coladd == 0
2561#endif
2562 )
2563 {
2564 /* Move the cursor to the character right of the block. */
2565 curwin->w_set_curswant = TRUE;
2566 while (*ml_get_cursor() != NUL
2567 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2568 ++curwin->w_cursor.col;
2569 if (bd.is_short && !bd.is_MAX)
2570 {
2571 /* First line was too short, make it longer and adjust the
2572 * values in "bd". */
2573 if (u_save_cursor() == FAIL)
2574 return;
2575 for (i = 0; i < bd.endspaces; ++i)
2576 ins_char(' ');
2577 bd.textlen += bd.endspaces;
2578 }
2579 }
2580 else
2581 {
2582 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002583 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 /* Works just like an 'i'nsert on the next character. */
2586 if (!lineempty(curwin->w_cursor.lnum)
2587 && oap->start_vcol != oap->end_vcol)
2588 inc_cursor();
2589 }
2590 }
2591
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002592 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 edit(NUL, FALSE, (linenr_T)count1);
2594
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002595 /* When a tab was inserted, and the characters in front of the tab
2596 * have been converted to a tab as well, the column of the cursor
2597 * might have actually been reduced, so need to adjust here. */
2598 if (t1.lnum == curbuf->b_op_start_orig.lnum
2599 && lt(curbuf->b_op_start_orig, t1))
2600 oap->start = curbuf->b_op_start_orig;
2601
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002602 /* If user has moved off this line, we don't know what to do, so do
2603 * nothing.
2604 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2605 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 return;
2607
2608 if (oap->block_mode)
2609 {
2610 struct block_def bd2;
2611
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002612 /* The user may have moved the cursor before inserting something, try
2613 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002614 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 {
2616 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002617 && oap->start.col
2618#ifdef FEAT_VIRTUALEDIT
2619 + oap->start.coladd
2620#endif
2621 != curbuf->b_op_start_orig.col
2622#ifdef FEAT_VIRTUALEDIT
2623 + curbuf->b_op_start_orig.coladd
2624#endif
2625 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002626 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002627 int t = getviscol2(curbuf->b_op_start_orig.col,
2628 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002629 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002630 pre_textlen -= t - oap->start_vcol;
2631 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002632 }
2633 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002634 && oap->end.col
2635#ifdef FEAT_VIRTUALEDIT
2636 + oap->end.coladd
2637#endif
2638 >= curbuf->b_op_start_orig.col
2639#ifdef FEAT_VIRTUALEDIT
2640 + curbuf->b_op_start_orig.coladd
2641#endif
2642 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002643 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002644 int t = getviscol2(curbuf->b_op_start_orig.col,
2645 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002646 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002647 /* reset pre_textlen to the value of OP_INSERT */
2648 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002649 pre_textlen -= t - oap->start_vcol;
2650 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002651 oap->op_type = OP_INSERT;
2652 }
2653 }
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /*
2656 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002657 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * Don't do this when "$" used, end-of-line will have changed.
2659 */
2660 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2661 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2662 {
2663 if (oap->op_type == OP_APPEND)
2664 {
2665 pre_textlen += bd2.textlen - bd.textlen;
2666 if (bd2.endspaces)
2667 --bd2.textlen;
2668 }
2669 bd.textcol = bd2.textcol;
2670 bd.textlen = bd2.textlen;
2671 }
2672
2673 /*
2674 * Subsequent calls to ml_get() flush the firstline data - take a
2675 * copy of the required string.
2676 */
2677 firstline = ml_get(oap->start.lnum) + bd.textcol;
2678 if (oap->op_type == OP_APPEND)
2679 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002680 if (pre_textlen >= 0
2681 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 ins_text = vim_strnsave(firstline, (int)ins_len);
2684 if (ins_text != NULL)
2685 {
2686 /* block handled here */
2687 if (u_save(oap->start.lnum,
2688 (linenr_T)(oap->end.lnum + 1)) == OK)
2689 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2690 &bd);
2691
2692 curwin->w_cursor.col = oap->start.col;
2693 check_cursor();
2694 vim_free(ins_text);
2695 }
2696 }
2697 }
2698}
2699#endif
2700
2701/*
2702 * op_change - handle a change operation
2703 *
2704 * return TRUE if edit() returns because of a CTRL-O command
2705 */
2706 int
2707op_change(oap)
2708 oparg_T *oap;
2709{
2710 colnr_T l;
2711 int retval;
2712#ifdef FEAT_VISUALEXTRA
2713 long offset;
2714 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002715 long ins_len;
2716 long pre_textlen = 0;
2717 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 char_u *firstline;
2719 char_u *ins_text, *newp, *oldp;
2720 struct block_def bd;
2721#endif
2722
2723 l = oap->start.col;
2724 if (oap->motion_type == MLINE)
2725 {
2726 l = 0;
2727#ifdef FEAT_SMARTINDENT
2728 if (!p_paste && curbuf->b_p_si
2729# ifdef FEAT_CINDENT
2730 && !curbuf->b_p_cin
2731# endif
2732 )
2733 can_si = TRUE; /* It's like opening a new line, do si */
2734#endif
2735 }
2736
2737 /* First delete the text in the region. In an empty buffer only need to
2738 * save for undo */
2739 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2740 {
2741 if (u_save_cursor() == FAIL)
2742 return FALSE;
2743 }
2744 else if (op_delete(oap) == FAIL)
2745 return FALSE;
2746
2747 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2748 && !virtual_op)
2749 inc_cursor();
2750
2751#ifdef FEAT_VISUALEXTRA
2752 /* check for still on same line (<CR> in inserted text meaningless) */
2753 /* skip blank lines too */
2754 if (oap->block_mode)
2755 {
2756# ifdef FEAT_VIRTUALEDIT
2757 /* Add spaces before getting the current line length. */
2758 if (virtual_op && (curwin->w_cursor.coladd > 0
2759 || gchar_cursor() == NUL))
2760 coladvance_force(getviscol());
2761# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002762 firstline = ml_get(oap->start.lnum);
2763 pre_textlen = (long)STRLEN(firstline);
2764 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 bd.textcol = curwin->w_cursor.col;
2766 }
2767#endif
2768
2769#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2770 if (oap->motion_type == MLINE)
2771 fix_indent();
2772#endif
2773
2774 retval = edit(NUL, FALSE, (linenr_T)1);
2775
2776#ifdef FEAT_VISUALEXTRA
2777 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002778 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002780 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002782 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002784 /* Auto-indenting may have changed the indent. If the cursor was past
2785 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002787 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002789 long new_indent = (long)(skipwhite(firstline) - firstline);
2790
2791 pre_textlen += new_indent - pre_indent;
2792 bd.textcol += new_indent - pre_indent;
2793 }
2794
2795 ins_len = (long)STRLEN(firstline) - pre_textlen;
2796 if (ins_len > 0)
2797 {
2798 /* Subsequent calls to ml_get() flush the firstline data - take a
2799 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2801 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002802 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2804 linenr++)
2805 {
2806 block_prep(oap, &bd, linenr, TRUE);
2807 if (!bd.is_short || virtual_op)
2808 {
2809# ifdef FEAT_VIRTUALEDIT
2810 pos_T vpos;
2811
2812 /* If the block starts in virtual space, count the
2813 * initial coladd offset as part of "startspaces" */
2814 if (bd.is_short)
2815 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002816 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 else
2820 vpos.coladd = 0;
2821# endif
2822 oldp = ml_get(linenr);
2823 newp = alloc_check((unsigned)(STRLEN(oldp)
2824# ifdef FEAT_VIRTUALEDIT
2825 + vpos.coladd
2826# endif
2827 + ins_len + 1));
2828 if (newp == NULL)
2829 continue;
2830 /* copy up to block start */
2831 mch_memmove(newp, oldp, (size_t)bd.textcol);
2832 offset = bd.textcol;
2833# ifdef FEAT_VIRTUALEDIT
2834 copy_spaces(newp + offset, (size_t)vpos.coladd);
2835 offset += vpos.coladd;
2836# endif
2837 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2838 offset += ins_len;
2839 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002840 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 ml_replace(linenr, newp, FALSE);
2842 }
2843 }
2844 check_cursor();
2845
2846 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2847 }
2848 vim_free(ins_text);
2849 }
2850 }
2851#endif
2852
2853 return retval;
2854}
2855
2856/*
2857 * set all the yank registers to empty (called from main())
2858 */
2859 void
2860init_yank()
2861{
2862 int i;
2863
2864 for (i = 0; i < NUM_REGISTERS; ++i)
2865 y_regs[i].y_array = NULL;
2866}
2867
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002868#if defined(EXITFREE) || defined(PROTO)
2869 void
2870clear_registers()
2871{
2872 int i;
2873
2874 for (i = 0; i < NUM_REGISTERS; ++i)
2875 {
2876 y_current = &y_regs[i];
2877 if (y_current->y_array != NULL)
2878 free_yank_all();
2879 }
2880}
2881#endif
2882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883/*
2884 * Free "n" lines from the current yank register.
2885 * Called for normal freeing and in case of error.
2886 */
2887 static void
2888free_yank(n)
2889 long n;
2890{
2891 if (y_current->y_array != NULL)
2892 {
2893 long i;
2894
2895 for (i = n; --i >= 0; )
2896 {
2897#ifdef AMIGA /* only for very slow machines */
2898 if ((i & 1023) == 1023) /* this may take a while */
2899 {
2900 /*
2901 * This message should never cause a hit-return message.
2902 * Overwrite this message with any next message.
2903 */
2904 ++no_wait_return;
2905 smsg((char_u *)_("freeing %ld lines"), i + 1);
2906 --no_wait_return;
2907 msg_didout = FALSE;
2908 msg_col = 0;
2909 }
2910#endif
2911 vim_free(y_current->y_array[i]);
2912 }
2913 vim_free(y_current->y_array);
2914 y_current->y_array = NULL;
2915#ifdef AMIGA
2916 if (n >= 1000)
2917 MSG("");
2918#endif
2919 }
2920}
2921
2922 static void
2923free_yank_all()
2924{
2925 free_yank(y_current->y_size);
2926}
2927
2928/*
2929 * Yank the text between "oap->start" and "oap->end" into a yank register.
2930 * If we are to append (uppercase register), we first yank into a new yank
2931 * register and then concatenate the old and the new one (so we keep the old
2932 * one in case of out-of-memory).
2933 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002934 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 int
2937op_yank(oap, deleting, mess)
2938 oparg_T *oap;
2939 int deleting;
2940 int mess;
2941{
2942 long y_idx; /* index in y_array[] */
2943 struct yankreg *curr; /* copy of y_current */
2944 struct yankreg newreg; /* new yank register when appending */
2945 char_u **new_ptr;
2946 linenr_T lnum; /* current line number */
2947 long j;
2948 int yanktype = oap->motion_type;
2949 long yanklines = oap->line_count;
2950 linenr_T yankendlnum = oap->end.lnum;
2951 char_u *p;
2952 char_u *pnew;
2953 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002954#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002955 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /* check for read-only register */
2959 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2960 {
2961 beep_flush();
2962 return FAIL;
2963 }
2964 if (oap->regname == '_') /* black hole: nothing to do */
2965 return OK;
2966
2967#ifdef FEAT_CLIPBOARD
2968 if (!clip_star.available && oap->regname == '*')
2969 oap->regname = 0;
2970 else if (!clip_plus.available && oap->regname == '+')
2971 oap->regname = 0;
2972#endif
2973
2974 if (!deleting) /* op_delete() already set y_current */
2975 get_yank_register(oap->regname, TRUE);
2976
2977 curr = y_current;
2978 /* append to existing contents */
2979 if (y_append && y_current->y_array != NULL)
2980 y_current = &newreg;
2981 else
2982 free_yank_all(); /* free previously yanked lines */
2983
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002984 /*
2985 * If the cursor was in column 1 before and after the movement, and the
2986 * operator is not inclusive, the yank is always linewise.
2987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if ( oap->motion_type == MCHAR
2989 && oap->start.col == 0
2990 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002992 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 && oap->end.col == 0
2994 && yanklines > 1)
2995 {
2996 yanktype = MLINE;
2997 --yankendlnum;
2998 --yanklines;
2999 }
3000
3001 y_current->y_size = yanklines;
3002 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3005 yanklines), TRUE);
3006
3007 if (y_current->y_array == NULL)
3008 {
3009 y_current = curr;
3010 return FAIL;
3011 }
3012
3013 y_idx = 0;
3014 lnum = oap->start.lnum;
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (oap->block_mode)
3017 {
3018 /* Visual block mode */
3019 y_current->y_type = MBLOCK; /* set the yank register type */
3020 y_current->y_width = oap->end_vcol - oap->start_vcol;
3021
3022 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3023 y_current->y_width--;
3024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3027 {
3028 switch (y_current->y_type)
3029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 case MBLOCK:
3031 block_prep(oap, &bd, lnum, FALSE);
3032 if (yank_copy_line(&bd, y_idx) == FAIL)
3033 goto fail;
3034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 case MLINE:
3037 if ((y_current->y_array[y_idx] =
3038 vim_strsave(ml_get(lnum))) == NULL)
3039 goto fail;
3040 break;
3041
3042 case MCHAR:
3043 {
3044 colnr_T startcol = 0, endcol = MAXCOL;
3045#ifdef FEAT_VIRTUALEDIT
3046 int is_oneChar = FALSE;
3047 colnr_T cs, ce;
3048#endif
3049 p = ml_get(lnum);
3050 bd.startspaces = 0;
3051 bd.endspaces = 0;
3052
3053 if (lnum == oap->start.lnum)
3054 {
3055 startcol = oap->start.col;
3056#ifdef FEAT_VIRTUALEDIT
3057 if (virtual_op)
3058 {
3059 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3060 if (ce != cs && oap->start.coladd > 0)
3061 {
3062 /* Part of a tab selected -- but don't
3063 * double-count it. */
3064 bd.startspaces = (ce - cs + 1)
3065 - oap->start.coladd;
3066 startcol++;
3067 }
3068 }
3069#endif
3070 }
3071
3072 if (lnum == oap->end.lnum)
3073 {
3074 endcol = oap->end.col;
3075#ifdef FEAT_VIRTUALEDIT
3076 if (virtual_op)
3077 {
3078 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3079 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3080# ifdef FEAT_MBYTE
3081 /* Don't add space for double-wide
3082 * char; endcol will be on last byte
3083 * of multi-byte char. */
3084 && (*mb_head_off)(p, p + endcol) == 0
3085# endif
3086 ))
3087 {
3088 if (oap->start.lnum == oap->end.lnum
3089 && oap->start.col == oap->end.col)
3090 {
3091 /* Special case: inside a single char */
3092 is_oneChar = TRUE;
3093 bd.startspaces = oap->end.coladd
3094 - oap->start.coladd + oap->inclusive;
3095 endcol = startcol;
3096 }
3097 else
3098 {
3099 bd.endspaces = oap->end.coladd
3100 + oap->inclusive;
3101 endcol -= oap->inclusive;
3102 }
3103 }
3104 }
3105#endif
3106 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003107 if (endcol == MAXCOL)
3108 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (startcol > endcol
3110#ifdef FEAT_VIRTUALEDIT
3111 || is_oneChar
3112#endif
3113 )
3114 bd.textlen = 0;
3115 else
3116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 bd.textlen = endcol - startcol + oap->inclusive;
3118 }
3119 bd.textstart = p + startcol;
3120 if (yank_copy_line(&bd, y_idx) == FAIL)
3121 goto fail;
3122 break;
3123 }
3124 /* NOTREACHED */
3125 }
3126 }
3127
3128 if (curr != y_current) /* append the new block to the old block */
3129 {
3130 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3131 (curr->y_size + y_current->y_size)), TRUE);
3132 if (new_ptr == NULL)
3133 goto fail;
3134 for (j = 0; j < curr->y_size; ++j)
3135 new_ptr[j] = curr->y_array[j];
3136 vim_free(curr->y_array);
3137 curr->y_array = new_ptr;
3138
3139 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3140 curr->y_type = MLINE;
3141
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003142 /* Concatenate the last line of the old block with the first line of
3143 * the new block, unless being Vi compatible. */
3144 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3147 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3148 if (pnew == NULL)
3149 {
3150 y_idx = y_current->y_size - 1;
3151 goto fail;
3152 }
3153 STRCPY(pnew, curr->y_array[--j]);
3154 STRCAT(pnew, y_current->y_array[0]);
3155 vim_free(curr->y_array[j]);
3156 vim_free(y_current->y_array[0]);
3157 curr->y_array[j++] = pnew;
3158 y_idx = 1;
3159 }
3160 else
3161 y_idx = 0;
3162 while (y_idx < y_current->y_size)
3163 curr->y_array[j++] = y_current->y_array[y_idx++];
3164 curr->y_size = j;
3165 vim_free(y_current->y_array);
3166 y_current = curr;
3167 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003168 if (curwin->w_p_rnu)
3169 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (mess) /* Display message about yank? */
3171 {
3172 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 && yanklines == 1)
3175 yanklines = 0;
3176 /* Some versions of Vi use ">=" here, some don't... */
3177 if (yanklines > p_report)
3178 {
3179 /* redisplay now, so message is not deleted */
3180 update_topline_redraw();
3181 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003182 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003183 if (oap->block_mode)
3184 MSG(_("block of 1 line yanked"));
3185 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003186 MSG(_("1 line yanked"));
3187 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003188 else if (oap->block_mode)
3189 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191 smsg((char_u *)_("%ld lines yanked"), yanklines);
3192 }
3193 }
3194
3195 /*
3196 * Set "'[" and "']" marks.
3197 */
3198 curbuf->b_op_start = oap->start;
3199 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003200 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003201 {
3202 curbuf->b_op_start.col = 0;
3203 curbuf->b_op_end.col = MAXCOL;
3204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206#ifdef FEAT_CLIPBOARD
3207 /*
3208 * If we were yanking to the '*' register, send result to clipboard.
3209 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3210 * to the '*' register.
3211 */
3212 if (clip_star.available
3213 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003214 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003215 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
3217 if (curr != &(y_regs[STAR_REGISTER]))
3218 /* Copy the text from register 0 to the clipboard register. */
3219 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3220
3221 clip_own_selection(&clip_star);
3222 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003223# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003224 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227
3228# ifdef FEAT_X11
3229 /*
3230 * If we were yanking to the '+' register, send result to selection.
3231 * Also copy to the '*' register, in case auto-select is off.
3232 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003233 if (clip_plus.available
3234 && (curr == &(y_regs[PLUS_REGISTER])
3235 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003236 && ((clip_unnamed | clip_unnamed_saved) &
3237 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003239 if (curr != &(y_regs[PLUS_REGISTER]))
3240 /* Copy the text from register 0 to the clipboard register. */
3241 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 clip_own_selection(&clip_plus);
3244 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003245 if (!clip_isautosel_star() && !did_star
3246 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
3248 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3249 clip_own_selection(&clip_star);
3250 clip_gen_set_selection(&clip_star);
3251 }
3252 }
3253# endif
3254#endif
3255
3256 return OK;
3257
3258fail: /* free the allocated lines */
3259 free_yank(y_idx + 1);
3260 y_current = curr;
3261 return FAIL;
3262}
3263
3264 static int
3265yank_copy_line(bd, y_idx)
3266 struct block_def *bd;
3267 long y_idx;
3268{
3269 char_u *pnew;
3270
3271 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3272 == NULL)
3273 return FAIL;
3274 y_current->y_array[y_idx] = pnew;
3275 copy_spaces(pnew, (size_t)bd->startspaces);
3276 pnew += bd->startspaces;
3277 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3278 pnew += bd->textlen;
3279 copy_spaces(pnew, (size_t)bd->endspaces);
3280 pnew += bd->endspaces;
3281 *pnew = NUL;
3282 return OK;
3283}
3284
3285#ifdef FEAT_CLIPBOARD
3286/*
3287 * Make a copy of the y_current register to register "reg".
3288 */
3289 static void
3290copy_yank_reg(reg)
3291 struct yankreg *reg;
3292{
3293 struct yankreg *curr = y_current;
3294 long j;
3295
3296 y_current = reg;
3297 free_yank_all();
3298 *y_current = *curr;
3299 y_current->y_array = (char_u **)lalloc_clear(
3300 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3301 if (y_current->y_array == NULL)
3302 y_current->y_size = 0;
3303 else
3304 for (j = 0; j < y_current->y_size; ++j)
3305 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3306 {
3307 free_yank(j);
3308 y_current->y_size = 0;
3309 break;
3310 }
3311 y_current = curr;
3312}
3313#endif
3314
3315/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003316 * Put contents of register "regname" into the text.
3317 * Caller must check "regname" to be valid!
3318 * "flags": PUT_FIXINDENT make indent look nice
3319 * PUT_CURSEND leave cursor after end of new text
3320 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
3322 void
3323do_put(regname, dir, count, flags)
3324 int regname;
3325 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3326 long count;
3327 int flags;
3328{
3329 char_u *ptr;
3330 char_u *newp, *oldp;
3331 int yanklen;
3332 int totlen = 0; /* init for gcc */
3333 linenr_T lnum;
3334 colnr_T col;
3335 long i; /* index in y_array[] */
3336 int y_type;
3337 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 int oldlen;
3339 long y_width = 0;
3340 colnr_T vcol;
3341 int delcount;
3342 int incr = 0;
3343 long j;
3344 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 char_u **y_array = NULL;
3346 long nr_lines = 0;
3347 pos_T new_cursor;
3348 int indent;
3349 int orig_indent = 0; /* init for gcc */
3350 int indent_diff = 0; /* init for gcc */
3351 int first_indent = TRUE;
3352 int lendiff = 0;
3353 pos_T old_pos;
3354 char_u *insert_string = NULL;
3355 int allocated = FALSE;
3356 long cnt;
3357
3358#ifdef FEAT_CLIPBOARD
3359 /* Adjust register name for "unnamed" in 'clipboard'. */
3360 adjust_clip_reg(&regname);
3361 (void)may_get_selection(regname);
3362#endif
3363
3364 if (flags & PUT_FIXINDENT)
3365 orig_indent = get_indent();
3366
3367 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3368 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3369
3370 /*
3371 * Using inserted text works differently, because the register includes
3372 * special characters (newlines, etc.).
3373 */
3374 if (regname == '.')
3375 {
3376 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3377 (count == -1 ? 'O' : 'i')), count, FALSE);
3378 /* Putting the text is done later, so can't really move the cursor to
3379 * the next character. Use "l" to simulate it. */
3380 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3381 stuffcharReadbuff('l');
3382 return;
3383 }
3384
3385 /*
3386 * For special registers '%' (file name), '#' (alternate file name) and
3387 * ':' (last command line), etc. we have to create a fake yank register.
3388 */
3389 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3390 {
3391 if (insert_string == NULL)
3392 return;
3393 }
3394
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003395#ifdef FEAT_AUTOCMD
3396 /* Autocommands may be executed when saving lines for undo, which may make
3397 * y_array invalid. Start undo now to avoid that. */
3398 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3399#endif
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (insert_string != NULL)
3402 {
3403 y_type = MCHAR;
3404#ifdef FEAT_EVAL
3405 if (regname == '=')
3406 {
3407 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003408 * characters.
3409 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 for (;;)
3411 {
3412 y_size = 0;
3413 ptr = insert_string;
3414 while (ptr != NULL)
3415 {
3416 if (y_array != NULL)
3417 y_array[y_size] = ptr;
3418 ++y_size;
3419 ptr = vim_strchr(ptr, '\n');
3420 if (ptr != NULL)
3421 {
3422 if (y_array != NULL)
3423 *ptr = NUL;
3424 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003425 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (*ptr == NUL)
3427 {
3428 y_type = MLINE;
3429 break;
3430 }
3431 }
3432 }
3433 if (y_array != NULL)
3434 break;
3435 y_array = (char_u **)alloc((unsigned)
3436 (y_size * sizeof(char_u *)));
3437 if (y_array == NULL)
3438 goto end;
3439 }
3440 }
3441 else
3442#endif
3443 {
3444 y_size = 1; /* use fake one-line yank register */
3445 y_array = &insert_string;
3446 }
3447 }
3448 else
3449 {
3450 get_yank_register(regname, FALSE);
3451
3452 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 y_size = y_current->y_size;
3455 y_array = y_current->y_array;
3456 }
3457
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (y_type == MLINE)
3459 {
3460 if (flags & PUT_LINE_SPLIT)
3461 {
3462 /* "p" or "P" in Visual mode: split the lines to put the text in
3463 * between. */
3464 if (u_save_cursor() == FAIL)
3465 goto end;
3466 ptr = vim_strsave(ml_get_cursor());
3467 if (ptr == NULL)
3468 goto end;
3469 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3470 vim_free(ptr);
3471
3472 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3473 if (ptr == NULL)
3474 goto end;
3475 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3476 ++nr_lines;
3477 dir = FORWARD;
3478 }
3479 if (flags & PUT_LINE_FORWARD)
3480 {
3481 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003482 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 dir = FORWARD;
3484 }
3485 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3486 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3490 y_type = MLINE;
3491
3492 if (y_size == 0 || y_array == NULL)
3493 {
3494 EMSG2(_("E353: Nothing in register %s"),
3495 regname == 0 ? (char_u *)"\"" : transchar(regname));
3496 goto end;
3497 }
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (y_type == MBLOCK)
3500 {
3501 lnum = curwin->w_cursor.lnum + y_size + 1;
3502 if (lnum > curbuf->b_ml.ml_line_count)
3503 lnum = curbuf->b_ml.ml_line_count + 1;
3504 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3505 goto end;
3506 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003507 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 lnum = curwin->w_cursor.lnum;
3510#ifdef FEAT_FOLDING
3511 /* Correct line number for closed fold. Don't move the cursor yet,
3512 * u_save() uses it. */
3513 if (dir == BACKWARD)
3514 (void)hasFolding(lnum, &lnum, NULL);
3515 else
3516 (void)hasFolding(lnum, NULL, &lnum);
3517#endif
3518 if (dir == FORWARD)
3519 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003520 /* In an empty buffer the empty line is going to be replaced, include
3521 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003522 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 goto end;
3524#ifdef FEAT_FOLDING
3525 if (dir == FORWARD)
3526 curwin->w_cursor.lnum = lnum - 1;
3527 else
3528 curwin->w_cursor.lnum = lnum;
3529 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3530#endif
3531 }
3532 else if (u_save_cursor() == FAIL)
3533 goto end;
3534
3535 yanklen = (int)STRLEN(y_array[0]);
3536
3537#ifdef FEAT_VIRTUALEDIT
3538 if (ve_flags == VE_ALL && y_type == MCHAR)
3539 {
3540 if (gchar_cursor() == TAB)
3541 {
3542 /* Don't need to insert spaces when "p" on the last position of a
3543 * tab or "P" on the first position. */
3544 if (dir == FORWARD
3545 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3546 : curwin->w_cursor.coladd > 0)
3547 coladvance_force(getviscol());
3548 else
3549 curwin->w_cursor.coladd = 0;
3550 }
3551 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3552 coladvance_force(getviscol() + (dir == FORWARD));
3553 }
3554#endif
3555
3556 lnum = curwin->w_cursor.lnum;
3557 col = curwin->w_cursor.col;
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * Block mode
3561 */
3562 if (y_type == MBLOCK)
3563 {
3564 char c = gchar_cursor();
3565 colnr_T endcol2 = 0;
3566
3567 if (dir == FORWARD && c != NUL)
3568 {
3569#ifdef FEAT_VIRTUALEDIT
3570 if (ve_flags == VE_ALL)
3571 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3572 else
3573#endif
3574 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3575
3576#ifdef FEAT_MBYTE
3577 if (has_mbyte)
3578 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003579 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 else
3581#endif
3582#ifdef FEAT_VIRTUALEDIT
3583 if (c != TAB || ve_flags != VE_ALL)
3584#endif
3585 ++curwin->w_cursor.col;
3586 ++col;
3587 }
3588 else
3589 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3590
3591#ifdef FEAT_VIRTUALEDIT
3592 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003593 if (ve_flags == VE_ALL
3594 && (curwin->w_cursor.coladd > 0
3595 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 if (dir == FORWARD && c == NUL)
3598 ++col;
3599 if (dir != FORWARD && c != NUL)
3600 ++curwin->w_cursor.col;
3601 if (c == TAB)
3602 {
3603 if (dir == BACKWARD && curwin->w_cursor.col)
3604 curwin->w_cursor.col--;
3605 if (dir == FORWARD && col - 1 == endcol2)
3606 curwin->w_cursor.col++;
3607 }
3608 }
3609 curwin->w_cursor.coladd = 0;
3610#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003611 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 for (i = 0; i < y_size; ++i)
3613 {
3614 int spaces;
3615 char shortline;
3616
3617 bd.startspaces = 0;
3618 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 vcol = 0;
3620 delcount = 0;
3621
3622 /* add a new line */
3623 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3624 {
3625 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3626 (colnr_T)1, FALSE) == FAIL)
3627 break;
3628 ++nr_lines;
3629 }
3630 /* get the old line and advance to the position to insert at */
3631 oldp = ml_get_curline();
3632 oldlen = (int)STRLEN(oldp);
3633 for (ptr = oldp; vcol < col && *ptr; )
3634 {
3635 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003636 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 vcol += incr;
3638 }
3639 bd.textcol = (colnr_T)(ptr - oldp);
3640
3641 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3642
3643 if (vcol < col) /* line too short, padd with spaces */
3644 bd.startspaces = col - vcol;
3645 else if (vcol > col)
3646 {
3647 bd.endspaces = vcol - col;
3648 bd.startspaces = incr - bd.endspaces;
3649 --bd.textcol;
3650 delcount = 1;
3651#ifdef FEAT_MBYTE
3652 if (has_mbyte)
3653 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3654#endif
3655 if (oldp[bd.textcol] != TAB)
3656 {
3657 /* Only a Tab can be split into spaces. Other
3658 * characters will have to be moved to after the
3659 * block, causing misalignment. */
3660 delcount = 0;
3661 bd.endspaces = 0;
3662 }
3663 }
3664
3665 yanklen = (int)STRLEN(y_array[i]);
3666
3667 /* calculate number of spaces required to fill right side of block*/
3668 spaces = y_width + 1;
3669 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003670 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (spaces < 0)
3672 spaces = 0;
3673
3674 /* insert the new text */
3675 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3676 newp = alloc_check((unsigned)totlen + oldlen + 1);
3677 if (newp == NULL)
3678 break;
3679 /* copy part up to cursor to new line */
3680 ptr = newp;
3681 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3682 ptr += bd.textcol;
3683 /* may insert some spaces before the new text */
3684 copy_spaces(ptr, (size_t)bd.startspaces);
3685 ptr += bd.startspaces;
3686 /* insert the new text */
3687 for (j = 0; j < count; ++j)
3688 {
3689 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3690 ptr += yanklen;
3691
3692 /* insert block's trailing spaces only if there's text behind */
3693 if ((j < count - 1 || !shortline) && spaces)
3694 {
3695 copy_spaces(ptr, (size_t)spaces);
3696 ptr += spaces;
3697 }
3698 }
3699 /* may insert some spaces after the new text */
3700 copy_spaces(ptr, (size_t)bd.endspaces);
3701 ptr += bd.endspaces;
3702 /* move the text after the cursor to the end of the line. */
3703 mch_memmove(ptr, oldp + bd.textcol + delcount,
3704 (size_t)(oldlen - bd.textcol - delcount + 1));
3705 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3706
3707 ++curwin->w_cursor.lnum;
3708 if (i == 0)
3709 curwin->w_cursor.col += bd.startspaces;
3710 }
3711
3712 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3713
3714 /* Set '[ mark. */
3715 curbuf->b_op_start = curwin->w_cursor;
3716 curbuf->b_op_start.lnum = lnum;
3717
3718 /* adjust '] mark */
3719 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3720 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003721# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (flags & PUT_CURSEND)
3725 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003726 colnr_T len;
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 curwin->w_cursor = curbuf->b_op_end;
3729 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003730
3731 /* in Insert mode we might be after the NUL, correct for that */
3732 len = (colnr_T)STRLEN(ml_get_curline());
3733 if (curwin->w_cursor.col > len)
3734 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736 else
3737 curwin->w_cursor.lnum = lnum;
3738 }
3739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
3741 /*
3742 * Character or Line mode
3743 */
3744 if (y_type == MCHAR)
3745 {
3746 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3747 * char */
3748 if (dir == FORWARD && gchar_cursor() != NUL)
3749 {
3750#ifdef FEAT_MBYTE
3751 if (has_mbyte)
3752 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003753 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 /* put it on the next of the multi-byte character. */
3756 col += bytelen;
3757 if (yanklen)
3758 {
3759 curwin->w_cursor.col += bytelen;
3760 curbuf->b_op_end.col += bytelen;
3761 }
3762 }
3763 else
3764#endif
3765 {
3766 ++col;
3767 if (yanklen)
3768 {
3769 ++curwin->w_cursor.col;
3770 ++curbuf->b_op_end.col;
3771 }
3772 }
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 curbuf->b_op_start = curwin->w_cursor;
3775 }
3776 /*
3777 * Line mode: BACKWARD is the same as FORWARD on the previous line
3778 */
3779 else if (dir == BACKWARD)
3780 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003781 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 /*
3784 * simple case: insert into current line
3785 */
3786 if (y_type == MCHAR && y_size == 1)
3787 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003788 do {
3789 totlen = count * yanklen;
3790 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003792 oldp = ml_get(lnum);
3793 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3794 if (newp == NULL)
3795 goto end; /* alloc() gave an error message */
3796 mch_memmove(newp, oldp, (size_t)col);
3797 ptr = newp + col;
3798 for (i = 0; i < count; ++i)
3799 {
3800 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3801 ptr += yanklen;
3802 }
3803 STRMOVE(ptr, oldp + col);
3804 ml_replace(lnum, newp, FALSE);
3805 /* Place cursor on last putted char. */
3806 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003807 {
3808 /* make sure curwin->w_virtcol is updated */
3809 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003810 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003813 if (VIsual_active)
3814 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003815 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003816
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003817 if (VIsual_active) /* reset lnum to the last visual line */
3818 lnum--;
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 curbuf->b_op_end = curwin->w_cursor;
3821 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3822 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3823 ++curwin->w_cursor.col;
3824 changed_bytes(lnum, col);
3825 }
3826 else
3827 {
3828 /*
3829 * Insert at least one line. When y_type is MCHAR, break the first
3830 * line in two.
3831 */
3832 for (cnt = 1; cnt <= count; ++cnt)
3833 {
3834 i = 0;
3835 if (y_type == MCHAR)
3836 {
3837 /*
3838 * Split the current line in two at the insert position.
3839 * First insert y_array[size - 1] in front of second line.
3840 * Then append y_array[0] to first line.
3841 */
3842 lnum = new_cursor.lnum;
3843 ptr = ml_get(lnum) + col;
3844 totlen = (int)STRLEN(y_array[y_size - 1]);
3845 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3846 if (newp == NULL)
3847 goto error;
3848 STRCPY(newp, y_array[y_size - 1]);
3849 STRCAT(newp, ptr);
3850 /* insert second line */
3851 ml_append(lnum, newp, (colnr_T)0, FALSE);
3852 vim_free(newp);
3853
3854 oldp = ml_get(lnum);
3855 newp = alloc_check((unsigned)(col + yanklen + 1));
3856 if (newp == NULL)
3857 goto error;
3858 /* copy first part of line */
3859 mch_memmove(newp, oldp, (size_t)col);
3860 /* append to first line */
3861 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3862 ml_replace(lnum, newp, FALSE);
3863
3864 curwin->w_cursor.lnum = lnum;
3865 i = 1;
3866 }
3867
3868 for (; i < y_size; ++i)
3869 {
3870 if ((y_type != MCHAR || i < y_size - 1)
3871 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3872 == FAIL)
3873 goto error;
3874 lnum++;
3875 ++nr_lines;
3876 if (flags & PUT_FIXINDENT)
3877 {
3878 old_pos = curwin->w_cursor;
3879 curwin->w_cursor.lnum = lnum;
3880 ptr = ml_get(lnum);
3881 if (cnt == count && i == y_size - 1)
3882 lendiff = (int)STRLEN(ptr);
3883#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3884 if (*ptr == '#' && preprocs_left())
3885 indent = 0; /* Leave # lines at start */
3886 else
3887#endif
3888 if (*ptr == NUL)
3889 indent = 0; /* Ignore empty lines */
3890 else if (first_indent)
3891 {
3892 indent_diff = orig_indent - get_indent();
3893 indent = orig_indent;
3894 first_indent = FALSE;
3895 }
3896 else if ((indent = get_indent() + indent_diff) < 0)
3897 indent = 0;
3898 (void)set_indent(indent, 0);
3899 curwin->w_cursor = old_pos;
3900 /* remember how many chars were removed */
3901 if (cnt == count && i == y_size - 1)
3902 lendiff -= (int)STRLEN(ml_get(lnum));
3903 }
3904 }
3905 }
3906
3907error:
3908 /* Adjust marks. */
3909 if (y_type == MLINE)
3910 {
3911 curbuf->b_op_start.col = 0;
3912 if (dir == FORWARD)
3913 curbuf->b_op_start.lnum++;
3914 }
3915 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3916 (linenr_T)MAXLNUM, nr_lines, 0L);
3917
3918 /* note changed text for displaying and folding */
3919 if (y_type == MCHAR)
3920 changed_lines(curwin->w_cursor.lnum, col,
3921 curwin->w_cursor.lnum + 1, nr_lines);
3922 else
3923 changed_lines(curbuf->b_op_start.lnum, 0,
3924 curbuf->b_op_start.lnum, nr_lines);
3925
3926 /* put '] mark at last inserted character */
3927 curbuf->b_op_end.lnum = lnum;
3928 /* correct length for change in indent */
3929 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3930 if (col > 1)
3931 curbuf->b_op_end.col = col - 1;
3932 else
3933 curbuf->b_op_end.col = 0;
3934
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003935 if (flags & PUT_CURSLINE)
3936 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003937 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003938 curwin->w_cursor.lnum = lnum;
3939 beginline(BL_WHITE | BL_FIX);
3940 }
3941 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
3943 /* put cursor after inserted text */
3944 if (y_type == MLINE)
3945 {
3946 if (lnum >= curbuf->b_ml.ml_line_count)
3947 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3948 else
3949 curwin->w_cursor.lnum = lnum + 1;
3950 curwin->w_cursor.col = 0;
3951 }
3952 else
3953 {
3954 curwin->w_cursor.lnum = lnum;
3955 curwin->w_cursor.col = col;
3956 }
3957 }
3958 else if (y_type == MLINE)
3959 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003960 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 curwin->w_cursor.col = 0;
3962 if (dir == FORWARD)
3963 ++curwin->w_cursor.lnum;
3964 beginline(BL_WHITE | BL_FIX);
3965 }
3966 else /* put cursor on first inserted character */
3967 curwin->w_cursor = new_cursor;
3968 }
3969 }
3970
3971 msgmore(nr_lines);
3972 curwin->w_set_curswant = TRUE;
3973
3974end:
3975 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003977 if (regname == '=')
3978 vim_free(y_array);
3979
Bram Moolenaar033d8882013-09-25 23:24:57 +02003980 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003981
Bram Moolenaar677ee682005-01-27 14:41:15 +00003982 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003983 adjust_cursor_eol();
3984}
3985
3986/*
3987 * When the cursor is on the NUL past the end of the line and it should not be
3988 * there move it left.
3989 */
3990 void
3991adjust_cursor_eol()
3992{
3993 if (curwin->w_cursor.col > 0
3994 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003995#ifdef FEAT_VIRTUALEDIT
3996 && (ve_flags & VE_ONEMORE) == 0
3997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 && !(restart_edit || (State & INSERT)))
3999 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004000 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004001 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#ifdef FEAT_VIRTUALEDIT
4004 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004005 {
4006 colnr_T scol, ecol;
4007
4008 /* Coladd is set to the width of the last character. */
4009 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4010 curwin->w_cursor.coladd = ecol - scol + 1;
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#endif
4013 }
4014}
4015
4016#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4017/*
4018 * Return TRUE if lines starting with '#' should be left aligned.
4019 */
4020 int
4021preprocs_left()
4022{
4023 return
4024# ifdef FEAT_SMARTINDENT
4025# ifdef FEAT_CINDENT
4026 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4027# else
4028 curbuf->b_p_si
4029# endif
4030# endif
4031# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004032 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4033 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034# endif
4035 ;
4036}
4037#endif
4038
4039/* Return the character name of the register with the given number */
4040 int
4041get_register_name(num)
4042 int num;
4043{
4044 if (num == -1)
4045 return '"';
4046 else if (num < 10)
4047 return num + '0';
4048 else if (num == DELETION_REGISTER)
4049 return '-';
4050#ifdef FEAT_CLIPBOARD
4051 else if (num == STAR_REGISTER)
4052 return '*';
4053 else if (num == PLUS_REGISTER)
4054 return '+';
4055#endif
4056 else
4057 {
4058#ifdef EBCDIC
4059 int i;
4060
4061 /* EBCDIC is really braindead ... */
4062 i = 'a' + (num - 10);
4063 if (i > 'i')
4064 i += 7;
4065 if (i > 'r')
4066 i += 8;
4067 return i;
4068#else
4069 return num + 'a' - 10;
4070#endif
4071 }
4072}
4073
4074/*
4075 * ":dis" and ":registers": Display the contents of the yank registers.
4076 */
4077 void
4078ex_display(eap)
4079 exarg_T *eap;
4080{
4081 int i, n;
4082 long j;
4083 char_u *p;
4084 struct yankreg *yb;
4085 int name;
4086 int attr;
4087 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004088#ifdef FEAT_MBYTE
4089 int clen;
4090#else
4091# define clen 1
4092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 if (arg != NULL && *arg == NUL)
4095 arg = NULL;
4096 attr = hl_attr(HLF_8);
4097
4098 /* Highlight title */
4099 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4100 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4101 {
4102 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004103 if (arg != NULL && vim_strchr(arg, name) == NULL
4104#ifdef ONE_CLIPBOARD
4105 /* Star register and plus register contain the same thing. */
4106 && (name != '*' || vim_strchr(arg, '+') == NULL)
4107#endif
4108 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 continue; /* did not ask for this register */
4110
4111#ifdef FEAT_CLIPBOARD
4112 /* Adjust register name for "unnamed" in 'clipboard'.
4113 * When it's a clipboard register, fill it with the current contents
4114 * of the clipboard. */
4115 adjust_clip_reg(&name);
4116 (void)may_get_selection(name);
4117#endif
4118
4119 if (i == -1)
4120 {
4121 if (y_previous != NULL)
4122 yb = y_previous;
4123 else
4124 yb = &(y_regs[0]);
4125 }
4126 else
4127 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004128
4129#ifdef FEAT_EVAL
4130 if (name == MB_TOLOWER(redir_reg)
4131 || (redir_reg == '"' && yb == y_previous))
4132 continue; /* do not list register being written to, the
4133 * pointer can be freed */
4134#endif
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (yb->y_array != NULL)
4137 {
4138 msg_putchar('\n');
4139 msg_putchar('"');
4140 msg_putchar(name);
4141 MSG_PUTS(" ");
4142
4143 n = (int)Columns - 6;
4144 for (j = 0; j < yb->y_size && n > 1; ++j)
4145 {
4146 if (j)
4147 {
4148 MSG_PUTS_ATTR("^J", attr);
4149 n -= 2;
4150 }
4151 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004154 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004155#endif
4156 msg_outtrans_len(p, clen);
4157#ifdef FEAT_MBYTE
4158 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#endif
4160 }
4161 }
4162 if (n > 1 && yb->y_type == MLINE)
4163 MSG_PUTS_ATTR("^J", attr);
4164 out_flush(); /* show one line at a time */
4165 }
4166 ui_breakcheck();
4167 }
4168
4169 /*
4170 * display last inserted text
4171 */
4172 if ((p = get_last_insert()) != NULL
4173 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4174 {
4175 MSG_PUTS("\n\". ");
4176 dis_msg(p, TRUE);
4177 }
4178
4179 /*
4180 * display last command line
4181 */
4182 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4183 && !got_int)
4184 {
4185 MSG_PUTS("\n\": ");
4186 dis_msg(last_cmdline, FALSE);
4187 }
4188
4189 /*
4190 * display current file name
4191 */
4192 if (curbuf->b_fname != NULL
4193 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4194 {
4195 MSG_PUTS("\n\"% ");
4196 dis_msg(curbuf->b_fname, FALSE);
4197 }
4198
4199 /*
4200 * display alternate file name
4201 */
4202 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4203 {
4204 char_u *fname;
4205 linenr_T dummy;
4206
4207 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4208 {
4209 MSG_PUTS("\n\"# ");
4210 dis_msg(fname, FALSE);
4211 }
4212 }
4213
4214 /*
4215 * display last search pattern
4216 */
4217 if (last_search_pat() != NULL
4218 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4219 {
4220 MSG_PUTS("\n\"/ ");
4221 dis_msg(last_search_pat(), FALSE);
4222 }
4223
4224#ifdef FEAT_EVAL
4225 /*
4226 * display last used expression
4227 */
4228 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4229 && !got_int)
4230 {
4231 MSG_PUTS("\n\"= ");
4232 dis_msg(expr_line, FALSE);
4233 }
4234#endif
4235}
4236
4237/*
4238 * display a string for do_dis()
4239 * truncate at end of screen line
4240 */
4241 static void
4242dis_msg(p, skip_esc)
4243 char_u *p;
4244 int skip_esc; /* if TRUE, ignore trailing ESC */
4245{
4246 int n;
4247#ifdef FEAT_MBYTE
4248 int l;
4249#endif
4250
4251 n = (int)Columns - 6;
4252 while (*p != NUL
4253 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4254 && (n -= ptr2cells(p)) >= 0)
4255 {
4256#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004257 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 msg_outtrans_len(p, l);
4260 p += l;
4261 }
4262 else
4263#endif
4264 msg_outtrans_len(p++, 1);
4265 }
4266 ui_breakcheck();
4267}
4268
Bram Moolenaar81340392012-06-06 16:12:59 +02004269#if defined(FEAT_COMMENTS) || defined(PROTO)
4270/*
4271 * If "process" is TRUE and the line begins with a comment leader (possibly
4272 * after some white space), return a pointer to the text after it. Put a boolean
4273 * value indicating whether the line ends with an unclosed comment in
4274 * "is_comment".
4275 * line - line to be processed,
4276 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004277 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004278 * include_space - whether to also skip space following the comment leader,
4279 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004280 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004281 */
4282 static char_u *
4283skip_comment(line, process, include_space, is_comment)
4284 char_u *line;
4285 int process;
4286 int include_space;
4287 int *is_comment;
4288{
4289 char_u *comment_flags = NULL;
4290 int lead_len;
4291 int leader_offset = get_last_leader_offset(line, &comment_flags);
4292
4293 *is_comment = FALSE;
4294 if (leader_offset != -1)
4295 {
4296 /* Let's check whether the line ends with an unclosed comment.
4297 * If the last comment leader has COM_END in flags, there's no comment.
4298 */
4299 while (*comment_flags)
4300 {
4301 if (*comment_flags == COM_END
4302 || *comment_flags == ':')
4303 break;
4304 ++comment_flags;
4305 }
4306 if (*comment_flags != COM_END)
4307 *is_comment = TRUE;
4308 }
4309
4310 if (process == FALSE)
4311 return line;
4312
4313 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4314
4315 if (lead_len == 0)
4316 return line;
4317
4318 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004319 * - COM_END,
4320 * - colon,
4321 * whichever comes first.
4322 */
4323 while (*comment_flags)
4324 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004325 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004326 || *comment_flags == ':')
4327 {
4328 break;
4329 }
4330 ++comment_flags;
4331 }
4332
4333 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004334 * starting with a closing part of a three-part comment. That's good,
4335 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004336 */
4337 if (*comment_flags == ':' || *comment_flags == NUL)
4338 line += lead_len;
4339
4340 return line;
4341}
4342#endif
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004345 * Join 'count' lines (minimal 2) at cursor position.
4346 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004347 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4348 * leaders should not be removed.
4349 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4350 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004352 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 */
4354 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004355do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004356 long count;
4357 int insert_space;
4358 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004359 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004360 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004362 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004363 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004364 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004366 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004367 int endcurr1 = NUL;
4368 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004369 int currsize = 0; /* size of the current line */
4370 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004372 colnr_T col = 0;
4373 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004374#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004375 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004376 int remove_comments = (use_formatoptions == TRUE)
4377 && has_format_option(FO_REMOVE_COMS);
4378 int prev_was_comment;
4379#endif
4380
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004382 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4383 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004386 /* Allocate an array to store the number of spaces inserted before each
4387 * line. We will use it to pre-compute the length of the new line and the
4388 * proper placement of each original line in the new one. */
4389 spaces = lalloc_clear((long_u)count, TRUE);
4390 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004392#if defined(FEAT_COMMENTS) || defined(PROTO)
4393 if (remove_comments)
4394 {
4395 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4396 if (comments == NULL)
4397 {
4398 vim_free(spaces);
4399 return FAIL;
4400 }
4401 }
4402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004405 * Don't move anything, just compute the final line length
4406 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004408 for (t = 0; t < count; ++t)
4409 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004410 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004411 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004412 {
4413 /* Set the '[ mark. */
4414 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4415 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4416 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004417#if defined(FEAT_COMMENTS) || defined(PROTO)
4418 if (remove_comments)
4419 {
4420 /* We don't want to remove the comment leader if the
4421 * previous line is not a comment. */
4422 if (t > 0 && prev_was_comment)
4423 {
4424
4425 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4426 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004427 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004428 curr = new_curr;
4429 }
4430 else
4431 curr = skip_comment(curr, FALSE, insert_space,
4432 &prev_was_comment);
4433 }
4434#endif
4435
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004436 if (insert_space && t > 0)
4437 {
4438 curr = skipwhite(curr);
4439 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4440#ifdef FEAT_MBYTE
4441 && (!has_format_option(FO_MBYTE_JOIN)
4442 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4443 && (!has_format_option(FO_MBYTE_JOIN2)
4444 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4445#endif
4446 )
4447 {
4448 /* don't add a space if the line is ending in a space */
4449 if (endcurr1 == ' ')
4450 endcurr1 = endcurr2;
4451 else
4452 ++spaces[t];
4453 /* extra space when 'joinspaces' set and line ends in '.' */
4454 if ( p_js
4455 && (endcurr1 == '.'
4456 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4457 && (endcurr1 == '?' || endcurr1 == '!'))))
4458 ++spaces[t];
4459 }
4460 }
4461 currsize = (int)STRLEN(curr);
4462 sumsize += currsize + spaces[t];
4463 endcurr1 = endcurr2 = NUL;
4464 if (insert_space && currsize > 0)
4465 {
4466#ifdef FEAT_MBYTE
4467 if (has_mbyte)
4468 {
4469 cend = curr + currsize;
4470 mb_ptr_back(curr, cend);
4471 endcurr1 = (*mb_ptr2char)(cend);
4472 if (cend > curr)
4473 {
4474 mb_ptr_back(curr, cend);
4475 endcurr2 = (*mb_ptr2char)(cend);
4476 }
4477 }
4478 else
4479#endif
4480 {
4481 endcurr1 = *(curr + currsize - 1);
4482 if (currsize > 1)
4483 endcurr2 = *(curr + currsize - 2);
4484 }
4485 }
4486 line_breakcheck();
4487 if (got_int)
4488 {
4489 ret = FAIL;
4490 goto theend;
4491 }
4492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004494 /* store the column position before last line */
4495 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004497 /* allocate the space for the new line */
4498 newp = alloc_check((unsigned)(sumsize + 1));
4499 cend = newp + sumsize;
4500 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 /*
4503 * Move affected lines to the new long one.
4504 *
4505 * Move marks from each deleted line to the joined line, adjusting the
4506 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4507 * should not really be a problem.
4508 */
4509 for (t = count - 1; ; --t)
4510 {
4511 cend -= currsize;
4512 mch_memmove(cend, curr, (size_t)currsize);
4513 if (spaces[t] > 0)
4514 {
4515 cend -= spaces[t];
4516 copy_spaces(cend, (size_t)(spaces[t]));
4517 }
4518 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004519 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004520 if (t == 0)
4521 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004522 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004523#if defined(FEAT_COMMENTS) || defined(PROTO)
4524 if (remove_comments)
4525 curr += comments[t - 1];
4526#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004527 if (insert_space && t > 1)
4528 curr = skipwhite(curr);
4529 currsize = (int)STRLEN(curr);
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4532
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004533 if (setmark)
4534 {
4535 /* Set the '] mark. */
4536 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4537 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4538 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /* Only report the change in the first line here, del_lines() will report
4541 * the deleted line. */
4542 changed_lines(curwin->w_cursor.lnum, currsize,
4543 curwin->w_cursor.lnum + 1, 0L);
4544
4545 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004546 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 * briefly, and then move it back. After del_lines() the cursor may
4548 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
4550 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004552 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 curwin->w_cursor.lnum = t;
4554
4555 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 * Set the cursor column:
4557 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004558 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004560 curwin->w_cursor.col =
4561 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004563
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#ifdef FEAT_VIRTUALEDIT
4565 curwin->w_cursor.coladd = 0;
4566#endif
4567 curwin->w_set_curswant = TRUE;
4568
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004569theend:
4570 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004571#if defined(FEAT_COMMENTS) || defined(PROTO)
4572 if (remove_comments)
4573 vim_free(comments);
4574#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004575 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576}
4577
4578#ifdef FEAT_COMMENTS
4579/*
4580 * Return TRUE if the two comment leaders given are the same. "lnum" is
4581 * the first line. White-space is ignored. Note that the whole of
4582 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4583 */
4584 static int
4585same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4586 linenr_T lnum;
4587 int leader1_len;
4588 char_u *leader1_flags;
4589 int leader2_len;
4590 char_u *leader2_flags;
4591{
4592 int idx1 = 0, idx2 = 0;
4593 char_u *p;
4594 char_u *line1;
4595 char_u *line2;
4596
4597 if (leader1_len == 0)
4598 return (leader2_len == 0);
4599
4600 /*
4601 * If first leader has 'f' flag, the lines can be joined only if the
4602 * second line does not have a leader.
4603 * If first leader has 'e' flag, the lines can never be joined.
4604 * If fist leader has 's' flag, the lines can only be joined if there is
4605 * some text after it and the second line has the 'm' flag.
4606 */
4607 if (leader1_flags != NULL)
4608 {
4609 for (p = leader1_flags; *p && *p != ':'; ++p)
4610 {
4611 if (*p == COM_FIRST)
4612 return (leader2_len == 0);
4613 if (*p == COM_END)
4614 return FALSE;
4615 if (*p == COM_START)
4616 {
4617 if (*(ml_get(lnum) + leader1_len) == NUL)
4618 return FALSE;
4619 if (leader2_flags == NULL || leader2_len == 0)
4620 return FALSE;
4621 for (p = leader2_flags; *p && *p != ':'; ++p)
4622 if (*p == COM_MIDDLE)
4623 return TRUE;
4624 return FALSE;
4625 }
4626 }
4627 }
4628
4629 /*
4630 * Get current line and next line, compare the leaders.
4631 * The first line has to be saved, only one line can be locked at a time.
4632 */
4633 line1 = vim_strsave(ml_get(lnum));
4634 if (line1 != NULL)
4635 {
4636 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4637 ;
4638 line2 = ml_get(lnum + 1);
4639 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4640 {
4641 if (!vim_iswhite(line2[idx2]))
4642 {
4643 if (line1[idx1++] != line2[idx2])
4644 break;
4645 }
4646 else
4647 while (vim_iswhite(line1[idx1]))
4648 ++idx1;
4649 }
4650 vim_free(line1);
4651 }
4652 return (idx2 == leader2_len && idx1 == leader1_len);
4653}
4654#endif
4655
4656/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004657 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 */
4659 void
4660op_format(oap, keep_cursor)
4661 oparg_T *oap;
4662 int keep_cursor; /* keep cursor on same text char */
4663{
4664 long old_line_count = curbuf->b_ml.ml_line_count;
4665
4666 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4667 * can put it back there. */
4668 curwin->w_cursor = oap->cursor_start;
4669
4670 if (u_save((linenr_T)(oap->start.lnum - 1),
4671 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4672 return;
4673 curwin->w_cursor = oap->start;
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (oap->is_VIsual)
4676 /* When there is no change: need to remove the Visual selection */
4677 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /* Set '[ mark at the start of the formatted area */
4680 curbuf->b_op_start = oap->start;
4681
4682 /* For "gw" remember the cursor position and put it back below (adjusted
4683 * for joined and split lines). */
4684 if (keep_cursor)
4685 saved_cursor = oap->cursor_start;
4686
Bram Moolenaar81a82092008-03-12 16:27:00 +00004687 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689 /*
4690 * Leave the cursor at the first non-blank of the last formatted line.
4691 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4692 * line, so "." will do the next lines.
4693 */
4694 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4695 ++curwin->w_cursor.lnum;
4696 beginline(BL_WHITE | BL_FIX);
4697 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4698 msgmore(old_line_count);
4699
4700 /* put '] mark on the end of the formatted area */
4701 curbuf->b_op_end = curwin->w_cursor;
4702
4703 if (keep_cursor)
4704 {
4705 curwin->w_cursor = saved_cursor;
4706 saved_cursor.lnum = 0;
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if (oap->is_VIsual)
4710 {
4711 win_T *wp;
4712
4713 FOR_ALL_WINDOWS(wp)
4714 {
4715 if (wp->w_old_cursor_lnum != 0)
4716 {
4717 /* When lines have been inserted or deleted, adjust the end of
4718 * the Visual area to be redrawn. */
4719 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4720 wp->w_old_cursor_lnum += old_line_count;
4721 else
4722 wp->w_old_visual_lnum += old_line_count;
4723 }
4724 }
4725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726}
4727
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004728#if defined(FEAT_EVAL) || defined(PROTO)
4729/*
4730 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4731 */
4732 void
4733op_formatexpr(oap)
4734 oparg_T *oap;
4735{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004736 if (oap->is_VIsual)
4737 /* When there is no change: need to remove the Visual selection */
4738 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004739
Bram Moolenaar700303e2010-07-11 17:35:50 +02004740 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4741 /* As documented: when 'formatexpr' returns non-zero fall back to
4742 * internal formatting. */
4743 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004744}
4745
4746 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004747fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004748 linenr_T lnum;
4749 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004750 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004751{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004752 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4753 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004754 int r;
4755
4756 /*
4757 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004758 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004759 */
4760 set_vim_var_nr(VV_LNUM, lnum);
4761 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004762 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004763
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004764 /*
4765 * Evaluate the function.
4766 */
4767 if (use_sandbox)
4768 ++sandbox;
4769 r = eval_to_number(curbuf->b_p_fex);
4770 if (use_sandbox)
4771 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004772
4773 set_vim_var_string(VV_CHAR, NULL, -1);
4774
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004775 return r;
4776}
4777#endif
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779/*
4780 * Format "line_count" lines, starting at the cursor position.
4781 * When "line_count" is negative, format until the end of the paragraph.
4782 * Lines after the cursor line are saved for undo, caller must have saved the
4783 * first line.
4784 */
4785 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004786format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004788 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 int max_len;
4791 int is_not_par; /* current line not part of parag. */
4792 int next_is_not_par; /* next line not part of paragraph */
4793 int is_end_par; /* at end of paragraph */
4794 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4795 int next_is_start_par = FALSE;
4796#ifdef FEAT_COMMENTS
4797 int leader_len = 0; /* leader len of current line */
4798 int next_leader_len; /* leader len of next line */
4799 char_u *leader_flags = NULL; /* flags for leader of current line */
4800 char_u *next_leader_flags; /* flags for leader of next line */
4801 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004802 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803#endif
4804 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004805 int second_indent = -1; /* indent for second line (comment
4806 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 int do_second_indent;
4808 int do_number_indent;
4809 int do_trail_white;
4810 int first_par_line = TRUE;
4811 int smd_save;
4812 long count;
4813 int need_set_indent = TRUE; /* set indent of next paragraph */
4814 int force_format = FALSE;
4815 int old_State = State;
4816
4817 /* length of a line to force formatting: 3 * 'tw' */
4818 max_len = comp_textwidth(TRUE) * 3;
4819
4820 /* check for 'q', '2' and '1' in 'formatoptions' */
4821#ifdef FEAT_COMMENTS
4822 do_comments = has_format_option(FO_Q_COMS);
4823#endif
4824 do_second_indent = has_format_option(FO_Q_SECOND);
4825 do_number_indent = has_format_option(FO_Q_NUMBER);
4826 do_trail_white = has_format_option(FO_WHITE_PAR);
4827
4828 /*
4829 * Get info about the previous and current line.
4830 */
4831 if (curwin->w_cursor.lnum > 1)
4832 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4833#ifdef FEAT_COMMENTS
4834 , &leader_len, &leader_flags, do_comments
4835#endif
4836 );
4837 else
4838 is_not_par = TRUE;
4839 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4840#ifdef FEAT_COMMENTS
4841 , &next_leader_len, &next_leader_flags, do_comments
4842#endif
4843 );
4844 is_end_par = (is_not_par || next_is_not_par);
4845 if (!is_end_par && do_trail_white)
4846 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4847
4848 curwin->w_cursor.lnum--;
4849 for (count = line_count; count != 0 && !got_int; --count)
4850 {
4851 /*
4852 * Advance to next paragraph.
4853 */
4854 if (advance)
4855 {
4856 curwin->w_cursor.lnum++;
4857 prev_is_end_par = is_end_par;
4858 is_not_par = next_is_not_par;
4859#ifdef FEAT_COMMENTS
4860 leader_len = next_leader_len;
4861 leader_flags = next_leader_flags;
4862#endif
4863 }
4864
4865 /*
4866 * The last line to be formatted.
4867 */
4868 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4869 {
4870 next_is_not_par = TRUE;
4871#ifdef FEAT_COMMENTS
4872 next_leader_len = 0;
4873 next_leader_flags = NULL;
4874#endif
4875 }
4876 else
4877 {
4878 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4879#ifdef FEAT_COMMENTS
4880 , &next_leader_len, &next_leader_flags, do_comments
4881#endif
4882 );
4883 if (do_number_indent)
4884 next_is_start_par =
4885 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4886 }
4887 advance = TRUE;
4888 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4889 if (!is_end_par && do_trail_white)
4890 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4891
4892 /*
4893 * Skip lines that are not in a paragraph.
4894 */
4895 if (is_not_par)
4896 {
4897 if (line_count < 0)
4898 break;
4899 }
4900 else
4901 {
4902 /*
4903 * For the first line of a paragraph, check indent of second line.
4904 * Don't do this for comments and empty lines.
4905 */
4906 if (first_par_line
4907 && (do_second_indent || do_number_indent)
4908 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004909 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004911 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4912 {
4913#ifdef FEAT_COMMENTS
4914 if (leader_len == 0 && next_leader_len == 0)
4915 {
4916 /* no comment found */
4917#endif
4918 second_indent =
4919 get_indent_lnum(curwin->w_cursor.lnum + 1);
4920#ifdef FEAT_COMMENTS
4921 }
4922 else
4923 {
4924 second_indent = next_leader_len;
4925 do_comments_list = 1;
4926 }
4927#endif
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004930 {
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_number_indent(curwin->w_cursor.lnum);
4938#ifdef FEAT_COMMENTS
4939 }
4940 else
4941 {
4942 /* get_number_indent() is now "comment aware"... */
4943 second_indent =
4944 get_number_indent(curwin->w_cursor.lnum);
4945 do_comments_list = 1;
4946 }
4947#endif
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950
4951 /*
4952 * When the comment leader changes, it's the end of the paragraph.
4953 */
4954 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4955#ifdef FEAT_COMMENTS
4956 || !same_leader(curwin->w_cursor.lnum,
4957 leader_len, leader_flags,
4958 next_leader_len, next_leader_flags)
4959#endif
4960 )
4961 is_end_par = TRUE;
4962
4963 /*
4964 * If we have got to the end of a paragraph, or the line is
4965 * getting long, format it.
4966 */
4967 if (is_end_par || force_format)
4968 {
4969 if (need_set_indent)
4970 /* replace indent in first line with minimal number of
4971 * tabs and spaces, according to current options */
4972 (void)set_indent(get_indent(), SIN_CHANGED);
4973
4974 /* put cursor on last non-space */
4975 State = NORMAL; /* don't go past end-of-line */
4976 coladvance((colnr_T)MAXCOL);
4977 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4978 dec_cursor();
4979
4980 /* do the formatting, without 'showmode' */
4981 State = INSERT; /* for open_line() */
4982 smd_save = p_smd;
4983 p_smd = FALSE;
4984 insertchar(NUL, INSCHAR_FORMAT
4985#ifdef FEAT_COMMENTS
4986 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004987 + (do_comments && do_comments_list
4988 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004990 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 State = old_State;
4992 p_smd = smd_save;
4993 second_indent = -1;
4994 /* at end of par.: need to set indent of next par. */
4995 need_set_indent = is_end_par;
4996 if (is_end_par)
4997 {
4998 /* When called with a negative line count, break at the
4999 * end of the paragraph. */
5000 if (line_count < 0)
5001 break;
5002 first_par_line = TRUE;
5003 }
5004 force_format = FALSE;
5005 }
5006
5007 /*
5008 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005009 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 */
5011 if (!is_end_par)
5012 {
5013 advance = FALSE;
5014 curwin->w_cursor.lnum++;
5015 curwin->w_cursor.col = 0;
5016 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005020 {
5021 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5023 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005024 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005026 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5027 {
5028 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005029 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005030
5031 if (indent > 0)
5032 {
5033 (void)del_bytes(indent, FALSE, FALSE);
5034 mark_col_adjust(curwin->w_cursor.lnum,
5035 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005036 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005039 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 beep_flush();
5042 break;
5043 }
5044 first_par_line = FALSE;
5045 /* If the line is getting long, format it next time */
5046 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5047 force_format = TRUE;
5048 else
5049 force_format = FALSE;
5050 }
5051 }
5052 line_breakcheck();
5053 }
5054}
5055
5056/*
5057 * Return TRUE if line "lnum" ends in a white character.
5058 */
5059 static int
5060ends_in_white(lnum)
5061 linenr_T lnum;
5062{
5063 char_u *s = ml_get(lnum);
5064 size_t l;
5065
5066 if (*s == NUL)
5067 return FALSE;
5068 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5069 * invocation may call function multiple times". */
5070 l = STRLEN(s) - 1;
5071 return vim_iswhite(s[l]);
5072}
5073
5074/*
5075 * Blank lines, and lines containing only the comment leader, are left
5076 * untouched by the formatting. The function returns TRUE in this
5077 * case. It also returns TRUE when a line starts with the end of a comment
5078 * ('e' in comment flags), so that this line is skipped, and not joined to the
5079 * previous line. A new paragraph starts after a blank line, or when the
5080 * comment leader changes -- webb.
5081 */
5082#ifdef FEAT_COMMENTS
5083 static int
5084fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5085 linenr_T lnum;
5086 int *leader_len;
5087 char_u **leader_flags;
5088 int do_comments;
5089{
5090 char_u *flags = NULL; /* init for GCC */
5091 char_u *ptr;
5092
5093 ptr = ml_get(lnum);
5094 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005095 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else
5097 *leader_len = 0;
5098
5099 if (*leader_len > 0)
5100 {
5101 /*
5102 * Search for 'e' flag in comment leader flags.
5103 */
5104 flags = *leader_flags;
5105 while (*flags && *flags != ':' && *flags != COM_END)
5106 ++flags;
5107 }
5108
5109 return (*skipwhite(ptr + *leader_len) == NUL
5110 || (*leader_len > 0 && *flags == COM_END)
5111 || startPS(lnum, NUL, FALSE));
5112}
5113#else
5114 static int
5115fmt_check_par(lnum)
5116 linenr_T lnum;
5117{
5118 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5119}
5120#endif
5121
5122/*
5123 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5124 * previous line is in the same paragraph. Used for auto-formatting.
5125 */
5126 int
5127paragraph_start(lnum)
5128 linenr_T lnum;
5129{
5130 char_u *p;
5131#ifdef FEAT_COMMENTS
5132 int leader_len = 0; /* leader len of current line */
5133 char_u *leader_flags = NULL; /* flags for leader of current line */
5134 int next_leader_len; /* leader len of next line */
5135 char_u *next_leader_flags; /* flags for leader of next line */
5136 int do_comments; /* format comments */
5137#endif
5138
5139 if (lnum <= 1)
5140 return TRUE; /* start of the file */
5141
5142 p = ml_get(lnum - 1);
5143 if (*p == NUL)
5144 return TRUE; /* after empty line */
5145
5146#ifdef FEAT_COMMENTS
5147 do_comments = has_format_option(FO_Q_COMS);
5148#endif
5149 if (fmt_check_par(lnum - 1
5150#ifdef FEAT_COMMENTS
5151 , &leader_len, &leader_flags, do_comments
5152#endif
5153 ))
5154 return TRUE; /* after non-paragraph line */
5155
5156 if (fmt_check_par(lnum
5157#ifdef FEAT_COMMENTS
5158 , &next_leader_len, &next_leader_flags, do_comments
5159#endif
5160 ))
5161 return TRUE; /* "lnum" is not a paragraph line */
5162
5163 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5164 return TRUE; /* missing trailing space in previous line. */
5165
5166 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5167 return TRUE; /* numbered item starts in "lnum". */
5168
5169#ifdef FEAT_COMMENTS
5170 if (!same_leader(lnum - 1, leader_len, leader_flags,
5171 next_leader_len, next_leader_flags))
5172 return TRUE; /* change of comment leader. */
5173#endif
5174
5175 return FALSE;
5176}
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
5179 * prepare a few things for block mode yank/delete/tilde
5180 *
5181 * for delete:
5182 * - textlen includes the first/last char to be (partly) deleted
5183 * - start/endspaces is the number of columns that are taken by the
5184 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005185 * deleted.
5186 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 * - textlen includes the first/last char to be wholly yanked
5188 * - start/endspaces is the number of columns of the first/last yanked char
5189 * that are to be yanked.
5190 */
5191 static void
5192block_prep(oap, bdp, lnum, is_del)
5193 oparg_T *oap;
5194 struct block_def *bdp;
5195 linenr_T lnum;
5196 int is_del;
5197{
5198 int incr = 0;
5199 char_u *pend;
5200 char_u *pstart;
5201 char_u *line;
5202 char_u *prev_pstart;
5203 char_u *prev_pend;
5204
5205 bdp->startspaces = 0;
5206 bdp->endspaces = 0;
5207 bdp->textlen = 0;
5208 bdp->start_vcol = 0;
5209 bdp->end_vcol = 0;
5210#ifdef FEAT_VISUALEXTRA
5211 bdp->is_short = FALSE;
5212 bdp->is_oneChar = FALSE;
5213 bdp->pre_whitesp = 0;
5214 bdp->pre_whitesp_c = 0;
5215 bdp->end_char_vcols = 0;
5216#endif
5217 bdp->start_char_vcols = 0;
5218
5219 line = ml_get(lnum);
5220 pstart = line;
5221 prev_pstart = line;
5222 while (bdp->start_vcol < oap->start_vcol && *pstart)
5223 {
5224 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005225 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 bdp->start_vcol += incr;
5227#ifdef FEAT_VISUALEXTRA
5228 if (vim_iswhite(*pstart))
5229 {
5230 bdp->pre_whitesp += incr;
5231 bdp->pre_whitesp_c++;
5232 }
5233 else
5234 {
5235 bdp->pre_whitesp = 0;
5236 bdp->pre_whitesp_c = 0;
5237 }
5238#endif
5239 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005240 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 bdp->start_char_vcols = incr;
5243 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5244 {
5245 bdp->end_vcol = bdp->start_vcol;
5246#ifdef FEAT_VISUALEXTRA
5247 bdp->is_short = TRUE;
5248#endif
5249 if (!is_del || oap->op_type == OP_APPEND)
5250 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5251 }
5252 else
5253 {
5254 /* notice: this converts partly selected Multibyte characters to
5255 * spaces, too. */
5256 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5257 if (is_del && bdp->startspaces)
5258 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5259 pend = pstart;
5260 bdp->end_vcol = bdp->start_vcol;
5261 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5262 {
5263#ifdef FEAT_VISUALEXTRA
5264 bdp->is_oneChar = TRUE;
5265#endif
5266 if (oap->op_type == OP_INSERT)
5267 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5268 else if (oap->op_type == OP_APPEND)
5269 {
5270 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5271 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5272 }
5273 else
5274 {
5275 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5276 if (is_del && oap->op_type != OP_LSHIFT)
5277 {
5278 /* just putting the sum of those two into
5279 * bdp->startspaces doesn't work for Visual replace,
5280 * so we have to split the tab in two */
5281 bdp->startspaces = bdp->start_char_vcols
5282 - (bdp->start_vcol - oap->start_vcol);
5283 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5284 }
5285 }
5286 }
5287 else
5288 {
5289 prev_pend = pend;
5290 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5291 {
5292 /* Count a tab for what it's worth (if list mode not on) */
5293 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005294 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 bdp->end_vcol += incr;
5296 }
5297 if (bdp->end_vcol <= oap->end_vcol
5298 && (!is_del
5299 || oap->op_type == OP_APPEND
5300 || oap->op_type == OP_REPLACE)) /* line too short */
5301 {
5302#ifdef FEAT_VISUALEXTRA
5303 bdp->is_short = TRUE;
5304#endif
5305 /* Alternative: include spaces to fill up the block.
5306 * Disadvantage: can lead to trailing spaces when the line is
5307 * short where the text is put */
5308 /* if (!is_del || oap->op_type == OP_APPEND) */
5309 if (oap->op_type == OP_APPEND || virtual_op)
5310 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005311 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 else
5313 bdp->endspaces = 0; /* replace doesn't add characters */
5314 }
5315 else if (bdp->end_vcol > oap->end_vcol)
5316 {
5317 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5318 if (!is_del && bdp->endspaces)
5319 {
5320 bdp->endspaces = incr - bdp->endspaces;
5321 if (pend != pstart)
5322 pend = prev_pend;
5323 }
5324 }
5325 }
5326#ifdef FEAT_VISUALEXTRA
5327 bdp->end_char_vcols = incr;
5328#endif
5329 if (is_del && bdp->startspaces)
5330 pstart = prev_pstart;
5331 bdp->textlen = (int)(pend - pstart);
5332 }
5333 bdp->textcol = (colnr_T) (pstart - line);
5334 bdp->textstart = pstart;
5335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337#ifdef FEAT_RIGHTLEFT
5338static void reverse_line __ARGS((char_u *s));
5339
5340 static void
5341reverse_line(s)
5342 char_u *s;
5343{
5344 int i, j;
5345 char_u c;
5346
5347 if ((i = (int)STRLEN(s) - 1) <= 0)
5348 return;
5349
5350 curwin->w_cursor.col = i - curwin->w_cursor.col;
5351 for (j = 0; j < i; j++, i--)
5352 {
5353 c = s[i]; s[i] = s[j]; s[j] = c;
5354 }
5355}
5356
5357# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5358#else
5359# define RLADDSUBFIX(ptr)
5360#endif
5361
5362/*
5363 * add or subtract 'Prenum1' from a number in a line
5364 * 'command' is CTRL-A for add, CTRL-X for subtract
5365 *
5366 * return FAIL for failure, OK otherwise
5367 */
5368 int
5369do_addsub(command, Prenum1)
5370 int command;
5371 linenr_T Prenum1;
5372{
5373 int col;
5374 char_u *buf1;
5375 char_u buf2[NUMBUFLEN];
5376 int hex; /* 'X' or 'x': hex; '0': octal */
5377 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005378 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 long_u oldn;
5380 char_u *ptr;
5381 int c;
5382 int length = 0; /* character length of the number */
5383 int todel;
5384 int dohex;
5385 int dooct;
5386 int doalp;
5387 int firstdigit;
5388 int negative;
5389 int subtract;
5390
5391 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5392 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5393 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5394
5395 ptr = ml_get_curline();
5396 RLADDSUBFIX(ptr);
5397
5398 /*
5399 * First check if we are on a hexadecimal number, after the "0x".
5400 */
5401 col = curwin->w_cursor.col;
5402 if (dohex)
5403 while (col > 0 && vim_isxdigit(ptr[col]))
5404 --col;
5405 if ( dohex
5406 && col > 0
5407 && (ptr[col] == 'X'
5408 || ptr[col] == 'x')
5409 && ptr[col - 1] == '0'
5410 && vim_isxdigit(ptr[col + 1]))
5411 {
5412 /*
5413 * Found hexadecimal number, move to its start.
5414 */
5415 --col;
5416 }
5417 else
5418 {
5419 /*
5420 * Search forward and then backward to find the start of number.
5421 */
5422 col = curwin->w_cursor.col;
5423
5424 while (ptr[col] != NUL
5425 && !vim_isdigit(ptr[col])
5426 && !(doalp && ASCII_ISALPHA(ptr[col])))
5427 ++col;
5428
5429 while (col > 0
5430 && vim_isdigit(ptr[col - 1])
5431 && !(doalp && ASCII_ISALPHA(ptr[col])))
5432 --col;
5433 }
5434
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 /*
5436 * If a number was found, and saving for undo works, replace the number.
5437 */
5438 firstdigit = ptr[col];
5439 RLADDSUBFIX(ptr);
5440 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5441 || u_save_cursor() != OK)
5442 {
5443 beep_flush();
5444 return FAIL;
5445 }
5446
5447 /* get ptr again, because u_save() may have changed it */
5448 ptr = ml_get_curline();
5449 RLADDSUBFIX(ptr);
5450
5451 if (doalp && ASCII_ISALPHA(firstdigit))
5452 {
5453 /* decrement or increment alphabetic character */
5454 if (command == Ctrl_X)
5455 {
5456 if (CharOrd(firstdigit) < Prenum1)
5457 {
5458 if (isupper(firstdigit))
5459 firstdigit = 'A';
5460 else
5461 firstdigit = 'a';
5462 }
5463 else
5464#ifdef EBCDIC
5465 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5466#else
5467 firstdigit -= Prenum1;
5468#endif
5469 }
5470 else
5471 {
5472 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5473 {
5474 if (isupper(firstdigit))
5475 firstdigit = 'Z';
5476 else
5477 firstdigit = 'z';
5478 }
5479 else
5480#ifdef EBCDIC
5481 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5482#else
5483 firstdigit += Prenum1;
5484#endif
5485 }
5486 curwin->w_cursor.col = col;
5487 (void)del_char(FALSE);
5488 ins_char(firstdigit);
5489 }
5490 else
5491 {
5492 negative = FALSE;
5493 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5494 {
5495 --col;
5496 negative = TRUE;
5497 }
5498
5499 /* get the number value (unsigned) */
5500 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5501
5502 /* ignore leading '-' for hex and octal numbers */
5503 if (hex && negative)
5504 {
5505 ++col;
5506 --length;
5507 negative = FALSE;
5508 }
5509
5510 /* add or subtract */
5511 subtract = FALSE;
5512 if (command == Ctrl_X)
5513 subtract ^= TRUE;
5514 if (negative)
5515 subtract ^= TRUE;
5516
5517 oldn = n;
5518 if (subtract)
5519 n -= (unsigned long)Prenum1;
5520 else
5521 n += (unsigned long)Prenum1;
5522
5523 /* handle wraparound for decimal numbers */
5524 if (!hex)
5525 {
5526 if (subtract)
5527 {
5528 if (n > oldn)
5529 {
5530 n = 1 + (n ^ (unsigned long)-1);
5531 negative ^= TRUE;
5532 }
5533 }
5534 else /* add */
5535 {
5536 if (n < oldn)
5537 {
5538 n = (n ^ (unsigned long)-1);
5539 negative ^= TRUE;
5540 }
5541 }
5542 if (n == 0)
5543 negative = FALSE;
5544 }
5545
5546 /*
5547 * Delete the old number.
5548 */
5549 curwin->w_cursor.col = col;
5550 todel = length;
5551 c = gchar_cursor();
5552 /*
5553 * Don't include the '-' in the length, only the length of the part
5554 * after it is kept the same.
5555 */
5556 if (c == '-')
5557 --length;
5558 while (todel-- > 0)
5559 {
5560 if (c < 0x100 && isalpha(c))
5561 {
5562 if (isupper(c))
5563 hexupper = TRUE;
5564 else
5565 hexupper = FALSE;
5566 }
5567 /* del_char() will mark line needing displaying */
5568 (void)del_char(FALSE);
5569 c = gchar_cursor();
5570 }
5571
5572 /*
5573 * Prepare the leading characters in buf1[].
5574 * When there are many leading zeros it could be very long. Allocate
5575 * a bit too much.
5576 */
5577 buf1 = alloc((unsigned)length + NUMBUFLEN);
5578 if (buf1 == NULL)
5579 return FAIL;
5580 ptr = buf1;
5581 if (negative)
5582 {
5583 *ptr++ = '-';
5584 }
5585 if (hex)
5586 {
5587 *ptr++ = '0';
5588 --length;
5589 }
5590 if (hex == 'x' || hex == 'X')
5591 {
5592 *ptr++ = hex;
5593 --length;
5594 }
5595
5596 /*
5597 * Put the number characters in buf2[].
5598 */
5599 if (hex == 0)
5600 sprintf((char *)buf2, "%lu", n);
5601 else if (hex == '0')
5602 sprintf((char *)buf2, "%lo", n);
5603 else if (hex && hexupper)
5604 sprintf((char *)buf2, "%lX", n);
5605 else
5606 sprintf((char *)buf2, "%lx", n);
5607 length -= (int)STRLEN(buf2);
5608
5609 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005610 * Adjust number of zeros to the new number of digits, so the
5611 * total length of the number remains the same.
5612 * Don't do this when
5613 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005615 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 while (length-- > 0)
5617 *ptr++ = '0';
5618 *ptr = NUL;
5619 STRCAT(buf1, buf2);
5620 ins_str(buf1); /* insert the new number */
5621 vim_free(buf1);
5622 }
5623 --curwin->w_cursor.col;
5624 curwin->w_set_curswant = TRUE;
5625#ifdef FEAT_RIGHTLEFT
5626 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5627 RLADDSUBFIX(ptr);
5628#endif
5629 return OK;
5630}
5631
5632#ifdef FEAT_VIMINFO
5633 int
5634read_viminfo_register(virp, force)
5635 vir_T *virp;
5636 int force;
5637{
5638 int eof;
5639 int do_it = TRUE;
5640 int size;
5641 int limit;
5642 int i;
5643 int set_prev = FALSE;
5644 char_u *str;
5645 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005646 int new_type = MCHAR; /* init to shut up compiler */
5647 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 /* We only get here (hopefully) if line[0] == '"' */
5650 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005651
5652 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (*str == '"')
5654 {
5655 set_prev = TRUE;
5656 str++;
5657 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (!ASCII_ISALNUM(*str) && *str != '-')
5660 {
5661 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5662 return TRUE; /* too many errors, pretend end-of-file */
5663 do_it = FALSE;
5664 }
5665 get_yank_register(*str++, FALSE);
5666 if (!force && y_current->y_array != NULL)
5667 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005668
5669 if (*str == '@')
5670 {
5671 /* "x@: register x used for @@ */
5672 if (force || execreg_lastc == NUL)
5673 execreg_lastc = str[-1];
5674 }
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 size = 0;
5677 limit = 100; /* Optimized for registers containing <= 100 lines */
5678 if (do_it)
5679 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005680 /*
5681 * Build the new register in array[].
5682 * y_array is kept as-is until done.
5683 * The "do_it" flag is reset when something is wrong, in which case
5684 * array[] needs to be freed.
5685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (set_prev)
5687 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005688 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005689 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005691 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005693 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005695 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* get the block width; if it's missing we get a zero, which is OK */
5697 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005698 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 while (!(eof = viminfo_readline(virp))
5702 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5703 {
5704 if (do_it)
5705 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005706 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005708 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005710
5711 if (new_array == NULL)
5712 {
5713 do_it = FALSE;
5714 break;
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005717 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005719 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 str = viminfo_readstring(virp, 1, TRUE);
5723 if (str != NULL)
5724 array[size++] = str;
5725 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005726 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 do_it = FALSE;
5728 }
5729 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (do_it)
5732 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005733 /* free y_array[] */
5734 for (i = 0; i < y_current->y_size; i++)
5735 vim_free(y_current->y_array[i]);
5736 vim_free(y_current->y_array);
5737
5738 y_current->y_type = new_type;
5739 y_current->y_width = new_width;
5740 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 if (size == 0)
5742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 y_current->y_array = NULL;
5744 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005747 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 y_current->y_array =
5749 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5750 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005751 {
5752 if (y_current->y_array == NULL)
5753 vim_free(array[i]);
5754 else
5755 y_current->y_array[i] = array[i];
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005759 else
5760 {
5761 /* Free array[] if it was filled. */
5762 for (i = 0; i < size; i++)
5763 vim_free(array[i]);
5764 }
5765 vim_free(array);
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 return eof;
5768}
5769
5770 void
5771write_viminfo_registers(fp)
5772 FILE *fp;
5773{
5774 int i, j;
5775 char_u *type;
5776 char_u c;
5777 int num_lines;
5778 int max_num_lines;
5779 int max_kbyte;
5780 long len;
5781
Bram Moolenaar64404472010-06-26 06:24:45 +02005782 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 /* Get '<' value, use old '"' value if '<' is not found. */
5785 max_num_lines = get_viminfo_parameter('<');
5786 if (max_num_lines < 0)
5787 max_num_lines = get_viminfo_parameter('"');
5788 if (max_num_lines == 0)
5789 return;
5790 max_kbyte = get_viminfo_parameter('s');
5791 if (max_kbyte == 0)
5792 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 for (i = 0; i < NUM_REGISTERS; i++)
5795 {
5796 if (y_regs[i].y_array == NULL)
5797 continue;
5798#ifdef FEAT_CLIPBOARD
5799 /* Skip '*'/'+' register, we don't want them back next time */
5800 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5801 continue;
5802#endif
5803#ifdef FEAT_DND
5804 /* Neither do we want the '~' register */
5805 if (i == TILDE_REGISTER)
5806 continue;
5807#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005808 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005810 if (num_lines == 0
5811 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005812 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005813 continue;
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (max_kbyte > 0)
5816 {
5817 /* Skip register if there is more text than the maximum size. */
5818 len = 0;
5819 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005820 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (len > (long)max_kbyte * 1024L)
5822 continue;
5823 }
5824
5825 switch (y_regs[i].y_type)
5826 {
5827 case MLINE:
5828 type = (char_u *)"LINE";
5829 break;
5830 case MCHAR:
5831 type = (char_u *)"CHAR";
5832 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 case MBLOCK:
5834 type = (char_u *)"BLOCK";
5835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 default:
5837 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005838 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 emsg(IObuff);
5840 type = (char_u *)"LINE";
5841 break;
5842 }
5843 if (y_previous == &y_regs[i])
5844 fprintf(fp, "\"");
5845 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005846 fprintf(fp, "\"%c", c);
5847 if (c == execreg_lastc)
5848 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005849 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 /* If max_num_lines < 0, then we save ALL the lines in the register */
5852 if (max_num_lines > 0 && num_lines > max_num_lines)
5853 num_lines = max_num_lines;
5854 for (j = 0; j < num_lines; j++)
5855 {
5856 putc('\t', fp);
5857 viminfo_writestring(fp, y_regs[i].y_array[j]);
5858 }
5859 }
5860}
5861#endif /* FEAT_VIMINFO */
5862
5863#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5864/*
5865 * SELECTION / PRIMARY ('*')
5866 *
5867 * Text selection stuff that uses the GUI selection register '*'. When using a
5868 * GUI this may be text from another window, otherwise it is the last text we
5869 * had highlighted with VIsual mode. With mouse support, clicking the middle
5870 * button performs the paste, otherwise you will need to do <"*p>. "
5871 * If not under X, it is synonymous with the clipboard register '+'.
5872 *
5873 * X CLIPBOARD ('+')
5874 *
5875 * Text selection stuff that uses the GUI clipboard register '+'.
5876 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5877 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5878 * otherwise you will need to do <"+p>. "
5879 * If not under X, it is synonymous with the selection register '*'.
5880 */
5881
5882/*
5883 * Routine to export any final X selection we had to the environment
5884 * so that the text is still available after vim has exited. X selections
5885 * only exist while the owning application exists, so we write to the
5886 * permanent (while X runs) store CUT_BUFFER0.
5887 * Dump the CLIPBOARD selection if we own it (it's logically the more
5888 * 'permanent' of the two), otherwise the PRIMARY one.
5889 * For now, use a hard-coded sanity limit of 1Mb of data.
5890 */
5891#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5892 void
5893x11_export_final_selection()
5894{
5895 Display *dpy;
5896 char_u *str = NULL;
5897 long_u len = 0;
5898 int motion_type = -1;
5899
5900# ifdef FEAT_GUI
5901 if (gui.in_use)
5902 dpy = X_DISPLAY;
5903 else
5904# endif
5905# ifdef FEAT_XCLIPBOARD
5906 dpy = xterm_dpy;
5907# else
5908 return;
5909# endif
5910
5911 /* Get selection to export */
5912 if (clip_plus.owned)
5913 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5914 else if (clip_star.owned)
5915 motion_type = clip_convert_selection(&str, &len, &clip_star);
5916
5917 /* Check it's OK */
5918 if (dpy != NULL && str != NULL && motion_type >= 0
5919 && len < 1024*1024 && len > 0)
5920 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005921#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005922 int ok = TRUE;
5923
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005924 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5925 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5926 * encoding conversion usually doesn't work, so keep the text as-is.
5927 */
5928 if (has_mbyte)
5929 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005930 vimconv_T vc;
5931
5932 vc.vc_type = CONV_NONE;
5933 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5934 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005935 int intlen = len;
5936 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005937
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005938 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005939 conv_str = string_convert(&vc, str, &intlen);
5940 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005941 if (conv_str != NULL)
5942 {
5943 vim_free(str);
5944 str = conv_str;
5945 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005946 else
5947 {
5948 ok = FALSE;
5949 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005950 convert_setup(&vc, NULL, NULL);
5951 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005952 else
5953 {
5954 ok = FALSE;
5955 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005956 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005957
5958 /* Do not store the string if conversion failed. Better to use any
5959 * other selection than garbled text. */
5960 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005961#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005962 {
5963 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5964 XFlush(dpy);
5965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
5967
5968 vim_free(str);
5969}
5970#endif
5971
5972 void
5973clip_free_selection(cbd)
5974 VimClipboard *cbd;
5975{
5976 struct yankreg *y_ptr = y_current;
5977
5978 if (cbd == &clip_plus)
5979 y_current = &y_regs[PLUS_REGISTER];
5980 else
5981 y_current = &y_regs[STAR_REGISTER];
5982 free_yank_all();
5983 y_current->y_size = 0;
5984 y_current = y_ptr;
5985}
5986
5987/*
5988 * Get the selected text and put it in the gui selection register '*' or '+'.
5989 */
5990 void
5991clip_get_selection(cbd)
5992 VimClipboard *cbd;
5993{
5994 struct yankreg *old_y_previous, *old_y_current;
5995 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 pos_T old_visual;
5997 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 colnr_T old_curswant;
5999 int old_set_curswant;
6000 pos_T old_op_start, old_op_end;
6001 oparg_T oa;
6002 cmdarg_T ca;
6003
6004 if (cbd->owned)
6005 {
6006 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6007 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6008 return;
6009
6010 /* Get the text between clip_star.start & clip_star.end */
6011 old_y_previous = y_previous;
6012 old_y_current = y_current;
6013 old_cursor = curwin->w_cursor;
6014 old_curswant = curwin->w_curswant;
6015 old_set_curswant = curwin->w_set_curswant;
6016 old_op_start = curbuf->b_op_start;
6017 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 old_visual = VIsual;
6019 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 clear_oparg(&oa);
6021 oa.regname = (cbd == &clip_plus ? '+' : '*');
6022 oa.op_type = OP_YANK;
6023 vim_memset(&ca, 0, sizeof(ca));
6024 ca.oap = &oa;
6025 ca.cmdchar = 'y';
6026 ca.count1 = 1;
6027 ca.retval = CA_NO_ADJ_OP_END;
6028 do_pending_operator(&ca, 0, TRUE);
6029 y_previous = old_y_previous;
6030 y_current = old_y_current;
6031 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006032 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 curwin->w_curswant = old_curswant;
6034 curwin->w_set_curswant = old_set_curswant;
6035 curbuf->b_op_start = old_op_start;
6036 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 VIsual = old_visual;
6038 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 }
6040 else
6041 {
6042 clip_free_selection(cbd);
6043
6044 /* Try to get selected text from another window */
6045 clip_gen_request_selection(cbd);
6046 }
6047}
6048
Bram Moolenaard44347f2011-06-19 01:14:29 +02006049/*
6050 * Convert from the GUI selection string into the '*'/'+' register.
6051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 void
6053clip_yank_selection(type, str, len, cbd)
6054 int type;
6055 char_u *str;
6056 long len;
6057 VimClipboard *cbd;
6058{
6059 struct yankreg *y_ptr;
6060
6061 if (cbd == &clip_plus)
6062 y_ptr = &y_regs[PLUS_REGISTER];
6063 else
6064 y_ptr = &y_regs[STAR_REGISTER];
6065
6066 clip_free_selection(cbd);
6067
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006068 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069}
6070
6071/*
6072 * Convert the '*'/'+' register into a GUI selection string returned in *str
6073 * with length *len.
6074 * Returns the motion type, or -1 for failure.
6075 */
6076 int
6077clip_convert_selection(str, len, cbd)
6078 char_u **str;
6079 long_u *len;
6080 VimClipboard *cbd;
6081{
6082 char_u *p;
6083 int lnum;
6084 int i, j;
6085 int_u eolsize;
6086 struct yankreg *y_ptr;
6087
6088 if (cbd == &clip_plus)
6089 y_ptr = &y_regs[PLUS_REGISTER];
6090 else
6091 y_ptr = &y_regs[STAR_REGISTER];
6092
6093#ifdef USE_CRNL
6094 eolsize = 2;
6095#else
6096 eolsize = 1;
6097#endif
6098
6099 *str = NULL;
6100 *len = 0;
6101 if (y_ptr->y_array == NULL)
6102 return -1;
6103
6104 for (i = 0; i < y_ptr->y_size; i++)
6105 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6106
6107 /*
6108 * Don't want newline character at end of last line if we're in MCHAR mode.
6109 */
6110 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6111 *len -= eolsize;
6112
6113 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6114 if (p == NULL)
6115 return -1;
6116 lnum = 0;
6117 for (i = 0, j = 0; i < (int)*len; i++, j++)
6118 {
6119 if (y_ptr->y_array[lnum][j] == '\n')
6120 p[i] = NUL;
6121 else if (y_ptr->y_array[lnum][j] == NUL)
6122 {
6123#ifdef USE_CRNL
6124 p[i++] = '\r';
6125#endif
6126#ifdef USE_CR
6127 p[i] = '\r';
6128#else
6129 p[i] = '\n';
6130#endif
6131 lnum++;
6132 j = -1;
6133 }
6134 else
6135 p[i] = y_ptr->y_array[lnum][j];
6136 }
6137 return y_ptr->y_type;
6138}
6139
6140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141/*
6142 * If we have written to a clipboard register, send the text to the clipboard.
6143 */
6144 static void
6145may_set_selection()
6146{
6147 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6148 {
6149 clip_own_selection(&clip_star);
6150 clip_gen_set_selection(&clip_star);
6151 }
6152 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6153 {
6154 clip_own_selection(&clip_plus);
6155 clip_gen_set_selection(&clip_plus);
6156 }
6157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159#endif /* FEAT_CLIPBOARD || PROTO */
6160
6161
6162#if defined(FEAT_DND) || defined(PROTO)
6163/*
6164 * Replace the contents of the '~' register with str.
6165 */
6166 void
6167dnd_yank_drag_data(str, len)
6168 char_u *str;
6169 long len;
6170{
6171 struct yankreg *curr;
6172
6173 curr = y_current;
6174 y_current = &y_regs[TILDE_REGISTER];
6175 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006176 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 y_current = curr;
6178}
6179#endif
6180
6181
6182#if defined(FEAT_EVAL) || defined(PROTO)
6183/*
6184 * Return the type of a register.
6185 * Used for getregtype()
6186 * Returns MAUTO for error.
6187 */
6188 char_u
6189get_reg_type(regname, reglen)
6190 int regname;
6191 long *reglen;
6192{
6193 switch (regname)
6194 {
6195 case '%': /* file name */
6196 case '#': /* alternate file name */
6197 case '=': /* expression */
6198 case ':': /* last command line */
6199 case '/': /* last search-pattern */
6200 case '.': /* last inserted text */
6201#ifdef FEAT_SEARCHPATH
6202 case Ctrl_F: /* Filename under cursor */
6203 case Ctrl_P: /* Path under cursor, expand via "path" */
6204#endif
6205 case Ctrl_W: /* word under cursor */
6206 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6207 case '_': /* black hole: always empty */
6208 return MCHAR;
6209 }
6210
6211#ifdef FEAT_CLIPBOARD
6212 regname = may_get_selection(regname);
6213#endif
6214
Bram Moolenaar32b92012014-01-14 12:33:36 +01006215 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6216 return MAUTO;
6217
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 get_yank_register(regname, FALSE);
6219
6220 if (y_current->y_array != NULL)
6221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (reglen != NULL && y_current->y_type == MBLOCK)
6223 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return y_current->y_type;
6225 }
6226 return MAUTO;
6227}
6228
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006229static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6230
6231/*
6232 * When "flags" has GREG_LIST return a list with text "s".
6233 * Otherwise just return "s".
6234 */
6235 static char_u *
6236getreg_wrap_one_line(s, flags)
6237 char_u *s;
6238 int flags;
6239{
6240 if (flags & GREG_LIST)
6241 {
6242 list_T *list = list_alloc();
6243
6244 if (list != NULL)
6245 {
6246 if (list_append_string(list, NULL, -1) == FAIL)
6247 {
6248 list_free(list, TRUE);
6249 return NULL;
6250 }
6251 list->lv_first->li_tv.vval.v_string = s;
6252 }
6253 return (char_u *)list;
6254 }
6255 return s;
6256}
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258/*
6259 * Return the contents of a register as a single allocated string.
6260 * Used for "@r" in expressions and for getreg().
6261 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006262 * Flags:
6263 * GREG_NO_EXPR Do not allow expression register
6264 * GREG_EXPR_SRC For the expression register: return expression itself,
6265 * not the result of its evaluation.
6266 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 */
6268 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006269get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006271 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 long i;
6274 char_u *retval;
6275 int allocated;
6276 long len;
6277
6278 /* Don't allow using an expression register inside an expression */
6279 if (regname == '=')
6280 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006281 if (flags & GREG_NO_EXPR)
6282 return NULL;
6283 if (flags & GREG_EXPR_SRC)
6284 return getreg_wrap_one_line(get_expr_line_src(), flags);
6285 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287
6288 if (regname == '@') /* "@@" is used for unnamed register */
6289 regname = '"';
6290
6291 /* check for valid regname */
6292 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6293 return NULL;
6294
6295#ifdef FEAT_CLIPBOARD
6296 regname = may_get_selection(regname);
6297#endif
6298
6299 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6300 {
6301 if (retval == NULL)
6302 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006303 if (allocated)
6304 return getreg_wrap_one_line(retval, flags);
6305 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 }
6307
6308 get_yank_register(regname, FALSE);
6309 if (y_current->y_array == NULL)
6310 return NULL;
6311
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006312 if (flags & GREG_LIST)
6313 {
6314 list_T *list = list_alloc();
6315 int error = FALSE;
6316
6317 if (list == NULL)
6318 return NULL;
6319 for (i = 0; i < y_current->y_size; ++i)
6320 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6321 error = TRUE;
6322 if (error)
6323 {
6324 list_free(list, TRUE);
6325 return NULL;
6326 }
6327 return (char_u *)list;
6328 }
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 /*
6331 * Compute length of resulting string.
6332 */
6333 len = 0;
6334 for (i = 0; i < y_current->y_size; ++i)
6335 {
6336 len += (long)STRLEN(y_current->y_array[i]);
6337 /*
6338 * Insert a newline between lines and after last line if
6339 * y_type is MLINE.
6340 */
6341 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6342 ++len;
6343 }
6344
6345 retval = lalloc(len + 1, TRUE);
6346
6347 /*
6348 * Copy the lines of the yank register into the string.
6349 */
6350 if (retval != NULL)
6351 {
6352 len = 0;
6353 for (i = 0; i < y_current->y_size; ++i)
6354 {
6355 STRCPY(retval + len, y_current->y_array[i]);
6356 len += (long)STRLEN(retval + len);
6357
6358 /*
6359 * Insert a NL between lines and after the last line if y_type is
6360 * MLINE.
6361 */
6362 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6363 retval[len++] = '\n';
6364 }
6365 retval[len] = NUL;
6366 }
6367
6368 return retval;
6369}
6370
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006371 static int
6372init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6373 int name;
6374 struct yankreg **old_y_previous;
6375 struct yankreg **old_y_current;
6376 int must_append;
6377 int *yank_type UNUSED;
6378{
6379 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6380 {
6381 emsg_invreg(name);
6382 return FAIL;
6383 }
6384
6385 /* Don't want to change the current (unnamed) register */
6386 *old_y_previous = y_previous;
6387 *old_y_current = y_current;
6388
6389 get_yank_register(name, TRUE);
6390 if (!y_append && !must_append)
6391 free_yank_all();
6392 return OK;
6393}
6394
6395 static void
6396finish_write_reg(name, old_y_previous, old_y_current)
6397 int name;
6398 struct yankreg *old_y_previous;
6399 struct yankreg *old_y_current;
6400{
6401# ifdef FEAT_CLIPBOARD
6402 /* Send text of clipboard register to the clipboard. */
6403 may_set_selection();
6404# endif
6405
6406 /* ':let @" = "val"' should change the meaning of the "" register */
6407 if (name != '"')
6408 y_previous = old_y_previous;
6409 y_current = old_y_current;
6410}
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412/*
6413 * Store string "str" in register "name".
6414 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6415 * If "must_append" is TRUE, always append to the register. Otherwise append
6416 * if "name" is an uppercase letter.
6417 * Note: "maxlen" and "must_append" don't work for the "/" register.
6418 * Careful: 'str' is modified, you may have to use a copy!
6419 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6420 */
6421 void
6422write_reg_contents(name, str, maxlen, must_append)
6423 int name;
6424 char_u *str;
6425 int maxlen;
6426 int must_append;
6427{
6428 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6429}
6430
6431 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006432write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6433 int name;
6434 char_u **strings;
6435 int maxlen UNUSED;
6436 int must_append;
6437 int yank_type;
6438 long block_len;
6439{
6440 struct yankreg *old_y_previous, *old_y_current;
6441
6442 if (name == '/'
6443#ifdef FEAT_EVAL
6444 || name == '='
6445#endif
6446 )
6447 {
6448 char_u *s;
6449
6450 if (strings[0] == NULL)
6451 s = (char_u *)"";
6452 else if (strings[1] != NULL)
6453 {
6454 EMSG(_("E883: search pattern and expression register may not "
6455 "contain two or more lines"));
6456 return;
6457 }
6458 else
6459 s = strings[0];
6460 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6461 return;
6462 }
6463
6464 if (name == '_') /* black hole: nothing to do */
6465 return;
6466
6467 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6468 &yank_type) == FAIL)
6469 return;
6470
6471 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6472
6473 finish_write_reg(name, old_y_previous, old_y_current);
6474}
6475
6476 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6478 int name;
6479 char_u *str;
6480 int maxlen;
6481 int must_append;
6482 int yank_type;
6483 long block_len;
6484{
6485 struct yankreg *old_y_previous, *old_y_current;
6486 long len;
6487
Bram Moolenaare7566042005-06-17 22:00:15 +00006488 if (maxlen >= 0)
6489 len = maxlen;
6490 else
6491 len = (long)STRLEN(str);
6492
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 /* Special case: '/' search pattern */
6494 if (name == '/')
6495 {
6496 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6497 return;
6498 }
6499
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006500 if (name == '#')
6501 {
6502 buf_T *buf;
6503
6504 if (VIM_ISDIGIT(*str))
6505 {
6506 int num = atoi((char *)str);
6507
6508 buf = buflist_findnr(num);
6509 if (buf == NULL)
6510 EMSGN(_(e_nobufnr), (long)num);
6511 }
6512 else
6513 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6514 TRUE, FALSE, FALSE));
6515 if (buf == NULL)
6516 return;
6517 curwin->w_alt_fnum = buf->b_fnum;
6518 return;
6519 }
6520
Bram Moolenaare7566042005-06-17 22:00:15 +00006521#ifdef FEAT_EVAL
6522 if (name == '=')
6523 {
6524 char_u *p, *s;
6525
6526 p = vim_strnsave(str, (int)len);
6527 if (p == NULL)
6528 return;
6529 if (must_append)
6530 {
6531 s = concat_str(get_expr_line_src(), p);
6532 vim_free(p);
6533 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006534 }
6535 set_expr_line(p);
6536 return;
6537 }
6538#endif
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (name == '_') /* black hole: nothing to do */
6541 return;
6542
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006543 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6544 &yank_type) == FAIL)
6545 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006547 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006549 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550}
6551#endif /* FEAT_EVAL */
6552
6553#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6554/*
6555 * Put a string into a register. When the register is not empty, the string
6556 * is appended.
6557 */
6558 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006559str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006561 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 char_u *str; /* string to put in register */
6563 long len; /* length of string */
6564 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006565 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006567 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 int lnum;
6569 long start;
6570 long i;
6571 int extra;
6572 int newlines; /* number of lines added */
6573 int extraline = 0; /* extra line at the end */
6574 int append = FALSE; /* append to last line in register */
6575 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006576 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006580 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 y_ptr->y_size = 0;
6582
Bram Moolenaard44347f2011-06-19 01:14:29 +02006583 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006584 type = ((str_list || (len > 0 && (str[len - 1] == NL
6585 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006586 ? MLINE : MCHAR);
6587 else
6588 type = yank_type;
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 /*
6591 * Count the number of lines within the string
6592 */
6593 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006594 if (str_list)
6595 {
6596 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006601 for (i = 0; i < len; i++)
6602 if (str[i] == '\n')
6603 ++newlines;
6604 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6605 {
6606 extraline = 1;
6607 ++newlines; /* count extra newline at the end */
6608 }
6609 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6610 {
6611 append = TRUE;
6612 --newlines; /* uncount newline when appending first line */
6613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006616 /* Without any lines make the register empty. */
6617 if (y_ptr->y_size + newlines == 0)
6618 {
6619 vim_free(y_ptr->y_array);
6620 y_ptr->y_array = NULL;
6621 return;
6622 }
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 /*
6625 * Allocate an array to hold the pointers to the new register lines.
6626 * If the register was not empty, move the existing lines to the new array.
6627 */
6628 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6629 * sizeof(char_u *), TRUE);
6630 if (pp == NULL) /* out of memory */
6631 return;
6632 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6633 pp[lnum] = y_ptr->y_array[lnum];
6634 vim_free(y_ptr->y_array);
6635 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 /*
6639 * Find the end of each line and save it into the array.
6640 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006641 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006643 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6644 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006645 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006646 pp[lnum] = vim_strnsave(*ss, i);
6647 if (i > maxlen)
6648 maxlen = i;
6649 }
6650 }
6651 else
6652 {
6653 for (start = 0; start < len + extraline; start += i + 1)
6654 {
6655 for (i = start; i < len; ++i) /* find the end of the line */
6656 if (str[i] == '\n')
6657 break;
6658 i -= start; /* i is now length of line */
6659 if (i > maxlen)
6660 maxlen = i;
6661 if (append)
6662 {
6663 --lnum;
6664 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6665 }
6666 else
6667 extra = 0;
6668 s = alloc((unsigned)(i + extra + 1));
6669 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006671 if (extra)
6672 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6673 if (append)
6674 vim_free(y_ptr->y_array[lnum]);
6675 if (i)
6676 mch_memmove(s + extra, str + start, (size_t)i);
6677 extra += i;
6678 s[extra] = NUL;
6679 y_ptr->y_array[lnum++] = s;
6680 while (--extra >= 0)
6681 {
6682 if (*s == NUL)
6683 *s = '\n'; /* replace NUL with newline */
6684 ++s;
6685 }
6686 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
6689 y_ptr->y_type = type;
6690 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (type == MBLOCK)
6692 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6693 else
6694 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695}
6696#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6697
6698 void
6699clear_oparg(oap)
6700 oparg_T *oap;
6701{
6702 vim_memset(oap, 0, sizeof(oparg_T));
6703}
6704
Bram Moolenaar7c626922005-02-07 22:01:03 +00006705static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006708 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 *
6710 * "Words" are counted by looking for boundaries between non-space and
6711 * space characters. (it seems to produce results that match 'wc'.)
6712 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006713 * Return value is byte count; word count for the line is added to "*wc".
6714 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 *
6716 * The function will only examine the first "limit" characters in the
6717 * line, stopping if it encounters an end-of-line (NUL byte). In that
6718 * case, eol_size will be added to the character count to account for
6719 * the size of the EOL character.
6720 */
6721 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006722line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 char_u *line;
6724 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006725 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 long limit;
6727 int eol_size;
6728{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006729 long i;
6730 long words = 0;
6731 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 int is_word = 0;
6733
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006734 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {
6736 if (is_word)
6737 {
6738 if (vim_isspace(line[i]))
6739 {
6740 words++;
6741 is_word = 0;
6742 }
6743 }
6744 else if (!vim_isspace(line[i]))
6745 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006746 ++chars;
6747#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006748 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006749#else
6750 ++i;
6751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753
6754 if (is_word)
6755 words++;
6756 *wc += words;
6757
6758 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006759 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006762 chars += eol_size;
6763 }
6764 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 return i;
6766}
6767
6768/*
6769 * Give some info about the position of the cursor (for "g CTRL-G").
6770 * In Visual mode, give some info about the selected region. (In this case,
6771 * the *_count_cursor variables store running totals for the selection.)
6772 */
6773 void
6774cursor_pos_info()
6775{
6776 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006777 char_u buf1[50];
6778 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006780 long byte_count = 0;
6781 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 long char_count = 0;
6783 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 long word_count = 0;
6785 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006786 int eol_size;
6787 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 long line_count_selected = 0;
6789 pos_T min_pos, max_pos;
6790 oparg_T oparg;
6791 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792
6793 /*
6794 * Compute the length of the file in characters.
6795 */
6796 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6797 {
6798 MSG(_(no_lines_msg));
6799 }
6800 else
6801 {
6802 if (get_fileformat(curbuf) == EOL_DOS)
6803 eol_size = 2;
6804 else
6805 eol_size = 1;
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (VIsual_active)
6808 {
6809 if (lt(VIsual, curwin->w_cursor))
6810 {
6811 min_pos = VIsual;
6812 max_pos = curwin->w_cursor;
6813 }
6814 else
6815 {
6816 min_pos = curwin->w_cursor;
6817 max_pos = VIsual;
6818 }
6819 if (*p_sel == 'e' && max_pos.col > 0)
6820 --max_pos.col;
6821
6822 if (VIsual_mode == Ctrl_V)
6823 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006824#ifdef FEAT_LINEBREAK
6825 char_u * saved_sbr = p_sbr;
6826
6827 /* Make 'sbr' empty for a moment to get the correct size. */
6828 p_sbr = empty_option;
6829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 oparg.is_VIsual = 1;
6831 oparg.block_mode = TRUE;
6832 oparg.op_type = OP_NOP;
6833 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006834 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006835#ifdef FEAT_LINEBREAK
6836 p_sbr = saved_sbr;
6837#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006838 if (curwin->w_curswant == MAXCOL)
6839 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 /* Swap the start, end vcol if needed */
6841 if (oparg.end_vcol < oparg.start_vcol)
6842 {
6843 oparg.end_vcol += oparg.start_vcol;
6844 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6845 oparg.end_vcol -= oparg.start_vcol;
6846 }
6847 }
6848 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
6851 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6852 {
6853 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006854 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
6856 ui_breakcheck();
6857 if (got_int)
6858 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006859 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 /* Do extra processing for VIsual mode. */
6863 if (VIsual_active
6864 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6865 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006866 char_u *s = NULL;
6867 long len = 0L;
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 switch (VIsual_mode)
6870 {
6871 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006872#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006876#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006878#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006879 s = bd.textstart;
6880 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 break;
6882 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006883 s = ml_get(lnum);
6884 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 break;
6886 case 'v':
6887 {
6888 colnr_T start_col = (lnum == min_pos.lnum)
6889 ? min_pos.col : 0;
6890 colnr_T end_col = (lnum == max_pos.lnum)
6891 ? max_pos.col - start_col + 1 : MAXCOL;
6892
Bram Moolenaardef9e822004-12-31 20:58:58 +00006893 s = ml_get(lnum) + start_col;
6894 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 }
6896 break;
6897 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006898 if (s != NULL)
6899 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006900 byte_count_cursor += line_count_info(s, &word_count_cursor,
6901 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006902 if (lnum == curbuf->b_ml.ml_line_count
6903 && !curbuf->b_p_eol
6904 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006905 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006906 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
6909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 {
6911 /* In non-visual mode, check for the line the cursor is on */
6912 if (lnum == curwin->w_cursor.lnum)
6913 {
6914 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006915 char_count_cursor += char_count;
6916 byte_count_cursor = byte_count +
6917 line_count_info(ml_get(lnum),
6918 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 (long)(curwin->w_cursor.col + 1), eol_size);
6920 }
6921 }
6922 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006923 byte_count += line_count_info(ml_get(lnum), &word_count,
6924 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 }
6926
6927 /* Correction for when last line doesn't have an EOL. */
6928 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006929 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (VIsual_active)
6932 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006933 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
6935 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006936 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006937 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6939 }
6940 else
6941 buf1[0] = NUL;
6942
Bram Moolenaar7c626922005-02-07 22:01:03 +00006943 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006944 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006945 vim_snprintf((char *)IObuff, IOSIZE,
6946 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 buf1, line_count_selected,
6948 (long)curbuf->b_ml.ml_line_count,
6949 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006950 byte_count_cursor, byte_count);
6951 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006952 vim_snprintf((char *)IObuff, IOSIZE,
6953 _("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 +00006954 buf1, line_count_selected,
6955 (long)curbuf->b_ml.ml_line_count,
6956 word_count_cursor, word_count,
6957 char_count_cursor, char_count,
6958 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 {
6962 p = ml_get_curline();
6963 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006964 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006966 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6967 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar7c626922005-02-07 22:01:03 +00006969 if (char_count_cursor == byte_count_cursor
6970 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006971 vim_snprintf((char *)IObuff, IOSIZE,
6972 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 (char *)buf1, (char *)buf2,
6974 (long)curwin->w_cursor.lnum,
6975 (long)curbuf->b_ml.ml_line_count,
6976 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006977 byte_count_cursor, byte_count);
6978 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006979 vim_snprintf((char *)IObuff, IOSIZE,
6980 _("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 +00006981 (char *)buf1, (char *)buf2,
6982 (long)curwin->w_cursor.lnum,
6983 (long)curbuf->b_ml.ml_line_count,
6984 word_count_cursor, word_count,
6985 char_count_cursor, char_count,
6986 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988
6989#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006990 byte_count = bomb_size();
6991 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006993 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
6995 /* Don't shorten this message, the user asked for it. */
6996 p = p_shm;
6997 p_shm = (char_u *)"";
6998 msg(IObuff);
6999 p_shm = p;
7000 }
7001}