blob: 4ad982f7f64315ea966739bebdfe8d8daa809861 [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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010092static void shift_block(oparg_T *oap, int amount);
93static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static int stuff_yank(int, char_u *);
96static void put_reedit_in_typebuf(int silent);
97static int put_in_typebuf(char_u *s, int esc, int colon,
98 int silent);
99static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static void free_yank(long);
104static void free_yank_all(void);
105static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#ifdef FEAT_CLIPBOARD
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100107static void copy_yank_reg(struct yankreg *reg);
108static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaar81340392012-06-06 16:12:59 +0200111#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static char_u *skip_comment(char_u *line, int process, int include_space, int *is_comment);
Bram Moolenaar81340392012-06-06 16:12:59 +0200113#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100114static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
115static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static void str_to_reg(struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static int same_leader(linenr_T lnum, int, char_u *, int, char_u *);
122static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
127/*
128 * The names of operators.
129 * IMPORTANT: Index must correspond with defines in vim.h!!!
130 * The third field indicates whether the operator always works on lines.
131 */
132static char opchars[][3] =
133{
134 {NUL, NUL, FALSE}, /* OP_NOP */
135 {'d', NUL, FALSE}, /* OP_DELETE */
136 {'y', NUL, FALSE}, /* OP_YANK */
137 {'c', NUL, FALSE}, /* OP_CHANGE */
138 {'<', NUL, TRUE}, /* OP_LSHIFT */
139 {'>', NUL, TRUE}, /* OP_RSHIFT */
140 {'!', NUL, TRUE}, /* OP_FILTER */
141 {'g', '~', FALSE}, /* OP_TILDE */
142 {'=', NUL, TRUE}, /* OP_INDENT */
143 {'g', 'q', TRUE}, /* OP_FORMAT */
144 {':', NUL, TRUE}, /* OP_COLON */
145 {'g', 'U', FALSE}, /* OP_UPPER */
146 {'g', 'u', FALSE}, /* OP_LOWER */
147 {'J', NUL, TRUE}, /* DO_JOIN */
148 {'g', 'J', TRUE}, /* DO_JOIN_NS */
149 {'g', '?', FALSE}, /* OP_ROT13 */
150 {'r', NUL, FALSE}, /* OP_REPLACE */
151 {'I', NUL, FALSE}, /* OP_INSERT */
152 {'A', NUL, FALSE}, /* OP_APPEND */
153 {'z', 'f', TRUE}, /* OP_FOLD */
154 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
155 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
156 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
157 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
158 {'z', 'd', TRUE}, /* OP_FOLDDEL */
159 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
160 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000161 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaard79e5502016-01-10 22:13:02 +0100162 {Ctrl_A, NUL, FALSE}, /* OP_NR_ADD */
163 {Ctrl_X, NUL, FALSE}, /* OP_NR_SUB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164};
165
166/*
167 * Translate a command name into an operator type.
168 * Must only be called with a valid operator name!
169 */
170 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100171get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
173 int i;
174
175 if (char1 == 'r') /* ignore second character */
176 return OP_REPLACE;
177 if (char1 == '~') /* when tilde is an operator */
178 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100179 if (char1 == 'g' && char2 == Ctrl_A) /* add */
180 return OP_NR_ADD;
181 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
182 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 for (i = 0; ; ++i)
184 if (opchars[i][0] == char1 && opchars[i][1] == char2)
185 break;
186 return i;
187}
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189/*
190 * Return TRUE if operator "op" always works on whole lines.
191 */
192 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100193op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194{
195 return opchars[op][2];
196}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
198/*
199 * Get first operator command character.
200 * Returns 'g' or 'z' if there is another command character.
201 */
202 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100203get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
205 return opchars[optype][0];
206}
207
208/*
209 * Get second operator command character.
210 */
211 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100212get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 return opchars[optype][1];
215}
216
217/*
218 * op_shift - handle a shift operation
219 */
220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100221op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 long i;
224 int first_char;
225 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228 if (u_save((linenr_T)(oap->start.lnum - 1),
229 (linenr_T)(oap->end.lnum + 1)) == FAIL)
230 return;
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 if (oap->block_mode)
233 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
235 for (i = oap->line_count; --i >= 0; )
236 {
237 first_char = *ml_get_curline();
238 if (first_char == NUL) /* empty line */
239 curwin->w_cursor.col = 0;
240#ifdef FEAT_VISUALEXTRA
241 else if (oap->block_mode)
242 shift_block(oap, amount);
243#endif
244 else
245 /* Move the line right if it doesn't start with '#', 'smartindent'
246 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
247#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
248 if (first_char != '#' || !preprocs_left())
249#endif
250 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000251 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
253 ++curwin->w_cursor.lnum;
254 }
255
256 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200257#ifdef FEAT_FOLDING
258 /* The cursor line is not in a closed fold */
259 foldOpenCursor();
260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (oap->block_mode)
263 {
264 curwin->w_cursor.lnum = oap->start.lnum;
265 curwin->w_cursor.col = block_col;
266 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100267 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 {
269 curwin->w_cursor.lnum = oap->start.lnum;
270 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
271 }
272 else
273 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
274
275 if (oap->line_count > p_report)
276 {
277 if (oap->op_type == OP_RSHIFT)
278 s = (char_u *)">";
279 else
280 s = (char_u *)"<";
281 if (oap->line_count == 1)
282 {
283 if (amount == 1)
284 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
285 else
286 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
287 }
288 else
289 {
290 if (amount == 1)
291 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
292 oap->line_count, s);
293 else
294 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
295 oap->line_count, s, amount);
296 }
297 msg(IObuff);
298 }
299
300 /*
301 * Set "'[" and "']" marks.
302 */
303 curbuf->b_op_start = oap->start;
304 curbuf->b_op_end.lnum = oap->end.lnum;
305 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
306 if (curbuf->b_op_end.col > 0)
307 --curbuf->b_op_end.col;
308}
309
310/*
311 * shift the current line one shiftwidth left (if left != 0) or right
312 * leaves cursor on first blank in the line
313 */
314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100315shift_line(
316 int left,
317 int round,
318 int amount,
319 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 int count;
322 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100323 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
325 count = get_indent(); /* get current indent */
326
327 if (round) /* round off indent */
328 {
329 i = count / p_sw; /* number of p_sw rounded down */
330 j = count % p_sw; /* extra spaces */
331 if (j && left) /* first remove extra spaces */
332 --amount;
333 if (left)
334 {
335 i -= amount;
336 if (i < 0)
337 i = 0;
338 }
339 else
340 i += amount;
341 count = i * p_sw;
342 }
343 else /* original vi indent */
344 {
345 if (left)
346 {
347 count -= p_sw * amount;
348 if (count < 0)
349 count = 0;
350 }
351 else
352 count += p_sw * amount;
353 }
354
355 /* Set new indent */
356#ifdef FEAT_VREPLACE
357 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000358 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 else
360#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000361 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362}
363
364#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
365/*
366 * Shift one line of the current block one shiftwidth right or left.
367 * Leaves cursor on first character in block.
368 */
369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100370shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
372 int left = (oap->op_type == OP_LSHIFT);
373 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000374 int total;
375 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100377 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 int p_ts = (int)curbuf->b_p_ts;
379 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000381 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 int i = 0, j = 0;
383 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef FEAT_RIGHTLEFT
385 int old_p_ri = p_ri;
386
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200387 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#endif
389
390 State = INSERT; /* don't want REPLACE for State */
391 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
392 if (bd.is_short)
393 return;
394
395 /* total is number of screen columns to be inserted/removed */
396 total = amount * p_sw;
397 oldp = ml_get_curline();
398
399 if (!left)
400 {
401 /*
402 * 1. Get start vcol
403 * 2. Total ws vcols
404 * 3. Divvy into TABs & spp
405 * 4. Construct new string
406 */
407 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
408 ws_vcol = bd.start_vcol - bd.pre_whitesp;
409 if (bd.startspaces)
410 {
411#ifdef FEAT_MBYTE
412 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000413 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000416 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 }
418 for ( ; vim_iswhite(*bd.textstart); )
419 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200420 /* TODO: is passing bd.textstart for start of the line OK? */
421 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
422 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 total += incr;
424 bd.start_vcol += incr;
425 }
426 /* OK, now total=all the VWS reqd, and textstart points at the 1st
427 * non-ws char in the block. */
428 if (!curbuf->b_p_et)
429 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
430 if (i)
431 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
432 else
433 j = total;
434 /* if we're splitting a TAB, allow for it */
435 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
436 len = (int)STRLEN(bd.textstart) + 1;
437 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
438 if (newp == NULL)
439 return;
440 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
441 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200442 vim_memset(newp + bd.textcol, TAB, (size_t)i);
443 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 /* the end */
445 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
446 }
447 else /* left */
448 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000449 colnr_T destination_col; /* column to which text in block will
450 be shifted */
451 char_u *verbatim_copy_end; /* end of the part of the line which is
452 copied verbatim */
453 colnr_T verbatim_copy_width;/* the (displayed) width of this part
454 of line */
455 unsigned fill; /* nr of spaces that replace a TAB */
456 unsigned new_line_len; /* the length of the line after the
457 block shift */
458 size_t block_space_width;
459 size_t shift_amount;
460 char_u *non_white = bd.textstart;
461 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000463 /*
464 * Firstly, let's find the first non-whitespace character that is
465 * displayed after the block's start column and the character's column
466 * number. Also, let's calculate the width of all the whitespace
467 * characters that are displayed in the block and precede the searched
468 * non-whitespace character.
469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000471 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
472 * the part of which is displayed at the block's beginning. Let's start
473 * searching from the next character. */
474 if (bd.startspaces)
475 mb_ptr_adv(non_white);
476
477 /* The character's column is in "bd.start_vcol". */
478 non_white_col = bd.start_vcol;
479
480 while (vim_iswhite(*non_white))
481 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200482 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000483 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 }
485
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000486 block_space_width = non_white_col - oap->start_vcol;
487 /* We will shift by "total" or "block_space_width", whichever is less.
488 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000489 shift_amount = (block_space_width < (size_t)total
490 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000491
492 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000493 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494
495 /* Now let's find out how much of the beginning of the line we can
496 * reuse without modification. */
497 verbatim_copy_end = bd.textstart;
498 verbatim_copy_width = bd.start_vcol;
499
500 /* If "bd.startspaces" is set, "bd.textstart" points to the character
501 * preceding the block. We have to subtract its width to obtain its
502 * column number. */
503 if (bd.startspaces)
504 verbatim_copy_width -= bd.start_char_vcols;
505 while (verbatim_copy_width < destination_col)
506 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200507 char_u *line = verbatim_copy_end;
508
509 /* TODO: is passing verbatim_copy_end for start of the line OK? */
510 incr = lbr_chartabsize(line, verbatim_copy_end,
511 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000512 if (verbatim_copy_width + incr > destination_col)
513 break;
514 verbatim_copy_width += incr;
515 mb_ptr_adv(verbatim_copy_end);
516 }
517
518 /* If "destination_col" is different from the width of the initial
519 * part of the line that will be copied, it means we encountered a tab
520 * character, which we will have to partly replace with spaces. */
521 fill = destination_col - verbatim_copy_width;
522
523 /* The replacement line will consist of:
524 * - the beginning of the original line up to "verbatim_copy_end",
525 * - "fill" number of spaces,
526 * - the rest of the line, pointed to by non_white. */
527 new_line_len = (unsigned)(verbatim_copy_end - oldp)
528 + fill
529 + (unsigned)STRLEN(non_white) + 1;
530
531 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (newp == NULL)
533 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000534 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200535 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000536 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538 /* replace the line */
539 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
540 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
541 State = oldstate;
542 curwin->w_cursor.col = oldcol;
543#ifdef FEAT_RIGHTLEFT
544 p_ri = old_p_ri;
545#endif
546}
547#endif
548
549#ifdef FEAT_VISUALEXTRA
550/*
551 * Insert string "s" (b_insert ? before : after) block :AKelly
552 * Caller must prepare for undo.
553 */
554 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100555block_insert(
556 oparg_T *oap,
557 char_u *s,
558 int b_insert,
559 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 int p_ts;
562 int count = 0; /* extra spaces to replace a cut TAB */
563 int spaces = 0; /* non-zero if cutting a TAB */
564 colnr_T offset; /* pointer along new line */
565 unsigned s_len; /* STRLEN(s) */
566 char_u *newp, *oldp; /* new, old lines */
567 linenr_T lnum; /* loop var */
568 int oldstate = State;
569
570 State = INSERT; /* don't want REPLACE for State */
571 s_len = (unsigned)STRLEN(s);
572
573 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
574 {
575 block_prep(oap, bdp, lnum, TRUE);
576 if (bdp->is_short && b_insert)
577 continue; /* OP_INSERT, line ends before block start */
578
579 oldp = ml_get(lnum);
580
581 if (b_insert)
582 {
583 p_ts = bdp->start_char_vcols;
584 spaces = bdp->startspaces;
585 if (spaces != 0)
586 count = p_ts - 1; /* we're cutting a TAB */
587 offset = bdp->textcol;
588 }
589 else /* append */
590 {
591 p_ts = bdp->end_char_vcols;
592 if (!bdp->is_short) /* spaces = padding after block */
593 {
594 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
595 if (spaces != 0)
596 count = p_ts - 1; /* we're cutting a TAB */
597 offset = bdp->textcol + bdp->textlen - (spaces != 0);
598 }
599 else /* spaces = padding to block edge */
600 {
601 /* if $ used, just append to EOL (ie spaces==0) */
602 if (!bdp->is_MAX)
603 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
604 count = spaces;
605 offset = bdp->textcol + bdp->textlen;
606 }
607 }
608
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200609#ifdef FEAT_MBYTE
610 if (has_mbyte && spaces > 0)
611 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100612 int off;
613
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200614 /* Avoid starting halfway a multi-byte character. */
615 if (b_insert)
616 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100617 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200618 }
619 else
620 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100621 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200622 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200623 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100624 spaces -= off;
625 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200626 }
627#endif
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
630 if (newp == NULL)
631 continue;
632
633 /* copy up to shifted part */
634 mch_memmove(newp, oldp, (size_t)(offset));
635 oldp += offset;
636
637 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200638 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640 /* copy the new text */
641 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
642 offset += s_len;
643
644 if (spaces && !bdp->is_short)
645 {
646 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200647 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 /* We're splitting a TAB, don't copy it. */
649 oldp++;
650 /* We allowed for that TAB, remember this now */
651 count++;
652 }
653
654 if (spaces > 0)
655 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000656 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
658 ml_replace(lnum, newp, FALSE);
659
660 if (lnum == oap->end.lnum)
661 {
662 /* Set "']" mark to the end of the block instead of the end of
663 * the insert in the first line. */
664 curbuf->b_op_end.lnum = oap->end.lnum;
665 curbuf->b_op_end.col = offset;
666 }
667 } /* for all lnum */
668
669 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
670
671 State = oldstate;
672}
673#endif
674
675#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
676/*
677 * op_reindent - handle reindenting a block of lines.
678 */
679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 long i;
683 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200684 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 linenr_T first_changed = 0;
686 linenr_T last_changed = 0;
687 linenr_T start_lnum = curwin->w_cursor.lnum;
688
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000689 /* Don't even try when 'modifiable' is off. */
690 if (!curbuf->b_p_ma)
691 {
692 EMSG(_(e_modifiable));
693 return;
694 }
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 for (i = oap->line_count; --i >= 0 && !got_int; )
697 {
698 /* it's a slow thing to do, so give feedback so there's no worry that
699 * the computer's just hung. */
700
701 if (i > 1
702 && (i % 50 == 0 || i == oap->line_count - 1)
703 && oap->line_count > p_report)
704 smsg((char_u *)_("%ld lines to indent... "), i);
705
706 /*
707 * Be vi-compatible: For lisp indenting the first line is not
708 * indented, unless there is only one line.
709 */
710#ifdef FEAT_LISP
711 if (i != oap->line_count - 1 || oap->line_count == 1
712 || how != get_lisp_indent)
713#endif
714 {
715 l = skipwhite(ml_get_curline());
716 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200717 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200719 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200721 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 /* did change the indent, call changed_lines() later */
724 if (first_changed == 0)
725 first_changed = curwin->w_cursor.lnum;
726 last_changed = curwin->w_cursor.lnum;
727 }
728 }
729 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000730 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732
733 /* put cursor on first non-blank of indented line */
734 curwin->w_cursor.lnum = start_lnum;
735 beginline(BL_SOL | BL_FIX);
736
737 /* Mark changed lines so that they will be redrawn. When Visual
738 * highlighting was present, need to continue until the last line. When
739 * there is no change still need to remove the Visual highlighting. */
740 if (last_changed != 0)
741 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 else if (oap->is_VIsual)
745 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747 if (oap->line_count > p_report)
748 {
749 i = oap->line_count - (i + 1);
750 if (i == 1)
751 MSG(_("1 line indented "));
752 else
753 smsg((char_u *)_("%ld lines indented "), i);
754 }
755 /* set '[ and '] marks */
756 curbuf->b_op_start = oap->start;
757 curbuf->b_op_end = oap->end;
758}
759#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
760
761#if defined(FEAT_EVAL) || defined(PROTO)
762/*
763 * Keep the last expression line here, for repeating.
764 */
765static char_u *expr_line = NULL;
766
767/*
768 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
769 * Returns '=' when OK, NUL otherwise.
770 */
771 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100772get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773{
774 char_u *new_line;
775
776 new_line = getcmdline('=', 0L, 0);
777 if (new_line == NULL)
778 return NUL;
779 if (*new_line == NUL) /* use previous line */
780 vim_free(new_line);
781 else
782 set_expr_line(new_line);
783 return '=';
784}
785
786/*
787 * Set the expression for the '=' register.
788 * Argument must be an allocated string.
789 */
790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100791set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 vim_free(expr_line);
794 expr_line = new_line;
795}
796
797/*
798 * Get the result of the '=' register expression.
799 * Returns a pointer to allocated memory, or NULL for failure.
800 */
801 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100802get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 char_u *expr_copy;
805 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000806 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 if (expr_line == NULL)
809 return NULL;
810
811 /* Make a copy of the expression, because evaluating it may cause it to be
812 * changed. */
813 expr_copy = vim_strsave(expr_line);
814 if (expr_copy == NULL)
815 return NULL;
816
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000817 /* When we are invoked recursively limit the evaluation to 10 levels.
818 * Then return the string as-is. */
819 if (nested >= 10)
820 return expr_copy;
821
822 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000823 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000824 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 vim_free(expr_copy);
826 return rv;
827}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000828
829/*
830 * Get the '=' register expression itself, without evaluating it.
831 */
832 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100833get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000834{
835 if (expr_line == NULL)
836 return NULL;
837 return vim_strsave(expr_line);
838}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#endif /* FEAT_EVAL */
840
841/*
842 * Check if 'regname' is a valid name of a yank register.
843 * Note: There is no check for 0 (default register), caller should do this
844 */
845 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100846valid_yank_reg(
847 int regname,
848 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849{
850 if ( (regname > 0 && ASCII_ISALNUM(regname))
851 || (!writing && vim_strchr((char_u *)
852#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100853 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100855 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100858 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 || regname == '"'
860 || regname == '-'
861 || regname == '_'
862#ifdef FEAT_CLIPBOARD
863 || regname == '*'
864 || regname == '+'
865#endif
866#ifdef FEAT_DND
867 || (!writing && regname == '~')
868#endif
869 )
870 return TRUE;
871 return FALSE;
872}
873
874/*
875 * Set y_current and y_append, according to the value of "regname".
876 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000877 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 *
879 * If regname is 0 and writing, use register 0
880 * If regname is 0 and reading, use previous register
881 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100883get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 int i;
886
887 y_append = FALSE;
888 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
889 {
890 y_current = y_previous;
891 return;
892 }
893 i = regname;
894 if (VIM_ISDIGIT(i))
895 i -= '0';
896 else if (ASCII_ISLOWER(i))
897 i = CharOrdLow(i) + 10;
898 else if (ASCII_ISUPPER(i))
899 {
900 i = CharOrdUp(i) + 10;
901 y_append = TRUE;
902 }
903 else if (regname == '-')
904 i = DELETION_REGISTER;
905#ifdef FEAT_CLIPBOARD
906 /* When selection is not available, use register 0 instead of '*' */
907 else if (clip_star.available && regname == '*')
908 i = STAR_REGISTER;
909 /* When clipboard is not available, use register 0 instead of '+' */
910 else if (clip_plus.available && regname == '+')
911 i = PLUS_REGISTER;
912#endif
913#ifdef FEAT_DND
914 else if (!writing && regname == '~')
915 i = TILDE_REGISTER;
916#endif
917 else /* not 0-9, a-z, A-Z or '-': use register 0 */
918 i = 0;
919 y_current = &(y_regs[i]);
920 if (writing) /* remember the register we write into for do_put() */
921 y_previous = y_current;
922}
923
Bram Moolenaar8299df92004-07-10 09:47:34 +0000924#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * When "regname" is a clipboard register, obtain the selection. If it's not
927 * available return zero, otherwise return "regname".
928 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100930may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 if (regname == '*')
933 {
934 if (!clip_star.available)
935 regname = 0;
936 else
937 clip_get_selection(&clip_star);
938 }
939 else if (regname == '+')
940 {
941 if (!clip_plus.available)
942 regname = 0;
943 else
944 clip_get_selection(&clip_plus);
945 }
946 return regname;
947}
948#endif
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950/*
951 * Obtain the contents of a "normal" register. The register is made empty.
952 * The returned pointer has allocated memory, use put_register() later.
953 */
954 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100955get_register(
956 int name,
957 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000959 struct yankreg *reg;
960 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
962#ifdef FEAT_CLIPBOARD
963 /* When Visual area changed, may have to update selection. Obtain the
964 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000965 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200967 if (clip_isautosel_star())
968 clip_update_selection(&clip_star);
969 may_get_selection(name);
970 }
971 if (name == '+' && clip_plus.available)
972 {
973 if (clip_isautosel_plus())
974 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 may_get_selection(name);
976 }
977#endif
978
979 get_yank_register(name, 0);
980 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
981 if (reg != NULL)
982 {
983 *reg = *y_current;
984 if (copy)
985 {
986 /* If we run out of memory some or all of the lines are empty. */
987 if (reg->y_size == 0)
988 reg->y_array = NULL;
989 else
990 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
991 * reg->y_size));
992 if (reg->y_array != NULL)
993 {
994 for (i = 0; i < reg->y_size; ++i)
995 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
996 }
997 }
998 else
999 y_current->y_array = NULL;
1000 }
1001 return (void *)reg;
1002}
1003
1004/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001005 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 */
1007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001008put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 get_yank_register(name, 0);
1011 free_yank_all();
1012 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001013 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001015#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 /* Send text written to clipboard register to the clipboard. */
1017 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001020
1021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001022free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001023{
1024 struct yankreg tmp;
1025
1026 tmp = *y_current;
1027 *y_current = *(struct yankreg *)reg;
1028 free_yank_all();
1029 vim_free(reg);
1030 *y_current = tmp;
1031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033#if defined(FEAT_MOUSE) || defined(PROTO)
1034/*
1035 * return TRUE if the current yank register has type MLINE
1036 */
1037 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001038yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
1040 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1041 return FALSE;
1042 if (regname == '_') /* black hole is always empty */
1043 return FALSE;
1044 get_yank_register(regname, FALSE);
1045 return (y_current->y_type == MLINE);
1046}
1047#endif
1048
1049/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001050 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001052 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 */
1054 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001055do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaard55de222007-05-06 13:38:48 +00001057 char_u *p;
1058 static int regname;
1059 struct yankreg *old_y_previous, *old_y_current;
1060 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
1062 if (Recording == FALSE) /* start recording */
1063 {
1064 /* registers 0-9, a-z and " are allowed */
1065 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1066 retval = FAIL;
1067 else
1068 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001069 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 showmode();
1071 regname = c;
1072 retval = OK;
1073 }
1074 }
1075 else /* stop recording */
1076 {
1077 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001078 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1079 * needs to be removed again to put it in a register. exec_reg then
1080 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
1082 Recording = FALSE;
1083 MSG("");
1084 p = get_recorded();
1085 if (p == NULL)
1086 retval = FAIL;
1087 else
1088 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001089 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1090 vim_unescape_csi(p);
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 /*
1093 * We don't want to change the default register here, so save and
1094 * restore the current register name.
1095 */
1096 old_y_previous = y_previous;
1097 old_y_current = y_current;
1098
1099 retval = stuff_yank(regname, p);
1100
1101 y_previous = old_y_previous;
1102 y_current = old_y_current;
1103 }
1104 }
1105 return retval;
1106}
1107
1108/*
1109 * Stuff string "p" into yank register "regname" as a single line (append if
1110 * uppercase). "p" must have been alloced.
1111 *
1112 * return FAIL for failure, OK otherwise
1113 */
1114 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001115stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 char_u *lp;
1118 char_u **pp;
1119
1120 /* check for read-only register */
1121 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1122 {
1123 vim_free(p);
1124 return FAIL;
1125 }
1126 if (regname == '_') /* black hole: don't do anything */
1127 {
1128 vim_free(p);
1129 return OK;
1130 }
1131 get_yank_register(regname, TRUE);
1132 if (y_append && y_current->y_array != NULL)
1133 {
1134 pp = &(y_current->y_array[y_current->y_size - 1]);
1135 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1136 if (lp == NULL)
1137 {
1138 vim_free(p);
1139 return FAIL;
1140 }
1141 STRCPY(lp, *pp);
1142 STRCAT(lp, p);
1143 vim_free(p);
1144 vim_free(*pp);
1145 *pp = lp;
1146 }
1147 else
1148 {
1149 free_yank_all();
1150 if ((y_current->y_array =
1151 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1152 {
1153 vim_free(p);
1154 return FAIL;
1155 }
1156 y_current->y_array[0] = p;
1157 y_current->y_size = 1;
1158 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1159 }
1160 return OK;
1161}
1162
Bram Moolenaar42b94362009-05-26 16:12:37 +00001163static int execreg_lastc = NUL;
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165/*
1166 * execute a yank register: copy it into the stuff buffer
1167 *
1168 * return FAIL for failure, OK otherwise
1169 */
1170 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001171do_execreg(
1172 int regname,
1173 int colon, /* insert ':' before each line */
1174 int addcr, /* always add '\n' to end of line */
1175 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 long i;
1178 char_u *p;
1179 int retval = OK;
1180 int remap;
1181
1182 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001183 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001184 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001185 {
1186 EMSG(_("E748: No previously used register"));
1187 return FAIL;
1188 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001189 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 /* check for valid regname */
1192 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001193 {
1194 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001196 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001197 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199#ifdef FEAT_CLIPBOARD
1200 regname = may_get_selection(regname);
1201#endif
1202
1203 if (regname == '_') /* black hole: don't stuff anything */
1204 return OK;
1205
1206#ifdef FEAT_CMDHIST
1207 if (regname == ':') /* use last command line */
1208 {
1209 if (last_cmdline == NULL)
1210 {
1211 EMSG(_(e_nolastcmd));
1212 return FAIL;
1213 }
1214 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1215 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001216 /* Escape all control characters with a CTRL-V */
1217 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001218 (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 +00001219 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001220 {
1221 /* When in Visual mode "'<,'>" will be prepended to the command.
1222 * Remove it when it's already there. */
1223 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001224 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001225 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001226 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001227 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001228 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230#endif
1231#ifdef FEAT_EVAL
1232 else if (regname == '=')
1233 {
1234 p = get_expr_line();
1235 if (p == NULL)
1236 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001237 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 vim_free(p);
1239 }
1240#endif
1241 else if (regname == '.') /* use last inserted text */
1242 {
1243 p = get_last_insert_save();
1244 if (p == NULL)
1245 {
1246 EMSG(_(e_noinstext));
1247 return FAIL;
1248 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001249 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 vim_free(p);
1251 }
1252 else
1253 {
1254 get_yank_register(regname, FALSE);
1255 if (y_current->y_array == NULL)
1256 return FAIL;
1257
1258 /* Disallow remaping for ":@r". */
1259 remap = colon ? REMAP_NONE : REMAP_YES;
1260
1261 /*
1262 * Insert lines into typeahead buffer, from last one to first one.
1263 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001264 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 for (i = y_current->y_size; --i >= 0; )
1266 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001267 char_u *escaped;
1268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 /* insert NL between lines and after last line if type is MLINE */
1270 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1271 || addcr)
1272 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001273 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 return FAIL;
1275 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001276 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1277 if (escaped == NULL)
1278 return FAIL;
1279 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1280 vim_free(escaped);
1281 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001283 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 == FAIL)
1285 return FAIL;
1286 }
1287 Exec_reg = TRUE; /* disable the 'q' command */
1288 }
1289 return retval;
1290}
1291
1292/*
1293 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1294 * used only after other typeahead has been processed.
1295 */
1296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001297put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 char_u buf[3];
1300
1301 if (restart_edit != NUL)
1302 {
1303 if (restart_edit == 'V')
1304 {
1305 buf[0] = 'g';
1306 buf[1] = 'R';
1307 buf[2] = NUL;
1308 }
1309 else
1310 {
1311 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1312 buf[1] = NUL;
1313 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001314 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 restart_edit = NUL;
1316 }
1317}
1318
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001319/*
1320 * Insert register contents "s" into the typeahead buffer, so that it will be
1321 * executed again.
1322 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1323 * no remapping.
1324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001326put_in_typebuf(
1327 char_u *s,
1328 int esc,
1329 int colon, /* add ':' before the line */
1330 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int retval = OK;
1333
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001334 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001336 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001338 {
1339 char_u *p;
1340
1341 if (esc)
1342 p = vim_strsave_escape_csi(s);
1343 else
1344 p = s;
1345 if (p == NULL)
1346 retval = FAIL;
1347 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001348 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1349 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001350 if (esc)
1351 vim_free(p);
1352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001354 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 return retval;
1356}
1357
1358/*
1359 * Insert a yank register: copy it into the Read buffer.
1360 * Used by CTRL-R command and middle mouse button in insert mode.
1361 *
1362 * return FAIL for failure, OK otherwise
1363 */
1364 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001365insert_reg(
1366 int regname,
1367 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 long i;
1370 int retval = OK;
1371 char_u *arg;
1372 int allocated;
1373
1374 /*
1375 * It is possible to get into an endless loop by having CTRL-R a in
1376 * register a and then, in insert mode, doing CTRL-R a.
1377 * If you hit CTRL-C, the loop will be broken here.
1378 */
1379 ui_breakcheck();
1380 if (got_int)
1381 return FAIL;
1382
1383 /* check for valid regname */
1384 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1385 return FAIL;
1386
1387#ifdef FEAT_CLIPBOARD
1388 regname = may_get_selection(regname);
1389#endif
1390
1391 if (regname == '.') /* insert last inserted text */
1392 retval = stuff_inserted(NUL, 1L, TRUE);
1393 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1394 {
1395 if (arg == NULL)
1396 return FAIL;
1397 stuffescaped(arg, literally);
1398 if (allocated)
1399 vim_free(arg);
1400 }
1401 else /* name or number register */
1402 {
1403 get_yank_register(regname, FALSE);
1404 if (y_current->y_array == NULL)
1405 retval = FAIL;
1406 else
1407 {
1408 for (i = 0; i < y_current->y_size; ++i)
1409 {
1410 stuffescaped(y_current->y_array[i], literally);
1411 /*
1412 * Insert a newline between lines and after last line if
1413 * y_type is MLINE.
1414 */
1415 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1416 stuffcharReadbuff('\n');
1417 }
1418 }
1419 }
1420
1421 return retval;
1422}
1423
1424/*
1425 * Stuff a string into the typeahead buffer, such that edit() will insert it
1426 * literally ("literally" TRUE) or interpret is as typed characters.
1427 */
1428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001429stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
1431 int c;
1432 char_u *start;
1433
1434 while (*arg != NUL)
1435 {
1436 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1437 * stuff K_SPECIAL to get the effect of a special key when "literally"
1438 * is TRUE. */
1439 start = arg;
1440 while ((*arg >= ' '
1441#ifndef EBCDIC
1442 && *arg < DEL /* EBCDIC: chars above space are normal */
1443#endif
1444 )
1445 || (*arg == K_SPECIAL && !literally))
1446 ++arg;
1447 if (arg > start)
1448 stuffReadbuffLen(start, (long)(arg - start));
1449
1450 /* stuff a single special character */
1451 if (*arg != NUL)
1452 {
1453#ifdef FEAT_MBYTE
1454 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001455 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else
1457#endif
1458 c = *arg++;
1459 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1460 stuffcharReadbuff(Ctrl_V);
1461 stuffcharReadbuff(c);
1462 }
1463 }
1464}
1465
1466/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001467 * If "regname" is a special register, return TRUE and store a pointer to its
1468 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001470 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001471get_spec_reg(
1472 int regname,
1473 char_u **argp,
1474 int *allocated, /* return: TRUE when value was allocated */
1475 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 int cnt;
1478
1479 *argp = NULL;
1480 *allocated = FALSE;
1481 switch (regname)
1482 {
1483 case '%': /* file name */
1484 if (errmsg)
1485 check_fname(); /* will give emsg if not set */
1486 *argp = curbuf->b_fname;
1487 return TRUE;
1488
1489 case '#': /* alternate file name */
1490 *argp = getaltfname(errmsg); /* may give emsg if not set */
1491 return TRUE;
1492
1493#ifdef FEAT_EVAL
1494 case '=': /* result of expression */
1495 *argp = get_expr_line();
1496 *allocated = TRUE;
1497 return TRUE;
1498#endif
1499
1500 case ':': /* last command line */
1501 if (last_cmdline == NULL && errmsg)
1502 EMSG(_(e_nolastcmd));
1503 *argp = last_cmdline;
1504 return TRUE;
1505
1506 case '/': /* last search-pattern */
1507 if (last_search_pat() == NULL && errmsg)
1508 EMSG(_(e_noprevre));
1509 *argp = last_search_pat();
1510 return TRUE;
1511
1512 case '.': /* last inserted text */
1513 *argp = get_last_insert_save();
1514 *allocated = TRUE;
1515 if (*argp == NULL && errmsg)
1516 EMSG(_(e_noinstext));
1517 return TRUE;
1518
1519#ifdef FEAT_SEARCHPATH
1520 case Ctrl_F: /* Filename under cursor */
1521 case Ctrl_P: /* Path under cursor, expand via "path" */
1522 if (!errmsg)
1523 return FALSE;
1524 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001525 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 *allocated = TRUE;
1527 return TRUE;
1528#endif
1529
1530 case Ctrl_W: /* word under cursor */
1531 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1532 if (!errmsg)
1533 return FALSE;
1534 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1535 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1536 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1537 *allocated = TRUE;
1538 return TRUE;
1539
1540 case '_': /* black hole: always empty */
1541 *argp = (char_u *)"";
1542 return TRUE;
1543 }
1544
1545 return FALSE;
1546}
1547
1548/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001549 * Paste a yank register into the command line.
1550 * Only for non-special registers.
1551 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 * insert_reg() can't be used here, because special characters from the
1553 * register contents will be interpreted as commands.
1554 *
1555 * return FAIL for failure, OK otherwise
1556 */
1557 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001558cmdline_paste_reg(
1559 int regname,
1560 int literally, /* Insert text literally instead of "as typed" */
1561 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565 get_yank_register(regname, FALSE);
1566 if (y_current->y_array == NULL)
1567 return FAIL;
1568
1569 for (i = 0; i < y_current->y_size; ++i)
1570 {
1571 cmdline_paste_str(y_current->y_array[i], literally);
1572
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001573 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001574 * Don't do this when "remcr" is TRUE. */
1575 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 cmdline_paste_str((char_u *)"\r", literally);
1577
1578 /* Check for CTRL-C, in case someone tries to paste a few thousand
1579 * lines and gets bored. */
1580 ui_breakcheck();
1581 if (got_int)
1582 return FAIL;
1583 }
1584 return OK;
1585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1588/*
1589 * Adjust the register name pointed to with "rp" for the clipboard being
1590 * used always and the clipboard being available.
1591 */
1592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001593adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001595 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1596 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001597 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1598 {
1599 if (clip_unnamed != 0)
1600 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001601 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001602 else
1603 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1604 ? '+' : '*';
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (!clip_star.available && *rp == '*')
1607 *rp = 0;
1608 if (!clip_plus.available && *rp == '+')
1609 *rp = 0;
1610}
1611#endif
1612
1613/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001614 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001616 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
1618 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001619op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 int n;
1622 linenr_T lnum;
1623 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 char_u *newp, *oldp;
1625 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1627 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001628 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1631 return OK;
1632
1633 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1634 if (oap->empty)
1635 return u_save_cursor();
1636
1637 if (!curbuf->b_p_ma)
1638 {
1639 EMSG(_(e_modifiable));
1640 return FAIL;
1641 }
1642
1643#ifdef FEAT_CLIPBOARD
1644 adjust_clip_reg(&oap->regname);
1645#endif
1646
1647#ifdef FEAT_MBYTE
1648 if (has_mbyte)
1649 mb_adjust_opend(oap);
1650#endif
1651
Bram Moolenaard04b7502010-07-08 22:27:55 +02001652 /*
1653 * Imitate the strange Vi behaviour: If the delete spans more than one
1654 * line and motion_type == MCHAR and the result is a blank line, make the
1655 * delete linewise. Don't do this for the change command or Visual mode.
1656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001659 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001661 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 && oap->op_type == OP_DELETE)
1663 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001664 ptr = ml_get(oap->end.lnum) + oap->end.col;
1665 if (*ptr != NUL)
1666 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ptr = skipwhite(ptr);
1668 if (*ptr == NUL && inindent(0))
1669 oap->motion_type = MLINE;
1670 }
1671
Bram Moolenaard04b7502010-07-08 22:27:55 +02001672 /*
1673 * Check for trying to delete (e.g. "D") in an empty line.
1674 * Note: For the change operator it is ok.
1675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if ( oap->motion_type == MCHAR
1677 && oap->line_count == 1
1678 && oap->op_type == OP_DELETE
1679 && *ml_get(oap->start.lnum) == NUL)
1680 {
1681 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001682 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 * 'cpoptions' (Vi compatible).
1684 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001685#ifdef FEAT_VIRTUALEDIT
1686 if (virtual_op)
1687 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1688 * marks as if it happened. */
1689 goto setmarks;
1690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1692 beep_flush();
1693 return OK;
1694 }
1695
Bram Moolenaard04b7502010-07-08 22:27:55 +02001696 /*
1697 * Do a yank of whatever we're about to delete.
1698 * If a yank register was specified, put the deleted text into that
1699 * register. For the black hole register '_' don't yank anything.
1700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (oap->regname != '_')
1702 {
1703 if (oap->regname != 0)
1704 {
1705 /* check for read-only register */
1706 if (!valid_yank_reg(oap->regname, TRUE))
1707 {
1708 beep_flush();
1709 return OK;
1710 }
1711 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1712 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1713 did_yank = TRUE;
1714 }
1715
1716 /*
1717 * Put deleted text into register 1 and shift number registers if the
1718 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001719 * Use the register name from before adjust_clip_reg() may have
1720 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001722 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 || oap->line_count > 1 || oap->use_reg_one)
1724 {
1725 y_current = &y_regs[9];
1726 free_yank_all(); /* free register nine */
1727 for (n = 9; n > 1; --n)
1728 y_regs[n] = y_regs[n - 1];
1729 y_previous = y_current = &y_regs[1];
1730 y_regs[1].y_array = NULL; /* set register one to empty */
1731 if (op_yank(oap, TRUE, FALSE) == OK)
1732 did_yank = TRUE;
1733 }
1734
Bram Moolenaar84298db2012-04-20 13:46:08 +02001735 /* Yank into small delete register when no named register specified
1736 * and the delete is within one line. */
1737 if ((
1738#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001739 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1740 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001741#endif
1742 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 && oap->line_count == 1)
1744 {
1745 oap->regname = '-';
1746 get_yank_register(oap->regname, TRUE);
1747 if (op_yank(oap, TRUE, FALSE) == OK)
1748 did_yank = TRUE;
1749 oap->regname = 0;
1750 }
1751
1752 /*
1753 * If there's too much stuff to fit in the yank register, then get a
1754 * confirmation before doing the delete. This is crude, but simple.
1755 * And it avoids doing a delete of something we can't put back if we
1756 * want.
1757 */
1758 if (!did_yank)
1759 {
1760 int msg_silent_save = msg_silent;
1761
1762 msg_silent = 0; /* must display the prompt */
1763 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1764 msg_silent = msg_silent_save;
1765 if (n != 'y')
1766 {
1767 EMSG(_(e_abort));
1768 return FAIL;
1769 }
1770 }
1771 }
1772
Bram Moolenaard04b7502010-07-08 22:27:55 +02001773 /*
1774 * block mode delete
1775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (oap->block_mode)
1777 {
1778 if (u_save((linenr_T)(oap->start.lnum - 1),
1779 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1780 return FAIL;
1781
1782 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1783 {
1784 block_prep(oap, &bd, lnum, TRUE);
1785 if (bd.textlen == 0) /* nothing to delete */
1786 continue;
1787
1788 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1789 if (lnum == curwin->w_cursor.lnum)
1790 {
1791 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1792# ifdef FEAT_VIRTUALEDIT
1793 curwin->w_cursor.coladd = 0;
1794# endif
1795 }
1796
1797 /* n == number of chars deleted
1798 * If we delete a TAB, it may be replaced by several characters.
1799 * Thus the number of characters may increase!
1800 */
1801 n = bd.textlen - bd.startspaces - bd.endspaces;
1802 oldp = ml_get(lnum);
1803 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1804 if (newp == NULL)
1805 continue;
1806 /* copy up to deleted part */
1807 mch_memmove(newp, oldp, (size_t)bd.textcol);
1808 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001809 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 (size_t)(bd.startspaces + bd.endspaces));
1811 /* copy the part after the deleted part */
1812 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001813 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /* replace the line */
1815 ml_replace(lnum, newp, FALSE);
1816 }
1817
1818 check_cursor_col();
1819 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1820 oap->end.lnum + 1, 0L);
1821 oap->line_count = 0; /* no lines deleted */
1822 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001823 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 if (oap->op_type == OP_CHANGE)
1826 {
1827 /* Delete the lines except the first one. Temporarily move the
1828 * cursor to the next line. Save the current line number, if the
1829 * last line is deleted it may be changed.
1830 */
1831 if (oap->line_count > 1)
1832 {
1833 lnum = curwin->w_cursor.lnum;
1834 ++curwin->w_cursor.lnum;
1835 del_lines((long)(oap->line_count - 1), TRUE);
1836 curwin->w_cursor.lnum = lnum;
1837 }
1838 if (u_save_cursor() == FAIL)
1839 return FAIL;
1840 if (curbuf->b_p_ai) /* don't delete indent */
1841 {
1842 beginline(BL_WHITE); /* cursor on first non-white */
1843 did_ai = TRUE; /* delete the indent when ESC hit */
1844 ai_col = curwin->w_cursor.col;
1845 }
1846 else
1847 beginline(0); /* cursor in column 0 */
1848 truncate_line(FALSE); /* delete the rest of the line */
1849 /* leave cursor past last char in line */
1850 if (oap->line_count > 1)
1851 u_clearline(); /* "U" command not possible after "2cc" */
1852 }
1853 else
1854 {
1855 del_lines(oap->line_count, TRUE);
1856 beginline(BL_WHITE | BL_FIX);
1857 u_clearline(); /* "U" command not possible after "dd" */
1858 }
1859 }
1860 else
1861 {
1862#ifdef FEAT_VIRTUALEDIT
1863 if (virtual_op)
1864 {
1865 int endcol = 0;
1866
1867 /* For virtualedit: break the tabs that are partly included. */
1868 if (gchar_pos(&oap->start) == '\t')
1869 {
1870 if (u_save_cursor() == FAIL) /* save first line for undo */
1871 return FAIL;
1872 if (oap->line_count == 1)
1873 endcol = getviscol2(oap->end.col, oap->end.coladd);
1874 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1875 oap->start = curwin->w_cursor;
1876 if (oap->line_count == 1)
1877 {
1878 coladvance(endcol);
1879 oap->end.col = curwin->w_cursor.col;
1880 oap->end.coladd = curwin->w_cursor.coladd;
1881 curwin->w_cursor = oap->start;
1882 }
1883 }
1884
1885 /* Break a tab only when it's included in the area. */
1886 if (gchar_pos(&oap->end) == '\t'
1887 && (int)oap->end.coladd < oap->inclusive)
1888 {
1889 /* save last line for undo */
1890 if (u_save((linenr_T)(oap->end.lnum - 1),
1891 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1892 return FAIL;
1893 curwin->w_cursor = oap->end;
1894 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1895 oap->end = curwin->w_cursor;
1896 curwin->w_cursor = oap->start;
1897 }
1898 }
1899#endif
1900
1901 if (oap->line_count == 1) /* delete characters within one line */
1902 {
1903 if (u_save_cursor() == FAIL) /* save line for undo */
1904 return FAIL;
1905
1906 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001907 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 && oap->op_type == OP_CHANGE
1909 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001910 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 display_dollar(oap->end.col - !oap->inclusive);
1912
1913 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1914
1915#ifdef FEAT_VIRTUALEDIT
1916 if (virtual_op)
1917 {
1918 /* fix up things for virtualedit-delete:
1919 * break the tabs which are going to get in our way
1920 */
1921 char_u *curline = ml_get_curline();
1922 int len = (int)STRLEN(curline);
1923
1924 if (oap->end.coladd != 0
1925 && (int)oap->end.col >= len - 1
1926 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1927 n++;
1928 /* Delete at least one char (e.g, when on a control char). */
1929 if (n == 0 && oap->start.coladd != oap->end.coladd)
1930 n = 1;
1931
1932 /* When deleted a char in the line, reset coladd. */
1933 if (gchar_cursor() != NUL)
1934 curwin->w_cursor.coladd = 0;
1935 }
1936#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001937 (void)del_bytes((long)n, !virtual_op,
1938 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 else /* delete characters between lines */
1941 {
1942 pos_T curpos;
1943
1944 /* save deleted and changed lines for undo */
1945 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1946 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1947 return FAIL;
1948
1949 truncate_line(TRUE); /* delete from cursor to end of line */
1950
1951 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1952 ++curwin->w_cursor.lnum;
1953 del_lines((long)(oap->line_count - 2), FALSE);
1954
Bram Moolenaard009e862015-06-09 20:20:03 +02001955 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001956 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001957 curwin->w_cursor.col = 0;
1958 (void)del_bytes((long)n, !virtual_op,
1959 oap->op_type == OP_DELETE && !oap->is_VIsual);
1960 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1961 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 }
1964
1965 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1966
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001967#ifdef FEAT_VIRTUALEDIT
1968setmarks:
1969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (oap->block_mode)
1971 {
1972 curbuf->b_op_end.lnum = oap->end.lnum;
1973 curbuf->b_op_end.col = oap->start.col;
1974 }
1975 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 curbuf->b_op_end = oap->start;
1977 curbuf->b_op_start = oap->start;
1978
1979 return OK;
1980}
1981
1982#ifdef FEAT_MBYTE
1983/*
1984 * Adjust end of operating area for ending on a multi-byte character.
1985 * Used for deletion.
1986 */
1987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001988mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 char_u *p;
1991
1992 if (oap->inclusive)
1993 {
1994 p = ml_get(oap->end.lnum);
1995 oap->end.col += mb_tail_off(p, p + oap->end.col);
1996 }
1997}
1998#endif
1999
2000#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2001/*
2002 * Replace a whole area with one character.
2003 */
2004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 int n, numc;
2008#ifdef FEAT_MBYTE
2009 int num_chars;
2010#endif
2011 char_u *newp, *oldp;
2012 size_t oldlen;
2013 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002014 char_u *after_p = NULL;
2015 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2018 return OK; /* nothing to do */
2019
Bram Moolenaard9820532013-11-04 01:41:17 +01002020 if (had_ctrl_v_cr)
2021 c = (c == -1 ? '\r' : '\n');
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#ifdef FEAT_MBYTE
2024 if (has_mbyte)
2025 mb_adjust_opend(oap);
2026#endif
2027
2028 if (u_save((linenr_T)(oap->start.lnum - 1),
2029 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2030 return FAIL;
2031
2032 /*
2033 * block mode replace
2034 */
2035 if (oap->block_mode)
2036 {
2037 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2038 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2039 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002040 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2042 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2043 continue; /* nothing to replace */
2044
2045 /* n == number of extra chars required
2046 * If we split a TAB, it may be replaced by several characters.
2047 * Thus the number of characters may increase!
2048 */
2049#ifdef FEAT_VIRTUALEDIT
2050 /* If the range starts in virtual space, count the initial
2051 * coladd offset as part of "startspaces" */
2052 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2053 {
2054 pos_T vpos;
2055
Bram Moolenaara1381de2009-11-03 15:44:21 +00002056 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 getvpos(&vpos, oap->start_vcol);
2058 bd.startspaces += vpos.coladd;
2059 n = bd.startspaces;
2060 }
2061 else
2062#endif
2063 /* allow for pre spaces */
2064 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2065
2066 /* allow for post spp */
2067 n += (bd.endspaces
2068#ifdef FEAT_VIRTUALEDIT
2069 && !bd.is_oneChar
2070#endif
2071 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2072 /* Figure out how many characters to replace. */
2073 numc = oap->end_vcol - oap->start_vcol + 1;
2074 if (bd.is_short && (!virtual_op || bd.is_MAX))
2075 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2076
2077#ifdef FEAT_MBYTE
2078 /* A double-wide character can be replaced only up to half the
2079 * times. */
2080 if ((*mb_char2cells)(c) > 1)
2081 {
2082 if ((numc & 1) && !bd.is_short)
2083 {
2084 ++bd.endspaces;
2085 ++n;
2086 }
2087 numc = numc / 2;
2088 }
2089
2090 /* Compute bytes needed, move character count to num_chars. */
2091 num_chars = numc;
2092 numc *= (*mb_char2len)(c);
2093#endif
2094 /* oldlen includes textlen, so don't double count */
2095 n += numc - bd.textlen;
2096
2097 oldp = ml_get_curline();
2098 oldlen = STRLEN(oldp);
2099 newp = alloc_check((unsigned)oldlen + 1 + n);
2100 if (newp == NULL)
2101 continue;
2102 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2103 /* copy up to deleted part */
2104 mch_memmove(newp, oldp, (size_t)bd.textcol);
2105 oldp += bd.textcol + bd.textlen;
2106 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002107 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002109 /* -1/-2 is used for entering CR literally. */
2110 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002112#ifdef FEAT_MBYTE
2113 if (has_mbyte)
2114 {
2115 n = (int)STRLEN(newp);
2116 while (--num_chars >= 0)
2117 n += (*mb_char2bytes)(c, newp + n);
2118 }
2119 else
2120#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002121 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002122 if (!bd.is_short)
2123 {
2124 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002125 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002126 /* copy the part after the changed part */
2127 STRMOVE(newp + STRLEN(newp), oldp);
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002132 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002133 after_p = alloc_check(
2134 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002135 if (after_p != NULL)
2136 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 /* replace the line */
2139 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002140 if (after_p != NULL)
2141 {
2142 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2143 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2144 oap->end.lnum++;
2145 vim_free(after_p);
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148 }
2149 else
2150 {
2151 /*
2152 * MCHAR and MLINE motion replace.
2153 */
2154 if (oap->motion_type == MLINE)
2155 {
2156 oap->start.col = 0;
2157 curwin->w_cursor.col = 0;
2158 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2159 if (oap->end.col)
2160 --oap->end.col;
2161 }
2162 else if (!oap->inclusive)
2163 dec(&(oap->end));
2164
2165 while (ltoreq(curwin->w_cursor, oap->end))
2166 {
2167 n = gchar_cursor();
2168 if (n != NUL)
2169 {
2170#ifdef FEAT_MBYTE
2171 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2172 {
2173 /* This is slow, but it handles replacing a single-byte
2174 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002175 if (curwin->w_cursor.lnum == oap->end.lnum)
2176 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 n = State;
2178 State = REPLACE;
2179 ins_char(c);
2180 State = n;
2181 /* Backup to the replaced character. */
2182 dec_cursor();
2183 }
2184 else
2185#endif
2186 {
2187#ifdef FEAT_VIRTUALEDIT
2188 if (n == TAB)
2189 {
2190 int end_vcol = 0;
2191
2192 if (curwin->w_cursor.lnum == oap->end.lnum)
2193 {
2194 /* oap->end has to be recalculated when
2195 * the tab breaks */
2196 end_vcol = getviscol2(oap->end.col,
2197 oap->end.coladd);
2198 }
2199 coladvance_force(getviscol());
2200 if (curwin->w_cursor.lnum == oap->end.lnum)
2201 getvpos(&oap->end, end_vcol);
2202 }
2203#endif
2204 pchar(curwin->w_cursor, c);
2205 }
2206 }
2207#ifdef FEAT_VIRTUALEDIT
2208 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2209 {
2210 int virtcols = oap->end.coladd;
2211
2212 if (curwin->w_cursor.lnum == oap->start.lnum
2213 && oap->start.col == oap->end.col && oap->start.coladd)
2214 virtcols -= oap->start.coladd;
2215
2216 /* oap->end has been trimmed so it's effectively inclusive;
2217 * as a result an extra +1 must be counted so we don't
2218 * trample the NUL byte. */
2219 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2220 curwin->w_cursor.col -= (virtcols + 1);
2221 for (; virtcols >= 0; virtcols--)
2222 {
2223 pchar(curwin->w_cursor, c);
2224 if (inc(&curwin->w_cursor) == -1)
2225 break;
2226 }
2227 }
2228#endif
2229
2230 /* Advance to next character, stop at the end of the file. */
2231 if (inc_cursor() == -1)
2232 break;
2233 }
2234 }
2235
2236 curwin->w_cursor = oap->start;
2237 check_cursor();
2238 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2239
2240 /* Set "'[" and "']" marks. */
2241 curbuf->b_op_start = oap->start;
2242 curbuf->b_op_end = oap->end;
2243
2244 return OK;
2245}
2246#endif
2247
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002248static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250/*
2251 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2252 */
2253 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002254op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002258 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260 if (u_save((linenr_T)(oap->start.lnum - 1),
2261 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2262 return;
2263
2264 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (oap->block_mode) /* Visual block mode */
2266 {
2267 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2268 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002269 int one_change;
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 block_prep(oap, &bd, pos.lnum, FALSE);
2272 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002273 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2274 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002275
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002276#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002277 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2280
Bram Moolenaar009b2592004-10-24 19:18:58 +00002281 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2282 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002284 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 if (did_change)
2289 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2290 }
2291 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 if (oap->motion_type == MLINE)
2294 {
2295 oap->start.col = 0;
2296 pos.col = 0;
2297 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2298 if (oap->end.col)
2299 --oap->end.col;
2300 }
2301 else if (!oap->inclusive)
2302 dec(&(oap->end));
2303
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002304 if (pos.lnum == oap->end.lnum)
2305 did_change = swapchars(oap->op_type, &pos,
2306 oap->end.col - pos.col + 1);
2307 else
2308 for (;;)
2309 {
2310 did_change |= swapchars(oap->op_type, &pos,
2311 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2312 (int)STRLEN(ml_get_pos(&pos)));
2313 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2314 break;
2315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (did_change)
2317 {
2318 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2319 0L);
2320#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002321 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 char_u *ptr;
2324 int count;
2325
2326 pos = oap->start;
2327 while (pos.lnum < oap->end.lnum)
2328 {
2329 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002330 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002331 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002333 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 pos.col = 0;
2335 pos.lnum++;
2336 }
2337 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2338 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002339 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002341 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343#endif
2344 }
2345 }
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (!did_change && oap->is_VIsual)
2348 /* No change: need to remove the Visual selection */
2349 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
2352 * Set '[ and '] marks.
2353 */
2354 curbuf->b_op_start = oap->start;
2355 curbuf->b_op_end = oap->end;
2356
2357 if (oap->line_count > p_report)
2358 {
2359 if (oap->line_count == 1)
2360 MSG(_("1 line changed"));
2361 else
2362 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2363 }
2364}
2365
2366/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002367 * Invoke swapchar() on "length" bytes at position "pos".
2368 * "pos" is advanced to just after the changed characters.
2369 * "length" is rounded up to include the whole last multi-byte character.
2370 * Also works correctly when the number of bytes changes.
2371 * Returns TRUE if some character was changed.
2372 */
2373 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002374swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002375{
2376 int todo;
2377 int did_change = 0;
2378
2379 for (todo = length; todo > 0; --todo)
2380 {
2381# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002382 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002383 {
2384 int len = (*mb_ptr2len)(ml_get_pos(pos));
2385
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002386 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002387 if (len > 0)
2388 todo -= len - 1;
2389 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002390# endif
2391 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002392 if (inc(pos) == -1) /* at end of file */
2393 break;
2394 }
2395 return did_change;
2396}
2397
2398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * If op_type == OP_UPPER: make uppercase,
2400 * if op_type == OP_LOWER: make lowercase,
2401 * if op_type == OP_ROT13: do rot13 encoding,
2402 * else swap case of character at 'pos'
2403 * returns TRUE when something actually changed.
2404 */
2405 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002406swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408 int c;
2409 int nc;
2410
2411 c = gchar_pos(pos);
2412
2413 /* Only do rot13 encoding for ASCII characters. */
2414 if (c >= 0x80 && op_type == OP_ROT13)
2415 return FALSE;
2416
2417#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002418 if (op_type == OP_UPPER && c == 0xdf
2419 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002420 {
2421 pos_T sp = curwin->w_cursor;
2422
2423 /* Special handling of German sharp s: change to "SS". */
2424 curwin->w_cursor = *pos;
2425 del_char(FALSE);
2426 ins_char('S');
2427 ins_char('S');
2428 curwin->w_cursor = sp;
2429 inc(pos);
2430 }
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2433 return FALSE;
2434#endif
2435 nc = c;
2436 if (MB_ISLOWER(c))
2437 {
2438 if (op_type == OP_ROT13)
2439 nc = ROT13(c, 'a');
2440 else if (op_type != OP_LOWER)
2441 nc = MB_TOUPPER(c);
2442 }
2443 else if (MB_ISUPPER(c))
2444 {
2445 if (op_type == OP_ROT13)
2446 nc = ROT13(c, 'A');
2447 else if (op_type != OP_UPPER)
2448 nc = MB_TOLOWER(c);
2449 }
2450 if (nc != c)
2451 {
2452#ifdef FEAT_MBYTE
2453 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2454 {
2455 pos_T sp = curwin->w_cursor;
2456
2457 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002458 /* don't use del_char(), it also removes composing chars */
2459 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 ins_char(nc);
2461 curwin->w_cursor = sp;
2462 }
2463 else
2464#endif
2465 pchar(*pos, nc);
2466 return TRUE;
2467 }
2468 return FALSE;
2469}
2470
2471#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2472/*
2473 * op_insert - Insert and append operators for Visual mode.
2474 */
2475 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002476op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 long ins_len, pre_textlen = 0;
2479 char_u *firstline, *ins_text;
2480 struct block_def bd;
2481 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002482 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 /* edit() changes this - record it for OP_APPEND */
2485 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2486
2487 /* vis block is still marked. Get rid of it now. */
2488 curwin->w_cursor.lnum = oap->start.lnum;
2489 update_screen(INVERTED);
2490
2491 if (oap->block_mode)
2492 {
2493#ifdef FEAT_VIRTUALEDIT
2494 /* When 'virtualedit' is used, need to insert the extra spaces before
2495 * doing block_prep(). When only "block" is used, virtual edit is
2496 * already disabled, but still need it when calling
2497 * coladvance_force(). */
2498 if (curwin->w_cursor.coladd > 0)
2499 {
2500 int old_ve_flags = ve_flags;
2501
2502 ve_flags = VE_ALL;
2503 if (u_save_cursor() == FAIL)
2504 return;
2505 coladvance_force(oap->op_type == OP_APPEND
2506 ? oap->end_vcol + 1 : getviscol());
2507 if (oap->op_type == OP_APPEND)
2508 --curwin->w_cursor.col;
2509 ve_flags = old_ve_flags;
2510 }
2511#endif
2512 /* Get the info about the block before entering the text */
2513 block_prep(oap, &bd, oap->start.lnum, TRUE);
2514 firstline = ml_get(oap->start.lnum) + bd.textcol;
2515 if (oap->op_type == OP_APPEND)
2516 firstline += bd.textlen;
2517 pre_textlen = (long)STRLEN(firstline);
2518 }
2519
2520 if (oap->op_type == OP_APPEND)
2521 {
2522 if (oap->block_mode
2523#ifdef FEAT_VIRTUALEDIT
2524 && curwin->w_cursor.coladd == 0
2525#endif
2526 )
2527 {
2528 /* Move the cursor to the character right of the block. */
2529 curwin->w_set_curswant = TRUE;
2530 while (*ml_get_cursor() != NUL
2531 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2532 ++curwin->w_cursor.col;
2533 if (bd.is_short && !bd.is_MAX)
2534 {
2535 /* First line was too short, make it longer and adjust the
2536 * values in "bd". */
2537 if (u_save_cursor() == FAIL)
2538 return;
2539 for (i = 0; i < bd.endspaces; ++i)
2540 ins_char(' ');
2541 bd.textlen += bd.endspaces;
2542 }
2543 }
2544 else
2545 {
2546 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002547 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /* Works just like an 'i'nsert on the next character. */
2550 if (!lineempty(curwin->w_cursor.lnum)
2551 && oap->start_vcol != oap->end_vcol)
2552 inc_cursor();
2553 }
2554 }
2555
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002556 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 edit(NUL, FALSE, (linenr_T)count1);
2558
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002559 /* When a tab was inserted, and the characters in front of the tab
2560 * have been converted to a tab as well, the column of the cursor
2561 * might have actually been reduced, so need to adjust here. */
2562 if (t1.lnum == curbuf->b_op_start_orig.lnum
2563 && lt(curbuf->b_op_start_orig, t1))
2564 oap->start = curbuf->b_op_start_orig;
2565
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002566 /* If user has moved off this line, we don't know what to do, so do
2567 * nothing.
2568 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2569 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return;
2571
2572 if (oap->block_mode)
2573 {
2574 struct block_def bd2;
2575
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002576 /* The user may have moved the cursor before inserting something, try
2577 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002578 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002579 {
2580 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002581 && oap->start.col
2582#ifdef FEAT_VIRTUALEDIT
2583 + oap->start.coladd
2584#endif
2585 != curbuf->b_op_start_orig.col
2586#ifdef FEAT_VIRTUALEDIT
2587 + curbuf->b_op_start_orig.coladd
2588#endif
2589 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002590 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002591 int t = getviscol2(curbuf->b_op_start_orig.col,
2592 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002593 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002594 pre_textlen -= t - oap->start_vcol;
2595 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002596 }
2597 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002598 && oap->end.col
2599#ifdef FEAT_VIRTUALEDIT
2600 + oap->end.coladd
2601#endif
2602 >= curbuf->b_op_start_orig.col
2603#ifdef FEAT_VIRTUALEDIT
2604 + curbuf->b_op_start_orig.coladd
2605#endif
2606 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002607 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002608 int t = getviscol2(curbuf->b_op_start_orig.col,
2609 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002610 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002611 /* reset pre_textlen to the value of OP_INSERT */
2612 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002613 pre_textlen -= t - oap->start_vcol;
2614 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 oap->op_type = OP_INSERT;
2616 }
2617 }
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 /*
2620 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002621 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 * Don't do this when "$" used, end-of-line will have changed.
2623 */
2624 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2625 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2626 {
2627 if (oap->op_type == OP_APPEND)
2628 {
2629 pre_textlen += bd2.textlen - bd.textlen;
2630 if (bd2.endspaces)
2631 --bd2.textlen;
2632 }
2633 bd.textcol = bd2.textcol;
2634 bd.textlen = bd2.textlen;
2635 }
2636
2637 /*
2638 * Subsequent calls to ml_get() flush the firstline data - take a
2639 * copy of the required string.
2640 */
2641 firstline = ml_get(oap->start.lnum) + bd.textcol;
2642 if (oap->op_type == OP_APPEND)
2643 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002644 if (pre_textlen >= 0
2645 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
2647 ins_text = vim_strnsave(firstline, (int)ins_len);
2648 if (ins_text != NULL)
2649 {
2650 /* block handled here */
2651 if (u_save(oap->start.lnum,
2652 (linenr_T)(oap->end.lnum + 1)) == OK)
2653 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2654 &bd);
2655
2656 curwin->w_cursor.col = oap->start.col;
2657 check_cursor();
2658 vim_free(ins_text);
2659 }
2660 }
2661 }
2662}
2663#endif
2664
2665/*
2666 * op_change - handle a change operation
2667 *
2668 * return TRUE if edit() returns because of a CTRL-O command
2669 */
2670 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002671op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 colnr_T l;
2674 int retval;
2675#ifdef FEAT_VISUALEXTRA
2676 long offset;
2677 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002678 long ins_len;
2679 long pre_textlen = 0;
2680 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 char_u *firstline;
2682 char_u *ins_text, *newp, *oldp;
2683 struct block_def bd;
2684#endif
2685
2686 l = oap->start.col;
2687 if (oap->motion_type == MLINE)
2688 {
2689 l = 0;
2690#ifdef FEAT_SMARTINDENT
2691 if (!p_paste && curbuf->b_p_si
2692# ifdef FEAT_CINDENT
2693 && !curbuf->b_p_cin
2694# endif
2695 )
2696 can_si = TRUE; /* It's like opening a new line, do si */
2697#endif
2698 }
2699
2700 /* First delete the text in the region. In an empty buffer only need to
2701 * save for undo */
2702 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2703 {
2704 if (u_save_cursor() == FAIL)
2705 return FALSE;
2706 }
2707 else if (op_delete(oap) == FAIL)
2708 return FALSE;
2709
2710 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2711 && !virtual_op)
2712 inc_cursor();
2713
2714#ifdef FEAT_VISUALEXTRA
2715 /* check for still on same line (<CR> in inserted text meaningless) */
2716 /* skip blank lines too */
2717 if (oap->block_mode)
2718 {
2719# ifdef FEAT_VIRTUALEDIT
2720 /* Add spaces before getting the current line length. */
2721 if (virtual_op && (curwin->w_cursor.coladd > 0
2722 || gchar_cursor() == NUL))
2723 coladvance_force(getviscol());
2724# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002725 firstline = ml_get(oap->start.lnum);
2726 pre_textlen = (long)STRLEN(firstline);
2727 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 bd.textcol = curwin->w_cursor.col;
2729 }
2730#endif
2731
2732#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2733 if (oap->motion_type == MLINE)
2734 fix_indent();
2735#endif
2736
2737 retval = edit(NUL, FALSE, (linenr_T)1);
2738
2739#ifdef FEAT_VISUALEXTRA
2740 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002741 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002743 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002745 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002747 /* Auto-indenting may have changed the indent. If the cursor was past
2748 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002750 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002752 long new_indent = (long)(skipwhite(firstline) - firstline);
2753
2754 pre_textlen += new_indent - pre_indent;
2755 bd.textcol += new_indent - pre_indent;
2756 }
2757
2758 ins_len = (long)STRLEN(firstline) - pre_textlen;
2759 if (ins_len > 0)
2760 {
2761 /* Subsequent calls to ml_get() flush the firstline data - take a
2762 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2764 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002765 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2767 linenr++)
2768 {
2769 block_prep(oap, &bd, linenr, TRUE);
2770 if (!bd.is_short || virtual_op)
2771 {
2772# ifdef FEAT_VIRTUALEDIT
2773 pos_T vpos;
2774
2775 /* If the block starts in virtual space, count the
2776 * initial coladd offset as part of "startspaces" */
2777 if (bd.is_short)
2778 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002779 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782 else
2783 vpos.coladd = 0;
2784# endif
2785 oldp = ml_get(linenr);
2786 newp = alloc_check((unsigned)(STRLEN(oldp)
2787# ifdef FEAT_VIRTUALEDIT
2788 + vpos.coladd
2789# endif
2790 + ins_len + 1));
2791 if (newp == NULL)
2792 continue;
2793 /* copy up to block start */
2794 mch_memmove(newp, oldp, (size_t)bd.textcol);
2795 offset = bd.textcol;
2796# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002797 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 offset += vpos.coladd;
2799# endif
2800 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2801 offset += ins_len;
2802 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002803 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 ml_replace(linenr, newp, FALSE);
2805 }
2806 }
2807 check_cursor();
2808
2809 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2810 }
2811 vim_free(ins_text);
2812 }
2813 }
2814#endif
2815
2816 return retval;
2817}
2818
2819/*
2820 * set all the yank registers to empty (called from main())
2821 */
2822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002823init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 int i;
2826
2827 for (i = 0; i < NUM_REGISTERS; ++i)
2828 y_regs[i].y_array = NULL;
2829}
2830
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002831#if defined(EXITFREE) || defined(PROTO)
2832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002833clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002834{
2835 int i;
2836
2837 for (i = 0; i < NUM_REGISTERS; ++i)
2838 {
2839 y_current = &y_regs[i];
2840 if (y_current->y_array != NULL)
2841 free_yank_all();
2842 }
2843}
2844#endif
2845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
2847 * Free "n" lines from the current yank register.
2848 * Called for normal freeing and in case of error.
2849 */
2850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 if (y_current->y_array != NULL)
2854 {
2855 long i;
2856
2857 for (i = n; --i >= 0; )
2858 {
2859#ifdef AMIGA /* only for very slow machines */
2860 if ((i & 1023) == 1023) /* this may take a while */
2861 {
2862 /*
2863 * This message should never cause a hit-return message.
2864 * Overwrite this message with any next message.
2865 */
2866 ++no_wait_return;
2867 smsg((char_u *)_("freeing %ld lines"), i + 1);
2868 --no_wait_return;
2869 msg_didout = FALSE;
2870 msg_col = 0;
2871 }
2872#endif
2873 vim_free(y_current->y_array[i]);
2874 }
2875 vim_free(y_current->y_array);
2876 y_current->y_array = NULL;
2877#ifdef AMIGA
2878 if (n >= 1000)
2879 MSG("");
2880#endif
2881 }
2882}
2883
2884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 free_yank(y_current->y_size);
2888}
2889
2890/*
2891 * Yank the text between "oap->start" and "oap->end" into a yank register.
2892 * If we are to append (uppercase register), we first yank into a new yank
2893 * register and then concatenate the old and the new one (so we keep the old
2894 * one in case of out-of-memory).
2895 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002896 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
2898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002899op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
2901 long y_idx; /* index in y_array[] */
2902 struct yankreg *curr; /* copy of y_current */
2903 struct yankreg newreg; /* new yank register when appending */
2904 char_u **new_ptr;
2905 linenr_T lnum; /* current line number */
2906 long j;
2907 int yanktype = oap->motion_type;
2908 long yanklines = oap->line_count;
2909 linenr_T yankendlnum = oap->end.lnum;
2910 char_u *p;
2911 char_u *pnew;
2912 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002913#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002914 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 /* check for read-only register */
2918 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2919 {
2920 beep_flush();
2921 return FAIL;
2922 }
2923 if (oap->regname == '_') /* black hole: nothing to do */
2924 return OK;
2925
2926#ifdef FEAT_CLIPBOARD
2927 if (!clip_star.available && oap->regname == '*')
2928 oap->regname = 0;
2929 else if (!clip_plus.available && oap->regname == '+')
2930 oap->regname = 0;
2931#endif
2932
2933 if (!deleting) /* op_delete() already set y_current */
2934 get_yank_register(oap->regname, TRUE);
2935
2936 curr = y_current;
2937 /* append to existing contents */
2938 if (y_append && y_current->y_array != NULL)
2939 y_current = &newreg;
2940 else
2941 free_yank_all(); /* free previously yanked lines */
2942
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002943 /*
2944 * If the cursor was in column 1 before and after the movement, and the
2945 * operator is not inclusive, the yank is always linewise.
2946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if ( oap->motion_type == MCHAR
2948 && oap->start.col == 0
2949 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002951 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 && oap->end.col == 0
2953 && yanklines > 1)
2954 {
2955 yanktype = MLINE;
2956 --yankendlnum;
2957 --yanklines;
2958 }
2959
2960 y_current->y_size = yanklines;
2961 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2964 yanklines), TRUE);
2965
2966 if (y_current->y_array == NULL)
2967 {
2968 y_current = curr;
2969 return FAIL;
2970 }
2971
2972 y_idx = 0;
2973 lnum = oap->start.lnum;
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (oap->block_mode)
2976 {
2977 /* Visual block mode */
2978 y_current->y_type = MBLOCK; /* set the yank register type */
2979 y_current->y_width = oap->end_vcol - oap->start_vcol;
2980
2981 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2982 y_current->y_width--;
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2986 {
2987 switch (y_current->y_type)
2988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 case MBLOCK:
2990 block_prep(oap, &bd, lnum, FALSE);
2991 if (yank_copy_line(&bd, y_idx) == FAIL)
2992 goto fail;
2993 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 case MLINE:
2996 if ((y_current->y_array[y_idx] =
2997 vim_strsave(ml_get(lnum))) == NULL)
2998 goto fail;
2999 break;
3000
3001 case MCHAR:
3002 {
3003 colnr_T startcol = 0, endcol = MAXCOL;
3004#ifdef FEAT_VIRTUALEDIT
3005 int is_oneChar = FALSE;
3006 colnr_T cs, ce;
3007#endif
3008 p = ml_get(lnum);
3009 bd.startspaces = 0;
3010 bd.endspaces = 0;
3011
3012 if (lnum == oap->start.lnum)
3013 {
3014 startcol = oap->start.col;
3015#ifdef FEAT_VIRTUALEDIT
3016 if (virtual_op)
3017 {
3018 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3019 if (ce != cs && oap->start.coladd > 0)
3020 {
3021 /* Part of a tab selected -- but don't
3022 * double-count it. */
3023 bd.startspaces = (ce - cs + 1)
3024 - oap->start.coladd;
3025 startcol++;
3026 }
3027 }
3028#endif
3029 }
3030
3031 if (lnum == oap->end.lnum)
3032 {
3033 endcol = oap->end.col;
3034#ifdef FEAT_VIRTUALEDIT
3035 if (virtual_op)
3036 {
3037 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3038 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3039# ifdef FEAT_MBYTE
3040 /* Don't add space for double-wide
3041 * char; endcol will be on last byte
3042 * of multi-byte char. */
3043 && (*mb_head_off)(p, p + endcol) == 0
3044# endif
3045 ))
3046 {
3047 if (oap->start.lnum == oap->end.lnum
3048 && oap->start.col == oap->end.col)
3049 {
3050 /* Special case: inside a single char */
3051 is_oneChar = TRUE;
3052 bd.startspaces = oap->end.coladd
3053 - oap->start.coladd + oap->inclusive;
3054 endcol = startcol;
3055 }
3056 else
3057 {
3058 bd.endspaces = oap->end.coladd
3059 + oap->inclusive;
3060 endcol -= oap->inclusive;
3061 }
3062 }
3063 }
3064#endif
3065 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003066 if (endcol == MAXCOL)
3067 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (startcol > endcol
3069#ifdef FEAT_VIRTUALEDIT
3070 || is_oneChar
3071#endif
3072 )
3073 bd.textlen = 0;
3074 else
3075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 bd.textlen = endcol - startcol + oap->inclusive;
3077 }
3078 bd.textstart = p + startcol;
3079 if (yank_copy_line(&bd, y_idx) == FAIL)
3080 goto fail;
3081 break;
3082 }
3083 /* NOTREACHED */
3084 }
3085 }
3086
3087 if (curr != y_current) /* append the new block to the old block */
3088 {
3089 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3090 (curr->y_size + y_current->y_size)), TRUE);
3091 if (new_ptr == NULL)
3092 goto fail;
3093 for (j = 0; j < curr->y_size; ++j)
3094 new_ptr[j] = curr->y_array[j];
3095 vim_free(curr->y_array);
3096 curr->y_array = new_ptr;
3097
3098 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3099 curr->y_type = MLINE;
3100
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003101 /* Concatenate the last line of the old block with the first line of
3102 * the new block, unless being Vi compatible. */
3103 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3106 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3107 if (pnew == NULL)
3108 {
3109 y_idx = y_current->y_size - 1;
3110 goto fail;
3111 }
3112 STRCPY(pnew, curr->y_array[--j]);
3113 STRCAT(pnew, y_current->y_array[0]);
3114 vim_free(curr->y_array[j]);
3115 vim_free(y_current->y_array[0]);
3116 curr->y_array[j++] = pnew;
3117 y_idx = 1;
3118 }
3119 else
3120 y_idx = 0;
3121 while (y_idx < y_current->y_size)
3122 curr->y_array[j++] = y_current->y_array[y_idx++];
3123 curr->y_size = j;
3124 vim_free(y_current->y_array);
3125 y_current = curr;
3126 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003127 if (curwin->w_p_rnu)
3128 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (mess) /* Display message about yank? */
3130 {
3131 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 && yanklines == 1)
3134 yanklines = 0;
3135 /* Some versions of Vi use ">=" here, some don't... */
3136 if (yanklines > p_report)
3137 {
3138 /* redisplay now, so message is not deleted */
3139 update_topline_redraw();
3140 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003141 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003142 if (oap->block_mode)
3143 MSG(_("block of 1 line yanked"));
3144 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003145 MSG(_("1 line yanked"));
3146 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003147 else if (oap->block_mode)
3148 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 else
3150 smsg((char_u *)_("%ld lines yanked"), yanklines);
3151 }
3152 }
3153
3154 /*
3155 * Set "'[" and "']" marks.
3156 */
3157 curbuf->b_op_start = oap->start;
3158 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003159 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003160 {
3161 curbuf->b_op_start.col = 0;
3162 curbuf->b_op_end.col = MAXCOL;
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165#ifdef FEAT_CLIPBOARD
3166 /*
3167 * If we were yanking to the '*' register, send result to clipboard.
3168 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3169 * to the '*' register.
3170 */
3171 if (clip_star.available
3172 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003173 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003174 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 if (curr != &(y_regs[STAR_REGISTER]))
3177 /* Copy the text from register 0 to the clipboard register. */
3178 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3179
3180 clip_own_selection(&clip_star);
3181 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003182# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003183 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186
3187# ifdef FEAT_X11
3188 /*
3189 * If we were yanking to the '+' register, send result to selection.
3190 * Also copy to the '*' register, in case auto-select is off.
3191 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003192 if (clip_plus.available
3193 && (curr == &(y_regs[PLUS_REGISTER])
3194 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003195 && ((clip_unnamed | clip_unnamed_saved) &
3196 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003198 if (curr != &(y_regs[PLUS_REGISTER]))
3199 /* Copy the text from register 0 to the clipboard register. */
3200 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 clip_own_selection(&clip_plus);
3203 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003204 if (!clip_isautosel_star() && !did_star
3205 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
3207 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3208 clip_own_selection(&clip_star);
3209 clip_gen_set_selection(&clip_star);
3210 }
3211 }
3212# endif
3213#endif
3214
3215 return OK;
3216
3217fail: /* free the allocated lines */
3218 free_yank(y_idx + 1);
3219 y_current = curr;
3220 return FAIL;
3221}
3222
3223 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003224yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 char_u *pnew;
3227
3228 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3229 == NULL)
3230 return FAIL;
3231 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003232 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 pnew += bd->startspaces;
3234 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3235 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003236 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 pnew += bd->endspaces;
3238 *pnew = NUL;
3239 return OK;
3240}
3241
3242#ifdef FEAT_CLIPBOARD
3243/*
3244 * Make a copy of the y_current register to register "reg".
3245 */
3246 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003247copy_yank_reg(struct yankreg *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 struct yankreg *curr = y_current;
3250 long j;
3251
3252 y_current = reg;
3253 free_yank_all();
3254 *y_current = *curr;
3255 y_current->y_array = (char_u **)lalloc_clear(
3256 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3257 if (y_current->y_array == NULL)
3258 y_current->y_size = 0;
3259 else
3260 for (j = 0; j < y_current->y_size; ++j)
3261 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3262 {
3263 free_yank(j);
3264 y_current->y_size = 0;
3265 break;
3266 }
3267 y_current = curr;
3268}
3269#endif
3270
3271/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003272 * Put contents of register "regname" into the text.
3273 * Caller must check "regname" to be valid!
3274 * "flags": PUT_FIXINDENT make indent look nice
3275 * PUT_CURSEND leave cursor after end of new text
3276 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003279do_put(
3280 int regname,
3281 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3282 long count,
3283 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 char_u *ptr;
3286 char_u *newp, *oldp;
3287 int yanklen;
3288 int totlen = 0; /* init for gcc */
3289 linenr_T lnum;
3290 colnr_T col;
3291 long i; /* index in y_array[] */
3292 int y_type;
3293 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 int oldlen;
3295 long y_width = 0;
3296 colnr_T vcol;
3297 int delcount;
3298 int incr = 0;
3299 long j;
3300 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 char_u **y_array = NULL;
3302 long nr_lines = 0;
3303 pos_T new_cursor;
3304 int indent;
3305 int orig_indent = 0; /* init for gcc */
3306 int indent_diff = 0; /* init for gcc */
3307 int first_indent = TRUE;
3308 int lendiff = 0;
3309 pos_T old_pos;
3310 char_u *insert_string = NULL;
3311 int allocated = FALSE;
3312 long cnt;
3313
3314#ifdef FEAT_CLIPBOARD
3315 /* Adjust register name for "unnamed" in 'clipboard'. */
3316 adjust_clip_reg(&regname);
3317 (void)may_get_selection(regname);
3318#endif
3319
3320 if (flags & PUT_FIXINDENT)
3321 orig_indent = get_indent();
3322
3323 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3324 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3325
3326 /*
3327 * Using inserted text works differently, because the register includes
3328 * special characters (newlines, etc.).
3329 */
3330 if (regname == '.')
3331 {
3332 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3333 (count == -1 ? 'O' : 'i')), count, FALSE);
3334 /* Putting the text is done later, so can't really move the cursor to
3335 * the next character. Use "l" to simulate it. */
3336 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3337 stuffcharReadbuff('l');
3338 return;
3339 }
3340
3341 /*
3342 * For special registers '%' (file name), '#' (alternate file name) and
3343 * ':' (last command line), etc. we have to create a fake yank register.
3344 */
3345 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3346 {
3347 if (insert_string == NULL)
3348 return;
3349 }
3350
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003351#ifdef FEAT_AUTOCMD
3352 /* Autocommands may be executed when saving lines for undo, which may make
3353 * y_array invalid. Start undo now to avoid that. */
3354 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3355#endif
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (insert_string != NULL)
3358 {
3359 y_type = MCHAR;
3360#ifdef FEAT_EVAL
3361 if (regname == '=')
3362 {
3363 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003364 * characters.
3365 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 for (;;)
3367 {
3368 y_size = 0;
3369 ptr = insert_string;
3370 while (ptr != NULL)
3371 {
3372 if (y_array != NULL)
3373 y_array[y_size] = ptr;
3374 ++y_size;
3375 ptr = vim_strchr(ptr, '\n');
3376 if (ptr != NULL)
3377 {
3378 if (y_array != NULL)
3379 *ptr = NUL;
3380 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003381 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (*ptr == NUL)
3383 {
3384 y_type = MLINE;
3385 break;
3386 }
3387 }
3388 }
3389 if (y_array != NULL)
3390 break;
3391 y_array = (char_u **)alloc((unsigned)
3392 (y_size * sizeof(char_u *)));
3393 if (y_array == NULL)
3394 goto end;
3395 }
3396 }
3397 else
3398#endif
3399 {
3400 y_size = 1; /* use fake one-line yank register */
3401 y_array = &insert_string;
3402 }
3403 }
3404 else
3405 {
3406 get_yank_register(regname, FALSE);
3407
3408 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 y_size = y_current->y_size;
3411 y_array = y_current->y_array;
3412 }
3413
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (y_type == MLINE)
3415 {
3416 if (flags & PUT_LINE_SPLIT)
3417 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003418 char_u *p;
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 /* "p" or "P" in Visual mode: split the lines to put the text in
3421 * between. */
3422 if (u_save_cursor() == FAIL)
3423 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003424 p = ml_get_cursor();
3425 if (dir == FORWARD && *p != NUL)
3426 mb_ptr_adv(p);
3427 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (ptr == NULL)
3429 goto end;
3430 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3431 vim_free(ptr);
3432
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003433 oldp = ml_get_curline();
3434 p = oldp + curwin->w_cursor.col;
3435 if (dir == FORWARD && *p != NUL)
3436 mb_ptr_adv(p);
3437 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (ptr == NULL)
3439 goto end;
3440 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3441 ++nr_lines;
3442 dir = FORWARD;
3443 }
3444 if (flags & PUT_LINE_FORWARD)
3445 {
3446 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003447 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 dir = FORWARD;
3449 }
3450 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3451 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3455 y_type = MLINE;
3456
3457 if (y_size == 0 || y_array == NULL)
3458 {
3459 EMSG2(_("E353: Nothing in register %s"),
3460 regname == 0 ? (char_u *)"\"" : transchar(regname));
3461 goto end;
3462 }
3463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (y_type == MBLOCK)
3465 {
3466 lnum = curwin->w_cursor.lnum + y_size + 1;
3467 if (lnum > curbuf->b_ml.ml_line_count)
3468 lnum = curbuf->b_ml.ml_line_count + 1;
3469 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3470 goto end;
3471 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003472 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 lnum = curwin->w_cursor.lnum;
3475#ifdef FEAT_FOLDING
3476 /* Correct line number for closed fold. Don't move the cursor yet,
3477 * u_save() uses it. */
3478 if (dir == BACKWARD)
3479 (void)hasFolding(lnum, &lnum, NULL);
3480 else
3481 (void)hasFolding(lnum, NULL, &lnum);
3482#endif
3483 if (dir == FORWARD)
3484 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003485 /* In an empty buffer the empty line is going to be replaced, include
3486 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003487 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 goto end;
3489#ifdef FEAT_FOLDING
3490 if (dir == FORWARD)
3491 curwin->w_cursor.lnum = lnum - 1;
3492 else
3493 curwin->w_cursor.lnum = lnum;
3494 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3495#endif
3496 }
3497 else if (u_save_cursor() == FAIL)
3498 goto end;
3499
3500 yanklen = (int)STRLEN(y_array[0]);
3501
3502#ifdef FEAT_VIRTUALEDIT
3503 if (ve_flags == VE_ALL && y_type == MCHAR)
3504 {
3505 if (gchar_cursor() == TAB)
3506 {
3507 /* Don't need to insert spaces when "p" on the last position of a
3508 * tab or "P" on the first position. */
3509 if (dir == FORWARD
3510 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3511 : curwin->w_cursor.coladd > 0)
3512 coladvance_force(getviscol());
3513 else
3514 curwin->w_cursor.coladd = 0;
3515 }
3516 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3517 coladvance_force(getviscol() + (dir == FORWARD));
3518 }
3519#endif
3520
3521 lnum = curwin->w_cursor.lnum;
3522 col = curwin->w_cursor.col;
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /*
3525 * Block mode
3526 */
3527 if (y_type == MBLOCK)
3528 {
3529 char c = gchar_cursor();
3530 colnr_T endcol2 = 0;
3531
3532 if (dir == FORWARD && c != NUL)
3533 {
3534#ifdef FEAT_VIRTUALEDIT
3535 if (ve_flags == VE_ALL)
3536 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3537 else
3538#endif
3539 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3540
3541#ifdef FEAT_MBYTE
3542 if (has_mbyte)
3543 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003544 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else
3546#endif
3547#ifdef FEAT_VIRTUALEDIT
3548 if (c != TAB || ve_flags != VE_ALL)
3549#endif
3550 ++curwin->w_cursor.col;
3551 ++col;
3552 }
3553 else
3554 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3555
3556#ifdef FEAT_VIRTUALEDIT
3557 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003558 if (ve_flags == VE_ALL
3559 && (curwin->w_cursor.coladd > 0
3560 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 if (dir == FORWARD && c == NUL)
3563 ++col;
3564 if (dir != FORWARD && c != NUL)
3565 ++curwin->w_cursor.col;
3566 if (c == TAB)
3567 {
3568 if (dir == BACKWARD && curwin->w_cursor.col)
3569 curwin->w_cursor.col--;
3570 if (dir == FORWARD && col - 1 == endcol2)
3571 curwin->w_cursor.col++;
3572 }
3573 }
3574 curwin->w_cursor.coladd = 0;
3575#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003576 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 for (i = 0; i < y_size; ++i)
3578 {
3579 int spaces;
3580 char shortline;
3581
3582 bd.startspaces = 0;
3583 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 vcol = 0;
3585 delcount = 0;
3586
3587 /* add a new line */
3588 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3589 {
3590 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3591 (colnr_T)1, FALSE) == FAIL)
3592 break;
3593 ++nr_lines;
3594 }
3595 /* get the old line and advance to the position to insert at */
3596 oldp = ml_get_curline();
3597 oldlen = (int)STRLEN(oldp);
3598 for (ptr = oldp; vcol < col && *ptr; )
3599 {
3600 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003601 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 vcol += incr;
3603 }
3604 bd.textcol = (colnr_T)(ptr - oldp);
3605
3606 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3607
3608 if (vcol < col) /* line too short, padd with spaces */
3609 bd.startspaces = col - vcol;
3610 else if (vcol > col)
3611 {
3612 bd.endspaces = vcol - col;
3613 bd.startspaces = incr - bd.endspaces;
3614 --bd.textcol;
3615 delcount = 1;
3616#ifdef FEAT_MBYTE
3617 if (has_mbyte)
3618 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3619#endif
3620 if (oldp[bd.textcol] != TAB)
3621 {
3622 /* Only a Tab can be split into spaces. Other
3623 * characters will have to be moved to after the
3624 * block, causing misalignment. */
3625 delcount = 0;
3626 bd.endspaces = 0;
3627 }
3628 }
3629
3630 yanklen = (int)STRLEN(y_array[i]);
3631
3632 /* calculate number of spaces required to fill right side of block*/
3633 spaces = y_width + 1;
3634 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003635 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (spaces < 0)
3637 spaces = 0;
3638
3639 /* insert the new text */
3640 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3641 newp = alloc_check((unsigned)totlen + oldlen + 1);
3642 if (newp == NULL)
3643 break;
3644 /* copy part up to cursor to new line */
3645 ptr = newp;
3646 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3647 ptr += bd.textcol;
3648 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003649 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 ptr += bd.startspaces;
3651 /* insert the new text */
3652 for (j = 0; j < count; ++j)
3653 {
3654 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3655 ptr += yanklen;
3656
3657 /* insert block's trailing spaces only if there's text behind */
3658 if ((j < count - 1 || !shortline) && spaces)
3659 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003660 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 ptr += spaces;
3662 }
3663 }
3664 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003665 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 ptr += bd.endspaces;
3667 /* move the text after the cursor to the end of the line. */
3668 mch_memmove(ptr, oldp + bd.textcol + delcount,
3669 (size_t)(oldlen - bd.textcol - delcount + 1));
3670 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3671
3672 ++curwin->w_cursor.lnum;
3673 if (i == 0)
3674 curwin->w_cursor.col += bd.startspaces;
3675 }
3676
3677 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3678
3679 /* Set '[ mark. */
3680 curbuf->b_op_start = curwin->w_cursor;
3681 curbuf->b_op_start.lnum = lnum;
3682
3683 /* adjust '] mark */
3684 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3685 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003686# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (flags & PUT_CURSEND)
3690 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003691 colnr_T len;
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 curwin->w_cursor = curbuf->b_op_end;
3694 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003695
3696 /* in Insert mode we might be after the NUL, correct for that */
3697 len = (colnr_T)STRLEN(ml_get_curline());
3698 if (curwin->w_cursor.col > len)
3699 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 else
3702 curwin->w_cursor.lnum = lnum;
3703 }
3704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
3706 /*
3707 * Character or Line mode
3708 */
3709 if (y_type == MCHAR)
3710 {
3711 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3712 * char */
3713 if (dir == FORWARD && gchar_cursor() != NUL)
3714 {
3715#ifdef FEAT_MBYTE
3716 if (has_mbyte)
3717 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003718 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /* put it on the next of the multi-byte character. */
3721 col += bytelen;
3722 if (yanklen)
3723 {
3724 curwin->w_cursor.col += bytelen;
3725 curbuf->b_op_end.col += bytelen;
3726 }
3727 }
3728 else
3729#endif
3730 {
3731 ++col;
3732 if (yanklen)
3733 {
3734 ++curwin->w_cursor.col;
3735 ++curbuf->b_op_end.col;
3736 }
3737 }
3738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 curbuf->b_op_start = curwin->w_cursor;
3740 }
3741 /*
3742 * Line mode: BACKWARD is the same as FORWARD on the previous line
3743 */
3744 else if (dir == BACKWARD)
3745 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003746 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 /*
3749 * simple case: insert into current line
3750 */
3751 if (y_type == MCHAR && y_size == 1)
3752 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003753 do {
3754 totlen = count * yanklen;
3755 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003757 oldp = ml_get(lnum);
3758 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3759 if (newp == NULL)
3760 goto end; /* alloc() gave an error message */
3761 mch_memmove(newp, oldp, (size_t)col);
3762 ptr = newp + col;
3763 for (i = 0; i < count; ++i)
3764 {
3765 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3766 ptr += yanklen;
3767 }
3768 STRMOVE(ptr, oldp + col);
3769 ml_replace(lnum, newp, FALSE);
3770 /* Place cursor on last putted char. */
3771 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003772 {
3773 /* make sure curwin->w_virtcol is updated */
3774 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003775 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003778 if (VIsual_active)
3779 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003780 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003781
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003782 if (VIsual_active) /* reset lnum to the last visual line */
3783 lnum--;
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 curbuf->b_op_end = curwin->w_cursor;
3786 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3787 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3788 ++curwin->w_cursor.col;
3789 changed_bytes(lnum, col);
3790 }
3791 else
3792 {
3793 /*
3794 * Insert at least one line. When y_type is MCHAR, break the first
3795 * line in two.
3796 */
3797 for (cnt = 1; cnt <= count; ++cnt)
3798 {
3799 i = 0;
3800 if (y_type == MCHAR)
3801 {
3802 /*
3803 * Split the current line in two at the insert position.
3804 * First insert y_array[size - 1] in front of second line.
3805 * Then append y_array[0] to first line.
3806 */
3807 lnum = new_cursor.lnum;
3808 ptr = ml_get(lnum) + col;
3809 totlen = (int)STRLEN(y_array[y_size - 1]);
3810 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3811 if (newp == NULL)
3812 goto error;
3813 STRCPY(newp, y_array[y_size - 1]);
3814 STRCAT(newp, ptr);
3815 /* insert second line */
3816 ml_append(lnum, newp, (colnr_T)0, FALSE);
3817 vim_free(newp);
3818
3819 oldp = ml_get(lnum);
3820 newp = alloc_check((unsigned)(col + yanklen + 1));
3821 if (newp == NULL)
3822 goto error;
3823 /* copy first part of line */
3824 mch_memmove(newp, oldp, (size_t)col);
3825 /* append to first line */
3826 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3827 ml_replace(lnum, newp, FALSE);
3828
3829 curwin->w_cursor.lnum = lnum;
3830 i = 1;
3831 }
3832
3833 for (; i < y_size; ++i)
3834 {
3835 if ((y_type != MCHAR || i < y_size - 1)
3836 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3837 == FAIL)
3838 goto error;
3839 lnum++;
3840 ++nr_lines;
3841 if (flags & PUT_FIXINDENT)
3842 {
3843 old_pos = curwin->w_cursor;
3844 curwin->w_cursor.lnum = lnum;
3845 ptr = ml_get(lnum);
3846 if (cnt == count && i == y_size - 1)
3847 lendiff = (int)STRLEN(ptr);
3848#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3849 if (*ptr == '#' && preprocs_left())
3850 indent = 0; /* Leave # lines at start */
3851 else
3852#endif
3853 if (*ptr == NUL)
3854 indent = 0; /* Ignore empty lines */
3855 else if (first_indent)
3856 {
3857 indent_diff = orig_indent - get_indent();
3858 indent = orig_indent;
3859 first_indent = FALSE;
3860 }
3861 else if ((indent = get_indent() + indent_diff) < 0)
3862 indent = 0;
3863 (void)set_indent(indent, 0);
3864 curwin->w_cursor = old_pos;
3865 /* remember how many chars were removed */
3866 if (cnt == count && i == y_size - 1)
3867 lendiff -= (int)STRLEN(ml_get(lnum));
3868 }
3869 }
3870 }
3871
3872error:
3873 /* Adjust marks. */
3874 if (y_type == MLINE)
3875 {
3876 curbuf->b_op_start.col = 0;
3877 if (dir == FORWARD)
3878 curbuf->b_op_start.lnum++;
3879 }
3880 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3881 (linenr_T)MAXLNUM, nr_lines, 0L);
3882
3883 /* note changed text for displaying and folding */
3884 if (y_type == MCHAR)
3885 changed_lines(curwin->w_cursor.lnum, col,
3886 curwin->w_cursor.lnum + 1, nr_lines);
3887 else
3888 changed_lines(curbuf->b_op_start.lnum, 0,
3889 curbuf->b_op_start.lnum, nr_lines);
3890
3891 /* put '] mark at last inserted character */
3892 curbuf->b_op_end.lnum = lnum;
3893 /* correct length for change in indent */
3894 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3895 if (col > 1)
3896 curbuf->b_op_end.col = col - 1;
3897 else
3898 curbuf->b_op_end.col = 0;
3899
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003900 if (flags & PUT_CURSLINE)
3901 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003902 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003903 curwin->w_cursor.lnum = lnum;
3904 beginline(BL_WHITE | BL_FIX);
3905 }
3906 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 /* put cursor after inserted text */
3909 if (y_type == MLINE)
3910 {
3911 if (lnum >= curbuf->b_ml.ml_line_count)
3912 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3913 else
3914 curwin->w_cursor.lnum = lnum + 1;
3915 curwin->w_cursor.col = 0;
3916 }
3917 else
3918 {
3919 curwin->w_cursor.lnum = lnum;
3920 curwin->w_cursor.col = col;
3921 }
3922 }
3923 else if (y_type == MLINE)
3924 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003925 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 curwin->w_cursor.col = 0;
3927 if (dir == FORWARD)
3928 ++curwin->w_cursor.lnum;
3929 beginline(BL_WHITE | BL_FIX);
3930 }
3931 else /* put cursor on first inserted character */
3932 curwin->w_cursor = new_cursor;
3933 }
3934 }
3935
3936 msgmore(nr_lines);
3937 curwin->w_set_curswant = TRUE;
3938
3939end:
3940 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003942 if (regname == '=')
3943 vim_free(y_array);
3944
Bram Moolenaar033d8882013-09-25 23:24:57 +02003945 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003946
Bram Moolenaar677ee682005-01-27 14:41:15 +00003947 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003948 adjust_cursor_eol();
3949}
3950
3951/*
3952 * When the cursor is on the NUL past the end of the line and it should not be
3953 * there move it left.
3954 */
3955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003956adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003957{
3958 if (curwin->w_cursor.col > 0
3959 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003960#ifdef FEAT_VIRTUALEDIT
3961 && (ve_flags & VE_ONEMORE) == 0
3962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 && !(restart_edit || (State & INSERT)))
3964 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003965 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003966 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968#ifdef FEAT_VIRTUALEDIT
3969 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003970 {
3971 colnr_T scol, ecol;
3972
3973 /* Coladd is set to the width of the last character. */
3974 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3975 curwin->w_cursor.coladd = ecol - scol + 1;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977#endif
3978 }
3979}
3980
3981#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3982/*
3983 * Return TRUE if lines starting with '#' should be left aligned.
3984 */
3985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003986preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 return
3989# ifdef FEAT_SMARTINDENT
3990# ifdef FEAT_CINDENT
3991 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3992# else
3993 curbuf->b_p_si
3994# endif
3995# endif
3996# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01003997 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
3998 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999# endif
4000 ;
4001}
4002#endif
4003
4004/* Return the character name of the register with the given number */
4005 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004006get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
4008 if (num == -1)
4009 return '"';
4010 else if (num < 10)
4011 return num + '0';
4012 else if (num == DELETION_REGISTER)
4013 return '-';
4014#ifdef FEAT_CLIPBOARD
4015 else if (num == STAR_REGISTER)
4016 return '*';
4017 else if (num == PLUS_REGISTER)
4018 return '+';
4019#endif
4020 else
4021 {
4022#ifdef EBCDIC
4023 int i;
4024
4025 /* EBCDIC is really braindead ... */
4026 i = 'a' + (num - 10);
4027 if (i > 'i')
4028 i += 7;
4029 if (i > 'r')
4030 i += 8;
4031 return i;
4032#else
4033 return num + 'a' - 10;
4034#endif
4035 }
4036}
4037
4038/*
4039 * ":dis" and ":registers": Display the contents of the yank registers.
4040 */
4041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004042ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 int i, n;
4045 long j;
4046 char_u *p;
4047 struct yankreg *yb;
4048 int name;
4049 int attr;
4050 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004051#ifdef FEAT_MBYTE
4052 int clen;
4053#else
4054# define clen 1
4055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 if (arg != NULL && *arg == NUL)
4058 arg = NULL;
4059 attr = hl_attr(HLF_8);
4060
4061 /* Highlight title */
4062 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4063 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4064 {
4065 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004066 if (arg != NULL && vim_strchr(arg, name) == NULL
4067#ifdef ONE_CLIPBOARD
4068 /* Star register and plus register contain the same thing. */
4069 && (name != '*' || vim_strchr(arg, '+') == NULL)
4070#endif
4071 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 continue; /* did not ask for this register */
4073
4074#ifdef FEAT_CLIPBOARD
4075 /* Adjust register name for "unnamed" in 'clipboard'.
4076 * When it's a clipboard register, fill it with the current contents
4077 * of the clipboard. */
4078 adjust_clip_reg(&name);
4079 (void)may_get_selection(name);
4080#endif
4081
4082 if (i == -1)
4083 {
4084 if (y_previous != NULL)
4085 yb = y_previous;
4086 else
4087 yb = &(y_regs[0]);
4088 }
4089 else
4090 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004091
4092#ifdef FEAT_EVAL
4093 if (name == MB_TOLOWER(redir_reg)
4094 || (redir_reg == '"' && yb == y_previous))
4095 continue; /* do not list register being written to, the
4096 * pointer can be freed */
4097#endif
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 if (yb->y_array != NULL)
4100 {
4101 msg_putchar('\n');
4102 msg_putchar('"');
4103 msg_putchar(name);
4104 MSG_PUTS(" ");
4105
4106 n = (int)Columns - 6;
4107 for (j = 0; j < yb->y_size && n > 1; ++j)
4108 {
4109 if (j)
4110 {
4111 MSG_PUTS_ATTR("^J", attr);
4112 n -= 2;
4113 }
4114 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004117 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004118#endif
4119 msg_outtrans_len(p, clen);
4120#ifdef FEAT_MBYTE
4121 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122#endif
4123 }
4124 }
4125 if (n > 1 && yb->y_type == MLINE)
4126 MSG_PUTS_ATTR("^J", attr);
4127 out_flush(); /* show one line at a time */
4128 }
4129 ui_breakcheck();
4130 }
4131
4132 /*
4133 * display last inserted text
4134 */
4135 if ((p = get_last_insert()) != NULL
4136 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4137 {
4138 MSG_PUTS("\n\". ");
4139 dis_msg(p, TRUE);
4140 }
4141
4142 /*
4143 * display last command line
4144 */
4145 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4146 && !got_int)
4147 {
4148 MSG_PUTS("\n\": ");
4149 dis_msg(last_cmdline, FALSE);
4150 }
4151
4152 /*
4153 * display current file name
4154 */
4155 if (curbuf->b_fname != NULL
4156 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4157 {
4158 MSG_PUTS("\n\"% ");
4159 dis_msg(curbuf->b_fname, FALSE);
4160 }
4161
4162 /*
4163 * display alternate file name
4164 */
4165 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4166 {
4167 char_u *fname;
4168 linenr_T dummy;
4169
4170 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4171 {
4172 MSG_PUTS("\n\"# ");
4173 dis_msg(fname, FALSE);
4174 }
4175 }
4176
4177 /*
4178 * display last search pattern
4179 */
4180 if (last_search_pat() != NULL
4181 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4182 {
4183 MSG_PUTS("\n\"/ ");
4184 dis_msg(last_search_pat(), FALSE);
4185 }
4186
4187#ifdef FEAT_EVAL
4188 /*
4189 * display last used expression
4190 */
4191 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4192 && !got_int)
4193 {
4194 MSG_PUTS("\n\"= ");
4195 dis_msg(expr_line, FALSE);
4196 }
4197#endif
4198}
4199
4200/*
4201 * display a string for do_dis()
4202 * truncate at end of screen line
4203 */
4204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205dis_msg(
4206 char_u *p,
4207 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 int n;
4210#ifdef FEAT_MBYTE
4211 int l;
4212#endif
4213
4214 n = (int)Columns - 6;
4215 while (*p != NUL
4216 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4217 && (n -= ptr2cells(p)) >= 0)
4218 {
4219#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004220 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 msg_outtrans_len(p, l);
4223 p += l;
4224 }
4225 else
4226#endif
4227 msg_outtrans_len(p++, 1);
4228 }
4229 ui_breakcheck();
4230}
4231
Bram Moolenaar81340392012-06-06 16:12:59 +02004232#if defined(FEAT_COMMENTS) || defined(PROTO)
4233/*
4234 * If "process" is TRUE and the line begins with a comment leader (possibly
4235 * after some white space), return a pointer to the text after it. Put a boolean
4236 * value indicating whether the line ends with an unclosed comment in
4237 * "is_comment".
4238 * line - line to be processed,
4239 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004240 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004241 * include_space - whether to also skip space following the comment leader,
4242 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004243 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004244 */
4245 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004246skip_comment(
4247 char_u *line,
4248 int process,
4249 int include_space,
4250 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004251{
4252 char_u *comment_flags = NULL;
4253 int lead_len;
4254 int leader_offset = get_last_leader_offset(line, &comment_flags);
4255
4256 *is_comment = FALSE;
4257 if (leader_offset != -1)
4258 {
4259 /* Let's check whether the line ends with an unclosed comment.
4260 * If the last comment leader has COM_END in flags, there's no comment.
4261 */
4262 while (*comment_flags)
4263 {
4264 if (*comment_flags == COM_END
4265 || *comment_flags == ':')
4266 break;
4267 ++comment_flags;
4268 }
4269 if (*comment_flags != COM_END)
4270 *is_comment = TRUE;
4271 }
4272
4273 if (process == FALSE)
4274 return line;
4275
4276 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4277
4278 if (lead_len == 0)
4279 return line;
4280
4281 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004282 * - COM_END,
4283 * - colon,
4284 * whichever comes first.
4285 */
4286 while (*comment_flags)
4287 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004288 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004289 || *comment_flags == ':')
4290 {
4291 break;
4292 }
4293 ++comment_flags;
4294 }
4295
4296 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004297 * starting with a closing part of a three-part comment. That's good,
4298 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004299 */
4300 if (*comment_flags == ':' || *comment_flags == NUL)
4301 line += lead_len;
4302
4303 return line;
4304}
4305#endif
4306
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004308 * Join 'count' lines (minimal 2) at cursor position.
4309 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004310 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4311 * leaders should not be removed.
4312 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4313 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004315 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 */
4317 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004318do_join(
4319 long count,
4320 int insert_space,
4321 int save_undo,
4322 int use_formatoptions UNUSED,
4323 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004325 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004326 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004327 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004329 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004330 int endcurr1 = NUL;
4331 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004332 int currsize = 0; /* size of the current line */
4333 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004335 colnr_T col = 0;
4336 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004337#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004338 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004339 int remove_comments = (use_formatoptions == TRUE)
4340 && has_format_option(FO_REMOVE_COMS);
4341 int prev_was_comment;
4342#endif
4343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004345 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4346 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004349 /* Allocate an array to store the number of spaces inserted before each
4350 * line. We will use it to pre-compute the length of the new line and the
4351 * proper placement of each original line in the new one. */
4352 spaces = lalloc_clear((long_u)count, TRUE);
4353 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004355#if defined(FEAT_COMMENTS) || defined(PROTO)
4356 if (remove_comments)
4357 {
4358 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4359 if (comments == NULL)
4360 {
4361 vim_free(spaces);
4362 return FAIL;
4363 }
4364 }
4365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004368 * Don't move anything, just compute the final line length
4369 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004371 for (t = 0; t < count; ++t)
4372 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004373 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004374 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004375 {
4376 /* Set the '[ mark. */
4377 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4378 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4379 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004380#if defined(FEAT_COMMENTS) || defined(PROTO)
4381 if (remove_comments)
4382 {
4383 /* We don't want to remove the comment leader if the
4384 * previous line is not a comment. */
4385 if (t > 0 && prev_was_comment)
4386 {
4387
4388 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4389 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004390 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004391 curr = new_curr;
4392 }
4393 else
4394 curr = skip_comment(curr, FALSE, insert_space,
4395 &prev_was_comment);
4396 }
4397#endif
4398
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004399 if (insert_space && t > 0)
4400 {
4401 curr = skipwhite(curr);
4402 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4403#ifdef FEAT_MBYTE
4404 && (!has_format_option(FO_MBYTE_JOIN)
4405 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4406 && (!has_format_option(FO_MBYTE_JOIN2)
4407 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4408#endif
4409 )
4410 {
4411 /* don't add a space if the line is ending in a space */
4412 if (endcurr1 == ' ')
4413 endcurr1 = endcurr2;
4414 else
4415 ++spaces[t];
4416 /* extra space when 'joinspaces' set and line ends in '.' */
4417 if ( p_js
4418 && (endcurr1 == '.'
4419 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4420 && (endcurr1 == '?' || endcurr1 == '!'))))
4421 ++spaces[t];
4422 }
4423 }
4424 currsize = (int)STRLEN(curr);
4425 sumsize += currsize + spaces[t];
4426 endcurr1 = endcurr2 = NUL;
4427 if (insert_space && currsize > 0)
4428 {
4429#ifdef FEAT_MBYTE
4430 if (has_mbyte)
4431 {
4432 cend = curr + currsize;
4433 mb_ptr_back(curr, cend);
4434 endcurr1 = (*mb_ptr2char)(cend);
4435 if (cend > curr)
4436 {
4437 mb_ptr_back(curr, cend);
4438 endcurr2 = (*mb_ptr2char)(cend);
4439 }
4440 }
4441 else
4442#endif
4443 {
4444 endcurr1 = *(curr + currsize - 1);
4445 if (currsize > 1)
4446 endcurr2 = *(curr + currsize - 2);
4447 }
4448 }
4449 line_breakcheck();
4450 if (got_int)
4451 {
4452 ret = FAIL;
4453 goto theend;
4454 }
4455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004457 /* store the column position before last line */
4458 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004460 /* allocate the space for the new line */
4461 newp = alloc_check((unsigned)(sumsize + 1));
4462 cend = newp + sumsize;
4463 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004465 /*
4466 * Move affected lines to the new long one.
4467 *
4468 * Move marks from each deleted line to the joined line, adjusting the
4469 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4470 * should not really be a problem.
4471 */
4472 for (t = count - 1; ; --t)
4473 {
4474 cend -= currsize;
4475 mch_memmove(cend, curr, (size_t)currsize);
4476 if (spaces[t] > 0)
4477 {
4478 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004479 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004480 }
4481 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004482 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004483 if (t == 0)
4484 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004485 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004486#if defined(FEAT_COMMENTS) || defined(PROTO)
4487 if (remove_comments)
4488 curr += comments[t - 1];
4489#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004490 if (insert_space && t > 1)
4491 curr = skipwhite(curr);
4492 currsize = (int)STRLEN(curr);
4493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4495
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004496 if (setmark)
4497 {
4498 /* Set the '] mark. */
4499 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4500 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4501 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* Only report the change in the first line here, del_lines() will report
4504 * the deleted line. */
4505 changed_lines(curwin->w_cursor.lnum, currsize,
4506 curwin->w_cursor.lnum + 1, 0L);
4507
4508 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004509 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 * briefly, and then move it back. After del_lines() the cursor may
4511 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 */
4513 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004515 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 curwin->w_cursor.lnum = t;
4517
4518 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004519 * Set the cursor column:
4520 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004521 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004523 curwin->w_cursor.col =
4524 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef FEAT_VIRTUALEDIT
4528 curwin->w_cursor.coladd = 0;
4529#endif
4530 curwin->w_set_curswant = TRUE;
4531
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004532theend:
4533 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004534#if defined(FEAT_COMMENTS) || defined(PROTO)
4535 if (remove_comments)
4536 vim_free(comments);
4537#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
4541#ifdef FEAT_COMMENTS
4542/*
4543 * Return TRUE if the two comment leaders given are the same. "lnum" is
4544 * the first line. White-space is ignored. Note that the whole of
4545 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4546 */
4547 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004548same_leader(
4549 linenr_T lnum,
4550 int leader1_len,
4551 char_u *leader1_flags,
4552 int leader2_len,
4553 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 int idx1 = 0, idx2 = 0;
4556 char_u *p;
4557 char_u *line1;
4558 char_u *line2;
4559
4560 if (leader1_len == 0)
4561 return (leader2_len == 0);
4562
4563 /*
4564 * If first leader has 'f' flag, the lines can be joined only if the
4565 * second line does not have a leader.
4566 * If first leader has 'e' flag, the lines can never be joined.
4567 * If fist leader has 's' flag, the lines can only be joined if there is
4568 * some text after it and the second line has the 'm' flag.
4569 */
4570 if (leader1_flags != NULL)
4571 {
4572 for (p = leader1_flags; *p && *p != ':'; ++p)
4573 {
4574 if (*p == COM_FIRST)
4575 return (leader2_len == 0);
4576 if (*p == COM_END)
4577 return FALSE;
4578 if (*p == COM_START)
4579 {
4580 if (*(ml_get(lnum) + leader1_len) == NUL)
4581 return FALSE;
4582 if (leader2_flags == NULL || leader2_len == 0)
4583 return FALSE;
4584 for (p = leader2_flags; *p && *p != ':'; ++p)
4585 if (*p == COM_MIDDLE)
4586 return TRUE;
4587 return FALSE;
4588 }
4589 }
4590 }
4591
4592 /*
4593 * Get current line and next line, compare the leaders.
4594 * The first line has to be saved, only one line can be locked at a time.
4595 */
4596 line1 = vim_strsave(ml_get(lnum));
4597 if (line1 != NULL)
4598 {
4599 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4600 ;
4601 line2 = ml_get(lnum + 1);
4602 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4603 {
4604 if (!vim_iswhite(line2[idx2]))
4605 {
4606 if (line1[idx1++] != line2[idx2])
4607 break;
4608 }
4609 else
4610 while (vim_iswhite(line1[idx1]))
4611 ++idx1;
4612 }
4613 vim_free(line1);
4614 }
4615 return (idx2 == leader2_len && idx1 == leader1_len);
4616}
4617#endif
4618
4619/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004620 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 */
4622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004623op_format(
4624 oparg_T *oap,
4625 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 long old_line_count = curbuf->b_ml.ml_line_count;
4628
4629 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4630 * can put it back there. */
4631 curwin->w_cursor = oap->cursor_start;
4632
4633 if (u_save((linenr_T)(oap->start.lnum - 1),
4634 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4635 return;
4636 curwin->w_cursor = oap->start;
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (oap->is_VIsual)
4639 /* When there is no change: need to remove the Visual selection */
4640 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /* Set '[ mark at the start of the formatted area */
4643 curbuf->b_op_start = oap->start;
4644
4645 /* For "gw" remember the cursor position and put it back below (adjusted
4646 * for joined and split lines). */
4647 if (keep_cursor)
4648 saved_cursor = oap->cursor_start;
4649
Bram Moolenaar81a82092008-03-12 16:27:00 +00004650 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 /*
4653 * Leave the cursor at the first non-blank of the last formatted line.
4654 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4655 * line, so "." will do the next lines.
4656 */
4657 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4658 ++curwin->w_cursor.lnum;
4659 beginline(BL_WHITE | BL_FIX);
4660 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4661 msgmore(old_line_count);
4662
4663 /* put '] mark on the end of the formatted area */
4664 curbuf->b_op_end = curwin->w_cursor;
4665
4666 if (keep_cursor)
4667 {
4668 curwin->w_cursor = saved_cursor;
4669 saved_cursor.lnum = 0;
4670 }
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (oap->is_VIsual)
4673 {
4674 win_T *wp;
4675
4676 FOR_ALL_WINDOWS(wp)
4677 {
4678 if (wp->w_old_cursor_lnum != 0)
4679 {
4680 /* When lines have been inserted or deleted, adjust the end of
4681 * the Visual area to be redrawn. */
4682 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4683 wp->w_old_cursor_lnum += old_line_count;
4684 else
4685 wp->w_old_visual_lnum += old_line_count;
4686 }
4687 }
4688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689}
4690
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004691#if defined(FEAT_EVAL) || defined(PROTO)
4692/*
4693 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4694 */
4695 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004696op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004697{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004698 if (oap->is_VIsual)
4699 /* When there is no change: need to remove the Visual selection */
4700 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004701
Bram Moolenaar700303e2010-07-11 17:35:50 +02004702 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4703 /* As documented: when 'formatexpr' returns non-zero fall back to
4704 * internal formatting. */
4705 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004706}
4707
4708 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004709fex_format(
4710 linenr_T lnum,
4711 long count,
4712 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004713{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004714 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4715 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004716 int r;
4717
4718 /*
4719 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004720 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004721 */
4722 set_vim_var_nr(VV_LNUM, lnum);
4723 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004724 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004725
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004726 /*
4727 * Evaluate the function.
4728 */
4729 if (use_sandbox)
4730 ++sandbox;
4731 r = eval_to_number(curbuf->b_p_fex);
4732 if (use_sandbox)
4733 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004734
4735 set_vim_var_string(VV_CHAR, NULL, -1);
4736
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004737 return r;
4738}
4739#endif
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741/*
4742 * Format "line_count" lines, starting at the cursor position.
4743 * When "line_count" is negative, format until the end of the paragraph.
4744 * Lines after the cursor line are saved for undo, caller must have saved the
4745 * first line.
4746 */
4747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748format_lines(
4749 linenr_T line_count,
4750 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 int max_len;
4753 int is_not_par; /* current line not part of parag. */
4754 int next_is_not_par; /* next line not part of paragraph */
4755 int is_end_par; /* at end of paragraph */
4756 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4757 int next_is_start_par = FALSE;
4758#ifdef FEAT_COMMENTS
4759 int leader_len = 0; /* leader len of current line */
4760 int next_leader_len; /* leader len of next line */
4761 char_u *leader_flags = NULL; /* flags for leader of current line */
4762 char_u *next_leader_flags; /* flags for leader of next line */
4763 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004764 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#endif
4766 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004767 int second_indent = -1; /* indent for second line (comment
4768 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 int do_second_indent;
4770 int do_number_indent;
4771 int do_trail_white;
4772 int first_par_line = TRUE;
4773 int smd_save;
4774 long count;
4775 int need_set_indent = TRUE; /* set indent of next paragraph */
4776 int force_format = FALSE;
4777 int old_State = State;
4778
4779 /* length of a line to force formatting: 3 * 'tw' */
4780 max_len = comp_textwidth(TRUE) * 3;
4781
4782 /* check for 'q', '2' and '1' in 'formatoptions' */
4783#ifdef FEAT_COMMENTS
4784 do_comments = has_format_option(FO_Q_COMS);
4785#endif
4786 do_second_indent = has_format_option(FO_Q_SECOND);
4787 do_number_indent = has_format_option(FO_Q_NUMBER);
4788 do_trail_white = has_format_option(FO_WHITE_PAR);
4789
4790 /*
4791 * Get info about the previous and current line.
4792 */
4793 if (curwin->w_cursor.lnum > 1)
4794 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4795#ifdef FEAT_COMMENTS
4796 , &leader_len, &leader_flags, do_comments
4797#endif
4798 );
4799 else
4800 is_not_par = TRUE;
4801 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4802#ifdef FEAT_COMMENTS
4803 , &next_leader_len, &next_leader_flags, do_comments
4804#endif
4805 );
4806 is_end_par = (is_not_par || next_is_not_par);
4807 if (!is_end_par && do_trail_white)
4808 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4809
4810 curwin->w_cursor.lnum--;
4811 for (count = line_count; count != 0 && !got_int; --count)
4812 {
4813 /*
4814 * Advance to next paragraph.
4815 */
4816 if (advance)
4817 {
4818 curwin->w_cursor.lnum++;
4819 prev_is_end_par = is_end_par;
4820 is_not_par = next_is_not_par;
4821#ifdef FEAT_COMMENTS
4822 leader_len = next_leader_len;
4823 leader_flags = next_leader_flags;
4824#endif
4825 }
4826
4827 /*
4828 * The last line to be formatted.
4829 */
4830 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4831 {
4832 next_is_not_par = TRUE;
4833#ifdef FEAT_COMMENTS
4834 next_leader_len = 0;
4835 next_leader_flags = NULL;
4836#endif
4837 }
4838 else
4839 {
4840 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4841#ifdef FEAT_COMMENTS
4842 , &next_leader_len, &next_leader_flags, do_comments
4843#endif
4844 );
4845 if (do_number_indent)
4846 next_is_start_par =
4847 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4848 }
4849 advance = TRUE;
4850 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4851 if (!is_end_par && do_trail_white)
4852 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4853
4854 /*
4855 * Skip lines that are not in a paragraph.
4856 */
4857 if (is_not_par)
4858 {
4859 if (line_count < 0)
4860 break;
4861 }
4862 else
4863 {
4864 /*
4865 * For the first line of a paragraph, check indent of second line.
4866 * Don't do this for comments and empty lines.
4867 */
4868 if (first_par_line
4869 && (do_second_indent || do_number_indent)
4870 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004871 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004873 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4874 {
4875#ifdef FEAT_COMMENTS
4876 if (leader_len == 0 && next_leader_len == 0)
4877 {
4878 /* no comment found */
4879#endif
4880 second_indent =
4881 get_indent_lnum(curwin->w_cursor.lnum + 1);
4882#ifdef FEAT_COMMENTS
4883 }
4884 else
4885 {
4886 second_indent = next_leader_len;
4887 do_comments_list = 1;
4888 }
4889#endif
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004892 {
4893#ifdef FEAT_COMMENTS
4894 if (leader_len == 0 && next_leader_len == 0)
4895 {
4896 /* no comment found */
4897#endif
4898 second_indent =
4899 get_number_indent(curwin->w_cursor.lnum);
4900#ifdef FEAT_COMMENTS
4901 }
4902 else
4903 {
4904 /* get_number_indent() is now "comment aware"... */
4905 second_indent =
4906 get_number_indent(curwin->w_cursor.lnum);
4907 do_comments_list = 1;
4908 }
4909#endif
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912
4913 /*
4914 * When the comment leader changes, it's the end of the paragraph.
4915 */
4916 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4917#ifdef FEAT_COMMENTS
4918 || !same_leader(curwin->w_cursor.lnum,
4919 leader_len, leader_flags,
4920 next_leader_len, next_leader_flags)
4921#endif
4922 )
4923 is_end_par = TRUE;
4924
4925 /*
4926 * If we have got to the end of a paragraph, or the line is
4927 * getting long, format it.
4928 */
4929 if (is_end_par || force_format)
4930 {
4931 if (need_set_indent)
4932 /* replace indent in first line with minimal number of
4933 * tabs and spaces, according to current options */
4934 (void)set_indent(get_indent(), SIN_CHANGED);
4935
4936 /* put cursor on last non-space */
4937 State = NORMAL; /* don't go past end-of-line */
4938 coladvance((colnr_T)MAXCOL);
4939 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4940 dec_cursor();
4941
4942 /* do the formatting, without 'showmode' */
4943 State = INSERT; /* for open_line() */
4944 smd_save = p_smd;
4945 p_smd = FALSE;
4946 insertchar(NUL, INSCHAR_FORMAT
4947#ifdef FEAT_COMMENTS
4948 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004949 + (do_comments && do_comments_list
4950 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004952 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 State = old_State;
4954 p_smd = smd_save;
4955 second_indent = -1;
4956 /* at end of par.: need to set indent of next par. */
4957 need_set_indent = is_end_par;
4958 if (is_end_par)
4959 {
4960 /* When called with a negative line count, break at the
4961 * end of the paragraph. */
4962 if (line_count < 0)
4963 break;
4964 first_par_line = TRUE;
4965 }
4966 force_format = FALSE;
4967 }
4968
4969 /*
4970 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004971 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 */
4973 if (!is_end_par)
4974 {
4975 advance = FALSE;
4976 curwin->w_cursor.lnum++;
4977 curwin->w_cursor.col = 0;
4978 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004982 {
4983 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4985 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004986 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004988 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
4989 {
4990 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01004991 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004992
4993 if (indent > 0)
4994 {
4995 (void)del_bytes(indent, FALSE, FALSE);
4996 mark_col_adjust(curwin->w_cursor.lnum,
4997 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01004998 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005001 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 beep_flush();
5004 break;
5005 }
5006 first_par_line = FALSE;
5007 /* If the line is getting long, format it next time */
5008 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5009 force_format = TRUE;
5010 else
5011 force_format = FALSE;
5012 }
5013 }
5014 line_breakcheck();
5015 }
5016}
5017
5018/*
5019 * Return TRUE if line "lnum" ends in a white character.
5020 */
5021 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005022ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 char_u *s = ml_get(lnum);
5025 size_t l;
5026
5027 if (*s == NUL)
5028 return FALSE;
5029 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5030 * invocation may call function multiple times". */
5031 l = STRLEN(s) - 1;
5032 return vim_iswhite(s[l]);
5033}
5034
5035/*
5036 * Blank lines, and lines containing only the comment leader, are left
5037 * untouched by the formatting. The function returns TRUE in this
5038 * case. It also returns TRUE when a line starts with the end of a comment
5039 * ('e' in comment flags), so that this line is skipped, and not joined to the
5040 * previous line. A new paragraph starts after a blank line, or when the
5041 * comment leader changes -- webb.
5042 */
5043#ifdef FEAT_COMMENTS
5044 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005045fmt_check_par(
5046 linenr_T lnum,
5047 int *leader_len,
5048 char_u **leader_flags,
5049 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 char_u *flags = NULL; /* init for GCC */
5052 char_u *ptr;
5053
5054 ptr = ml_get(lnum);
5055 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005056 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
5058 *leader_len = 0;
5059
5060 if (*leader_len > 0)
5061 {
5062 /*
5063 * Search for 'e' flag in comment leader flags.
5064 */
5065 flags = *leader_flags;
5066 while (*flags && *flags != ':' && *flags != COM_END)
5067 ++flags;
5068 }
5069
5070 return (*skipwhite(ptr + *leader_len) == NUL
5071 || (*leader_len > 0 && *flags == COM_END)
5072 || startPS(lnum, NUL, FALSE));
5073}
5074#else
5075 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005076fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5079}
5080#endif
5081
5082/*
5083 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5084 * previous line is in the same paragraph. Used for auto-formatting.
5085 */
5086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 char_u *p;
5090#ifdef FEAT_COMMENTS
5091 int leader_len = 0; /* leader len of current line */
5092 char_u *leader_flags = NULL; /* flags for leader of current line */
5093 int next_leader_len; /* leader len of next line */
5094 char_u *next_leader_flags; /* flags for leader of next line */
5095 int do_comments; /* format comments */
5096#endif
5097
5098 if (lnum <= 1)
5099 return TRUE; /* start of the file */
5100
5101 p = ml_get(lnum - 1);
5102 if (*p == NUL)
5103 return TRUE; /* after empty line */
5104
5105#ifdef FEAT_COMMENTS
5106 do_comments = has_format_option(FO_Q_COMS);
5107#endif
5108 if (fmt_check_par(lnum - 1
5109#ifdef FEAT_COMMENTS
5110 , &leader_len, &leader_flags, do_comments
5111#endif
5112 ))
5113 return TRUE; /* after non-paragraph line */
5114
5115 if (fmt_check_par(lnum
5116#ifdef FEAT_COMMENTS
5117 , &next_leader_len, &next_leader_flags, do_comments
5118#endif
5119 ))
5120 return TRUE; /* "lnum" is not a paragraph line */
5121
5122 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5123 return TRUE; /* missing trailing space in previous line. */
5124
5125 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5126 return TRUE; /* numbered item starts in "lnum". */
5127
5128#ifdef FEAT_COMMENTS
5129 if (!same_leader(lnum - 1, leader_len, leader_flags,
5130 next_leader_len, next_leader_flags))
5131 return TRUE; /* change of comment leader. */
5132#endif
5133
5134 return FALSE;
5135}
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137/*
5138 * prepare a few things for block mode yank/delete/tilde
5139 *
5140 * for delete:
5141 * - textlen includes the first/last char to be (partly) deleted
5142 * - start/endspaces is the number of columns that are taken by the
5143 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005144 * deleted.
5145 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * - textlen includes the first/last char to be wholly yanked
5147 * - start/endspaces is the number of columns of the first/last yanked char
5148 * that are to be yanked.
5149 */
5150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005151block_prep(
5152 oparg_T *oap,
5153 struct block_def *bdp,
5154 linenr_T lnum,
5155 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
5157 int incr = 0;
5158 char_u *pend;
5159 char_u *pstart;
5160 char_u *line;
5161 char_u *prev_pstart;
5162 char_u *prev_pend;
5163
5164 bdp->startspaces = 0;
5165 bdp->endspaces = 0;
5166 bdp->textlen = 0;
5167 bdp->start_vcol = 0;
5168 bdp->end_vcol = 0;
5169#ifdef FEAT_VISUALEXTRA
5170 bdp->is_short = FALSE;
5171 bdp->is_oneChar = FALSE;
5172 bdp->pre_whitesp = 0;
5173 bdp->pre_whitesp_c = 0;
5174 bdp->end_char_vcols = 0;
5175#endif
5176 bdp->start_char_vcols = 0;
5177
5178 line = ml_get(lnum);
5179 pstart = line;
5180 prev_pstart = line;
5181 while (bdp->start_vcol < oap->start_vcol && *pstart)
5182 {
5183 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005184 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 bdp->start_vcol += incr;
5186#ifdef FEAT_VISUALEXTRA
5187 if (vim_iswhite(*pstart))
5188 {
5189 bdp->pre_whitesp += incr;
5190 bdp->pre_whitesp_c++;
5191 }
5192 else
5193 {
5194 bdp->pre_whitesp = 0;
5195 bdp->pre_whitesp_c = 0;
5196 }
5197#endif
5198 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005199 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 bdp->start_char_vcols = incr;
5202 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5203 {
5204 bdp->end_vcol = bdp->start_vcol;
5205#ifdef FEAT_VISUALEXTRA
5206 bdp->is_short = TRUE;
5207#endif
5208 if (!is_del || oap->op_type == OP_APPEND)
5209 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5210 }
5211 else
5212 {
5213 /* notice: this converts partly selected Multibyte characters to
5214 * spaces, too. */
5215 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5216 if (is_del && bdp->startspaces)
5217 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5218 pend = pstart;
5219 bdp->end_vcol = bdp->start_vcol;
5220 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5221 {
5222#ifdef FEAT_VISUALEXTRA
5223 bdp->is_oneChar = TRUE;
5224#endif
5225 if (oap->op_type == OP_INSERT)
5226 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5227 else if (oap->op_type == OP_APPEND)
5228 {
5229 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5230 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5231 }
5232 else
5233 {
5234 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5235 if (is_del && oap->op_type != OP_LSHIFT)
5236 {
5237 /* just putting the sum of those two into
5238 * bdp->startspaces doesn't work for Visual replace,
5239 * so we have to split the tab in two */
5240 bdp->startspaces = bdp->start_char_vcols
5241 - (bdp->start_vcol - oap->start_vcol);
5242 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5243 }
5244 }
5245 }
5246 else
5247 {
5248 prev_pend = pend;
5249 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5250 {
5251 /* Count a tab for what it's worth (if list mode not on) */
5252 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005253 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 bdp->end_vcol += incr;
5255 }
5256 if (bdp->end_vcol <= oap->end_vcol
5257 && (!is_del
5258 || oap->op_type == OP_APPEND
5259 || oap->op_type == OP_REPLACE)) /* line too short */
5260 {
5261#ifdef FEAT_VISUALEXTRA
5262 bdp->is_short = TRUE;
5263#endif
5264 /* Alternative: include spaces to fill up the block.
5265 * Disadvantage: can lead to trailing spaces when the line is
5266 * short where the text is put */
5267 /* if (!is_del || oap->op_type == OP_APPEND) */
5268 if (oap->op_type == OP_APPEND || virtual_op)
5269 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005270 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else
5272 bdp->endspaces = 0; /* replace doesn't add characters */
5273 }
5274 else if (bdp->end_vcol > oap->end_vcol)
5275 {
5276 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5277 if (!is_del && bdp->endspaces)
5278 {
5279 bdp->endspaces = incr - bdp->endspaces;
5280 if (pend != pstart)
5281 pend = prev_pend;
5282 }
5283 }
5284 }
5285#ifdef FEAT_VISUALEXTRA
5286 bdp->end_char_vcols = incr;
5287#endif
5288 if (is_del && bdp->startspaces)
5289 pstart = prev_pstart;
5290 bdp->textlen = (int)(pend - pstart);
5291 }
5292 bdp->textcol = (colnr_T) (pstart - line);
5293 bdp->textstart = pstart;
5294}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005297 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005300op_addsub(
5301 oparg_T *oap,
5302 linenr_T Prenum1, /* Amount of add/subtract */
5303 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005305 pos_T pos;
5306 struct block_def bd;
5307 int change_cnt = 0;
5308 linenr_T amount = Prenum1;
5309
5310 if (!VIsual_active)
5311 {
5312 pos = curwin->w_cursor;
5313 if (u_save_cursor() == FAIL)
5314 return;
5315 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5316 if (change_cnt)
5317 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5318 }
5319 else
5320 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005321 int one_change;
5322 int length;
5323 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005324
5325 if (u_save((linenr_T)(oap->start.lnum - 1),
5326 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5327 return;
5328
5329 pos = oap->start;
5330 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5331 {
5332 if (oap->block_mode) /* Visual block mode */
5333 {
5334 block_prep(oap, &bd, pos.lnum, FALSE);
5335 pos.col = bd.textcol;
5336 length = bd.textlen;
5337 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005338 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005339 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005340 curwin->w_cursor.col = 0;
5341 pos.col = 0;
5342 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5343 }
5344 else /* oap->motion_type == MCHAR */
5345 {
5346 if (!oap->inclusive)
5347 dec(&(oap->end));
5348 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5349 pos.col = 0;
5350 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005351 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005352 pos.col += oap->start.col;
5353 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005354 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005355 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005356 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005357 length = (int)STRLEN(ml_get(oap->end.lnum));
5358 if (oap->end.col >= length)
5359 oap->end.col = length - 1;
5360 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005361 }
5362 }
5363 one_change = do_addsub(oap->op_type, &pos, length, amount);
5364 if (one_change)
5365 {
5366 /* Remember the start position of the first change. */
5367 if (change_cnt == 0)
5368 startpos = curbuf->b_op_start;
5369 ++change_cnt;
5370 }
5371
5372#ifdef FEAT_NETBEANS_INTG
5373 if (netbeans_active() && one_change)
5374 {
5375 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5376
5377 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5378 netbeans_inserted(curbuf, pos.lnum, pos.col,
5379 &ptr[pos.col], length);
5380 }
5381#endif
5382 if (g_cmd && one_change)
5383 amount += Prenum1;
5384 }
5385 if (change_cnt)
5386 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5387
5388 if (!change_cnt && oap->is_VIsual)
5389 /* No change: need to remove the Visual selection */
5390 redraw_curbuf_later(INVERTED);
5391
5392 /* Set '[ mark if something changed. Keep the last end
5393 * position from do_addsub(). */
5394 if (change_cnt > 0)
5395 curbuf->b_op_start = startpos;
5396
5397 if (change_cnt > p_report)
5398 {
5399 if (change_cnt == 1)
5400 MSG(_("1 line changed"));
5401 else
5402 smsg((char_u *)_("%ld lines changed"), change_cnt);
5403 }
5404 }
5405}
5406
5407/*
5408 * Add or subtract 'Prenum1' from a number in a line
5409 * op_type is OP_NR_ADD or OP_NR_SUB
5410 *
5411 * Returns TRUE if some character was changed.
5412 */
5413 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005414do_addsub(
5415 int op_type,
5416 pos_T *pos,
5417 int length,
5418 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 int col;
5421 char_u *buf1;
5422 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005423 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005425 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 long_u oldn;
5427 char_u *ptr;
5428 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 int todel;
5430 int dohex;
5431 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005432 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 int doalp;
5434 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005436 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005437 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005438 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005439 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005440 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005441 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005442 pos_T startpos;
5443 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5446 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005447 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5449
Bram Moolenaard79e5502016-01-10 22:13:02 +01005450 curwin->w_cursor = *pos;
5451 ptr = ml_get(pos->lnum);
5452 col = pos->col;
5453
5454 if (*ptr == NUL)
5455 goto theend;
5456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 /*
5458 * First check if we are on a hexadecimal number, after the "0x".
5459 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005460 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005462 if (dobin)
5463 while (col > 0 && vim_isbdigit(ptr[col]))
5464 --col;
5465
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005466 if (dohex)
5467 while (col > 0 && vim_isxdigit(ptr[col]))
5468 --col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005469
5470 if ( dobin
5471 && dohex
5472 && ! ((col > 0
5473 && (ptr[col] == 'X'
5474 || ptr[col] == 'x')
5475 && ptr[col - 1] == '0'
5476 && vim_isxdigit(ptr[col + 1]))))
5477 {
5478
5479 /* In case of binary/hexadecimal pattern overlap match, rescan */
5480
Bram Moolenaard79e5502016-01-10 22:13:02 +01005481 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005482
5483 while (col > 0 && vim_isdigit(ptr[col]))
5484 col--;
5485 }
5486
5487 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005488 && col > 0
5489 && (ptr[col] == 'X'
5490 || ptr[col] == 'x')
5491 && ptr[col - 1] == '0'
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005492 && vim_isxdigit(ptr[col + 1])) ||
5493 ( dobin
5494 && col > 0
5495 && (ptr[col] == 'B'
5496 || ptr[col] == 'b')
5497 && ptr[col - 1] == '0'
5498 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005499 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005500 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005501 --col;
5502 }
5503 else
5504 {
5505 /*
5506 * Search forward and then backward to find the start of number.
5507 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005508 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005509
5510 while (ptr[col] != NUL
5511 && !vim_isdigit(ptr[col])
5512 && !(doalp && ASCII_ISALPHA(ptr[col])))
5513 ++col;
5514
5515 while (col > 0
5516 && vim_isdigit(ptr[col - 1])
5517 && !(doalp && ASCII_ISALPHA(ptr[col])))
5518 --col;
5519 }
5520 }
5521
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005522 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005523 {
5524 while (ptr[col] != NUL && length > 0
5525 && !vim_isdigit(ptr[col])
5526 && !(doalp && ASCII_ISALPHA(ptr[col])))
5527 {
5528 ++col;
5529 --length;
5530 }
5531
5532 if (length == 0)
5533 goto theend;
5534
5535 if (col > pos->col && ptr[col - 1] == '-')
5536 {
5537 negative = TRUE;
5538 was_positive = FALSE;
5539 }
5540 }
5541
5542 /*
5543 * If a number was found, and saving for undo works, replace the number.
5544 */
5545 firstdigit = ptr[col];
5546 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5547 {
5548 beep_flush();
5549 goto theend;
5550 }
5551
5552 if (doalp && ASCII_ISALPHA(firstdigit))
5553 {
5554 /* decrement or increment alphabetic character */
5555 if (op_type == OP_NR_SUB)
5556 {
5557 if (CharOrd(firstdigit) < Prenum1)
5558 {
5559 if (isupper(firstdigit))
5560 firstdigit = 'A';
5561 else
5562 firstdigit = 'a';
5563 }
5564 else
5565#ifdef EBCDIC
5566 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5567#else
5568 firstdigit -= Prenum1;
5569#endif
5570 }
5571 else
5572 {
5573 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5574 {
5575 if (isupper(firstdigit))
5576 firstdigit = 'Z';
5577 else
5578 firstdigit = 'z';
5579 }
5580 else
5581#ifdef EBCDIC
5582 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5583#else
5584 firstdigit += Prenum1;
5585#endif
5586 }
5587 curwin->w_cursor.col = col;
5588 if (!did_change)
5589 startpos = curwin->w_cursor;
5590 did_change = TRUE;
5591 (void)del_char(FALSE);
5592 ins_char(firstdigit);
5593 endpos = curwin->w_cursor;
5594 curwin->w_cursor.col = col;
5595 }
5596 else
5597 {
5598 if (col > 0 && ptr[col - 1] == '-' && !visual)
5599 {
5600 /* negative number */
5601 --col;
5602 negative = TRUE;
5603 }
5604 /* get the number value (unsigned) */
5605 if (visual && VIsual_mode != 'V')
5606 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5607 ? (int)STRLEN(ptr) - col
5608 : length);
5609
5610 vim_str2nr(ptr + col, &pre, &length,
5611 0 + (dobin ? STR2NR_BIN : 0)
5612 + (dooct ? STR2NR_OCT : 0)
5613 + (dohex ? STR2NR_HEX : 0),
5614 NULL, &n, maxlen);
5615
5616 /* ignore leading '-' for hex and octal and bin numbers */
5617 if (pre && negative)
5618 {
5619 ++col;
5620 --length;
5621 negative = FALSE;
5622 }
5623 /* add or subtract */
5624 subtract = FALSE;
5625 if (op_type == OP_NR_SUB)
5626 subtract ^= TRUE;
5627 if (negative)
5628 subtract ^= TRUE;
5629
5630 oldn = n;
5631 if (subtract)
5632 n -= (unsigned long)Prenum1;
5633 else
5634 n += (unsigned long)Prenum1;
5635 /* handle wraparound for decimal numbers */
5636 if (!pre)
5637 {
5638 if (subtract)
5639 {
5640 if (n > oldn)
5641 {
5642 n = 1 + (n ^ (unsigned long)-1);
5643 negative ^= TRUE;
5644 }
5645 }
5646 else
5647 {
5648 /* add */
5649 if (n < oldn)
5650 {
5651 n = (n ^ (unsigned long)-1);
5652 negative ^= TRUE;
5653 }
5654 }
5655 if (n == 0)
5656 negative = FALSE;
5657 }
5658
5659 if (visual && !was_positive && !negative && col > 0)
5660 {
5661 /* need to remove the '-' */
5662 col--;
5663 length++;
5664 }
5665
5666 /*
5667 * Delete the old number.
5668 */
5669 curwin->w_cursor.col = col;
5670 if (!did_change)
5671 startpos = curwin->w_cursor;
5672 did_change = TRUE;
5673 todel = length;
5674 c = gchar_cursor();
5675 /*
5676 * Don't include the '-' in the length, only the length of the
5677 * part after it is kept the same.
5678 */
5679 if (c == '-')
5680 --length;
5681 while (todel-- > 0)
5682 {
5683 if (c < 0x100 && isalpha(c))
5684 {
5685 if (isupper(c))
5686 hexupper = TRUE;
5687 else
5688 hexupper = FALSE;
5689 }
5690 /* del_char() will mark line needing displaying */
5691 (void)del_char(FALSE);
5692 c = gchar_cursor();
5693 }
5694
5695 /*
5696 * Prepare the leading characters in buf1[].
5697 * When there are many leading zeros it could be very long.
5698 * Allocate a bit too much.
5699 */
5700 buf1 = alloc((unsigned)length + NUMBUFLEN);
5701 if (buf1 == NULL)
5702 goto theend;
5703 ptr = buf1;
5704 if (negative && (!visual || (visual && was_positive)))
5705 {
5706 *ptr++ = '-';
5707 }
5708 if (pre)
5709 {
5710 *ptr++ = '0';
5711 --length;
5712 }
5713 if (pre == 'b' || pre == 'B' ||
5714 pre == 'x' || pre == 'X')
5715 {
5716 *ptr++ = pre;
5717 --length;
5718 }
5719
5720 /*
5721 * Put the number characters in buf2[].
5722 */
5723 if (pre == 'b' || pre == 'B')
5724 {
5725 int i;
5726 int bit = 0;
5727 int bits = sizeof(unsigned long) * 8;
5728
5729 /* leading zeros */
5730 for (bit = bits; bit > 0; bit--)
5731 if ((n >> (bit - 1)) & 0x1) break;
5732
5733 for (i = 0; bit > 0; bit--)
5734 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5735
5736 buf2[i] = '\0';
5737 }
5738 else if (pre == 0)
5739 sprintf((char *)buf2, "%lu", n);
5740 else if (pre == '0')
5741 sprintf((char *)buf2, "%lo", n);
5742 else if (pre && hexupper)
5743 sprintf((char *)buf2, "%lX", n);
5744 else
5745 sprintf((char *)buf2, "%lx", n);
5746 length -= (int)STRLEN(buf2);
5747
5748 /*
5749 * Adjust number of zeros to the new number of digits, so the
5750 * total length of the number remains the same.
5751 * Don't do this when
5752 * the result may look like an octal number.
5753 */
5754 if (firstdigit == '0' && !(dooct && pre == 0))
5755 while (length-- > 0)
5756 *ptr++ = '0';
5757 *ptr = NUL;
5758 STRCAT(buf1, buf2);
5759 ins_str(buf1); /* insert the new number */
5760 vim_free(buf1);
5761 endpos = curwin->w_cursor;
5762 if (did_change && curwin->w_cursor.col)
5763 --curwin->w_cursor.col;
5764 }
5765
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005766 if (did_change)
5767 {
5768 /* set the '[ and '] marks */
5769 curbuf->b_op_start = startpos;
5770 curbuf->b_op_end = endpos;
5771 if (curbuf->b_op_end.col > 0)
5772 --curbuf->b_op_end.col;
5773 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005774
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005775theend:
5776 if (visual)
5777 curwin->w_cursor = save_cursor;
5778
Bram Moolenaard79e5502016-01-10 22:13:02 +01005779 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
5782#ifdef FEAT_VIMINFO
5783 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005784read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 int eof;
5787 int do_it = TRUE;
5788 int size;
5789 int limit;
5790 int i;
5791 int set_prev = FALSE;
5792 char_u *str;
5793 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005794 int new_type = MCHAR; /* init to shut up compiler */
5795 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 /* We only get here (hopefully) if line[0] == '"' */
5798 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005799
5800 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (*str == '"')
5802 {
5803 set_prev = TRUE;
5804 str++;
5805 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 if (!ASCII_ISALNUM(*str) && *str != '-')
5808 {
5809 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5810 return TRUE; /* too many errors, pretend end-of-file */
5811 do_it = FALSE;
5812 }
5813 get_yank_register(*str++, FALSE);
5814 if (!force && y_current->y_array != NULL)
5815 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005816
5817 if (*str == '@')
5818 {
5819 /* "x@: register x used for @@ */
5820 if (force || execreg_lastc == NUL)
5821 execreg_lastc = str[-1];
5822 }
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 size = 0;
5825 limit = 100; /* Optimized for registers containing <= 100 lines */
5826 if (do_it)
5827 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005828 /*
5829 * Build the new register in array[].
5830 * y_array is kept as-is until done.
5831 * The "do_it" flag is reset when something is wrong, in which case
5832 * array[] needs to be freed.
5833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (set_prev)
5835 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005836 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005837 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005839 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005841 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005843 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 /* get the block width; if it's missing we get a zero, which is OK */
5845 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005846 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
5848
5849 while (!(eof = viminfo_readline(virp))
5850 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5851 {
5852 if (do_it)
5853 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005854 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005856 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005858
5859 if (new_array == NULL)
5860 {
5861 do_it = FALSE;
5862 break;
5863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005865 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005867 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 }
5870 str = viminfo_readstring(virp, 1, TRUE);
5871 if (str != NULL)
5872 array[size++] = str;
5873 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005874 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 do_it = FALSE;
5876 }
5877 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (do_it)
5880 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005881 /* free y_array[] */
5882 for (i = 0; i < y_current->y_size; i++)
5883 vim_free(y_current->y_array[i]);
5884 vim_free(y_current->y_array);
5885
5886 y_current->y_type = new_type;
5887 y_current->y_width = new_width;
5888 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (size == 0)
5890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 y_current->y_array = NULL;
5892 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005895 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 y_current->y_array =
5897 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5898 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005899 {
5900 if (y_current->y_array == NULL)
5901 vim_free(array[i]);
5902 else
5903 y_current->y_array[i] = array[i];
5904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005907 else
5908 {
5909 /* Free array[] if it was filled. */
5910 for (i = 0; i < size; i++)
5911 vim_free(array[i]);
5912 }
5913 vim_free(array);
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 return eof;
5916}
5917
5918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005919write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 int i, j;
5922 char_u *type;
5923 char_u c;
5924 int num_lines;
5925 int max_num_lines;
5926 int max_kbyte;
5927 long len;
5928
Bram Moolenaar64404472010-06-26 06:24:45 +02005929 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 /* Get '<' value, use old '"' value if '<' is not found. */
5932 max_num_lines = get_viminfo_parameter('<');
5933 if (max_num_lines < 0)
5934 max_num_lines = get_viminfo_parameter('"');
5935 if (max_num_lines == 0)
5936 return;
5937 max_kbyte = get_viminfo_parameter('s');
5938 if (max_kbyte == 0)
5939 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 for (i = 0; i < NUM_REGISTERS; i++)
5942 {
5943 if (y_regs[i].y_array == NULL)
5944 continue;
5945#ifdef FEAT_CLIPBOARD
5946 /* Skip '*'/'+' register, we don't want them back next time */
5947 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5948 continue;
5949#endif
5950#ifdef FEAT_DND
5951 /* Neither do we want the '~' register */
5952 if (i == TILDE_REGISTER)
5953 continue;
5954#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005955 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005957 if (num_lines == 0
5958 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005959 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005960 continue;
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (max_kbyte > 0)
5963 {
5964 /* Skip register if there is more text than the maximum size. */
5965 len = 0;
5966 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005967 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (len > (long)max_kbyte * 1024L)
5969 continue;
5970 }
5971
5972 switch (y_regs[i].y_type)
5973 {
5974 case MLINE:
5975 type = (char_u *)"LINE";
5976 break;
5977 case MCHAR:
5978 type = (char_u *)"CHAR";
5979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 case MBLOCK:
5981 type = (char_u *)"BLOCK";
5982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 default:
5984 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005985 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 emsg(IObuff);
5987 type = (char_u *)"LINE";
5988 break;
5989 }
5990 if (y_previous == &y_regs[i])
5991 fprintf(fp, "\"");
5992 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00005993 fprintf(fp, "\"%c", c);
5994 if (c == execreg_lastc)
5995 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005996 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
5998 /* If max_num_lines < 0, then we save ALL the lines in the register */
5999 if (max_num_lines > 0 && num_lines > max_num_lines)
6000 num_lines = max_num_lines;
6001 for (j = 0; j < num_lines; j++)
6002 {
6003 putc('\t', fp);
6004 viminfo_writestring(fp, y_regs[i].y_array[j]);
6005 }
6006 }
6007}
6008#endif /* FEAT_VIMINFO */
6009
6010#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6011/*
6012 * SELECTION / PRIMARY ('*')
6013 *
6014 * Text selection stuff that uses the GUI selection register '*'. When using a
6015 * GUI this may be text from another window, otherwise it is the last text we
6016 * had highlighted with VIsual mode. With mouse support, clicking the middle
6017 * button performs the paste, otherwise you will need to do <"*p>. "
6018 * If not under X, it is synonymous with the clipboard register '+'.
6019 *
6020 * X CLIPBOARD ('+')
6021 *
6022 * Text selection stuff that uses the GUI clipboard register '+'.
6023 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6024 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6025 * otherwise you will need to do <"+p>. "
6026 * If not under X, it is synonymous with the selection register '*'.
6027 */
6028
6029/*
6030 * Routine to export any final X selection we had to the environment
6031 * so that the text is still available after vim has exited. X selections
6032 * only exist while the owning application exists, so we write to the
6033 * permanent (while X runs) store CUT_BUFFER0.
6034 * Dump the CLIPBOARD selection if we own it (it's logically the more
6035 * 'permanent' of the two), otherwise the PRIMARY one.
6036 * For now, use a hard-coded sanity limit of 1Mb of data.
6037 */
6038#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
6039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006040x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 Display *dpy;
6043 char_u *str = NULL;
6044 long_u len = 0;
6045 int motion_type = -1;
6046
6047# ifdef FEAT_GUI
6048 if (gui.in_use)
6049 dpy = X_DISPLAY;
6050 else
6051# endif
6052# ifdef FEAT_XCLIPBOARD
6053 dpy = xterm_dpy;
6054# else
6055 return;
6056# endif
6057
6058 /* Get selection to export */
6059 if (clip_plus.owned)
6060 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6061 else if (clip_star.owned)
6062 motion_type = clip_convert_selection(&str, &len, &clip_star);
6063
6064 /* Check it's OK */
6065 if (dpy != NULL && str != NULL && motion_type >= 0
6066 && len < 1024*1024 && len > 0)
6067 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006068#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006069 int ok = TRUE;
6070
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006071 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6072 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6073 * encoding conversion usually doesn't work, so keep the text as-is.
6074 */
6075 if (has_mbyte)
6076 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006077 vimconv_T vc;
6078
6079 vc.vc_type = CONV_NONE;
6080 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6081 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006082 int intlen = len;
6083 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006084
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006085 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006086 conv_str = string_convert(&vc, str, &intlen);
6087 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006088 if (conv_str != NULL)
6089 {
6090 vim_free(str);
6091 str = conv_str;
6092 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006093 else
6094 {
6095 ok = FALSE;
6096 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006097 convert_setup(&vc, NULL, NULL);
6098 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006099 else
6100 {
6101 ok = FALSE;
6102 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006103 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006104
6105 /* Do not store the string if conversion failed. Better to use any
6106 * other selection than garbled text. */
6107 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006108#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006109 {
6110 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6111 XFlush(dpy);
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114
6115 vim_free(str);
6116}
6117#endif
6118
6119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006120clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 struct yankreg *y_ptr = y_current;
6123
6124 if (cbd == &clip_plus)
6125 y_current = &y_regs[PLUS_REGISTER];
6126 else
6127 y_current = &y_regs[STAR_REGISTER];
6128 free_yank_all();
6129 y_current->y_size = 0;
6130 y_current = y_ptr;
6131}
6132
6133/*
6134 * Get the selected text and put it in the gui selection register '*' or '+'.
6135 */
6136 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006137clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 struct yankreg *old_y_previous, *old_y_current;
6140 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 pos_T old_visual;
6142 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 colnr_T old_curswant;
6144 int old_set_curswant;
6145 pos_T old_op_start, old_op_end;
6146 oparg_T oa;
6147 cmdarg_T ca;
6148
6149 if (cbd->owned)
6150 {
6151 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6152 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6153 return;
6154
6155 /* Get the text between clip_star.start & clip_star.end */
6156 old_y_previous = y_previous;
6157 old_y_current = y_current;
6158 old_cursor = curwin->w_cursor;
6159 old_curswant = curwin->w_curswant;
6160 old_set_curswant = curwin->w_set_curswant;
6161 old_op_start = curbuf->b_op_start;
6162 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 old_visual = VIsual;
6164 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 clear_oparg(&oa);
6166 oa.regname = (cbd == &clip_plus ? '+' : '*');
6167 oa.op_type = OP_YANK;
6168 vim_memset(&ca, 0, sizeof(ca));
6169 ca.oap = &oa;
6170 ca.cmdchar = 'y';
6171 ca.count1 = 1;
6172 ca.retval = CA_NO_ADJ_OP_END;
6173 do_pending_operator(&ca, 0, TRUE);
6174 y_previous = old_y_previous;
6175 y_current = old_y_current;
6176 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006177 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 curwin->w_curswant = old_curswant;
6179 curwin->w_set_curswant = old_set_curswant;
6180 curbuf->b_op_start = old_op_start;
6181 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 VIsual = old_visual;
6183 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 else
6186 {
6187 clip_free_selection(cbd);
6188
6189 /* Try to get selected text from another window */
6190 clip_gen_request_selection(cbd);
6191 }
6192}
6193
Bram Moolenaard44347f2011-06-19 01:14:29 +02006194/*
6195 * Convert from the GUI selection string into the '*'/'+' register.
6196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006198clip_yank_selection(
6199 int type,
6200 char_u *str,
6201 long len,
6202 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 struct yankreg *y_ptr;
6205
6206 if (cbd == &clip_plus)
6207 y_ptr = &y_regs[PLUS_REGISTER];
6208 else
6209 y_ptr = &y_regs[STAR_REGISTER];
6210
6211 clip_free_selection(cbd);
6212
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006213 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214}
6215
6216/*
6217 * Convert the '*'/'+' register into a GUI selection string returned in *str
6218 * with length *len.
6219 * Returns the motion type, or -1 for failure.
6220 */
6221 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006222clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 char_u *p;
6225 int lnum;
6226 int i, j;
6227 int_u eolsize;
6228 struct yankreg *y_ptr;
6229
6230 if (cbd == &clip_plus)
6231 y_ptr = &y_regs[PLUS_REGISTER];
6232 else
6233 y_ptr = &y_regs[STAR_REGISTER];
6234
6235#ifdef USE_CRNL
6236 eolsize = 2;
6237#else
6238 eolsize = 1;
6239#endif
6240
6241 *str = NULL;
6242 *len = 0;
6243 if (y_ptr->y_array == NULL)
6244 return -1;
6245
6246 for (i = 0; i < y_ptr->y_size; i++)
6247 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6248
6249 /*
6250 * Don't want newline character at end of last line if we're in MCHAR mode.
6251 */
6252 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6253 *len -= eolsize;
6254
6255 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6256 if (p == NULL)
6257 return -1;
6258 lnum = 0;
6259 for (i = 0, j = 0; i < (int)*len; i++, j++)
6260 {
6261 if (y_ptr->y_array[lnum][j] == '\n')
6262 p[i] = NUL;
6263 else if (y_ptr->y_array[lnum][j] == NUL)
6264 {
6265#ifdef USE_CRNL
6266 p[i++] = '\r';
6267#endif
6268#ifdef USE_CR
6269 p[i] = '\r';
6270#else
6271 p[i] = '\n';
6272#endif
6273 lnum++;
6274 j = -1;
6275 }
6276 else
6277 p[i] = y_ptr->y_array[lnum][j];
6278 }
6279 return y_ptr->y_type;
6280}
6281
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283/*
6284 * If we have written to a clipboard register, send the text to the clipboard.
6285 */
6286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006287may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6290 {
6291 clip_own_selection(&clip_star);
6292 clip_gen_set_selection(&clip_star);
6293 }
6294 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6295 {
6296 clip_own_selection(&clip_plus);
6297 clip_gen_set_selection(&clip_plus);
6298 }
6299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
6301#endif /* FEAT_CLIPBOARD || PROTO */
6302
6303
6304#if defined(FEAT_DND) || defined(PROTO)
6305/*
6306 * Replace the contents of the '~' register with str.
6307 */
6308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006309dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 struct yankreg *curr;
6312
6313 curr = y_current;
6314 y_current = &y_regs[TILDE_REGISTER];
6315 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006316 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 y_current = curr;
6318}
6319#endif
6320
6321
6322#if defined(FEAT_EVAL) || defined(PROTO)
6323/*
6324 * Return the type of a register.
6325 * Used for getregtype()
6326 * Returns MAUTO for error.
6327 */
6328 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006329get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 switch (regname)
6332 {
6333 case '%': /* file name */
6334 case '#': /* alternate file name */
6335 case '=': /* expression */
6336 case ':': /* last command line */
6337 case '/': /* last search-pattern */
6338 case '.': /* last inserted text */
6339#ifdef FEAT_SEARCHPATH
6340 case Ctrl_F: /* Filename under cursor */
6341 case Ctrl_P: /* Path under cursor, expand via "path" */
6342#endif
6343 case Ctrl_W: /* word under cursor */
6344 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6345 case '_': /* black hole: always empty */
6346 return MCHAR;
6347 }
6348
6349#ifdef FEAT_CLIPBOARD
6350 regname = may_get_selection(regname);
6351#endif
6352
Bram Moolenaar32b92012014-01-14 12:33:36 +01006353 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006354 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 get_yank_register(regname, FALSE);
6357
6358 if (y_current->y_array != NULL)
6359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (reglen != NULL && y_current->y_type == MBLOCK)
6361 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 return y_current->y_type;
6363 }
6364 return MAUTO;
6365}
6366
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006367static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006368
6369/*
6370 * When "flags" has GREG_LIST return a list with text "s".
6371 * Otherwise just return "s".
6372 */
6373 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006374getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006375{
6376 if (flags & GREG_LIST)
6377 {
6378 list_T *list = list_alloc();
6379
6380 if (list != NULL)
6381 {
6382 if (list_append_string(list, NULL, -1) == FAIL)
6383 {
6384 list_free(list, TRUE);
6385 return NULL;
6386 }
6387 list->lv_first->li_tv.vval.v_string = s;
6388 }
6389 return (char_u *)list;
6390 }
6391 return s;
6392}
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394/*
6395 * Return the contents of a register as a single allocated string.
6396 * Used for "@r" in expressions and for getreg().
6397 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006398 * Flags:
6399 * GREG_NO_EXPR Do not allow expression register
6400 * GREG_EXPR_SRC For the expression register: return expression itself,
6401 * not the result of its evaluation.
6402 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 */
6404 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006405get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 long i;
6408 char_u *retval;
6409 int allocated;
6410 long len;
6411
6412 /* Don't allow using an expression register inside an expression */
6413 if (regname == '=')
6414 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006415 if (flags & GREG_NO_EXPR)
6416 return NULL;
6417 if (flags & GREG_EXPR_SRC)
6418 return getreg_wrap_one_line(get_expr_line_src(), flags);
6419 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
6421
6422 if (regname == '@') /* "@@" is used for unnamed register */
6423 regname = '"';
6424
6425 /* check for valid regname */
6426 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6427 return NULL;
6428
6429#ifdef FEAT_CLIPBOARD
6430 regname = may_get_selection(regname);
6431#endif
6432
6433 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6434 {
6435 if (retval == NULL)
6436 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006437 if (allocated)
6438 return getreg_wrap_one_line(retval, flags);
6439 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441
6442 get_yank_register(regname, FALSE);
6443 if (y_current->y_array == NULL)
6444 return NULL;
6445
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006446 if (flags & GREG_LIST)
6447 {
6448 list_T *list = list_alloc();
6449 int error = FALSE;
6450
6451 if (list == NULL)
6452 return NULL;
6453 for (i = 0; i < y_current->y_size; ++i)
6454 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6455 error = TRUE;
6456 if (error)
6457 {
6458 list_free(list, TRUE);
6459 return NULL;
6460 }
6461 return (char_u *)list;
6462 }
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 /*
6465 * Compute length of resulting string.
6466 */
6467 len = 0;
6468 for (i = 0; i < y_current->y_size; ++i)
6469 {
6470 len += (long)STRLEN(y_current->y_array[i]);
6471 /*
6472 * Insert a newline between lines and after last line if
6473 * y_type is MLINE.
6474 */
6475 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6476 ++len;
6477 }
6478
6479 retval = lalloc(len + 1, TRUE);
6480
6481 /*
6482 * Copy the lines of the yank register into the string.
6483 */
6484 if (retval != NULL)
6485 {
6486 len = 0;
6487 for (i = 0; i < y_current->y_size; ++i)
6488 {
6489 STRCPY(retval + len, y_current->y_array[i]);
6490 len += (long)STRLEN(retval + len);
6491
6492 /*
6493 * Insert a NL between lines and after the last line if y_type is
6494 * MLINE.
6495 */
6496 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6497 retval[len++] = '\n';
6498 }
6499 retval[len] = NUL;
6500 }
6501
6502 return retval;
6503}
6504
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006505 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006506init_write_reg(
6507 int name,
6508 struct yankreg **old_y_previous,
6509 struct yankreg **old_y_current,
6510 int must_append,
6511 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006512{
6513 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6514 {
6515 emsg_invreg(name);
6516 return FAIL;
6517 }
6518
6519 /* Don't want to change the current (unnamed) register */
6520 *old_y_previous = y_previous;
6521 *old_y_current = y_current;
6522
6523 get_yank_register(name, TRUE);
6524 if (!y_append && !must_append)
6525 free_yank_all();
6526 return OK;
6527}
6528
6529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006530finish_write_reg(
6531 int name,
6532 struct yankreg *old_y_previous,
6533 struct yankreg *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006534{
6535# ifdef FEAT_CLIPBOARD
6536 /* Send text of clipboard register to the clipboard. */
6537 may_set_selection();
6538# endif
6539
6540 /* ':let @" = "val"' should change the meaning of the "" register */
6541 if (name != '"')
6542 y_previous = old_y_previous;
6543 y_current = old_y_current;
6544}
6545
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546/*
6547 * Store string "str" in register "name".
6548 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6549 * If "must_append" is TRUE, always append to the register. Otherwise append
6550 * if "name" is an uppercase letter.
6551 * Note: "maxlen" and "must_append" don't work for the "/" register.
6552 * Careful: 'str' is modified, you may have to use a copy!
6553 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6554 */
6555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556write_reg_contents(
6557 int name,
6558 char_u *str,
6559 int maxlen,
6560 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6563}
6564
6565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006566write_reg_contents_lst(
6567 int name,
6568 char_u **strings,
6569 int maxlen UNUSED,
6570 int must_append,
6571 int yank_type,
6572 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006573{
6574 struct yankreg *old_y_previous, *old_y_current;
6575
6576 if (name == '/'
6577#ifdef FEAT_EVAL
6578 || name == '='
6579#endif
6580 )
6581 {
6582 char_u *s;
6583
6584 if (strings[0] == NULL)
6585 s = (char_u *)"";
6586 else if (strings[1] != NULL)
6587 {
6588 EMSG(_("E883: search pattern and expression register may not "
6589 "contain two or more lines"));
6590 return;
6591 }
6592 else
6593 s = strings[0];
6594 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6595 return;
6596 }
6597
6598 if (name == '_') /* black hole: nothing to do */
6599 return;
6600
6601 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6602 &yank_type) == FAIL)
6603 return;
6604
6605 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6606
6607 finish_write_reg(name, old_y_previous, old_y_current);
6608}
6609
6610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611write_reg_contents_ex(
6612 int name,
6613 char_u *str,
6614 int maxlen,
6615 int must_append,
6616 int yank_type,
6617 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 struct yankreg *old_y_previous, *old_y_current;
6620 long len;
6621
Bram Moolenaare7566042005-06-17 22:00:15 +00006622 if (maxlen >= 0)
6623 len = maxlen;
6624 else
6625 len = (long)STRLEN(str);
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 /* Special case: '/' search pattern */
6628 if (name == '/')
6629 {
6630 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6631 return;
6632 }
6633
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006634 if (name == '#')
6635 {
6636 buf_T *buf;
6637
6638 if (VIM_ISDIGIT(*str))
6639 {
6640 int num = atoi((char *)str);
6641
6642 buf = buflist_findnr(num);
6643 if (buf == NULL)
6644 EMSGN(_(e_nobufnr), (long)num);
6645 }
6646 else
6647 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6648 TRUE, FALSE, FALSE));
6649 if (buf == NULL)
6650 return;
6651 curwin->w_alt_fnum = buf->b_fnum;
6652 return;
6653 }
6654
Bram Moolenaare7566042005-06-17 22:00:15 +00006655#ifdef FEAT_EVAL
6656 if (name == '=')
6657 {
6658 char_u *p, *s;
6659
6660 p = vim_strnsave(str, (int)len);
6661 if (p == NULL)
6662 return;
6663 if (must_append)
6664 {
6665 s = concat_str(get_expr_line_src(), p);
6666 vim_free(p);
6667 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006668 }
6669 set_expr_line(p);
6670 return;
6671 }
6672#endif
6673
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if (name == '_') /* black hole: nothing to do */
6675 return;
6676
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006677 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6678 &yank_type) == FAIL)
6679 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006681 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006683 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684}
6685#endif /* FEAT_EVAL */
6686
6687#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6688/*
6689 * Put a string into a register. When the register is not empty, the string
6690 * is appended.
6691 */
6692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006693str_to_reg(
6694 struct yankreg *y_ptr, /* pointer to yank register */
6695 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6696 char_u *str, /* string to put in register */
6697 long len, /* length of string */
6698 long blocklen, /* width of Visual block */
6699 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006701 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 int lnum;
6703 long start;
6704 long i;
6705 int extra;
6706 int newlines; /* number of lines added */
6707 int extraline = 0; /* extra line at the end */
6708 int append = FALSE; /* append to last line in register */
6709 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006710 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006714 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 y_ptr->y_size = 0;
6716
Bram Moolenaard44347f2011-06-19 01:14:29 +02006717 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006718 type = ((str_list || (len > 0 && (str[len - 1] == NL
6719 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006720 ? MLINE : MCHAR);
6721 else
6722 type = yank_type;
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 /*
6725 * Count the number of lines within the string
6726 */
6727 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006728 if (str_list)
6729 {
6730 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006735 for (i = 0; i < len; i++)
6736 if (str[i] == '\n')
6737 ++newlines;
6738 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6739 {
6740 extraline = 1;
6741 ++newlines; /* count extra newline at the end */
6742 }
6743 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6744 {
6745 append = TRUE;
6746 --newlines; /* uncount newline when appending first line */
6747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006750 /* Without any lines make the register empty. */
6751 if (y_ptr->y_size + newlines == 0)
6752 {
6753 vim_free(y_ptr->y_array);
6754 y_ptr->y_array = NULL;
6755 return;
6756 }
6757
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 /*
6759 * Allocate an array to hold the pointers to the new register lines.
6760 * If the register was not empty, move the existing lines to the new array.
6761 */
6762 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6763 * sizeof(char_u *), TRUE);
6764 if (pp == NULL) /* out of memory */
6765 return;
6766 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6767 pp[lnum] = y_ptr->y_array[lnum];
6768 vim_free(y_ptr->y_array);
6769 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 /*
6773 * Find the end of each line and save it into the array.
6774 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006775 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006777 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6778 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006779 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006780 pp[lnum] = vim_strnsave(*ss, i);
6781 if (i > maxlen)
6782 maxlen = i;
6783 }
6784 }
6785 else
6786 {
6787 for (start = 0; start < len + extraline; start += i + 1)
6788 {
6789 for (i = start; i < len; ++i) /* find the end of the line */
6790 if (str[i] == '\n')
6791 break;
6792 i -= start; /* i is now length of line */
6793 if (i > maxlen)
6794 maxlen = i;
6795 if (append)
6796 {
6797 --lnum;
6798 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6799 }
6800 else
6801 extra = 0;
6802 s = alloc((unsigned)(i + extra + 1));
6803 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006805 if (extra)
6806 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6807 if (append)
6808 vim_free(y_ptr->y_array[lnum]);
6809 if (i)
6810 mch_memmove(s + extra, str + start, (size_t)i);
6811 extra += i;
6812 s[extra] = NUL;
6813 y_ptr->y_array[lnum++] = s;
6814 while (--extra >= 0)
6815 {
6816 if (*s == NUL)
6817 *s = '\n'; /* replace NUL with newline */
6818 ++s;
6819 }
6820 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823 y_ptr->y_type = type;
6824 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (type == MBLOCK)
6826 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6827 else
6828 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829}
6830#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6831
6832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 vim_memset(oap, 0, sizeof(oparg_T));
6836}
6837
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006838static long line_count_info(char_u *line, long *wc, long *cc, long limit, int eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
6840/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006841 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 *
6843 * "Words" are counted by looking for boundaries between non-space and
6844 * space characters. (it seems to produce results that match 'wc'.)
6845 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006846 * Return value is byte count; word count for the line is added to "*wc".
6847 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 *
6849 * The function will only examine the first "limit" characters in the
6850 * line, stopping if it encounters an end-of-line (NUL byte). In that
6851 * case, eol_size will be added to the character count to account for
6852 * the size of the EOL character.
6853 */
6854 static long
Bram Moolenaar9b578142016-01-30 19:39:49 +01006855line_count_info(
6856 char_u *line,
6857 long *wc,
6858 long *cc,
6859 long limit,
6860 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006862 long i;
6863 long words = 0;
6864 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 int is_word = 0;
6866
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006867 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
6869 if (is_word)
6870 {
6871 if (vim_isspace(line[i]))
6872 {
6873 words++;
6874 is_word = 0;
6875 }
6876 }
6877 else if (!vim_isspace(line[i]))
6878 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006879 ++chars;
6880#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006881 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006882#else
6883 ++i;
6884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
6887 if (is_word)
6888 words++;
6889 *wc += words;
6890
6891 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006892 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006895 chars += eol_size;
6896 }
6897 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 return i;
6899}
6900
6901/*
6902 * Give some info about the position of the cursor (for "g CTRL-G").
6903 * In Visual mode, give some info about the selected region. (In this case,
6904 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006905 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 */
6907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006908cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
6910 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006911 char_u buf1[50];
6912 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006914 long byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01006915#ifdef FEAT_MBYTE
Bram Moolenaared767a22016-01-03 22:49:16 +01006916 long bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01006917#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006918 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 long char_count = 0;
6920 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 long word_count = 0;
6922 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006923 int eol_size;
6924 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 long line_count_selected = 0;
6926 pos_T min_pos, max_pos;
6927 oparg_T oparg;
6928 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 /*
6931 * Compute the length of the file in characters.
6932 */
6933 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6934 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006935 if (dict == NULL)
6936 {
6937 MSG(_(no_lines_msg));
6938 return;
6939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 else
6942 {
6943 if (get_fileformat(curbuf) == EOL_DOS)
6944 eol_size = 2;
6945 else
6946 eol_size = 1;
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (VIsual_active)
6949 {
6950 if (lt(VIsual, curwin->w_cursor))
6951 {
6952 min_pos = VIsual;
6953 max_pos = curwin->w_cursor;
6954 }
6955 else
6956 {
6957 min_pos = curwin->w_cursor;
6958 max_pos = VIsual;
6959 }
6960 if (*p_sel == 'e' && max_pos.col > 0)
6961 --max_pos.col;
6962
6963 if (VIsual_mode == Ctrl_V)
6964 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006965#ifdef FEAT_LINEBREAK
6966 char_u * saved_sbr = p_sbr;
6967
6968 /* Make 'sbr' empty for a moment to get the correct size. */
6969 p_sbr = empty_option;
6970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 oparg.is_VIsual = 1;
6972 oparg.block_mode = TRUE;
6973 oparg.op_type = OP_NOP;
6974 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006975 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006976#ifdef FEAT_LINEBREAK
6977 p_sbr = saved_sbr;
6978#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006979 if (curwin->w_curswant == MAXCOL)
6980 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 /* Swap the start, end vcol if needed */
6982 if (oparg.end_vcol < oparg.start_vcol)
6983 {
6984 oparg.end_vcol += oparg.start_vcol;
6985 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6986 oparg.end_vcol -= oparg.start_vcol;
6987 }
6988 }
6989 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6993 {
6994 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006995 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
6997 ui_breakcheck();
6998 if (got_int)
6999 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007000 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 /* Do extra processing for VIsual mode. */
7004 if (VIsual_active
7005 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7006 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007007 char_u *s = NULL;
7008 long len = 0L;
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 switch (VIsual_mode)
7011 {
7012 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007013#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007017#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007019#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007020 s = bd.textstart;
7021 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 break;
7023 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007024 s = ml_get(lnum);
7025 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 break;
7027 case 'v':
7028 {
7029 colnr_T start_col = (lnum == min_pos.lnum)
7030 ? min_pos.col : 0;
7031 colnr_T end_col = (lnum == max_pos.lnum)
7032 ? max_pos.col - start_col + 1 : MAXCOL;
7033
Bram Moolenaardef9e822004-12-31 20:58:58 +00007034 s = ml_get(lnum) + start_col;
7035 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 break;
7038 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007039 if (s != NULL)
7040 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007041 byte_count_cursor += line_count_info(s, &word_count_cursor,
7042 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007043 if (lnum == curbuf->b_ml.ml_line_count
7044 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007045 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007046 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007047 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
7052 /* In non-visual mode, check for the line the cursor is on */
7053 if (lnum == curwin->w_cursor.lnum)
7054 {
7055 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007056 char_count_cursor += char_count;
7057 byte_count_cursor = byte_count +
7058 line_count_info(ml_get(lnum),
7059 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 (long)(curwin->w_cursor.col + 1), eol_size);
7061 }
7062 }
7063 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007064 byte_count += line_count_info(ml_get(lnum), &word_count,
7065 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067
7068 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007069 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007070 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaared767a22016-01-03 22:49:16 +01007072 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007074 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007076 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7077 {
7078 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7079 &max_pos.col);
7080 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7081 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7082 }
7083 else
7084 buf1[0] = NUL;
7085
7086 if (char_count_cursor == byte_count_cursor
7087 && char_count == byte_count)
7088 vim_snprintf((char *)IObuff, IOSIZE,
7089 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
7090 buf1, line_count_selected,
7091 (long)curbuf->b_ml.ml_line_count,
7092 word_count_cursor, word_count,
7093 byte_count_cursor, byte_count);
7094 else
7095 vim_snprintf((char *)IObuff, IOSIZE,
7096 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
7097 buf1, line_count_selected,
7098 (long)curbuf->b_ml.ml_line_count,
7099 word_count_cursor, word_count,
7100 char_count_cursor, char_count,
7101 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 }
7103 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007104 {
7105 p = ml_get_curline();
7106 validate_virtcol();
7107 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7108 (int)curwin->w_virtcol + 1);
7109 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7110 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
Bram Moolenaared767a22016-01-03 22:49:16 +01007112 if (char_count_cursor == byte_count_cursor
7113 && char_count == byte_count)
7114 vim_snprintf((char *)IObuff, IOSIZE,
7115 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
7116 (char *)buf1, (char *)buf2,
7117 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 (long)curbuf->b_ml.ml_line_count,
7119 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007120 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007121 else
7122 vim_snprintf((char *)IObuff, IOSIZE,
7123 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
7124 (char *)buf1, (char *)buf2,
7125 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007126 (long)curbuf->b_ml.ml_line_count,
7127 word_count_cursor, word_count,
7128 char_count_cursor, char_count,
7129 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007130 }
7131 }
7132
Bram Moolenaared767a22016-01-03 22:49:16 +01007133#ifdef FEAT_MBYTE
7134 bom_count = bomb_size();
7135 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007136 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7137 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007138#endif
7139 if (dict == NULL)
7140 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007141 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007142 p = p_shm;
7143 p_shm = (char_u *)"";
7144 msg(IObuff);
7145 p_shm = p;
7146 }
7147 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007148#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007149 if (dict != NULL)
7150 {
7151 dict_add_nr_str(dict, "words", (long)word_count, NULL);
7152 dict_add_nr_str(dict, "chars", (long)char_count, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007153 dict_add_nr_str(dict, "bytes", (long)byte_count
7154# ifdef FEAT_MBYTE
7155 + bom_count
7156# endif
7157 , NULL);
7158 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7159 (long)byte_count_cursor, NULL);
7160 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7161 (long)char_count_cursor, NULL);
7162 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
7163 (long)word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}