blob: 35f2d2e95fdf1f8e171f06864abc28c167a97654 [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 Moolenaar20b4f462016-03-05 17:25:39 +0100413 {
414 if ((*mb_ptr2len)(bd.textstart) == 1)
415 ++bd.textstart;
416 else
417 {
418 ws_vcol = 0;
419 bd.startspaces = 0;
420 }
421 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000424 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 }
426 for ( ; vim_iswhite(*bd.textstart); )
427 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200428 /* TODO: is passing bd.textstart for start of the line OK? */
429 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
430 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 total += incr;
432 bd.start_vcol += incr;
433 }
434 /* OK, now total=all the VWS reqd, and textstart points at the 1st
435 * non-ws char in the block. */
436 if (!curbuf->b_p_et)
437 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
438 if (i)
439 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
440 else
441 j = total;
442 /* if we're splitting a TAB, allow for it */
443 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
444 len = (int)STRLEN(bd.textstart) + 1;
445 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
446 if (newp == NULL)
447 return;
448 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
449 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200450 vim_memset(newp + bd.textcol, TAB, (size_t)i);
451 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 /* the end */
453 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
454 }
455 else /* left */
456 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 colnr_T destination_col; /* column to which text in block will
458 be shifted */
459 char_u *verbatim_copy_end; /* end of the part of the line which is
460 copied verbatim */
461 colnr_T verbatim_copy_width;/* the (displayed) width of this part
462 of line */
463 unsigned fill; /* nr of spaces that replace a TAB */
464 unsigned new_line_len; /* the length of the line after the
465 block shift */
466 size_t block_space_width;
467 size_t shift_amount;
468 char_u *non_white = bd.textstart;
469 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000471 /*
472 * Firstly, let's find the first non-whitespace character that is
473 * displayed after the block's start column and the character's column
474 * number. Also, let's calculate the width of all the whitespace
475 * characters that are displayed in the block and precede the searched
476 * non-whitespace character.
477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000479 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
480 * the part of which is displayed at the block's beginning. Let's start
481 * searching from the next character. */
482 if (bd.startspaces)
483 mb_ptr_adv(non_white);
484
485 /* The character's column is in "bd.start_vcol". */
486 non_white_col = bd.start_vcol;
487
488 while (vim_iswhite(*non_white))
489 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200490 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000491 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 }
493
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494 block_space_width = non_white_col - oap->start_vcol;
495 /* We will shift by "total" or "block_space_width", whichever is less.
496 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000497 shift_amount = (block_space_width < (size_t)total
498 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000499
500 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000501 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* Now let's find out how much of the beginning of the line we can
504 * reuse without modification. */
505 verbatim_copy_end = bd.textstart;
506 verbatim_copy_width = bd.start_vcol;
507
508 /* If "bd.startspaces" is set, "bd.textstart" points to the character
509 * preceding the block. We have to subtract its width to obtain its
510 * column number. */
511 if (bd.startspaces)
512 verbatim_copy_width -= bd.start_char_vcols;
513 while (verbatim_copy_width < destination_col)
514 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 char_u *line = verbatim_copy_end;
516
517 /* TODO: is passing verbatim_copy_end for start of the line OK? */
518 incr = lbr_chartabsize(line, verbatim_copy_end,
519 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520 if (verbatim_copy_width + incr > destination_col)
521 break;
522 verbatim_copy_width += incr;
523 mb_ptr_adv(verbatim_copy_end);
524 }
525
526 /* If "destination_col" is different from the width of the initial
527 * part of the line that will be copied, it means we encountered a tab
528 * character, which we will have to partly replace with spaces. */
529 fill = destination_col - verbatim_copy_width;
530
531 /* The replacement line will consist of:
532 * - the beginning of the original line up to "verbatim_copy_end",
533 * - "fill" number of spaces,
534 * - the rest of the line, pointed to by non_white. */
535 new_line_len = (unsigned)(verbatim_copy_end - oldp)
536 + fill
537 + (unsigned)STRLEN(non_white) + 1;
538
539 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (newp == NULL)
541 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200543 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000544 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 /* replace the line */
547 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
548 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
549 State = oldstate;
550 curwin->w_cursor.col = oldcol;
551#ifdef FEAT_RIGHTLEFT
552 p_ri = old_p_ri;
553#endif
554}
555#endif
556
557#ifdef FEAT_VISUALEXTRA
558/*
559 * Insert string "s" (b_insert ? before : after) block :AKelly
560 * Caller must prepare for undo.
561 */
562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100563block_insert(
564 oparg_T *oap,
565 char_u *s,
566 int b_insert,
567 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 int p_ts;
570 int count = 0; /* extra spaces to replace a cut TAB */
571 int spaces = 0; /* non-zero if cutting a TAB */
572 colnr_T offset; /* pointer along new line */
573 unsigned s_len; /* STRLEN(s) */
574 char_u *newp, *oldp; /* new, old lines */
575 linenr_T lnum; /* loop var */
576 int oldstate = State;
577
578 State = INSERT; /* don't want REPLACE for State */
579 s_len = (unsigned)STRLEN(s);
580
581 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
582 {
583 block_prep(oap, bdp, lnum, TRUE);
584 if (bdp->is_short && b_insert)
585 continue; /* OP_INSERT, line ends before block start */
586
587 oldp = ml_get(lnum);
588
589 if (b_insert)
590 {
591 p_ts = bdp->start_char_vcols;
592 spaces = bdp->startspaces;
593 if (spaces != 0)
594 count = p_ts - 1; /* we're cutting a TAB */
595 offset = bdp->textcol;
596 }
597 else /* append */
598 {
599 p_ts = bdp->end_char_vcols;
600 if (!bdp->is_short) /* spaces = padding after block */
601 {
602 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
603 if (spaces != 0)
604 count = p_ts - 1; /* we're cutting a TAB */
605 offset = bdp->textcol + bdp->textlen - (spaces != 0);
606 }
607 else /* spaces = padding to block edge */
608 {
609 /* if $ used, just append to EOL (ie spaces==0) */
610 if (!bdp->is_MAX)
611 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
612 count = spaces;
613 offset = bdp->textcol + bdp->textlen;
614 }
615 }
616
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200617#ifdef FEAT_MBYTE
618 if (has_mbyte && spaces > 0)
619 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100620 int off;
621
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200622 /* Avoid starting halfway a multi-byte character. */
623 if (b_insert)
624 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100625 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200626 }
627 else
628 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100629 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200630 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100632 spaces -= off;
633 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200634 }
635#endif
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
638 if (newp == NULL)
639 continue;
640
641 /* copy up to shifted part */
642 mch_memmove(newp, oldp, (size_t)(offset));
643 oldp += offset;
644
645 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200646 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 /* copy the new text */
649 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
650 offset += s_len;
651
652 if (spaces && !bdp->is_short)
653 {
654 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200655 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 /* We're splitting a TAB, don't copy it. */
657 oldp++;
658 /* We allowed for that TAB, remember this now */
659 count++;
660 }
661
662 if (spaces > 0)
663 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000664 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666 ml_replace(lnum, newp, FALSE);
667
668 if (lnum == oap->end.lnum)
669 {
670 /* Set "']" mark to the end of the block instead of the end of
671 * the insert in the first line. */
672 curbuf->b_op_end.lnum = oap->end.lnum;
673 curbuf->b_op_end.col = offset;
674 }
675 } /* for all lnum */
676
677 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
678
679 State = oldstate;
680}
681#endif
682
683#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
684/*
685 * op_reindent - handle reindenting a block of lines.
686 */
687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100688op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
690 long i;
691 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200692 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 linenr_T first_changed = 0;
694 linenr_T last_changed = 0;
695 linenr_T start_lnum = curwin->w_cursor.lnum;
696
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000697 /* Don't even try when 'modifiable' is off. */
698 if (!curbuf->b_p_ma)
699 {
700 EMSG(_(e_modifiable));
701 return;
702 }
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 for (i = oap->line_count; --i >= 0 && !got_int; )
705 {
706 /* it's a slow thing to do, so give feedback so there's no worry that
707 * the computer's just hung. */
708
709 if (i > 1
710 && (i % 50 == 0 || i == oap->line_count - 1)
711 && oap->line_count > p_report)
712 smsg((char_u *)_("%ld lines to indent... "), i);
713
714 /*
715 * Be vi-compatible: For lisp indenting the first line is not
716 * indented, unless there is only one line.
717 */
718#ifdef FEAT_LISP
719 if (i != oap->line_count - 1 || oap->line_count == 1
720 || how != get_lisp_indent)
721#endif
722 {
723 l = skipwhite(ml_get_curline());
724 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200725 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200727 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200729 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 /* did change the indent, call changed_lines() later */
732 if (first_changed == 0)
733 first_changed = curwin->w_cursor.lnum;
734 last_changed = curwin->w_cursor.lnum;
735 }
736 }
737 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000738 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740
741 /* put cursor on first non-blank of indented line */
742 curwin->w_cursor.lnum = start_lnum;
743 beginline(BL_SOL | BL_FIX);
744
745 /* Mark changed lines so that they will be redrawn. When Visual
746 * highlighting was present, need to continue until the last line. When
747 * there is no change still need to remove the Visual highlighting. */
748 if (last_changed != 0)
749 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 else if (oap->is_VIsual)
753 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 if (oap->line_count > p_report)
756 {
757 i = oap->line_count - (i + 1);
758 if (i == 1)
759 MSG(_("1 line indented "));
760 else
761 smsg((char_u *)_("%ld lines indented "), i);
762 }
763 /* set '[ and '] marks */
764 curbuf->b_op_start = oap->start;
765 curbuf->b_op_end = oap->end;
766}
767#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
768
769#if defined(FEAT_EVAL) || defined(PROTO)
770/*
771 * Keep the last expression line here, for repeating.
772 */
773static char_u *expr_line = NULL;
774
775/*
776 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
777 * Returns '=' when OK, NUL otherwise.
778 */
779 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100780get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 char_u *new_line;
783
784 new_line = getcmdline('=', 0L, 0);
785 if (new_line == NULL)
786 return NUL;
787 if (*new_line == NUL) /* use previous line */
788 vim_free(new_line);
789 else
790 set_expr_line(new_line);
791 return '=';
792}
793
794/*
795 * Set the expression for the '=' register.
796 * Argument must be an allocated string.
797 */
798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100799set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 vim_free(expr_line);
802 expr_line = new_line;
803}
804
805/*
806 * Get the result of the '=' register expression.
807 * Returns a pointer to allocated memory, or NULL for failure.
808 */
809 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100810get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 char_u *expr_copy;
813 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000814 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 if (expr_line == NULL)
817 return NULL;
818
819 /* Make a copy of the expression, because evaluating it may cause it to be
820 * changed. */
821 expr_copy = vim_strsave(expr_line);
822 if (expr_copy == NULL)
823 return NULL;
824
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000825 /* When we are invoked recursively limit the evaluation to 10 levels.
826 * Then return the string as-is. */
827 if (nested >= 10)
828 return expr_copy;
829
830 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000832 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 vim_free(expr_copy);
834 return rv;
835}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000836
837/*
838 * Get the '=' register expression itself, without evaluating it.
839 */
840 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100841get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000842{
843 if (expr_line == NULL)
844 return NULL;
845 return vim_strsave(expr_line);
846}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#endif /* FEAT_EVAL */
848
849/*
850 * Check if 'regname' is a valid name of a yank register.
851 * Note: There is no check for 0 (default register), caller should do this
852 */
853 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854valid_yank_reg(
855 int regname,
856 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
858 if ( (regname > 0 && ASCII_ISALNUM(regname))
859 || (!writing && vim_strchr((char_u *)
860#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100861 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100863 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
865 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100866 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 || regname == '"'
868 || regname == '-'
869 || regname == '_'
870#ifdef FEAT_CLIPBOARD
871 || regname == '*'
872 || regname == '+'
873#endif
874#ifdef FEAT_DND
875 || (!writing && regname == '~')
876#endif
877 )
878 return TRUE;
879 return FALSE;
880}
881
882/*
883 * Set y_current and y_append, according to the value of "regname".
884 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000885 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 *
887 * If regname is 0 and writing, use register 0
888 * If regname is 0 and reading, use previous register
889 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100891get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 int i;
894
895 y_append = FALSE;
896 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
897 {
898 y_current = y_previous;
899 return;
900 }
901 i = regname;
902 if (VIM_ISDIGIT(i))
903 i -= '0';
904 else if (ASCII_ISLOWER(i))
905 i = CharOrdLow(i) + 10;
906 else if (ASCII_ISUPPER(i))
907 {
908 i = CharOrdUp(i) + 10;
909 y_append = TRUE;
910 }
911 else if (regname == '-')
912 i = DELETION_REGISTER;
913#ifdef FEAT_CLIPBOARD
914 /* When selection is not available, use register 0 instead of '*' */
915 else if (clip_star.available && regname == '*')
916 i = STAR_REGISTER;
917 /* When clipboard is not available, use register 0 instead of '+' */
918 else if (clip_plus.available && regname == '+')
919 i = PLUS_REGISTER;
920#endif
921#ifdef FEAT_DND
922 else if (!writing && regname == '~')
923 i = TILDE_REGISTER;
924#endif
925 else /* not 0-9, a-z, A-Z or '-': use register 0 */
926 i = 0;
927 y_current = &(y_regs[i]);
928 if (writing) /* remember the register we write into for do_put() */
929 y_previous = y_current;
930}
931
Bram Moolenaar8299df92004-07-10 09:47:34 +0000932#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933/*
934 * When "regname" is a clipboard register, obtain the selection. If it's not
935 * available return zero, otherwise return "regname".
936 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100938may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 if (regname == '*')
941 {
942 if (!clip_star.available)
943 regname = 0;
944 else
945 clip_get_selection(&clip_star);
946 }
947 else if (regname == '+')
948 {
949 if (!clip_plus.available)
950 regname = 0;
951 else
952 clip_get_selection(&clip_plus);
953 }
954 return regname;
955}
956#endif
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958/*
959 * Obtain the contents of a "normal" register. The register is made empty.
960 * The returned pointer has allocated memory, use put_register() later.
961 */
962 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100963get_register(
964 int name,
965 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000967 struct yankreg *reg;
968 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970#ifdef FEAT_CLIPBOARD
971 /* When Visual area changed, may have to update selection. Obtain the
972 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000973 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200975 if (clip_isautosel_star())
976 clip_update_selection(&clip_star);
977 may_get_selection(name);
978 }
979 if (name == '+' && clip_plus.available)
980 {
981 if (clip_isautosel_plus())
982 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 may_get_selection(name);
984 }
985#endif
986
987 get_yank_register(name, 0);
988 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
989 if (reg != NULL)
990 {
991 *reg = *y_current;
992 if (copy)
993 {
994 /* If we run out of memory some or all of the lines are empty. */
995 if (reg->y_size == 0)
996 reg->y_array = NULL;
997 else
998 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
999 * reg->y_size));
1000 if (reg->y_array != NULL)
1001 {
1002 for (i = 0; i < reg->y_size; ++i)
1003 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1004 }
1005 }
1006 else
1007 y_current->y_array = NULL;
1008 }
1009 return (void *)reg;
1010}
1011
1012/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001013 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 */
1015 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001016put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 get_yank_register(name, 0);
1019 free_yank_all();
1020 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001021 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001023#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 /* Send text written to clipboard register to the clipboard. */
1025 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001028
1029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001030free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031{
1032 struct yankreg tmp;
1033
1034 tmp = *y_current;
1035 *y_current = *(struct yankreg *)reg;
1036 free_yank_all();
1037 vim_free(reg);
1038 *y_current = tmp;
1039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041#if defined(FEAT_MOUSE) || defined(PROTO)
1042/*
1043 * return TRUE if the current yank register has type MLINE
1044 */
1045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001046yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1049 return FALSE;
1050 if (regname == '_') /* black hole is always empty */
1051 return FALSE;
1052 get_yank_register(regname, FALSE);
1053 return (y_current->y_type == MLINE);
1054}
1055#endif
1056
1057/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001058 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001060 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 */
1062 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 char_u *p;
1066 static int regname;
1067 struct yankreg *old_y_previous, *old_y_current;
1068 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 if (Recording == FALSE) /* start recording */
1071 {
1072 /* registers 0-9, a-z and " are allowed */
1073 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1074 retval = FAIL;
1075 else
1076 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001077 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 showmode();
1079 regname = c;
1080 retval = OK;
1081 }
1082 }
1083 else /* stop recording */
1084 {
1085 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001086 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1087 * needs to be removed again to put it in a register. exec_reg then
1088 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 Recording = FALSE;
1091 MSG("");
1092 p = get_recorded();
1093 if (p == NULL)
1094 retval = FAIL;
1095 else
1096 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001097 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1098 vim_unescape_csi(p);
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /*
1101 * We don't want to change the default register here, so save and
1102 * restore the current register name.
1103 */
1104 old_y_previous = y_previous;
1105 old_y_current = y_current;
1106
1107 retval = stuff_yank(regname, p);
1108
1109 y_previous = old_y_previous;
1110 y_current = old_y_current;
1111 }
1112 }
1113 return retval;
1114}
1115
1116/*
1117 * Stuff string "p" into yank register "regname" as a single line (append if
1118 * uppercase). "p" must have been alloced.
1119 *
1120 * return FAIL for failure, OK otherwise
1121 */
1122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001123stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 char_u *lp;
1126 char_u **pp;
1127
1128 /* check for read-only register */
1129 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1130 {
1131 vim_free(p);
1132 return FAIL;
1133 }
1134 if (regname == '_') /* black hole: don't do anything */
1135 {
1136 vim_free(p);
1137 return OK;
1138 }
1139 get_yank_register(regname, TRUE);
1140 if (y_append && y_current->y_array != NULL)
1141 {
1142 pp = &(y_current->y_array[y_current->y_size - 1]);
1143 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1144 if (lp == NULL)
1145 {
1146 vim_free(p);
1147 return FAIL;
1148 }
1149 STRCPY(lp, *pp);
1150 STRCAT(lp, p);
1151 vim_free(p);
1152 vim_free(*pp);
1153 *pp = lp;
1154 }
1155 else
1156 {
1157 free_yank_all();
1158 if ((y_current->y_array =
1159 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1160 {
1161 vim_free(p);
1162 return FAIL;
1163 }
1164 y_current->y_array[0] = p;
1165 y_current->y_size = 1;
1166 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1167 }
1168 return OK;
1169}
1170
Bram Moolenaar42b94362009-05-26 16:12:37 +00001171static int execreg_lastc = NUL;
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173/*
1174 * execute a yank register: copy it into the stuff buffer
1175 *
1176 * return FAIL for failure, OK otherwise
1177 */
1178 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001179do_execreg(
1180 int regname,
1181 int colon, /* insert ':' before each line */
1182 int addcr, /* always add '\n' to end of line */
1183 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 long i;
1186 char_u *p;
1187 int retval = OK;
1188 int remap;
1189
1190 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001191 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001192 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001193 {
1194 EMSG(_("E748: No previously used register"));
1195 return FAIL;
1196 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001197 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 /* check for valid regname */
1200 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001201 {
1202 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001204 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001205 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
1207#ifdef FEAT_CLIPBOARD
1208 regname = may_get_selection(regname);
1209#endif
1210
1211 if (regname == '_') /* black hole: don't stuff anything */
1212 return OK;
1213
1214#ifdef FEAT_CMDHIST
1215 if (regname == ':') /* use last command line */
1216 {
1217 if (last_cmdline == NULL)
1218 {
1219 EMSG(_(e_nolastcmd));
1220 return FAIL;
1221 }
1222 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1223 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001224 /* Escape all control characters with a CTRL-V */
1225 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001226 (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 +00001227 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001228 {
1229 /* When in Visual mode "'<,'>" will be prepended to the command.
1230 * Remove it when it's already there. */
1231 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001232 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001233 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001234 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001235 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001236 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238#endif
1239#ifdef FEAT_EVAL
1240 else if (regname == '=')
1241 {
1242 p = get_expr_line();
1243 if (p == NULL)
1244 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001245 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 vim_free(p);
1247 }
1248#endif
1249 else if (regname == '.') /* use last inserted text */
1250 {
1251 p = get_last_insert_save();
1252 if (p == NULL)
1253 {
1254 EMSG(_(e_noinstext));
1255 return FAIL;
1256 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001257 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 vim_free(p);
1259 }
1260 else
1261 {
1262 get_yank_register(regname, FALSE);
1263 if (y_current->y_array == NULL)
1264 return FAIL;
1265
1266 /* Disallow remaping for ":@r". */
1267 remap = colon ? REMAP_NONE : REMAP_YES;
1268
1269 /*
1270 * Insert lines into typeahead buffer, from last one to first one.
1271 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001272 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 for (i = y_current->y_size; --i >= 0; )
1274 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001275 char_u *escaped;
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 /* insert NL between lines and after last line if type is MLINE */
1278 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1279 || addcr)
1280 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001281 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 return FAIL;
1283 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001284 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1285 if (escaped == NULL)
1286 return FAIL;
1287 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1288 vim_free(escaped);
1289 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001291 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 == FAIL)
1293 return FAIL;
1294 }
1295 Exec_reg = TRUE; /* disable the 'q' command */
1296 }
1297 return retval;
1298}
1299
1300/*
1301 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1302 * used only after other typeahead has been processed.
1303 */
1304 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001305put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 char_u buf[3];
1308
1309 if (restart_edit != NUL)
1310 {
1311 if (restart_edit == 'V')
1312 {
1313 buf[0] = 'g';
1314 buf[1] = 'R';
1315 buf[2] = NUL;
1316 }
1317 else
1318 {
1319 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1320 buf[1] = NUL;
1321 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001322 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 restart_edit = NUL;
1324 }
1325}
1326
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001327/*
1328 * Insert register contents "s" into the typeahead buffer, so that it will be
1329 * executed again.
1330 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1331 * no remapping.
1332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001334put_in_typebuf(
1335 char_u *s,
1336 int esc,
1337 int colon, /* add ':' before the line */
1338 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int retval = OK;
1341
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001342 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001344 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001346 {
1347 char_u *p;
1348
1349 if (esc)
1350 p = vim_strsave_escape_csi(s);
1351 else
1352 p = s;
1353 if (p == NULL)
1354 retval = FAIL;
1355 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001356 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1357 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001358 if (esc)
1359 vim_free(p);
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001362 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 return retval;
1364}
1365
1366/*
1367 * Insert a yank register: copy it into the Read buffer.
1368 * Used by CTRL-R command and middle mouse button in insert mode.
1369 *
1370 * return FAIL for failure, OK otherwise
1371 */
1372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001373insert_reg(
1374 int regname,
1375 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 long i;
1378 int retval = OK;
1379 char_u *arg;
1380 int allocated;
1381
1382 /*
1383 * It is possible to get into an endless loop by having CTRL-R a in
1384 * register a and then, in insert mode, doing CTRL-R a.
1385 * If you hit CTRL-C, the loop will be broken here.
1386 */
1387 ui_breakcheck();
1388 if (got_int)
1389 return FAIL;
1390
1391 /* check for valid regname */
1392 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1393 return FAIL;
1394
1395#ifdef FEAT_CLIPBOARD
1396 regname = may_get_selection(regname);
1397#endif
1398
1399 if (regname == '.') /* insert last inserted text */
1400 retval = stuff_inserted(NUL, 1L, TRUE);
1401 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1402 {
1403 if (arg == NULL)
1404 return FAIL;
1405 stuffescaped(arg, literally);
1406 if (allocated)
1407 vim_free(arg);
1408 }
1409 else /* name or number register */
1410 {
1411 get_yank_register(regname, FALSE);
1412 if (y_current->y_array == NULL)
1413 retval = FAIL;
1414 else
1415 {
1416 for (i = 0; i < y_current->y_size; ++i)
1417 {
1418 stuffescaped(y_current->y_array[i], literally);
1419 /*
1420 * Insert a newline between lines and after last line if
1421 * y_type is MLINE.
1422 */
1423 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1424 stuffcharReadbuff('\n');
1425 }
1426 }
1427 }
1428
1429 return retval;
1430}
1431
1432/*
1433 * Stuff a string into the typeahead buffer, such that edit() will insert it
1434 * literally ("literally" TRUE) or interpret is as typed characters.
1435 */
1436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001437stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 int c;
1440 char_u *start;
1441
1442 while (*arg != NUL)
1443 {
1444 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1445 * stuff K_SPECIAL to get the effect of a special key when "literally"
1446 * is TRUE. */
1447 start = arg;
1448 while ((*arg >= ' '
1449#ifndef EBCDIC
1450 && *arg < DEL /* EBCDIC: chars above space are normal */
1451#endif
1452 )
1453 || (*arg == K_SPECIAL && !literally))
1454 ++arg;
1455 if (arg > start)
1456 stuffReadbuffLen(start, (long)(arg - start));
1457
1458 /* stuff a single special character */
1459 if (*arg != NUL)
1460 {
1461#ifdef FEAT_MBYTE
1462 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001463 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 else
1465#endif
1466 c = *arg++;
1467 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1468 stuffcharReadbuff(Ctrl_V);
1469 stuffcharReadbuff(c);
1470 }
1471 }
1472}
1473
1474/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001475 * If "regname" is a special register, return TRUE and store a pointer to its
1476 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001478 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001479get_spec_reg(
1480 int regname,
1481 char_u **argp,
1482 int *allocated, /* return: TRUE when value was allocated */
1483 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 int cnt;
1486
1487 *argp = NULL;
1488 *allocated = FALSE;
1489 switch (regname)
1490 {
1491 case '%': /* file name */
1492 if (errmsg)
1493 check_fname(); /* will give emsg if not set */
1494 *argp = curbuf->b_fname;
1495 return TRUE;
1496
1497 case '#': /* alternate file name */
1498 *argp = getaltfname(errmsg); /* may give emsg if not set */
1499 return TRUE;
1500
1501#ifdef FEAT_EVAL
1502 case '=': /* result of expression */
1503 *argp = get_expr_line();
1504 *allocated = TRUE;
1505 return TRUE;
1506#endif
1507
1508 case ':': /* last command line */
1509 if (last_cmdline == NULL && errmsg)
1510 EMSG(_(e_nolastcmd));
1511 *argp = last_cmdline;
1512 return TRUE;
1513
1514 case '/': /* last search-pattern */
1515 if (last_search_pat() == NULL && errmsg)
1516 EMSG(_(e_noprevre));
1517 *argp = last_search_pat();
1518 return TRUE;
1519
1520 case '.': /* last inserted text */
1521 *argp = get_last_insert_save();
1522 *allocated = TRUE;
1523 if (*argp == NULL && errmsg)
1524 EMSG(_(e_noinstext));
1525 return TRUE;
1526
1527#ifdef FEAT_SEARCHPATH
1528 case Ctrl_F: /* Filename under cursor */
1529 case Ctrl_P: /* Path under cursor, expand via "path" */
1530 if (!errmsg)
1531 return FALSE;
1532 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001533 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 *allocated = TRUE;
1535 return TRUE;
1536#endif
1537
1538 case Ctrl_W: /* word under cursor */
1539 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1540 if (!errmsg)
1541 return FALSE;
1542 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1543 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1544 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1545 *allocated = TRUE;
1546 return TRUE;
1547
1548 case '_': /* black hole: always empty */
1549 *argp = (char_u *)"";
1550 return TRUE;
1551 }
1552
1553 return FALSE;
1554}
1555
1556/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001557 * Paste a yank register into the command line.
1558 * Only for non-special registers.
1559 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 * insert_reg() can't be used here, because special characters from the
1561 * register contents will be interpreted as commands.
1562 *
1563 * return FAIL for failure, OK otherwise
1564 */
1565 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001566cmdline_paste_reg(
1567 int regname,
1568 int literally, /* Insert text literally instead of "as typed" */
1569 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573 get_yank_register(regname, FALSE);
1574 if (y_current->y_array == NULL)
1575 return FAIL;
1576
1577 for (i = 0; i < y_current->y_size; ++i)
1578 {
1579 cmdline_paste_str(y_current->y_array[i], literally);
1580
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001581 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001582 * Don't do this when "remcr" is TRUE. */
1583 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 cmdline_paste_str((char_u *)"\r", literally);
1585
1586 /* Check for CTRL-C, in case someone tries to paste a few thousand
1587 * lines and gets bored. */
1588 ui_breakcheck();
1589 if (got_int)
1590 return FAIL;
1591 }
1592 return OK;
1593}
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1596/*
1597 * Adjust the register name pointed to with "rp" for the clipboard being
1598 * used always and the clipboard being available.
1599 */
1600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001603 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1604 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001605 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1606 {
1607 if (clip_unnamed != 0)
1608 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001609 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001610 else
1611 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1612 ? '+' : '*';
1613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (!clip_star.available && *rp == '*')
1615 *rp = 0;
1616 if (!clip_plus.available && *rp == '+')
1617 *rp = 0;
1618}
1619#endif
1620
1621/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001622 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001624 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
1626 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001627op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 int n;
1630 linenr_T lnum;
1631 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 char_u *newp, *oldp;
1633 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1635 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001636 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1639 return OK;
1640
1641 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1642 if (oap->empty)
1643 return u_save_cursor();
1644
1645 if (!curbuf->b_p_ma)
1646 {
1647 EMSG(_(e_modifiable));
1648 return FAIL;
1649 }
1650
1651#ifdef FEAT_CLIPBOARD
1652 adjust_clip_reg(&oap->regname);
1653#endif
1654
1655#ifdef FEAT_MBYTE
1656 if (has_mbyte)
1657 mb_adjust_opend(oap);
1658#endif
1659
Bram Moolenaard04b7502010-07-08 22:27:55 +02001660 /*
1661 * Imitate the strange Vi behaviour: If the delete spans more than one
1662 * line and motion_type == MCHAR and the result is a blank line, make the
1663 * delete linewise. Don't do this for the change command or Visual mode.
1664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001667 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001669 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 && oap->op_type == OP_DELETE)
1671 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001672 ptr = ml_get(oap->end.lnum) + oap->end.col;
1673 if (*ptr != NUL)
1674 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 ptr = skipwhite(ptr);
1676 if (*ptr == NUL && inindent(0))
1677 oap->motion_type = MLINE;
1678 }
1679
Bram Moolenaard04b7502010-07-08 22:27:55 +02001680 /*
1681 * Check for trying to delete (e.g. "D") in an empty line.
1682 * Note: For the change operator it is ok.
1683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if ( oap->motion_type == MCHAR
1685 && oap->line_count == 1
1686 && oap->op_type == OP_DELETE
1687 && *ml_get(oap->start.lnum) == NUL)
1688 {
1689 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001690 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 * 'cpoptions' (Vi compatible).
1692 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001693#ifdef FEAT_VIRTUALEDIT
1694 if (virtual_op)
1695 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1696 * marks as if it happened. */
1697 goto setmarks;
1698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1700 beep_flush();
1701 return OK;
1702 }
1703
Bram Moolenaard04b7502010-07-08 22:27:55 +02001704 /*
1705 * Do a yank of whatever we're about to delete.
1706 * If a yank register was specified, put the deleted text into that
1707 * register. For the black hole register '_' don't yank anything.
1708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (oap->regname != '_')
1710 {
1711 if (oap->regname != 0)
1712 {
1713 /* check for read-only register */
1714 if (!valid_yank_reg(oap->regname, TRUE))
1715 {
1716 beep_flush();
1717 return OK;
1718 }
1719 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1720 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1721 did_yank = TRUE;
1722 }
1723
1724 /*
1725 * Put deleted text into register 1 and shift number registers if the
1726 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001727 * Use the register name from before adjust_clip_reg() may have
1728 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001730 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 || oap->line_count > 1 || oap->use_reg_one)
1732 {
1733 y_current = &y_regs[9];
1734 free_yank_all(); /* free register nine */
1735 for (n = 9; n > 1; --n)
1736 y_regs[n] = y_regs[n - 1];
1737 y_previous = y_current = &y_regs[1];
1738 y_regs[1].y_array = NULL; /* set register one to empty */
1739 if (op_yank(oap, TRUE, FALSE) == OK)
1740 did_yank = TRUE;
1741 }
1742
Bram Moolenaar84298db2012-04-20 13:46:08 +02001743 /* Yank into small delete register when no named register specified
1744 * and the delete is within one line. */
1745 if ((
1746#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001747 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1748 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001749#endif
1750 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 && oap->line_count == 1)
1752 {
1753 oap->regname = '-';
1754 get_yank_register(oap->regname, TRUE);
1755 if (op_yank(oap, TRUE, FALSE) == OK)
1756 did_yank = TRUE;
1757 oap->regname = 0;
1758 }
1759
1760 /*
1761 * If there's too much stuff to fit in the yank register, then get a
1762 * confirmation before doing the delete. This is crude, but simple.
1763 * And it avoids doing a delete of something we can't put back if we
1764 * want.
1765 */
1766 if (!did_yank)
1767 {
1768 int msg_silent_save = msg_silent;
1769
1770 msg_silent = 0; /* must display the prompt */
1771 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1772 msg_silent = msg_silent_save;
1773 if (n != 'y')
1774 {
1775 EMSG(_(e_abort));
1776 return FAIL;
1777 }
1778 }
1779 }
1780
Bram Moolenaard04b7502010-07-08 22:27:55 +02001781 /*
1782 * block mode delete
1783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (oap->block_mode)
1785 {
1786 if (u_save((linenr_T)(oap->start.lnum - 1),
1787 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1788 return FAIL;
1789
1790 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1791 {
1792 block_prep(oap, &bd, lnum, TRUE);
1793 if (bd.textlen == 0) /* nothing to delete */
1794 continue;
1795
1796 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1797 if (lnum == curwin->w_cursor.lnum)
1798 {
1799 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1800# ifdef FEAT_VIRTUALEDIT
1801 curwin->w_cursor.coladd = 0;
1802# endif
1803 }
1804
1805 /* n == number of chars deleted
1806 * If we delete a TAB, it may be replaced by several characters.
1807 * Thus the number of characters may increase!
1808 */
1809 n = bd.textlen - bd.startspaces - bd.endspaces;
1810 oldp = ml_get(lnum);
1811 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1812 if (newp == NULL)
1813 continue;
1814 /* copy up to deleted part */
1815 mch_memmove(newp, oldp, (size_t)bd.textcol);
1816 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001817 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 (size_t)(bd.startspaces + bd.endspaces));
1819 /* copy the part after the deleted part */
1820 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001821 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /* replace the line */
1823 ml_replace(lnum, newp, FALSE);
1824 }
1825
1826 check_cursor_col();
1827 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1828 oap->end.lnum + 1, 0L);
1829 oap->line_count = 0; /* no lines deleted */
1830 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001831 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 if (oap->op_type == OP_CHANGE)
1834 {
1835 /* Delete the lines except the first one. Temporarily move the
1836 * cursor to the next line. Save the current line number, if the
1837 * last line is deleted it may be changed.
1838 */
1839 if (oap->line_count > 1)
1840 {
1841 lnum = curwin->w_cursor.lnum;
1842 ++curwin->w_cursor.lnum;
1843 del_lines((long)(oap->line_count - 1), TRUE);
1844 curwin->w_cursor.lnum = lnum;
1845 }
1846 if (u_save_cursor() == FAIL)
1847 return FAIL;
1848 if (curbuf->b_p_ai) /* don't delete indent */
1849 {
1850 beginline(BL_WHITE); /* cursor on first non-white */
1851 did_ai = TRUE; /* delete the indent when ESC hit */
1852 ai_col = curwin->w_cursor.col;
1853 }
1854 else
1855 beginline(0); /* cursor in column 0 */
1856 truncate_line(FALSE); /* delete the rest of the line */
1857 /* leave cursor past last char in line */
1858 if (oap->line_count > 1)
1859 u_clearline(); /* "U" command not possible after "2cc" */
1860 }
1861 else
1862 {
1863 del_lines(oap->line_count, TRUE);
1864 beginline(BL_WHITE | BL_FIX);
1865 u_clearline(); /* "U" command not possible after "dd" */
1866 }
1867 }
1868 else
1869 {
1870#ifdef FEAT_VIRTUALEDIT
1871 if (virtual_op)
1872 {
1873 int endcol = 0;
1874
1875 /* For virtualedit: break the tabs that are partly included. */
1876 if (gchar_pos(&oap->start) == '\t')
1877 {
1878 if (u_save_cursor() == FAIL) /* save first line for undo */
1879 return FAIL;
1880 if (oap->line_count == 1)
1881 endcol = getviscol2(oap->end.col, oap->end.coladd);
1882 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1883 oap->start = curwin->w_cursor;
1884 if (oap->line_count == 1)
1885 {
1886 coladvance(endcol);
1887 oap->end.col = curwin->w_cursor.col;
1888 oap->end.coladd = curwin->w_cursor.coladd;
1889 curwin->w_cursor = oap->start;
1890 }
1891 }
1892
1893 /* Break a tab only when it's included in the area. */
1894 if (gchar_pos(&oap->end) == '\t'
1895 && (int)oap->end.coladd < oap->inclusive)
1896 {
1897 /* save last line for undo */
1898 if (u_save((linenr_T)(oap->end.lnum - 1),
1899 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1900 return FAIL;
1901 curwin->w_cursor = oap->end;
1902 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1903 oap->end = curwin->w_cursor;
1904 curwin->w_cursor = oap->start;
1905 }
1906 }
1907#endif
1908
1909 if (oap->line_count == 1) /* delete characters within one line */
1910 {
1911 if (u_save_cursor() == FAIL) /* save line for undo */
1912 return FAIL;
1913
1914 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001915 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 && oap->op_type == OP_CHANGE
1917 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001918 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 display_dollar(oap->end.col - !oap->inclusive);
1920
1921 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1922
1923#ifdef FEAT_VIRTUALEDIT
1924 if (virtual_op)
1925 {
1926 /* fix up things for virtualedit-delete:
1927 * break the tabs which are going to get in our way
1928 */
1929 char_u *curline = ml_get_curline();
1930 int len = (int)STRLEN(curline);
1931
1932 if (oap->end.coladd != 0
1933 && (int)oap->end.col >= len - 1
1934 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1935 n++;
1936 /* Delete at least one char (e.g, when on a control char). */
1937 if (n == 0 && oap->start.coladd != oap->end.coladd)
1938 n = 1;
1939
1940 /* When deleted a char in the line, reset coladd. */
1941 if (gchar_cursor() != NUL)
1942 curwin->w_cursor.coladd = 0;
1943 }
1944#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001945 (void)del_bytes((long)n, !virtual_op,
1946 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else /* delete characters between lines */
1949 {
1950 pos_T curpos;
1951
1952 /* save deleted and changed lines for undo */
1953 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1954 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1955 return FAIL;
1956
1957 truncate_line(TRUE); /* delete from cursor to end of line */
1958
1959 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1960 ++curwin->w_cursor.lnum;
1961 del_lines((long)(oap->line_count - 2), FALSE);
1962
Bram Moolenaard009e862015-06-09 20:20:03 +02001963 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001964 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001965 curwin->w_cursor.col = 0;
1966 (void)del_bytes((long)n, !virtual_op,
1967 oap->op_type == OP_DELETE && !oap->is_VIsual);
1968 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1969 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
1972
1973 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1974
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001975#ifdef FEAT_VIRTUALEDIT
1976setmarks:
1977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (oap->block_mode)
1979 {
1980 curbuf->b_op_end.lnum = oap->end.lnum;
1981 curbuf->b_op_end.col = oap->start.col;
1982 }
1983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 curbuf->b_op_end = oap->start;
1985 curbuf->b_op_start = oap->start;
1986
1987 return OK;
1988}
1989
1990#ifdef FEAT_MBYTE
1991/*
1992 * Adjust end of operating area for ending on a multi-byte character.
1993 * Used for deletion.
1994 */
1995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 char_u *p;
1999
2000 if (oap->inclusive)
2001 {
2002 p = ml_get(oap->end.lnum);
2003 oap->end.col += mb_tail_off(p, p + oap->end.col);
2004 }
2005}
2006#endif
2007
2008#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2009/*
2010 * Replace a whole area with one character.
2011 */
2012 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002013op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014{
2015 int n, numc;
2016#ifdef FEAT_MBYTE
2017 int num_chars;
2018#endif
2019 char_u *newp, *oldp;
2020 size_t oldlen;
2021 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002022 char_u *after_p = NULL;
2023 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2026 return OK; /* nothing to do */
2027
Bram Moolenaard9820532013-11-04 01:41:17 +01002028 if (had_ctrl_v_cr)
2029 c = (c == -1 ? '\r' : '\n');
2030
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031#ifdef FEAT_MBYTE
2032 if (has_mbyte)
2033 mb_adjust_opend(oap);
2034#endif
2035
2036 if (u_save((linenr_T)(oap->start.lnum - 1),
2037 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2038 return FAIL;
2039
2040 /*
2041 * block mode replace
2042 */
2043 if (oap->block_mode)
2044 {
2045 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2046 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2047 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002048 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2050 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2051 continue; /* nothing to replace */
2052
2053 /* n == number of extra chars required
2054 * If we split a TAB, it may be replaced by several characters.
2055 * Thus the number of characters may increase!
2056 */
2057#ifdef FEAT_VIRTUALEDIT
2058 /* If the range starts in virtual space, count the initial
2059 * coladd offset as part of "startspaces" */
2060 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2061 {
2062 pos_T vpos;
2063
Bram Moolenaara1381de2009-11-03 15:44:21 +00002064 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 getvpos(&vpos, oap->start_vcol);
2066 bd.startspaces += vpos.coladd;
2067 n = bd.startspaces;
2068 }
2069 else
2070#endif
2071 /* allow for pre spaces */
2072 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2073
2074 /* allow for post spp */
2075 n += (bd.endspaces
2076#ifdef FEAT_VIRTUALEDIT
2077 && !bd.is_oneChar
2078#endif
2079 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2080 /* Figure out how many characters to replace. */
2081 numc = oap->end_vcol - oap->start_vcol + 1;
2082 if (bd.is_short && (!virtual_op || bd.is_MAX))
2083 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2084
2085#ifdef FEAT_MBYTE
2086 /* A double-wide character can be replaced only up to half the
2087 * times. */
2088 if ((*mb_char2cells)(c) > 1)
2089 {
2090 if ((numc & 1) && !bd.is_short)
2091 {
2092 ++bd.endspaces;
2093 ++n;
2094 }
2095 numc = numc / 2;
2096 }
2097
2098 /* Compute bytes needed, move character count to num_chars. */
2099 num_chars = numc;
2100 numc *= (*mb_char2len)(c);
2101#endif
2102 /* oldlen includes textlen, so don't double count */
2103 n += numc - bd.textlen;
2104
2105 oldp = ml_get_curline();
2106 oldlen = STRLEN(oldp);
2107 newp = alloc_check((unsigned)oldlen + 1 + n);
2108 if (newp == NULL)
2109 continue;
2110 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2111 /* copy up to deleted part */
2112 mch_memmove(newp, oldp, (size_t)bd.textcol);
2113 oldp += bd.textcol + bd.textlen;
2114 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002115 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002117 /* -1/-2 is used for entering CR literally. */
2118 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002120#ifdef FEAT_MBYTE
2121 if (has_mbyte)
2122 {
2123 n = (int)STRLEN(newp);
2124 while (--num_chars >= 0)
2125 n += (*mb_char2bytes)(c, newp + n);
2126 }
2127 else
2128#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002129 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002130 if (!bd.is_short)
2131 {
2132 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002133 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002134 /* copy the part after the changed part */
2135 STRMOVE(newp + STRLEN(newp), oldp);
2136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002140 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002141 after_p = alloc_check(
2142 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002143 if (after_p != NULL)
2144 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 /* replace the line */
2147 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002148 if (after_p != NULL)
2149 {
2150 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2151 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2152 oap->end.lnum++;
2153 vim_free(after_p);
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 }
2157 else
2158 {
2159 /*
2160 * MCHAR and MLINE motion replace.
2161 */
2162 if (oap->motion_type == MLINE)
2163 {
2164 oap->start.col = 0;
2165 curwin->w_cursor.col = 0;
2166 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2167 if (oap->end.col)
2168 --oap->end.col;
2169 }
2170 else if (!oap->inclusive)
2171 dec(&(oap->end));
2172
2173 while (ltoreq(curwin->w_cursor, oap->end))
2174 {
2175 n = gchar_cursor();
2176 if (n != NUL)
2177 {
2178#ifdef FEAT_MBYTE
2179 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2180 {
2181 /* This is slow, but it handles replacing a single-byte
2182 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002183 if (curwin->w_cursor.lnum == oap->end.lnum)
2184 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 n = State;
2186 State = REPLACE;
2187 ins_char(c);
2188 State = n;
2189 /* Backup to the replaced character. */
2190 dec_cursor();
2191 }
2192 else
2193#endif
2194 {
2195#ifdef FEAT_VIRTUALEDIT
2196 if (n == TAB)
2197 {
2198 int end_vcol = 0;
2199
2200 if (curwin->w_cursor.lnum == oap->end.lnum)
2201 {
2202 /* oap->end has to be recalculated when
2203 * the tab breaks */
2204 end_vcol = getviscol2(oap->end.col,
2205 oap->end.coladd);
2206 }
2207 coladvance_force(getviscol());
2208 if (curwin->w_cursor.lnum == oap->end.lnum)
2209 getvpos(&oap->end, end_vcol);
2210 }
2211#endif
2212 pchar(curwin->w_cursor, c);
2213 }
2214 }
2215#ifdef FEAT_VIRTUALEDIT
2216 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2217 {
2218 int virtcols = oap->end.coladd;
2219
2220 if (curwin->w_cursor.lnum == oap->start.lnum
2221 && oap->start.col == oap->end.col && oap->start.coladd)
2222 virtcols -= oap->start.coladd;
2223
2224 /* oap->end has been trimmed so it's effectively inclusive;
2225 * as a result an extra +1 must be counted so we don't
2226 * trample the NUL byte. */
2227 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2228 curwin->w_cursor.col -= (virtcols + 1);
2229 for (; virtcols >= 0; virtcols--)
2230 {
2231 pchar(curwin->w_cursor, c);
2232 if (inc(&curwin->w_cursor) == -1)
2233 break;
2234 }
2235 }
2236#endif
2237
2238 /* Advance to next character, stop at the end of the file. */
2239 if (inc_cursor() == -1)
2240 break;
2241 }
2242 }
2243
2244 curwin->w_cursor = oap->start;
2245 check_cursor();
2246 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2247
2248 /* Set "'[" and "']" marks. */
2249 curbuf->b_op_start = oap->start;
2250 curbuf->b_op_end = oap->end;
2251
2252 return OK;
2253}
2254#endif
2255
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002256static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258/*
2259 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2260 */
2261 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002262op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002266 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 if (u_save((linenr_T)(oap->start.lnum - 1),
2269 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2270 return;
2271
2272 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (oap->block_mode) /* Visual block mode */
2274 {
2275 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2276 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002277 int one_change;
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 block_prep(oap, &bd, pos.lnum, FALSE);
2280 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002281 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2282 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002283
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002284#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002285 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2288
Bram Moolenaar009b2592004-10-24 19:18:58 +00002289 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2290 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002292 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 if (did_change)
2297 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2298 }
2299 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 if (oap->motion_type == MLINE)
2302 {
2303 oap->start.col = 0;
2304 pos.col = 0;
2305 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2306 if (oap->end.col)
2307 --oap->end.col;
2308 }
2309 else if (!oap->inclusive)
2310 dec(&(oap->end));
2311
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002312 if (pos.lnum == oap->end.lnum)
2313 did_change = swapchars(oap->op_type, &pos,
2314 oap->end.col - pos.col + 1);
2315 else
2316 for (;;)
2317 {
2318 did_change |= swapchars(oap->op_type, &pos,
2319 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2320 (int)STRLEN(ml_get_pos(&pos)));
2321 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2322 break;
2323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (did_change)
2325 {
2326 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2327 0L);
2328#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002329 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
2331 char_u *ptr;
2332 int count;
2333
2334 pos = oap->start;
2335 while (pos.lnum < oap->end.lnum)
2336 {
2337 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002338 count = (int)STRLEN(ptr) - pos.col;
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 pos.col = 0;
2343 pos.lnum++;
2344 }
2345 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2346 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002347 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002349 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351#endif
2352 }
2353 }
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 if (!did_change && oap->is_VIsual)
2356 /* No change: need to remove the Visual selection */
2357 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 /*
2360 * Set '[ and '] marks.
2361 */
2362 curbuf->b_op_start = oap->start;
2363 curbuf->b_op_end = oap->end;
2364
2365 if (oap->line_count > p_report)
2366 {
2367 if (oap->line_count == 1)
2368 MSG(_("1 line changed"));
2369 else
2370 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2371 }
2372}
2373
2374/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002375 * Invoke swapchar() on "length" bytes at position "pos".
2376 * "pos" is advanced to just after the changed characters.
2377 * "length" is rounded up to include the whole last multi-byte character.
2378 * Also works correctly when the number of bytes changes.
2379 * Returns TRUE if some character was changed.
2380 */
2381 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002382swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002383{
2384 int todo;
2385 int did_change = 0;
2386
2387 for (todo = length; todo > 0; --todo)
2388 {
2389# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002390 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002391 {
2392 int len = (*mb_ptr2len)(ml_get_pos(pos));
2393
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002394 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002395 if (len > 0)
2396 todo -= len - 1;
2397 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002398# endif
2399 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002400 if (inc(pos) == -1) /* at end of file */
2401 break;
2402 }
2403 return did_change;
2404}
2405
2406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 * If op_type == OP_UPPER: make uppercase,
2408 * if op_type == OP_LOWER: make lowercase,
2409 * if op_type == OP_ROT13: do rot13 encoding,
2410 * else swap case of character at 'pos'
2411 * returns TRUE when something actually changed.
2412 */
2413 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002414swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 int c;
2417 int nc;
2418
2419 c = gchar_pos(pos);
2420
2421 /* Only do rot13 encoding for ASCII characters. */
2422 if (c >= 0x80 && op_type == OP_ROT13)
2423 return FALSE;
2424
2425#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002426 if (op_type == OP_UPPER && c == 0xdf
2427 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002428 {
2429 pos_T sp = curwin->w_cursor;
2430
2431 /* Special handling of German sharp s: change to "SS". */
2432 curwin->w_cursor = *pos;
2433 del_char(FALSE);
2434 ins_char('S');
2435 ins_char('S');
2436 curwin->w_cursor = sp;
2437 inc(pos);
2438 }
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2441 return FALSE;
2442#endif
2443 nc = c;
2444 if (MB_ISLOWER(c))
2445 {
2446 if (op_type == OP_ROT13)
2447 nc = ROT13(c, 'a');
2448 else if (op_type != OP_LOWER)
2449 nc = MB_TOUPPER(c);
2450 }
2451 else if (MB_ISUPPER(c))
2452 {
2453 if (op_type == OP_ROT13)
2454 nc = ROT13(c, 'A');
2455 else if (op_type != OP_UPPER)
2456 nc = MB_TOLOWER(c);
2457 }
2458 if (nc != c)
2459 {
2460#ifdef FEAT_MBYTE
2461 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2462 {
2463 pos_T sp = curwin->w_cursor;
2464
2465 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002466 /* don't use del_char(), it also removes composing chars */
2467 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 ins_char(nc);
2469 curwin->w_cursor = sp;
2470 }
2471 else
2472#endif
2473 pchar(*pos, nc);
2474 return TRUE;
2475 }
2476 return FALSE;
2477}
2478
2479#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2480/*
2481 * op_insert - Insert and append operators for Visual mode.
2482 */
2483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002484op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 long ins_len, pre_textlen = 0;
2487 char_u *firstline, *ins_text;
2488 struct block_def bd;
2489 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002490 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 /* edit() changes this - record it for OP_APPEND */
2493 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2494
2495 /* vis block is still marked. Get rid of it now. */
2496 curwin->w_cursor.lnum = oap->start.lnum;
2497 update_screen(INVERTED);
2498
2499 if (oap->block_mode)
2500 {
2501#ifdef FEAT_VIRTUALEDIT
2502 /* When 'virtualedit' is used, need to insert the extra spaces before
2503 * doing block_prep(). When only "block" is used, virtual edit is
2504 * already disabled, but still need it when calling
2505 * coladvance_force(). */
2506 if (curwin->w_cursor.coladd > 0)
2507 {
2508 int old_ve_flags = ve_flags;
2509
2510 ve_flags = VE_ALL;
2511 if (u_save_cursor() == FAIL)
2512 return;
2513 coladvance_force(oap->op_type == OP_APPEND
2514 ? oap->end_vcol + 1 : getviscol());
2515 if (oap->op_type == OP_APPEND)
2516 --curwin->w_cursor.col;
2517 ve_flags = old_ve_flags;
2518 }
2519#endif
2520 /* Get the info about the block before entering the text */
2521 block_prep(oap, &bd, oap->start.lnum, TRUE);
2522 firstline = ml_get(oap->start.lnum) + bd.textcol;
2523 if (oap->op_type == OP_APPEND)
2524 firstline += bd.textlen;
2525 pre_textlen = (long)STRLEN(firstline);
2526 }
2527
2528 if (oap->op_type == OP_APPEND)
2529 {
2530 if (oap->block_mode
2531#ifdef FEAT_VIRTUALEDIT
2532 && curwin->w_cursor.coladd == 0
2533#endif
2534 )
2535 {
2536 /* Move the cursor to the character right of the block. */
2537 curwin->w_set_curswant = TRUE;
2538 while (*ml_get_cursor() != NUL
2539 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2540 ++curwin->w_cursor.col;
2541 if (bd.is_short && !bd.is_MAX)
2542 {
2543 /* First line was too short, make it longer and adjust the
2544 * values in "bd". */
2545 if (u_save_cursor() == FAIL)
2546 return;
2547 for (i = 0; i < bd.endspaces; ++i)
2548 ins_char(' ');
2549 bd.textlen += bd.endspaces;
2550 }
2551 }
2552 else
2553 {
2554 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002555 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557 /* Works just like an 'i'nsert on the next character. */
2558 if (!lineempty(curwin->w_cursor.lnum)
2559 && oap->start_vcol != oap->end_vcol)
2560 inc_cursor();
2561 }
2562 }
2563
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002564 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 edit(NUL, FALSE, (linenr_T)count1);
2566
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002567 /* When a tab was inserted, and the characters in front of the tab
2568 * have been converted to a tab as well, the column of the cursor
2569 * might have actually been reduced, so need to adjust here. */
2570 if (t1.lnum == curbuf->b_op_start_orig.lnum
2571 && lt(curbuf->b_op_start_orig, t1))
2572 oap->start = curbuf->b_op_start_orig;
2573
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002574 /* If user has moved off this line, we don't know what to do, so do
2575 * nothing.
2576 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2577 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return;
2579
2580 if (oap->block_mode)
2581 {
2582 struct block_def bd2;
2583
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002584 /* The user may have moved the cursor before inserting something, try
2585 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002586 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002587 {
2588 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002589 && oap->start.col
2590#ifdef FEAT_VIRTUALEDIT
2591 + oap->start.coladd
2592#endif
2593 != curbuf->b_op_start_orig.col
2594#ifdef FEAT_VIRTUALEDIT
2595 + curbuf->b_op_start_orig.coladd
2596#endif
2597 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002598 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002599 int t = getviscol2(curbuf->b_op_start_orig.col,
2600 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002601 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002602 pre_textlen -= t - oap->start_vcol;
2603 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002604 }
2605 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002606 && oap->end.col
2607#ifdef FEAT_VIRTUALEDIT
2608 + oap->end.coladd
2609#endif
2610 >= curbuf->b_op_start_orig.col
2611#ifdef FEAT_VIRTUALEDIT
2612 + curbuf->b_op_start_orig.coladd
2613#endif
2614 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002615 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002616 int t = getviscol2(curbuf->b_op_start_orig.col,
2617 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002618 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002619 /* reset pre_textlen to the value of OP_INSERT */
2620 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002621 pre_textlen -= t - oap->start_vcol;
2622 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002623 oap->op_type = OP_INSERT;
2624 }
2625 }
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002629 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 * Don't do this when "$" used, end-of-line will have changed.
2631 */
2632 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2633 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2634 {
2635 if (oap->op_type == OP_APPEND)
2636 {
2637 pre_textlen += bd2.textlen - bd.textlen;
2638 if (bd2.endspaces)
2639 --bd2.textlen;
2640 }
2641 bd.textcol = bd2.textcol;
2642 bd.textlen = bd2.textlen;
2643 }
2644
2645 /*
2646 * Subsequent calls to ml_get() flush the firstline data - take a
2647 * copy of the required string.
2648 */
2649 firstline = ml_get(oap->start.lnum) + bd.textcol;
2650 if (oap->op_type == OP_APPEND)
2651 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002652 if (pre_textlen >= 0
2653 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 ins_text = vim_strnsave(firstline, (int)ins_len);
2656 if (ins_text != NULL)
2657 {
2658 /* block handled here */
2659 if (u_save(oap->start.lnum,
2660 (linenr_T)(oap->end.lnum + 1)) == OK)
2661 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2662 &bd);
2663
2664 curwin->w_cursor.col = oap->start.col;
2665 check_cursor();
2666 vim_free(ins_text);
2667 }
2668 }
2669 }
2670}
2671#endif
2672
2673/*
2674 * op_change - handle a change operation
2675 *
2676 * return TRUE if edit() returns because of a CTRL-O command
2677 */
2678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 colnr_T l;
2682 int retval;
2683#ifdef FEAT_VISUALEXTRA
2684 long offset;
2685 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002686 long ins_len;
2687 long pre_textlen = 0;
2688 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 char_u *firstline;
2690 char_u *ins_text, *newp, *oldp;
2691 struct block_def bd;
2692#endif
2693
2694 l = oap->start.col;
2695 if (oap->motion_type == MLINE)
2696 {
2697 l = 0;
2698#ifdef FEAT_SMARTINDENT
2699 if (!p_paste && curbuf->b_p_si
2700# ifdef FEAT_CINDENT
2701 && !curbuf->b_p_cin
2702# endif
2703 )
2704 can_si = TRUE; /* It's like opening a new line, do si */
2705#endif
2706 }
2707
2708 /* First delete the text in the region. In an empty buffer only need to
2709 * save for undo */
2710 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2711 {
2712 if (u_save_cursor() == FAIL)
2713 return FALSE;
2714 }
2715 else if (op_delete(oap) == FAIL)
2716 return FALSE;
2717
2718 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2719 && !virtual_op)
2720 inc_cursor();
2721
2722#ifdef FEAT_VISUALEXTRA
2723 /* check for still on same line (<CR> in inserted text meaningless) */
2724 /* skip blank lines too */
2725 if (oap->block_mode)
2726 {
2727# ifdef FEAT_VIRTUALEDIT
2728 /* Add spaces before getting the current line length. */
2729 if (virtual_op && (curwin->w_cursor.coladd > 0
2730 || gchar_cursor() == NUL))
2731 coladvance_force(getviscol());
2732# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002733 firstline = ml_get(oap->start.lnum);
2734 pre_textlen = (long)STRLEN(firstline);
2735 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 bd.textcol = curwin->w_cursor.col;
2737 }
2738#endif
2739
2740#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2741 if (oap->motion_type == MLINE)
2742 fix_indent();
2743#endif
2744
2745 retval = edit(NUL, FALSE, (linenr_T)1);
2746
2747#ifdef FEAT_VISUALEXTRA
2748 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002749 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002751 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002753 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002755 /* Auto-indenting may have changed the indent. If the cursor was past
2756 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002758 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002760 long new_indent = (long)(skipwhite(firstline) - firstline);
2761
2762 pre_textlen += new_indent - pre_indent;
2763 bd.textcol += new_indent - pre_indent;
2764 }
2765
2766 ins_len = (long)STRLEN(firstline) - pre_textlen;
2767 if (ins_len > 0)
2768 {
2769 /* Subsequent calls to ml_get() flush the firstline data - take a
2770 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2772 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002773 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2775 linenr++)
2776 {
2777 block_prep(oap, &bd, linenr, TRUE);
2778 if (!bd.is_short || virtual_op)
2779 {
2780# ifdef FEAT_VIRTUALEDIT
2781 pos_T vpos;
2782
2783 /* If the block starts in virtual space, count the
2784 * initial coladd offset as part of "startspaces" */
2785 if (bd.is_short)
2786 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002787 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 else
2791 vpos.coladd = 0;
2792# endif
2793 oldp = ml_get(linenr);
2794 newp = alloc_check((unsigned)(STRLEN(oldp)
2795# ifdef FEAT_VIRTUALEDIT
2796 + vpos.coladd
2797# endif
2798 + ins_len + 1));
2799 if (newp == NULL)
2800 continue;
2801 /* copy up to block start */
2802 mch_memmove(newp, oldp, (size_t)bd.textcol);
2803 offset = bd.textcol;
2804# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002805 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 offset += vpos.coladd;
2807# endif
2808 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2809 offset += ins_len;
2810 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002811 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 ml_replace(linenr, newp, FALSE);
2813 }
2814 }
2815 check_cursor();
2816
2817 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2818 }
2819 vim_free(ins_text);
2820 }
2821 }
2822#endif
2823
2824 return retval;
2825}
2826
2827/*
2828 * set all the yank registers to empty (called from main())
2829 */
2830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002831init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 int i;
2834
2835 for (i = 0; i < NUM_REGISTERS; ++i)
2836 y_regs[i].y_array = NULL;
2837}
2838
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002839#if defined(EXITFREE) || defined(PROTO)
2840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002842{
2843 int i;
2844
2845 for (i = 0; i < NUM_REGISTERS; ++i)
2846 {
2847 y_current = &y_regs[i];
2848 if (y_current->y_array != NULL)
2849 free_yank_all();
2850 }
2851}
2852#endif
2853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854/*
2855 * Free "n" lines from the current yank register.
2856 * Called for normal freeing and in case of error.
2857 */
2858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002859free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
2861 if (y_current->y_array != NULL)
2862 {
2863 long i;
2864
2865 for (i = n; --i >= 0; )
2866 {
2867#ifdef AMIGA /* only for very slow machines */
2868 if ((i & 1023) == 1023) /* this may take a while */
2869 {
2870 /*
2871 * This message should never cause a hit-return message.
2872 * Overwrite this message with any next message.
2873 */
2874 ++no_wait_return;
2875 smsg((char_u *)_("freeing %ld lines"), i + 1);
2876 --no_wait_return;
2877 msg_didout = FALSE;
2878 msg_col = 0;
2879 }
2880#endif
2881 vim_free(y_current->y_array[i]);
2882 }
2883 vim_free(y_current->y_array);
2884 y_current->y_array = NULL;
2885#ifdef AMIGA
2886 if (n >= 1000)
2887 MSG("");
2888#endif
2889 }
2890}
2891
2892 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002893free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 free_yank(y_current->y_size);
2896}
2897
2898/*
2899 * Yank the text between "oap->start" and "oap->end" into a yank register.
2900 * If we are to append (uppercase register), we first yank into a new yank
2901 * register and then concatenate the old and the new one (so we keep the old
2902 * one in case of out-of-memory).
2903 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002904 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 */
2906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002907op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 long y_idx; /* index in y_array[] */
2910 struct yankreg *curr; /* copy of y_current */
2911 struct yankreg newreg; /* new yank register when appending */
2912 char_u **new_ptr;
2913 linenr_T lnum; /* current line number */
2914 long j;
2915 int yanktype = oap->motion_type;
2916 long yanklines = oap->line_count;
2917 linenr_T yankendlnum = oap->end.lnum;
2918 char_u *p;
2919 char_u *pnew;
2920 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002921#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002922 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /* check for read-only register */
2926 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2927 {
2928 beep_flush();
2929 return FAIL;
2930 }
2931 if (oap->regname == '_') /* black hole: nothing to do */
2932 return OK;
2933
2934#ifdef FEAT_CLIPBOARD
2935 if (!clip_star.available && oap->regname == '*')
2936 oap->regname = 0;
2937 else if (!clip_plus.available && oap->regname == '+')
2938 oap->regname = 0;
2939#endif
2940
2941 if (!deleting) /* op_delete() already set y_current */
2942 get_yank_register(oap->regname, TRUE);
2943
2944 curr = y_current;
2945 /* append to existing contents */
2946 if (y_append && y_current->y_array != NULL)
2947 y_current = &newreg;
2948 else
2949 free_yank_all(); /* free previously yanked lines */
2950
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002951 /*
2952 * If the cursor was in column 1 before and after the movement, and the
2953 * operator is not inclusive, the yank is always linewise.
2954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if ( oap->motion_type == MCHAR
2956 && oap->start.col == 0
2957 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002959 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 && oap->end.col == 0
2961 && yanklines > 1)
2962 {
2963 yanktype = MLINE;
2964 --yankendlnum;
2965 --yanklines;
2966 }
2967
2968 y_current->y_size = yanklines;
2969 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2972 yanklines), TRUE);
2973
2974 if (y_current->y_array == NULL)
2975 {
2976 y_current = curr;
2977 return FAIL;
2978 }
2979
2980 y_idx = 0;
2981 lnum = oap->start.lnum;
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (oap->block_mode)
2984 {
2985 /* Visual block mode */
2986 y_current->y_type = MBLOCK; /* set the yank register type */
2987 y_current->y_width = oap->end_vcol - oap->start_vcol;
2988
2989 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2990 y_current->y_width--;
2991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2994 {
2995 switch (y_current->y_type)
2996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 case MBLOCK:
2998 block_prep(oap, &bd, lnum, FALSE);
2999 if (yank_copy_line(&bd, y_idx) == FAIL)
3000 goto fail;
3001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 case MLINE:
3004 if ((y_current->y_array[y_idx] =
3005 vim_strsave(ml_get(lnum))) == NULL)
3006 goto fail;
3007 break;
3008
3009 case MCHAR:
3010 {
3011 colnr_T startcol = 0, endcol = MAXCOL;
3012#ifdef FEAT_VIRTUALEDIT
3013 int is_oneChar = FALSE;
3014 colnr_T cs, ce;
3015#endif
3016 p = ml_get(lnum);
3017 bd.startspaces = 0;
3018 bd.endspaces = 0;
3019
3020 if (lnum == oap->start.lnum)
3021 {
3022 startcol = oap->start.col;
3023#ifdef FEAT_VIRTUALEDIT
3024 if (virtual_op)
3025 {
3026 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3027 if (ce != cs && oap->start.coladd > 0)
3028 {
3029 /* Part of a tab selected -- but don't
3030 * double-count it. */
3031 bd.startspaces = (ce - cs + 1)
3032 - oap->start.coladd;
3033 startcol++;
3034 }
3035 }
3036#endif
3037 }
3038
3039 if (lnum == oap->end.lnum)
3040 {
3041 endcol = oap->end.col;
3042#ifdef FEAT_VIRTUALEDIT
3043 if (virtual_op)
3044 {
3045 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3046 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3047# ifdef FEAT_MBYTE
3048 /* Don't add space for double-wide
3049 * char; endcol will be on last byte
3050 * of multi-byte char. */
3051 && (*mb_head_off)(p, p + endcol) == 0
3052# endif
3053 ))
3054 {
3055 if (oap->start.lnum == oap->end.lnum
3056 && oap->start.col == oap->end.col)
3057 {
3058 /* Special case: inside a single char */
3059 is_oneChar = TRUE;
3060 bd.startspaces = oap->end.coladd
3061 - oap->start.coladd + oap->inclusive;
3062 endcol = startcol;
3063 }
3064 else
3065 {
3066 bd.endspaces = oap->end.coladd
3067 + oap->inclusive;
3068 endcol -= oap->inclusive;
3069 }
3070 }
3071 }
3072#endif
3073 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003074 if (endcol == MAXCOL)
3075 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 if (startcol > endcol
3077#ifdef FEAT_VIRTUALEDIT
3078 || is_oneChar
3079#endif
3080 )
3081 bd.textlen = 0;
3082 else
3083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 bd.textlen = endcol - startcol + oap->inclusive;
3085 }
3086 bd.textstart = p + startcol;
3087 if (yank_copy_line(&bd, y_idx) == FAIL)
3088 goto fail;
3089 break;
3090 }
3091 /* NOTREACHED */
3092 }
3093 }
3094
3095 if (curr != y_current) /* append the new block to the old block */
3096 {
3097 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3098 (curr->y_size + y_current->y_size)), TRUE);
3099 if (new_ptr == NULL)
3100 goto fail;
3101 for (j = 0; j < curr->y_size; ++j)
3102 new_ptr[j] = curr->y_array[j];
3103 vim_free(curr->y_array);
3104 curr->y_array = new_ptr;
3105
3106 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3107 curr->y_type = MLINE;
3108
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003109 /* Concatenate the last line of the old block with the first line of
3110 * the new block, unless being Vi compatible. */
3111 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
3113 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3114 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3115 if (pnew == NULL)
3116 {
3117 y_idx = y_current->y_size - 1;
3118 goto fail;
3119 }
3120 STRCPY(pnew, curr->y_array[--j]);
3121 STRCAT(pnew, y_current->y_array[0]);
3122 vim_free(curr->y_array[j]);
3123 vim_free(y_current->y_array[0]);
3124 curr->y_array[j++] = pnew;
3125 y_idx = 1;
3126 }
3127 else
3128 y_idx = 0;
3129 while (y_idx < y_current->y_size)
3130 curr->y_array[j++] = y_current->y_array[y_idx++];
3131 curr->y_size = j;
3132 vim_free(y_current->y_array);
3133 y_current = curr;
3134 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003135 if (curwin->w_p_rnu)
3136 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (mess) /* Display message about yank? */
3138 {
3139 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 && yanklines == 1)
3142 yanklines = 0;
3143 /* Some versions of Vi use ">=" here, some don't... */
3144 if (yanklines > p_report)
3145 {
3146 /* redisplay now, so message is not deleted */
3147 update_topline_redraw();
3148 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003149 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003150 if (oap->block_mode)
3151 MSG(_("block of 1 line yanked"));
3152 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003153 MSG(_("1 line yanked"));
3154 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003155 else if (oap->block_mode)
3156 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 else
3158 smsg((char_u *)_("%ld lines yanked"), yanklines);
3159 }
3160 }
3161
3162 /*
3163 * Set "'[" and "']" marks.
3164 */
3165 curbuf->b_op_start = oap->start;
3166 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003167 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003168 {
3169 curbuf->b_op_start.col = 0;
3170 curbuf->b_op_end.col = MAXCOL;
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173#ifdef FEAT_CLIPBOARD
3174 /*
3175 * If we were yanking to the '*' register, send result to clipboard.
3176 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3177 * to the '*' register.
3178 */
3179 if (clip_star.available
3180 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003181 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003182 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 if (curr != &(y_regs[STAR_REGISTER]))
3185 /* Copy the text from register 0 to the clipboard register. */
3186 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3187
3188 clip_own_selection(&clip_star);
3189 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003190# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003191 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 }
3194
3195# ifdef FEAT_X11
3196 /*
3197 * If we were yanking to the '+' register, send result to selection.
3198 * Also copy to the '*' register, in case auto-select is off.
3199 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003200 if (clip_plus.available
3201 && (curr == &(y_regs[PLUS_REGISTER])
3202 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003203 && ((clip_unnamed | clip_unnamed_saved) &
3204 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003206 if (curr != &(y_regs[PLUS_REGISTER]))
3207 /* Copy the text from register 0 to the clipboard register. */
3208 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 clip_own_selection(&clip_plus);
3211 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003212 if (!clip_isautosel_star() && !did_star
3213 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
3215 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3216 clip_own_selection(&clip_star);
3217 clip_gen_set_selection(&clip_star);
3218 }
3219 }
3220# endif
3221#endif
3222
3223 return OK;
3224
3225fail: /* free the allocated lines */
3226 free_yank(y_idx + 1);
3227 y_current = curr;
3228 return FAIL;
3229}
3230
3231 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 char_u *pnew;
3235
3236 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3237 == NULL)
3238 return FAIL;
3239 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003240 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 pnew += bd->startspaces;
3242 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3243 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003244 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 pnew += bd->endspaces;
3246 *pnew = NUL;
3247 return OK;
3248}
3249
3250#ifdef FEAT_CLIPBOARD
3251/*
3252 * Make a copy of the y_current register to register "reg".
3253 */
3254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003255copy_yank_reg(struct yankreg *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 struct yankreg *curr = y_current;
3258 long j;
3259
3260 y_current = reg;
3261 free_yank_all();
3262 *y_current = *curr;
3263 y_current->y_array = (char_u **)lalloc_clear(
3264 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3265 if (y_current->y_array == NULL)
3266 y_current->y_size = 0;
3267 else
3268 for (j = 0; j < y_current->y_size; ++j)
3269 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3270 {
3271 free_yank(j);
3272 y_current->y_size = 0;
3273 break;
3274 }
3275 y_current = curr;
3276}
3277#endif
3278
3279/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003280 * Put contents of register "regname" into the text.
3281 * Caller must check "regname" to be valid!
3282 * "flags": PUT_FIXINDENT make indent look nice
3283 * PUT_CURSEND leave cursor after end of new text
3284 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 */
3286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287do_put(
3288 int regname,
3289 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3290 long count,
3291 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 char_u *ptr;
3294 char_u *newp, *oldp;
3295 int yanklen;
3296 int totlen = 0; /* init for gcc */
3297 linenr_T lnum;
3298 colnr_T col;
3299 long i; /* index in y_array[] */
3300 int y_type;
3301 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int oldlen;
3303 long y_width = 0;
3304 colnr_T vcol;
3305 int delcount;
3306 int incr = 0;
3307 long j;
3308 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 char_u **y_array = NULL;
3310 long nr_lines = 0;
3311 pos_T new_cursor;
3312 int indent;
3313 int orig_indent = 0; /* init for gcc */
3314 int indent_diff = 0; /* init for gcc */
3315 int first_indent = TRUE;
3316 int lendiff = 0;
3317 pos_T old_pos;
3318 char_u *insert_string = NULL;
3319 int allocated = FALSE;
3320 long cnt;
3321
3322#ifdef FEAT_CLIPBOARD
3323 /* Adjust register name for "unnamed" in 'clipboard'. */
3324 adjust_clip_reg(&regname);
3325 (void)may_get_selection(regname);
3326#endif
3327
3328 if (flags & PUT_FIXINDENT)
3329 orig_indent = get_indent();
3330
3331 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3332 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3333
3334 /*
3335 * Using inserted text works differently, because the register includes
3336 * special characters (newlines, etc.).
3337 */
3338 if (regname == '.')
3339 {
3340 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3341 (count == -1 ? 'O' : 'i')), count, FALSE);
3342 /* Putting the text is done later, so can't really move the cursor to
3343 * the next character. Use "l" to simulate it. */
3344 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3345 stuffcharReadbuff('l');
3346 return;
3347 }
3348
3349 /*
3350 * For special registers '%' (file name), '#' (alternate file name) and
3351 * ':' (last command line), etc. we have to create a fake yank register.
3352 */
3353 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3354 {
3355 if (insert_string == NULL)
3356 return;
3357 }
3358
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003359#ifdef FEAT_AUTOCMD
3360 /* Autocommands may be executed when saving lines for undo, which may make
3361 * y_array invalid. Start undo now to avoid that. */
3362 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3363#endif
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (insert_string != NULL)
3366 {
3367 y_type = MCHAR;
3368#ifdef FEAT_EVAL
3369 if (regname == '=')
3370 {
3371 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003372 * characters.
3373 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 for (;;)
3375 {
3376 y_size = 0;
3377 ptr = insert_string;
3378 while (ptr != NULL)
3379 {
3380 if (y_array != NULL)
3381 y_array[y_size] = ptr;
3382 ++y_size;
3383 ptr = vim_strchr(ptr, '\n');
3384 if (ptr != NULL)
3385 {
3386 if (y_array != NULL)
3387 *ptr = NUL;
3388 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003389 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (*ptr == NUL)
3391 {
3392 y_type = MLINE;
3393 break;
3394 }
3395 }
3396 }
3397 if (y_array != NULL)
3398 break;
3399 y_array = (char_u **)alloc((unsigned)
3400 (y_size * sizeof(char_u *)));
3401 if (y_array == NULL)
3402 goto end;
3403 }
3404 }
3405 else
3406#endif
3407 {
3408 y_size = 1; /* use fake one-line yank register */
3409 y_array = &insert_string;
3410 }
3411 }
3412 else
3413 {
3414 get_yank_register(regname, FALSE);
3415
3416 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 y_size = y_current->y_size;
3419 y_array = y_current->y_array;
3420 }
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (y_type == MLINE)
3423 {
3424 if (flags & PUT_LINE_SPLIT)
3425 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003426 char_u *p;
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 /* "p" or "P" in Visual mode: split the lines to put the text in
3429 * between. */
3430 if (u_save_cursor() == FAIL)
3431 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003432 p = ml_get_cursor();
3433 if (dir == FORWARD && *p != NUL)
3434 mb_ptr_adv(p);
3435 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (ptr == NULL)
3437 goto end;
3438 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3439 vim_free(ptr);
3440
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003441 oldp = ml_get_curline();
3442 p = oldp + curwin->w_cursor.col;
3443 if (dir == FORWARD && *p != NUL)
3444 mb_ptr_adv(p);
3445 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (ptr == NULL)
3447 goto end;
3448 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3449 ++nr_lines;
3450 dir = FORWARD;
3451 }
3452 if (flags & PUT_LINE_FORWARD)
3453 {
3454 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003455 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 dir = FORWARD;
3457 }
3458 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3459 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
3462 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3463 y_type = MLINE;
3464
3465 if (y_size == 0 || y_array == NULL)
3466 {
3467 EMSG2(_("E353: Nothing in register %s"),
3468 regname == 0 ? (char_u *)"\"" : transchar(regname));
3469 goto end;
3470 }
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (y_type == MBLOCK)
3473 {
3474 lnum = curwin->w_cursor.lnum + y_size + 1;
3475 if (lnum > curbuf->b_ml.ml_line_count)
3476 lnum = curbuf->b_ml.ml_line_count + 1;
3477 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3478 goto end;
3479 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003480 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 lnum = curwin->w_cursor.lnum;
3483#ifdef FEAT_FOLDING
3484 /* Correct line number for closed fold. Don't move the cursor yet,
3485 * u_save() uses it. */
3486 if (dir == BACKWARD)
3487 (void)hasFolding(lnum, &lnum, NULL);
3488 else
3489 (void)hasFolding(lnum, NULL, &lnum);
3490#endif
3491 if (dir == FORWARD)
3492 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003493 /* In an empty buffer the empty line is going to be replaced, include
3494 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003495 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 goto end;
3497#ifdef FEAT_FOLDING
3498 if (dir == FORWARD)
3499 curwin->w_cursor.lnum = lnum - 1;
3500 else
3501 curwin->w_cursor.lnum = lnum;
3502 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3503#endif
3504 }
3505 else if (u_save_cursor() == FAIL)
3506 goto end;
3507
3508 yanklen = (int)STRLEN(y_array[0]);
3509
3510#ifdef FEAT_VIRTUALEDIT
3511 if (ve_flags == VE_ALL && y_type == MCHAR)
3512 {
3513 if (gchar_cursor() == TAB)
3514 {
3515 /* Don't need to insert spaces when "p" on the last position of a
3516 * tab or "P" on the first position. */
3517 if (dir == FORWARD
3518 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3519 : curwin->w_cursor.coladd > 0)
3520 coladvance_force(getviscol());
3521 else
3522 curwin->w_cursor.coladd = 0;
3523 }
3524 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3525 coladvance_force(getviscol() + (dir == FORWARD));
3526 }
3527#endif
3528
3529 lnum = curwin->w_cursor.lnum;
3530 col = curwin->w_cursor.col;
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 /*
3533 * Block mode
3534 */
3535 if (y_type == MBLOCK)
3536 {
3537 char c = gchar_cursor();
3538 colnr_T endcol2 = 0;
3539
3540 if (dir == FORWARD && c != NUL)
3541 {
3542#ifdef FEAT_VIRTUALEDIT
3543 if (ve_flags == VE_ALL)
3544 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3545 else
3546#endif
3547 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3548
3549#ifdef FEAT_MBYTE
3550 if (has_mbyte)
3551 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003552 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 else
3554#endif
3555#ifdef FEAT_VIRTUALEDIT
3556 if (c != TAB || ve_flags != VE_ALL)
3557#endif
3558 ++curwin->w_cursor.col;
3559 ++col;
3560 }
3561 else
3562 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3563
3564#ifdef FEAT_VIRTUALEDIT
3565 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003566 if (ve_flags == VE_ALL
3567 && (curwin->w_cursor.coladd > 0
3568 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 if (dir == FORWARD && c == NUL)
3571 ++col;
3572 if (dir != FORWARD && c != NUL)
3573 ++curwin->w_cursor.col;
3574 if (c == TAB)
3575 {
3576 if (dir == BACKWARD && curwin->w_cursor.col)
3577 curwin->w_cursor.col--;
3578 if (dir == FORWARD && col - 1 == endcol2)
3579 curwin->w_cursor.col++;
3580 }
3581 }
3582 curwin->w_cursor.coladd = 0;
3583#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003584 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 for (i = 0; i < y_size; ++i)
3586 {
3587 int spaces;
3588 char shortline;
3589
3590 bd.startspaces = 0;
3591 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 vcol = 0;
3593 delcount = 0;
3594
3595 /* add a new line */
3596 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3597 {
3598 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3599 (colnr_T)1, FALSE) == FAIL)
3600 break;
3601 ++nr_lines;
3602 }
3603 /* get the old line and advance to the position to insert at */
3604 oldp = ml_get_curline();
3605 oldlen = (int)STRLEN(oldp);
3606 for (ptr = oldp; vcol < col && *ptr; )
3607 {
3608 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003609 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 vcol += incr;
3611 }
3612 bd.textcol = (colnr_T)(ptr - oldp);
3613
3614 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3615
3616 if (vcol < col) /* line too short, padd with spaces */
3617 bd.startspaces = col - vcol;
3618 else if (vcol > col)
3619 {
3620 bd.endspaces = vcol - col;
3621 bd.startspaces = incr - bd.endspaces;
3622 --bd.textcol;
3623 delcount = 1;
3624#ifdef FEAT_MBYTE
3625 if (has_mbyte)
3626 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3627#endif
3628 if (oldp[bd.textcol] != TAB)
3629 {
3630 /* Only a Tab can be split into spaces. Other
3631 * characters will have to be moved to after the
3632 * block, causing misalignment. */
3633 delcount = 0;
3634 bd.endspaces = 0;
3635 }
3636 }
3637
3638 yanklen = (int)STRLEN(y_array[i]);
3639
3640 /* calculate number of spaces required to fill right side of block*/
3641 spaces = y_width + 1;
3642 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003643 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (spaces < 0)
3645 spaces = 0;
3646
3647 /* insert the new text */
3648 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3649 newp = alloc_check((unsigned)totlen + oldlen + 1);
3650 if (newp == NULL)
3651 break;
3652 /* copy part up to cursor to new line */
3653 ptr = newp;
3654 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3655 ptr += bd.textcol;
3656 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003657 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 ptr += bd.startspaces;
3659 /* insert the new text */
3660 for (j = 0; j < count; ++j)
3661 {
3662 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3663 ptr += yanklen;
3664
3665 /* insert block's trailing spaces only if there's text behind */
3666 if ((j < count - 1 || !shortline) && spaces)
3667 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003668 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 ptr += spaces;
3670 }
3671 }
3672 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003673 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 ptr += bd.endspaces;
3675 /* move the text after the cursor to the end of the line. */
3676 mch_memmove(ptr, oldp + bd.textcol + delcount,
3677 (size_t)(oldlen - bd.textcol - delcount + 1));
3678 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3679
3680 ++curwin->w_cursor.lnum;
3681 if (i == 0)
3682 curwin->w_cursor.col += bd.startspaces;
3683 }
3684
3685 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3686
3687 /* Set '[ mark. */
3688 curbuf->b_op_start = curwin->w_cursor;
3689 curbuf->b_op_start.lnum = lnum;
3690
3691 /* adjust '] mark */
3692 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3693 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003694# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (flags & PUT_CURSEND)
3698 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003699 colnr_T len;
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 curwin->w_cursor = curbuf->b_op_end;
3702 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003703
3704 /* in Insert mode we might be after the NUL, correct for that */
3705 len = (colnr_T)STRLEN(ml_get_curline());
3706 if (curwin->w_cursor.col > len)
3707 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 else
3710 curwin->w_cursor.lnum = lnum;
3711 }
3712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
3714 /*
3715 * Character or Line mode
3716 */
3717 if (y_type == MCHAR)
3718 {
3719 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3720 * char */
3721 if (dir == FORWARD && gchar_cursor() != NUL)
3722 {
3723#ifdef FEAT_MBYTE
3724 if (has_mbyte)
3725 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003726 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 /* put it on the next of the multi-byte character. */
3729 col += bytelen;
3730 if (yanklen)
3731 {
3732 curwin->w_cursor.col += bytelen;
3733 curbuf->b_op_end.col += bytelen;
3734 }
3735 }
3736 else
3737#endif
3738 {
3739 ++col;
3740 if (yanklen)
3741 {
3742 ++curwin->w_cursor.col;
3743 ++curbuf->b_op_end.col;
3744 }
3745 }
3746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 curbuf->b_op_start = curwin->w_cursor;
3748 }
3749 /*
3750 * Line mode: BACKWARD is the same as FORWARD on the previous line
3751 */
3752 else if (dir == BACKWARD)
3753 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003754 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
3756 /*
3757 * simple case: insert into current line
3758 */
3759 if (y_type == MCHAR && y_size == 1)
3760 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003761 do {
3762 totlen = count * yanklen;
3763 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003765 oldp = ml_get(lnum);
3766 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3767 if (newp == NULL)
3768 goto end; /* alloc() gave an error message */
3769 mch_memmove(newp, oldp, (size_t)col);
3770 ptr = newp + col;
3771 for (i = 0; i < count; ++i)
3772 {
3773 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3774 ptr += yanklen;
3775 }
3776 STRMOVE(ptr, oldp + col);
3777 ml_replace(lnum, newp, FALSE);
3778 /* Place cursor on last putted char. */
3779 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003780 {
3781 /* make sure curwin->w_virtcol is updated */
3782 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003783 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003786 if (VIsual_active)
3787 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003788 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003789
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003790 if (VIsual_active) /* reset lnum to the last visual line */
3791 lnum--;
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 curbuf->b_op_end = curwin->w_cursor;
3794 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3795 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3796 ++curwin->w_cursor.col;
3797 changed_bytes(lnum, col);
3798 }
3799 else
3800 {
3801 /*
3802 * Insert at least one line. When y_type is MCHAR, break the first
3803 * line in two.
3804 */
3805 for (cnt = 1; cnt <= count; ++cnt)
3806 {
3807 i = 0;
3808 if (y_type == MCHAR)
3809 {
3810 /*
3811 * Split the current line in two at the insert position.
3812 * First insert y_array[size - 1] in front of second line.
3813 * Then append y_array[0] to first line.
3814 */
3815 lnum = new_cursor.lnum;
3816 ptr = ml_get(lnum) + col;
3817 totlen = (int)STRLEN(y_array[y_size - 1]);
3818 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3819 if (newp == NULL)
3820 goto error;
3821 STRCPY(newp, y_array[y_size - 1]);
3822 STRCAT(newp, ptr);
3823 /* insert second line */
3824 ml_append(lnum, newp, (colnr_T)0, FALSE);
3825 vim_free(newp);
3826
3827 oldp = ml_get(lnum);
3828 newp = alloc_check((unsigned)(col + yanklen + 1));
3829 if (newp == NULL)
3830 goto error;
3831 /* copy first part of line */
3832 mch_memmove(newp, oldp, (size_t)col);
3833 /* append to first line */
3834 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3835 ml_replace(lnum, newp, FALSE);
3836
3837 curwin->w_cursor.lnum = lnum;
3838 i = 1;
3839 }
3840
3841 for (; i < y_size; ++i)
3842 {
3843 if ((y_type != MCHAR || i < y_size - 1)
3844 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3845 == FAIL)
3846 goto error;
3847 lnum++;
3848 ++nr_lines;
3849 if (flags & PUT_FIXINDENT)
3850 {
3851 old_pos = curwin->w_cursor;
3852 curwin->w_cursor.lnum = lnum;
3853 ptr = ml_get(lnum);
3854 if (cnt == count && i == y_size - 1)
3855 lendiff = (int)STRLEN(ptr);
3856#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3857 if (*ptr == '#' && preprocs_left())
3858 indent = 0; /* Leave # lines at start */
3859 else
3860#endif
3861 if (*ptr == NUL)
3862 indent = 0; /* Ignore empty lines */
3863 else if (first_indent)
3864 {
3865 indent_diff = orig_indent - get_indent();
3866 indent = orig_indent;
3867 first_indent = FALSE;
3868 }
3869 else if ((indent = get_indent() + indent_diff) < 0)
3870 indent = 0;
3871 (void)set_indent(indent, 0);
3872 curwin->w_cursor = old_pos;
3873 /* remember how many chars were removed */
3874 if (cnt == count && i == y_size - 1)
3875 lendiff -= (int)STRLEN(ml_get(lnum));
3876 }
3877 }
3878 }
3879
3880error:
3881 /* Adjust marks. */
3882 if (y_type == MLINE)
3883 {
3884 curbuf->b_op_start.col = 0;
3885 if (dir == FORWARD)
3886 curbuf->b_op_start.lnum++;
3887 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003888 /* Skip mark_adjust when adding lines after the last one, there
3889 * can't be marks there. */
3890 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
3891 < curbuf->b_ml.ml_line_count)
3892 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 (linenr_T)MAXLNUM, nr_lines, 0L);
3894
3895 /* note changed text for displaying and folding */
3896 if (y_type == MCHAR)
3897 changed_lines(curwin->w_cursor.lnum, col,
3898 curwin->w_cursor.lnum + 1, nr_lines);
3899 else
3900 changed_lines(curbuf->b_op_start.lnum, 0,
3901 curbuf->b_op_start.lnum, nr_lines);
3902
3903 /* put '] mark at last inserted character */
3904 curbuf->b_op_end.lnum = lnum;
3905 /* correct length for change in indent */
3906 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3907 if (col > 1)
3908 curbuf->b_op_end.col = col - 1;
3909 else
3910 curbuf->b_op_end.col = 0;
3911
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003912 if (flags & PUT_CURSLINE)
3913 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003914 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003915 curwin->w_cursor.lnum = lnum;
3916 beginline(BL_WHITE | BL_FIX);
3917 }
3918 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 /* put cursor after inserted text */
3921 if (y_type == MLINE)
3922 {
3923 if (lnum >= curbuf->b_ml.ml_line_count)
3924 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3925 else
3926 curwin->w_cursor.lnum = lnum + 1;
3927 curwin->w_cursor.col = 0;
3928 }
3929 else
3930 {
3931 curwin->w_cursor.lnum = lnum;
3932 curwin->w_cursor.col = col;
3933 }
3934 }
3935 else if (y_type == MLINE)
3936 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003937 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 curwin->w_cursor.col = 0;
3939 if (dir == FORWARD)
3940 ++curwin->w_cursor.lnum;
3941 beginline(BL_WHITE | BL_FIX);
3942 }
3943 else /* put cursor on first inserted character */
3944 curwin->w_cursor = new_cursor;
3945 }
3946 }
3947
3948 msgmore(nr_lines);
3949 curwin->w_set_curswant = TRUE;
3950
3951end:
3952 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003954 if (regname == '=')
3955 vim_free(y_array);
3956
Bram Moolenaar033d8882013-09-25 23:24:57 +02003957 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003958
Bram Moolenaar677ee682005-01-27 14:41:15 +00003959 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003960 adjust_cursor_eol();
3961}
3962
3963/*
3964 * When the cursor is on the NUL past the end of the line and it should not be
3965 * there move it left.
3966 */
3967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003968adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003969{
3970 if (curwin->w_cursor.col > 0
3971 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003972#ifdef FEAT_VIRTUALEDIT
3973 && (ve_flags & VE_ONEMORE) == 0
3974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 && !(restart_edit || (State & INSERT)))
3976 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003977 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003978 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980#ifdef FEAT_VIRTUALEDIT
3981 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003982 {
3983 colnr_T scol, ecol;
3984
3985 /* Coladd is set to the width of the last character. */
3986 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3987 curwin->w_cursor.coladd = ecol - scol + 1;
3988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#endif
3990 }
3991}
3992
3993#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3994/*
3995 * Return TRUE if lines starting with '#' should be left aligned.
3996 */
3997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003998preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999{
4000 return
4001# ifdef FEAT_SMARTINDENT
4002# ifdef FEAT_CINDENT
4003 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4004# else
4005 curbuf->b_p_si
4006# endif
4007# endif
4008# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004009 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4010 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011# endif
4012 ;
4013}
4014#endif
4015
4016/* Return the character name of the register with the given number */
4017 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004018get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 if (num == -1)
4021 return '"';
4022 else if (num < 10)
4023 return num + '0';
4024 else if (num == DELETION_REGISTER)
4025 return '-';
4026#ifdef FEAT_CLIPBOARD
4027 else if (num == STAR_REGISTER)
4028 return '*';
4029 else if (num == PLUS_REGISTER)
4030 return '+';
4031#endif
4032 else
4033 {
4034#ifdef EBCDIC
4035 int i;
4036
4037 /* EBCDIC is really braindead ... */
4038 i = 'a' + (num - 10);
4039 if (i > 'i')
4040 i += 7;
4041 if (i > 'r')
4042 i += 8;
4043 return i;
4044#else
4045 return num + 'a' - 10;
4046#endif
4047 }
4048}
4049
4050/*
4051 * ":dis" and ":registers": Display the contents of the yank registers.
4052 */
4053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004054ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
4056 int i, n;
4057 long j;
4058 char_u *p;
4059 struct yankreg *yb;
4060 int name;
4061 int attr;
4062 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004063#ifdef FEAT_MBYTE
4064 int clen;
4065#else
4066# define clen 1
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 if (arg != NULL && *arg == NUL)
4070 arg = NULL;
4071 attr = hl_attr(HLF_8);
4072
4073 /* Highlight title */
4074 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4075 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4076 {
4077 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004078 if (arg != NULL && vim_strchr(arg, name) == NULL
4079#ifdef ONE_CLIPBOARD
4080 /* Star register and plus register contain the same thing. */
4081 && (name != '*' || vim_strchr(arg, '+') == NULL)
4082#endif
4083 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 continue; /* did not ask for this register */
4085
4086#ifdef FEAT_CLIPBOARD
4087 /* Adjust register name for "unnamed" in 'clipboard'.
4088 * When it's a clipboard register, fill it with the current contents
4089 * of the clipboard. */
4090 adjust_clip_reg(&name);
4091 (void)may_get_selection(name);
4092#endif
4093
4094 if (i == -1)
4095 {
4096 if (y_previous != NULL)
4097 yb = y_previous;
4098 else
4099 yb = &(y_regs[0]);
4100 }
4101 else
4102 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004103
4104#ifdef FEAT_EVAL
4105 if (name == MB_TOLOWER(redir_reg)
4106 || (redir_reg == '"' && yb == y_previous))
4107 continue; /* do not list register being written to, the
4108 * pointer can be freed */
4109#endif
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (yb->y_array != NULL)
4112 {
4113 msg_putchar('\n');
4114 msg_putchar('"');
4115 msg_putchar(name);
4116 MSG_PUTS(" ");
4117
4118 n = (int)Columns - 6;
4119 for (j = 0; j < yb->y_size && n > 1; ++j)
4120 {
4121 if (j)
4122 {
4123 MSG_PUTS_ATTR("^J", attr);
4124 n -= 2;
4125 }
4126 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004129 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004130#endif
4131 msg_outtrans_len(p, clen);
4132#ifdef FEAT_MBYTE
4133 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#endif
4135 }
4136 }
4137 if (n > 1 && yb->y_type == MLINE)
4138 MSG_PUTS_ATTR("^J", attr);
4139 out_flush(); /* show one line at a time */
4140 }
4141 ui_breakcheck();
4142 }
4143
4144 /*
4145 * display last inserted text
4146 */
4147 if ((p = get_last_insert()) != NULL
4148 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4149 {
4150 MSG_PUTS("\n\". ");
4151 dis_msg(p, TRUE);
4152 }
4153
4154 /*
4155 * display last command line
4156 */
4157 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4158 && !got_int)
4159 {
4160 MSG_PUTS("\n\": ");
4161 dis_msg(last_cmdline, FALSE);
4162 }
4163
4164 /*
4165 * display current file name
4166 */
4167 if (curbuf->b_fname != NULL
4168 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4169 {
4170 MSG_PUTS("\n\"% ");
4171 dis_msg(curbuf->b_fname, FALSE);
4172 }
4173
4174 /*
4175 * display alternate file name
4176 */
4177 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4178 {
4179 char_u *fname;
4180 linenr_T dummy;
4181
4182 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4183 {
4184 MSG_PUTS("\n\"# ");
4185 dis_msg(fname, FALSE);
4186 }
4187 }
4188
4189 /*
4190 * display last search pattern
4191 */
4192 if (last_search_pat() != NULL
4193 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4194 {
4195 MSG_PUTS("\n\"/ ");
4196 dis_msg(last_search_pat(), FALSE);
4197 }
4198
4199#ifdef FEAT_EVAL
4200 /*
4201 * display last used expression
4202 */
4203 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4204 && !got_int)
4205 {
4206 MSG_PUTS("\n\"= ");
4207 dis_msg(expr_line, FALSE);
4208 }
4209#endif
4210}
4211
4212/*
4213 * display a string for do_dis()
4214 * truncate at end of screen line
4215 */
4216 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004217dis_msg(
4218 char_u *p,
4219 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 int n;
4222#ifdef FEAT_MBYTE
4223 int l;
4224#endif
4225
4226 n = (int)Columns - 6;
4227 while (*p != NUL
4228 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4229 && (n -= ptr2cells(p)) >= 0)
4230 {
4231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004232 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 msg_outtrans_len(p, l);
4235 p += l;
4236 }
4237 else
4238#endif
4239 msg_outtrans_len(p++, 1);
4240 }
4241 ui_breakcheck();
4242}
4243
Bram Moolenaar81340392012-06-06 16:12:59 +02004244#if defined(FEAT_COMMENTS) || defined(PROTO)
4245/*
4246 * If "process" is TRUE and the line begins with a comment leader (possibly
4247 * after some white space), return a pointer to the text after it. Put a boolean
4248 * value indicating whether the line ends with an unclosed comment in
4249 * "is_comment".
4250 * line - line to be processed,
4251 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004252 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004253 * include_space - whether to also skip space following the comment leader,
4254 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004255 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004256 */
4257 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004258skip_comment(
4259 char_u *line,
4260 int process,
4261 int include_space,
4262 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004263{
4264 char_u *comment_flags = NULL;
4265 int lead_len;
4266 int leader_offset = get_last_leader_offset(line, &comment_flags);
4267
4268 *is_comment = FALSE;
4269 if (leader_offset != -1)
4270 {
4271 /* Let's check whether the line ends with an unclosed comment.
4272 * If the last comment leader has COM_END in flags, there's no comment.
4273 */
4274 while (*comment_flags)
4275 {
4276 if (*comment_flags == COM_END
4277 || *comment_flags == ':')
4278 break;
4279 ++comment_flags;
4280 }
4281 if (*comment_flags != COM_END)
4282 *is_comment = TRUE;
4283 }
4284
4285 if (process == FALSE)
4286 return line;
4287
4288 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4289
4290 if (lead_len == 0)
4291 return line;
4292
4293 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004294 * - COM_END,
4295 * - colon,
4296 * whichever comes first.
4297 */
4298 while (*comment_flags)
4299 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004300 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004301 || *comment_flags == ':')
4302 {
4303 break;
4304 }
4305 ++comment_flags;
4306 }
4307
4308 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004309 * starting with a closing part of a three-part comment. That's good,
4310 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004311 */
4312 if (*comment_flags == ':' || *comment_flags == NUL)
4313 line += lead_len;
4314
4315 return line;
4316}
4317#endif
4318
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004320 * Join 'count' lines (minimal 2) at cursor position.
4321 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004322 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4323 * leaders should not be removed.
4324 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4325 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004327 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 */
4329 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004330do_join(
4331 long count,
4332 int insert_space,
4333 int save_undo,
4334 int use_formatoptions UNUSED,
4335 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004337 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004338 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004339 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004341 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004342 int endcurr1 = NUL;
4343 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004344 int currsize = 0; /* size of the current line */
4345 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004347 colnr_T col = 0;
4348 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004349#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004350 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004351 int remove_comments = (use_formatoptions == TRUE)
4352 && has_format_option(FO_REMOVE_COMS);
4353 int prev_was_comment;
4354#endif
4355
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004357 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4358 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004361 /* Allocate an array to store the number of spaces inserted before each
4362 * line. We will use it to pre-compute the length of the new line and the
4363 * proper placement of each original line in the new one. */
4364 spaces = lalloc_clear((long_u)count, TRUE);
4365 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004367#if defined(FEAT_COMMENTS) || defined(PROTO)
4368 if (remove_comments)
4369 {
4370 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4371 if (comments == NULL)
4372 {
4373 vim_free(spaces);
4374 return FAIL;
4375 }
4376 }
4377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004380 * Don't move anything, just compute the final line length
4381 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004383 for (t = 0; t < count; ++t)
4384 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004385 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004386 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004387 {
4388 /* Set the '[ mark. */
4389 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4390 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4391 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004392#if defined(FEAT_COMMENTS) || defined(PROTO)
4393 if (remove_comments)
4394 {
4395 /* We don't want to remove the comment leader if the
4396 * previous line is not a comment. */
4397 if (t > 0 && prev_was_comment)
4398 {
4399
4400 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4401 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004402 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004403 curr = new_curr;
4404 }
4405 else
4406 curr = skip_comment(curr, FALSE, insert_space,
4407 &prev_was_comment);
4408 }
4409#endif
4410
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004411 if (insert_space && t > 0)
4412 {
4413 curr = skipwhite(curr);
4414 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4415#ifdef FEAT_MBYTE
4416 && (!has_format_option(FO_MBYTE_JOIN)
4417 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4418 && (!has_format_option(FO_MBYTE_JOIN2)
4419 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4420#endif
4421 )
4422 {
4423 /* don't add a space if the line is ending in a space */
4424 if (endcurr1 == ' ')
4425 endcurr1 = endcurr2;
4426 else
4427 ++spaces[t];
4428 /* extra space when 'joinspaces' set and line ends in '.' */
4429 if ( p_js
4430 && (endcurr1 == '.'
4431 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4432 && (endcurr1 == '?' || endcurr1 == '!'))))
4433 ++spaces[t];
4434 }
4435 }
4436 currsize = (int)STRLEN(curr);
4437 sumsize += currsize + spaces[t];
4438 endcurr1 = endcurr2 = NUL;
4439 if (insert_space && currsize > 0)
4440 {
4441#ifdef FEAT_MBYTE
4442 if (has_mbyte)
4443 {
4444 cend = curr + currsize;
4445 mb_ptr_back(curr, cend);
4446 endcurr1 = (*mb_ptr2char)(cend);
4447 if (cend > curr)
4448 {
4449 mb_ptr_back(curr, cend);
4450 endcurr2 = (*mb_ptr2char)(cend);
4451 }
4452 }
4453 else
4454#endif
4455 {
4456 endcurr1 = *(curr + currsize - 1);
4457 if (currsize > 1)
4458 endcurr2 = *(curr + currsize - 2);
4459 }
4460 }
4461 line_breakcheck();
4462 if (got_int)
4463 {
4464 ret = FAIL;
4465 goto theend;
4466 }
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004469 /* store the column position before last line */
4470 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004472 /* allocate the space for the new line */
4473 newp = alloc_check((unsigned)(sumsize + 1));
4474 cend = newp + sumsize;
4475 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004477 /*
4478 * Move affected lines to the new long one.
4479 *
4480 * Move marks from each deleted line to the joined line, adjusting the
4481 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4482 * should not really be a problem.
4483 */
4484 for (t = count - 1; ; --t)
4485 {
4486 cend -= currsize;
4487 mch_memmove(cend, curr, (size_t)currsize);
4488 if (spaces[t] > 0)
4489 {
4490 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004491 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004492 }
4493 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004494 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004495 if (t == 0)
4496 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004497 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004498#if defined(FEAT_COMMENTS) || defined(PROTO)
4499 if (remove_comments)
4500 curr += comments[t - 1];
4501#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 if (insert_space && t > 1)
4503 curr = skipwhite(curr);
4504 currsize = (int)STRLEN(curr);
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4507
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004508 if (setmark)
4509 {
4510 /* Set the '] mark. */
4511 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4512 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4513 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* Only report the change in the first line here, del_lines() will report
4516 * the deleted line. */
4517 changed_lines(curwin->w_cursor.lnum, currsize,
4518 curwin->w_cursor.lnum + 1, 0L);
4519
4520 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004521 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 * briefly, and then move it back. After del_lines() the cursor may
4523 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 */
4525 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004527 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 curwin->w_cursor.lnum = t;
4529
4530 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004531 * Set the cursor column:
4532 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004533 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004535 curwin->w_cursor.col =
4536 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#ifdef FEAT_VIRTUALEDIT
4540 curwin->w_cursor.coladd = 0;
4541#endif
4542 curwin->w_set_curswant = TRUE;
4543
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004544theend:
4545 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004546#if defined(FEAT_COMMENTS) || defined(PROTO)
4547 if (remove_comments)
4548 vim_free(comments);
4549#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551}
4552
4553#ifdef FEAT_COMMENTS
4554/*
4555 * Return TRUE if the two comment leaders given are the same. "lnum" is
4556 * the first line. White-space is ignored. Note that the whole of
4557 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4558 */
4559 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004560same_leader(
4561 linenr_T lnum,
4562 int leader1_len,
4563 char_u *leader1_flags,
4564 int leader2_len,
4565 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 int idx1 = 0, idx2 = 0;
4568 char_u *p;
4569 char_u *line1;
4570 char_u *line2;
4571
4572 if (leader1_len == 0)
4573 return (leader2_len == 0);
4574
4575 /*
4576 * If first leader has 'f' flag, the lines can be joined only if the
4577 * second line does not have a leader.
4578 * If first leader has 'e' flag, the lines can never be joined.
4579 * If fist leader has 's' flag, the lines can only be joined if there is
4580 * some text after it and the second line has the 'm' flag.
4581 */
4582 if (leader1_flags != NULL)
4583 {
4584 for (p = leader1_flags; *p && *p != ':'; ++p)
4585 {
4586 if (*p == COM_FIRST)
4587 return (leader2_len == 0);
4588 if (*p == COM_END)
4589 return FALSE;
4590 if (*p == COM_START)
4591 {
4592 if (*(ml_get(lnum) + leader1_len) == NUL)
4593 return FALSE;
4594 if (leader2_flags == NULL || leader2_len == 0)
4595 return FALSE;
4596 for (p = leader2_flags; *p && *p != ':'; ++p)
4597 if (*p == COM_MIDDLE)
4598 return TRUE;
4599 return FALSE;
4600 }
4601 }
4602 }
4603
4604 /*
4605 * Get current line and next line, compare the leaders.
4606 * The first line has to be saved, only one line can be locked at a time.
4607 */
4608 line1 = vim_strsave(ml_get(lnum));
4609 if (line1 != NULL)
4610 {
4611 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4612 ;
4613 line2 = ml_get(lnum + 1);
4614 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4615 {
4616 if (!vim_iswhite(line2[idx2]))
4617 {
4618 if (line1[idx1++] != line2[idx2])
4619 break;
4620 }
4621 else
4622 while (vim_iswhite(line1[idx1]))
4623 ++idx1;
4624 }
4625 vim_free(line1);
4626 }
4627 return (idx2 == leader2_len && idx1 == leader1_len);
4628}
4629#endif
4630
4631/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004632 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 */
4634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004635op_format(
4636 oparg_T *oap,
4637 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
4639 long old_line_count = curbuf->b_ml.ml_line_count;
4640
4641 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4642 * can put it back there. */
4643 curwin->w_cursor = oap->cursor_start;
4644
4645 if (u_save((linenr_T)(oap->start.lnum - 1),
4646 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4647 return;
4648 curwin->w_cursor = oap->start;
4649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (oap->is_VIsual)
4651 /* When there is no change: need to remove the Visual selection */
4652 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 /* Set '[ mark at the start of the formatted area */
4655 curbuf->b_op_start = oap->start;
4656
4657 /* For "gw" remember the cursor position and put it back below (adjusted
4658 * for joined and split lines). */
4659 if (keep_cursor)
4660 saved_cursor = oap->cursor_start;
4661
Bram Moolenaar81a82092008-03-12 16:27:00 +00004662 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 /*
4665 * Leave the cursor at the first non-blank of the last formatted line.
4666 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4667 * line, so "." will do the next lines.
4668 */
4669 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4670 ++curwin->w_cursor.lnum;
4671 beginline(BL_WHITE | BL_FIX);
4672 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4673 msgmore(old_line_count);
4674
4675 /* put '] mark on the end of the formatted area */
4676 curbuf->b_op_end = curwin->w_cursor;
4677
4678 if (keep_cursor)
4679 {
4680 curwin->w_cursor = saved_cursor;
4681 saved_cursor.lnum = 0;
4682 }
4683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (oap->is_VIsual)
4685 {
4686 win_T *wp;
4687
4688 FOR_ALL_WINDOWS(wp)
4689 {
4690 if (wp->w_old_cursor_lnum != 0)
4691 {
4692 /* When lines have been inserted or deleted, adjust the end of
4693 * the Visual area to be redrawn. */
4694 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4695 wp->w_old_cursor_lnum += old_line_count;
4696 else
4697 wp->w_old_visual_lnum += old_line_count;
4698 }
4699 }
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701}
4702
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004703#if defined(FEAT_EVAL) || defined(PROTO)
4704/*
4705 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4706 */
4707 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004709{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004710 if (oap->is_VIsual)
4711 /* When there is no change: need to remove the Visual selection */
4712 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004713
Bram Moolenaar700303e2010-07-11 17:35:50 +02004714 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4715 /* As documented: when 'formatexpr' returns non-zero fall back to
4716 * internal formatting. */
4717 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004718}
4719
4720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004721fex_format(
4722 linenr_T lnum,
4723 long count,
4724 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004725{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004726 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4727 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004728 int r;
4729
4730 /*
4731 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004732 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004733 */
4734 set_vim_var_nr(VV_LNUM, lnum);
4735 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004736 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004737
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004738 /*
4739 * Evaluate the function.
4740 */
4741 if (use_sandbox)
4742 ++sandbox;
4743 r = eval_to_number(curbuf->b_p_fex);
4744 if (use_sandbox)
4745 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004746
4747 set_vim_var_string(VV_CHAR, NULL, -1);
4748
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004749 return r;
4750}
4751#endif
4752
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753/*
4754 * Format "line_count" lines, starting at the cursor position.
4755 * When "line_count" is negative, format until the end of the paragraph.
4756 * Lines after the cursor line are saved for undo, caller must have saved the
4757 * first line.
4758 */
4759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004760format_lines(
4761 linenr_T line_count,
4762 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 int max_len;
4765 int is_not_par; /* current line not part of parag. */
4766 int next_is_not_par; /* next line not part of paragraph */
4767 int is_end_par; /* at end of paragraph */
4768 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4769 int next_is_start_par = FALSE;
4770#ifdef FEAT_COMMENTS
4771 int leader_len = 0; /* leader len of current line */
4772 int next_leader_len; /* leader len of next line */
4773 char_u *leader_flags = NULL; /* flags for leader of current line */
4774 char_u *next_leader_flags; /* flags for leader of next line */
4775 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004776 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777#endif
4778 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004779 int second_indent = -1; /* indent for second line (comment
4780 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 int do_second_indent;
4782 int do_number_indent;
4783 int do_trail_white;
4784 int first_par_line = TRUE;
4785 int smd_save;
4786 long count;
4787 int need_set_indent = TRUE; /* set indent of next paragraph */
4788 int force_format = FALSE;
4789 int old_State = State;
4790
4791 /* length of a line to force formatting: 3 * 'tw' */
4792 max_len = comp_textwidth(TRUE) * 3;
4793
4794 /* check for 'q', '2' and '1' in 'formatoptions' */
4795#ifdef FEAT_COMMENTS
4796 do_comments = has_format_option(FO_Q_COMS);
4797#endif
4798 do_second_indent = has_format_option(FO_Q_SECOND);
4799 do_number_indent = has_format_option(FO_Q_NUMBER);
4800 do_trail_white = has_format_option(FO_WHITE_PAR);
4801
4802 /*
4803 * Get info about the previous and current line.
4804 */
4805 if (curwin->w_cursor.lnum > 1)
4806 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4807#ifdef FEAT_COMMENTS
4808 , &leader_len, &leader_flags, do_comments
4809#endif
4810 );
4811 else
4812 is_not_par = TRUE;
4813 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4814#ifdef FEAT_COMMENTS
4815 , &next_leader_len, &next_leader_flags, do_comments
4816#endif
4817 );
4818 is_end_par = (is_not_par || next_is_not_par);
4819 if (!is_end_par && do_trail_white)
4820 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4821
4822 curwin->w_cursor.lnum--;
4823 for (count = line_count; count != 0 && !got_int; --count)
4824 {
4825 /*
4826 * Advance to next paragraph.
4827 */
4828 if (advance)
4829 {
4830 curwin->w_cursor.lnum++;
4831 prev_is_end_par = is_end_par;
4832 is_not_par = next_is_not_par;
4833#ifdef FEAT_COMMENTS
4834 leader_len = next_leader_len;
4835 leader_flags = next_leader_flags;
4836#endif
4837 }
4838
4839 /*
4840 * The last line to be formatted.
4841 */
4842 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4843 {
4844 next_is_not_par = TRUE;
4845#ifdef FEAT_COMMENTS
4846 next_leader_len = 0;
4847 next_leader_flags = NULL;
4848#endif
4849 }
4850 else
4851 {
4852 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4853#ifdef FEAT_COMMENTS
4854 , &next_leader_len, &next_leader_flags, do_comments
4855#endif
4856 );
4857 if (do_number_indent)
4858 next_is_start_par =
4859 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4860 }
4861 advance = TRUE;
4862 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4863 if (!is_end_par && do_trail_white)
4864 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4865
4866 /*
4867 * Skip lines that are not in a paragraph.
4868 */
4869 if (is_not_par)
4870 {
4871 if (line_count < 0)
4872 break;
4873 }
4874 else
4875 {
4876 /*
4877 * For the first line of a paragraph, check indent of second line.
4878 * Don't do this for comments and empty lines.
4879 */
4880 if (first_par_line
4881 && (do_second_indent || do_number_indent)
4882 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004883 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004885 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4886 {
4887#ifdef FEAT_COMMENTS
4888 if (leader_len == 0 && next_leader_len == 0)
4889 {
4890 /* no comment found */
4891#endif
4892 second_indent =
4893 get_indent_lnum(curwin->w_cursor.lnum + 1);
4894#ifdef FEAT_COMMENTS
4895 }
4896 else
4897 {
4898 second_indent = next_leader_len;
4899 do_comments_list = 1;
4900 }
4901#endif
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004904 {
4905#ifdef FEAT_COMMENTS
4906 if (leader_len == 0 && next_leader_len == 0)
4907 {
4908 /* no comment found */
4909#endif
4910 second_indent =
4911 get_number_indent(curwin->w_cursor.lnum);
4912#ifdef FEAT_COMMENTS
4913 }
4914 else
4915 {
4916 /* get_number_indent() is now "comment aware"... */
4917 second_indent =
4918 get_number_indent(curwin->w_cursor.lnum);
4919 do_comments_list = 1;
4920 }
4921#endif
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924
4925 /*
4926 * When the comment leader changes, it's the end of the paragraph.
4927 */
4928 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4929#ifdef FEAT_COMMENTS
4930 || !same_leader(curwin->w_cursor.lnum,
4931 leader_len, leader_flags,
4932 next_leader_len, next_leader_flags)
4933#endif
4934 )
4935 is_end_par = TRUE;
4936
4937 /*
4938 * If we have got to the end of a paragraph, or the line is
4939 * getting long, format it.
4940 */
4941 if (is_end_par || force_format)
4942 {
4943 if (need_set_indent)
4944 /* replace indent in first line with minimal number of
4945 * tabs and spaces, according to current options */
4946 (void)set_indent(get_indent(), SIN_CHANGED);
4947
4948 /* put cursor on last non-space */
4949 State = NORMAL; /* don't go past end-of-line */
4950 coladvance((colnr_T)MAXCOL);
4951 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4952 dec_cursor();
4953
4954 /* do the formatting, without 'showmode' */
4955 State = INSERT; /* for open_line() */
4956 smd_save = p_smd;
4957 p_smd = FALSE;
4958 insertchar(NUL, INSCHAR_FORMAT
4959#ifdef FEAT_COMMENTS
4960 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004961 + (do_comments && do_comments_list
4962 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004964 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 State = old_State;
4966 p_smd = smd_save;
4967 second_indent = -1;
4968 /* at end of par.: need to set indent of next par. */
4969 need_set_indent = is_end_par;
4970 if (is_end_par)
4971 {
4972 /* When called with a negative line count, break at the
4973 * end of the paragraph. */
4974 if (line_count < 0)
4975 break;
4976 first_par_line = TRUE;
4977 }
4978 force_format = FALSE;
4979 }
4980
4981 /*
4982 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004983 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 */
4985 if (!is_end_par)
4986 {
4987 advance = FALSE;
4988 curwin->w_cursor.lnum++;
4989 curwin->w_cursor.col = 0;
4990 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004994 {
4995 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4997 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004998 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005000 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5001 {
5002 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005003 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005004
5005 if (indent > 0)
5006 {
5007 (void)del_bytes(indent, FALSE, FALSE);
5008 mark_col_adjust(curwin->w_cursor.lnum,
5009 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005010 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005013 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
5015 beep_flush();
5016 break;
5017 }
5018 first_par_line = FALSE;
5019 /* If the line is getting long, format it next time */
5020 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5021 force_format = TRUE;
5022 else
5023 force_format = FALSE;
5024 }
5025 }
5026 line_breakcheck();
5027 }
5028}
5029
5030/*
5031 * Return TRUE if line "lnum" ends in a white character.
5032 */
5033 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005034ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036 char_u *s = ml_get(lnum);
5037 size_t l;
5038
5039 if (*s == NUL)
5040 return FALSE;
5041 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5042 * invocation may call function multiple times". */
5043 l = STRLEN(s) - 1;
5044 return vim_iswhite(s[l]);
5045}
5046
5047/*
5048 * Blank lines, and lines containing only the comment leader, are left
5049 * untouched by the formatting. The function returns TRUE in this
5050 * case. It also returns TRUE when a line starts with the end of a comment
5051 * ('e' in comment flags), so that this line is skipped, and not joined to the
5052 * previous line. A new paragraph starts after a blank line, or when the
5053 * comment leader changes -- webb.
5054 */
5055#ifdef FEAT_COMMENTS
5056 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005057fmt_check_par(
5058 linenr_T lnum,
5059 int *leader_len,
5060 char_u **leader_flags,
5061 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062{
5063 char_u *flags = NULL; /* init for GCC */
5064 char_u *ptr;
5065
5066 ptr = ml_get(lnum);
5067 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005068 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 else
5070 *leader_len = 0;
5071
5072 if (*leader_len > 0)
5073 {
5074 /*
5075 * Search for 'e' flag in comment leader flags.
5076 */
5077 flags = *leader_flags;
5078 while (*flags && *flags != ':' && *flags != COM_END)
5079 ++flags;
5080 }
5081
5082 return (*skipwhite(ptr + *leader_len) == NUL
5083 || (*leader_len > 0 && *flags == COM_END)
5084 || startPS(lnum, NUL, FALSE));
5085}
5086#else
5087 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005088fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
5090 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5091}
5092#endif
5093
5094/*
5095 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5096 * previous line is in the same paragraph. Used for auto-formatting.
5097 */
5098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005099paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
5101 char_u *p;
5102#ifdef FEAT_COMMENTS
5103 int leader_len = 0; /* leader len of current line */
5104 char_u *leader_flags = NULL; /* flags for leader of current line */
5105 int next_leader_len; /* leader len of next line */
5106 char_u *next_leader_flags; /* flags for leader of next line */
5107 int do_comments; /* format comments */
5108#endif
5109
5110 if (lnum <= 1)
5111 return TRUE; /* start of the file */
5112
5113 p = ml_get(lnum - 1);
5114 if (*p == NUL)
5115 return TRUE; /* after empty line */
5116
5117#ifdef FEAT_COMMENTS
5118 do_comments = has_format_option(FO_Q_COMS);
5119#endif
5120 if (fmt_check_par(lnum - 1
5121#ifdef FEAT_COMMENTS
5122 , &leader_len, &leader_flags, do_comments
5123#endif
5124 ))
5125 return TRUE; /* after non-paragraph line */
5126
5127 if (fmt_check_par(lnum
5128#ifdef FEAT_COMMENTS
5129 , &next_leader_len, &next_leader_flags, do_comments
5130#endif
5131 ))
5132 return TRUE; /* "lnum" is not a paragraph line */
5133
5134 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5135 return TRUE; /* missing trailing space in previous line. */
5136
5137 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5138 return TRUE; /* numbered item starts in "lnum". */
5139
5140#ifdef FEAT_COMMENTS
5141 if (!same_leader(lnum - 1, leader_len, leader_flags,
5142 next_leader_len, next_leader_flags))
5143 return TRUE; /* change of comment leader. */
5144#endif
5145
5146 return FALSE;
5147}
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
5150 * prepare a few things for block mode yank/delete/tilde
5151 *
5152 * for delete:
5153 * - textlen includes the first/last char to be (partly) deleted
5154 * - start/endspaces is the number of columns that are taken by the
5155 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005156 * deleted.
5157 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 * - textlen includes the first/last char to be wholly yanked
5159 * - start/endspaces is the number of columns of the first/last yanked char
5160 * that are to be yanked.
5161 */
5162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163block_prep(
5164 oparg_T *oap,
5165 struct block_def *bdp,
5166 linenr_T lnum,
5167 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 int incr = 0;
5170 char_u *pend;
5171 char_u *pstart;
5172 char_u *line;
5173 char_u *prev_pstart;
5174 char_u *prev_pend;
5175
5176 bdp->startspaces = 0;
5177 bdp->endspaces = 0;
5178 bdp->textlen = 0;
5179 bdp->start_vcol = 0;
5180 bdp->end_vcol = 0;
5181#ifdef FEAT_VISUALEXTRA
5182 bdp->is_short = FALSE;
5183 bdp->is_oneChar = FALSE;
5184 bdp->pre_whitesp = 0;
5185 bdp->pre_whitesp_c = 0;
5186 bdp->end_char_vcols = 0;
5187#endif
5188 bdp->start_char_vcols = 0;
5189
5190 line = ml_get(lnum);
5191 pstart = line;
5192 prev_pstart = line;
5193 while (bdp->start_vcol < oap->start_vcol && *pstart)
5194 {
5195 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005196 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 bdp->start_vcol += incr;
5198#ifdef FEAT_VISUALEXTRA
5199 if (vim_iswhite(*pstart))
5200 {
5201 bdp->pre_whitesp += incr;
5202 bdp->pre_whitesp_c++;
5203 }
5204 else
5205 {
5206 bdp->pre_whitesp = 0;
5207 bdp->pre_whitesp_c = 0;
5208 }
5209#endif
5210 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005211 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 bdp->start_char_vcols = incr;
5214 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5215 {
5216 bdp->end_vcol = bdp->start_vcol;
5217#ifdef FEAT_VISUALEXTRA
5218 bdp->is_short = TRUE;
5219#endif
5220 if (!is_del || oap->op_type == OP_APPEND)
5221 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5222 }
5223 else
5224 {
5225 /* notice: this converts partly selected Multibyte characters to
5226 * spaces, too. */
5227 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5228 if (is_del && bdp->startspaces)
5229 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5230 pend = pstart;
5231 bdp->end_vcol = bdp->start_vcol;
5232 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5233 {
5234#ifdef FEAT_VISUALEXTRA
5235 bdp->is_oneChar = TRUE;
5236#endif
5237 if (oap->op_type == OP_INSERT)
5238 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5239 else if (oap->op_type == OP_APPEND)
5240 {
5241 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5242 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5243 }
5244 else
5245 {
5246 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5247 if (is_del && oap->op_type != OP_LSHIFT)
5248 {
5249 /* just putting the sum of those two into
5250 * bdp->startspaces doesn't work for Visual replace,
5251 * so we have to split the tab in two */
5252 bdp->startspaces = bdp->start_char_vcols
5253 - (bdp->start_vcol - oap->start_vcol);
5254 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5255 }
5256 }
5257 }
5258 else
5259 {
5260 prev_pend = pend;
5261 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5262 {
5263 /* Count a tab for what it's worth (if list mode not on) */
5264 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005265 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 bdp->end_vcol += incr;
5267 }
5268 if (bdp->end_vcol <= oap->end_vcol
5269 && (!is_del
5270 || oap->op_type == OP_APPEND
5271 || oap->op_type == OP_REPLACE)) /* line too short */
5272 {
5273#ifdef FEAT_VISUALEXTRA
5274 bdp->is_short = TRUE;
5275#endif
5276 /* Alternative: include spaces to fill up the block.
5277 * Disadvantage: can lead to trailing spaces when the line is
5278 * short where the text is put */
5279 /* if (!is_del || oap->op_type == OP_APPEND) */
5280 if (oap->op_type == OP_APPEND || virtual_op)
5281 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005282 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 else
5284 bdp->endspaces = 0; /* replace doesn't add characters */
5285 }
5286 else if (bdp->end_vcol > oap->end_vcol)
5287 {
5288 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5289 if (!is_del && bdp->endspaces)
5290 {
5291 bdp->endspaces = incr - bdp->endspaces;
5292 if (pend != pstart)
5293 pend = prev_pend;
5294 }
5295 }
5296 }
5297#ifdef FEAT_VISUALEXTRA
5298 bdp->end_char_vcols = incr;
5299#endif
5300 if (is_del && bdp->startspaces)
5301 pstart = prev_pstart;
5302 bdp->textlen = (int)(pend - pstart);
5303 }
5304 bdp->textcol = (colnr_T) (pstart - line);
5305 bdp->textstart = pstart;
5306}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005309 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005311 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005312op_addsub(
5313 oparg_T *oap,
5314 linenr_T Prenum1, /* Amount of add/subtract */
5315 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005317 pos_T pos;
5318 struct block_def bd;
5319 int change_cnt = 0;
5320 linenr_T amount = Prenum1;
5321
5322 if (!VIsual_active)
5323 {
5324 pos = curwin->w_cursor;
5325 if (u_save_cursor() == FAIL)
5326 return;
5327 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5328 if (change_cnt)
5329 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5330 }
5331 else
5332 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005333 int one_change;
5334 int length;
5335 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005336
5337 if (u_save((linenr_T)(oap->start.lnum - 1),
5338 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5339 return;
5340
5341 pos = oap->start;
5342 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5343 {
5344 if (oap->block_mode) /* Visual block mode */
5345 {
5346 block_prep(oap, &bd, pos.lnum, FALSE);
5347 pos.col = bd.textcol;
5348 length = bd.textlen;
5349 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005350 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005351 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005352 curwin->w_cursor.col = 0;
5353 pos.col = 0;
5354 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5355 }
5356 else /* oap->motion_type == MCHAR */
5357 {
5358 if (!oap->inclusive)
5359 dec(&(oap->end));
5360 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5361 pos.col = 0;
5362 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005363 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005364 pos.col += oap->start.col;
5365 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005366 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005367 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005368 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005369 length = (int)STRLEN(ml_get(oap->end.lnum));
5370 if (oap->end.col >= length)
5371 oap->end.col = length - 1;
5372 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005373 }
5374 }
5375 one_change = do_addsub(oap->op_type, &pos, length, amount);
5376 if (one_change)
5377 {
5378 /* Remember the start position of the first change. */
5379 if (change_cnt == 0)
5380 startpos = curbuf->b_op_start;
5381 ++change_cnt;
5382 }
5383
5384#ifdef FEAT_NETBEANS_INTG
5385 if (netbeans_active() && one_change)
5386 {
5387 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5388
5389 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5390 netbeans_inserted(curbuf, pos.lnum, pos.col,
5391 &ptr[pos.col], length);
5392 }
5393#endif
5394 if (g_cmd && one_change)
5395 amount += Prenum1;
5396 }
5397 if (change_cnt)
5398 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5399
5400 if (!change_cnt && oap->is_VIsual)
5401 /* No change: need to remove the Visual selection */
5402 redraw_curbuf_later(INVERTED);
5403
5404 /* Set '[ mark if something changed. Keep the last end
5405 * position from do_addsub(). */
5406 if (change_cnt > 0)
5407 curbuf->b_op_start = startpos;
5408
5409 if (change_cnt > p_report)
5410 {
5411 if (change_cnt == 1)
5412 MSG(_("1 line changed"));
5413 else
5414 smsg((char_u *)_("%ld lines changed"), change_cnt);
5415 }
5416 }
5417}
5418
5419/*
5420 * Add or subtract 'Prenum1' from a number in a line
5421 * op_type is OP_NR_ADD or OP_NR_SUB
5422 *
5423 * Returns TRUE if some character was changed.
5424 */
5425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005426do_addsub(
5427 int op_type,
5428 pos_T *pos,
5429 int length,
5430 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005431{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 int col;
5433 char_u *buf1;
5434 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005435 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005437 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 long_u oldn;
5439 char_u *ptr;
5440 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 int todel;
5442 int dohex;
5443 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005444 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 int doalp;
5446 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005448 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005449 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005450 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005451 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005452 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005453 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005454 pos_T startpos;
5455 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5458 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005459 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5461
Bram Moolenaard79e5502016-01-10 22:13:02 +01005462 curwin->w_cursor = *pos;
5463 ptr = ml_get(pos->lnum);
5464 col = pos->col;
5465
5466 if (*ptr == NUL)
5467 goto theend;
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 /*
5470 * First check if we are on a hexadecimal number, after the "0x".
5471 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005472 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005474 if (dobin)
5475 while (col > 0 && vim_isbdigit(ptr[col]))
5476 --col;
5477
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005478 if (dohex)
5479 while (col > 0 && vim_isxdigit(ptr[col]))
5480 --col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005481
5482 if ( dobin
5483 && dohex
5484 && ! ((col > 0
5485 && (ptr[col] == 'X'
5486 || ptr[col] == 'x')
5487 && ptr[col - 1] == '0'
5488 && vim_isxdigit(ptr[col + 1]))))
5489 {
5490
5491 /* In case of binary/hexadecimal pattern overlap match, rescan */
5492
Bram Moolenaard79e5502016-01-10 22:13:02 +01005493 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005494
5495 while (col > 0 && vim_isdigit(ptr[col]))
5496 col--;
5497 }
5498
5499 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005500 && col > 0
5501 && (ptr[col] == 'X'
5502 || ptr[col] == 'x')
5503 && ptr[col - 1] == '0'
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005504 && vim_isxdigit(ptr[col + 1])) ||
5505 ( dobin
5506 && col > 0
5507 && (ptr[col] == 'B'
5508 || ptr[col] == 'b')
5509 && ptr[col - 1] == '0'
5510 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005511 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005512 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005513 --col;
5514 }
5515 else
5516 {
5517 /*
5518 * Search forward and then backward to find the start of number.
5519 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005520 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005521
5522 while (ptr[col] != NUL
5523 && !vim_isdigit(ptr[col])
5524 && !(doalp && ASCII_ISALPHA(ptr[col])))
5525 ++col;
5526
5527 while (col > 0
5528 && vim_isdigit(ptr[col - 1])
5529 && !(doalp && ASCII_ISALPHA(ptr[col])))
5530 --col;
5531 }
5532 }
5533
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005534 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005535 {
5536 while (ptr[col] != NUL && length > 0
5537 && !vim_isdigit(ptr[col])
5538 && !(doalp && ASCII_ISALPHA(ptr[col])))
5539 {
5540 ++col;
5541 --length;
5542 }
5543
5544 if (length == 0)
5545 goto theend;
5546
5547 if (col > pos->col && ptr[col - 1] == '-')
5548 {
5549 negative = TRUE;
5550 was_positive = FALSE;
5551 }
5552 }
5553
5554 /*
5555 * If a number was found, and saving for undo works, replace the number.
5556 */
5557 firstdigit = ptr[col];
5558 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5559 {
5560 beep_flush();
5561 goto theend;
5562 }
5563
5564 if (doalp && ASCII_ISALPHA(firstdigit))
5565 {
5566 /* decrement or increment alphabetic character */
5567 if (op_type == OP_NR_SUB)
5568 {
5569 if (CharOrd(firstdigit) < Prenum1)
5570 {
5571 if (isupper(firstdigit))
5572 firstdigit = 'A';
5573 else
5574 firstdigit = 'a';
5575 }
5576 else
5577#ifdef EBCDIC
5578 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5579#else
5580 firstdigit -= Prenum1;
5581#endif
5582 }
5583 else
5584 {
5585 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5586 {
5587 if (isupper(firstdigit))
5588 firstdigit = 'Z';
5589 else
5590 firstdigit = 'z';
5591 }
5592 else
5593#ifdef EBCDIC
5594 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5595#else
5596 firstdigit += Prenum1;
5597#endif
5598 }
5599 curwin->w_cursor.col = col;
5600 if (!did_change)
5601 startpos = curwin->w_cursor;
5602 did_change = TRUE;
5603 (void)del_char(FALSE);
5604 ins_char(firstdigit);
5605 endpos = curwin->w_cursor;
5606 curwin->w_cursor.col = col;
5607 }
5608 else
5609 {
5610 if (col > 0 && ptr[col - 1] == '-' && !visual)
5611 {
5612 /* negative number */
5613 --col;
5614 negative = TRUE;
5615 }
5616 /* get the number value (unsigned) */
5617 if (visual && VIsual_mode != 'V')
5618 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5619 ? (int)STRLEN(ptr) - col
5620 : length);
5621
5622 vim_str2nr(ptr + col, &pre, &length,
5623 0 + (dobin ? STR2NR_BIN : 0)
5624 + (dooct ? STR2NR_OCT : 0)
5625 + (dohex ? STR2NR_HEX : 0),
5626 NULL, &n, maxlen);
5627
5628 /* ignore leading '-' for hex and octal and bin numbers */
5629 if (pre && negative)
5630 {
5631 ++col;
5632 --length;
5633 negative = FALSE;
5634 }
5635 /* add or subtract */
5636 subtract = FALSE;
5637 if (op_type == OP_NR_SUB)
5638 subtract ^= TRUE;
5639 if (negative)
5640 subtract ^= TRUE;
5641
5642 oldn = n;
5643 if (subtract)
5644 n -= (unsigned long)Prenum1;
5645 else
5646 n += (unsigned long)Prenum1;
5647 /* handle wraparound for decimal numbers */
5648 if (!pre)
5649 {
5650 if (subtract)
5651 {
5652 if (n > oldn)
5653 {
5654 n = 1 + (n ^ (unsigned long)-1);
5655 negative ^= TRUE;
5656 }
5657 }
5658 else
5659 {
5660 /* add */
5661 if (n < oldn)
5662 {
5663 n = (n ^ (unsigned long)-1);
5664 negative ^= TRUE;
5665 }
5666 }
5667 if (n == 0)
5668 negative = FALSE;
5669 }
5670
5671 if (visual && !was_positive && !negative && col > 0)
5672 {
5673 /* need to remove the '-' */
5674 col--;
5675 length++;
5676 }
5677
5678 /*
5679 * Delete the old number.
5680 */
5681 curwin->w_cursor.col = col;
5682 if (!did_change)
5683 startpos = curwin->w_cursor;
5684 did_change = TRUE;
5685 todel = length;
5686 c = gchar_cursor();
5687 /*
5688 * Don't include the '-' in the length, only the length of the
5689 * part after it is kept the same.
5690 */
5691 if (c == '-')
5692 --length;
5693 while (todel-- > 0)
5694 {
5695 if (c < 0x100 && isalpha(c))
5696 {
5697 if (isupper(c))
5698 hexupper = TRUE;
5699 else
5700 hexupper = FALSE;
5701 }
5702 /* del_char() will mark line needing displaying */
5703 (void)del_char(FALSE);
5704 c = gchar_cursor();
5705 }
5706
5707 /*
5708 * Prepare the leading characters in buf1[].
5709 * When there are many leading zeros it could be very long.
5710 * Allocate a bit too much.
5711 */
5712 buf1 = alloc((unsigned)length + NUMBUFLEN);
5713 if (buf1 == NULL)
5714 goto theend;
5715 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005716 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005717 {
5718 *ptr++ = '-';
5719 }
5720 if (pre)
5721 {
5722 *ptr++ = '0';
5723 --length;
5724 }
5725 if (pre == 'b' || pre == 'B' ||
5726 pre == 'x' || pre == 'X')
5727 {
5728 *ptr++ = pre;
5729 --length;
5730 }
5731
5732 /*
5733 * Put the number characters in buf2[].
5734 */
5735 if (pre == 'b' || pre == 'B')
5736 {
5737 int i;
5738 int bit = 0;
5739 int bits = sizeof(unsigned long) * 8;
5740
5741 /* leading zeros */
5742 for (bit = bits; bit > 0; bit--)
5743 if ((n >> (bit - 1)) & 0x1) break;
5744
5745 for (i = 0; bit > 0; bit--)
5746 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5747
5748 buf2[i] = '\0';
5749 }
5750 else if (pre == 0)
5751 sprintf((char *)buf2, "%lu", n);
5752 else if (pre == '0')
5753 sprintf((char *)buf2, "%lo", n);
5754 else if (pre && hexupper)
5755 sprintf((char *)buf2, "%lX", n);
5756 else
5757 sprintf((char *)buf2, "%lx", n);
5758 length -= (int)STRLEN(buf2);
5759
5760 /*
5761 * Adjust number of zeros to the new number of digits, so the
5762 * total length of the number remains the same.
5763 * Don't do this when
5764 * the result may look like an octal number.
5765 */
5766 if (firstdigit == '0' && !(dooct && pre == 0))
5767 while (length-- > 0)
5768 *ptr++ = '0';
5769 *ptr = NUL;
5770 STRCAT(buf1, buf2);
5771 ins_str(buf1); /* insert the new number */
5772 vim_free(buf1);
5773 endpos = curwin->w_cursor;
5774 if (did_change && curwin->w_cursor.col)
5775 --curwin->w_cursor.col;
5776 }
5777
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005778 if (did_change)
5779 {
5780 /* set the '[ and '] marks */
5781 curbuf->b_op_start = startpos;
5782 curbuf->b_op_end = endpos;
5783 if (curbuf->b_op_end.col > 0)
5784 --curbuf->b_op_end.col;
5785 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005786
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005787theend:
5788 if (visual)
5789 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005790 else if (did_change)
5791 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005792
Bram Moolenaard79e5502016-01-10 22:13:02 +01005793 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794}
5795
5796#ifdef FEAT_VIMINFO
5797 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 int eof;
5801 int do_it = TRUE;
5802 int size;
5803 int limit;
5804 int i;
5805 int set_prev = FALSE;
5806 char_u *str;
5807 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005808 int new_type = MCHAR; /* init to shut up compiler */
5809 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 /* We only get here (hopefully) if line[0] == '"' */
5812 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005813
5814 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (*str == '"')
5816 {
5817 set_prev = TRUE;
5818 str++;
5819 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (!ASCII_ISALNUM(*str) && *str != '-')
5822 {
5823 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5824 return TRUE; /* too many errors, pretend end-of-file */
5825 do_it = FALSE;
5826 }
5827 get_yank_register(*str++, FALSE);
5828 if (!force && y_current->y_array != NULL)
5829 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005830
5831 if (*str == '@')
5832 {
5833 /* "x@: register x used for @@ */
5834 if (force || execreg_lastc == NUL)
5835 execreg_lastc = str[-1];
5836 }
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 size = 0;
5839 limit = 100; /* Optimized for registers containing <= 100 lines */
5840 if (do_it)
5841 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005842 /*
5843 * Build the new register in array[].
5844 * y_array is kept as-is until done.
5845 * The "do_it" flag is reset when something is wrong, in which case
5846 * array[] needs to be freed.
5847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (set_prev)
5849 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005850 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005851 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005853 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005855 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005857 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 /* get the block width; if it's missing we get a zero, which is OK */
5859 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005860 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862
5863 while (!(eof = viminfo_readline(virp))
5864 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5865 {
5866 if (do_it)
5867 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005868 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005870 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005872
5873 if (new_array == NULL)
5874 {
5875 do_it = FALSE;
5876 break;
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005879 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005881 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 }
5884 str = viminfo_readstring(virp, 1, TRUE);
5885 if (str != NULL)
5886 array[size++] = str;
5887 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005888 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 do_it = FALSE;
5890 }
5891 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005892
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (do_it)
5894 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005895 /* free y_array[] */
5896 for (i = 0; i < y_current->y_size; i++)
5897 vim_free(y_current->y_array[i]);
5898 vim_free(y_current->y_array);
5899
5900 y_current->y_type = new_type;
5901 y_current->y_width = new_width;
5902 y_current->y_size = size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (size == 0)
5904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 y_current->y_array = NULL;
5906 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005909 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 y_current->y_array =
5911 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5912 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005913 {
5914 if (y_current->y_array == NULL)
5915 vim_free(array[i]);
5916 else
5917 y_current->y_array[i] = array[i];
5918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005921 else
5922 {
5923 /* Free array[] if it was filled. */
5924 for (i = 0; i < size; i++)
5925 vim_free(array[i]);
5926 }
5927 vim_free(array);
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return eof;
5930}
5931
5932 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int i, j;
5936 char_u *type;
5937 char_u c;
5938 int num_lines;
5939 int max_num_lines;
5940 int max_kbyte;
5941 long len;
5942
Bram Moolenaar64404472010-06-26 06:24:45 +02005943 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 /* Get '<' value, use old '"' value if '<' is not found. */
5946 max_num_lines = get_viminfo_parameter('<');
5947 if (max_num_lines < 0)
5948 max_num_lines = get_viminfo_parameter('"');
5949 if (max_num_lines == 0)
5950 return;
5951 max_kbyte = get_viminfo_parameter('s');
5952 if (max_kbyte == 0)
5953 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 for (i = 0; i < NUM_REGISTERS; i++)
5956 {
5957 if (y_regs[i].y_array == NULL)
5958 continue;
5959#ifdef FEAT_CLIPBOARD
5960 /* Skip '*'/'+' register, we don't want them back next time */
5961 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5962 continue;
5963#endif
5964#ifdef FEAT_DND
5965 /* Neither do we want the '~' register */
5966 if (i == TILDE_REGISTER)
5967 continue;
5968#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005969 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005971 if (num_lines == 0
5972 || (num_lines == 1 && y_regs[i].y_type == MCHAR
Bram Moolenaar64404472010-06-26 06:24:45 +02005973 && *y_regs[i].y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005974 continue;
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (max_kbyte > 0)
5977 {
5978 /* Skip register if there is more text than the maximum size. */
5979 len = 0;
5980 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005981 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 if (len > (long)max_kbyte * 1024L)
5983 continue;
5984 }
5985
5986 switch (y_regs[i].y_type)
5987 {
5988 case MLINE:
5989 type = (char_u *)"LINE";
5990 break;
5991 case MCHAR:
5992 type = (char_u *)"CHAR";
5993 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 case MBLOCK:
5995 type = (char_u *)"BLOCK";
5996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 default:
5998 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005999 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 emsg(IObuff);
6001 type = (char_u *)"LINE";
6002 break;
6003 }
6004 if (y_previous == &y_regs[i])
6005 fprintf(fp, "\"");
6006 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006007 fprintf(fp, "\"%c", c);
6008 if (c == execreg_lastc)
6009 fprintf(fp, "@");
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006010 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
6012 /* If max_num_lines < 0, then we save ALL the lines in the register */
6013 if (max_num_lines > 0 && num_lines > max_num_lines)
6014 num_lines = max_num_lines;
6015 for (j = 0; j < num_lines; j++)
6016 {
6017 putc('\t', fp);
6018 viminfo_writestring(fp, y_regs[i].y_array[j]);
6019 }
6020 }
6021}
6022#endif /* FEAT_VIMINFO */
6023
6024#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6025/*
6026 * SELECTION / PRIMARY ('*')
6027 *
6028 * Text selection stuff that uses the GUI selection register '*'. When using a
6029 * GUI this may be text from another window, otherwise it is the last text we
6030 * had highlighted with VIsual mode. With mouse support, clicking the middle
6031 * button performs the paste, otherwise you will need to do <"*p>. "
6032 * If not under X, it is synonymous with the clipboard register '+'.
6033 *
6034 * X CLIPBOARD ('+')
6035 *
6036 * Text selection stuff that uses the GUI clipboard register '+'.
6037 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6038 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6039 * otherwise you will need to do <"+p>. "
6040 * If not under X, it is synonymous with the selection register '*'.
6041 */
6042
6043/*
6044 * Routine to export any final X selection we had to the environment
6045 * so that the text is still available after vim has exited. X selections
6046 * only exist while the owning application exists, so we write to the
6047 * permanent (while X runs) store CUT_BUFFER0.
6048 * Dump the CLIPBOARD selection if we own it (it's logically the more
6049 * 'permanent' of the two), otherwise the PRIMARY one.
6050 * For now, use a hard-coded sanity limit of 1Mb of data.
6051 */
6052#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
6053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006054x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 Display *dpy;
6057 char_u *str = NULL;
6058 long_u len = 0;
6059 int motion_type = -1;
6060
6061# ifdef FEAT_GUI
6062 if (gui.in_use)
6063 dpy = X_DISPLAY;
6064 else
6065# endif
6066# ifdef FEAT_XCLIPBOARD
6067 dpy = xterm_dpy;
6068# else
6069 return;
6070# endif
6071
6072 /* Get selection to export */
6073 if (clip_plus.owned)
6074 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6075 else if (clip_star.owned)
6076 motion_type = clip_convert_selection(&str, &len, &clip_star);
6077
6078 /* Check it's OK */
6079 if (dpy != NULL && str != NULL && motion_type >= 0
6080 && len < 1024*1024 && len > 0)
6081 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006082#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006083 int ok = TRUE;
6084
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006085 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6086 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6087 * encoding conversion usually doesn't work, so keep the text as-is.
6088 */
6089 if (has_mbyte)
6090 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006091 vimconv_T vc;
6092
6093 vc.vc_type = CONV_NONE;
6094 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6095 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006096 int intlen = len;
6097 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006098
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006099 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006100 conv_str = string_convert(&vc, str, &intlen);
6101 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006102 if (conv_str != NULL)
6103 {
6104 vim_free(str);
6105 str = conv_str;
6106 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006107 else
6108 {
6109 ok = FALSE;
6110 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006111 convert_setup(&vc, NULL, NULL);
6112 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006113 else
6114 {
6115 ok = FALSE;
6116 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006117 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006118
6119 /* Do not store the string if conversion failed. Better to use any
6120 * other selection than garbled text. */
6121 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006122#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006123 {
6124 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6125 XFlush(dpy);
6126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 }
6128
6129 vim_free(str);
6130}
6131#endif
6132
6133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006134clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 struct yankreg *y_ptr = y_current;
6137
6138 if (cbd == &clip_plus)
6139 y_current = &y_regs[PLUS_REGISTER];
6140 else
6141 y_current = &y_regs[STAR_REGISTER];
6142 free_yank_all();
6143 y_current->y_size = 0;
6144 y_current = y_ptr;
6145}
6146
6147/*
6148 * Get the selected text and put it in the gui selection register '*' or '+'.
6149 */
6150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 struct yankreg *old_y_previous, *old_y_current;
6154 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 pos_T old_visual;
6156 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 colnr_T old_curswant;
6158 int old_set_curswant;
6159 pos_T old_op_start, old_op_end;
6160 oparg_T oa;
6161 cmdarg_T ca;
6162
6163 if (cbd->owned)
6164 {
6165 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6166 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6167 return;
6168
6169 /* Get the text between clip_star.start & clip_star.end */
6170 old_y_previous = y_previous;
6171 old_y_current = y_current;
6172 old_cursor = curwin->w_cursor;
6173 old_curswant = curwin->w_curswant;
6174 old_set_curswant = curwin->w_set_curswant;
6175 old_op_start = curbuf->b_op_start;
6176 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 old_visual = VIsual;
6178 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 clear_oparg(&oa);
6180 oa.regname = (cbd == &clip_plus ? '+' : '*');
6181 oa.op_type = OP_YANK;
6182 vim_memset(&ca, 0, sizeof(ca));
6183 ca.oap = &oa;
6184 ca.cmdchar = 'y';
6185 ca.count1 = 1;
6186 ca.retval = CA_NO_ADJ_OP_END;
6187 do_pending_operator(&ca, 0, TRUE);
6188 y_previous = old_y_previous;
6189 y_current = old_y_current;
6190 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006191 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 curwin->w_curswant = old_curswant;
6193 curwin->w_set_curswant = old_set_curswant;
6194 curbuf->b_op_start = old_op_start;
6195 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 VIsual = old_visual;
6197 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199 else
6200 {
6201 clip_free_selection(cbd);
6202
6203 /* Try to get selected text from another window */
6204 clip_gen_request_selection(cbd);
6205 }
6206}
6207
Bram Moolenaard44347f2011-06-19 01:14:29 +02006208/*
6209 * Convert from the GUI selection string into the '*'/'+' register.
6210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006212clip_yank_selection(
6213 int type,
6214 char_u *str,
6215 long len,
6216 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
6218 struct yankreg *y_ptr;
6219
6220 if (cbd == &clip_plus)
6221 y_ptr = &y_regs[PLUS_REGISTER];
6222 else
6223 y_ptr = &y_regs[STAR_REGISTER];
6224
6225 clip_free_selection(cbd);
6226
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006227 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228}
6229
6230/*
6231 * Convert the '*'/'+' register into a GUI selection string returned in *str
6232 * with length *len.
6233 * Returns the motion type, or -1 for failure.
6234 */
6235 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006236clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 char_u *p;
6239 int lnum;
6240 int i, j;
6241 int_u eolsize;
6242 struct yankreg *y_ptr;
6243
6244 if (cbd == &clip_plus)
6245 y_ptr = &y_regs[PLUS_REGISTER];
6246 else
6247 y_ptr = &y_regs[STAR_REGISTER];
6248
6249#ifdef USE_CRNL
6250 eolsize = 2;
6251#else
6252 eolsize = 1;
6253#endif
6254
6255 *str = NULL;
6256 *len = 0;
6257 if (y_ptr->y_array == NULL)
6258 return -1;
6259
6260 for (i = 0; i < y_ptr->y_size; i++)
6261 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6262
6263 /*
6264 * Don't want newline character at end of last line if we're in MCHAR mode.
6265 */
6266 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6267 *len -= eolsize;
6268
6269 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6270 if (p == NULL)
6271 return -1;
6272 lnum = 0;
6273 for (i = 0, j = 0; i < (int)*len; i++, j++)
6274 {
6275 if (y_ptr->y_array[lnum][j] == '\n')
6276 p[i] = NUL;
6277 else if (y_ptr->y_array[lnum][j] == NUL)
6278 {
6279#ifdef USE_CRNL
6280 p[i++] = '\r';
6281#endif
6282#ifdef USE_CR
6283 p[i] = '\r';
6284#else
6285 p[i] = '\n';
6286#endif
6287 lnum++;
6288 j = -1;
6289 }
6290 else
6291 p[i] = y_ptr->y_array[lnum][j];
6292 }
6293 return y_ptr->y_type;
6294}
6295
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297/*
6298 * If we have written to a clipboard register, send the text to the clipboard.
6299 */
6300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006301may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6304 {
6305 clip_own_selection(&clip_star);
6306 clip_gen_set_selection(&clip_star);
6307 }
6308 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6309 {
6310 clip_own_selection(&clip_plus);
6311 clip_gen_set_selection(&clip_plus);
6312 }
6313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315#endif /* FEAT_CLIPBOARD || PROTO */
6316
6317
6318#if defined(FEAT_DND) || defined(PROTO)
6319/*
6320 * Replace the contents of the '~' register with str.
6321 */
6322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 struct yankreg *curr;
6326
6327 curr = y_current;
6328 y_current = &y_regs[TILDE_REGISTER];
6329 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006330 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 y_current = curr;
6332}
6333#endif
6334
6335
6336#if defined(FEAT_EVAL) || defined(PROTO)
6337/*
6338 * Return the type of a register.
6339 * Used for getregtype()
6340 * Returns MAUTO for error.
6341 */
6342 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 switch (regname)
6346 {
6347 case '%': /* file name */
6348 case '#': /* alternate file name */
6349 case '=': /* expression */
6350 case ':': /* last command line */
6351 case '/': /* last search-pattern */
6352 case '.': /* last inserted text */
6353#ifdef FEAT_SEARCHPATH
6354 case Ctrl_F: /* Filename under cursor */
6355 case Ctrl_P: /* Path under cursor, expand via "path" */
6356#endif
6357 case Ctrl_W: /* word under cursor */
6358 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6359 case '_': /* black hole: always empty */
6360 return MCHAR;
6361 }
6362
6363#ifdef FEAT_CLIPBOARD
6364 regname = may_get_selection(regname);
6365#endif
6366
Bram Moolenaar32b92012014-01-14 12:33:36 +01006367 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006368 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 get_yank_register(regname, FALSE);
6371
6372 if (y_current->y_array != NULL)
6373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (reglen != NULL && y_current->y_type == MBLOCK)
6375 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 return y_current->y_type;
6377 }
6378 return MAUTO;
6379}
6380
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006381static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006382
6383/*
6384 * When "flags" has GREG_LIST return a list with text "s".
6385 * Otherwise just return "s".
6386 */
6387 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006389{
6390 if (flags & GREG_LIST)
6391 {
6392 list_T *list = list_alloc();
6393
6394 if (list != NULL)
6395 {
6396 if (list_append_string(list, NULL, -1) == FAIL)
6397 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006398 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006399 return NULL;
6400 }
6401 list->lv_first->li_tv.vval.v_string = s;
6402 }
6403 return (char_u *)list;
6404 }
6405 return s;
6406}
6407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408/*
6409 * Return the contents of a register as a single allocated string.
6410 * Used for "@r" in expressions and for getreg().
6411 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006412 * Flags:
6413 * GREG_NO_EXPR Do not allow expression register
6414 * GREG_EXPR_SRC For the expression register: return expression itself,
6415 * not the result of its evaluation.
6416 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 */
6418 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006419get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 long i;
6422 char_u *retval;
6423 int allocated;
6424 long len;
6425
6426 /* Don't allow using an expression register inside an expression */
6427 if (regname == '=')
6428 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006429 if (flags & GREG_NO_EXPR)
6430 return NULL;
6431 if (flags & GREG_EXPR_SRC)
6432 return getreg_wrap_one_line(get_expr_line_src(), flags);
6433 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435
6436 if (regname == '@') /* "@@" is used for unnamed register */
6437 regname = '"';
6438
6439 /* check for valid regname */
6440 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6441 return NULL;
6442
6443#ifdef FEAT_CLIPBOARD
6444 regname = may_get_selection(regname);
6445#endif
6446
6447 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6448 {
6449 if (retval == NULL)
6450 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006451 if (allocated)
6452 return getreg_wrap_one_line(retval, flags);
6453 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455
6456 get_yank_register(regname, FALSE);
6457 if (y_current->y_array == NULL)
6458 return NULL;
6459
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006460 if (flags & GREG_LIST)
6461 {
6462 list_T *list = list_alloc();
6463 int error = FALSE;
6464
6465 if (list == NULL)
6466 return NULL;
6467 for (i = 0; i < y_current->y_size; ++i)
6468 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6469 error = TRUE;
6470 if (error)
6471 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006472 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006473 return NULL;
6474 }
6475 return (char_u *)list;
6476 }
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 /*
6479 * Compute length of resulting string.
6480 */
6481 len = 0;
6482 for (i = 0; i < y_current->y_size; ++i)
6483 {
6484 len += (long)STRLEN(y_current->y_array[i]);
6485 /*
6486 * Insert a newline between lines and after last line if
6487 * y_type is MLINE.
6488 */
6489 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6490 ++len;
6491 }
6492
6493 retval = lalloc(len + 1, TRUE);
6494
6495 /*
6496 * Copy the lines of the yank register into the string.
6497 */
6498 if (retval != NULL)
6499 {
6500 len = 0;
6501 for (i = 0; i < y_current->y_size; ++i)
6502 {
6503 STRCPY(retval + len, y_current->y_array[i]);
6504 len += (long)STRLEN(retval + len);
6505
6506 /*
6507 * Insert a NL between lines and after the last line if y_type is
6508 * MLINE.
6509 */
6510 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6511 retval[len++] = '\n';
6512 }
6513 retval[len] = NUL;
6514 }
6515
6516 return retval;
6517}
6518
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006519 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006520init_write_reg(
6521 int name,
6522 struct yankreg **old_y_previous,
6523 struct yankreg **old_y_current,
6524 int must_append,
6525 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006526{
6527 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6528 {
6529 emsg_invreg(name);
6530 return FAIL;
6531 }
6532
6533 /* Don't want to change the current (unnamed) register */
6534 *old_y_previous = y_previous;
6535 *old_y_current = y_current;
6536
6537 get_yank_register(name, TRUE);
6538 if (!y_append && !must_append)
6539 free_yank_all();
6540 return OK;
6541}
6542
6543 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006544finish_write_reg(
6545 int name,
6546 struct yankreg *old_y_previous,
6547 struct yankreg *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006548{
6549# ifdef FEAT_CLIPBOARD
6550 /* Send text of clipboard register to the clipboard. */
6551 may_set_selection();
6552# endif
6553
6554 /* ':let @" = "val"' should change the meaning of the "" register */
6555 if (name != '"')
6556 y_previous = old_y_previous;
6557 y_current = old_y_current;
6558}
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560/*
6561 * Store string "str" in register "name".
6562 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6563 * If "must_append" is TRUE, always append to the register. Otherwise append
6564 * if "name" is an uppercase letter.
6565 * Note: "maxlen" and "must_append" don't work for the "/" register.
6566 * Careful: 'str' is modified, you may have to use a copy!
6567 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6568 */
6569 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006570write_reg_contents(
6571 int name,
6572 char_u *str,
6573 int maxlen,
6574 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6577}
6578
6579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006580write_reg_contents_lst(
6581 int name,
6582 char_u **strings,
6583 int maxlen UNUSED,
6584 int must_append,
6585 int yank_type,
6586 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006587{
6588 struct yankreg *old_y_previous, *old_y_current;
6589
6590 if (name == '/'
6591#ifdef FEAT_EVAL
6592 || name == '='
6593#endif
6594 )
6595 {
6596 char_u *s;
6597
6598 if (strings[0] == NULL)
6599 s = (char_u *)"";
6600 else if (strings[1] != NULL)
6601 {
6602 EMSG(_("E883: search pattern and expression register may not "
6603 "contain two or more lines"));
6604 return;
6605 }
6606 else
6607 s = strings[0];
6608 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6609 return;
6610 }
6611
6612 if (name == '_') /* black hole: nothing to do */
6613 return;
6614
6615 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6616 &yank_type) == FAIL)
6617 return;
6618
6619 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6620
6621 finish_write_reg(name, old_y_previous, old_y_current);
6622}
6623
6624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006625write_reg_contents_ex(
6626 int name,
6627 char_u *str,
6628 int maxlen,
6629 int must_append,
6630 int yank_type,
6631 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 struct yankreg *old_y_previous, *old_y_current;
6634 long len;
6635
Bram Moolenaare7566042005-06-17 22:00:15 +00006636 if (maxlen >= 0)
6637 len = maxlen;
6638 else
6639 len = (long)STRLEN(str);
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 /* Special case: '/' search pattern */
6642 if (name == '/')
6643 {
6644 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6645 return;
6646 }
6647
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006648 if (name == '#')
6649 {
6650 buf_T *buf;
6651
6652 if (VIM_ISDIGIT(*str))
6653 {
6654 int num = atoi((char *)str);
6655
6656 buf = buflist_findnr(num);
6657 if (buf == NULL)
6658 EMSGN(_(e_nobufnr), (long)num);
6659 }
6660 else
6661 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6662 TRUE, FALSE, FALSE));
6663 if (buf == NULL)
6664 return;
6665 curwin->w_alt_fnum = buf->b_fnum;
6666 return;
6667 }
6668
Bram Moolenaare7566042005-06-17 22:00:15 +00006669#ifdef FEAT_EVAL
6670 if (name == '=')
6671 {
6672 char_u *p, *s;
6673
6674 p = vim_strnsave(str, (int)len);
6675 if (p == NULL)
6676 return;
6677 if (must_append)
6678 {
6679 s = concat_str(get_expr_line_src(), p);
6680 vim_free(p);
6681 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006682 }
6683 set_expr_line(p);
6684 return;
6685 }
6686#endif
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 if (name == '_') /* black hole: nothing to do */
6689 return;
6690
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006691 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6692 &yank_type) == FAIL)
6693 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006695 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006697 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699#endif /* FEAT_EVAL */
6700
6701#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6702/*
6703 * Put a string into a register. When the register is not empty, the string
6704 * is appended.
6705 */
6706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006707str_to_reg(
6708 struct yankreg *y_ptr, /* pointer to yank register */
6709 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6710 char_u *str, /* string to put in register */
6711 long len, /* length of string */
6712 long blocklen, /* width of Visual block */
6713 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006715 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 int lnum;
6717 long start;
6718 long i;
6719 int extra;
6720 int newlines; /* number of lines added */
6721 int extraline = 0; /* extra line at the end */
6722 int append = FALSE; /* append to last line in register */
6723 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006724 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006728 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 y_ptr->y_size = 0;
6730
Bram Moolenaard44347f2011-06-19 01:14:29 +02006731 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006732 type = ((str_list || (len > 0 && (str[len - 1] == NL
6733 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006734 ? MLINE : MCHAR);
6735 else
6736 type = yank_type;
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 /*
6739 * Count the number of lines within the string
6740 */
6741 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006742 if (str_list)
6743 {
6744 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006749 for (i = 0; i < len; i++)
6750 if (str[i] == '\n')
6751 ++newlines;
6752 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6753 {
6754 extraline = 1;
6755 ++newlines; /* count extra newline at the end */
6756 }
6757 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6758 {
6759 append = TRUE;
6760 --newlines; /* uncount newline when appending first line */
6761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 }
6763
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006764 /* Without any lines make the register empty. */
6765 if (y_ptr->y_size + newlines == 0)
6766 {
6767 vim_free(y_ptr->y_array);
6768 y_ptr->y_array = NULL;
6769 return;
6770 }
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 /*
6773 * Allocate an array to hold the pointers to the new register lines.
6774 * If the register was not empty, move the existing lines to the new array.
6775 */
6776 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6777 * sizeof(char_u *), TRUE);
6778 if (pp == NULL) /* out of memory */
6779 return;
6780 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6781 pp[lnum] = y_ptr->y_array[lnum];
6782 vim_free(y_ptr->y_array);
6783 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 /*
6787 * Find the end of each line and save it into the array.
6788 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006789 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006791 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6792 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006793 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006794 pp[lnum] = vim_strnsave(*ss, i);
6795 if (i > maxlen)
6796 maxlen = i;
6797 }
6798 }
6799 else
6800 {
6801 for (start = 0; start < len + extraline; start += i + 1)
6802 {
6803 for (i = start; i < len; ++i) /* find the end of the line */
6804 if (str[i] == '\n')
6805 break;
6806 i -= start; /* i is now length of line */
6807 if (i > maxlen)
6808 maxlen = i;
6809 if (append)
6810 {
6811 --lnum;
6812 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6813 }
6814 else
6815 extra = 0;
6816 s = alloc((unsigned)(i + extra + 1));
6817 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006819 if (extra)
6820 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6821 if (append)
6822 vim_free(y_ptr->y_array[lnum]);
6823 if (i)
6824 mch_memmove(s + extra, str + start, (size_t)i);
6825 extra += i;
6826 s[extra] = NUL;
6827 y_ptr->y_array[lnum++] = s;
6828 while (--extra >= 0)
6829 {
6830 if (*s == NUL)
6831 *s = '\n'; /* replace NUL with newline */
6832 ++s;
6833 }
6834 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837 y_ptr->y_type = type;
6838 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 if (type == MBLOCK)
6840 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6841 else
6842 y_ptr->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843}
6844#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6845
6846 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006847clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 vim_memset(oap, 0, sizeof(oparg_T));
6850}
6851
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006852static long line_count_info(char_u *line, long *wc, long *cc, long limit, int eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006855 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 *
6857 * "Words" are counted by looking for boundaries between non-space and
6858 * space characters. (it seems to produce results that match 'wc'.)
6859 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006860 * Return value is byte count; word count for the line is added to "*wc".
6861 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 *
6863 * The function will only examine the first "limit" characters in the
6864 * line, stopping if it encounters an end-of-line (NUL byte). In that
6865 * case, eol_size will be added to the character count to account for
6866 * the size of the EOL character.
6867 */
6868 static long
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869line_count_info(
6870 char_u *line,
6871 long *wc,
6872 long *cc,
6873 long limit,
6874 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006876 long i;
6877 long words = 0;
6878 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int is_word = 0;
6880
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006881 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 if (is_word)
6884 {
6885 if (vim_isspace(line[i]))
6886 {
6887 words++;
6888 is_word = 0;
6889 }
6890 }
6891 else if (!vim_isspace(line[i]))
6892 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006893 ++chars;
6894#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006895 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006896#else
6897 ++i;
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 if (is_word)
6902 words++;
6903 *wc += words;
6904
6905 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006906 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006909 chars += eol_size;
6910 }
6911 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 return i;
6913}
6914
6915/*
6916 * Give some info about the position of the cursor (for "g CTRL-G").
6917 * In Visual mode, give some info about the selected region. (In this case,
6918 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006919 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 */
6921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006922cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006925 char_u buf1[50];
6926 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006928 long byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01006929#ifdef FEAT_MBYTE
Bram Moolenaared767a22016-01-03 22:49:16 +01006930 long bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01006931#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006932 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 long char_count = 0;
6934 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 long word_count = 0;
6936 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006937 int eol_size;
6938 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 long line_count_selected = 0;
6940 pos_T min_pos, max_pos;
6941 oparg_T oparg;
6942 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 /*
6945 * Compute the length of the file in characters.
6946 */
6947 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6948 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006949 if (dict == NULL)
6950 {
6951 MSG(_(no_lines_msg));
6952 return;
6953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955 else
6956 {
6957 if (get_fileformat(curbuf) == EOL_DOS)
6958 eol_size = 2;
6959 else
6960 eol_size = 1;
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (VIsual_active)
6963 {
6964 if (lt(VIsual, curwin->w_cursor))
6965 {
6966 min_pos = VIsual;
6967 max_pos = curwin->w_cursor;
6968 }
6969 else
6970 {
6971 min_pos = curwin->w_cursor;
6972 max_pos = VIsual;
6973 }
6974 if (*p_sel == 'e' && max_pos.col > 0)
6975 --max_pos.col;
6976
6977 if (VIsual_mode == Ctrl_V)
6978 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006979#ifdef FEAT_LINEBREAK
6980 char_u * saved_sbr = p_sbr;
6981
6982 /* Make 'sbr' empty for a moment to get the correct size. */
6983 p_sbr = empty_option;
6984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 oparg.is_VIsual = 1;
6986 oparg.block_mode = TRUE;
6987 oparg.op_type = OP_NOP;
6988 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006989 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006990#ifdef FEAT_LINEBREAK
6991 p_sbr = saved_sbr;
6992#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006993 if (curwin->w_curswant == MAXCOL)
6994 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 /* Swap the start, end vcol if needed */
6996 if (oparg.end_vcol < oparg.start_vcol)
6997 {
6998 oparg.end_vcol += oparg.start_vcol;
6999 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7000 oparg.end_vcol -= oparg.start_vcol;
7001 }
7002 }
7003 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7007 {
7008 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007009 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 ui_breakcheck();
7012 if (got_int)
7013 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007014 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 }
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 /* Do extra processing for VIsual mode. */
7018 if (VIsual_active
7019 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7020 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007021 char_u *s = NULL;
7022 long len = 0L;
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 switch (VIsual_mode)
7025 {
7026 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007027#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007031#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007033#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007034 s = bd.textstart;
7035 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 break;
7037 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007038 s = ml_get(lnum);
7039 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 break;
7041 case 'v':
7042 {
7043 colnr_T start_col = (lnum == min_pos.lnum)
7044 ? min_pos.col : 0;
7045 colnr_T end_col = (lnum == max_pos.lnum)
7046 ? max_pos.col - start_col + 1 : MAXCOL;
7047
Bram Moolenaardef9e822004-12-31 20:58:58 +00007048 s = ml_get(lnum) + start_col;
7049 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 break;
7052 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007053 if (s != NULL)
7054 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007055 byte_count_cursor += line_count_info(s, &word_count_cursor,
7056 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007057 if (lnum == curbuf->b_ml.ml_line_count
7058 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007059 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007060 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007061 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 }
7064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066 /* In non-visual mode, check for the line the cursor is on */
7067 if (lnum == curwin->w_cursor.lnum)
7068 {
7069 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007070 char_count_cursor += char_count;
7071 byte_count_cursor = byte_count +
7072 line_count_info(ml_get(lnum),
7073 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 (long)(curwin->w_cursor.col + 1), eol_size);
7075 }
7076 }
7077 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007078 byte_count += line_count_info(ml_get(lnum), &word_count,
7079 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081
7082 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007083 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007084 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaared767a22016-01-03 22:49:16 +01007086 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007088 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007090 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7091 {
7092 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7093 &max_pos.col);
7094 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7095 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7096 }
7097 else
7098 buf1[0] = NUL;
7099
7100 if (char_count_cursor == byte_count_cursor
7101 && char_count == byte_count)
7102 vim_snprintf((char *)IObuff, IOSIZE,
7103 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
7104 buf1, line_count_selected,
7105 (long)curbuf->b_ml.ml_line_count,
7106 word_count_cursor, word_count,
7107 byte_count_cursor, byte_count);
7108 else
7109 vim_snprintf((char *)IObuff, IOSIZE,
7110 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
7111 buf1, line_count_selected,
7112 (long)curbuf->b_ml.ml_line_count,
7113 word_count_cursor, word_count,
7114 char_count_cursor, char_count,
7115 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007118 {
7119 p = ml_get_curline();
7120 validate_virtcol();
7121 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7122 (int)curwin->w_virtcol + 1);
7123 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7124 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaared767a22016-01-03 22:49:16 +01007126 if (char_count_cursor == byte_count_cursor
7127 && char_count == byte_count)
7128 vim_snprintf((char *)IObuff, IOSIZE,
7129 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
7130 (char *)buf1, (char *)buf2,
7131 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 (long)curbuf->b_ml.ml_line_count,
7133 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007134 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007135 else
7136 vim_snprintf((char *)IObuff, IOSIZE,
7137 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
7138 (char *)buf1, (char *)buf2,
7139 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007140 (long)curbuf->b_ml.ml_line_count,
7141 word_count_cursor, word_count,
7142 char_count_cursor, char_count,
7143 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007144 }
7145 }
7146
Bram Moolenaared767a22016-01-03 22:49:16 +01007147#ifdef FEAT_MBYTE
7148 bom_count = bomb_size();
7149 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007150 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7151 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007152#endif
7153 if (dict == NULL)
7154 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007155 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007156 p = p_shm;
7157 p_shm = (char_u *)"";
7158 msg(IObuff);
7159 p_shm = p;
7160 }
7161 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007162#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007163 if (dict != NULL)
7164 {
7165 dict_add_nr_str(dict, "words", (long)word_count, NULL);
7166 dict_add_nr_str(dict, "chars", (long)char_count, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007167 dict_add_nr_str(dict, "bytes", (long)byte_count
7168# ifdef FEAT_MBYTE
7169 + bom_count
7170# endif
7171 , NULL);
7172 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7173 (long)byte_count_cursor, NULL);
7174 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7175 (long)char_count_cursor, NULL);
7176 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
7177 (long)word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}