blob: 090d3f8918a9c4763e8ed8a07e5f01b8be942d2e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061} y_regs[NUM_REGISTERS];
62
63static struct yankreg *y_current; /* ptr to current yankreg */
64static int y_append; /* TRUE when appending */
65static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
66
67/*
68 * structure used by block_prep, op_delete and op_yank for blockwise operators
69 * also op_change, op_shift, op_insert, op_replace - AKelly
70 */
71struct block_def
72{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000073 int startspaces; /* 'extra' cols before first char */
74 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000076 char_u *textstart; /* pointer to 1st char (partially) in block */
77 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 colnr_T start_vcol; /* start col of 1st char wholly inside block */
79 colnr_T end_vcol; /* start col of 1st char wholly after block */
80#ifdef FEAT_VISUALEXTRA
81 int is_short; /* TRUE if line is too short to fit in block */
82 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
83 int is_oneChar; /* TRUE if block within one character */
84 int pre_whitesp; /* screen cols of ws before block */
85 int pre_whitesp_c; /* chars of ws before block */
86 colnr_T end_char_vcols; /* number of vcols of post-block char */
87#endif
88 colnr_T start_char_vcols; /* number of vcols of pre-block char */
89};
90
91#ifdef FEAT_VISUALEXTRA
92static void shift_block __ARGS((oparg_T *oap, int amount));
93static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
94#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000095static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000096static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000097static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
98 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +000099static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
101static void mb_adjust_opend __ARGS((oparg_T *oap));
102#endif
103static void free_yank __ARGS((long));
104static void free_yank_all __ARGS((void));
105static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
106#ifdef FEAT_CLIPBOARD
107static void copy_yank_reg __ARGS((struct yankreg *reg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static void may_set_selection __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110static void dis_msg __ARGS((char_u *p, int skip_esc));
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
112static char_u *skip_comment __ARGS((char_u *line, int process, int include_space, int *is_comment));
113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar0eac8282014-04-12 12:26:36 +0200116static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118static int ends_in_white __ARGS((linenr_T lnum));
119#ifdef FEAT_COMMENTS
120static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
121static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
122#else
123static int fmt_check_par __ARGS((linenr_T));
124#endif
125
126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
129 * The third field indicates whether the operator always works on lines.
130 */
131static char opchars[][3] =
132{
133 {NUL, NUL, FALSE}, /* OP_NOP */
134 {'d', NUL, FALSE}, /* OP_DELETE */
135 {'y', NUL, FALSE}, /* OP_YANK */
136 {'c', NUL, FALSE}, /* OP_CHANGE */
137 {'<', NUL, TRUE}, /* OP_LSHIFT */
138 {'>', NUL, TRUE}, /* OP_RSHIFT */
139 {'!', NUL, TRUE}, /* OP_FILTER */
140 {'g', '~', FALSE}, /* OP_TILDE */
141 {'=', NUL, TRUE}, /* OP_INDENT */
142 {'g', 'q', TRUE}, /* OP_FORMAT */
143 {':', NUL, TRUE}, /* OP_COLON */
144 {'g', 'U', FALSE}, /* OP_UPPER */
145 {'g', 'u', FALSE}, /* OP_LOWER */
146 {'J', NUL, TRUE}, /* DO_JOIN */
147 {'g', 'J', TRUE}, /* DO_JOIN_NS */
148 {'g', '?', FALSE}, /* OP_ROT13 */
149 {'r', NUL, FALSE}, /* OP_REPLACE */
150 {'I', NUL, FALSE}, /* OP_INSERT */
151 {'A', NUL, FALSE}, /* OP_APPEND */
152 {'z', 'f', TRUE}, /* OP_FOLD */
153 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
154 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
155 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
156 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
157 {'z', 'd', TRUE}, /* OP_FOLDDEL */
158 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
159 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000160 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
168get_op_type(char1, char2)
169 int char1;
170 int char2;
171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
178 for (i = 0; ; ++i)
179 if (opchars[i][0] == char1 && opchars[i][1] == char2)
180 break;
181 return i;
182}
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/*
185 * Return TRUE if operator "op" always works on whole lines.
186 */
187 int
188op_on_lines(op)
189 int op;
190{
191 return opchars[op][2];
192}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194/*
195 * Get first operator command character.
196 * Returns 'g' or 'z' if there is another command character.
197 */
198 int
199get_op_char(optype)
200 int optype;
201{
202 return opchars[optype][0];
203}
204
205/*
206 * Get second operator command character.
207 */
208 int
209get_extra_op_char(optype)
210 int optype;
211{
212 return opchars[optype][1];
213}
214
215/*
216 * op_shift - handle a shift operation
217 */
218 void
219op_shift(oap, curs_top, amount)
220 oparg_T *oap;
221 int curs_top;
222 int amount;
223{
224 long i;
225 int first_char;
226 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 if (u_save((linenr_T)(oap->start.lnum - 1),
230 (linenr_T)(oap->end.lnum + 1)) == FAIL)
231 return;
232
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (oap->block_mode)
234 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
236 for (i = oap->line_count; --i >= 0; )
237 {
238 first_char = *ml_get_curline();
239 if (first_char == NUL) /* empty line */
240 curwin->w_cursor.col = 0;
241#ifdef FEAT_VISUALEXTRA
242 else if (oap->block_mode)
243 shift_block(oap, amount);
244#endif
245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
251 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000252 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254 ++curwin->w_cursor.lnum;
255 }
256
257 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200258#ifdef FEAT_FOLDING
259 /* The cursor line is not in a closed fold */
260 foldOpenCursor();
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (oap->block_mode)
264 {
265 curwin->w_cursor.lnum = oap->start.lnum;
266 curwin->w_cursor.col = block_col;
267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100268 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 curwin->w_cursor.lnum = oap->start.lnum;
271 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
272 }
273 else
274 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
275
276 if (oap->line_count > p_report)
277 {
278 if (oap->op_type == OP_RSHIFT)
279 s = (char_u *)">";
280 else
281 s = (char_u *)"<";
282 if (oap->line_count == 1)
283 {
284 if (amount == 1)
285 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
286 else
287 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
288 }
289 else
290 {
291 if (amount == 1)
292 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
293 oap->line_count, s);
294 else
295 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
296 oap->line_count, s, amount);
297 }
298 msg(IObuff);
299 }
300
301 /*
302 * Set "'[" and "']" marks.
303 */
304 curbuf->b_op_start = oap->start;
305 curbuf->b_op_end.lnum = oap->end.lnum;
306 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
307 if (curbuf->b_op_end.col > 0)
308 --curbuf->b_op_end.col;
309}
310
311/*
312 * shift the current line one shiftwidth left (if left != 0) or right
313 * leaves cursor on first blank in the line
314 */
315 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000316shift_line(left, round, amount, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 int left;
318 int round;
319 int amount;
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000320 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int count;
323 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100324 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
326 count = get_indent(); /* get current indent */
327
328 if (round) /* round off indent */
329 {
330 i = count / p_sw; /* number of p_sw rounded down */
331 j = count % p_sw; /* extra spaces */
332 if (j && left) /* first remove extra spaces */
333 --amount;
334 if (left)
335 {
336 i -= amount;
337 if (i < 0)
338 i = 0;
339 }
340 else
341 i += amount;
342 count = i * p_sw;
343 }
344 else /* original vi indent */
345 {
346 if (left)
347 {
348 count -= p_sw * amount;
349 if (count < 0)
350 count = 0;
351 }
352 else
353 count += p_sw * amount;
354 }
355
356 /* Set new indent */
357#ifdef FEAT_VREPLACE
358 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000359 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 else
361#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000362 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
365#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
366/*
367 * Shift one line of the current block one shiftwidth right or left.
368 * Leaves cursor on first character in block.
369 */
370 static void
371shift_block(oap, amount)
372 oparg_T *oap;
373 int amount;
374{
375 int left = (oap->op_type == OP_LSHIFT);
376 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000377 int total;
378 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100380 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int p_ts = (int)curbuf->b_p_ts;
382 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int i = 0, j = 0;
386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_RIGHTLEFT
388 int old_p_ri = p_ri;
389
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200390 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391#endif
392
393 State = INSERT; /* don't want REPLACE for State */
394 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
395 if (bd.is_short)
396 return;
397
398 /* total is number of screen columns to be inserted/removed */
399 total = amount * p_sw;
400 oldp = ml_get_curline();
401
402 if (!left)
403 {
404 /*
405 * 1. Get start vcol
406 * 2. Total ws vcols
407 * 3. Divvy into TABs & spp
408 * 4. Construct new string
409 */
410 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
411 ws_vcol = bd.start_vcol - bd.pre_whitesp;
412 if (bd.startspaces)
413 {
414#ifdef FEAT_MBYTE
415 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000416 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000419 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421 for ( ; vim_iswhite(*bd.textstart); )
422 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200423 /* TODO: is passing bd.textstart for start of the line OK? */
424 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
425 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 total += incr;
427 bd.start_vcol += incr;
428 }
429 /* OK, now total=all the VWS reqd, and textstart points at the 1st
430 * non-ws char in the block. */
431 if (!curbuf->b_p_et)
432 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
433 if (i)
434 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
435 else
436 j = total;
437 /* if we're splitting a TAB, allow for it */
438 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
439 len = (int)STRLEN(bd.textstart) + 1;
440 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
441 if (newp == NULL)
442 return;
443 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
444 mch_memmove(newp, oldp, (size_t)bd.textcol);
445 copy_chars(newp + bd.textcol, (size_t)i, TAB);
446 copy_spaces(newp + bd.textcol + i, (size_t)j);
447 /* the end */
448 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
449 }
450 else /* left */
451 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000452 colnr_T destination_col; /* column to which text in block will
453 be shifted */
454 char_u *verbatim_copy_end; /* end of the part of the line which is
455 copied verbatim */
456 colnr_T verbatim_copy_width;/* the (displayed) width of this part
457 of line */
458 unsigned fill; /* nr of spaces that replace a TAB */
459 unsigned new_line_len; /* the length of the line after the
460 block shift */
461 size_t block_space_width;
462 size_t shift_amount;
463 char_u *non_white = bd.textstart;
464 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000466 /*
467 * Firstly, let's find the first non-whitespace character that is
468 * displayed after the block's start column and the character's column
469 * number. Also, let's calculate the width of all the whitespace
470 * characters that are displayed in the block and precede the searched
471 * non-whitespace character.
472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000474 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
475 * the part of which is displayed at the block's beginning. Let's start
476 * searching from the next character. */
477 if (bd.startspaces)
478 mb_ptr_adv(non_white);
479
480 /* The character's column is in "bd.start_vcol". */
481 non_white_col = bd.start_vcol;
482
483 while (vim_iswhite(*non_white))
484 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200485 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000486 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 }
488
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 block_space_width = non_white_col - oap->start_vcol;
490 /* We will shift by "total" or "block_space_width", whichever is less.
491 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000492 shift_amount = (block_space_width < (size_t)total
493 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494
495 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000496 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
498 /* Now let's find out how much of the beginning of the line we can
499 * reuse without modification. */
500 verbatim_copy_end = bd.textstart;
501 verbatim_copy_width = bd.start_vcol;
502
503 /* If "bd.startspaces" is set, "bd.textstart" points to the character
504 * preceding the block. We have to subtract its width to obtain its
505 * column number. */
506 if (bd.startspaces)
507 verbatim_copy_width -= bd.start_char_vcols;
508 while (verbatim_copy_width < destination_col)
509 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 char_u *line = verbatim_copy_end;
511
512 /* TODO: is passing verbatim_copy_end for start of the line OK? */
513 incr = lbr_chartabsize(line, verbatim_copy_end,
514 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 if (verbatim_copy_width + incr > destination_col)
516 break;
517 verbatim_copy_width += incr;
518 mb_ptr_adv(verbatim_copy_end);
519 }
520
521 /* If "destination_col" is different from the width of the initial
522 * part of the line that will be copied, it means we encountered a tab
523 * character, which we will have to partly replace with spaces. */
524 fill = destination_col - verbatim_copy_width;
525
526 /* The replacement line will consist of:
527 * - the beginning of the original line up to "verbatim_copy_end",
528 * - "fill" number of spaces,
529 * - the rest of the line, pointed to by non_white. */
530 new_line_len = (unsigned)(verbatim_copy_end - oldp)
531 + fill
532 + (unsigned)STRLEN(non_white) + 1;
533
534 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (newp == NULL)
536 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
538 copy_spaces(newp + (verbatim_copy_end - oldp), (size_t)fill);
539 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 /* replace the line */
542 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
543 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
544 State = oldstate;
545 curwin->w_cursor.col = oldcol;
546#ifdef FEAT_RIGHTLEFT
547 p_ri = old_p_ri;
548#endif
549}
550#endif
551
552#ifdef FEAT_VISUALEXTRA
553/*
554 * Insert string "s" (b_insert ? before : after) block :AKelly
555 * Caller must prepare for undo.
556 */
557 static void
558block_insert(oap, s, b_insert, bdp)
559 oparg_T *oap;
560 char_u *s;
561 int b_insert;
562 struct block_def *bdp;
563{
564 int p_ts;
565 int count = 0; /* extra spaces to replace a cut TAB */
566 int spaces = 0; /* non-zero if cutting a TAB */
567 colnr_T offset; /* pointer along new line */
568 unsigned s_len; /* STRLEN(s) */
569 char_u *newp, *oldp; /* new, old lines */
570 linenr_T lnum; /* loop var */
571 int oldstate = State;
572
573 State = INSERT; /* don't want REPLACE for State */
574 s_len = (unsigned)STRLEN(s);
575
576 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
577 {
578 block_prep(oap, bdp, lnum, TRUE);
579 if (bdp->is_short && b_insert)
580 continue; /* OP_INSERT, line ends before block start */
581
582 oldp = ml_get(lnum);
583
584 if (b_insert)
585 {
586 p_ts = bdp->start_char_vcols;
587 spaces = bdp->startspaces;
588 if (spaces != 0)
589 count = p_ts - 1; /* we're cutting a TAB */
590 offset = bdp->textcol;
591 }
592 else /* append */
593 {
594 p_ts = bdp->end_char_vcols;
595 if (!bdp->is_short) /* spaces = padding after block */
596 {
597 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
598 if (spaces != 0)
599 count = p_ts - 1; /* we're cutting a TAB */
600 offset = bdp->textcol + bdp->textlen - (spaces != 0);
601 }
602 else /* spaces = padding to block edge */
603 {
604 /* if $ used, just append to EOL (ie spaces==0) */
605 if (!bdp->is_MAX)
606 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
607 count = spaces;
608 offset = bdp->textcol + bdp->textlen;
609 }
610 }
611
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200612#ifdef FEAT_MBYTE
613 if (has_mbyte && spaces > 0)
614 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100615 int off;
616
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200617 /* Avoid starting halfway a multi-byte character. */
618 if (b_insert)
619 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100620 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200621 }
622 else
623 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100624 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200625 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200626 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100627 spaces -= off;
628 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200629 }
630#endif
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
633 if (newp == NULL)
634 continue;
635
636 /* copy up to shifted part */
637 mch_memmove(newp, oldp, (size_t)(offset));
638 oldp += offset;
639
640 /* insert pre-padding */
641 copy_spaces(newp + offset, (size_t)spaces);
642
643 /* copy the new text */
644 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
645 offset += s_len;
646
647 if (spaces && !bdp->is_short)
648 {
649 /* insert post-padding */
650 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
651 /* We're splitting a TAB, don't copy it. */
652 oldp++;
653 /* We allowed for that TAB, remember this now */
654 count++;
655 }
656
657 if (spaces > 0)
658 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000659 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 ml_replace(lnum, newp, FALSE);
662
663 if (lnum == oap->end.lnum)
664 {
665 /* Set "']" mark to the end of the block instead of the end of
666 * the insert in the first line. */
667 curbuf->b_op_end.lnum = oap->end.lnum;
668 curbuf->b_op_end.col = offset;
669 }
670 } /* for all lnum */
671
672 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
673
674 State = oldstate;
675}
676#endif
677
678#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
679/*
680 * op_reindent - handle reindenting a block of lines.
681 */
682 void
683op_reindent(oap, how)
684 oparg_T *oap;
685 int (*how) __ARGS((void));
686{
687 long i;
688 char_u *l;
689 int count;
690 linenr_T first_changed = 0;
691 linenr_T last_changed = 0;
692 linenr_T start_lnum = curwin->w_cursor.lnum;
693
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000694 /* Don't even try when 'modifiable' is off. */
695 if (!curbuf->b_p_ma)
696 {
697 EMSG(_(e_modifiable));
698 return;
699 }
700
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 for (i = oap->line_count; --i >= 0 && !got_int; )
702 {
703 /* it's a slow thing to do, so give feedback so there's no worry that
704 * the computer's just hung. */
705
706 if (i > 1
707 && (i % 50 == 0 || i == oap->line_count - 1)
708 && oap->line_count > p_report)
709 smsg((char_u *)_("%ld lines to indent... "), i);
710
711 /*
712 * Be vi-compatible: For lisp indenting the first line is not
713 * indented, unless there is only one line.
714 */
715#ifdef FEAT_LISP
716 if (i != oap->line_count - 1 || oap->line_count == 1
717 || how != get_lisp_indent)
718#endif
719 {
720 l = skipwhite(ml_get_curline());
721 if (*l == NUL) /* empty or blank line */
722 count = 0;
723 else
724 count = how(); /* get the indent for this line */
725
726 if (set_indent(count, SIN_UNDO))
727 {
728 /* did change the indent, call changed_lines() later */
729 if (first_changed == 0)
730 first_changed = curwin->w_cursor.lnum;
731 last_changed = curwin->w_cursor.lnum;
732 }
733 }
734 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000735 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737
738 /* put cursor on first non-blank of indented line */
739 curwin->w_cursor.lnum = start_lnum;
740 beginline(BL_SOL | BL_FIX);
741
742 /* Mark changed lines so that they will be redrawn. When Visual
743 * highlighting was present, need to continue until the last line. When
744 * there is no change still need to remove the Visual highlighting. */
745 if (last_changed != 0)
746 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 else if (oap->is_VIsual)
750 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
752 if (oap->line_count > p_report)
753 {
754 i = oap->line_count - (i + 1);
755 if (i == 1)
756 MSG(_("1 line indented "));
757 else
758 smsg((char_u *)_("%ld lines indented "), i);
759 }
760 /* set '[ and '] marks */
761 curbuf->b_op_start = oap->start;
762 curbuf->b_op_end = oap->end;
763}
764#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
765
766#if defined(FEAT_EVAL) || defined(PROTO)
767/*
768 * Keep the last expression line here, for repeating.
769 */
770static char_u *expr_line = NULL;
771
772/*
773 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
774 * Returns '=' when OK, NUL otherwise.
775 */
776 int
777get_expr_register()
778{
779 char_u *new_line;
780
781 new_line = getcmdline('=', 0L, 0);
782 if (new_line == NULL)
783 return NUL;
784 if (*new_line == NUL) /* use previous line */
785 vim_free(new_line);
786 else
787 set_expr_line(new_line);
788 return '=';
789}
790
791/*
792 * Set the expression for the '=' register.
793 * Argument must be an allocated string.
794 */
795 void
796set_expr_line(new_line)
797 char_u *new_line;
798{
799 vim_free(expr_line);
800 expr_line = new_line;
801}
802
803/*
804 * Get the result of the '=' register expression.
805 * Returns a pointer to allocated memory, or NULL for failure.
806 */
807 char_u *
808get_expr_line()
809{
810 char_u *expr_copy;
811 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000812 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 if (expr_line == NULL)
815 return NULL;
816
817 /* Make a copy of the expression, because evaluating it may cause it to be
818 * changed. */
819 expr_copy = vim_strsave(expr_line);
820 if (expr_copy == NULL)
821 return NULL;
822
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000823 /* When we are invoked recursively limit the evaluation to 10 levels.
824 * Then return the string as-is. */
825 if (nested >= 10)
826 return expr_copy;
827
828 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000830 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 vim_free(expr_copy);
832 return rv;
833}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000834
835/*
836 * Get the '=' register expression itself, without evaluating it.
837 */
838 char_u *
839get_expr_line_src()
840{
841 if (expr_line == NULL)
842 return NULL;
843 return vim_strsave(expr_line);
844}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif /* FEAT_EVAL */
846
847/*
848 * Check if 'regname' is a valid name of a yank register.
849 * Note: There is no check for 0 (default register), caller should do this
850 */
851 int
852valid_yank_reg(regname, writing)
853 int regname;
854 int writing; /* if TRUE check for writable registers */
855{
856 if ( (regname > 0 && ASCII_ISALNUM(regname))
857 || (!writing && vim_strchr((char_u *)
858#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100859 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100861 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100864 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 || regname == '"'
866 || regname == '-'
867 || regname == '_'
868#ifdef FEAT_CLIPBOARD
869 || regname == '*'
870 || regname == '+'
871#endif
872#ifdef FEAT_DND
873 || (!writing && regname == '~')
874#endif
875 )
876 return TRUE;
877 return FALSE;
878}
879
880/*
881 * Set y_current and y_append, according to the value of "regname".
882 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000883 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 *
885 * If regname is 0 and writing, use register 0
886 * If regname is 0 and reading, use previous register
887 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000888 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889get_yank_register(regname, writing)
890 int regname;
891 int writing;
892{
893 int i;
894
895 y_append = FALSE;
896 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
897 {
898 y_current = y_previous;
899 return;
900 }
901 i = regname;
902 if (VIM_ISDIGIT(i))
903 i -= '0';
904 else if (ASCII_ISLOWER(i))
905 i = CharOrdLow(i) + 10;
906 else if (ASCII_ISUPPER(i))
907 {
908 i = CharOrdUp(i) + 10;
909 y_append = TRUE;
910 }
911 else if (regname == '-')
912 i = DELETION_REGISTER;
913#ifdef FEAT_CLIPBOARD
914 /* When selection is not available, use register 0 instead of '*' */
915 else if (clip_star.available && regname == '*')
916 i = STAR_REGISTER;
917 /* When clipboard is not available, use register 0 instead of '+' */
918 else if (clip_plus.available && regname == '+')
919 i = PLUS_REGISTER;
920#endif
921#ifdef FEAT_DND
922 else if (!writing && regname == '~')
923 i = TILDE_REGISTER;
924#endif
925 else /* not 0-9, a-z, A-Z or '-': use register 0 */
926 i = 0;
927 y_current = &(y_regs[i]);
928 if (writing) /* remember the register we write into for do_put() */
929 y_previous = y_current;
930}
931
Bram Moolenaar8299df92004-07-10 09:47:34 +0000932#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
934 * When "regname" is a clipboard register, obtain the selection. If it's not
935 * available return zero, otherwise return "regname".
936 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938may_get_selection(regname)
939 int regname;
940{
941 if (regname == '*')
942 {
943 if (!clip_star.available)
944 regname = 0;
945 else
946 clip_get_selection(&clip_star);
947 }
948 else if (regname == '+')
949 {
950 if (!clip_plus.available)
951 regname = 0;
952 else
953 clip_get_selection(&clip_plus);
954 }
955 return regname;
956}
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959/*
960 * Obtain the contents of a "normal" register. The register is made empty.
961 * The returned pointer has allocated memory, use put_register() later.
962 */
963 void *
964get_register(name, copy)
965 int name;
966 int copy; /* make a copy, if FALSE make register empty. */
967{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000968 struct yankreg *reg;
969 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971#ifdef FEAT_CLIPBOARD
972 /* When Visual area changed, may have to update selection. Obtain the
973 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000974 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200976 if (clip_isautosel_star())
977 clip_update_selection(&clip_star);
978 may_get_selection(name);
979 }
980 if (name == '+' && clip_plus.available)
981 {
982 if (clip_isautosel_plus())
983 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 may_get_selection(name);
985 }
986#endif
987
988 get_yank_register(name, 0);
989 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
990 if (reg != NULL)
991 {
992 *reg = *y_current;
993 if (copy)
994 {
995 /* If we run out of memory some or all of the lines are empty. */
996 if (reg->y_size == 0)
997 reg->y_array = NULL;
998 else
999 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1000 * reg->y_size));
1001 if (reg->y_array != NULL)
1002 {
1003 for (i = 0; i < reg->y_size; ++i)
1004 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1005 }
1006 }
1007 else
1008 y_current->y_array = NULL;
1009 }
1010 return (void *)reg;
1011}
1012
1013/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001014 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 */
1016 void
1017put_register(name, reg)
1018 int name;
1019 void *reg;
1020{
1021 get_yank_register(name, 0);
1022 free_yank_all();
1023 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
1032 void
1033free_register(reg)
1034 void *reg;
1035{
1036 struct yankreg tmp;
1037
1038 tmp = *y_current;
1039 *y_current = *(struct yankreg *)reg;
1040 free_yank_all();
1041 vim_free(reg);
1042 *y_current = tmp;
1043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
1045#if defined(FEAT_MOUSE) || defined(PROTO)
1046/*
1047 * return TRUE if the current yank register has type MLINE
1048 */
1049 int
1050yank_register_mline(regname)
1051 int regname;
1052{
1053 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1054 return FALSE;
1055 if (regname == '_') /* black hole is always empty */
1056 return FALSE;
1057 get_yank_register(regname, FALSE);
1058 return (y_current->y_type == MLINE);
1059}
1060#endif
1061
1062/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001063 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
1068do_record(c)
1069 int c;
1070{
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 char_u *p;
1072 static int regname;
1073 struct yankreg *old_y_previous, *old_y_current;
1074 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 if (Recording == FALSE) /* start recording */
1077 {
1078 /* registers 0-9, a-z and " are allowed */
1079 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1080 retval = FAIL;
1081 else
1082 {
1083 Recording = TRUE;
1084 showmode();
1085 regname = c;
1086 retval = OK;
1087 }
1088 }
1089 else /* stop recording */
1090 {
1091 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1093 * needs to be removed again to put it in a register. exec_reg then
1094 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
1096 Recording = FALSE;
1097 MSG("");
1098 p = get_recorded();
1099 if (p == NULL)
1100 retval = FAIL;
1101 else
1102 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001103 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1104 vim_unescape_csi(p);
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * We don't want to change the default register here, so save and
1108 * restore the current register name.
1109 */
1110 old_y_previous = y_previous;
1111 old_y_current = y_current;
1112
1113 retval = stuff_yank(regname, p);
1114
1115 y_previous = old_y_previous;
1116 y_current = old_y_current;
1117 }
1118 }
1119 return retval;
1120}
1121
1122/*
1123 * Stuff string "p" into yank register "regname" as a single line (append if
1124 * uppercase). "p" must have been alloced.
1125 *
1126 * return FAIL for failure, OK otherwise
1127 */
1128 static int
1129stuff_yank(regname, p)
1130 int regname;
1131 char_u *p;
1132{
1133 char_u *lp;
1134 char_u **pp;
1135
1136 /* check for read-only register */
1137 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1138 {
1139 vim_free(p);
1140 return FAIL;
1141 }
1142 if (regname == '_') /* black hole: don't do anything */
1143 {
1144 vim_free(p);
1145 return OK;
1146 }
1147 get_yank_register(regname, TRUE);
1148 if (y_append && y_current->y_array != NULL)
1149 {
1150 pp = &(y_current->y_array[y_current->y_size - 1]);
1151 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1152 if (lp == NULL)
1153 {
1154 vim_free(p);
1155 return FAIL;
1156 }
1157 STRCPY(lp, *pp);
1158 STRCAT(lp, p);
1159 vim_free(p);
1160 vim_free(*pp);
1161 *pp = lp;
1162 }
1163 else
1164 {
1165 free_yank_all();
1166 if ((y_current->y_array =
1167 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1168 {
1169 vim_free(p);
1170 return FAIL;
1171 }
1172 y_current->y_array[0] = p;
1173 y_current->y_size = 1;
1174 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1175 }
1176 return OK;
1177}
1178
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * execute a yank register: copy it into the stuff buffer
1183 *
1184 * return FAIL for failure, OK otherwise
1185 */
1186 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001187do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 int regname;
1189 int colon; /* insert ':' before each line */
1190 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001191 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 long i;
1194 char_u *p;
1195 int retval = OK;
1196 int remap;
1197
1198 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001200 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001201 {
1202 EMSG(_("E748: No previously used register"));
1203 return FAIL;
1204 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001205 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 /* check for valid regname */
1208 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215#ifdef FEAT_CLIPBOARD
1216 regname = may_get_selection(regname);
1217#endif
1218
1219 if (regname == '_') /* black hole: don't stuff anything */
1220 return OK;
1221
1222#ifdef FEAT_CMDHIST
1223 if (regname == ':') /* use last command line */
1224 {
1225 if (last_cmdline == NULL)
1226 {
1227 EMSG(_(e_nolastcmd));
1228 return FAIL;
1229 }
1230 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1231 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001232 /* Escape all control characters with a CTRL-V */
1233 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001234 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001235 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001236 {
1237 /* When in Visual mode "'<,'>" will be prepended to the command.
1238 * Remove it when it's already there. */
1239 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001240 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001241 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001242 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001243 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001244 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246#endif
1247#ifdef FEAT_EVAL
1248 else if (regname == '=')
1249 {
1250 p = get_expr_line();
1251 if (p == NULL)
1252 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001253 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 vim_free(p);
1255 }
1256#endif
1257 else if (regname == '.') /* use last inserted text */
1258 {
1259 p = get_last_insert_save();
1260 if (p == NULL)
1261 {
1262 EMSG(_(e_noinstext));
1263 return FAIL;
1264 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001265 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 vim_free(p);
1267 }
1268 else
1269 {
1270 get_yank_register(regname, FALSE);
1271 if (y_current->y_array == NULL)
1272 return FAIL;
1273
1274 /* Disallow remaping for ":@r". */
1275 remap = colon ? REMAP_NONE : REMAP_YES;
1276
1277 /*
1278 * Insert lines into typeahead buffer, from last one to first one.
1279 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001280 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 for (i = y_current->y_size; --i >= 0; )
1282 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 char_u *escaped;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 /* insert NL between lines and after last line if type is MLINE */
1286 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1287 || addcr)
1288 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001289 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return FAIL;
1291 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001292 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1293 if (escaped == NULL)
1294 return FAIL;
1295 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1296 vim_free(escaped);
1297 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001299 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 == FAIL)
1301 return FAIL;
1302 }
1303 Exec_reg = TRUE; /* disable the 'q' command */
1304 }
1305 return retval;
1306}
1307
1308/*
1309 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1310 * used only after other typeahead has been processed.
1311 */
1312 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001313put_reedit_in_typebuf(silent)
1314 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 char_u buf[3];
1317
1318 if (restart_edit != NUL)
1319 {
1320 if (restart_edit == 'V')
1321 {
1322 buf[0] = 'g';
1323 buf[1] = 'R';
1324 buf[2] = NUL;
1325 }
1326 else
1327 {
1328 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1329 buf[1] = NUL;
1330 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001331 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 restart_edit = NUL;
1333 }
1334}
1335
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001336/*
1337 * Insert register contents "s" into the typeahead buffer, so that it will be
1338 * executed again.
1339 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1340 * no remapping.
1341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001343put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *s;
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001345 int esc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001347 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int retval = OK;
1350
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001351 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001355 {
1356 char_u *p;
1357
1358 if (esc)
1359 p = vim_strsave_escape_csi(s);
1360 else
1361 p = s;
1362 if (p == NULL)
1363 retval = FAIL;
1364 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001365 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1366 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001367 if (esc)
1368 vim_free(p);
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001371 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 return retval;
1373}
1374
1375/*
1376 * Insert a yank register: copy it into the Read buffer.
1377 * Used by CTRL-R command and middle mouse button in insert mode.
1378 *
1379 * return FAIL for failure, OK otherwise
1380 */
1381 int
1382insert_reg(regname, literally)
1383 int regname;
1384 int literally; /* insert literally, not as if typed */
1385{
1386 long i;
1387 int retval = OK;
1388 char_u *arg;
1389 int allocated;
1390
1391 /*
1392 * It is possible to get into an endless loop by having CTRL-R a in
1393 * register a and then, in insert mode, doing CTRL-R a.
1394 * If you hit CTRL-C, the loop will be broken here.
1395 */
1396 ui_breakcheck();
1397 if (got_int)
1398 return FAIL;
1399
1400 /* check for valid regname */
1401 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1402 return FAIL;
1403
1404#ifdef FEAT_CLIPBOARD
1405 regname = may_get_selection(regname);
1406#endif
1407
1408 if (regname == '.') /* insert last inserted text */
1409 retval = stuff_inserted(NUL, 1L, TRUE);
1410 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1411 {
1412 if (arg == NULL)
1413 return FAIL;
1414 stuffescaped(arg, literally);
1415 if (allocated)
1416 vim_free(arg);
1417 }
1418 else /* name or number register */
1419 {
1420 get_yank_register(regname, FALSE);
1421 if (y_current->y_array == NULL)
1422 retval = FAIL;
1423 else
1424 {
1425 for (i = 0; i < y_current->y_size; ++i)
1426 {
1427 stuffescaped(y_current->y_array[i], literally);
1428 /*
1429 * Insert a newline between lines and after last line if
1430 * y_type is MLINE.
1431 */
1432 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1433 stuffcharReadbuff('\n');
1434 }
1435 }
1436 }
1437
1438 return retval;
1439}
1440
1441/*
1442 * Stuff a string into the typeahead buffer, such that edit() will insert it
1443 * literally ("literally" TRUE) or interpret is as typed characters.
1444 */
1445 static void
1446stuffescaped(arg, literally)
1447 char_u *arg;
1448 int literally;
1449{
1450 int c;
1451 char_u *start;
1452
1453 while (*arg != NUL)
1454 {
1455 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1456 * stuff K_SPECIAL to get the effect of a special key when "literally"
1457 * is TRUE. */
1458 start = arg;
1459 while ((*arg >= ' '
1460#ifndef EBCDIC
1461 && *arg < DEL /* EBCDIC: chars above space are normal */
1462#endif
1463 )
1464 || (*arg == K_SPECIAL && !literally))
1465 ++arg;
1466 if (arg > start)
1467 stuffReadbuffLen(start, (long)(arg - start));
1468
1469 /* stuff a single special character */
1470 if (*arg != NUL)
1471 {
1472#ifdef FEAT_MBYTE
1473 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001474 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
1476#endif
1477 c = *arg++;
1478 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1479 stuffcharReadbuff(Ctrl_V);
1480 stuffcharReadbuff(c);
1481 }
1482 }
1483}
1484
1485/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001486 * If "regname" is a special register, return TRUE and store a pointer to its
1487 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001489 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490get_spec_reg(regname, argp, allocated, errmsg)
1491 int regname;
1492 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001493 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int errmsg; /* give error message when failing */
1495{
1496 int cnt;
1497
1498 *argp = NULL;
1499 *allocated = FALSE;
1500 switch (regname)
1501 {
1502 case '%': /* file name */
1503 if (errmsg)
1504 check_fname(); /* will give emsg if not set */
1505 *argp = curbuf->b_fname;
1506 return TRUE;
1507
1508 case '#': /* alternate file name */
1509 *argp = getaltfname(errmsg); /* may give emsg if not set */
1510 return TRUE;
1511
1512#ifdef FEAT_EVAL
1513 case '=': /* result of expression */
1514 *argp = get_expr_line();
1515 *allocated = TRUE;
1516 return TRUE;
1517#endif
1518
1519 case ':': /* last command line */
1520 if (last_cmdline == NULL && errmsg)
1521 EMSG(_(e_nolastcmd));
1522 *argp = last_cmdline;
1523 return TRUE;
1524
1525 case '/': /* last search-pattern */
1526 if (last_search_pat() == NULL && errmsg)
1527 EMSG(_(e_noprevre));
1528 *argp = last_search_pat();
1529 return TRUE;
1530
1531 case '.': /* last inserted text */
1532 *argp = get_last_insert_save();
1533 *allocated = TRUE;
1534 if (*argp == NULL && errmsg)
1535 EMSG(_(e_noinstext));
1536 return TRUE;
1537
1538#ifdef FEAT_SEARCHPATH
1539 case Ctrl_F: /* Filename under cursor */
1540 case Ctrl_P: /* Path under cursor, expand via "path" */
1541 if (!errmsg)
1542 return FALSE;
1543 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001544 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 *allocated = TRUE;
1546 return TRUE;
1547#endif
1548
1549 case Ctrl_W: /* word under cursor */
1550 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1551 if (!errmsg)
1552 return FALSE;
1553 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1554 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1555 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1556 *allocated = TRUE;
1557 return TRUE;
1558
1559 case '_': /* black hole: always empty */
1560 *argp = (char_u *)"";
1561 return TRUE;
1562 }
1563
1564 return FALSE;
1565}
1566
1567/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001568 * Paste a yank register into the command line.
1569 * Only for non-special registers.
1570 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * insert_reg() can't be used here, because special characters from the
1572 * register contents will be interpreted as commands.
1573 *
1574 * return FAIL for failure, OK otherwise
1575 */
1576 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001577cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 int regname;
1579 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001580 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 get_yank_register(regname, FALSE);
1585 if (y_current->y_array == NULL)
1586 return FAIL;
1587
1588 for (i = 0; i < y_current->y_size; ++i)
1589 {
1590 cmdline_paste_str(y_current->y_array[i], literally);
1591
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001592 /* Insert ^M between lines and after last line if type is MLINE.
1593 * Don't do this when "remcr" is TRUE and the next line is empty. */
1594 if (y_current->y_type == MLINE
1595 || (i < y_current->y_size - 1
1596 && !(remcr
1597 && i == y_current->y_size - 2
1598 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 cmdline_paste_str((char_u *)"\r", literally);
1600
1601 /* Check for CTRL-C, in case someone tries to paste a few thousand
1602 * lines and gets bored. */
1603 ui_breakcheck();
1604 if (got_int)
1605 return FAIL;
1606 }
1607 return OK;
1608}
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1611/*
1612 * Adjust the register name pointed to with "rp" for the clipboard being
1613 * used always and the clipboard being available.
1614 */
1615 void
1616adjust_clip_reg(rp)
1617 int *rp;
1618{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001619 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1620 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001621 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1622 {
1623 if (clip_unnamed != 0)
1624 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001625 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001626 else
1627 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1628 ? '+' : '*';
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!clip_star.available && *rp == '*')
1631 *rp = 0;
1632 if (!clip_plus.available && *rp == '+')
1633 *rp = 0;
1634}
1635#endif
1636
1637/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001638 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001640 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
1642 int
1643op_delete(oap)
1644 oparg_T *oap;
1645{
1646 int n;
1647 linenr_T lnum;
1648 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 char_u *newp, *oldp;
1650 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1652 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001653 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1656 return OK;
1657
1658 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1659 if (oap->empty)
1660 return u_save_cursor();
1661
1662 if (!curbuf->b_p_ma)
1663 {
1664 EMSG(_(e_modifiable));
1665 return FAIL;
1666 }
1667
1668#ifdef FEAT_CLIPBOARD
1669 adjust_clip_reg(&oap->regname);
1670#endif
1671
1672#ifdef FEAT_MBYTE
1673 if (has_mbyte)
1674 mb_adjust_opend(oap);
1675#endif
1676
Bram Moolenaard04b7502010-07-08 22:27:55 +02001677 /*
1678 * Imitate the strange Vi behaviour: If the delete spans more than one
1679 * line and motion_type == MCHAR and the result is a blank line, make the
1680 * delete linewise. Don't do this for the change command or Visual mode.
1681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001684 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001686 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && oap->op_type == OP_DELETE)
1688 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001689 ptr = ml_get(oap->end.lnum) + oap->end.col;
1690 if (*ptr != NUL)
1691 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ptr = skipwhite(ptr);
1693 if (*ptr == NUL && inindent(0))
1694 oap->motion_type = MLINE;
1695 }
1696
Bram Moolenaard04b7502010-07-08 22:27:55 +02001697 /*
1698 * Check for trying to delete (e.g. "D") in an empty line.
1699 * Note: For the change operator it is ok.
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if ( oap->motion_type == MCHAR
1702 && oap->line_count == 1
1703 && oap->op_type == OP_DELETE
1704 && *ml_get(oap->start.lnum) == NUL)
1705 {
1706 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001707 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 * 'cpoptions' (Vi compatible).
1709 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001710#ifdef FEAT_VIRTUALEDIT
1711 if (virtual_op)
1712 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1713 * marks as if it happened. */
1714 goto setmarks;
1715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1717 beep_flush();
1718 return OK;
1719 }
1720
Bram Moolenaard04b7502010-07-08 22:27:55 +02001721 /*
1722 * Do a yank of whatever we're about to delete.
1723 * If a yank register was specified, put the deleted text into that
1724 * register. For the black hole register '_' don't yank anything.
1725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (oap->regname != '_')
1727 {
1728 if (oap->regname != 0)
1729 {
1730 /* check for read-only register */
1731 if (!valid_yank_reg(oap->regname, TRUE))
1732 {
1733 beep_flush();
1734 return OK;
1735 }
1736 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1737 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1738 did_yank = TRUE;
1739 }
1740
1741 /*
1742 * Put deleted text into register 1 and shift number registers if the
1743 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001744 * Use the register name from before adjust_clip_reg() may have
1745 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001747 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 || oap->line_count > 1 || oap->use_reg_one)
1749 {
1750 y_current = &y_regs[9];
1751 free_yank_all(); /* free register nine */
1752 for (n = 9; n > 1; --n)
1753 y_regs[n] = y_regs[n - 1];
1754 y_previous = y_current = &y_regs[1];
1755 y_regs[1].y_array = NULL; /* set register one to empty */
1756 if (op_yank(oap, TRUE, FALSE) == OK)
1757 did_yank = TRUE;
1758 }
1759
Bram Moolenaar84298db2012-04-20 13:46:08 +02001760 /* Yank into small delete register when no named register specified
1761 * and the delete is within one line. */
1762 if ((
1763#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001764 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1765 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001766#endif
1767 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && oap->line_count == 1)
1769 {
1770 oap->regname = '-';
1771 get_yank_register(oap->regname, TRUE);
1772 if (op_yank(oap, TRUE, FALSE) == OK)
1773 did_yank = TRUE;
1774 oap->regname = 0;
1775 }
1776
1777 /*
1778 * If there's too much stuff to fit in the yank register, then get a
1779 * confirmation before doing the delete. This is crude, but simple.
1780 * And it avoids doing a delete of something we can't put back if we
1781 * want.
1782 */
1783 if (!did_yank)
1784 {
1785 int msg_silent_save = msg_silent;
1786
1787 msg_silent = 0; /* must display the prompt */
1788 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1789 msg_silent = msg_silent_save;
1790 if (n != 'y')
1791 {
1792 EMSG(_(e_abort));
1793 return FAIL;
1794 }
1795 }
1796 }
1797
Bram Moolenaard04b7502010-07-08 22:27:55 +02001798 /*
1799 * block mode delete
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (oap->block_mode)
1802 {
1803 if (u_save((linenr_T)(oap->start.lnum - 1),
1804 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1805 return FAIL;
1806
1807 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1808 {
1809 block_prep(oap, &bd, lnum, TRUE);
1810 if (bd.textlen == 0) /* nothing to delete */
1811 continue;
1812
1813 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1814 if (lnum == curwin->w_cursor.lnum)
1815 {
1816 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1817# ifdef FEAT_VIRTUALEDIT
1818 curwin->w_cursor.coladd = 0;
1819# endif
1820 }
1821
1822 /* n == number of chars deleted
1823 * If we delete a TAB, it may be replaced by several characters.
1824 * Thus the number of characters may increase!
1825 */
1826 n = bd.textlen - bd.startspaces - bd.endspaces;
1827 oldp = ml_get(lnum);
1828 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1829 if (newp == NULL)
1830 continue;
1831 /* copy up to deleted part */
1832 mch_memmove(newp, oldp, (size_t)bd.textcol);
1833 /* insert spaces */
1834 copy_spaces(newp + bd.textcol,
1835 (size_t)(bd.startspaces + bd.endspaces));
1836 /* copy the part after the deleted part */
1837 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001838 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 /* replace the line */
1840 ml_replace(lnum, newp, FALSE);
1841 }
1842
1843 check_cursor_col();
1844 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1845 oap->end.lnum + 1, 0L);
1846 oap->line_count = 0; /* no lines deleted */
1847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001848 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 if (oap->op_type == OP_CHANGE)
1851 {
1852 /* Delete the lines except the first one. Temporarily move the
1853 * cursor to the next line. Save the current line number, if the
1854 * last line is deleted it may be changed.
1855 */
1856 if (oap->line_count > 1)
1857 {
1858 lnum = curwin->w_cursor.lnum;
1859 ++curwin->w_cursor.lnum;
1860 del_lines((long)(oap->line_count - 1), TRUE);
1861 curwin->w_cursor.lnum = lnum;
1862 }
1863 if (u_save_cursor() == FAIL)
1864 return FAIL;
1865 if (curbuf->b_p_ai) /* don't delete indent */
1866 {
1867 beginline(BL_WHITE); /* cursor on first non-white */
1868 did_ai = TRUE; /* delete the indent when ESC hit */
1869 ai_col = curwin->w_cursor.col;
1870 }
1871 else
1872 beginline(0); /* cursor in column 0 */
1873 truncate_line(FALSE); /* delete the rest of the line */
1874 /* leave cursor past last char in line */
1875 if (oap->line_count > 1)
1876 u_clearline(); /* "U" command not possible after "2cc" */
1877 }
1878 else
1879 {
1880 del_lines(oap->line_count, TRUE);
1881 beginline(BL_WHITE | BL_FIX);
1882 u_clearline(); /* "U" command not possible after "dd" */
1883 }
1884 }
1885 else
1886 {
1887#ifdef FEAT_VIRTUALEDIT
1888 if (virtual_op)
1889 {
1890 int endcol = 0;
1891
1892 /* For virtualedit: break the tabs that are partly included. */
1893 if (gchar_pos(&oap->start) == '\t')
1894 {
1895 if (u_save_cursor() == FAIL) /* save first line for undo */
1896 return FAIL;
1897 if (oap->line_count == 1)
1898 endcol = getviscol2(oap->end.col, oap->end.coladd);
1899 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1900 oap->start = curwin->w_cursor;
1901 if (oap->line_count == 1)
1902 {
1903 coladvance(endcol);
1904 oap->end.col = curwin->w_cursor.col;
1905 oap->end.coladd = curwin->w_cursor.coladd;
1906 curwin->w_cursor = oap->start;
1907 }
1908 }
1909
1910 /* Break a tab only when it's included in the area. */
1911 if (gchar_pos(&oap->end) == '\t'
1912 && (int)oap->end.coladd < oap->inclusive)
1913 {
1914 /* save last line for undo */
1915 if (u_save((linenr_T)(oap->end.lnum - 1),
1916 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1917 return FAIL;
1918 curwin->w_cursor = oap->end;
1919 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1920 oap->end = curwin->w_cursor;
1921 curwin->w_cursor = oap->start;
1922 }
1923 }
1924#endif
1925
1926 if (oap->line_count == 1) /* delete characters within one line */
1927 {
1928 if (u_save_cursor() == FAIL) /* save line for undo */
1929 return FAIL;
1930
1931 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001932 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 && oap->op_type == OP_CHANGE
1934 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001935 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 display_dollar(oap->end.col - !oap->inclusive);
1937
1938 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1939
1940#ifdef FEAT_VIRTUALEDIT
1941 if (virtual_op)
1942 {
1943 /* fix up things for virtualedit-delete:
1944 * break the tabs which are going to get in our way
1945 */
1946 char_u *curline = ml_get_curline();
1947 int len = (int)STRLEN(curline);
1948
1949 if (oap->end.coladd != 0
1950 && (int)oap->end.col >= len - 1
1951 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1952 n++;
1953 /* Delete at least one char (e.g, when on a control char). */
1954 if (n == 0 && oap->start.coladd != oap->end.coladd)
1955 n = 1;
1956
1957 /* When deleted a char in the line, reset coladd. */
1958 if (gchar_cursor() != NUL)
1959 curwin->w_cursor.coladd = 0;
1960 }
1961#endif
Bram Moolenaara554a192011-09-21 17:33:53 +02001962 if (oap->op_type == OP_DELETE
1963 && oap->inclusive
1964 && oap->end.lnum == curbuf->b_ml.ml_line_count
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001965 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1966 {
1967 /* Special case: gH<Del> deletes the last line. */
1968 del_lines(1L, FALSE);
1969 }
1970 else
1971 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001972 (void)del_bytes((long)n, !virtual_op,
1973 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 else /* delete characters between lines */
1977 {
1978 pos_T curpos;
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001979 int delete_last_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 /* save deleted and changed lines for undo */
1982 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1983 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1984 return FAIL;
1985
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001986 delete_last_line = (oap->end.lnum == curbuf->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 truncate_line(TRUE); /* delete from cursor to end of line */
1988
1989 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1990 ++curwin->w_cursor.lnum;
1991 del_lines((long)(oap->line_count - 2), FALSE);
1992
Bram Moolenaar9e98edf2012-03-07 19:30:36 +01001993 if (delete_last_line)
1994 oap->end.lnum = curbuf->b_ml.ml_line_count;
1995
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001996 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaar5f1e3e42012-02-22 17:38:00 +01001997 if (oap->inclusive && delete_last_line
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001998 && n > (int)STRLEN(ml_get(oap->end.lnum)))
1999 {
2000 /* Special case: gH<Del> deletes the last line. */
2001 del_lines(1L, FALSE);
2002 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
Bram Moolenaar66accae2012-01-10 13:44:27 +01002003 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2004 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002005 }
2006 else
2007 {
2008 /* delete from start of line until op_end */
2009 curwin->w_cursor.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002010 (void)del_bytes((long)n, !virtual_op,
2011 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002012 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2013 }
2014 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002015 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
2017 }
2018
2019 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2020
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002021#ifdef FEAT_VIRTUALEDIT
2022setmarks:
2023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (oap->block_mode)
2025 {
2026 curbuf->b_op_end.lnum = oap->end.lnum;
2027 curbuf->b_op_end.col = oap->start.col;
2028 }
2029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 curbuf->b_op_end = oap->start;
2031 curbuf->b_op_start = oap->start;
2032
2033 return OK;
2034}
2035
2036#ifdef FEAT_MBYTE
2037/*
2038 * Adjust end of operating area for ending on a multi-byte character.
2039 * Used for deletion.
2040 */
2041 static void
2042mb_adjust_opend(oap)
2043 oparg_T *oap;
2044{
2045 char_u *p;
2046
2047 if (oap->inclusive)
2048 {
2049 p = ml_get(oap->end.lnum);
2050 oap->end.col += mb_tail_off(p, p + oap->end.col);
2051 }
2052}
2053#endif
2054
2055#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2056/*
2057 * Replace a whole area with one character.
2058 */
2059 int
2060op_replace(oap, c)
2061 oparg_T *oap;
2062 int c;
2063{
2064 int n, numc;
2065#ifdef FEAT_MBYTE
2066 int num_chars;
2067#endif
2068 char_u *newp, *oldp;
2069 size_t oldlen;
2070 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002071 char_u *after_p = NULL;
2072 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2075 return OK; /* nothing to do */
2076
Bram Moolenaard9820532013-11-04 01:41:17 +01002077 if (had_ctrl_v_cr)
2078 c = (c == -1 ? '\r' : '\n');
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef FEAT_MBYTE
2081 if (has_mbyte)
2082 mb_adjust_opend(oap);
2083#endif
2084
2085 if (u_save((linenr_T)(oap->start.lnum - 1),
2086 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2087 return FAIL;
2088
2089 /*
2090 * block mode replace
2091 */
2092 if (oap->block_mode)
2093 {
2094 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2095 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2096 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002097 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2099 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2100 continue; /* nothing to replace */
2101
2102 /* n == number of extra chars required
2103 * If we split a TAB, it may be replaced by several characters.
2104 * Thus the number of characters may increase!
2105 */
2106#ifdef FEAT_VIRTUALEDIT
2107 /* If the range starts in virtual space, count the initial
2108 * coladd offset as part of "startspaces" */
2109 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2110 {
2111 pos_T vpos;
2112
Bram Moolenaara1381de2009-11-03 15:44:21 +00002113 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 getvpos(&vpos, oap->start_vcol);
2115 bd.startspaces += vpos.coladd;
2116 n = bd.startspaces;
2117 }
2118 else
2119#endif
2120 /* allow for pre spaces */
2121 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2122
2123 /* allow for post spp */
2124 n += (bd.endspaces
2125#ifdef FEAT_VIRTUALEDIT
2126 && !bd.is_oneChar
2127#endif
2128 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2129 /* Figure out how many characters to replace. */
2130 numc = oap->end_vcol - oap->start_vcol + 1;
2131 if (bd.is_short && (!virtual_op || bd.is_MAX))
2132 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2133
2134#ifdef FEAT_MBYTE
2135 /* A double-wide character can be replaced only up to half the
2136 * times. */
2137 if ((*mb_char2cells)(c) > 1)
2138 {
2139 if ((numc & 1) && !bd.is_short)
2140 {
2141 ++bd.endspaces;
2142 ++n;
2143 }
2144 numc = numc / 2;
2145 }
2146
2147 /* Compute bytes needed, move character count to num_chars. */
2148 num_chars = numc;
2149 numc *= (*mb_char2len)(c);
2150#endif
2151 /* oldlen includes textlen, so don't double count */
2152 n += numc - bd.textlen;
2153
2154 oldp = ml_get_curline();
2155 oldlen = STRLEN(oldp);
2156 newp = alloc_check((unsigned)oldlen + 1 + n);
2157 if (newp == NULL)
2158 continue;
2159 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2160 /* copy up to deleted part */
2161 mch_memmove(newp, oldp, (size_t)bd.textcol);
2162 oldp += bd.textcol + bd.textlen;
2163 /* insert pre-spaces */
2164 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2165 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002166 /* -1/-2 is used for entering CR literally. */
2167 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002169#ifdef FEAT_MBYTE
2170 if (has_mbyte)
2171 {
2172 n = (int)STRLEN(newp);
2173 while (--num_chars >= 0)
2174 n += (*mb_char2bytes)(c, newp + n);
2175 }
2176 else
2177#endif
2178 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2179 if (!bd.is_short)
2180 {
2181 /* insert post-spaces */
2182 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2183 /* copy the part after the changed part */
2184 STRMOVE(newp + STRLEN(newp), oldp);
2185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002189 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002190 after_p = alloc_check(
2191 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002192 if (after_p != NULL)
2193 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
2195 /* replace the line */
2196 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002197 if (after_p != NULL)
2198 {
2199 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2200 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2201 oap->end.lnum++;
2202 vim_free(after_p);
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 }
2206 else
2207 {
2208 /*
2209 * MCHAR and MLINE motion replace.
2210 */
2211 if (oap->motion_type == MLINE)
2212 {
2213 oap->start.col = 0;
2214 curwin->w_cursor.col = 0;
2215 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2216 if (oap->end.col)
2217 --oap->end.col;
2218 }
2219 else if (!oap->inclusive)
2220 dec(&(oap->end));
2221
2222 while (ltoreq(curwin->w_cursor, oap->end))
2223 {
2224 n = gchar_cursor();
2225 if (n != NUL)
2226 {
2227#ifdef FEAT_MBYTE
2228 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2229 {
2230 /* This is slow, but it handles replacing a single-byte
2231 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002232 if (curwin->w_cursor.lnum == oap->end.lnum)
2233 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 n = State;
2235 State = REPLACE;
2236 ins_char(c);
2237 State = n;
2238 /* Backup to the replaced character. */
2239 dec_cursor();
2240 }
2241 else
2242#endif
2243 {
2244#ifdef FEAT_VIRTUALEDIT
2245 if (n == TAB)
2246 {
2247 int end_vcol = 0;
2248
2249 if (curwin->w_cursor.lnum == oap->end.lnum)
2250 {
2251 /* oap->end has to be recalculated when
2252 * the tab breaks */
2253 end_vcol = getviscol2(oap->end.col,
2254 oap->end.coladd);
2255 }
2256 coladvance_force(getviscol());
2257 if (curwin->w_cursor.lnum == oap->end.lnum)
2258 getvpos(&oap->end, end_vcol);
2259 }
2260#endif
2261 pchar(curwin->w_cursor, c);
2262 }
2263 }
2264#ifdef FEAT_VIRTUALEDIT
2265 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2266 {
2267 int virtcols = oap->end.coladd;
2268
2269 if (curwin->w_cursor.lnum == oap->start.lnum
2270 && oap->start.col == oap->end.col && oap->start.coladd)
2271 virtcols -= oap->start.coladd;
2272
2273 /* oap->end has been trimmed so it's effectively inclusive;
2274 * as a result an extra +1 must be counted so we don't
2275 * trample the NUL byte. */
2276 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2277 curwin->w_cursor.col -= (virtcols + 1);
2278 for (; virtcols >= 0; virtcols--)
2279 {
2280 pchar(curwin->w_cursor, c);
2281 if (inc(&curwin->w_cursor) == -1)
2282 break;
2283 }
2284 }
2285#endif
2286
2287 /* Advance to next character, stop at the end of the file. */
2288 if (inc_cursor() == -1)
2289 break;
2290 }
2291 }
2292
2293 curwin->w_cursor = oap->start;
2294 check_cursor();
2295 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2296
2297 /* Set "'[" and "']" marks. */
2298 curbuf->b_op_start = oap->start;
2299 curbuf->b_op_end = oap->end;
2300
2301 return OK;
2302}
2303#endif
2304
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002305static int swapchars __ARGS((int op_type, pos_T *pos, int length));
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2309 */
2310 void
2311op_tilde(oap)
2312 oparg_T *oap;
2313{
2314 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002316 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 if (u_save((linenr_T)(oap->start.lnum - 1),
2319 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2320 return;
2321
2322 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (oap->block_mode) /* Visual block mode */
2324 {
2325 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2326 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002327 int one_change;
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 block_prep(oap, &bd, pos.lnum, FALSE);
2330 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002331 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2332 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002333
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002334#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002335 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2338
Bram Moolenaar009b2592004-10-24 19:18:58 +00002339 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2340 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002342 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346 if (did_change)
2347 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2348 }
2349 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 if (oap->motion_type == MLINE)
2352 {
2353 oap->start.col = 0;
2354 pos.col = 0;
2355 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2356 if (oap->end.col)
2357 --oap->end.col;
2358 }
2359 else if (!oap->inclusive)
2360 dec(&(oap->end));
2361
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002362 if (pos.lnum == oap->end.lnum)
2363 did_change = swapchars(oap->op_type, &pos,
2364 oap->end.col - pos.col + 1);
2365 else
2366 for (;;)
2367 {
2368 did_change |= swapchars(oap->op_type, &pos,
2369 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2370 (int)STRLEN(ml_get_pos(&pos)));
2371 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2372 break;
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (did_change)
2375 {
2376 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2377 0L);
2378#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002379 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
2381 char_u *ptr;
2382 int count;
2383
2384 pos = oap->start;
2385 while (pos.lnum < oap->end.lnum)
2386 {
2387 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002388 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002389 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002391 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 pos.col = 0;
2393 pos.lnum++;
2394 }
2395 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2396 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002397 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002399 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401#endif
2402 }
2403 }
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (!did_change && oap->is_VIsual)
2406 /* No change: need to remove the Visual selection */
2407 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /*
2410 * Set '[ and '] marks.
2411 */
2412 curbuf->b_op_start = oap->start;
2413 curbuf->b_op_end = oap->end;
2414
2415 if (oap->line_count > p_report)
2416 {
2417 if (oap->line_count == 1)
2418 MSG(_("1 line changed"));
2419 else
2420 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2421 }
2422}
2423
2424/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002425 * Invoke swapchar() on "length" bytes at position "pos".
2426 * "pos" is advanced to just after the changed characters.
2427 * "length" is rounded up to include the whole last multi-byte character.
2428 * Also works correctly when the number of bytes changes.
2429 * Returns TRUE if some character was changed.
2430 */
2431 static int
2432swapchars(op_type, pos, length)
2433 int op_type;
2434 pos_T *pos;
2435 int length;
2436{
2437 int todo;
2438 int did_change = 0;
2439
2440 for (todo = length; todo > 0; --todo)
2441 {
2442# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002443 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002444 {
2445 int len = (*mb_ptr2len)(ml_get_pos(pos));
2446
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002447 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002448 if (len > 0)
2449 todo -= len - 1;
2450 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002451# endif
2452 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002453 if (inc(pos) == -1) /* at end of file */
2454 break;
2455 }
2456 return did_change;
2457}
2458
2459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 * If op_type == OP_UPPER: make uppercase,
2461 * if op_type == OP_LOWER: make lowercase,
2462 * if op_type == OP_ROT13: do rot13 encoding,
2463 * else swap case of character at 'pos'
2464 * returns TRUE when something actually changed.
2465 */
2466 int
2467swapchar(op_type, pos)
2468 int op_type;
2469 pos_T *pos;
2470{
2471 int c;
2472 int nc;
2473
2474 c = gchar_pos(pos);
2475
2476 /* Only do rot13 encoding for ASCII characters. */
2477 if (c >= 0x80 && op_type == OP_ROT13)
2478 return FALSE;
2479
2480#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002481 if (op_type == OP_UPPER && c == 0xdf
2482 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002483 {
2484 pos_T sp = curwin->w_cursor;
2485
2486 /* Special handling of German sharp s: change to "SS". */
2487 curwin->w_cursor = *pos;
2488 del_char(FALSE);
2489 ins_char('S');
2490 ins_char('S');
2491 curwin->w_cursor = sp;
2492 inc(pos);
2493 }
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2496 return FALSE;
2497#endif
2498 nc = c;
2499 if (MB_ISLOWER(c))
2500 {
2501 if (op_type == OP_ROT13)
2502 nc = ROT13(c, 'a');
2503 else if (op_type != OP_LOWER)
2504 nc = MB_TOUPPER(c);
2505 }
2506 else if (MB_ISUPPER(c))
2507 {
2508 if (op_type == OP_ROT13)
2509 nc = ROT13(c, 'A');
2510 else if (op_type != OP_UPPER)
2511 nc = MB_TOLOWER(c);
2512 }
2513 if (nc != c)
2514 {
2515#ifdef FEAT_MBYTE
2516 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2517 {
2518 pos_T sp = curwin->w_cursor;
2519
2520 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002521 /* don't use del_char(), it also removes composing chars */
2522 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 ins_char(nc);
2524 curwin->w_cursor = sp;
2525 }
2526 else
2527#endif
2528 pchar(*pos, nc);
2529 return TRUE;
2530 }
2531 return FALSE;
2532}
2533
2534#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2535/*
2536 * op_insert - Insert and append operators for Visual mode.
2537 */
2538 void
2539op_insert(oap, count1)
2540 oparg_T *oap;
2541 long count1;
2542{
2543 long ins_len, pre_textlen = 0;
2544 char_u *firstline, *ins_text;
2545 struct block_def bd;
2546 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002547 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /* edit() changes this - record it for OP_APPEND */
2550 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2551
2552 /* vis block is still marked. Get rid of it now. */
2553 curwin->w_cursor.lnum = oap->start.lnum;
2554 update_screen(INVERTED);
2555
2556 if (oap->block_mode)
2557 {
2558#ifdef FEAT_VIRTUALEDIT
2559 /* When 'virtualedit' is used, need to insert the extra spaces before
2560 * doing block_prep(). When only "block" is used, virtual edit is
2561 * already disabled, but still need it when calling
2562 * coladvance_force(). */
2563 if (curwin->w_cursor.coladd > 0)
2564 {
2565 int old_ve_flags = ve_flags;
2566
2567 ve_flags = VE_ALL;
2568 if (u_save_cursor() == FAIL)
2569 return;
2570 coladvance_force(oap->op_type == OP_APPEND
2571 ? oap->end_vcol + 1 : getviscol());
2572 if (oap->op_type == OP_APPEND)
2573 --curwin->w_cursor.col;
2574 ve_flags = old_ve_flags;
2575 }
2576#endif
2577 /* Get the info about the block before entering the text */
2578 block_prep(oap, &bd, oap->start.lnum, TRUE);
2579 firstline = ml_get(oap->start.lnum) + bd.textcol;
2580 if (oap->op_type == OP_APPEND)
2581 firstline += bd.textlen;
2582 pre_textlen = (long)STRLEN(firstline);
2583 }
2584
2585 if (oap->op_type == OP_APPEND)
2586 {
2587 if (oap->block_mode
2588#ifdef FEAT_VIRTUALEDIT
2589 && curwin->w_cursor.coladd == 0
2590#endif
2591 )
2592 {
2593 /* Move the cursor to the character right of the block. */
2594 curwin->w_set_curswant = TRUE;
2595 while (*ml_get_cursor() != NUL
2596 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2597 ++curwin->w_cursor.col;
2598 if (bd.is_short && !bd.is_MAX)
2599 {
2600 /* First line was too short, make it longer and adjust the
2601 * values in "bd". */
2602 if (u_save_cursor() == FAIL)
2603 return;
2604 for (i = 0; i < bd.endspaces; ++i)
2605 ins_char(' ');
2606 bd.textlen += bd.endspaces;
2607 }
2608 }
2609 else
2610 {
2611 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002612 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /* Works just like an 'i'nsert on the next character. */
2615 if (!lineempty(curwin->w_cursor.lnum)
2616 && oap->start_vcol != oap->end_vcol)
2617 inc_cursor();
2618 }
2619 }
2620
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002621 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 edit(NUL, FALSE, (linenr_T)count1);
2623
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002624 /* When a tab was inserted, and the characters in front of the tab
2625 * have been converted to a tab as well, the column of the cursor
2626 * might have actually been reduced, so need to adjust here. */
2627 if (t1.lnum == curbuf->b_op_start_orig.lnum
2628 && lt(curbuf->b_op_start_orig, t1))
2629 oap->start = curbuf->b_op_start_orig;
2630
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002631 /* If user has moved off this line, we don't know what to do, so do
2632 * nothing.
2633 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2634 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return;
2636
2637 if (oap->block_mode)
2638 {
2639 struct block_def bd2;
2640
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002641 /* The user may have moved the cursor before inserting something, try
2642 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002643 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002644 {
2645 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002646 && oap->start.col
2647#ifdef FEAT_VIRTUALEDIT
2648 + oap->start.coladd
2649#endif
2650 != curbuf->b_op_start_orig.col
2651#ifdef FEAT_VIRTUALEDIT
2652 + curbuf->b_op_start_orig.coladd
2653#endif
2654 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002655 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002656 int t = getviscol2(curbuf->b_op_start_orig.col,
2657 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002658 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002659 pre_textlen -= t - oap->start_vcol;
2660 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002661 }
2662 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002663 && oap->end.col
2664#ifdef FEAT_VIRTUALEDIT
2665 + oap->end.coladd
2666#endif
2667 >= curbuf->b_op_start_orig.col
2668#ifdef FEAT_VIRTUALEDIT
2669 + curbuf->b_op_start_orig.coladd
2670#endif
2671 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002672 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002673 int t = getviscol2(curbuf->b_op_start_orig.col,
2674 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002675 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002676 /* reset pre_textlen to the value of OP_INSERT */
2677 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002678 pre_textlen -= t - oap->start_vcol;
2679 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002680 oap->op_type = OP_INSERT;
2681 }
2682 }
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 /*
2685 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002686 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 * Don't do this when "$" used, end-of-line will have changed.
2688 */
2689 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2690 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2691 {
2692 if (oap->op_type == OP_APPEND)
2693 {
2694 pre_textlen += bd2.textlen - bd.textlen;
2695 if (bd2.endspaces)
2696 --bd2.textlen;
2697 }
2698 bd.textcol = bd2.textcol;
2699 bd.textlen = bd2.textlen;
2700 }
2701
2702 /*
2703 * Subsequent calls to ml_get() flush the firstline data - take a
2704 * copy of the required string.
2705 */
2706 firstline = ml_get(oap->start.lnum) + bd.textcol;
2707 if (oap->op_type == OP_APPEND)
2708 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002709 if (pre_textlen >= 0
2710 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
2712 ins_text = vim_strnsave(firstline, (int)ins_len);
2713 if (ins_text != NULL)
2714 {
2715 /* block handled here */
2716 if (u_save(oap->start.lnum,
2717 (linenr_T)(oap->end.lnum + 1)) == OK)
2718 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2719 &bd);
2720
2721 curwin->w_cursor.col = oap->start.col;
2722 check_cursor();
2723 vim_free(ins_text);
2724 }
2725 }
2726 }
2727}
2728#endif
2729
2730/*
2731 * op_change - handle a change operation
2732 *
2733 * return TRUE if edit() returns because of a CTRL-O command
2734 */
2735 int
2736op_change(oap)
2737 oparg_T *oap;
2738{
2739 colnr_T l;
2740 int retval;
2741#ifdef FEAT_VISUALEXTRA
2742 long offset;
2743 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002744 long ins_len;
2745 long pre_textlen = 0;
2746 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 char_u *firstline;
2748 char_u *ins_text, *newp, *oldp;
2749 struct block_def bd;
2750#endif
2751
2752 l = oap->start.col;
2753 if (oap->motion_type == MLINE)
2754 {
2755 l = 0;
2756#ifdef FEAT_SMARTINDENT
2757 if (!p_paste && curbuf->b_p_si
2758# ifdef FEAT_CINDENT
2759 && !curbuf->b_p_cin
2760# endif
2761 )
2762 can_si = TRUE; /* It's like opening a new line, do si */
2763#endif
2764 }
2765
2766 /* First delete the text in the region. In an empty buffer only need to
2767 * save for undo */
2768 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2769 {
2770 if (u_save_cursor() == FAIL)
2771 return FALSE;
2772 }
2773 else if (op_delete(oap) == FAIL)
2774 return FALSE;
2775
2776 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2777 && !virtual_op)
2778 inc_cursor();
2779
2780#ifdef FEAT_VISUALEXTRA
2781 /* check for still on same line (<CR> in inserted text meaningless) */
2782 /* skip blank lines too */
2783 if (oap->block_mode)
2784 {
2785# ifdef FEAT_VIRTUALEDIT
2786 /* Add spaces before getting the current line length. */
2787 if (virtual_op && (curwin->w_cursor.coladd > 0
2788 || gchar_cursor() == NUL))
2789 coladvance_force(getviscol());
2790# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002791 firstline = ml_get(oap->start.lnum);
2792 pre_textlen = (long)STRLEN(firstline);
2793 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 bd.textcol = curwin->w_cursor.col;
2795 }
2796#endif
2797
2798#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2799 if (oap->motion_type == MLINE)
2800 fix_indent();
2801#endif
2802
2803 retval = edit(NUL, FALSE, (linenr_T)1);
2804
2805#ifdef FEAT_VISUALEXTRA
2806 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002807 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002809 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002811 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002813 /* Auto-indenting may have changed the indent. If the cursor was past
2814 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002816 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002818 long new_indent = (long)(skipwhite(firstline) - firstline);
2819
2820 pre_textlen += new_indent - pre_indent;
2821 bd.textcol += new_indent - pre_indent;
2822 }
2823
2824 ins_len = (long)STRLEN(firstline) - pre_textlen;
2825 if (ins_len > 0)
2826 {
2827 /* Subsequent calls to ml_get() flush the firstline data - take a
2828 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2830 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002831 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2833 linenr++)
2834 {
2835 block_prep(oap, &bd, linenr, TRUE);
2836 if (!bd.is_short || virtual_op)
2837 {
2838# ifdef FEAT_VIRTUALEDIT
2839 pos_T vpos;
2840
2841 /* If the block starts in virtual space, count the
2842 * initial coladd offset as part of "startspaces" */
2843 if (bd.is_short)
2844 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002845 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 else
2849 vpos.coladd = 0;
2850# endif
2851 oldp = ml_get(linenr);
2852 newp = alloc_check((unsigned)(STRLEN(oldp)
2853# ifdef FEAT_VIRTUALEDIT
2854 + vpos.coladd
2855# endif
2856 + ins_len + 1));
2857 if (newp == NULL)
2858 continue;
2859 /* copy up to block start */
2860 mch_memmove(newp, oldp, (size_t)bd.textcol);
2861 offset = bd.textcol;
2862# ifdef FEAT_VIRTUALEDIT
2863 copy_spaces(newp + offset, (size_t)vpos.coladd);
2864 offset += vpos.coladd;
2865# endif
2866 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2867 offset += ins_len;
2868 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002869 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 ml_replace(linenr, newp, FALSE);
2871 }
2872 }
2873 check_cursor();
2874
2875 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2876 }
2877 vim_free(ins_text);
2878 }
2879 }
2880#endif
2881
2882 return retval;
2883}
2884
2885/*
2886 * set all the yank registers to empty (called from main())
2887 */
2888 void
2889init_yank()
2890{
2891 int i;
2892
2893 for (i = 0; i < NUM_REGISTERS; ++i)
2894 y_regs[i].y_array = NULL;
2895}
2896
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002897#if defined(EXITFREE) || defined(PROTO)
2898 void
2899clear_registers()
2900{
2901 int i;
2902
2903 for (i = 0; i < NUM_REGISTERS; ++i)
2904 {
2905 y_current = &y_regs[i];
2906 if (y_current->y_array != NULL)
2907 free_yank_all();
2908 }
2909}
2910#endif
2911
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912/*
2913 * Free "n" lines from the current yank register.
2914 * Called for normal freeing and in case of error.
2915 */
2916 static void
2917free_yank(n)
2918 long n;
2919{
2920 if (y_current->y_array != NULL)
2921 {
2922 long i;
2923
2924 for (i = n; --i >= 0; )
2925 {
2926#ifdef AMIGA /* only for very slow machines */
2927 if ((i & 1023) == 1023) /* this may take a while */
2928 {
2929 /*
2930 * This message should never cause a hit-return message.
2931 * Overwrite this message with any next message.
2932 */
2933 ++no_wait_return;
2934 smsg((char_u *)_("freeing %ld lines"), i + 1);
2935 --no_wait_return;
2936 msg_didout = FALSE;
2937 msg_col = 0;
2938 }
2939#endif
2940 vim_free(y_current->y_array[i]);
2941 }
2942 vim_free(y_current->y_array);
2943 y_current->y_array = NULL;
2944#ifdef AMIGA
2945 if (n >= 1000)
2946 MSG("");
2947#endif
2948 }
2949}
2950
2951 static void
2952free_yank_all()
2953{
2954 free_yank(y_current->y_size);
2955}
2956
2957/*
2958 * Yank the text between "oap->start" and "oap->end" into a yank register.
2959 * If we are to append (uppercase register), we first yank into a new yank
2960 * register and then concatenate the old and the new one (so we keep the old
2961 * one in case of out-of-memory).
2962 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002963 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 */
2965 int
2966op_yank(oap, deleting, mess)
2967 oparg_T *oap;
2968 int deleting;
2969 int mess;
2970{
2971 long y_idx; /* index in y_array[] */
2972 struct yankreg *curr; /* copy of y_current */
2973 struct yankreg newreg; /* new yank register when appending */
2974 char_u **new_ptr;
2975 linenr_T lnum; /* current line number */
2976 long j;
2977 int yanktype = oap->motion_type;
2978 long yanklines = oap->line_count;
2979 linenr_T yankendlnum = oap->end.lnum;
2980 char_u *p;
2981 char_u *pnew;
2982 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002983#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002984 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /* check for read-only register */
2988 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2989 {
2990 beep_flush();
2991 return FAIL;
2992 }
2993 if (oap->regname == '_') /* black hole: nothing to do */
2994 return OK;
2995
2996#ifdef FEAT_CLIPBOARD
2997 if (!clip_star.available && oap->regname == '*')
2998 oap->regname = 0;
2999 else if (!clip_plus.available && oap->regname == '+')
3000 oap->regname = 0;
3001#endif
3002
3003 if (!deleting) /* op_delete() already set y_current */
3004 get_yank_register(oap->regname, TRUE);
3005
3006 curr = y_current;
3007 /* append to existing contents */
3008 if (y_append && y_current->y_array != NULL)
3009 y_current = &newreg;
3010 else
3011 free_yank_all(); /* free previously yanked lines */
3012
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003013 /*
3014 * If the cursor was in column 1 before and after the movement, and the
3015 * operator is not inclusive, the yank is always linewise.
3016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if ( oap->motion_type == MCHAR
3018 && oap->start.col == 0
3019 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003021 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 && oap->end.col == 0
3023 && yanklines > 1)
3024 {
3025 yanktype = MLINE;
3026 --yankendlnum;
3027 --yanklines;
3028 }
3029
3030 y_current->y_size = yanklines;
3031 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3034 yanklines), TRUE);
3035
3036 if (y_current->y_array == NULL)
3037 {
3038 y_current = curr;
3039 return FAIL;
3040 }
3041
3042 y_idx = 0;
3043 lnum = oap->start.lnum;
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (oap->block_mode)
3046 {
3047 /* Visual block mode */
3048 y_current->y_type = MBLOCK; /* set the yank register type */
3049 y_current->y_width = oap->end_vcol - oap->start_vcol;
3050
3051 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3052 y_current->y_width--;
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3056 {
3057 switch (y_current->y_type)
3058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 case MBLOCK:
3060 block_prep(oap, &bd, lnum, FALSE);
3061 if (yank_copy_line(&bd, y_idx) == FAIL)
3062 goto fail;
3063 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 case MLINE:
3066 if ((y_current->y_array[y_idx] =
3067 vim_strsave(ml_get(lnum))) == NULL)
3068 goto fail;
3069 break;
3070
3071 case MCHAR:
3072 {
3073 colnr_T startcol = 0, endcol = MAXCOL;
3074#ifdef FEAT_VIRTUALEDIT
3075 int is_oneChar = FALSE;
3076 colnr_T cs, ce;
3077#endif
3078 p = ml_get(lnum);
3079 bd.startspaces = 0;
3080 bd.endspaces = 0;
3081
3082 if (lnum == oap->start.lnum)
3083 {
3084 startcol = oap->start.col;
3085#ifdef FEAT_VIRTUALEDIT
3086 if (virtual_op)
3087 {
3088 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3089 if (ce != cs && oap->start.coladd > 0)
3090 {
3091 /* Part of a tab selected -- but don't
3092 * double-count it. */
3093 bd.startspaces = (ce - cs + 1)
3094 - oap->start.coladd;
3095 startcol++;
3096 }
3097 }
3098#endif
3099 }
3100
3101 if (lnum == oap->end.lnum)
3102 {
3103 endcol = oap->end.col;
3104#ifdef FEAT_VIRTUALEDIT
3105 if (virtual_op)
3106 {
3107 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3108 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3109# ifdef FEAT_MBYTE
3110 /* Don't add space for double-wide
3111 * char; endcol will be on last byte
3112 * of multi-byte char. */
3113 && (*mb_head_off)(p, p + endcol) == 0
3114# endif
3115 ))
3116 {
3117 if (oap->start.lnum == oap->end.lnum
3118 && oap->start.col == oap->end.col)
3119 {
3120 /* Special case: inside a single char */
3121 is_oneChar = TRUE;
3122 bd.startspaces = oap->end.coladd
3123 - oap->start.coladd + oap->inclusive;
3124 endcol = startcol;
3125 }
3126 else
3127 {
3128 bd.endspaces = oap->end.coladd
3129 + oap->inclusive;
3130 endcol -= oap->inclusive;
3131 }
3132 }
3133 }
3134#endif
3135 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003136 if (endcol == MAXCOL)
3137 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (startcol > endcol
3139#ifdef FEAT_VIRTUALEDIT
3140 || is_oneChar
3141#endif
3142 )
3143 bd.textlen = 0;
3144 else
3145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 bd.textlen = endcol - startcol + oap->inclusive;
3147 }
3148 bd.textstart = p + startcol;
3149 if (yank_copy_line(&bd, y_idx) == FAIL)
3150 goto fail;
3151 break;
3152 }
3153 /* NOTREACHED */
3154 }
3155 }
3156
3157 if (curr != y_current) /* append the new block to the old block */
3158 {
3159 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3160 (curr->y_size + y_current->y_size)), TRUE);
3161 if (new_ptr == NULL)
3162 goto fail;
3163 for (j = 0; j < curr->y_size; ++j)
3164 new_ptr[j] = curr->y_array[j];
3165 vim_free(curr->y_array);
3166 curr->y_array = new_ptr;
3167
3168 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3169 curr->y_type = MLINE;
3170
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003171 /* Concatenate the last line of the old block with the first line of
3172 * the new block, unless being Vi compatible. */
3173 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
3175 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3176 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3177 if (pnew == NULL)
3178 {
3179 y_idx = y_current->y_size - 1;
3180 goto fail;
3181 }
3182 STRCPY(pnew, curr->y_array[--j]);
3183 STRCAT(pnew, y_current->y_array[0]);
3184 vim_free(curr->y_array[j]);
3185 vim_free(y_current->y_array[0]);
3186 curr->y_array[j++] = pnew;
3187 y_idx = 1;
3188 }
3189 else
3190 y_idx = 0;
3191 while (y_idx < y_current->y_size)
3192 curr->y_array[j++] = y_current->y_array[y_idx++];
3193 curr->y_size = j;
3194 vim_free(y_current->y_array);
3195 y_current = curr;
3196 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003197 if (curwin->w_p_rnu)
3198 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (mess) /* Display message about yank? */
3200 {
3201 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 && yanklines == 1)
3204 yanklines = 0;
3205 /* Some versions of Vi use ">=" here, some don't... */
3206 if (yanklines > p_report)
3207 {
3208 /* redisplay now, so message is not deleted */
3209 update_topline_redraw();
3210 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003211 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003212 if (oap->block_mode)
3213 MSG(_("block of 1 line yanked"));
3214 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003215 MSG(_("1 line yanked"));
3216 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003217 else if (oap->block_mode)
3218 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
3220 smsg((char_u *)_("%ld lines yanked"), yanklines);
3221 }
3222 }
3223
3224 /*
3225 * Set "'[" and "']" marks.
3226 */
3227 curbuf->b_op_start = oap->start;
3228 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003229 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003230 {
3231 curbuf->b_op_start.col = 0;
3232 curbuf->b_op_end.col = MAXCOL;
3233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
3235#ifdef FEAT_CLIPBOARD
3236 /*
3237 * If we were yanking to the '*' register, send result to clipboard.
3238 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3239 * to the '*' register.
3240 */
3241 if (clip_star.available
3242 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003243 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003244 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 if (curr != &(y_regs[STAR_REGISTER]))
3247 /* Copy the text from register 0 to the clipboard register. */
3248 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3249
3250 clip_own_selection(&clip_star);
3251 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003252# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003253 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003254# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
3256
3257# ifdef FEAT_X11
3258 /*
3259 * If we were yanking to the '+' register, send result to selection.
3260 * Also copy to the '*' register, in case auto-select is off.
3261 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003262 if (clip_plus.available
3263 && (curr == &(y_regs[PLUS_REGISTER])
3264 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003265 && ((clip_unnamed | clip_unnamed_saved) &
3266 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003268 if (curr != &(y_regs[PLUS_REGISTER]))
3269 /* Copy the text from register 0 to the clipboard register. */
3270 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 clip_own_selection(&clip_plus);
3273 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003274 if (!clip_isautosel_star() && !did_star
3275 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
3277 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3278 clip_own_selection(&clip_star);
3279 clip_gen_set_selection(&clip_star);
3280 }
3281 }
3282# endif
3283#endif
3284
3285 return OK;
3286
3287fail: /* free the allocated lines */
3288 free_yank(y_idx + 1);
3289 y_current = curr;
3290 return FAIL;
3291}
3292
3293 static int
3294yank_copy_line(bd, y_idx)
3295 struct block_def *bd;
3296 long y_idx;
3297{
3298 char_u *pnew;
3299
3300 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3301 == NULL)
3302 return FAIL;
3303 y_current->y_array[y_idx] = pnew;
3304 copy_spaces(pnew, (size_t)bd->startspaces);
3305 pnew += bd->startspaces;
3306 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3307 pnew += bd->textlen;
3308 copy_spaces(pnew, (size_t)bd->endspaces);
3309 pnew += bd->endspaces;
3310 *pnew = NUL;
3311 return OK;
3312}
3313
3314#ifdef FEAT_CLIPBOARD
3315/*
3316 * Make a copy of the y_current register to register "reg".
3317 */
3318 static void
3319copy_yank_reg(reg)
3320 struct yankreg *reg;
3321{
3322 struct yankreg *curr = y_current;
3323 long j;
3324
3325 y_current = reg;
3326 free_yank_all();
3327 *y_current = *curr;
3328 y_current->y_array = (char_u **)lalloc_clear(
3329 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3330 if (y_current->y_array == NULL)
3331 y_current->y_size = 0;
3332 else
3333 for (j = 0; j < y_current->y_size; ++j)
3334 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3335 {
3336 free_yank(j);
3337 y_current->y_size = 0;
3338 break;
3339 }
3340 y_current = curr;
3341}
3342#endif
3343
3344/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003345 * Put contents of register "regname" into the text.
3346 * Caller must check "regname" to be valid!
3347 * "flags": PUT_FIXINDENT make indent look nice
3348 * PUT_CURSEND leave cursor after end of new text
3349 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
3351 void
3352do_put(regname, dir, count, flags)
3353 int regname;
3354 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3355 long count;
3356 int flags;
3357{
3358 char_u *ptr;
3359 char_u *newp, *oldp;
3360 int yanklen;
3361 int totlen = 0; /* init for gcc */
3362 linenr_T lnum;
3363 colnr_T col;
3364 long i; /* index in y_array[] */
3365 int y_type;
3366 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 int oldlen;
3368 long y_width = 0;
3369 colnr_T vcol;
3370 int delcount;
3371 int incr = 0;
3372 long j;
3373 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 char_u **y_array = NULL;
3375 long nr_lines = 0;
3376 pos_T new_cursor;
3377 int indent;
3378 int orig_indent = 0; /* init for gcc */
3379 int indent_diff = 0; /* init for gcc */
3380 int first_indent = TRUE;
3381 int lendiff = 0;
3382 pos_T old_pos;
3383 char_u *insert_string = NULL;
3384 int allocated = FALSE;
3385 long cnt;
3386
3387#ifdef FEAT_CLIPBOARD
3388 /* Adjust register name for "unnamed" in 'clipboard'. */
3389 adjust_clip_reg(&regname);
3390 (void)may_get_selection(regname);
3391#endif
3392
3393 if (flags & PUT_FIXINDENT)
3394 orig_indent = get_indent();
3395
3396 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3397 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3398
3399 /*
3400 * Using inserted text works differently, because the register includes
3401 * special characters (newlines, etc.).
3402 */
3403 if (regname == '.')
3404 {
3405 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3406 (count == -1 ? 'O' : 'i')), count, FALSE);
3407 /* Putting the text is done later, so can't really move the cursor to
3408 * the next character. Use "l" to simulate it. */
3409 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3410 stuffcharReadbuff('l');
3411 return;
3412 }
3413
3414 /*
3415 * For special registers '%' (file name), '#' (alternate file name) and
3416 * ':' (last command line), etc. we have to create a fake yank register.
3417 */
3418 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3419 {
3420 if (insert_string == NULL)
3421 return;
3422 }
3423
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003424#ifdef FEAT_AUTOCMD
3425 /* Autocommands may be executed when saving lines for undo, which may make
3426 * y_array invalid. Start undo now to avoid that. */
3427 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3428#endif
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (insert_string != NULL)
3431 {
3432 y_type = MCHAR;
3433#ifdef FEAT_EVAL
3434 if (regname == '=')
3435 {
3436 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003437 * characters.
3438 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 for (;;)
3440 {
3441 y_size = 0;
3442 ptr = insert_string;
3443 while (ptr != NULL)
3444 {
3445 if (y_array != NULL)
3446 y_array[y_size] = ptr;
3447 ++y_size;
3448 ptr = vim_strchr(ptr, '\n');
3449 if (ptr != NULL)
3450 {
3451 if (y_array != NULL)
3452 *ptr = NUL;
3453 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003454 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (*ptr == NUL)
3456 {
3457 y_type = MLINE;
3458 break;
3459 }
3460 }
3461 }
3462 if (y_array != NULL)
3463 break;
3464 y_array = (char_u **)alloc((unsigned)
3465 (y_size * sizeof(char_u *)));
3466 if (y_array == NULL)
3467 goto end;
3468 }
3469 }
3470 else
3471#endif
3472 {
3473 y_size = 1; /* use fake one-line yank register */
3474 y_array = &insert_string;
3475 }
3476 }
3477 else
3478 {
3479 get_yank_register(regname, FALSE);
3480
3481 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 y_size = y_current->y_size;
3484 y_array = y_current->y_array;
3485 }
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (y_type == MLINE)
3488 {
3489 if (flags & PUT_LINE_SPLIT)
3490 {
3491 /* "p" or "P" in Visual mode: split the lines to put the text in
3492 * between. */
3493 if (u_save_cursor() == FAIL)
3494 goto end;
3495 ptr = vim_strsave(ml_get_cursor());
3496 if (ptr == NULL)
3497 goto end;
3498 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3499 vim_free(ptr);
3500
3501 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3502 if (ptr == NULL)
3503 goto end;
3504 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3505 ++nr_lines;
3506 dir = FORWARD;
3507 }
3508 if (flags & PUT_LINE_FORWARD)
3509 {
3510 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003511 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 dir = FORWARD;
3513 }
3514 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3515 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3519 y_type = MLINE;
3520
3521 if (y_size == 0 || y_array == NULL)
3522 {
3523 EMSG2(_("E353: Nothing in register %s"),
3524 regname == 0 ? (char_u *)"\"" : transchar(regname));
3525 goto end;
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (y_type == MBLOCK)
3529 {
3530 lnum = curwin->w_cursor.lnum + y_size + 1;
3531 if (lnum > curbuf->b_ml.ml_line_count)
3532 lnum = curbuf->b_ml.ml_line_count + 1;
3533 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3534 goto end;
3535 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003536 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 lnum = curwin->w_cursor.lnum;
3539#ifdef FEAT_FOLDING
3540 /* Correct line number for closed fold. Don't move the cursor yet,
3541 * u_save() uses it. */
3542 if (dir == BACKWARD)
3543 (void)hasFolding(lnum, &lnum, NULL);
3544 else
3545 (void)hasFolding(lnum, NULL, &lnum);
3546#endif
3547 if (dir == FORWARD)
3548 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003549 /* In an empty buffer the empty line is going to be replaced, include
3550 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003551 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 goto end;
3553#ifdef FEAT_FOLDING
3554 if (dir == FORWARD)
3555 curwin->w_cursor.lnum = lnum - 1;
3556 else
3557 curwin->w_cursor.lnum = lnum;
3558 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3559#endif
3560 }
3561 else if (u_save_cursor() == FAIL)
3562 goto end;
3563
3564 yanklen = (int)STRLEN(y_array[0]);
3565
3566#ifdef FEAT_VIRTUALEDIT
3567 if (ve_flags == VE_ALL && y_type == MCHAR)
3568 {
3569 if (gchar_cursor() == TAB)
3570 {
3571 /* Don't need to insert spaces when "p" on the last position of a
3572 * tab or "P" on the first position. */
3573 if (dir == FORWARD
3574 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3575 : curwin->w_cursor.coladd > 0)
3576 coladvance_force(getviscol());
3577 else
3578 curwin->w_cursor.coladd = 0;
3579 }
3580 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3581 coladvance_force(getviscol() + (dir == FORWARD));
3582 }
3583#endif
3584
3585 lnum = curwin->w_cursor.lnum;
3586 col = curwin->w_cursor.col;
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 /*
3589 * Block mode
3590 */
3591 if (y_type == MBLOCK)
3592 {
3593 char c = gchar_cursor();
3594 colnr_T endcol2 = 0;
3595
3596 if (dir == FORWARD && c != NUL)
3597 {
3598#ifdef FEAT_VIRTUALEDIT
3599 if (ve_flags == VE_ALL)
3600 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3601 else
3602#endif
3603 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3604
3605#ifdef FEAT_MBYTE
3606 if (has_mbyte)
3607 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003608 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 else
3610#endif
3611#ifdef FEAT_VIRTUALEDIT
3612 if (c != TAB || ve_flags != VE_ALL)
3613#endif
3614 ++curwin->w_cursor.col;
3615 ++col;
3616 }
3617 else
3618 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3619
3620#ifdef FEAT_VIRTUALEDIT
3621 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003622 if (ve_flags == VE_ALL
3623 && (curwin->w_cursor.coladd > 0
3624 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 if (dir == FORWARD && c == NUL)
3627 ++col;
3628 if (dir != FORWARD && c != NUL)
3629 ++curwin->w_cursor.col;
3630 if (c == TAB)
3631 {
3632 if (dir == BACKWARD && curwin->w_cursor.col)
3633 curwin->w_cursor.col--;
3634 if (dir == FORWARD && col - 1 == endcol2)
3635 curwin->w_cursor.col++;
3636 }
3637 }
3638 curwin->w_cursor.coladd = 0;
3639#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003640 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 for (i = 0; i < y_size; ++i)
3642 {
3643 int spaces;
3644 char shortline;
3645
3646 bd.startspaces = 0;
3647 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 vcol = 0;
3649 delcount = 0;
3650
3651 /* add a new line */
3652 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3653 {
3654 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3655 (colnr_T)1, FALSE) == FAIL)
3656 break;
3657 ++nr_lines;
3658 }
3659 /* get the old line and advance to the position to insert at */
3660 oldp = ml_get_curline();
3661 oldlen = (int)STRLEN(oldp);
3662 for (ptr = oldp; vcol < col && *ptr; )
3663 {
3664 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003665 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 vcol += incr;
3667 }
3668 bd.textcol = (colnr_T)(ptr - oldp);
3669
3670 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3671
3672 if (vcol < col) /* line too short, padd with spaces */
3673 bd.startspaces = col - vcol;
3674 else if (vcol > col)
3675 {
3676 bd.endspaces = vcol - col;
3677 bd.startspaces = incr - bd.endspaces;
3678 --bd.textcol;
3679 delcount = 1;
3680#ifdef FEAT_MBYTE
3681 if (has_mbyte)
3682 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3683#endif
3684 if (oldp[bd.textcol] != TAB)
3685 {
3686 /* Only a Tab can be split into spaces. Other
3687 * characters will have to be moved to after the
3688 * block, causing misalignment. */
3689 delcount = 0;
3690 bd.endspaces = 0;
3691 }
3692 }
3693
3694 yanklen = (int)STRLEN(y_array[i]);
3695
3696 /* calculate number of spaces required to fill right side of block*/
3697 spaces = y_width + 1;
3698 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003699 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (spaces < 0)
3701 spaces = 0;
3702
3703 /* insert the new text */
3704 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3705 newp = alloc_check((unsigned)totlen + oldlen + 1);
3706 if (newp == NULL)
3707 break;
3708 /* copy part up to cursor to new line */
3709 ptr = newp;
3710 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3711 ptr += bd.textcol;
3712 /* may insert some spaces before the new text */
3713 copy_spaces(ptr, (size_t)bd.startspaces);
3714 ptr += bd.startspaces;
3715 /* insert the new text */
3716 for (j = 0; j < count; ++j)
3717 {
3718 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3719 ptr += yanklen;
3720
3721 /* insert block's trailing spaces only if there's text behind */
3722 if ((j < count - 1 || !shortline) && spaces)
3723 {
3724 copy_spaces(ptr, (size_t)spaces);
3725 ptr += spaces;
3726 }
3727 }
3728 /* may insert some spaces after the new text */
3729 copy_spaces(ptr, (size_t)bd.endspaces);
3730 ptr += bd.endspaces;
3731 /* move the text after the cursor to the end of the line. */
3732 mch_memmove(ptr, oldp + bd.textcol + delcount,
3733 (size_t)(oldlen - bd.textcol - delcount + 1));
3734 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3735
3736 ++curwin->w_cursor.lnum;
3737 if (i == 0)
3738 curwin->w_cursor.col += bd.startspaces;
3739 }
3740
3741 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3742
3743 /* Set '[ mark. */
3744 curbuf->b_op_start = curwin->w_cursor;
3745 curbuf->b_op_start.lnum = lnum;
3746
3747 /* adjust '] mark */
3748 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3749 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003750# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (flags & PUT_CURSEND)
3754 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003755 colnr_T len;
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 curwin->w_cursor = curbuf->b_op_end;
3758 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003759
3760 /* in Insert mode we might be after the NUL, correct for that */
3761 len = (colnr_T)STRLEN(ml_get_curline());
3762 if (curwin->w_cursor.col > len)
3763 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765 else
3766 curwin->w_cursor.lnum = lnum;
3767 }
3768 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 /*
3771 * Character or Line mode
3772 */
3773 if (y_type == MCHAR)
3774 {
3775 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3776 * char */
3777 if (dir == FORWARD && gchar_cursor() != NUL)
3778 {
3779#ifdef FEAT_MBYTE
3780 if (has_mbyte)
3781 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003782 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 /* put it on the next of the multi-byte character. */
3785 col += bytelen;
3786 if (yanklen)
3787 {
3788 curwin->w_cursor.col += bytelen;
3789 curbuf->b_op_end.col += bytelen;
3790 }
3791 }
3792 else
3793#endif
3794 {
3795 ++col;
3796 if (yanklen)
3797 {
3798 ++curwin->w_cursor.col;
3799 ++curbuf->b_op_end.col;
3800 }
3801 }
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 curbuf->b_op_start = curwin->w_cursor;
3804 }
3805 /*
3806 * Line mode: BACKWARD is the same as FORWARD on the previous line
3807 */
3808 else if (dir == BACKWARD)
3809 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003810 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 /*
3813 * simple case: insert into current line
3814 */
3815 if (y_type == MCHAR && y_size == 1)
3816 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003817 do {
3818 totlen = count * yanklen;
3819 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003821 oldp = ml_get(lnum);
3822 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3823 if (newp == NULL)
3824 goto end; /* alloc() gave an error message */
3825 mch_memmove(newp, oldp, (size_t)col);
3826 ptr = newp + col;
3827 for (i = 0; i < count; ++i)
3828 {
3829 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3830 ptr += yanklen;
3831 }
3832 STRMOVE(ptr, oldp + col);
3833 ml_replace(lnum, newp, FALSE);
3834 /* Place cursor on last putted char. */
3835 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003836 {
3837 /* make sure curwin->w_virtcol is updated */
3838 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003839 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003842 if (VIsual_active)
3843 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003844 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003845
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003846 if (VIsual_active) /* reset lnum to the last visual line */
3847 lnum--;
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 curbuf->b_op_end = curwin->w_cursor;
3850 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3851 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3852 ++curwin->w_cursor.col;
3853 changed_bytes(lnum, col);
3854 }
3855 else
3856 {
3857 /*
3858 * Insert at least one line. When y_type is MCHAR, break the first
3859 * line in two.
3860 */
3861 for (cnt = 1; cnt <= count; ++cnt)
3862 {
3863 i = 0;
3864 if (y_type == MCHAR)
3865 {
3866 /*
3867 * Split the current line in two at the insert position.
3868 * First insert y_array[size - 1] in front of second line.
3869 * Then append y_array[0] to first line.
3870 */
3871 lnum = new_cursor.lnum;
3872 ptr = ml_get(lnum) + col;
3873 totlen = (int)STRLEN(y_array[y_size - 1]);
3874 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3875 if (newp == NULL)
3876 goto error;
3877 STRCPY(newp, y_array[y_size - 1]);
3878 STRCAT(newp, ptr);
3879 /* insert second line */
3880 ml_append(lnum, newp, (colnr_T)0, FALSE);
3881 vim_free(newp);
3882
3883 oldp = ml_get(lnum);
3884 newp = alloc_check((unsigned)(col + yanklen + 1));
3885 if (newp == NULL)
3886 goto error;
3887 /* copy first part of line */
3888 mch_memmove(newp, oldp, (size_t)col);
3889 /* append to first line */
3890 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3891 ml_replace(lnum, newp, FALSE);
3892
3893 curwin->w_cursor.lnum = lnum;
3894 i = 1;
3895 }
3896
3897 for (; i < y_size; ++i)
3898 {
3899 if ((y_type != MCHAR || i < y_size - 1)
3900 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3901 == FAIL)
3902 goto error;
3903 lnum++;
3904 ++nr_lines;
3905 if (flags & PUT_FIXINDENT)
3906 {
3907 old_pos = curwin->w_cursor;
3908 curwin->w_cursor.lnum = lnum;
3909 ptr = ml_get(lnum);
3910 if (cnt == count && i == y_size - 1)
3911 lendiff = (int)STRLEN(ptr);
3912#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3913 if (*ptr == '#' && preprocs_left())
3914 indent = 0; /* Leave # lines at start */
3915 else
3916#endif
3917 if (*ptr == NUL)
3918 indent = 0; /* Ignore empty lines */
3919 else if (first_indent)
3920 {
3921 indent_diff = orig_indent - get_indent();
3922 indent = orig_indent;
3923 first_indent = FALSE;
3924 }
3925 else if ((indent = get_indent() + indent_diff) < 0)
3926 indent = 0;
3927 (void)set_indent(indent, 0);
3928 curwin->w_cursor = old_pos;
3929 /* remember how many chars were removed */
3930 if (cnt == count && i == y_size - 1)
3931 lendiff -= (int)STRLEN(ml_get(lnum));
3932 }
3933 }
3934 }
3935
3936error:
3937 /* Adjust marks. */
3938 if (y_type == MLINE)
3939 {
3940 curbuf->b_op_start.col = 0;
3941 if (dir == FORWARD)
3942 curbuf->b_op_start.lnum++;
3943 }
3944 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3945 (linenr_T)MAXLNUM, nr_lines, 0L);
3946
3947 /* note changed text for displaying and folding */
3948 if (y_type == MCHAR)
3949 changed_lines(curwin->w_cursor.lnum, col,
3950 curwin->w_cursor.lnum + 1, nr_lines);
3951 else
3952 changed_lines(curbuf->b_op_start.lnum, 0,
3953 curbuf->b_op_start.lnum, nr_lines);
3954
3955 /* put '] mark at last inserted character */
3956 curbuf->b_op_end.lnum = lnum;
3957 /* correct length for change in indent */
3958 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3959 if (col > 1)
3960 curbuf->b_op_end.col = col - 1;
3961 else
3962 curbuf->b_op_end.col = 0;
3963
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003964 if (flags & PUT_CURSLINE)
3965 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003966 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003967 curwin->w_cursor.lnum = lnum;
3968 beginline(BL_WHITE | BL_FIX);
3969 }
3970 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 /* put cursor after inserted text */
3973 if (y_type == MLINE)
3974 {
3975 if (lnum >= curbuf->b_ml.ml_line_count)
3976 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3977 else
3978 curwin->w_cursor.lnum = lnum + 1;
3979 curwin->w_cursor.col = 0;
3980 }
3981 else
3982 {
3983 curwin->w_cursor.lnum = lnum;
3984 curwin->w_cursor.col = col;
3985 }
3986 }
3987 else if (y_type == MLINE)
3988 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003989 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 curwin->w_cursor.col = 0;
3991 if (dir == FORWARD)
3992 ++curwin->w_cursor.lnum;
3993 beginline(BL_WHITE | BL_FIX);
3994 }
3995 else /* put cursor on first inserted character */
3996 curwin->w_cursor = new_cursor;
3997 }
3998 }
3999
4000 msgmore(nr_lines);
4001 curwin->w_set_curswant = TRUE;
4002
4003end:
4004 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004006 if (regname == '=')
4007 vim_free(y_array);
4008
Bram Moolenaar033d8882013-09-25 23:24:57 +02004009 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004010
Bram Moolenaar677ee682005-01-27 14:41:15 +00004011 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004012 adjust_cursor_eol();
4013}
4014
4015/*
4016 * When the cursor is on the NUL past the end of the line and it should not be
4017 * there move it left.
4018 */
4019 void
4020adjust_cursor_eol()
4021{
4022 if (curwin->w_cursor.col > 0
4023 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004024#ifdef FEAT_VIRTUALEDIT
4025 && (ve_flags & VE_ONEMORE) == 0
4026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 && !(restart_edit || (State & INSERT)))
4028 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004029 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004030 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032#ifdef FEAT_VIRTUALEDIT
4033 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004034 {
4035 colnr_T scol, ecol;
4036
4037 /* Coladd is set to the width of the last character. */
4038 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4039 curwin->w_cursor.coladd = ecol - scol + 1;
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#endif
4042 }
4043}
4044
4045#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4046/*
4047 * Return TRUE if lines starting with '#' should be left aligned.
4048 */
4049 int
4050preprocs_left()
4051{
4052 return
4053# ifdef FEAT_SMARTINDENT
4054# ifdef FEAT_CINDENT
4055 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4056# else
4057 curbuf->b_p_si
4058# endif
4059# endif
4060# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004061 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4062 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063# endif
4064 ;
4065}
4066#endif
4067
4068/* Return the character name of the register with the given number */
4069 int
4070get_register_name(num)
4071 int num;
4072{
4073 if (num == -1)
4074 return '"';
4075 else if (num < 10)
4076 return num + '0';
4077 else if (num == DELETION_REGISTER)
4078 return '-';
4079#ifdef FEAT_CLIPBOARD
4080 else if (num == STAR_REGISTER)
4081 return '*';
4082 else if (num == PLUS_REGISTER)
4083 return '+';
4084#endif
4085 else
4086 {
4087#ifdef EBCDIC
4088 int i;
4089
4090 /* EBCDIC is really braindead ... */
4091 i = 'a' + (num - 10);
4092 if (i > 'i')
4093 i += 7;
4094 if (i > 'r')
4095 i += 8;
4096 return i;
4097#else
4098 return num + 'a' - 10;
4099#endif
4100 }
4101}
4102
4103/*
4104 * ":dis" and ":registers": Display the contents of the yank registers.
4105 */
4106 void
4107ex_display(eap)
4108 exarg_T *eap;
4109{
4110 int i, n;
4111 long j;
4112 char_u *p;
4113 struct yankreg *yb;
4114 int name;
4115 int attr;
4116 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004117#ifdef FEAT_MBYTE
4118 int clen;
4119#else
4120# define clen 1
4121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 if (arg != NULL && *arg == NUL)
4124 arg = NULL;
4125 attr = hl_attr(HLF_8);
4126
4127 /* Highlight title */
4128 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4129 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4130 {
4131 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004132 if (arg != NULL && vim_strchr(arg, name) == NULL
4133#ifdef ONE_CLIPBOARD
4134 /* Star register and plus register contain the same thing. */
4135 && (name != '*' || vim_strchr(arg, '+') == NULL)
4136#endif
4137 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 continue; /* did not ask for this register */
4139
4140#ifdef FEAT_CLIPBOARD
4141 /* Adjust register name for "unnamed" in 'clipboard'.
4142 * When it's a clipboard register, fill it with the current contents
4143 * of the clipboard. */
4144 adjust_clip_reg(&name);
4145 (void)may_get_selection(name);
4146#endif
4147
4148 if (i == -1)
4149 {
4150 if (y_previous != NULL)
4151 yb = y_previous;
4152 else
4153 yb = &(y_regs[0]);
4154 }
4155 else
4156 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004157
4158#ifdef FEAT_EVAL
4159 if (name == MB_TOLOWER(redir_reg)
4160 || (redir_reg == '"' && yb == y_previous))
4161 continue; /* do not list register being written to, the
4162 * pointer can be freed */
4163#endif
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if (yb->y_array != NULL)
4166 {
4167 msg_putchar('\n');
4168 msg_putchar('"');
4169 msg_putchar(name);
4170 MSG_PUTS(" ");
4171
4172 n = (int)Columns - 6;
4173 for (j = 0; j < yb->y_size && n > 1; ++j)
4174 {
4175 if (j)
4176 {
4177 MSG_PUTS_ATTR("^J", attr);
4178 n -= 2;
4179 }
4180 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004183 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004184#endif
4185 msg_outtrans_len(p, clen);
4186#ifdef FEAT_MBYTE
4187 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#endif
4189 }
4190 }
4191 if (n > 1 && yb->y_type == MLINE)
4192 MSG_PUTS_ATTR("^J", attr);
4193 out_flush(); /* show one line at a time */
4194 }
4195 ui_breakcheck();
4196 }
4197
4198 /*
4199 * display last inserted text
4200 */
4201 if ((p = get_last_insert()) != NULL
4202 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4203 {
4204 MSG_PUTS("\n\". ");
4205 dis_msg(p, TRUE);
4206 }
4207
4208 /*
4209 * display last command line
4210 */
4211 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4212 && !got_int)
4213 {
4214 MSG_PUTS("\n\": ");
4215 dis_msg(last_cmdline, FALSE);
4216 }
4217
4218 /*
4219 * display current file name
4220 */
4221 if (curbuf->b_fname != NULL
4222 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4223 {
4224 MSG_PUTS("\n\"% ");
4225 dis_msg(curbuf->b_fname, FALSE);
4226 }
4227
4228 /*
4229 * display alternate file name
4230 */
4231 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4232 {
4233 char_u *fname;
4234 linenr_T dummy;
4235
4236 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4237 {
4238 MSG_PUTS("\n\"# ");
4239 dis_msg(fname, FALSE);
4240 }
4241 }
4242
4243 /*
4244 * display last search pattern
4245 */
4246 if (last_search_pat() != NULL
4247 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4248 {
4249 MSG_PUTS("\n\"/ ");
4250 dis_msg(last_search_pat(), FALSE);
4251 }
4252
4253#ifdef FEAT_EVAL
4254 /*
4255 * display last used expression
4256 */
4257 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4258 && !got_int)
4259 {
4260 MSG_PUTS("\n\"= ");
4261 dis_msg(expr_line, FALSE);
4262 }
4263#endif
4264}
4265
4266/*
4267 * display a string for do_dis()
4268 * truncate at end of screen line
4269 */
4270 static void
4271dis_msg(p, skip_esc)
4272 char_u *p;
4273 int skip_esc; /* if TRUE, ignore trailing ESC */
4274{
4275 int n;
4276#ifdef FEAT_MBYTE
4277 int l;
4278#endif
4279
4280 n = (int)Columns - 6;
4281 while (*p != NUL
4282 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4283 && (n -= ptr2cells(p)) >= 0)
4284 {
4285#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004286 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 msg_outtrans_len(p, l);
4289 p += l;
4290 }
4291 else
4292#endif
4293 msg_outtrans_len(p++, 1);
4294 }
4295 ui_breakcheck();
4296}
4297
Bram Moolenaar81340392012-06-06 16:12:59 +02004298#if defined(FEAT_COMMENTS) || defined(PROTO)
4299/*
4300 * If "process" is TRUE and the line begins with a comment leader (possibly
4301 * after some white space), return a pointer to the text after it. Put a boolean
4302 * value indicating whether the line ends with an unclosed comment in
4303 * "is_comment".
4304 * line - line to be processed,
4305 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004306 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004307 * include_space - whether to also skip space following the comment leader,
4308 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004309 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004310 */
4311 static char_u *
4312skip_comment(line, process, include_space, is_comment)
4313 char_u *line;
4314 int process;
4315 int include_space;
4316 int *is_comment;
4317{
4318 char_u *comment_flags = NULL;
4319 int lead_len;
4320 int leader_offset = get_last_leader_offset(line, &comment_flags);
4321
4322 *is_comment = FALSE;
4323 if (leader_offset != -1)
4324 {
4325 /* Let's check whether the line ends with an unclosed comment.
4326 * If the last comment leader has COM_END in flags, there's no comment.
4327 */
4328 while (*comment_flags)
4329 {
4330 if (*comment_flags == COM_END
4331 || *comment_flags == ':')
4332 break;
4333 ++comment_flags;
4334 }
4335 if (*comment_flags != COM_END)
4336 *is_comment = TRUE;
4337 }
4338
4339 if (process == FALSE)
4340 return line;
4341
4342 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4343
4344 if (lead_len == 0)
4345 return line;
4346
4347 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004348 * - COM_END,
4349 * - colon,
4350 * whichever comes first.
4351 */
4352 while (*comment_flags)
4353 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004354 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004355 || *comment_flags == ':')
4356 {
4357 break;
4358 }
4359 ++comment_flags;
4360 }
4361
4362 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004363 * starting with a closing part of a three-part comment. That's good,
4364 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004365 */
4366 if (*comment_flags == ':' || *comment_flags == NUL)
4367 line += lead_len;
4368
4369 return line;
4370}
4371#endif
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004374 * Join 'count' lines (minimal 2) at cursor position.
4375 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004376 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4377 * leaders should not be removed.
4378 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4379 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004381 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 int
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004384do_join(count, insert_space, save_undo, use_formatoptions, setmark)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004385 long count;
4386 int insert_space;
4387 int save_undo;
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 int use_formatoptions UNUSED;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004389 int setmark;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004391 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004392 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004393 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004395 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004396 int endcurr1 = NUL;
4397 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004398 int currsize = 0; /* size of the current line */
4399 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004401 colnr_T col = 0;
4402 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004403#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004404 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004405 int remove_comments = (use_formatoptions == TRUE)
4406 && has_format_option(FO_REMOVE_COMS);
4407 int prev_was_comment;
4408#endif
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004411 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4412 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004415 /* Allocate an array to store the number of spaces inserted before each
4416 * line. We will use it to pre-compute the length of the new line and the
4417 * proper placement of each original line in the new one. */
4418 spaces = lalloc_clear((long_u)count, TRUE);
4419 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004421#if defined(FEAT_COMMENTS) || defined(PROTO)
4422 if (remove_comments)
4423 {
4424 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4425 if (comments == NULL)
4426 {
4427 vim_free(spaces);
4428 return FAIL;
4429 }
4430 }
4431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004434 * Don't move anything, just compute the final line length
4435 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004437 for (t = 0; t < count; ++t)
4438 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004439 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004440 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004441 {
4442 /* Set the '[ mark. */
4443 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4444 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4445 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004446#if defined(FEAT_COMMENTS) || defined(PROTO)
4447 if (remove_comments)
4448 {
4449 /* We don't want to remove the comment leader if the
4450 * previous line is not a comment. */
4451 if (t > 0 && prev_was_comment)
4452 {
4453
4454 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4455 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004456 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004457 curr = new_curr;
4458 }
4459 else
4460 curr = skip_comment(curr, FALSE, insert_space,
4461 &prev_was_comment);
4462 }
4463#endif
4464
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004465 if (insert_space && t > 0)
4466 {
4467 curr = skipwhite(curr);
4468 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4469#ifdef FEAT_MBYTE
4470 && (!has_format_option(FO_MBYTE_JOIN)
4471 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4472 && (!has_format_option(FO_MBYTE_JOIN2)
4473 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4474#endif
4475 )
4476 {
4477 /* don't add a space if the line is ending in a space */
4478 if (endcurr1 == ' ')
4479 endcurr1 = endcurr2;
4480 else
4481 ++spaces[t];
4482 /* extra space when 'joinspaces' set and line ends in '.' */
4483 if ( p_js
4484 && (endcurr1 == '.'
4485 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4486 && (endcurr1 == '?' || endcurr1 == '!'))))
4487 ++spaces[t];
4488 }
4489 }
4490 currsize = (int)STRLEN(curr);
4491 sumsize += currsize + spaces[t];
4492 endcurr1 = endcurr2 = NUL;
4493 if (insert_space && currsize > 0)
4494 {
4495#ifdef FEAT_MBYTE
4496 if (has_mbyte)
4497 {
4498 cend = curr + currsize;
4499 mb_ptr_back(curr, cend);
4500 endcurr1 = (*mb_ptr2char)(cend);
4501 if (cend > curr)
4502 {
4503 mb_ptr_back(curr, cend);
4504 endcurr2 = (*mb_ptr2char)(cend);
4505 }
4506 }
4507 else
4508#endif
4509 {
4510 endcurr1 = *(curr + currsize - 1);
4511 if (currsize > 1)
4512 endcurr2 = *(curr + currsize - 2);
4513 }
4514 }
4515 line_breakcheck();
4516 if (got_int)
4517 {
4518 ret = FAIL;
4519 goto theend;
4520 }
4521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004523 /* store the column position before last line */
4524 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004526 /* allocate the space for the new line */
4527 newp = alloc_check((unsigned)(sumsize + 1));
4528 cend = newp + sumsize;
4529 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004531 /*
4532 * Move affected lines to the new long one.
4533 *
4534 * Move marks from each deleted line to the joined line, adjusting the
4535 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4536 * should not really be a problem.
4537 */
4538 for (t = count - 1; ; --t)
4539 {
4540 cend -= currsize;
4541 mch_memmove(cend, curr, (size_t)currsize);
4542 if (spaces[t] > 0)
4543 {
4544 cend -= spaces[t];
4545 copy_spaces(cend, (size_t)(spaces[t]));
4546 }
4547 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004548 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004549 if (t == 0)
4550 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004551 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004552#if defined(FEAT_COMMENTS) || defined(PROTO)
4553 if (remove_comments)
4554 curr += comments[t - 1];
4555#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 if (insert_space && t > 1)
4557 curr = skipwhite(curr);
4558 currsize = (int)STRLEN(curr);
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4561
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004562 if (setmark)
4563 {
4564 /* Set the '] mark. */
4565 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4566 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4567 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /* Only report the change in the first line here, del_lines() will report
4570 * the deleted line. */
4571 changed_lines(curwin->w_cursor.lnum, currsize,
4572 curwin->w_cursor.lnum + 1, 0L);
4573
4574 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004575 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 * briefly, and then move it back. After del_lines() the cursor may
4577 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 */
4579 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004581 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 curwin->w_cursor.lnum = t;
4583
4584 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004585 * Set the cursor column:
4586 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004587 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004589 curwin->w_cursor.col =
4590 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#ifdef FEAT_VIRTUALEDIT
4594 curwin->w_cursor.coladd = 0;
4595#endif
4596 curwin->w_set_curswant = TRUE;
4597
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004598theend:
4599 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004600#if defined(FEAT_COMMENTS) || defined(PROTO)
4601 if (remove_comments)
4602 vim_free(comments);
4603#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004604 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
4607#ifdef FEAT_COMMENTS
4608/*
4609 * Return TRUE if the two comment leaders given are the same. "lnum" is
4610 * the first line. White-space is ignored. Note that the whole of
4611 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4612 */
4613 static int
4614same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4615 linenr_T lnum;
4616 int leader1_len;
4617 char_u *leader1_flags;
4618 int leader2_len;
4619 char_u *leader2_flags;
4620{
4621 int idx1 = 0, idx2 = 0;
4622 char_u *p;
4623 char_u *line1;
4624 char_u *line2;
4625
4626 if (leader1_len == 0)
4627 return (leader2_len == 0);
4628
4629 /*
4630 * If first leader has 'f' flag, the lines can be joined only if the
4631 * second line does not have a leader.
4632 * If first leader has 'e' flag, the lines can never be joined.
4633 * If fist leader has 's' flag, the lines can only be joined if there is
4634 * some text after it and the second line has the 'm' flag.
4635 */
4636 if (leader1_flags != NULL)
4637 {
4638 for (p = leader1_flags; *p && *p != ':'; ++p)
4639 {
4640 if (*p == COM_FIRST)
4641 return (leader2_len == 0);
4642 if (*p == COM_END)
4643 return FALSE;
4644 if (*p == COM_START)
4645 {
4646 if (*(ml_get(lnum) + leader1_len) == NUL)
4647 return FALSE;
4648 if (leader2_flags == NULL || leader2_len == 0)
4649 return FALSE;
4650 for (p = leader2_flags; *p && *p != ':'; ++p)
4651 if (*p == COM_MIDDLE)
4652 return TRUE;
4653 return FALSE;
4654 }
4655 }
4656 }
4657
4658 /*
4659 * Get current line and next line, compare the leaders.
4660 * The first line has to be saved, only one line can be locked at a time.
4661 */
4662 line1 = vim_strsave(ml_get(lnum));
4663 if (line1 != NULL)
4664 {
4665 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4666 ;
4667 line2 = ml_get(lnum + 1);
4668 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4669 {
4670 if (!vim_iswhite(line2[idx2]))
4671 {
4672 if (line1[idx1++] != line2[idx2])
4673 break;
4674 }
4675 else
4676 while (vim_iswhite(line1[idx1]))
4677 ++idx1;
4678 }
4679 vim_free(line1);
4680 }
4681 return (idx2 == leader2_len && idx1 == leader1_len);
4682}
4683#endif
4684
4685/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004686 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
4688 void
4689op_format(oap, keep_cursor)
4690 oparg_T *oap;
4691 int keep_cursor; /* keep cursor on same text char */
4692{
4693 long old_line_count = curbuf->b_ml.ml_line_count;
4694
4695 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4696 * can put it back there. */
4697 curwin->w_cursor = oap->cursor_start;
4698
4699 if (u_save((linenr_T)(oap->start.lnum - 1),
4700 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4701 return;
4702 curwin->w_cursor = oap->start;
4703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 if (oap->is_VIsual)
4705 /* When there is no change: need to remove the Visual selection */
4706 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 /* Set '[ mark at the start of the formatted area */
4709 curbuf->b_op_start = oap->start;
4710
4711 /* For "gw" remember the cursor position and put it back below (adjusted
4712 * for joined and split lines). */
4713 if (keep_cursor)
4714 saved_cursor = oap->cursor_start;
4715
Bram Moolenaar81a82092008-03-12 16:27:00 +00004716 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718 /*
4719 * Leave the cursor at the first non-blank of the last formatted line.
4720 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4721 * line, so "." will do the next lines.
4722 */
4723 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4724 ++curwin->w_cursor.lnum;
4725 beginline(BL_WHITE | BL_FIX);
4726 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4727 msgmore(old_line_count);
4728
4729 /* put '] mark on the end of the formatted area */
4730 curbuf->b_op_end = curwin->w_cursor;
4731
4732 if (keep_cursor)
4733 {
4734 curwin->w_cursor = saved_cursor;
4735 saved_cursor.lnum = 0;
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (oap->is_VIsual)
4739 {
4740 win_T *wp;
4741
4742 FOR_ALL_WINDOWS(wp)
4743 {
4744 if (wp->w_old_cursor_lnum != 0)
4745 {
4746 /* When lines have been inserted or deleted, adjust the end of
4747 * the Visual area to be redrawn. */
4748 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4749 wp->w_old_cursor_lnum += old_line_count;
4750 else
4751 wp->w_old_visual_lnum += old_line_count;
4752 }
4753 }
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755}
4756
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004757#if defined(FEAT_EVAL) || defined(PROTO)
4758/*
4759 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4760 */
4761 void
4762op_formatexpr(oap)
4763 oparg_T *oap;
4764{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004765 if (oap->is_VIsual)
4766 /* When there is no change: need to remove the Visual selection */
4767 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004768
Bram Moolenaar700303e2010-07-11 17:35:50 +02004769 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4770 /* As documented: when 'formatexpr' returns non-zero fall back to
4771 * internal formatting. */
4772 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004773}
4774
4775 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004776fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004777 linenr_T lnum;
4778 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004779 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004780{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004781 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4782 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004783 int r;
4784
4785 /*
4786 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004787 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004788 */
4789 set_vim_var_nr(VV_LNUM, lnum);
4790 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004791 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004792
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004793 /*
4794 * Evaluate the function.
4795 */
4796 if (use_sandbox)
4797 ++sandbox;
4798 r = eval_to_number(curbuf->b_p_fex);
4799 if (use_sandbox)
4800 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004801
4802 set_vim_var_string(VV_CHAR, NULL, -1);
4803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004804 return r;
4805}
4806#endif
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808/*
4809 * Format "line_count" lines, starting at the cursor position.
4810 * When "line_count" is negative, format until the end of the paragraph.
4811 * Lines after the cursor line are saved for undo, caller must have saved the
4812 * first line.
4813 */
4814 void
Bram Moolenaar81a82092008-03-12 16:27:00 +00004815format_lines(line_count, avoid_fex)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 linenr_T line_count;
Bram Moolenaar81a82092008-03-12 16:27:00 +00004817 int avoid_fex; /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 int max_len;
4820 int is_not_par; /* current line not part of parag. */
4821 int next_is_not_par; /* next line not part of paragraph */
4822 int is_end_par; /* at end of paragraph */
4823 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4824 int next_is_start_par = FALSE;
4825#ifdef FEAT_COMMENTS
4826 int leader_len = 0; /* leader len of current line */
4827 int next_leader_len; /* leader len of next line */
4828 char_u *leader_flags = NULL; /* flags for leader of current line */
4829 char_u *next_leader_flags; /* flags for leader of next line */
4830 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004831 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#endif
4833 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004834 int second_indent = -1; /* indent for second line (comment
4835 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 int do_second_indent;
4837 int do_number_indent;
4838 int do_trail_white;
4839 int first_par_line = TRUE;
4840 int smd_save;
4841 long count;
4842 int need_set_indent = TRUE; /* set indent of next paragraph */
4843 int force_format = FALSE;
4844 int old_State = State;
4845
4846 /* length of a line to force formatting: 3 * 'tw' */
4847 max_len = comp_textwidth(TRUE) * 3;
4848
4849 /* check for 'q', '2' and '1' in 'formatoptions' */
4850#ifdef FEAT_COMMENTS
4851 do_comments = has_format_option(FO_Q_COMS);
4852#endif
4853 do_second_indent = has_format_option(FO_Q_SECOND);
4854 do_number_indent = has_format_option(FO_Q_NUMBER);
4855 do_trail_white = has_format_option(FO_WHITE_PAR);
4856
4857 /*
4858 * Get info about the previous and current line.
4859 */
4860 if (curwin->w_cursor.lnum > 1)
4861 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4862#ifdef FEAT_COMMENTS
4863 , &leader_len, &leader_flags, do_comments
4864#endif
4865 );
4866 else
4867 is_not_par = TRUE;
4868 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4869#ifdef FEAT_COMMENTS
4870 , &next_leader_len, &next_leader_flags, do_comments
4871#endif
4872 );
4873 is_end_par = (is_not_par || next_is_not_par);
4874 if (!is_end_par && do_trail_white)
4875 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4876
4877 curwin->w_cursor.lnum--;
4878 for (count = line_count; count != 0 && !got_int; --count)
4879 {
4880 /*
4881 * Advance to next paragraph.
4882 */
4883 if (advance)
4884 {
4885 curwin->w_cursor.lnum++;
4886 prev_is_end_par = is_end_par;
4887 is_not_par = next_is_not_par;
4888#ifdef FEAT_COMMENTS
4889 leader_len = next_leader_len;
4890 leader_flags = next_leader_flags;
4891#endif
4892 }
4893
4894 /*
4895 * The last line to be formatted.
4896 */
4897 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4898 {
4899 next_is_not_par = TRUE;
4900#ifdef FEAT_COMMENTS
4901 next_leader_len = 0;
4902 next_leader_flags = NULL;
4903#endif
4904 }
4905 else
4906 {
4907 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4908#ifdef FEAT_COMMENTS
4909 , &next_leader_len, &next_leader_flags, do_comments
4910#endif
4911 );
4912 if (do_number_indent)
4913 next_is_start_par =
4914 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4915 }
4916 advance = TRUE;
4917 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4918 if (!is_end_par && do_trail_white)
4919 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4920
4921 /*
4922 * Skip lines that are not in a paragraph.
4923 */
4924 if (is_not_par)
4925 {
4926 if (line_count < 0)
4927 break;
4928 }
4929 else
4930 {
4931 /*
4932 * For the first line of a paragraph, check indent of second line.
4933 * Don't do this for comments and empty lines.
4934 */
4935 if (first_par_line
4936 && (do_second_indent || do_number_indent)
4937 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004938 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004940 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4941 {
4942#ifdef FEAT_COMMENTS
4943 if (leader_len == 0 && next_leader_len == 0)
4944 {
4945 /* no comment found */
4946#endif
4947 second_indent =
4948 get_indent_lnum(curwin->w_cursor.lnum + 1);
4949#ifdef FEAT_COMMENTS
4950 }
4951 else
4952 {
4953 second_indent = next_leader_len;
4954 do_comments_list = 1;
4955 }
4956#endif
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004959 {
4960#ifdef FEAT_COMMENTS
4961 if (leader_len == 0 && next_leader_len == 0)
4962 {
4963 /* no comment found */
4964#endif
4965 second_indent =
4966 get_number_indent(curwin->w_cursor.lnum);
4967#ifdef FEAT_COMMENTS
4968 }
4969 else
4970 {
4971 /* get_number_indent() is now "comment aware"... */
4972 second_indent =
4973 get_number_indent(curwin->w_cursor.lnum);
4974 do_comments_list = 1;
4975 }
4976#endif
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979
4980 /*
4981 * When the comment leader changes, it's the end of the paragraph.
4982 */
4983 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4984#ifdef FEAT_COMMENTS
4985 || !same_leader(curwin->w_cursor.lnum,
4986 leader_len, leader_flags,
4987 next_leader_len, next_leader_flags)
4988#endif
4989 )
4990 is_end_par = TRUE;
4991
4992 /*
4993 * If we have got to the end of a paragraph, or the line is
4994 * getting long, format it.
4995 */
4996 if (is_end_par || force_format)
4997 {
4998 if (need_set_indent)
4999 /* replace indent in first line with minimal number of
5000 * tabs and spaces, according to current options */
5001 (void)set_indent(get_indent(), SIN_CHANGED);
5002
5003 /* put cursor on last non-space */
5004 State = NORMAL; /* don't go past end-of-line */
5005 coladvance((colnr_T)MAXCOL);
5006 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5007 dec_cursor();
5008
5009 /* do the formatting, without 'showmode' */
5010 State = INSERT; /* for open_line() */
5011 smd_save = p_smd;
5012 p_smd = FALSE;
5013 insertchar(NUL, INSCHAR_FORMAT
5014#ifdef FEAT_COMMENTS
5015 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005016 + (do_comments && do_comments_list
5017 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005019 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 State = old_State;
5021 p_smd = smd_save;
5022 second_indent = -1;
5023 /* at end of par.: need to set indent of next par. */
5024 need_set_indent = is_end_par;
5025 if (is_end_par)
5026 {
5027 /* When called with a negative line count, break at the
5028 * end of the paragraph. */
5029 if (line_count < 0)
5030 break;
5031 first_par_line = TRUE;
5032 }
5033 force_format = FALSE;
5034 }
5035
5036 /*
5037 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005038 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 */
5040 if (!is_end_par)
5041 {
5042 advance = FALSE;
5043 curwin->w_cursor.lnum++;
5044 curwin->w_cursor.col = 0;
5045 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005049 {
5050 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5052 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005053 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005055 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5056 {
5057 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005058 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005059
5060 if (indent > 0)
5061 {
5062 (void)del_bytes(indent, FALSE, FALSE);
5063 mark_col_adjust(curwin->w_cursor.lnum,
5064 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005065 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005068 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 beep_flush();
5071 break;
5072 }
5073 first_par_line = FALSE;
5074 /* If the line is getting long, format it next time */
5075 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5076 force_format = TRUE;
5077 else
5078 force_format = FALSE;
5079 }
5080 }
5081 line_breakcheck();
5082 }
5083}
5084
5085/*
5086 * Return TRUE if line "lnum" ends in a white character.
5087 */
5088 static int
5089ends_in_white(lnum)
5090 linenr_T lnum;
5091{
5092 char_u *s = ml_get(lnum);
5093 size_t l;
5094
5095 if (*s == NUL)
5096 return FALSE;
5097 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5098 * invocation may call function multiple times". */
5099 l = STRLEN(s) - 1;
5100 return vim_iswhite(s[l]);
5101}
5102
5103/*
5104 * Blank lines, and lines containing only the comment leader, are left
5105 * untouched by the formatting. The function returns TRUE in this
5106 * case. It also returns TRUE when a line starts with the end of a comment
5107 * ('e' in comment flags), so that this line is skipped, and not joined to the
5108 * previous line. A new paragraph starts after a blank line, or when the
5109 * comment leader changes -- webb.
5110 */
5111#ifdef FEAT_COMMENTS
5112 static int
5113fmt_check_par(lnum, leader_len, leader_flags, do_comments)
5114 linenr_T lnum;
5115 int *leader_len;
5116 char_u **leader_flags;
5117 int do_comments;
5118{
5119 char_u *flags = NULL; /* init for GCC */
5120 char_u *ptr;
5121
5122 ptr = ml_get(lnum);
5123 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005124 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 else
5126 *leader_len = 0;
5127
5128 if (*leader_len > 0)
5129 {
5130 /*
5131 * Search for 'e' flag in comment leader flags.
5132 */
5133 flags = *leader_flags;
5134 while (*flags && *flags != ':' && *flags != COM_END)
5135 ++flags;
5136 }
5137
5138 return (*skipwhite(ptr + *leader_len) == NUL
5139 || (*leader_len > 0 && *flags == COM_END)
5140 || startPS(lnum, NUL, FALSE));
5141}
5142#else
5143 static int
5144fmt_check_par(lnum)
5145 linenr_T lnum;
5146{
5147 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5148}
5149#endif
5150
5151/*
5152 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5153 * previous line is in the same paragraph. Used for auto-formatting.
5154 */
5155 int
5156paragraph_start(lnum)
5157 linenr_T lnum;
5158{
5159 char_u *p;
5160#ifdef FEAT_COMMENTS
5161 int leader_len = 0; /* leader len of current line */
5162 char_u *leader_flags = NULL; /* flags for leader of current line */
5163 int next_leader_len; /* leader len of next line */
5164 char_u *next_leader_flags; /* flags for leader of next line */
5165 int do_comments; /* format comments */
5166#endif
5167
5168 if (lnum <= 1)
5169 return TRUE; /* start of the file */
5170
5171 p = ml_get(lnum - 1);
5172 if (*p == NUL)
5173 return TRUE; /* after empty line */
5174
5175#ifdef FEAT_COMMENTS
5176 do_comments = has_format_option(FO_Q_COMS);
5177#endif
5178 if (fmt_check_par(lnum - 1
5179#ifdef FEAT_COMMENTS
5180 , &leader_len, &leader_flags, do_comments
5181#endif
5182 ))
5183 return TRUE; /* after non-paragraph line */
5184
5185 if (fmt_check_par(lnum
5186#ifdef FEAT_COMMENTS
5187 , &next_leader_len, &next_leader_flags, do_comments
5188#endif
5189 ))
5190 return TRUE; /* "lnum" is not a paragraph line */
5191
5192 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5193 return TRUE; /* missing trailing space in previous line. */
5194
5195 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5196 return TRUE; /* numbered item starts in "lnum". */
5197
5198#ifdef FEAT_COMMENTS
5199 if (!same_leader(lnum - 1, leader_len, leader_flags,
5200 next_leader_len, next_leader_flags))
5201 return TRUE; /* change of comment leader. */
5202#endif
5203
5204 return FALSE;
5205}
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207/*
5208 * prepare a few things for block mode yank/delete/tilde
5209 *
5210 * for delete:
5211 * - textlen includes the first/last char to be (partly) deleted
5212 * - start/endspaces is the number of columns that are taken by the
5213 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005214 * deleted.
5215 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * - textlen includes the first/last char to be wholly yanked
5217 * - start/endspaces is the number of columns of the first/last yanked char
5218 * that are to be yanked.
5219 */
5220 static void
5221block_prep(oap, bdp, lnum, is_del)
5222 oparg_T *oap;
5223 struct block_def *bdp;
5224 linenr_T lnum;
5225 int is_del;
5226{
5227 int incr = 0;
5228 char_u *pend;
5229 char_u *pstart;
5230 char_u *line;
5231 char_u *prev_pstart;
5232 char_u *prev_pend;
5233
5234 bdp->startspaces = 0;
5235 bdp->endspaces = 0;
5236 bdp->textlen = 0;
5237 bdp->start_vcol = 0;
5238 bdp->end_vcol = 0;
5239#ifdef FEAT_VISUALEXTRA
5240 bdp->is_short = FALSE;
5241 bdp->is_oneChar = FALSE;
5242 bdp->pre_whitesp = 0;
5243 bdp->pre_whitesp_c = 0;
5244 bdp->end_char_vcols = 0;
5245#endif
5246 bdp->start_char_vcols = 0;
5247
5248 line = ml_get(lnum);
5249 pstart = line;
5250 prev_pstart = line;
5251 while (bdp->start_vcol < oap->start_vcol && *pstart)
5252 {
5253 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005254 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 bdp->start_vcol += incr;
5256#ifdef FEAT_VISUALEXTRA
5257 if (vim_iswhite(*pstart))
5258 {
5259 bdp->pre_whitesp += incr;
5260 bdp->pre_whitesp_c++;
5261 }
5262 else
5263 {
5264 bdp->pre_whitesp = 0;
5265 bdp->pre_whitesp_c = 0;
5266 }
5267#endif
5268 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005269 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 bdp->start_char_vcols = incr;
5272 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5273 {
5274 bdp->end_vcol = bdp->start_vcol;
5275#ifdef FEAT_VISUALEXTRA
5276 bdp->is_short = TRUE;
5277#endif
5278 if (!is_del || oap->op_type == OP_APPEND)
5279 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5280 }
5281 else
5282 {
5283 /* notice: this converts partly selected Multibyte characters to
5284 * spaces, too. */
5285 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5286 if (is_del && bdp->startspaces)
5287 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5288 pend = pstart;
5289 bdp->end_vcol = bdp->start_vcol;
5290 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5291 {
5292#ifdef FEAT_VISUALEXTRA
5293 bdp->is_oneChar = TRUE;
5294#endif
5295 if (oap->op_type == OP_INSERT)
5296 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5297 else if (oap->op_type == OP_APPEND)
5298 {
5299 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5300 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5301 }
5302 else
5303 {
5304 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5305 if (is_del && oap->op_type != OP_LSHIFT)
5306 {
5307 /* just putting the sum of those two into
5308 * bdp->startspaces doesn't work for Visual replace,
5309 * so we have to split the tab in two */
5310 bdp->startspaces = bdp->start_char_vcols
5311 - (bdp->start_vcol - oap->start_vcol);
5312 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5313 }
5314 }
5315 }
5316 else
5317 {
5318 prev_pend = pend;
5319 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5320 {
5321 /* Count a tab for what it's worth (if list mode not on) */
5322 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005323 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 bdp->end_vcol += incr;
5325 }
5326 if (bdp->end_vcol <= oap->end_vcol
5327 && (!is_del
5328 || oap->op_type == OP_APPEND
5329 || oap->op_type == OP_REPLACE)) /* line too short */
5330 {
5331#ifdef FEAT_VISUALEXTRA
5332 bdp->is_short = TRUE;
5333#endif
5334 /* Alternative: include spaces to fill up the block.
5335 * Disadvantage: can lead to trailing spaces when the line is
5336 * short where the text is put */
5337 /* if (!is_del || oap->op_type == OP_APPEND) */
5338 if (oap->op_type == OP_APPEND || virtual_op)
5339 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005340 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 else
5342 bdp->endspaces = 0; /* replace doesn't add characters */
5343 }
5344 else if (bdp->end_vcol > oap->end_vcol)
5345 {
5346 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5347 if (!is_del && bdp->endspaces)
5348 {
5349 bdp->endspaces = incr - bdp->endspaces;
5350 if (pend != pstart)
5351 pend = prev_pend;
5352 }
5353 }
5354 }
5355#ifdef FEAT_VISUALEXTRA
5356 bdp->end_char_vcols = incr;
5357#endif
5358 if (is_del && bdp->startspaces)
5359 pstart = prev_pstart;
5360 bdp->textlen = (int)(pend - pstart);
5361 }
5362 bdp->textcol = (colnr_T) (pstart - line);
5363 bdp->textstart = pstart;
5364}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366#ifdef FEAT_RIGHTLEFT
5367static void reverse_line __ARGS((char_u *s));
5368
5369 static void
5370reverse_line(s)
5371 char_u *s;
5372{
5373 int i, j;
5374 char_u c;
5375
5376 if ((i = (int)STRLEN(s) - 1) <= 0)
5377 return;
5378
5379 curwin->w_cursor.col = i - curwin->w_cursor.col;
5380 for (j = 0; j < i; j++, i--)
5381 {
5382 c = s[i]; s[i] = s[j]; s[j] = c;
5383 }
5384}
5385
5386# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
5387#else
5388# define RLADDSUBFIX(ptr)
5389#endif
5390
5391/*
5392 * add or subtract 'Prenum1' from a number in a line
5393 * 'command' is CTRL-A for add, CTRL-X for subtract
5394 *
5395 * return FAIL for failure, OK otherwise
5396 */
5397 int
5398do_addsub(command, Prenum1)
5399 int command;
5400 linenr_T Prenum1;
5401{
5402 int col;
5403 char_u *buf1;
5404 char_u buf2[NUMBUFLEN];
5405 int hex; /* 'X' or 'x': hex; '0': octal */
5406 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005407 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 long_u oldn;
5409 char_u *ptr;
5410 int c;
5411 int length = 0; /* character length of the number */
5412 int todel;
5413 int dohex;
5414 int dooct;
5415 int doalp;
5416 int firstdigit;
5417 int negative;
5418 int subtract;
5419
5420 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5421 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5422 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5423
5424 ptr = ml_get_curline();
5425 RLADDSUBFIX(ptr);
5426
5427 /*
5428 * First check if we are on a hexadecimal number, after the "0x".
5429 */
5430 col = curwin->w_cursor.col;
5431 if (dohex)
5432 while (col > 0 && vim_isxdigit(ptr[col]))
5433 --col;
5434 if ( dohex
5435 && col > 0
5436 && (ptr[col] == 'X'
5437 || ptr[col] == 'x')
5438 && ptr[col - 1] == '0'
5439 && vim_isxdigit(ptr[col + 1]))
5440 {
5441 /*
5442 * Found hexadecimal number, move to its start.
5443 */
5444 --col;
5445 }
5446 else
5447 {
5448 /*
5449 * Search forward and then backward to find the start of number.
5450 */
5451 col = curwin->w_cursor.col;
5452
5453 while (ptr[col] != NUL
5454 && !vim_isdigit(ptr[col])
5455 && !(doalp && ASCII_ISALPHA(ptr[col])))
5456 ++col;
5457
5458 while (col > 0
5459 && vim_isdigit(ptr[col - 1])
5460 && !(doalp && ASCII_ISALPHA(ptr[col])))
5461 --col;
5462 }
5463
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 /*
5465 * If a number was found, and saving for undo works, replace the number.
5466 */
5467 firstdigit = ptr[col];
5468 RLADDSUBFIX(ptr);
5469 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5470 || u_save_cursor() != OK)
5471 {
5472 beep_flush();
5473 return FAIL;
5474 }
5475
5476 /* get ptr again, because u_save() may have changed it */
5477 ptr = ml_get_curline();
5478 RLADDSUBFIX(ptr);
5479
5480 if (doalp && ASCII_ISALPHA(firstdigit))
5481 {
5482 /* decrement or increment alphabetic character */
5483 if (command == Ctrl_X)
5484 {
5485 if (CharOrd(firstdigit) < Prenum1)
5486 {
5487 if (isupper(firstdigit))
5488 firstdigit = 'A';
5489 else
5490 firstdigit = 'a';
5491 }
5492 else
5493#ifdef EBCDIC
5494 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5495#else
5496 firstdigit -= Prenum1;
5497#endif
5498 }
5499 else
5500 {
5501 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5502 {
5503 if (isupper(firstdigit))
5504 firstdigit = 'Z';
5505 else
5506 firstdigit = 'z';
5507 }
5508 else
5509#ifdef EBCDIC
5510 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5511#else
5512 firstdigit += Prenum1;
5513#endif
5514 }
5515 curwin->w_cursor.col = col;
5516 (void)del_char(FALSE);
5517 ins_char(firstdigit);
5518 }
5519 else
5520 {
5521 negative = FALSE;
5522 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5523 {
5524 --col;
5525 negative = TRUE;
5526 }
5527
5528 /* get the number value (unsigned) */
5529 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5530
5531 /* ignore leading '-' for hex and octal numbers */
5532 if (hex && negative)
5533 {
5534 ++col;
5535 --length;
5536 negative = FALSE;
5537 }
5538
5539 /* add or subtract */
5540 subtract = FALSE;
5541 if (command == Ctrl_X)
5542 subtract ^= TRUE;
5543 if (negative)
5544 subtract ^= TRUE;
5545
5546 oldn = n;
5547 if (subtract)
5548 n -= (unsigned long)Prenum1;
5549 else
5550 n += (unsigned long)Prenum1;
5551
5552 /* handle wraparound for decimal numbers */
5553 if (!hex)
5554 {
5555 if (subtract)
5556 {
5557 if (n > oldn)
5558 {
5559 n = 1 + (n ^ (unsigned long)-1);
5560 negative ^= TRUE;
5561 }
5562 }
5563 else /* add */
5564 {
5565 if (n < oldn)
5566 {
5567 n = (n ^ (unsigned long)-1);
5568 negative ^= TRUE;
5569 }
5570 }
5571 if (n == 0)
5572 negative = FALSE;
5573 }
5574
5575 /*
5576 * Delete the old number.
5577 */
5578 curwin->w_cursor.col = col;
5579 todel = length;
5580 c = gchar_cursor();
5581 /*
5582 * Don't include the '-' in the length, only the length of the part
5583 * after it is kept the same.
5584 */
5585 if (c == '-')
5586 --length;
5587 while (todel-- > 0)
5588 {
5589 if (c < 0x100 && isalpha(c))
5590 {
5591 if (isupper(c))
5592 hexupper = TRUE;
5593 else
5594 hexupper = FALSE;
5595 }
5596 /* del_char() will mark line needing displaying */
5597 (void)del_char(FALSE);
5598 c = gchar_cursor();
5599 }
5600
5601 /*
5602 * Prepare the leading characters in buf1[].
5603 * When there are many leading zeros it could be very long. Allocate
5604 * a bit too much.
5605 */
5606 buf1 = alloc((unsigned)length + NUMBUFLEN);
5607 if (buf1 == NULL)
5608 return FAIL;
5609 ptr = buf1;
5610 if (negative)
5611 {
5612 *ptr++ = '-';
5613 }
5614 if (hex)
5615 {
5616 *ptr++ = '0';
5617 --length;
5618 }
5619 if (hex == 'x' || hex == 'X')
5620 {
5621 *ptr++ = hex;
5622 --length;
5623 }
5624
5625 /*
5626 * Put the number characters in buf2[].
5627 */
5628 if (hex == 0)
5629 sprintf((char *)buf2, "%lu", n);
5630 else if (hex == '0')
5631 sprintf((char *)buf2, "%lo", n);
5632 else if (hex && hexupper)
5633 sprintf((char *)buf2, "%lX", n);
5634 else
5635 sprintf((char *)buf2, "%lx", n);
5636 length -= (int)STRLEN(buf2);
5637
5638 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005639 * Adjust number of zeros to the new number of digits, so the
5640 * total length of the number remains the same.
5641 * Don't do this when
5642 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005644 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 while (length-- > 0)
5646 *ptr++ = '0';
5647 *ptr = NUL;
5648 STRCAT(buf1, buf2);
5649 ins_str(buf1); /* insert the new number */
5650 vim_free(buf1);
5651 }
5652 --curwin->w_cursor.col;
5653 curwin->w_set_curswant = TRUE;
5654#ifdef FEAT_RIGHTLEFT
5655 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5656 RLADDSUBFIX(ptr);
5657#endif
5658 return OK;
5659}
5660
5661#ifdef FEAT_VIMINFO
5662 int
5663read_viminfo_register(virp, force)
5664 vir_T *virp;
5665 int force;
5666{
5667 int eof;
5668 int do_it = TRUE;
5669 int size;
5670 int limit;
5671 int i;
5672 int set_prev = FALSE;
5673 char_u *str;
5674 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005675 int new_type = MCHAR; /* init to shut up compiler */
5676 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /* We only get here (hopefully) if line[0] == '"' */
5679 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005680
5681 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (*str == '"')
5683 {
5684 set_prev = TRUE;
5685 str++;
5686 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005687
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 if (!ASCII_ISALNUM(*str) && *str != '-')
5689 {
5690 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5691 return TRUE; /* too many errors, pretend end-of-file */
5692 do_it = FALSE;
5693 }
5694 get_yank_register(*str++, FALSE);
5695 if (!force && y_current->y_array != NULL)
5696 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005697
5698 if (*str == '@')
5699 {
5700 /* "x@: register x used for @@ */
5701 if (force || execreg_lastc == NUL)
5702 execreg_lastc = str[-1];
5703 }
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 size = 0;
5706 limit = 100; /* Optimized for registers containing <= 100 lines */
5707 if (do_it)
5708 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005709 /*
5710 * Build the new register in array[].
5711 * y_array is kept as-is until done.
5712 * The "do_it" flag is reset when something is wrong, in which case
5713 * array[] needs to be freed.
5714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (set_prev)
5716 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005717 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005718 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005720 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005722 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005724 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 /* get the block width; if it's missing we get a zero, which is OK */
5726 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005727 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729
5730 while (!(eof = viminfo_readline(virp))
5731 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5732 {
5733 if (do_it)
5734 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005735 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005737 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005739
5740 if (new_array == NULL)
5741 {
5742 do_it = FALSE;
5743 break;
5744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005746 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005748 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751 str = viminfo_readstring(virp, 1, TRUE);
5752 if (str != NULL)
5753 array[size++] = str;
5754 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005755 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 do_it = FALSE;
5757 }
5758 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (do_it)
5761 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005762 /* free y_array[] */
5763 for (i = 0; i < y_current->y_size; i++)
5764 vim_free(y_current->y_array[i]);
5765 vim_free(y_current->y_array);
5766
5767 y_current->y_type = new_type;
5768 y_current->y_width = new_width;
5769 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (size == 0)
5771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 y_current->y_array = NULL;
5773 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005776 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 y_current->y_array =
5778 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5779 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005780 {
5781 if (y_current->y_array == NULL)
5782 vim_free(array[i]);
5783 else
5784 y_current->y_array[i] = array[i];
5785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005788 else
5789 {
5790 /* Free array[] if it was filled. */
5791 for (i = 0; i < size; i++)
5792 vim_free(array[i]);
5793 }
5794 vim_free(array);
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 return eof;
5797}
5798
5799 void
5800write_viminfo_registers(fp)
5801 FILE *fp;
5802{
5803 int i, j;
5804 char_u *type;
5805 char_u c;
5806 int num_lines;
5807 int max_num_lines;
5808 int max_kbyte;
5809 long len;
5810
Bram Moolenaar64404472010-06-26 06:24:45 +02005811 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
5813 /* Get '<' value, use old '"' value if '<' is not found. */
5814 max_num_lines = get_viminfo_parameter('<');
5815 if (max_num_lines < 0)
5816 max_num_lines = get_viminfo_parameter('"');
5817 if (max_num_lines == 0)
5818 return;
5819 max_kbyte = get_viminfo_parameter('s');
5820 if (max_kbyte == 0)
5821 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 for (i = 0; i < NUM_REGISTERS; i++)
5824 {
5825 if (y_regs[i].y_array == NULL)
5826 continue;
5827#ifdef FEAT_CLIPBOARD
5828 /* Skip '*'/'+' register, we don't want them back next time */
5829 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5830 continue;
5831#endif
5832#ifdef FEAT_DND
5833 /* Neither do we want the '~' register */
5834 if (i == TILDE_REGISTER)
5835 continue;
5836#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005837 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005839 if (num_lines == 0
5840 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005841 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005842 continue;
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 if (max_kbyte > 0)
5845 {
5846 /* Skip register if there is more text than the maximum size. */
5847 len = 0;
5848 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005849 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if (len > (long)max_kbyte * 1024L)
5851 continue;
5852 }
5853
5854 switch (y_regs[i].y_type)
5855 {
5856 case MLINE:
5857 type = (char_u *)"LINE";
5858 break;
5859 case MCHAR:
5860 type = (char_u *)"CHAR";
5861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 case MBLOCK:
5863 type = (char_u *)"BLOCK";
5864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 default:
5866 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005867 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 emsg(IObuff);
5869 type = (char_u *)"LINE";
5870 break;
5871 }
5872 if (y_previous == &y_regs[i])
5873 fprintf(fp, "\"");
5874 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005875 fprintf(fp, "\"%c", c);
5876 if (c == execreg_lastc)
5877 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005878 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 /* If max_num_lines < 0, then we save ALL the lines in the register */
5881 if (max_num_lines > 0 && num_lines > max_num_lines)
5882 num_lines = max_num_lines;
5883 for (j = 0; j < num_lines; j++)
5884 {
5885 putc('\t', fp);
5886 viminfo_writestring(fp, y_regs[i].y_array[j]);
5887 }
5888 }
5889}
5890#endif /* FEAT_VIMINFO */
5891
5892#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5893/*
5894 * SELECTION / PRIMARY ('*')
5895 *
5896 * Text selection stuff that uses the GUI selection register '*'. When using a
5897 * GUI this may be text from another window, otherwise it is the last text we
5898 * had highlighted with VIsual mode. With mouse support, clicking the middle
5899 * button performs the paste, otherwise you will need to do <"*p>. "
5900 * If not under X, it is synonymous with the clipboard register '+'.
5901 *
5902 * X CLIPBOARD ('+')
5903 *
5904 * Text selection stuff that uses the GUI clipboard register '+'.
5905 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5906 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5907 * otherwise you will need to do <"+p>. "
5908 * If not under X, it is synonymous with the selection register '*'.
5909 */
5910
5911/*
5912 * Routine to export any final X selection we had to the environment
5913 * so that the text is still available after vim has exited. X selections
5914 * only exist while the owning application exists, so we write to the
5915 * permanent (while X runs) store CUT_BUFFER0.
5916 * Dump the CLIPBOARD selection if we own it (it's logically the more
5917 * 'permanent' of the two), otherwise the PRIMARY one.
5918 * For now, use a hard-coded sanity limit of 1Mb of data.
5919 */
5920#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5921 void
5922x11_export_final_selection()
5923{
5924 Display *dpy;
5925 char_u *str = NULL;
5926 long_u len = 0;
5927 int motion_type = -1;
5928
5929# ifdef FEAT_GUI
5930 if (gui.in_use)
5931 dpy = X_DISPLAY;
5932 else
5933# endif
5934# ifdef FEAT_XCLIPBOARD
5935 dpy = xterm_dpy;
5936# else
5937 return;
5938# endif
5939
5940 /* Get selection to export */
5941 if (clip_plus.owned)
5942 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5943 else if (clip_star.owned)
5944 motion_type = clip_convert_selection(&str, &len, &clip_star);
5945
5946 /* Check it's OK */
5947 if (dpy != NULL && str != NULL && motion_type >= 0
5948 && len < 1024*1024 && len > 0)
5949 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005950#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005951 int ok = TRUE;
5952
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005953 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
5954 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
5955 * encoding conversion usually doesn't work, so keep the text as-is.
5956 */
5957 if (has_mbyte)
5958 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005959 vimconv_T vc;
5960
5961 vc.vc_type = CONV_NONE;
5962 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
5963 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01005964 int intlen = len;
5965 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005966
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005967 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00005968 conv_str = string_convert(&vc, str, &intlen);
5969 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005970 if (conv_str != NULL)
5971 {
5972 vim_free(str);
5973 str = conv_str;
5974 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005975 else
5976 {
5977 ok = FALSE;
5978 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005979 convert_setup(&vc, NULL, NULL);
5980 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005981 else
5982 {
5983 ok = FALSE;
5984 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005985 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005986
5987 /* Do not store the string if conversion failed. Better to use any
5988 * other selection than garbled text. */
5989 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005990#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01005991 {
5992 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5993 XFlush(dpy);
5994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996
5997 vim_free(str);
5998}
5999#endif
6000
6001 void
6002clip_free_selection(cbd)
6003 VimClipboard *cbd;
6004{
6005 struct yankreg *y_ptr = y_current;
6006
6007 if (cbd == &clip_plus)
6008 y_current = &y_regs[PLUS_REGISTER];
6009 else
6010 y_current = &y_regs[STAR_REGISTER];
6011 free_yank_all();
6012 y_current->y_size = 0;
6013 y_current = y_ptr;
6014}
6015
6016/*
6017 * Get the selected text and put it in the gui selection register '*' or '+'.
6018 */
6019 void
6020clip_get_selection(cbd)
6021 VimClipboard *cbd;
6022{
6023 struct yankreg *old_y_previous, *old_y_current;
6024 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 pos_T old_visual;
6026 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 colnr_T old_curswant;
6028 int old_set_curswant;
6029 pos_T old_op_start, old_op_end;
6030 oparg_T oa;
6031 cmdarg_T ca;
6032
6033 if (cbd->owned)
6034 {
6035 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6036 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6037 return;
6038
6039 /* Get the text between clip_star.start & clip_star.end */
6040 old_y_previous = y_previous;
6041 old_y_current = y_current;
6042 old_cursor = curwin->w_cursor;
6043 old_curswant = curwin->w_curswant;
6044 old_set_curswant = curwin->w_set_curswant;
6045 old_op_start = curbuf->b_op_start;
6046 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 old_visual = VIsual;
6048 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 clear_oparg(&oa);
6050 oa.regname = (cbd == &clip_plus ? '+' : '*');
6051 oa.op_type = OP_YANK;
6052 vim_memset(&ca, 0, sizeof(ca));
6053 ca.oap = &oa;
6054 ca.cmdchar = 'y';
6055 ca.count1 = 1;
6056 ca.retval = CA_NO_ADJ_OP_END;
6057 do_pending_operator(&ca, 0, TRUE);
6058 y_previous = old_y_previous;
6059 y_current = old_y_current;
6060 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006061 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 curwin->w_curswant = old_curswant;
6063 curwin->w_set_curswant = old_set_curswant;
6064 curbuf->b_op_start = old_op_start;
6065 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 VIsual = old_visual;
6067 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069 else
6070 {
6071 clip_free_selection(cbd);
6072
6073 /* Try to get selected text from another window */
6074 clip_gen_request_selection(cbd);
6075 }
6076}
6077
Bram Moolenaard44347f2011-06-19 01:14:29 +02006078/*
6079 * Convert from the GUI selection string into the '*'/'+' register.
6080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 void
6082clip_yank_selection(type, str, len, cbd)
6083 int type;
6084 char_u *str;
6085 long len;
6086 VimClipboard *cbd;
6087{
6088 struct yankreg *y_ptr;
6089
6090 if (cbd == &clip_plus)
6091 y_ptr = &y_regs[PLUS_REGISTER];
6092 else
6093 y_ptr = &y_regs[STAR_REGISTER];
6094
6095 clip_free_selection(cbd);
6096
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006097 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098}
6099
6100/*
6101 * Convert the '*'/'+' register into a GUI selection string returned in *str
6102 * with length *len.
6103 * Returns the motion type, or -1 for failure.
6104 */
6105 int
6106clip_convert_selection(str, len, cbd)
6107 char_u **str;
6108 long_u *len;
6109 VimClipboard *cbd;
6110{
6111 char_u *p;
6112 int lnum;
6113 int i, j;
6114 int_u eolsize;
6115 struct yankreg *y_ptr;
6116
6117 if (cbd == &clip_plus)
6118 y_ptr = &y_regs[PLUS_REGISTER];
6119 else
6120 y_ptr = &y_regs[STAR_REGISTER];
6121
6122#ifdef USE_CRNL
6123 eolsize = 2;
6124#else
6125 eolsize = 1;
6126#endif
6127
6128 *str = NULL;
6129 *len = 0;
6130 if (y_ptr->y_array == NULL)
6131 return -1;
6132
6133 for (i = 0; i < y_ptr->y_size; i++)
6134 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6135
6136 /*
6137 * Don't want newline character at end of last line if we're in MCHAR mode.
6138 */
6139 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6140 *len -= eolsize;
6141
6142 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6143 if (p == NULL)
6144 return -1;
6145 lnum = 0;
6146 for (i = 0, j = 0; i < (int)*len; i++, j++)
6147 {
6148 if (y_ptr->y_array[lnum][j] == '\n')
6149 p[i] = NUL;
6150 else if (y_ptr->y_array[lnum][j] == NUL)
6151 {
6152#ifdef USE_CRNL
6153 p[i++] = '\r';
6154#endif
6155#ifdef USE_CR
6156 p[i] = '\r';
6157#else
6158 p[i] = '\n';
6159#endif
6160 lnum++;
6161 j = -1;
6162 }
6163 else
6164 p[i] = y_ptr->y_array[lnum][j];
6165 }
6166 return y_ptr->y_type;
6167}
6168
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170/*
6171 * If we have written to a clipboard register, send the text to the clipboard.
6172 */
6173 static void
6174may_set_selection()
6175{
6176 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6177 {
6178 clip_own_selection(&clip_star);
6179 clip_gen_set_selection(&clip_star);
6180 }
6181 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6182 {
6183 clip_own_selection(&clip_plus);
6184 clip_gen_set_selection(&clip_plus);
6185 }
6186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188#endif /* FEAT_CLIPBOARD || PROTO */
6189
6190
6191#if defined(FEAT_DND) || defined(PROTO)
6192/*
6193 * Replace the contents of the '~' register with str.
6194 */
6195 void
6196dnd_yank_drag_data(str, len)
6197 char_u *str;
6198 long len;
6199{
6200 struct yankreg *curr;
6201
6202 curr = y_current;
6203 y_current = &y_regs[TILDE_REGISTER];
6204 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006205 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 y_current = curr;
6207}
6208#endif
6209
6210
6211#if defined(FEAT_EVAL) || defined(PROTO)
6212/*
6213 * Return the type of a register.
6214 * Used for getregtype()
6215 * Returns MAUTO for error.
6216 */
6217 char_u
6218get_reg_type(regname, reglen)
6219 int regname;
6220 long *reglen;
6221{
6222 switch (regname)
6223 {
6224 case '%': /* file name */
6225 case '#': /* alternate file name */
6226 case '=': /* expression */
6227 case ':': /* last command line */
6228 case '/': /* last search-pattern */
6229 case '.': /* last inserted text */
6230#ifdef FEAT_SEARCHPATH
6231 case Ctrl_F: /* Filename under cursor */
6232 case Ctrl_P: /* Path under cursor, expand via "path" */
6233#endif
6234 case Ctrl_W: /* word under cursor */
6235 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6236 case '_': /* black hole: always empty */
6237 return MCHAR;
6238 }
6239
6240#ifdef FEAT_CLIPBOARD
6241 regname = may_get_selection(regname);
6242#endif
6243
Bram Moolenaar32b92012014-01-14 12:33:36 +01006244 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6245 return MAUTO;
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 get_yank_register(regname, FALSE);
6248
6249 if (y_current->y_array != NULL)
6250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (reglen != NULL && y_current->y_type == MBLOCK)
6252 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 return y_current->y_type;
6254 }
6255 return MAUTO;
6256}
6257
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006258static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags));
6259
6260/*
6261 * When "flags" has GREG_LIST return a list with text "s".
6262 * Otherwise just return "s".
6263 */
6264 static char_u *
6265getreg_wrap_one_line(s, flags)
6266 char_u *s;
6267 int flags;
6268{
6269 if (flags & GREG_LIST)
6270 {
6271 list_T *list = list_alloc();
6272
6273 if (list != NULL)
6274 {
6275 if (list_append_string(list, NULL, -1) == FAIL)
6276 {
6277 list_free(list, TRUE);
6278 return NULL;
6279 }
6280 list->lv_first->li_tv.vval.v_string = s;
6281 }
6282 return (char_u *)list;
6283 }
6284 return s;
6285}
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287/*
6288 * Return the contents of a register as a single allocated string.
6289 * Used for "@r" in expressions and for getreg().
6290 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006291 * Flags:
6292 * GREG_NO_EXPR Do not allow expression register
6293 * GREG_EXPR_SRC For the expression register: return expression itself,
6294 * not the result of its evaluation.
6295 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 */
6297 char_u *
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006298get_reg_contents(regname, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 int regname;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006300 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 long i;
6303 char_u *retval;
6304 int allocated;
6305 long len;
6306
6307 /* Don't allow using an expression register inside an expression */
6308 if (regname == '=')
6309 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006310 if (flags & GREG_NO_EXPR)
6311 return NULL;
6312 if (flags & GREG_EXPR_SRC)
6313 return getreg_wrap_one_line(get_expr_line_src(), flags);
6314 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316
6317 if (regname == '@') /* "@@" is used for unnamed register */
6318 regname = '"';
6319
6320 /* check for valid regname */
6321 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6322 return NULL;
6323
6324#ifdef FEAT_CLIPBOARD
6325 regname = may_get_selection(regname);
6326#endif
6327
6328 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6329 {
6330 if (retval == NULL)
6331 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006332 if (allocated)
6333 return getreg_wrap_one_line(retval, flags);
6334 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336
6337 get_yank_register(regname, FALSE);
6338 if (y_current->y_array == NULL)
6339 return NULL;
6340
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006341 if (flags & GREG_LIST)
6342 {
6343 list_T *list = list_alloc();
6344 int error = FALSE;
6345
6346 if (list == NULL)
6347 return NULL;
6348 for (i = 0; i < y_current->y_size; ++i)
6349 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6350 error = TRUE;
6351 if (error)
6352 {
6353 list_free(list, TRUE);
6354 return NULL;
6355 }
6356 return (char_u *)list;
6357 }
6358
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 /*
6360 * Compute length of resulting string.
6361 */
6362 len = 0;
6363 for (i = 0; i < y_current->y_size; ++i)
6364 {
6365 len += (long)STRLEN(y_current->y_array[i]);
6366 /*
6367 * Insert a newline between lines and after last line if
6368 * y_type is MLINE.
6369 */
6370 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6371 ++len;
6372 }
6373
6374 retval = lalloc(len + 1, TRUE);
6375
6376 /*
6377 * Copy the lines of the yank register into the string.
6378 */
6379 if (retval != NULL)
6380 {
6381 len = 0;
6382 for (i = 0; i < y_current->y_size; ++i)
6383 {
6384 STRCPY(retval + len, y_current->y_array[i]);
6385 len += (long)STRLEN(retval + len);
6386
6387 /*
6388 * Insert a NL between lines and after the last line if y_type is
6389 * MLINE.
6390 */
6391 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6392 retval[len++] = '\n';
6393 }
6394 retval[len] = NUL;
6395 }
6396
6397 return retval;
6398}
6399
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006400 static int
6401init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type)
6402 int name;
6403 struct yankreg **old_y_previous;
6404 struct yankreg **old_y_current;
6405 int must_append;
6406 int *yank_type UNUSED;
6407{
6408 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6409 {
6410 emsg_invreg(name);
6411 return FAIL;
6412 }
6413
6414 /* Don't want to change the current (unnamed) register */
6415 *old_y_previous = y_previous;
6416 *old_y_current = y_current;
6417
6418 get_yank_register(name, TRUE);
6419 if (!y_append && !must_append)
6420 free_yank_all();
6421 return OK;
6422}
6423
6424 static void
6425finish_write_reg(name, old_y_previous, old_y_current)
6426 int name;
6427 struct yankreg *old_y_previous;
6428 struct yankreg *old_y_current;
6429{
6430# ifdef FEAT_CLIPBOARD
6431 /* Send text of clipboard register to the clipboard. */
6432 may_set_selection();
6433# endif
6434
6435 /* ':let @" = "val"' should change the meaning of the "" register */
6436 if (name != '"')
6437 y_previous = old_y_previous;
6438 y_current = old_y_current;
6439}
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441/*
6442 * Store string "str" in register "name".
6443 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6444 * If "must_append" is TRUE, always append to the register. Otherwise append
6445 * if "name" is an uppercase letter.
6446 * Note: "maxlen" and "must_append" don't work for the "/" register.
6447 * Careful: 'str' is modified, you may have to use a copy!
6448 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6449 */
6450 void
6451write_reg_contents(name, str, maxlen, must_append)
6452 int name;
6453 char_u *str;
6454 int maxlen;
6455 int must_append;
6456{
6457 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6458}
6459
6460 void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006461write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len)
6462 int name;
6463 char_u **strings;
6464 int maxlen UNUSED;
6465 int must_append;
6466 int yank_type;
6467 long block_len;
6468{
6469 struct yankreg *old_y_previous, *old_y_current;
6470
6471 if (name == '/'
6472#ifdef FEAT_EVAL
6473 || name == '='
6474#endif
6475 )
6476 {
6477 char_u *s;
6478
6479 if (strings[0] == NULL)
6480 s = (char_u *)"";
6481 else if (strings[1] != NULL)
6482 {
6483 EMSG(_("E883: search pattern and expression register may not "
6484 "contain two or more lines"));
6485 return;
6486 }
6487 else
6488 s = strings[0];
6489 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6490 return;
6491 }
6492
6493 if (name == '_') /* black hole: nothing to do */
6494 return;
6495
6496 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6497 &yank_type) == FAIL)
6498 return;
6499
6500 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6501
6502 finish_write_reg(name, old_y_previous, old_y_current);
6503}
6504
6505 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
6507 int name;
6508 char_u *str;
6509 int maxlen;
6510 int must_append;
6511 int yank_type;
6512 long block_len;
6513{
6514 struct yankreg *old_y_previous, *old_y_current;
6515 long len;
6516
Bram Moolenaare7566042005-06-17 22:00:15 +00006517 if (maxlen >= 0)
6518 len = maxlen;
6519 else
6520 len = (long)STRLEN(str);
6521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 /* Special case: '/' search pattern */
6523 if (name == '/')
6524 {
6525 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6526 return;
6527 }
6528
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006529 if (name == '#')
6530 {
6531 buf_T *buf;
6532
6533 if (VIM_ISDIGIT(*str))
6534 {
6535 int num = atoi((char *)str);
6536
6537 buf = buflist_findnr(num);
6538 if (buf == NULL)
6539 EMSGN(_(e_nobufnr), (long)num);
6540 }
6541 else
6542 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6543 TRUE, FALSE, FALSE));
6544 if (buf == NULL)
6545 return;
6546 curwin->w_alt_fnum = buf->b_fnum;
6547 return;
6548 }
6549
Bram Moolenaare7566042005-06-17 22:00:15 +00006550#ifdef FEAT_EVAL
6551 if (name == '=')
6552 {
6553 char_u *p, *s;
6554
6555 p = vim_strnsave(str, (int)len);
6556 if (p == NULL)
6557 return;
6558 if (must_append)
6559 {
6560 s = concat_str(get_expr_line_src(), p);
6561 vim_free(p);
6562 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006563 }
6564 set_expr_line(p);
6565 return;
6566 }
6567#endif
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (name == '_') /* black hole: nothing to do */
6570 return;
6571
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006572 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6573 &yank_type) == FAIL)
6574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006576 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006578 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579}
6580#endif /* FEAT_EVAL */
6581
6582#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6583/*
6584 * Put a string into a register. When the register is not empty, the string
6585 * is appended.
6586 */
6587 static void
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006588str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 struct yankreg *y_ptr; /* pointer to yank register */
Bram Moolenaard44347f2011-06-19 01:14:29 +02006590 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 char_u *str; /* string to put in register */
6592 long len; /* length of string */
6593 long blocklen; /* width of Visual block */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006594 int str_list; /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006596 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 int lnum;
6598 long start;
6599 long i;
6600 int extra;
6601 int newlines; /* number of lines added */
6602 int extraline = 0; /* extra line at the end */
6603 int append = FALSE; /* append to last line in register */
6604 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006605 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006609 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 y_ptr->y_size = 0;
6611
Bram Moolenaard44347f2011-06-19 01:14:29 +02006612 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006613 type = ((str_list || (len > 0 && (str[len - 1] == NL
6614 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006615 ? MLINE : MCHAR);
6616 else
6617 type = yank_type;
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 /*
6620 * Count the number of lines within the string
6621 */
6622 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006623 if (str_list)
6624 {
6625 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006630 for (i = 0; i < len; i++)
6631 if (str[i] == '\n')
6632 ++newlines;
6633 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6634 {
6635 extraline = 1;
6636 ++newlines; /* count extra newline at the end */
6637 }
6638 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6639 {
6640 append = TRUE;
6641 --newlines; /* uncount newline when appending first line */
6642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006645 /* Without any lines make the register empty. */
6646 if (y_ptr->y_size + newlines == 0)
6647 {
6648 vim_free(y_ptr->y_array);
6649 y_ptr->y_array = NULL;
6650 return;
6651 }
6652
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 /*
6654 * Allocate an array to hold the pointers to the new register lines.
6655 * If the register was not empty, move the existing lines to the new array.
6656 */
6657 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6658 * sizeof(char_u *), TRUE);
6659 if (pp == NULL) /* out of memory */
6660 return;
6661 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6662 pp[lnum] = y_ptr->y_array[lnum];
6663 vim_free(y_ptr->y_array);
6664 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 /*
6668 * Find the end of each line and save it into the array.
6669 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006670 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006672 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6673 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006674 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006675 pp[lnum] = vim_strnsave(*ss, i);
6676 if (i > maxlen)
6677 maxlen = i;
6678 }
6679 }
6680 else
6681 {
6682 for (start = 0; start < len + extraline; start += i + 1)
6683 {
6684 for (i = start; i < len; ++i) /* find the end of the line */
6685 if (str[i] == '\n')
6686 break;
6687 i -= start; /* i is now length of line */
6688 if (i > maxlen)
6689 maxlen = i;
6690 if (append)
6691 {
6692 --lnum;
6693 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6694 }
6695 else
6696 extra = 0;
6697 s = alloc((unsigned)(i + extra + 1));
6698 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006700 if (extra)
6701 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6702 if (append)
6703 vim_free(y_ptr->y_array[lnum]);
6704 if (i)
6705 mch_memmove(s + extra, str + start, (size_t)i);
6706 extra += i;
6707 s[extra] = NUL;
6708 y_ptr->y_array[lnum++] = s;
6709 while (--extra >= 0)
6710 {
6711 if (*s == NUL)
6712 *s = '\n'; /* replace NUL with newline */
6713 ++s;
6714 }
6715 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 }
6718 y_ptr->y_type = type;
6719 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (type == MBLOCK)
6721 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6722 else
6723 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724}
6725#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6726
6727 void
6728clear_oparg(oap)
6729 oparg_T *oap;
6730{
6731 vim_memset(oap, 0, sizeof(oparg_T));
6732}
6733
Bram Moolenaar7c626922005-02-07 22:01:03 +00006734static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006737 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 *
6739 * "Words" are counted by looking for boundaries between non-space and
6740 * space characters. (it seems to produce results that match 'wc'.)
6741 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006742 * Return value is byte count; word count for the line is added to "*wc".
6743 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 *
6745 * The function will only examine the first "limit" characters in the
6746 * line, stopping if it encounters an end-of-line (NUL byte). In that
6747 * case, eol_size will be added to the character count to account for
6748 * the size of the EOL character.
6749 */
6750 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006751line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 char_u *line;
6753 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006754 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 long limit;
6756 int eol_size;
6757{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006758 long i;
6759 long words = 0;
6760 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 int is_word = 0;
6762
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006763 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 if (is_word)
6766 {
6767 if (vim_isspace(line[i]))
6768 {
6769 words++;
6770 is_word = 0;
6771 }
6772 }
6773 else if (!vim_isspace(line[i]))
6774 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006775 ++chars;
6776#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006777 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006778#else
6779 ++i;
6780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782
6783 if (is_word)
6784 words++;
6785 *wc += words;
6786
6787 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006788 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006791 chars += eol_size;
6792 }
6793 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 return i;
6795}
6796
6797/*
6798 * Give some info about the position of the cursor (for "g CTRL-G").
6799 * In Visual mode, give some info about the selected region. (In this case,
6800 * the *_count_cursor variables store running totals for the selection.)
6801 */
6802 void
6803cursor_pos_info()
6804{
6805 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006806 char_u buf1[50];
6807 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006809 long byte_count = 0;
6810 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 long char_count = 0;
6812 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 long word_count = 0;
6814 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006815 int eol_size;
6816 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 long line_count_selected = 0;
6818 pos_T min_pos, max_pos;
6819 oparg_T oparg;
6820 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 /*
6823 * Compute the length of the file in characters.
6824 */
6825 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6826 {
6827 MSG(_(no_lines_msg));
6828 }
6829 else
6830 {
6831 if (get_fileformat(curbuf) == EOL_DOS)
6832 eol_size = 2;
6833 else
6834 eol_size = 1;
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (VIsual_active)
6837 {
6838 if (lt(VIsual, curwin->w_cursor))
6839 {
6840 min_pos = VIsual;
6841 max_pos = curwin->w_cursor;
6842 }
6843 else
6844 {
6845 min_pos = curwin->w_cursor;
6846 max_pos = VIsual;
6847 }
6848 if (*p_sel == 'e' && max_pos.col > 0)
6849 --max_pos.col;
6850
6851 if (VIsual_mode == Ctrl_V)
6852 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006853#ifdef FEAT_LINEBREAK
6854 char_u * saved_sbr = p_sbr;
6855
6856 /* Make 'sbr' empty for a moment to get the correct size. */
6857 p_sbr = empty_option;
6858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 oparg.is_VIsual = 1;
6860 oparg.block_mode = TRUE;
6861 oparg.op_type = OP_NOP;
6862 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006863 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006864#ifdef FEAT_LINEBREAK
6865 p_sbr = saved_sbr;
6866#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006867 if (curwin->w_curswant == MAXCOL)
6868 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 /* Swap the start, end vcol if needed */
6870 if (oparg.end_vcol < oparg.start_vcol)
6871 {
6872 oparg.end_vcol += oparg.start_vcol;
6873 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6874 oparg.end_vcol -= oparg.start_vcol;
6875 }
6876 }
6877 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6881 {
6882 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006883 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
6885 ui_breakcheck();
6886 if (got_int)
6887 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006888 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 /* Do extra processing for VIsual mode. */
6892 if (VIsual_active
6893 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6894 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006895 char_u *s = NULL;
6896 long len = 0L;
6897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 switch (VIsual_mode)
6899 {
6900 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006901#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006905#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006907#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006908 s = bd.textstart;
6909 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 break;
6911 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006912 s = ml_get(lnum);
6913 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 break;
6915 case 'v':
6916 {
6917 colnr_T start_col = (lnum == min_pos.lnum)
6918 ? min_pos.col : 0;
6919 colnr_T end_col = (lnum == max_pos.lnum)
6920 ? max_pos.col - start_col + 1 : MAXCOL;
6921
Bram Moolenaardef9e822004-12-31 20:58:58 +00006922 s = ml_get(lnum) + start_col;
6923 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925 break;
6926 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006927 if (s != NULL)
6928 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006929 byte_count_cursor += line_count_info(s, &word_count_cursor,
6930 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006931 if (lnum == curbuf->b_ml.ml_line_count
6932 && !curbuf->b_p_eol
6933 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006934 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006935 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940 /* In non-visual mode, check for the line the cursor is on */
6941 if (lnum == curwin->w_cursor.lnum)
6942 {
6943 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006944 char_count_cursor += char_count;
6945 byte_count_cursor = byte_count +
6946 line_count_info(ml_get(lnum),
6947 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 (long)(curwin->w_cursor.col + 1), eol_size);
6949 }
6950 }
6951 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006952 byte_count += line_count_info(ml_get(lnum), &word_count,
6953 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955
6956 /* Correction for when last line doesn't have an EOL. */
6957 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006958 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (VIsual_active)
6961 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006962 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
6964 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006965 &max_pos.col);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006966 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6968 }
6969 else
6970 buf1[0] = NUL;
6971
Bram Moolenaar7c626922005-02-07 22:01:03 +00006972 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006973 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006974 vim_snprintf((char *)IObuff, IOSIZE,
6975 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 buf1, line_count_selected,
6977 (long)curbuf->b_ml.ml_line_count,
6978 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006979 byte_count_cursor, byte_count);
6980 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006981 vim_snprintf((char *)IObuff, IOSIZE,
6982 _("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 +00006983 buf1, line_count_selected,
6984 (long)curbuf->b_ml.ml_line_count,
6985 word_count_cursor, word_count,
6986 char_count_cursor, char_count,
6987 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 }
6989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 p = ml_get_curline();
6992 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006993 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 (int)curwin->w_virtcol + 1);
Bram Moolenaar597a4222014-06-25 14:39:50 +02006995 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
6996 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar7c626922005-02-07 22:01:03 +00006998 if (char_count_cursor == byte_count_cursor
6999 && char_count == byte_count)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007000 vim_snprintf((char *)IObuff, IOSIZE,
7001 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 (char *)buf1, (char *)buf2,
7003 (long)curwin->w_cursor.lnum,
7004 (long)curbuf->b_ml.ml_line_count,
7005 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007006 byte_count_cursor, byte_count);
7007 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00007008 vim_snprintf((char *)IObuff, IOSIZE,
7009 _("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 +00007010 (char *)buf1, (char *)buf2,
7011 (long)curwin->w_cursor.lnum,
7012 (long)curbuf->b_ml.ml_line_count,
7013 word_count_cursor, word_count,
7014 char_count_cursor, char_count,
7015 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
7017
7018#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00007019 byte_count = bomb_size();
7020 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00007022 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023#endif
7024 /* Don't shorten this message, the user asked for it. */
7025 p = p_shm;
7026 p_shm = (char_u *)"";
7027 msg(IObuff);
7028 p_shm = p;
7029 }
7030}