blob: 44cd47366d3034e8c7e9572d7c9c4131637b2d4b [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/*
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020053 * Each yank register has an array of pointers to lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020055typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
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 Moolenaar46bbb0c2016-06-11 21:04:39 +020061#ifdef FEAT_VIMINFO
62 time_t y_time_set;
63#endif
64} yankreg_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020066static yankreg_T y_regs[NUM_REGISTERS];
67
68static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020070static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * structure used by block_prep, op_delete and op_yank for blockwise operators
74 * also op_change, op_shift, op_insert, op_replace - AKelly
75 */
76struct block_def
77{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000078 int startspaces; /* 'extra' cols before first char */
79 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000081 char_u *textstart; /* pointer to 1st char (partially) in block */
82 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 colnr_T start_vcol; /* start col of 1st char wholly inside block */
84 colnr_T end_vcol; /* start col of 1st char wholly after block */
85#ifdef FEAT_VISUALEXTRA
86 int is_short; /* TRUE if line is too short to fit in block */
87 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
88 int is_oneChar; /* TRUE if block within one character */
89 int pre_whitesp; /* screen cols of ws before block */
90 int pre_whitesp_c; /* chars of ws before block */
91 colnr_T end_char_vcols; /* number of vcols of post-block char */
92#endif
93 colnr_T start_char_vcols; /* number of vcols of pre-block char */
94};
95
96#ifdef FEAT_VISUALEXTRA
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010097static void shift_block(oparg_T *oap, int amount);
98static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static int stuff_yank(int, char_u *);
101static void put_reedit_in_typebuf(int silent);
102static int put_in_typebuf(char_u *s, int esc, int colon,
103 int silent);
104static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100106static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100108static void free_yank(long);
109static void free_yank_all(void);
110static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200112static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100113static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaar81340392012-06-06 16:12:59 +0200116#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static char_u *skip_comment(char_u *line, int process, int include_space, int *is_comment);
Bram Moolenaar81340392012-06-06 16:12:59 +0200118#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
120static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200122static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int same_leader(linenr_T lnum, int, char_u *, int, char_u *);
127static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100129static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
131
132/*
133 * The names of operators.
134 * IMPORTANT: Index must correspond with defines in vim.h!!!
135 * The third field indicates whether the operator always works on lines.
136 */
137static char opchars[][3] =
138{
139 {NUL, NUL, FALSE}, /* OP_NOP */
140 {'d', NUL, FALSE}, /* OP_DELETE */
141 {'y', NUL, FALSE}, /* OP_YANK */
142 {'c', NUL, FALSE}, /* OP_CHANGE */
143 {'<', NUL, TRUE}, /* OP_LSHIFT */
144 {'>', NUL, TRUE}, /* OP_RSHIFT */
145 {'!', NUL, TRUE}, /* OP_FILTER */
146 {'g', '~', FALSE}, /* OP_TILDE */
147 {'=', NUL, TRUE}, /* OP_INDENT */
148 {'g', 'q', TRUE}, /* OP_FORMAT */
149 {':', NUL, TRUE}, /* OP_COLON */
150 {'g', 'U', FALSE}, /* OP_UPPER */
151 {'g', 'u', FALSE}, /* OP_LOWER */
152 {'J', NUL, TRUE}, /* DO_JOIN */
153 {'g', 'J', TRUE}, /* DO_JOIN_NS */
154 {'g', '?', FALSE}, /* OP_ROT13 */
155 {'r', NUL, FALSE}, /* OP_REPLACE */
156 {'I', NUL, FALSE}, /* OP_INSERT */
157 {'A', NUL, FALSE}, /* OP_APPEND */
158 {'z', 'f', TRUE}, /* OP_FOLD */
159 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
160 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
161 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
162 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
163 {'z', 'd', TRUE}, /* OP_FOLDDEL */
164 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
165 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000166 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaard79e5502016-01-10 22:13:02 +0100167 {Ctrl_A, NUL, FALSE}, /* OP_NR_ADD */
168 {Ctrl_X, NUL, FALSE}, /* OP_NR_SUB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169};
170
171/*
172 * Translate a command name into an operator type.
173 * Must only be called with a valid operator name!
174 */
175 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100176get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
178 int i;
179
180 if (char1 == 'r') /* ignore second character */
181 return OP_REPLACE;
182 if (char1 == '~') /* when tilde is an operator */
183 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100184 if (char1 == 'g' && char2 == Ctrl_A) /* add */
185 return OP_NR_ADD;
186 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
187 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 for (i = 0; ; ++i)
189 if (opchars[i][0] == char1 && opchars[i][1] == char2)
190 break;
191 return i;
192}
193
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194/*
195 * Return TRUE if operator "op" always works on whole lines.
196 */
197 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100198op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 return opchars[op][2];
201}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
204 * Get first operator command character.
205 * Returns 'g' or 'z' if there is another command character.
206 */
207 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100208get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210 return opchars[optype][0];
211}
212
213/*
214 * Get second operator command character.
215 */
216 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100217get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219 return opchars[optype][1];
220}
221
222/*
223 * op_shift - handle a shift operation
224 */
225 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100226op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
228 long i;
229 int first_char;
230 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
233 if (u_save((linenr_T)(oap->start.lnum - 1),
234 (linenr_T)(oap->end.lnum + 1)) == FAIL)
235 return;
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (oap->block_mode)
238 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239
240 for (i = oap->line_count; --i >= 0; )
241 {
242 first_char = *ml_get_curline();
243 if (first_char == NUL) /* empty line */
244 curwin->w_cursor.col = 0;
245#ifdef FEAT_VISUALEXTRA
246 else if (oap->block_mode)
247 shift_block(oap, amount);
248#endif
249 else
250 /* Move the line right if it doesn't start with '#', 'smartindent'
251 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
252#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
253 if (first_char != '#' || !preprocs_left())
254#endif
255 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000256 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 }
258 ++curwin->w_cursor.lnum;
259 }
260
261 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar2b79bfd2013-07-13 16:34:32 +0200262#ifdef FEAT_FOLDING
263 /* The cursor line is not in a closed fold */
264 foldOpenCursor();
265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 if (oap->block_mode)
268 {
269 curwin->w_cursor.lnum = oap->start.lnum;
270 curwin->w_cursor.col = block_col;
271 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100272 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 {
274 curwin->w_cursor.lnum = oap->start.lnum;
275 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
276 }
277 else
278 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
279
280 if (oap->line_count > p_report)
281 {
282 if (oap->op_type == OP_RSHIFT)
283 s = (char_u *)">";
284 else
285 s = (char_u *)"<";
286 if (oap->line_count == 1)
287 {
288 if (amount == 1)
289 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
290 else
291 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
292 }
293 else
294 {
295 if (amount == 1)
296 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
297 oap->line_count, s);
298 else
299 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
300 oap->line_count, s, amount);
301 }
302 msg(IObuff);
303 }
304
305 /*
306 * Set "'[" and "']" marks.
307 */
308 curbuf->b_op_start = oap->start;
309 curbuf->b_op_end.lnum = oap->end.lnum;
310 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
311 if (curbuf->b_op_end.col > 0)
312 --curbuf->b_op_end.col;
313}
314
315/*
316 * shift the current line one shiftwidth left (if left != 0) or right
317 * leaves cursor on first blank in the line
318 */
319 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100320shift_line(
321 int left,
322 int round,
323 int amount,
324 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 int count;
327 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100328 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329
330 count = get_indent(); /* get current indent */
331
332 if (round) /* round off indent */
333 {
334 i = count / p_sw; /* number of p_sw rounded down */
335 j = count % p_sw; /* extra spaces */
336 if (j && left) /* first remove extra spaces */
337 --amount;
338 if (left)
339 {
340 i -= amount;
341 if (i < 0)
342 i = 0;
343 }
344 else
345 i += amount;
346 count = i * p_sw;
347 }
348 else /* original vi indent */
349 {
350 if (left)
351 {
352 count -= p_sw * amount;
353 if (count < 0)
354 count = 0;
355 }
356 else
357 count += p_sw * amount;
358 }
359
360 /* Set new indent */
361#ifdef FEAT_VREPLACE
362 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000363 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 else
365#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000366 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367}
368
369#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
370/*
371 * Shift one line of the current block one shiftwidth right or left.
372 * Leaves cursor on first character in block.
373 */
374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100375shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376{
377 int left = (oap->op_type == OP_LSHIFT);
378 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000379 int total;
380 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100382 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int p_ts = (int)curbuf->b_p_ts;
384 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000386 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 int i = 0, j = 0;
388 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#ifdef FEAT_RIGHTLEFT
390 int old_p_ri = p_ri;
391
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200392 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393#endif
394
395 State = INSERT; /* don't want REPLACE for State */
396 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
397 if (bd.is_short)
398 return;
399
400 /* total is number of screen columns to be inserted/removed */
401 total = amount * p_sw;
402 oldp = ml_get_curline();
403
404 if (!left)
405 {
406 /*
407 * 1. Get start vcol
408 * 2. Total ws vcols
409 * 3. Divvy into TABs & spp
410 * 4. Construct new string
411 */
412 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
413 ws_vcol = bd.start_vcol - bd.pre_whitesp;
414 if (bd.startspaces)
415 {
416#ifdef FEAT_MBYTE
417 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100418 {
419 if ((*mb_ptr2len)(bd.textstart) == 1)
420 ++bd.textstart;
421 else
422 {
423 ws_vcol = 0;
424 bd.startspaces = 0;
425 }
426 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000429 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431 for ( ; vim_iswhite(*bd.textstart); )
432 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 /* TODO: is passing bd.textstart for start of the line OK? */
434 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
435 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 total += incr;
437 bd.start_vcol += incr;
438 }
439 /* OK, now total=all the VWS reqd, and textstart points at the 1st
440 * non-ws char in the block. */
441 if (!curbuf->b_p_et)
442 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
443 if (i)
444 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
445 else
446 j = total;
447 /* if we're splitting a TAB, allow for it */
448 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
449 len = (int)STRLEN(bd.textstart) + 1;
450 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
451 if (newp == NULL)
452 return;
453 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
454 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200455 vim_memset(newp + bd.textcol, TAB, (size_t)i);
456 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 /* the end */
458 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
459 }
460 else /* left */
461 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000462 colnr_T destination_col; /* column to which text in block will
463 be shifted */
464 char_u *verbatim_copy_end; /* end of the part of the line which is
465 copied verbatim */
466 colnr_T verbatim_copy_width;/* the (displayed) width of this part
467 of line */
468 unsigned fill; /* nr of spaces that replace a TAB */
469 unsigned new_line_len; /* the length of the line after the
470 block shift */
471 size_t block_space_width;
472 size_t shift_amount;
473 char_u *non_white = bd.textstart;
474 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000476 /*
477 * Firstly, let's find the first non-whitespace character that is
478 * displayed after the block's start column and the character's column
479 * number. Also, let's calculate the width of all the whitespace
480 * characters that are displayed in the block and precede the searched
481 * non-whitespace character.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000484 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
485 * the part of which is displayed at the block's beginning. Let's start
486 * searching from the next character. */
487 if (bd.startspaces)
488 mb_ptr_adv(non_white);
489
490 /* The character's column is in "bd.start_vcol". */
491 non_white_col = bd.start_vcol;
492
493 while (vim_iswhite(*non_white))
494 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200495 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000496 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 }
498
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000499 block_space_width = non_white_col - oap->start_vcol;
500 /* We will shift by "total" or "block_space_width", whichever is less.
501 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000502 shift_amount = (block_space_width < (size_t)total
503 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000504
505 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000506 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507
508 /* Now let's find out how much of the beginning of the line we can
509 * reuse without modification. */
510 verbatim_copy_end = bd.textstart;
511 verbatim_copy_width = bd.start_vcol;
512
513 /* If "bd.startspaces" is set, "bd.textstart" points to the character
514 * preceding the block. We have to subtract its width to obtain its
515 * column number. */
516 if (bd.startspaces)
517 verbatim_copy_width -= bd.start_char_vcols;
518 while (verbatim_copy_width < destination_col)
519 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200520 char_u *line = verbatim_copy_end;
521
522 /* TODO: is passing verbatim_copy_end for start of the line OK? */
523 incr = lbr_chartabsize(line, verbatim_copy_end,
524 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000525 if (verbatim_copy_width + incr > destination_col)
526 break;
527 verbatim_copy_width += incr;
528 mb_ptr_adv(verbatim_copy_end);
529 }
530
531 /* If "destination_col" is different from the width of the initial
532 * part of the line that will be copied, it means we encountered a tab
533 * character, which we will have to partly replace with spaces. */
534 fill = destination_col - verbatim_copy_width;
535
536 /* The replacement line will consist of:
537 * - the beginning of the original line up to "verbatim_copy_end",
538 * - "fill" number of spaces,
539 * - the rest of the line, pointed to by non_white. */
540 new_line_len = (unsigned)(verbatim_copy_end - oldp)
541 + fill
542 + (unsigned)STRLEN(non_white) + 1;
543
544 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 if (newp == NULL)
546 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000547 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200548 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000549 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551 /* replace the line */
552 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
553 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
554 State = oldstate;
555 curwin->w_cursor.col = oldcol;
556#ifdef FEAT_RIGHTLEFT
557 p_ri = old_p_ri;
558#endif
559}
560#endif
561
562#ifdef FEAT_VISUALEXTRA
563/*
564 * Insert string "s" (b_insert ? before : after) block :AKelly
565 * Caller must prepare for undo.
566 */
567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100568block_insert(
569 oparg_T *oap,
570 char_u *s,
571 int b_insert,
572 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 int p_ts;
575 int count = 0; /* extra spaces to replace a cut TAB */
576 int spaces = 0; /* non-zero if cutting a TAB */
577 colnr_T offset; /* pointer along new line */
578 unsigned s_len; /* STRLEN(s) */
579 char_u *newp, *oldp; /* new, old lines */
580 linenr_T lnum; /* loop var */
581 int oldstate = State;
582
583 State = INSERT; /* don't want REPLACE for State */
584 s_len = (unsigned)STRLEN(s);
585
586 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
587 {
588 block_prep(oap, bdp, lnum, TRUE);
589 if (bdp->is_short && b_insert)
590 continue; /* OP_INSERT, line ends before block start */
591
592 oldp = ml_get(lnum);
593
594 if (b_insert)
595 {
596 p_ts = bdp->start_char_vcols;
597 spaces = bdp->startspaces;
598 if (spaces != 0)
599 count = p_ts - 1; /* we're cutting a TAB */
600 offset = bdp->textcol;
601 }
602 else /* append */
603 {
604 p_ts = bdp->end_char_vcols;
605 if (!bdp->is_short) /* spaces = padding after block */
606 {
607 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
608 if (spaces != 0)
609 count = p_ts - 1; /* we're cutting a TAB */
610 offset = bdp->textcol + bdp->textlen - (spaces != 0);
611 }
612 else /* spaces = padding to block edge */
613 {
614 /* if $ used, just append to EOL (ie spaces==0) */
615 if (!bdp->is_MAX)
616 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
617 count = spaces;
618 offset = bdp->textcol + bdp->textlen;
619 }
620 }
621
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200622#ifdef FEAT_MBYTE
623 if (has_mbyte && spaces > 0)
624 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100625 int off;
626
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200627 /* Avoid starting halfway a multi-byte character. */
628 if (b_insert)
629 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100630 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 }
632 else
633 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100634 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200636 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100637 spaces -= off;
638 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200639 }
640#endif
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
643 if (newp == NULL)
644 continue;
645
646 /* copy up to shifted part */
647 mch_memmove(newp, oldp, (size_t)(offset));
648 oldp += offset;
649
650 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200651 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653 /* copy the new text */
654 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
655 offset += s_len;
656
657 if (spaces && !bdp->is_short)
658 {
659 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200660 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 /* We're splitting a TAB, don't copy it. */
662 oldp++;
663 /* We allowed for that TAB, remember this now */
664 count++;
665 }
666
667 if (spaces > 0)
668 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000669 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 ml_replace(lnum, newp, FALSE);
672
673 if (lnum == oap->end.lnum)
674 {
675 /* Set "']" mark to the end of the block instead of the end of
676 * the insert in the first line. */
677 curbuf->b_op_end.lnum = oap->end.lnum;
678 curbuf->b_op_end.col = offset;
679 }
680 } /* for all lnum */
681
682 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
683
684 State = oldstate;
685}
686#endif
687
688#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
689/*
690 * op_reindent - handle reindenting a block of lines.
691 */
692 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100693op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 long i;
696 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200697 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 linenr_T first_changed = 0;
699 linenr_T last_changed = 0;
700 linenr_T start_lnum = curwin->w_cursor.lnum;
701
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000702 /* Don't even try when 'modifiable' is off. */
703 if (!curbuf->b_p_ma)
704 {
705 EMSG(_(e_modifiable));
706 return;
707 }
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 for (i = oap->line_count; --i >= 0 && !got_int; )
710 {
711 /* it's a slow thing to do, so give feedback so there's no worry that
712 * the computer's just hung. */
713
714 if (i > 1
715 && (i % 50 == 0 || i == oap->line_count - 1)
716 && oap->line_count > p_report)
717 smsg((char_u *)_("%ld lines to indent... "), i);
718
719 /*
720 * Be vi-compatible: For lisp indenting the first line is not
721 * indented, unless there is only one line.
722 */
723#ifdef FEAT_LISP
724 if (i != oap->line_count - 1 || oap->line_count == 1
725 || how != get_lisp_indent)
726#endif
727 {
728 l = skipwhite(ml_get_curline());
729 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200730 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200732 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200734 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
736 /* did change the indent, call changed_lines() later */
737 if (first_changed == 0)
738 first_changed = curwin->w_cursor.lnum;
739 last_changed = curwin->w_cursor.lnum;
740 }
741 }
742 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000743 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745
746 /* put cursor on first non-blank of indented line */
747 curwin->w_cursor.lnum = start_lnum;
748 beginline(BL_SOL | BL_FIX);
749
750 /* Mark changed lines so that they will be redrawn. When Visual
751 * highlighting was present, need to continue until the last line. When
752 * there is no change still need to remove the Visual highlighting. */
753 if (last_changed != 0)
754 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else if (oap->is_VIsual)
758 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 if (oap->line_count > p_report)
761 {
762 i = oap->line_count - (i + 1);
763 if (i == 1)
764 MSG(_("1 line indented "));
765 else
766 smsg((char_u *)_("%ld lines indented "), i);
767 }
768 /* set '[ and '] marks */
769 curbuf->b_op_start = oap->start;
770 curbuf->b_op_end = oap->end;
771}
772#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
773
774#if defined(FEAT_EVAL) || defined(PROTO)
775/*
776 * Keep the last expression line here, for repeating.
777 */
778static char_u *expr_line = NULL;
779
780/*
781 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
782 * Returns '=' when OK, NUL otherwise.
783 */
784 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100785get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
787 char_u *new_line;
788
789 new_line = getcmdline('=', 0L, 0);
790 if (new_line == NULL)
791 return NUL;
792 if (*new_line == NUL) /* use previous line */
793 vim_free(new_line);
794 else
795 set_expr_line(new_line);
796 return '=';
797}
798
799/*
800 * Set the expression for the '=' register.
801 * Argument must be an allocated string.
802 */
803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100804set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 vim_free(expr_line);
807 expr_line = new_line;
808}
809
810/*
811 * Get the result of the '=' register expression.
812 * Returns a pointer to allocated memory, or NULL for failure.
813 */
814 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100815get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 char_u *expr_copy;
818 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000819 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821 if (expr_line == NULL)
822 return NULL;
823
824 /* Make a copy of the expression, because evaluating it may cause it to be
825 * changed. */
826 expr_copy = vim_strsave(expr_line);
827 if (expr_copy == NULL)
828 return NULL;
829
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000830 /* When we are invoked recursively limit the evaluation to 10 levels.
831 * Then return the string as-is. */
832 if (nested >= 10)
833 return expr_copy;
834
835 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000836 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000837 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 vim_free(expr_copy);
839 return rv;
840}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000841
842/*
843 * Get the '=' register expression itself, without evaluating it.
844 */
845 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100846get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000847{
848 if (expr_line == NULL)
849 return NULL;
850 return vim_strsave(expr_line);
851}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif /* FEAT_EVAL */
853
854/*
855 * Check if 'regname' is a valid name of a yank register.
856 * Note: There is no check for 0 (default register), caller should do this
857 */
858 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100859valid_yank_reg(
860 int regname,
861 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 if ( (regname > 0 && ASCII_ISALNUM(regname))
864 || (!writing && vim_strchr((char_u *)
865#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100866 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100868 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#endif
870 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100871 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 || regname == '"'
873 || regname == '-'
874 || regname == '_'
875#ifdef FEAT_CLIPBOARD
876 || regname == '*'
877 || regname == '+'
878#endif
879#ifdef FEAT_DND
880 || (!writing && regname == '~')
881#endif
882 )
883 return TRUE;
884 return FALSE;
885}
886
887/*
888 * Set y_current and y_append, according to the value of "regname".
889 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000890 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 *
892 * If regname is 0 and writing, use register 0
893 * If regname is 0 and reading, use previous register
894 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000895 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100896get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898 int i;
899
900 y_append = FALSE;
901 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
902 {
903 y_current = y_previous;
904 return;
905 }
906 i = regname;
907 if (VIM_ISDIGIT(i))
908 i -= '0';
909 else if (ASCII_ISLOWER(i))
910 i = CharOrdLow(i) + 10;
911 else if (ASCII_ISUPPER(i))
912 {
913 i = CharOrdUp(i) + 10;
914 y_append = TRUE;
915 }
916 else if (regname == '-')
917 i = DELETION_REGISTER;
918#ifdef FEAT_CLIPBOARD
919 /* When selection is not available, use register 0 instead of '*' */
920 else if (clip_star.available && regname == '*')
921 i = STAR_REGISTER;
922 /* When clipboard is not available, use register 0 instead of '+' */
923 else if (clip_plus.available && regname == '+')
924 i = PLUS_REGISTER;
925#endif
926#ifdef FEAT_DND
927 else if (!writing && regname == '~')
928 i = TILDE_REGISTER;
929#endif
930 else /* not 0-9, a-z, A-Z or '-': use register 0 */
931 i = 0;
932 y_current = &(y_regs[i]);
933 if (writing) /* remember the register we write into for do_put() */
934 y_previous = y_current;
935}
936
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * When "regname" is a clipboard register, obtain the selection. If it's not
940 * available return zero, otherwise return "regname".
941 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100943may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945 if (regname == '*')
946 {
947 if (!clip_star.available)
948 regname = 0;
949 else
950 clip_get_selection(&clip_star);
951 }
952 else if (regname == '+')
953 {
954 if (!clip_plus.available)
955 regname = 0;
956 else
957 clip_get_selection(&clip_plus);
958 }
959 return regname;
960}
961#endif
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963/*
964 * Obtain the contents of a "normal" register. The register is made empty.
965 * The returned pointer has allocated memory, use put_register() later.
966 */
967 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968get_register(
969 int name,
970 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200972 yankreg_T *reg;
973 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975#ifdef FEAT_CLIPBOARD
976 /* When Visual area changed, may have to update selection. Obtain the
977 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000978 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200980 if (clip_isautosel_star())
981 clip_update_selection(&clip_star);
982 may_get_selection(name);
983 }
984 if (name == '+' && clip_plus.available)
985 {
986 if (clip_isautosel_plus())
987 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 may_get_selection(name);
989 }
990#endif
991
992 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200993 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (reg != NULL)
995 {
996 *reg = *y_current;
997 if (copy)
998 {
999 /* If we run out of memory some or all of the lines are empty. */
1000 if (reg->y_size == 0)
1001 reg->y_array = NULL;
1002 else
1003 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1004 * reg->y_size));
1005 if (reg->y_array != NULL)
1006 {
1007 for (i = 0; i < reg->y_size; ++i)
1008 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1009 }
1010 }
1011 else
1012 y_current->y_array = NULL;
1013 }
1014 return (void *)reg;
1015}
1016
1017/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001018 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 */
1020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001021put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
1023 get_yank_register(name, 0);
1024 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001025 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001026 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001028#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 /* Send text written to clipboard register to the clipboard. */
1030 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001033
1034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001035free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001036{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001037 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001038
1039 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001040 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041 free_yank_all();
1042 vim_free(reg);
1043 *y_current = tmp;
1044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046#if defined(FEAT_MOUSE) || defined(PROTO)
1047/*
1048 * return TRUE if the current yank register has type MLINE
1049 */
1050 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001051yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
1053 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1054 return FALSE;
1055 if (regname == '_') /* black hole is always empty */
1056 return FALSE;
1057 get_yank_register(regname, FALSE);
1058 return (y_current->y_type == MLINE);
1059}
1060#endif
1061
1062/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001063 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
1067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001068do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaard55de222007-05-06 13:38:48 +00001070 char_u *p;
1071 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001072 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001073 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 if (Recording == FALSE) /* start recording */
1076 {
1077 /* registers 0-9, a-z and " are allowed */
1078 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1079 retval = FAIL;
1080 else
1081 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001082 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 showmode();
1084 regname = c;
1085 retval = OK;
1086 }
1087 }
1088 else /* stop recording */
1089 {
1090 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001091 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1092 * needs to be removed again to put it in a register. exec_reg then
1093 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
1095 Recording = FALSE;
1096 MSG("");
1097 p = get_recorded();
1098 if (p == NULL)
1099 retval = FAIL;
1100 else
1101 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001102 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1103 vim_unescape_csi(p);
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
1106 * We don't want to change the default register here, so save and
1107 * restore the current register name.
1108 */
1109 old_y_previous = y_previous;
1110 old_y_current = y_current;
1111
1112 retval = stuff_yank(regname, p);
1113
1114 y_previous = old_y_previous;
1115 y_current = old_y_current;
1116 }
1117 }
1118 return retval;
1119}
1120
1121/*
1122 * Stuff string "p" into yank register "regname" as a single line (append if
1123 * uppercase). "p" must have been alloced.
1124 *
1125 * return FAIL for failure, OK otherwise
1126 */
1127 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001128stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
1130 char_u *lp;
1131 char_u **pp;
1132
1133 /* check for read-only register */
1134 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1135 {
1136 vim_free(p);
1137 return FAIL;
1138 }
1139 if (regname == '_') /* black hole: don't do anything */
1140 {
1141 vim_free(p);
1142 return OK;
1143 }
1144 get_yank_register(regname, TRUE);
1145 if (y_append && y_current->y_array != NULL)
1146 {
1147 pp = &(y_current->y_array[y_current->y_size - 1]);
1148 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1149 if (lp == NULL)
1150 {
1151 vim_free(p);
1152 return FAIL;
1153 }
1154 STRCPY(lp, *pp);
1155 STRCAT(lp, p);
1156 vim_free(p);
1157 vim_free(*pp);
1158 *pp = lp;
1159 }
1160 else
1161 {
1162 free_yank_all();
1163 if ((y_current->y_array =
1164 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1165 {
1166 vim_free(p);
1167 return FAIL;
1168 }
1169 y_current->y_array[0] = p;
1170 y_current->y_size = 1;
1171 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001172#ifdef FEAT_VIMINFO
1173 y_current->y_time_set = vim_time();
1174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 return OK;
1177}
1178
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * execute a yank register: copy it into the stuff buffer
1183 *
1184 * return FAIL for failure, OK otherwise
1185 */
1186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001187do_execreg(
1188 int regname,
1189 int colon, /* insert ':' before each line */
1190 int addcr, /* always add '\n' to end of line */
1191 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 long i;
1194 char_u *p;
1195 int retval = OK;
1196 int remap;
1197
1198 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001199 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001200 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001201 {
1202 EMSG(_("E748: No previously used register"));
1203 return FAIL;
1204 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001205 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 /* check for valid regname */
1208 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215#ifdef FEAT_CLIPBOARD
1216 regname = may_get_selection(regname);
1217#endif
1218
1219 if (regname == '_') /* black hole: don't stuff anything */
1220 return OK;
1221
1222#ifdef FEAT_CMDHIST
1223 if (regname == ':') /* use last command line */
1224 {
1225 if (last_cmdline == NULL)
1226 {
1227 EMSG(_(e_nolastcmd));
1228 return FAIL;
1229 }
1230 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1231 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001232 /* Escape all control characters with a CTRL-V */
1233 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001234 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001235 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001236 {
1237 /* When in Visual mode "'<,'>" will be prepended to the command.
1238 * Remove it when it's already there. */
1239 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001240 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001241 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001242 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001243 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001244 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246#endif
1247#ifdef FEAT_EVAL
1248 else if (regname == '=')
1249 {
1250 p = get_expr_line();
1251 if (p == NULL)
1252 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001253 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 vim_free(p);
1255 }
1256#endif
1257 else if (regname == '.') /* use last inserted text */
1258 {
1259 p = get_last_insert_save();
1260 if (p == NULL)
1261 {
1262 EMSG(_(e_noinstext));
1263 return FAIL;
1264 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001265 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 vim_free(p);
1267 }
1268 else
1269 {
1270 get_yank_register(regname, FALSE);
1271 if (y_current->y_array == NULL)
1272 return FAIL;
1273
1274 /* Disallow remaping for ":@r". */
1275 remap = colon ? REMAP_NONE : REMAP_YES;
1276
1277 /*
1278 * Insert lines into typeahead buffer, from last one to first one.
1279 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001280 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 for (i = y_current->y_size; --i >= 0; )
1282 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 char_u *escaped;
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 /* insert NL between lines and after last line if type is MLINE */
1286 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1287 || addcr)
1288 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001289 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 return FAIL;
1291 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001292 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1293 if (escaped == NULL)
1294 return FAIL;
1295 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1296 vim_free(escaped);
1297 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001299 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 == FAIL)
1301 return FAIL;
1302 }
1303 Exec_reg = TRUE; /* disable the 'q' command */
1304 }
1305 return retval;
1306}
1307
1308/*
1309 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1310 * used only after other typeahead has been processed.
1311 */
1312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001313put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 char_u buf[3];
1316
1317 if (restart_edit != NUL)
1318 {
1319 if (restart_edit == 'V')
1320 {
1321 buf[0] = 'g';
1322 buf[1] = 'R';
1323 buf[2] = NUL;
1324 }
1325 else
1326 {
1327 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1328 buf[1] = NUL;
1329 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 restart_edit = NUL;
1332 }
1333}
1334
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001335/*
1336 * Insert register contents "s" into the typeahead buffer, so that it will be
1337 * executed again.
1338 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1339 * no remapping.
1340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001342put_in_typebuf(
1343 char_u *s,
1344 int esc,
1345 int colon, /* add ':' before the line */
1346 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 int retval = OK;
1349
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001350 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001352 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001354 {
1355 char_u *p;
1356
1357 if (esc)
1358 p = vim_strsave_escape_csi(s);
1359 else
1360 p = s;
1361 if (p == NULL)
1362 retval = FAIL;
1363 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001364 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1365 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001366 if (esc)
1367 vim_free(p);
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 return retval;
1372}
1373
1374/*
1375 * Insert a yank register: copy it into the Read buffer.
1376 * Used by CTRL-R command and middle mouse button in insert mode.
1377 *
1378 * return FAIL for failure, OK otherwise
1379 */
1380 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001381insert_reg(
1382 int regname,
1383 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 long i;
1386 int retval = OK;
1387 char_u *arg;
1388 int allocated;
1389
1390 /*
1391 * It is possible to get into an endless loop by having CTRL-R a in
1392 * register a and then, in insert mode, doing CTRL-R a.
1393 * If you hit CTRL-C, the loop will be broken here.
1394 */
1395 ui_breakcheck();
1396 if (got_int)
1397 return FAIL;
1398
1399 /* check for valid regname */
1400 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1401 return FAIL;
1402
1403#ifdef FEAT_CLIPBOARD
1404 regname = may_get_selection(regname);
1405#endif
1406
1407 if (regname == '.') /* insert last inserted text */
1408 retval = stuff_inserted(NUL, 1L, TRUE);
1409 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1410 {
1411 if (arg == NULL)
1412 return FAIL;
1413 stuffescaped(arg, literally);
1414 if (allocated)
1415 vim_free(arg);
1416 }
1417 else /* name or number register */
1418 {
1419 get_yank_register(regname, FALSE);
1420 if (y_current->y_array == NULL)
1421 retval = FAIL;
1422 else
1423 {
1424 for (i = 0; i < y_current->y_size; ++i)
1425 {
1426 stuffescaped(y_current->y_array[i], literally);
1427 /*
1428 * Insert a newline between lines and after last line if
1429 * y_type is MLINE.
1430 */
1431 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1432 stuffcharReadbuff('\n');
1433 }
1434 }
1435 }
1436
1437 return retval;
1438}
1439
1440/*
1441 * Stuff a string into the typeahead buffer, such that edit() will insert it
1442 * literally ("literally" TRUE) or interpret is as typed characters.
1443 */
1444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 int c;
1448 char_u *start;
1449
1450 while (*arg != NUL)
1451 {
1452 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1453 * stuff K_SPECIAL to get the effect of a special key when "literally"
1454 * is TRUE. */
1455 start = arg;
1456 while ((*arg >= ' '
1457#ifndef EBCDIC
1458 && *arg < DEL /* EBCDIC: chars above space are normal */
1459#endif
1460 )
1461 || (*arg == K_SPECIAL && !literally))
1462 ++arg;
1463 if (arg > start)
1464 stuffReadbuffLen(start, (long)(arg - start));
1465
1466 /* stuff a single special character */
1467 if (*arg != NUL)
1468 {
1469#ifdef FEAT_MBYTE
1470 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001471 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 else
1473#endif
1474 c = *arg++;
1475 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1476 stuffcharReadbuff(Ctrl_V);
1477 stuffcharReadbuff(c);
1478 }
1479 }
1480}
1481
1482/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001483 * If "regname" is a special register, return TRUE and store a pointer to its
1484 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001486 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001487get_spec_reg(
1488 int regname,
1489 char_u **argp,
1490 int *allocated, /* return: TRUE when value was allocated */
1491 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493 int cnt;
1494
1495 *argp = NULL;
1496 *allocated = FALSE;
1497 switch (regname)
1498 {
1499 case '%': /* file name */
1500 if (errmsg)
1501 check_fname(); /* will give emsg if not set */
1502 *argp = curbuf->b_fname;
1503 return TRUE;
1504
1505 case '#': /* alternate file name */
1506 *argp = getaltfname(errmsg); /* may give emsg if not set */
1507 return TRUE;
1508
1509#ifdef FEAT_EVAL
1510 case '=': /* result of expression */
1511 *argp = get_expr_line();
1512 *allocated = TRUE;
1513 return TRUE;
1514#endif
1515
1516 case ':': /* last command line */
1517 if (last_cmdline == NULL && errmsg)
1518 EMSG(_(e_nolastcmd));
1519 *argp = last_cmdline;
1520 return TRUE;
1521
1522 case '/': /* last search-pattern */
1523 if (last_search_pat() == NULL && errmsg)
1524 EMSG(_(e_noprevre));
1525 *argp = last_search_pat();
1526 return TRUE;
1527
1528 case '.': /* last inserted text */
1529 *argp = get_last_insert_save();
1530 *allocated = TRUE;
1531 if (*argp == NULL && errmsg)
1532 EMSG(_(e_noinstext));
1533 return TRUE;
1534
1535#ifdef FEAT_SEARCHPATH
1536 case Ctrl_F: /* Filename under cursor */
1537 case Ctrl_P: /* Path under cursor, expand via "path" */
1538 if (!errmsg)
1539 return FALSE;
1540 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001541 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 *allocated = TRUE;
1543 return TRUE;
1544#endif
1545
1546 case Ctrl_W: /* word under cursor */
1547 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1548 if (!errmsg)
1549 return FALSE;
1550 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1551 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1552 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1553 *allocated = TRUE;
1554 return TRUE;
1555
1556 case '_': /* black hole: always empty */
1557 *argp = (char_u *)"";
1558 return TRUE;
1559 }
1560
1561 return FALSE;
1562}
1563
1564/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001565 * Paste a yank register into the command line.
1566 * Only for non-special registers.
1567 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 * insert_reg() can't be used here, because special characters from the
1569 * register contents will be interpreted as commands.
1570 *
1571 * return FAIL for failure, OK otherwise
1572 */
1573 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001574cmdline_paste_reg(
1575 int regname,
1576 int literally, /* Insert text literally instead of "as typed" */
1577 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 get_yank_register(regname, FALSE);
1582 if (y_current->y_array == NULL)
1583 return FAIL;
1584
1585 for (i = 0; i < y_current->y_size; ++i)
1586 {
1587 cmdline_paste_str(y_current->y_array[i], literally);
1588
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001589 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001590 * Don't do this when "remcr" is TRUE. */
1591 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 cmdline_paste_str((char_u *)"\r", literally);
1593
1594 /* Check for CTRL-C, in case someone tries to paste a few thousand
1595 * lines and gets bored. */
1596 ui_breakcheck();
1597 if (got_int)
1598 return FAIL;
1599 }
1600 return OK;
1601}
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1604/*
1605 * Adjust the register name pointed to with "rp" for the clipboard being
1606 * used always and the clipboard being available.
1607 */
1608 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001609adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001611 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1612 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001613 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1614 {
1615 if (clip_unnamed != 0)
1616 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001617 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001618 else
1619 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1620 ? '+' : '*';
1621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (!clip_star.available && *rp == '*')
1623 *rp = 0;
1624 if (!clip_plus.available && *rp == '+')
1625 *rp = 0;
1626}
1627#endif
1628
1629/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001630 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001632 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 */
1634 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001635op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 int n;
1638 linenr_T lnum;
1639 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 char_u *newp, *oldp;
1641 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1643 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001644 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1647 return OK;
1648
1649 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1650 if (oap->empty)
1651 return u_save_cursor();
1652
1653 if (!curbuf->b_p_ma)
1654 {
1655 EMSG(_(e_modifiable));
1656 return FAIL;
1657 }
1658
1659#ifdef FEAT_CLIPBOARD
1660 adjust_clip_reg(&oap->regname);
1661#endif
1662
1663#ifdef FEAT_MBYTE
1664 if (has_mbyte)
1665 mb_adjust_opend(oap);
1666#endif
1667
Bram Moolenaard04b7502010-07-08 22:27:55 +02001668 /*
1669 * Imitate the strange Vi behaviour: If the delete spans more than one
1670 * line and motion_type == MCHAR and the result is a blank line, make the
1671 * delete linewise. Don't do this for the change command or Visual mode.
1672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001675 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001677 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 && oap->op_type == OP_DELETE)
1679 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001680 ptr = ml_get(oap->end.lnum) + oap->end.col;
1681 if (*ptr != NUL)
1682 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 ptr = skipwhite(ptr);
1684 if (*ptr == NUL && inindent(0))
1685 oap->motion_type = MLINE;
1686 }
1687
Bram Moolenaard04b7502010-07-08 22:27:55 +02001688 /*
1689 * Check for trying to delete (e.g. "D") in an empty line.
1690 * Note: For the change operator it is ok.
1691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if ( oap->motion_type == MCHAR
1693 && oap->line_count == 1
1694 && oap->op_type == OP_DELETE
1695 && *ml_get(oap->start.lnum) == NUL)
1696 {
1697 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001698 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * 'cpoptions' (Vi compatible).
1700 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001701#ifdef FEAT_VIRTUALEDIT
1702 if (virtual_op)
1703 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1704 * marks as if it happened. */
1705 goto setmarks;
1706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1708 beep_flush();
1709 return OK;
1710 }
1711
Bram Moolenaard04b7502010-07-08 22:27:55 +02001712 /*
1713 * Do a yank of whatever we're about to delete.
1714 * If a yank register was specified, put the deleted text into that
1715 * register. For the black hole register '_' don't yank anything.
1716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (oap->regname != '_')
1718 {
1719 if (oap->regname != 0)
1720 {
1721 /* check for read-only register */
1722 if (!valid_yank_reg(oap->regname, TRUE))
1723 {
1724 beep_flush();
1725 return OK;
1726 }
1727 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1728 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1729 did_yank = TRUE;
1730 }
1731
1732 /*
1733 * Put deleted text into register 1 and shift number registers if the
1734 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001735 * Use the register name from before adjust_clip_reg() may have
1736 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001738 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 || oap->line_count > 1 || oap->use_reg_one)
1740 {
1741 y_current = &y_regs[9];
1742 free_yank_all(); /* free register nine */
1743 for (n = 9; n > 1; --n)
1744 y_regs[n] = y_regs[n - 1];
1745 y_previous = y_current = &y_regs[1];
1746 y_regs[1].y_array = NULL; /* set register one to empty */
1747 if (op_yank(oap, TRUE, FALSE) == OK)
1748 did_yank = TRUE;
1749 }
1750
Bram Moolenaar84298db2012-04-20 13:46:08 +02001751 /* Yank into small delete register when no named register specified
1752 * and the delete is within one line. */
1753 if ((
1754#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001755 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1756 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001757#endif
1758 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 && oap->line_count == 1)
1760 {
1761 oap->regname = '-';
1762 get_yank_register(oap->regname, TRUE);
1763 if (op_yank(oap, TRUE, FALSE) == OK)
1764 did_yank = TRUE;
1765 oap->regname = 0;
1766 }
1767
1768 /*
1769 * If there's too much stuff to fit in the yank register, then get a
1770 * confirmation before doing the delete. This is crude, but simple.
1771 * And it avoids doing a delete of something we can't put back if we
1772 * want.
1773 */
1774 if (!did_yank)
1775 {
1776 int msg_silent_save = msg_silent;
1777
1778 msg_silent = 0; /* must display the prompt */
1779 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1780 msg_silent = msg_silent_save;
1781 if (n != 'y')
1782 {
1783 EMSG(_(e_abort));
1784 return FAIL;
1785 }
1786 }
1787 }
1788
Bram Moolenaard04b7502010-07-08 22:27:55 +02001789 /*
1790 * block mode delete
1791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (oap->block_mode)
1793 {
1794 if (u_save((linenr_T)(oap->start.lnum - 1),
1795 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1796 return FAIL;
1797
1798 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1799 {
1800 block_prep(oap, &bd, lnum, TRUE);
1801 if (bd.textlen == 0) /* nothing to delete */
1802 continue;
1803
1804 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1805 if (lnum == curwin->w_cursor.lnum)
1806 {
1807 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1808# ifdef FEAT_VIRTUALEDIT
1809 curwin->w_cursor.coladd = 0;
1810# endif
1811 }
1812
1813 /* n == number of chars deleted
1814 * If we delete a TAB, it may be replaced by several characters.
1815 * Thus the number of characters may increase!
1816 */
1817 n = bd.textlen - bd.startspaces - bd.endspaces;
1818 oldp = ml_get(lnum);
1819 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1820 if (newp == NULL)
1821 continue;
1822 /* copy up to deleted part */
1823 mch_memmove(newp, oldp, (size_t)bd.textcol);
1824 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001825 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 (size_t)(bd.startspaces + bd.endspaces));
1827 /* copy the part after the deleted part */
1828 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001829 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 /* replace the line */
1831 ml_replace(lnum, newp, FALSE);
1832 }
1833
1834 check_cursor_col();
1835 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1836 oap->end.lnum + 1, 0L);
1837 oap->line_count = 0; /* no lines deleted */
1838 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001839 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 if (oap->op_type == OP_CHANGE)
1842 {
1843 /* Delete the lines except the first one. Temporarily move the
1844 * cursor to the next line. Save the current line number, if the
1845 * last line is deleted it may be changed.
1846 */
1847 if (oap->line_count > 1)
1848 {
1849 lnum = curwin->w_cursor.lnum;
1850 ++curwin->w_cursor.lnum;
1851 del_lines((long)(oap->line_count - 1), TRUE);
1852 curwin->w_cursor.lnum = lnum;
1853 }
1854 if (u_save_cursor() == FAIL)
1855 return FAIL;
1856 if (curbuf->b_p_ai) /* don't delete indent */
1857 {
1858 beginline(BL_WHITE); /* cursor on first non-white */
1859 did_ai = TRUE; /* delete the indent when ESC hit */
1860 ai_col = curwin->w_cursor.col;
1861 }
1862 else
1863 beginline(0); /* cursor in column 0 */
1864 truncate_line(FALSE); /* delete the rest of the line */
1865 /* leave cursor past last char in line */
1866 if (oap->line_count > 1)
1867 u_clearline(); /* "U" command not possible after "2cc" */
1868 }
1869 else
1870 {
1871 del_lines(oap->line_count, TRUE);
1872 beginline(BL_WHITE | BL_FIX);
1873 u_clearline(); /* "U" command not possible after "dd" */
1874 }
1875 }
1876 else
1877 {
1878#ifdef FEAT_VIRTUALEDIT
1879 if (virtual_op)
1880 {
1881 int endcol = 0;
1882
1883 /* For virtualedit: break the tabs that are partly included. */
1884 if (gchar_pos(&oap->start) == '\t')
1885 {
1886 if (u_save_cursor() == FAIL) /* save first line for undo */
1887 return FAIL;
1888 if (oap->line_count == 1)
1889 endcol = getviscol2(oap->end.col, oap->end.coladd);
1890 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1891 oap->start = curwin->w_cursor;
1892 if (oap->line_count == 1)
1893 {
1894 coladvance(endcol);
1895 oap->end.col = curwin->w_cursor.col;
1896 oap->end.coladd = curwin->w_cursor.coladd;
1897 curwin->w_cursor = oap->start;
1898 }
1899 }
1900
1901 /* Break a tab only when it's included in the area. */
1902 if (gchar_pos(&oap->end) == '\t'
1903 && (int)oap->end.coladd < oap->inclusive)
1904 {
1905 /* save last line for undo */
1906 if (u_save((linenr_T)(oap->end.lnum - 1),
1907 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1908 return FAIL;
1909 curwin->w_cursor = oap->end;
1910 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1911 oap->end = curwin->w_cursor;
1912 curwin->w_cursor = oap->start;
1913 }
1914 }
1915#endif
1916
1917 if (oap->line_count == 1) /* delete characters within one line */
1918 {
1919 if (u_save_cursor() == FAIL) /* save line for undo */
1920 return FAIL;
1921
1922 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001923 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 && oap->op_type == OP_CHANGE
1925 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001926 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 display_dollar(oap->end.col - !oap->inclusive);
1928
1929 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1930
1931#ifdef FEAT_VIRTUALEDIT
1932 if (virtual_op)
1933 {
1934 /* fix up things for virtualedit-delete:
1935 * break the tabs which are going to get in our way
1936 */
1937 char_u *curline = ml_get_curline();
1938 int len = (int)STRLEN(curline);
1939
1940 if (oap->end.coladd != 0
1941 && (int)oap->end.col >= len - 1
1942 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1943 n++;
1944 /* Delete at least one char (e.g, when on a control char). */
1945 if (n == 0 && oap->start.coladd != oap->end.coladd)
1946 n = 1;
1947
1948 /* When deleted a char in the line, reset coladd. */
1949 if (gchar_cursor() != NUL)
1950 curwin->w_cursor.coladd = 0;
1951 }
1952#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001953 (void)del_bytes((long)n, !virtual_op,
1954 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
1956 else /* delete characters between lines */
1957 {
1958 pos_T curpos;
1959
1960 /* save deleted and changed lines for undo */
1961 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1962 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1963 return FAIL;
1964
1965 truncate_line(TRUE); /* delete from cursor to end of line */
1966
1967 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1968 ++curwin->w_cursor.lnum;
1969 del_lines((long)(oap->line_count - 2), FALSE);
1970
Bram Moolenaard009e862015-06-09 20:20:03 +02001971 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001972 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02001973 curwin->w_cursor.col = 0;
1974 (void)del_bytes((long)n, !virtual_op,
1975 oap->op_type == OP_DELETE && !oap->is_VIsual);
1976 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1977 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 }
1980
1981 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1982
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001983#ifdef FEAT_VIRTUALEDIT
1984setmarks:
1985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (oap->block_mode)
1987 {
1988 curbuf->b_op_end.lnum = oap->end.lnum;
1989 curbuf->b_op_end.col = oap->start.col;
1990 }
1991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 curbuf->b_op_end = oap->start;
1993 curbuf->b_op_start = oap->start;
1994
1995 return OK;
1996}
1997
1998#ifdef FEAT_MBYTE
1999/*
2000 * Adjust end of operating area for ending on a multi-byte character.
2001 * Used for deletion.
2002 */
2003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 char_u *p;
2007
2008 if (oap->inclusive)
2009 {
2010 p = ml_get(oap->end.lnum);
2011 oap->end.col += mb_tail_off(p, p + oap->end.col);
2012 }
2013}
2014#endif
2015
2016#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2017/*
2018 * Replace a whole area with one character.
2019 */
2020 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 int n, numc;
2024#ifdef FEAT_MBYTE
2025 int num_chars;
2026#endif
2027 char_u *newp, *oldp;
2028 size_t oldlen;
2029 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002030 char_u *after_p = NULL;
2031 int had_ctrl_v_cr = (c == -1 || c == -2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2034 return OK; /* nothing to do */
2035
Bram Moolenaard9820532013-11-04 01:41:17 +01002036 if (had_ctrl_v_cr)
2037 c = (c == -1 ? '\r' : '\n');
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#ifdef FEAT_MBYTE
2040 if (has_mbyte)
2041 mb_adjust_opend(oap);
2042#endif
2043
2044 if (u_save((linenr_T)(oap->start.lnum - 1),
2045 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2046 return FAIL;
2047
2048 /*
2049 * block mode replace
2050 */
2051 if (oap->block_mode)
2052 {
2053 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2054 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2055 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002056 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2058 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2059 continue; /* nothing to replace */
2060
2061 /* n == number of extra chars required
2062 * If we split a TAB, it may be replaced by several characters.
2063 * Thus the number of characters may increase!
2064 */
2065#ifdef FEAT_VIRTUALEDIT
2066 /* If the range starts in virtual space, count the initial
2067 * coladd offset as part of "startspaces" */
2068 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2069 {
2070 pos_T vpos;
2071
Bram Moolenaara1381de2009-11-03 15:44:21 +00002072 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 getvpos(&vpos, oap->start_vcol);
2074 bd.startspaces += vpos.coladd;
2075 n = bd.startspaces;
2076 }
2077 else
2078#endif
2079 /* allow for pre spaces */
2080 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2081
2082 /* allow for post spp */
2083 n += (bd.endspaces
2084#ifdef FEAT_VIRTUALEDIT
2085 && !bd.is_oneChar
2086#endif
2087 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2088 /* Figure out how many characters to replace. */
2089 numc = oap->end_vcol - oap->start_vcol + 1;
2090 if (bd.is_short && (!virtual_op || bd.is_MAX))
2091 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2092
2093#ifdef FEAT_MBYTE
2094 /* A double-wide character can be replaced only up to half the
2095 * times. */
2096 if ((*mb_char2cells)(c) > 1)
2097 {
2098 if ((numc & 1) && !bd.is_short)
2099 {
2100 ++bd.endspaces;
2101 ++n;
2102 }
2103 numc = numc / 2;
2104 }
2105
2106 /* Compute bytes needed, move character count to num_chars. */
2107 num_chars = numc;
2108 numc *= (*mb_char2len)(c);
2109#endif
2110 /* oldlen includes textlen, so don't double count */
2111 n += numc - bd.textlen;
2112
2113 oldp = ml_get_curline();
2114 oldlen = STRLEN(oldp);
2115 newp = alloc_check((unsigned)oldlen + 1 + n);
2116 if (newp == NULL)
2117 continue;
2118 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2119 /* copy up to deleted part */
2120 mch_memmove(newp, oldp, (size_t)bd.textcol);
2121 oldp += bd.textcol + bd.textlen;
2122 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002123 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaard9820532013-11-04 01:41:17 +01002125 /* -1/-2 is used for entering CR literally. */
2126 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002128#ifdef FEAT_MBYTE
2129 if (has_mbyte)
2130 {
2131 n = (int)STRLEN(newp);
2132 while (--num_chars >= 0)
2133 n += (*mb_char2bytes)(c, newp + n);
2134 }
2135 else
2136#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002137 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002138 if (!bd.is_short)
2139 {
2140 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002141 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002142 /* copy the part after the changed part */
2143 STRMOVE(newp + STRLEN(newp), oldp);
2144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002148 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002149 after_p = alloc_check(
2150 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002151 if (after_p != NULL)
2152 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 /* replace the line */
2155 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002156 if (after_p != NULL)
2157 {
2158 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2159 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2160 oap->end.lnum++;
2161 vim_free(after_p);
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 }
2165 else
2166 {
2167 /*
2168 * MCHAR and MLINE motion replace.
2169 */
2170 if (oap->motion_type == MLINE)
2171 {
2172 oap->start.col = 0;
2173 curwin->w_cursor.col = 0;
2174 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2175 if (oap->end.col)
2176 --oap->end.col;
2177 }
2178 else if (!oap->inclusive)
2179 dec(&(oap->end));
2180
2181 while (ltoreq(curwin->w_cursor, oap->end))
2182 {
2183 n = gchar_cursor();
2184 if (n != NUL)
2185 {
2186#ifdef FEAT_MBYTE
2187 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2188 {
2189 /* This is slow, but it handles replacing a single-byte
2190 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002191 if (curwin->w_cursor.lnum == oap->end.lnum)
2192 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 n = State;
2194 State = REPLACE;
2195 ins_char(c);
2196 State = n;
2197 /* Backup to the replaced character. */
2198 dec_cursor();
2199 }
2200 else
2201#endif
2202 {
2203#ifdef FEAT_VIRTUALEDIT
2204 if (n == TAB)
2205 {
2206 int end_vcol = 0;
2207
2208 if (curwin->w_cursor.lnum == oap->end.lnum)
2209 {
2210 /* oap->end has to be recalculated when
2211 * the tab breaks */
2212 end_vcol = getviscol2(oap->end.col,
2213 oap->end.coladd);
2214 }
2215 coladvance_force(getviscol());
2216 if (curwin->w_cursor.lnum == oap->end.lnum)
2217 getvpos(&oap->end, end_vcol);
2218 }
2219#endif
2220 pchar(curwin->w_cursor, c);
2221 }
2222 }
2223#ifdef FEAT_VIRTUALEDIT
2224 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2225 {
2226 int virtcols = oap->end.coladd;
2227
2228 if (curwin->w_cursor.lnum == oap->start.lnum
2229 && oap->start.col == oap->end.col && oap->start.coladd)
2230 virtcols -= oap->start.coladd;
2231
2232 /* oap->end has been trimmed so it's effectively inclusive;
2233 * as a result an extra +1 must be counted so we don't
2234 * trample the NUL byte. */
2235 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2236 curwin->w_cursor.col -= (virtcols + 1);
2237 for (; virtcols >= 0; virtcols--)
2238 {
2239 pchar(curwin->w_cursor, c);
2240 if (inc(&curwin->w_cursor) == -1)
2241 break;
2242 }
2243 }
2244#endif
2245
2246 /* Advance to next character, stop at the end of the file. */
2247 if (inc_cursor() == -1)
2248 break;
2249 }
2250 }
2251
2252 curwin->w_cursor = oap->start;
2253 check_cursor();
2254 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2255
2256 /* Set "'[" and "']" marks. */
2257 curbuf->b_op_start = oap->start;
2258 curbuf->b_op_end = oap->end;
2259
2260 return OK;
2261}
2262#endif
2263
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002264static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266/*
2267 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2268 */
2269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002270op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002274 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
2276 if (u_save((linenr_T)(oap->start.lnum - 1),
2277 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2278 return;
2279
2280 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (oap->block_mode) /* Visual block mode */
2282 {
2283 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2284 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002285 int one_change;
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 block_prep(oap, &bd, pos.lnum, FALSE);
2288 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002289 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2290 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002291
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002292#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002293 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2296
Bram Moolenaar009b2592004-10-24 19:18:58 +00002297 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2298 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002300 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 if (did_change)
2305 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2306 }
2307 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 if (oap->motion_type == MLINE)
2310 {
2311 oap->start.col = 0;
2312 pos.col = 0;
2313 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2314 if (oap->end.col)
2315 --oap->end.col;
2316 }
2317 else if (!oap->inclusive)
2318 dec(&(oap->end));
2319
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002320 if (pos.lnum == oap->end.lnum)
2321 did_change = swapchars(oap->op_type, &pos,
2322 oap->end.col - pos.col + 1);
2323 else
2324 for (;;)
2325 {
2326 did_change |= swapchars(oap->op_type, &pos,
2327 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2328 (int)STRLEN(ml_get_pos(&pos)));
2329 if (ltoreq(oap->end, pos) || inc(&pos) == -1)
2330 break;
2331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (did_change)
2333 {
2334 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2335 0L);
2336#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002337 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 char_u *ptr;
2340 int count;
2341
2342 pos = oap->start;
2343 while (pos.lnum < oap->end.lnum)
2344 {
2345 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002346 count = (int)STRLEN(ptr) - pos.col;
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 pos.col = 0;
2351 pos.lnum++;
2352 }
2353 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2354 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002355 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002357 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359#endif
2360 }
2361 }
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (!did_change && oap->is_VIsual)
2364 /* No change: need to remove the Visual selection */
2365 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 /*
2368 * Set '[ and '] marks.
2369 */
2370 curbuf->b_op_start = oap->start;
2371 curbuf->b_op_end = oap->end;
2372
2373 if (oap->line_count > p_report)
2374 {
2375 if (oap->line_count == 1)
2376 MSG(_("1 line changed"));
2377 else
2378 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2379 }
2380}
2381
2382/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002383 * Invoke swapchar() on "length" bytes at position "pos".
2384 * "pos" is advanced to just after the changed characters.
2385 * "length" is rounded up to include the whole last multi-byte character.
2386 * Also works correctly when the number of bytes changes.
2387 * Returns TRUE if some character was changed.
2388 */
2389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002390swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002391{
2392 int todo;
2393 int did_change = 0;
2394
2395 for (todo = length; todo > 0; --todo)
2396 {
2397# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002398 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002399 {
2400 int len = (*mb_ptr2len)(ml_get_pos(pos));
2401
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002402 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002403 if (len > 0)
2404 todo -= len - 1;
2405 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002406# endif
2407 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002408 if (inc(pos) == -1) /* at end of file */
2409 break;
2410 }
2411 return did_change;
2412}
2413
2414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 * If op_type == OP_UPPER: make uppercase,
2416 * if op_type == OP_LOWER: make lowercase,
2417 * if op_type == OP_ROT13: do rot13 encoding,
2418 * else swap case of character at 'pos'
2419 * returns TRUE when something actually changed.
2420 */
2421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002422swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 int c;
2425 int nc;
2426
2427 c = gchar_pos(pos);
2428
2429 /* Only do rot13 encoding for ASCII characters. */
2430 if (c >= 0x80 && op_type == OP_ROT13)
2431 return FALSE;
2432
2433#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002434 if (op_type == OP_UPPER && c == 0xdf
2435 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002436 {
2437 pos_T sp = curwin->w_cursor;
2438
2439 /* Special handling of German sharp s: change to "SS". */
2440 curwin->w_cursor = *pos;
2441 del_char(FALSE);
2442 ins_char('S');
2443 ins_char('S');
2444 curwin->w_cursor = sp;
2445 inc(pos);
2446 }
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2449 return FALSE;
2450#endif
2451 nc = c;
2452 if (MB_ISLOWER(c))
2453 {
2454 if (op_type == OP_ROT13)
2455 nc = ROT13(c, 'a');
2456 else if (op_type != OP_LOWER)
2457 nc = MB_TOUPPER(c);
2458 }
2459 else if (MB_ISUPPER(c))
2460 {
2461 if (op_type == OP_ROT13)
2462 nc = ROT13(c, 'A');
2463 else if (op_type != OP_UPPER)
2464 nc = MB_TOLOWER(c);
2465 }
2466 if (nc != c)
2467 {
2468#ifdef FEAT_MBYTE
2469 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2470 {
2471 pos_T sp = curwin->w_cursor;
2472
2473 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002474 /* don't use del_char(), it also removes composing chars */
2475 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 ins_char(nc);
2477 curwin->w_cursor = sp;
2478 }
2479 else
2480#endif
2481 pchar(*pos, nc);
2482 return TRUE;
2483 }
2484 return FALSE;
2485}
2486
2487#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2488/*
2489 * op_insert - Insert and append operators for Visual mode.
2490 */
2491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002492op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 long ins_len, pre_textlen = 0;
2495 char_u *firstline, *ins_text;
2496 struct block_def bd;
2497 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002498 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 /* edit() changes this - record it for OP_APPEND */
2501 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2502
2503 /* vis block is still marked. Get rid of it now. */
2504 curwin->w_cursor.lnum = oap->start.lnum;
2505 update_screen(INVERTED);
2506
2507 if (oap->block_mode)
2508 {
2509#ifdef FEAT_VIRTUALEDIT
2510 /* When 'virtualedit' is used, need to insert the extra spaces before
2511 * doing block_prep(). When only "block" is used, virtual edit is
2512 * already disabled, but still need it when calling
2513 * coladvance_force(). */
2514 if (curwin->w_cursor.coladd > 0)
2515 {
2516 int old_ve_flags = ve_flags;
2517
2518 ve_flags = VE_ALL;
2519 if (u_save_cursor() == FAIL)
2520 return;
2521 coladvance_force(oap->op_type == OP_APPEND
2522 ? oap->end_vcol + 1 : getviscol());
2523 if (oap->op_type == OP_APPEND)
2524 --curwin->w_cursor.col;
2525 ve_flags = old_ve_flags;
2526 }
2527#endif
2528 /* Get the info about the block before entering the text */
2529 block_prep(oap, &bd, oap->start.lnum, TRUE);
2530 firstline = ml_get(oap->start.lnum) + bd.textcol;
2531 if (oap->op_type == OP_APPEND)
2532 firstline += bd.textlen;
2533 pre_textlen = (long)STRLEN(firstline);
2534 }
2535
2536 if (oap->op_type == OP_APPEND)
2537 {
2538 if (oap->block_mode
2539#ifdef FEAT_VIRTUALEDIT
2540 && curwin->w_cursor.coladd == 0
2541#endif
2542 )
2543 {
2544 /* Move the cursor to the character right of the block. */
2545 curwin->w_set_curswant = TRUE;
2546 while (*ml_get_cursor() != NUL
2547 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2548 ++curwin->w_cursor.col;
2549 if (bd.is_short && !bd.is_MAX)
2550 {
2551 /* First line was too short, make it longer and adjust the
2552 * values in "bd". */
2553 if (u_save_cursor() == FAIL)
2554 return;
2555 for (i = 0; i < bd.endspaces; ++i)
2556 ins_char(' ');
2557 bd.textlen += bd.endspaces;
2558 }
2559 }
2560 else
2561 {
2562 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002563 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 /* Works just like an 'i'nsert on the next character. */
2566 if (!lineempty(curwin->w_cursor.lnum)
2567 && oap->start_vcol != oap->end_vcol)
2568 inc_cursor();
2569 }
2570 }
2571
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002572 t1 = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 edit(NUL, FALSE, (linenr_T)count1);
2574
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002575 /* When a tab was inserted, and the characters in front of the tab
2576 * have been converted to a tab as well, the column of the cursor
2577 * might have actually been reduced, so need to adjust here. */
2578 if (t1.lnum == curbuf->b_op_start_orig.lnum
2579 && lt(curbuf->b_op_start_orig, t1))
2580 oap->start = curbuf->b_op_start_orig;
2581
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002582 /* If user has moved off this line, we don't know what to do, so do
2583 * nothing.
2584 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2585 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 return;
2587
2588 if (oap->block_mode)
2589 {
2590 struct block_def bd2;
2591
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002592 /* The user may have moved the cursor before inserting something, try
2593 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002594 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002595 {
2596 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002597 && oap->start.col
2598#ifdef FEAT_VIRTUALEDIT
2599 + oap->start.coladd
2600#endif
2601 != curbuf->b_op_start_orig.col
2602#ifdef FEAT_VIRTUALEDIT
2603 + curbuf->b_op_start_orig.coladd
2604#endif
2605 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002606 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002607 int t = getviscol2(curbuf->b_op_start_orig.col,
2608 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002609 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002610 pre_textlen -= t - oap->start_vcol;
2611 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002612 }
2613 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002614 && oap->end.col
2615#ifdef FEAT_VIRTUALEDIT
2616 + oap->end.coladd
2617#endif
2618 >= curbuf->b_op_start_orig.col
2619#ifdef FEAT_VIRTUALEDIT
2620 + curbuf->b_op_start_orig.coladd
2621#endif
2622 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002623 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002624 int t = getviscol2(curbuf->b_op_start_orig.col,
2625 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002626 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002627 /* reset pre_textlen to the value of OP_INSERT */
2628 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002629 pre_textlen -= t - oap->start_vcol;
2630 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002631 oap->op_type = OP_INSERT;
2632 }
2633 }
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 /*
2636 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002637 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * Don't do this when "$" used, end-of-line will have changed.
2639 */
2640 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2641 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2642 {
2643 if (oap->op_type == OP_APPEND)
2644 {
2645 pre_textlen += bd2.textlen - bd.textlen;
2646 if (bd2.endspaces)
2647 --bd2.textlen;
2648 }
2649 bd.textcol = bd2.textcol;
2650 bd.textlen = bd2.textlen;
2651 }
2652
2653 /*
2654 * Subsequent calls to ml_get() flush the firstline data - take a
2655 * copy of the required string.
2656 */
2657 firstline = ml_get(oap->start.lnum) + bd.textcol;
2658 if (oap->op_type == OP_APPEND)
2659 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002660 if (pre_textlen >= 0
2661 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 ins_text = vim_strnsave(firstline, (int)ins_len);
2664 if (ins_text != NULL)
2665 {
2666 /* block handled here */
2667 if (u_save(oap->start.lnum,
2668 (linenr_T)(oap->end.lnum + 1)) == OK)
2669 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2670 &bd);
2671
2672 curwin->w_cursor.col = oap->start.col;
2673 check_cursor();
2674 vim_free(ins_text);
2675 }
2676 }
2677 }
2678}
2679#endif
2680
2681/*
2682 * op_change - handle a change operation
2683 *
2684 * return TRUE if edit() returns because of a CTRL-O command
2685 */
2686 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002687op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 colnr_T l;
2690 int retval;
2691#ifdef FEAT_VISUALEXTRA
2692 long offset;
2693 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002694 long ins_len;
2695 long pre_textlen = 0;
2696 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 char_u *firstline;
2698 char_u *ins_text, *newp, *oldp;
2699 struct block_def bd;
2700#endif
2701
2702 l = oap->start.col;
2703 if (oap->motion_type == MLINE)
2704 {
2705 l = 0;
2706#ifdef FEAT_SMARTINDENT
2707 if (!p_paste && curbuf->b_p_si
2708# ifdef FEAT_CINDENT
2709 && !curbuf->b_p_cin
2710# endif
2711 )
2712 can_si = TRUE; /* It's like opening a new line, do si */
2713#endif
2714 }
2715
2716 /* First delete the text in the region. In an empty buffer only need to
2717 * save for undo */
2718 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2719 {
2720 if (u_save_cursor() == FAIL)
2721 return FALSE;
2722 }
2723 else if (op_delete(oap) == FAIL)
2724 return FALSE;
2725
2726 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2727 && !virtual_op)
2728 inc_cursor();
2729
2730#ifdef FEAT_VISUALEXTRA
2731 /* check for still on same line (<CR> in inserted text meaningless) */
2732 /* skip blank lines too */
2733 if (oap->block_mode)
2734 {
2735# ifdef FEAT_VIRTUALEDIT
2736 /* Add spaces before getting the current line length. */
2737 if (virtual_op && (curwin->w_cursor.coladd > 0
2738 || gchar_cursor() == NUL))
2739 coladvance_force(getviscol());
2740# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002741 firstline = ml_get(oap->start.lnum);
2742 pre_textlen = (long)STRLEN(firstline);
2743 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 bd.textcol = curwin->w_cursor.col;
2745 }
2746#endif
2747
2748#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2749 if (oap->motion_type == MLINE)
2750 fix_indent();
2751#endif
2752
2753 retval = edit(NUL, FALSE, (linenr_T)1);
2754
2755#ifdef FEAT_VISUALEXTRA
2756 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002757 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002759 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002761 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002763 /* Auto-indenting may have changed the indent. If the cursor was past
2764 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002766 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002768 long new_indent = (long)(skipwhite(firstline) - firstline);
2769
2770 pre_textlen += new_indent - pre_indent;
2771 bd.textcol += new_indent - pre_indent;
2772 }
2773
2774 ins_len = (long)STRLEN(firstline) - pre_textlen;
2775 if (ins_len > 0)
2776 {
2777 /* Subsequent calls to ml_get() flush the firstline data - take a
2778 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2780 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002781 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2783 linenr++)
2784 {
2785 block_prep(oap, &bd, linenr, TRUE);
2786 if (!bd.is_short || virtual_op)
2787 {
2788# ifdef FEAT_VIRTUALEDIT
2789 pos_T vpos;
2790
2791 /* If the block starts in virtual space, count the
2792 * initial coladd offset as part of "startspaces" */
2793 if (bd.is_short)
2794 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002795 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 else
2799 vpos.coladd = 0;
2800# endif
2801 oldp = ml_get(linenr);
2802 newp = alloc_check((unsigned)(STRLEN(oldp)
2803# ifdef FEAT_VIRTUALEDIT
2804 + vpos.coladd
2805# endif
2806 + ins_len + 1));
2807 if (newp == NULL)
2808 continue;
2809 /* copy up to block start */
2810 mch_memmove(newp, oldp, (size_t)bd.textcol);
2811 offset = bd.textcol;
2812# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002813 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 offset += vpos.coladd;
2815# endif
2816 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2817 offset += ins_len;
2818 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002819 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 ml_replace(linenr, newp, FALSE);
2821 }
2822 }
2823 check_cursor();
2824
2825 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2826 }
2827 vim_free(ins_text);
2828 }
2829 }
2830#endif
2831
2832 return retval;
2833}
2834
2835/*
2836 * set all the yank registers to empty (called from main())
2837 */
2838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002839init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 int i;
2842
2843 for (i = 0; i < NUM_REGISTERS; ++i)
2844 y_regs[i].y_array = NULL;
2845}
2846
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002847#if defined(EXITFREE) || defined(PROTO)
2848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002849clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002850{
2851 int i;
2852
2853 for (i = 0; i < NUM_REGISTERS; ++i)
2854 {
2855 y_current = &y_regs[i];
2856 if (y_current->y_array != NULL)
2857 free_yank_all();
2858 }
2859}
2860#endif
2861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862/*
2863 * Free "n" lines from the current yank register.
2864 * Called for normal freeing and in case of error.
2865 */
2866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002867free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 if (y_current->y_array != NULL)
2870 {
2871 long i;
2872
2873 for (i = n; --i >= 0; )
2874 {
2875#ifdef AMIGA /* only for very slow machines */
2876 if ((i & 1023) == 1023) /* this may take a while */
2877 {
2878 /*
2879 * This message should never cause a hit-return message.
2880 * Overwrite this message with any next message.
2881 */
2882 ++no_wait_return;
2883 smsg((char_u *)_("freeing %ld lines"), i + 1);
2884 --no_wait_return;
2885 msg_didout = FALSE;
2886 msg_col = 0;
2887 }
2888#endif
2889 vim_free(y_current->y_array[i]);
2890 }
2891 vim_free(y_current->y_array);
2892 y_current->y_array = NULL;
2893#ifdef AMIGA
2894 if (n >= 1000)
2895 MSG("");
2896#endif
2897 }
2898}
2899
2900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002901free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 free_yank(y_current->y_size);
2904}
2905
2906/*
2907 * Yank the text between "oap->start" and "oap->end" into a yank register.
2908 * If we are to append (uppercase register), we first yank into a new yank
2909 * register and then concatenate the old and the new one (so we keep the old
2910 * one in case of out-of-memory).
2911 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002912 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 */
2914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002915op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002918 yankreg_T *curr; /* copy of y_current */
2919 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 char_u **new_ptr;
2921 linenr_T lnum; /* current line number */
2922 long j;
2923 int yanktype = oap->motion_type;
2924 long yanklines = oap->line_count;
2925 linenr_T yankendlnum = oap->end.lnum;
2926 char_u *p;
2927 char_u *pnew;
2928 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002929#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01002930 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01002931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 /* check for read-only register */
2934 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2935 {
2936 beep_flush();
2937 return FAIL;
2938 }
2939 if (oap->regname == '_') /* black hole: nothing to do */
2940 return OK;
2941
2942#ifdef FEAT_CLIPBOARD
2943 if (!clip_star.available && oap->regname == '*')
2944 oap->regname = 0;
2945 else if (!clip_plus.available && oap->regname == '+')
2946 oap->regname = 0;
2947#endif
2948
2949 if (!deleting) /* op_delete() already set y_current */
2950 get_yank_register(oap->regname, TRUE);
2951
2952 curr = y_current;
2953 /* append to existing contents */
2954 if (y_append && y_current->y_array != NULL)
2955 y_current = &newreg;
2956 else
2957 free_yank_all(); /* free previously yanked lines */
2958
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002959 /*
2960 * If the cursor was in column 1 before and after the movement, and the
2961 * operator is not inclusive, the yank is always linewise.
2962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if ( oap->motion_type == MCHAR
2964 && oap->start.col == 0
2965 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002967 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 && oap->end.col == 0
2969 && yanklines > 1)
2970 {
2971 yanktype = MLINE;
2972 --yankendlnum;
2973 --yanklines;
2974 }
2975
2976 y_current->y_size = yanklines;
2977 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2980 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (y_current->y_array == NULL)
2982 {
2983 y_current = curr;
2984 return FAIL;
2985 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002986#ifdef FEAT_VIMINFO
2987 y_current->y_time_set = vim_time();
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 y_idx = 0;
2991 lnum = oap->start.lnum;
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (oap->block_mode)
2994 {
2995 /* Visual block mode */
2996 y_current->y_type = MBLOCK; /* set the yank register type */
2997 y_current->y_width = oap->end_vcol - oap->start_vcol;
2998
2999 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3000 y_current->y_width--;
3001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3004 {
3005 switch (y_current->y_type)
3006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 case MBLOCK:
3008 block_prep(oap, &bd, lnum, FALSE);
3009 if (yank_copy_line(&bd, y_idx) == FAIL)
3010 goto fail;
3011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 case MLINE:
3014 if ((y_current->y_array[y_idx] =
3015 vim_strsave(ml_get(lnum))) == NULL)
3016 goto fail;
3017 break;
3018
3019 case MCHAR:
3020 {
3021 colnr_T startcol = 0, endcol = MAXCOL;
3022#ifdef FEAT_VIRTUALEDIT
3023 int is_oneChar = FALSE;
3024 colnr_T cs, ce;
3025#endif
3026 p = ml_get(lnum);
3027 bd.startspaces = 0;
3028 bd.endspaces = 0;
3029
3030 if (lnum == oap->start.lnum)
3031 {
3032 startcol = oap->start.col;
3033#ifdef FEAT_VIRTUALEDIT
3034 if (virtual_op)
3035 {
3036 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3037 if (ce != cs && oap->start.coladd > 0)
3038 {
3039 /* Part of a tab selected -- but don't
3040 * double-count it. */
3041 bd.startspaces = (ce - cs + 1)
3042 - oap->start.coladd;
3043 startcol++;
3044 }
3045 }
3046#endif
3047 }
3048
3049 if (lnum == oap->end.lnum)
3050 {
3051 endcol = oap->end.col;
3052#ifdef FEAT_VIRTUALEDIT
3053 if (virtual_op)
3054 {
3055 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3056 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3057# ifdef FEAT_MBYTE
3058 /* Don't add space for double-wide
3059 * char; endcol will be on last byte
3060 * of multi-byte char. */
3061 && (*mb_head_off)(p, p + endcol) == 0
3062# endif
3063 ))
3064 {
3065 if (oap->start.lnum == oap->end.lnum
3066 && oap->start.col == oap->end.col)
3067 {
3068 /* Special case: inside a single char */
3069 is_oneChar = TRUE;
3070 bd.startspaces = oap->end.coladd
3071 - oap->start.coladd + oap->inclusive;
3072 endcol = startcol;
3073 }
3074 else
3075 {
3076 bd.endspaces = oap->end.coladd
3077 + oap->inclusive;
3078 endcol -= oap->inclusive;
3079 }
3080 }
3081 }
3082#endif
3083 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003084 if (endcol == MAXCOL)
3085 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (startcol > endcol
3087#ifdef FEAT_VIRTUALEDIT
3088 || is_oneChar
3089#endif
3090 )
3091 bd.textlen = 0;
3092 else
3093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 bd.textlen = endcol - startcol + oap->inclusive;
3095 }
3096 bd.textstart = p + startcol;
3097 if (yank_copy_line(&bd, y_idx) == FAIL)
3098 goto fail;
3099 break;
3100 }
3101 /* NOTREACHED */
3102 }
3103 }
3104
3105 if (curr != y_current) /* append the new block to the old block */
3106 {
3107 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3108 (curr->y_size + y_current->y_size)), TRUE);
3109 if (new_ptr == NULL)
3110 goto fail;
3111 for (j = 0; j < curr->y_size; ++j)
3112 new_ptr[j] = curr->y_array[j];
3113 vim_free(curr->y_array);
3114 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003115#ifdef FEAT_VIMINFO
3116 curr->y_time_set = vim_time();
3117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3120 curr->y_type = MLINE;
3121
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003122 /* Concatenate the last line of the old block with the first line of
3123 * the new block, unless being Vi compatible. */
3124 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 {
3126 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3127 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3128 if (pnew == NULL)
3129 {
3130 y_idx = y_current->y_size - 1;
3131 goto fail;
3132 }
3133 STRCPY(pnew, curr->y_array[--j]);
3134 STRCAT(pnew, y_current->y_array[0]);
3135 vim_free(curr->y_array[j]);
3136 vim_free(y_current->y_array[0]);
3137 curr->y_array[j++] = pnew;
3138 y_idx = 1;
3139 }
3140 else
3141 y_idx = 0;
3142 while (y_idx < y_current->y_size)
3143 curr->y_array[j++] = y_current->y_array[y_idx++];
3144 curr->y_size = j;
3145 vim_free(y_current->y_array);
3146 y_current = curr;
3147 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003148 if (curwin->w_p_rnu)
3149 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (mess) /* Display message about yank? */
3151 {
3152 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 && yanklines == 1)
3155 yanklines = 0;
3156 /* Some versions of Vi use ">=" here, some don't... */
3157 if (yanklines > p_report)
3158 {
3159 /* redisplay now, so message is not deleted */
3160 update_topline_redraw();
3161 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003162 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003163 if (oap->block_mode)
3164 MSG(_("block of 1 line yanked"));
3165 else
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003166 MSG(_("1 line yanked"));
3167 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003168 else if (oap->block_mode)
3169 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else
3171 smsg((char_u *)_("%ld lines yanked"), yanklines);
3172 }
3173 }
3174
3175 /*
3176 * Set "'[" and "']" marks.
3177 */
3178 curbuf->b_op_start = oap->start;
3179 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003180 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003181 {
3182 curbuf->b_op_start.col = 0;
3183 curbuf->b_op_end.col = MAXCOL;
3184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186#ifdef FEAT_CLIPBOARD
3187 /*
3188 * If we were yanking to the '*' register, send result to clipboard.
3189 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3190 * to the '*' register.
3191 */
3192 if (clip_star.available
3193 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003194 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003195 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 if (curr != &(y_regs[STAR_REGISTER]))
3198 /* Copy the text from register 0 to the clipboard register. */
3199 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3200
3201 clip_own_selection(&clip_star);
3202 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003203# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003204 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003205# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207
3208# ifdef FEAT_X11
3209 /*
3210 * If we were yanking to the '+' register, send result to selection.
3211 * Also copy to the '*' register, in case auto-select is off.
3212 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003213 if (clip_plus.available
3214 && (curr == &(y_regs[PLUS_REGISTER])
3215 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003216 && ((clip_unnamed | clip_unnamed_saved) &
3217 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003219 if (curr != &(y_regs[PLUS_REGISTER]))
3220 /* Copy the text from register 0 to the clipboard register. */
3221 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 clip_own_selection(&clip_plus);
3224 clip_gen_set_selection(&clip_plus);
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003225 if (!clip_isautosel_star() && !did_star
3226 && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3229 clip_own_selection(&clip_star);
3230 clip_gen_set_selection(&clip_star);
3231 }
3232 }
3233# endif
3234#endif
3235
3236 return OK;
3237
3238fail: /* free the allocated lines */
3239 free_yank(y_idx + 1);
3240 y_current = curr;
3241 return FAIL;
3242}
3243
3244 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003245yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 char_u *pnew;
3248
3249 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3250 == NULL)
3251 return FAIL;
3252 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003253 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 pnew += bd->startspaces;
3255 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3256 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003257 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 pnew += bd->endspaces;
3259 *pnew = NUL;
3260 return OK;
3261}
3262
3263#ifdef FEAT_CLIPBOARD
3264/*
3265 * Make a copy of the y_current register to register "reg".
3266 */
3267 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003268copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003270 yankreg_T *curr = y_current;
3271 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
3273 y_current = reg;
3274 free_yank_all();
3275 *y_current = *curr;
3276 y_current->y_array = (char_u **)lalloc_clear(
3277 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3278 if (y_current->y_array == NULL)
3279 y_current->y_size = 0;
3280 else
3281 for (j = 0; j < y_current->y_size; ++j)
3282 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3283 {
3284 free_yank(j);
3285 y_current->y_size = 0;
3286 break;
3287 }
3288 y_current = curr;
3289}
3290#endif
3291
3292/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003293 * Put contents of register "regname" into the text.
3294 * Caller must check "regname" to be valid!
3295 * "flags": PUT_FIXINDENT make indent look nice
3296 * PUT_CURSEND leave cursor after end of new text
3297 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300do_put(
3301 int regname,
3302 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3303 long count,
3304 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 char_u *ptr;
3307 char_u *newp, *oldp;
3308 int yanklen;
3309 int totlen = 0; /* init for gcc */
3310 linenr_T lnum;
3311 colnr_T col;
3312 long i; /* index in y_array[] */
3313 int y_type;
3314 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 int oldlen;
3316 long y_width = 0;
3317 colnr_T vcol;
3318 int delcount;
3319 int incr = 0;
3320 long j;
3321 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 char_u **y_array = NULL;
3323 long nr_lines = 0;
3324 pos_T new_cursor;
3325 int indent;
3326 int orig_indent = 0; /* init for gcc */
3327 int indent_diff = 0; /* init for gcc */
3328 int first_indent = TRUE;
3329 int lendiff = 0;
3330 pos_T old_pos;
3331 char_u *insert_string = NULL;
3332 int allocated = FALSE;
3333 long cnt;
3334
3335#ifdef FEAT_CLIPBOARD
3336 /* Adjust register name for "unnamed" in 'clipboard'. */
3337 adjust_clip_reg(&regname);
3338 (void)may_get_selection(regname);
3339#endif
3340
3341 if (flags & PUT_FIXINDENT)
3342 orig_indent = get_indent();
3343
3344 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3345 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3346
3347 /*
3348 * Using inserted text works differently, because the register includes
3349 * special characters (newlines, etc.).
3350 */
3351 if (regname == '.')
3352 {
3353 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3354 (count == -1 ? 'O' : 'i')), count, FALSE);
3355 /* Putting the text is done later, so can't really move the cursor to
3356 * the next character. Use "l" to simulate it. */
3357 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3358 stuffcharReadbuff('l');
3359 return;
3360 }
3361
3362 /*
3363 * For special registers '%' (file name), '#' (alternate file name) and
3364 * ':' (last command line), etc. we have to create a fake yank register.
3365 */
3366 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3367 {
3368 if (insert_string == NULL)
3369 return;
3370 }
3371
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003372#ifdef FEAT_AUTOCMD
3373 /* Autocommands may be executed when saving lines for undo, which may make
3374 * y_array invalid. Start undo now to avoid that. */
3375 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
3376#endif
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (insert_string != NULL)
3379 {
3380 y_type = MCHAR;
3381#ifdef FEAT_EVAL
3382 if (regname == '=')
3383 {
3384 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003385 * characters.
3386 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 for (;;)
3388 {
3389 y_size = 0;
3390 ptr = insert_string;
3391 while (ptr != NULL)
3392 {
3393 if (y_array != NULL)
3394 y_array[y_size] = ptr;
3395 ++y_size;
3396 ptr = vim_strchr(ptr, '\n');
3397 if (ptr != NULL)
3398 {
3399 if (y_array != NULL)
3400 *ptr = NUL;
3401 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003402 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (*ptr == NUL)
3404 {
3405 y_type = MLINE;
3406 break;
3407 }
3408 }
3409 }
3410 if (y_array != NULL)
3411 break;
3412 y_array = (char_u **)alloc((unsigned)
3413 (y_size * sizeof(char_u *)));
3414 if (y_array == NULL)
3415 goto end;
3416 }
3417 }
3418 else
3419#endif
3420 {
3421 y_size = 1; /* use fake one-line yank register */
3422 y_array = &insert_string;
3423 }
3424 }
3425 else
3426 {
3427 get_yank_register(regname, FALSE);
3428
3429 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 y_size = y_current->y_size;
3432 y_array = y_current->y_array;
3433 }
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (y_type == MLINE)
3436 {
3437 if (flags & PUT_LINE_SPLIT)
3438 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003439 char_u *p;
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /* "p" or "P" in Visual mode: split the lines to put the text in
3442 * between. */
3443 if (u_save_cursor() == FAIL)
3444 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003445 p = ml_get_cursor();
3446 if (dir == FORWARD && *p != NUL)
3447 mb_ptr_adv(p);
3448 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (ptr == NULL)
3450 goto end;
3451 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3452 vim_free(ptr);
3453
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003454 oldp = ml_get_curline();
3455 p = oldp + curwin->w_cursor.col;
3456 if (dir == FORWARD && *p != NUL)
3457 mb_ptr_adv(p);
3458 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (ptr == NULL)
3460 goto end;
3461 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3462 ++nr_lines;
3463 dir = FORWARD;
3464 }
3465 if (flags & PUT_LINE_FORWARD)
3466 {
3467 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003468 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 dir = FORWARD;
3470 }
3471 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3472 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3476 y_type = MLINE;
3477
3478 if (y_size == 0 || y_array == NULL)
3479 {
3480 EMSG2(_("E353: Nothing in register %s"),
3481 regname == 0 ? (char_u *)"\"" : transchar(regname));
3482 goto end;
3483 }
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (y_type == MBLOCK)
3486 {
3487 lnum = curwin->w_cursor.lnum + y_size + 1;
3488 if (lnum > curbuf->b_ml.ml_line_count)
3489 lnum = curbuf->b_ml.ml_line_count + 1;
3490 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3491 goto end;
3492 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003493 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 lnum = curwin->w_cursor.lnum;
3496#ifdef FEAT_FOLDING
3497 /* Correct line number for closed fold. Don't move the cursor yet,
3498 * u_save() uses it. */
3499 if (dir == BACKWARD)
3500 (void)hasFolding(lnum, &lnum, NULL);
3501 else
3502 (void)hasFolding(lnum, NULL, &lnum);
3503#endif
3504 if (dir == FORWARD)
3505 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003506 /* In an empty buffer the empty line is going to be replaced, include
3507 * it in the saved lines. */
Bram Moolenaarcaf2dff2013-07-03 14:19:54 +02003508 if ((bufempty() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 goto end;
3510#ifdef FEAT_FOLDING
3511 if (dir == FORWARD)
3512 curwin->w_cursor.lnum = lnum - 1;
3513 else
3514 curwin->w_cursor.lnum = lnum;
3515 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3516#endif
3517 }
3518 else if (u_save_cursor() == FAIL)
3519 goto end;
3520
3521 yanklen = (int)STRLEN(y_array[0]);
3522
3523#ifdef FEAT_VIRTUALEDIT
3524 if (ve_flags == VE_ALL && y_type == MCHAR)
3525 {
3526 if (gchar_cursor() == TAB)
3527 {
3528 /* Don't need to insert spaces when "p" on the last position of a
3529 * tab or "P" on the first position. */
3530 if (dir == FORWARD
3531 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3532 : curwin->w_cursor.coladd > 0)
3533 coladvance_force(getviscol());
3534 else
3535 curwin->w_cursor.coladd = 0;
3536 }
3537 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3538 coladvance_force(getviscol() + (dir == FORWARD));
3539 }
3540#endif
3541
3542 lnum = curwin->w_cursor.lnum;
3543 col = curwin->w_cursor.col;
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /*
3546 * Block mode
3547 */
3548 if (y_type == MBLOCK)
3549 {
3550 char c = gchar_cursor();
3551 colnr_T endcol2 = 0;
3552
3553 if (dir == FORWARD && c != NUL)
3554 {
3555#ifdef FEAT_VIRTUALEDIT
3556 if (ve_flags == VE_ALL)
3557 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3558 else
3559#endif
3560 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3561
3562#ifdef FEAT_MBYTE
3563 if (has_mbyte)
3564 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003565 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else
3567#endif
3568#ifdef FEAT_VIRTUALEDIT
3569 if (c != TAB || ve_flags != VE_ALL)
3570#endif
3571 ++curwin->w_cursor.col;
3572 ++col;
3573 }
3574 else
3575 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3576
3577#ifdef FEAT_VIRTUALEDIT
3578 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003579 if (ve_flags == VE_ALL
3580 && (curwin->w_cursor.coladd > 0
3581 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 if (dir == FORWARD && c == NUL)
3584 ++col;
3585 if (dir != FORWARD && c != NUL)
3586 ++curwin->w_cursor.col;
3587 if (c == TAB)
3588 {
3589 if (dir == BACKWARD && curwin->w_cursor.col)
3590 curwin->w_cursor.col--;
3591 if (dir == FORWARD && col - 1 == endcol2)
3592 curwin->w_cursor.col++;
3593 }
3594 }
3595 curwin->w_cursor.coladd = 0;
3596#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003597 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 for (i = 0; i < y_size; ++i)
3599 {
3600 int spaces;
3601 char shortline;
3602
3603 bd.startspaces = 0;
3604 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 vcol = 0;
3606 delcount = 0;
3607
3608 /* add a new line */
3609 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3610 {
3611 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3612 (colnr_T)1, FALSE) == FAIL)
3613 break;
3614 ++nr_lines;
3615 }
3616 /* get the old line and advance to the position to insert at */
3617 oldp = ml_get_curline();
3618 oldlen = (int)STRLEN(oldp);
3619 for (ptr = oldp; vcol < col && *ptr; )
3620 {
3621 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003622 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 vcol += incr;
3624 }
3625 bd.textcol = (colnr_T)(ptr - oldp);
3626
3627 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3628
3629 if (vcol < col) /* line too short, padd with spaces */
3630 bd.startspaces = col - vcol;
3631 else if (vcol > col)
3632 {
3633 bd.endspaces = vcol - col;
3634 bd.startspaces = incr - bd.endspaces;
3635 --bd.textcol;
3636 delcount = 1;
3637#ifdef FEAT_MBYTE
3638 if (has_mbyte)
3639 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3640#endif
3641 if (oldp[bd.textcol] != TAB)
3642 {
3643 /* Only a Tab can be split into spaces. Other
3644 * characters will have to be moved to after the
3645 * block, causing misalignment. */
3646 delcount = 0;
3647 bd.endspaces = 0;
3648 }
3649 }
3650
3651 yanklen = (int)STRLEN(y_array[i]);
3652
3653 /* calculate number of spaces required to fill right side of block*/
3654 spaces = y_width + 1;
3655 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003656 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (spaces < 0)
3658 spaces = 0;
3659
3660 /* insert the new text */
3661 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3662 newp = alloc_check((unsigned)totlen + oldlen + 1);
3663 if (newp == NULL)
3664 break;
3665 /* copy part up to cursor to new line */
3666 ptr = newp;
3667 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3668 ptr += bd.textcol;
3669 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003670 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 ptr += bd.startspaces;
3672 /* insert the new text */
3673 for (j = 0; j < count; ++j)
3674 {
3675 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3676 ptr += yanklen;
3677
3678 /* insert block's trailing spaces only if there's text behind */
3679 if ((j < count - 1 || !shortline) && spaces)
3680 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003681 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 ptr += spaces;
3683 }
3684 }
3685 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003686 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 ptr += bd.endspaces;
3688 /* move the text after the cursor to the end of the line. */
3689 mch_memmove(ptr, oldp + bd.textcol + delcount,
3690 (size_t)(oldlen - bd.textcol - delcount + 1));
3691 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3692
3693 ++curwin->w_cursor.lnum;
3694 if (i == 0)
3695 curwin->w_cursor.col += bd.startspaces;
3696 }
3697
3698 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3699
3700 /* Set '[ mark. */
3701 curbuf->b_op_start = curwin->w_cursor;
3702 curbuf->b_op_start.lnum = lnum;
3703
3704 /* adjust '] mark */
3705 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3706 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003707# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (flags & PUT_CURSEND)
3711 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003712 colnr_T len;
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 curwin->w_cursor = curbuf->b_op_end;
3715 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003716
3717 /* in Insert mode we might be after the NUL, correct for that */
3718 len = (colnr_T)STRLEN(ml_get_curline());
3719 if (curwin->w_cursor.col > len)
3720 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else
3723 curwin->w_cursor.lnum = lnum;
3724 }
3725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
3727 /*
3728 * Character or Line mode
3729 */
3730 if (y_type == MCHAR)
3731 {
3732 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3733 * char */
3734 if (dir == FORWARD && gchar_cursor() != NUL)
3735 {
3736#ifdef FEAT_MBYTE
3737 if (has_mbyte)
3738 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003739 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 /* put it on the next of the multi-byte character. */
3742 col += bytelen;
3743 if (yanklen)
3744 {
3745 curwin->w_cursor.col += bytelen;
3746 curbuf->b_op_end.col += bytelen;
3747 }
3748 }
3749 else
3750#endif
3751 {
3752 ++col;
3753 if (yanklen)
3754 {
3755 ++curwin->w_cursor.col;
3756 ++curbuf->b_op_end.col;
3757 }
3758 }
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 curbuf->b_op_start = curwin->w_cursor;
3761 }
3762 /*
3763 * Line mode: BACKWARD is the same as FORWARD on the previous line
3764 */
3765 else if (dir == BACKWARD)
3766 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003767 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 /*
3770 * simple case: insert into current line
3771 */
3772 if (y_type == MCHAR && y_size == 1)
3773 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003774 do {
3775 totlen = count * yanklen;
3776 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003778 oldp = ml_get(lnum);
3779 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3780 if (newp == NULL)
3781 goto end; /* alloc() gave an error message */
3782 mch_memmove(newp, oldp, (size_t)col);
3783 ptr = newp + col;
3784 for (i = 0; i < count; ++i)
3785 {
3786 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3787 ptr += yanklen;
3788 }
3789 STRMOVE(ptr, oldp + col);
3790 ml_replace(lnum, newp, FALSE);
3791 /* Place cursor on last putted char. */
3792 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003793 {
3794 /* make sure curwin->w_virtcol is updated */
3795 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003796 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003799 if (VIsual_active)
3800 lnum++;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003801 } while (VIsual_active && lnum <= curbuf->b_visual.vi_end.lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003802
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003803 if (VIsual_active) /* reset lnum to the last visual line */
3804 lnum--;
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 curbuf->b_op_end = curwin->w_cursor;
3807 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3808 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3809 ++curwin->w_cursor.col;
3810 changed_bytes(lnum, col);
3811 }
3812 else
3813 {
3814 /*
3815 * Insert at least one line. When y_type is MCHAR, break the first
3816 * line in two.
3817 */
3818 for (cnt = 1; cnt <= count; ++cnt)
3819 {
3820 i = 0;
3821 if (y_type == MCHAR)
3822 {
3823 /*
3824 * Split the current line in two at the insert position.
3825 * First insert y_array[size - 1] in front of second line.
3826 * Then append y_array[0] to first line.
3827 */
3828 lnum = new_cursor.lnum;
3829 ptr = ml_get(lnum) + col;
3830 totlen = (int)STRLEN(y_array[y_size - 1]);
3831 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3832 if (newp == NULL)
3833 goto error;
3834 STRCPY(newp, y_array[y_size - 1]);
3835 STRCAT(newp, ptr);
3836 /* insert second line */
3837 ml_append(lnum, newp, (colnr_T)0, FALSE);
3838 vim_free(newp);
3839
3840 oldp = ml_get(lnum);
3841 newp = alloc_check((unsigned)(col + yanklen + 1));
3842 if (newp == NULL)
3843 goto error;
3844 /* copy first part of line */
3845 mch_memmove(newp, oldp, (size_t)col);
3846 /* append to first line */
3847 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3848 ml_replace(lnum, newp, FALSE);
3849
3850 curwin->w_cursor.lnum = lnum;
3851 i = 1;
3852 }
3853
3854 for (; i < y_size; ++i)
3855 {
3856 if ((y_type != MCHAR || i < y_size - 1)
3857 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3858 == FAIL)
3859 goto error;
3860 lnum++;
3861 ++nr_lines;
3862 if (flags & PUT_FIXINDENT)
3863 {
3864 old_pos = curwin->w_cursor;
3865 curwin->w_cursor.lnum = lnum;
3866 ptr = ml_get(lnum);
3867 if (cnt == count && i == y_size - 1)
3868 lendiff = (int)STRLEN(ptr);
3869#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3870 if (*ptr == '#' && preprocs_left())
3871 indent = 0; /* Leave # lines at start */
3872 else
3873#endif
3874 if (*ptr == NUL)
3875 indent = 0; /* Ignore empty lines */
3876 else if (first_indent)
3877 {
3878 indent_diff = orig_indent - get_indent();
3879 indent = orig_indent;
3880 first_indent = FALSE;
3881 }
3882 else if ((indent = get_indent() + indent_diff) < 0)
3883 indent = 0;
3884 (void)set_indent(indent, 0);
3885 curwin->w_cursor = old_pos;
3886 /* remember how many chars were removed */
3887 if (cnt == count && i == y_size - 1)
3888 lendiff -= (int)STRLEN(ml_get(lnum));
3889 }
3890 }
3891 }
3892
3893error:
3894 /* Adjust marks. */
3895 if (y_type == MLINE)
3896 {
3897 curbuf->b_op_start.col = 0;
3898 if (dir == FORWARD)
3899 curbuf->b_op_start.lnum++;
3900 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003901 /* Skip mark_adjust when adding lines after the last one, there
3902 * can't be marks there. */
3903 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
3904 < curbuf->b_ml.ml_line_count)
3905 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 (linenr_T)MAXLNUM, nr_lines, 0L);
3907
3908 /* note changed text for displaying and folding */
3909 if (y_type == MCHAR)
3910 changed_lines(curwin->w_cursor.lnum, col,
3911 curwin->w_cursor.lnum + 1, nr_lines);
3912 else
3913 changed_lines(curbuf->b_op_start.lnum, 0,
3914 curbuf->b_op_start.lnum, nr_lines);
3915
3916 /* put '] mark at last inserted character */
3917 curbuf->b_op_end.lnum = lnum;
3918 /* correct length for change in indent */
3919 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3920 if (col > 1)
3921 curbuf->b_op_end.col = col - 1;
3922 else
3923 curbuf->b_op_end.col = 0;
3924
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003925 if (flags & PUT_CURSLINE)
3926 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003927 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003928 curwin->w_cursor.lnum = lnum;
3929 beginline(BL_WHITE | BL_FIX);
3930 }
3931 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 /* put cursor after inserted text */
3934 if (y_type == MLINE)
3935 {
3936 if (lnum >= curbuf->b_ml.ml_line_count)
3937 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3938 else
3939 curwin->w_cursor.lnum = lnum + 1;
3940 curwin->w_cursor.col = 0;
3941 }
3942 else
3943 {
3944 curwin->w_cursor.lnum = lnum;
3945 curwin->w_cursor.col = col;
3946 }
3947 }
3948 else if (y_type == MLINE)
3949 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003950 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 curwin->w_cursor.col = 0;
3952 if (dir == FORWARD)
3953 ++curwin->w_cursor.lnum;
3954 beginline(BL_WHITE | BL_FIX);
3955 }
3956 else /* put cursor on first inserted character */
3957 curwin->w_cursor = new_cursor;
3958 }
3959 }
3960
3961 msgmore(nr_lines);
3962 curwin->w_set_curswant = TRUE;
3963
3964end:
3965 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003967 if (regname == '=')
3968 vim_free(y_array);
3969
Bram Moolenaar033d8882013-09-25 23:24:57 +02003970 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02003971
Bram Moolenaar677ee682005-01-27 14:41:15 +00003972 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003973 adjust_cursor_eol();
3974}
3975
3976/*
3977 * When the cursor is on the NUL past the end of the line and it should not be
3978 * there move it left.
3979 */
3980 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003982{
3983 if (curwin->w_cursor.col > 0
3984 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003985#ifdef FEAT_VIRTUALEDIT
3986 && (ve_flags & VE_ONEMORE) == 0
3987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 && !(restart_edit || (State & INSERT)))
3989 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003990 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003991 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993#ifdef FEAT_VIRTUALEDIT
3994 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003995 {
3996 colnr_T scol, ecol;
3997
3998 /* Coladd is set to the width of the last character. */
3999 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4000 curwin->w_cursor.coladd = ecol - scol + 1;
4001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#endif
4003 }
4004}
4005
4006#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4007/*
4008 * Return TRUE if lines starting with '#' should be left aligned.
4009 */
4010 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004011preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012{
4013 return
4014# ifdef FEAT_SMARTINDENT
4015# ifdef FEAT_CINDENT
4016 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4017# else
4018 curbuf->b_p_si
4019# endif
4020# endif
4021# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004022 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4023 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024# endif
4025 ;
4026}
4027#endif
4028
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004029/*
4030 * Return the character name of the register with the given number.
4031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004033get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 if (num == -1)
4036 return '"';
4037 else if (num < 10)
4038 return num + '0';
4039 else if (num == DELETION_REGISTER)
4040 return '-';
4041#ifdef FEAT_CLIPBOARD
4042 else if (num == STAR_REGISTER)
4043 return '*';
4044 else if (num == PLUS_REGISTER)
4045 return '+';
4046#endif
4047 else
4048 {
4049#ifdef EBCDIC
4050 int i;
4051
4052 /* EBCDIC is really braindead ... */
4053 i = 'a' + (num - 10);
4054 if (i > 'i')
4055 i += 7;
4056 if (i > 'r')
4057 i += 8;
4058 return i;
4059#else
4060 return num + 'a' - 10;
4061#endif
4062 }
4063}
4064
4065/*
4066 * ":dis" and ":registers": Display the contents of the yank registers.
4067 */
4068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004071 int i, n;
4072 long j;
4073 char_u *p;
4074 yankreg_T *yb;
4075 int name;
4076 int attr;
4077 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004078#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004079 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004080#else
4081# define clen 1
4082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (arg != NULL && *arg == NUL)
4085 arg = NULL;
4086 attr = hl_attr(HLF_8);
4087
4088 /* Highlight title */
4089 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4090 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4091 {
4092 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004093 if (arg != NULL && vim_strchr(arg, name) == NULL
4094#ifdef ONE_CLIPBOARD
4095 /* Star register and plus register contain the same thing. */
4096 && (name != '*' || vim_strchr(arg, '+') == NULL)
4097#endif
4098 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 continue; /* did not ask for this register */
4100
4101#ifdef FEAT_CLIPBOARD
4102 /* Adjust register name for "unnamed" in 'clipboard'.
4103 * When it's a clipboard register, fill it with the current contents
4104 * of the clipboard. */
4105 adjust_clip_reg(&name);
4106 (void)may_get_selection(name);
4107#endif
4108
4109 if (i == -1)
4110 {
4111 if (y_previous != NULL)
4112 yb = y_previous;
4113 else
4114 yb = &(y_regs[0]);
4115 }
4116 else
4117 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004118
4119#ifdef FEAT_EVAL
4120 if (name == MB_TOLOWER(redir_reg)
4121 || (redir_reg == '"' && yb == y_previous))
4122 continue; /* do not list register being written to, the
4123 * pointer can be freed */
4124#endif
4125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (yb->y_array != NULL)
4127 {
4128 msg_putchar('\n');
4129 msg_putchar('"');
4130 msg_putchar(name);
4131 MSG_PUTS(" ");
4132
4133 n = (int)Columns - 6;
4134 for (j = 0; j < yb->y_size && n > 1; ++j)
4135 {
4136 if (j)
4137 {
4138 MSG_PUTS_ATTR("^J", attr);
4139 n -= 2;
4140 }
4141 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004144 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004145#endif
4146 msg_outtrans_len(p, clen);
4147#ifdef FEAT_MBYTE
4148 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#endif
4150 }
4151 }
4152 if (n > 1 && yb->y_type == MLINE)
4153 MSG_PUTS_ATTR("^J", attr);
4154 out_flush(); /* show one line at a time */
4155 }
4156 ui_breakcheck();
4157 }
4158
4159 /*
4160 * display last inserted text
4161 */
4162 if ((p = get_last_insert()) != NULL
4163 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4164 {
4165 MSG_PUTS("\n\". ");
4166 dis_msg(p, TRUE);
4167 }
4168
4169 /*
4170 * display last command line
4171 */
4172 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4173 && !got_int)
4174 {
4175 MSG_PUTS("\n\": ");
4176 dis_msg(last_cmdline, FALSE);
4177 }
4178
4179 /*
4180 * display current file name
4181 */
4182 if (curbuf->b_fname != NULL
4183 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4184 {
4185 MSG_PUTS("\n\"% ");
4186 dis_msg(curbuf->b_fname, FALSE);
4187 }
4188
4189 /*
4190 * display alternate file name
4191 */
4192 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4193 {
4194 char_u *fname;
4195 linenr_T dummy;
4196
4197 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4198 {
4199 MSG_PUTS("\n\"# ");
4200 dis_msg(fname, FALSE);
4201 }
4202 }
4203
4204 /*
4205 * display last search pattern
4206 */
4207 if (last_search_pat() != NULL
4208 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4209 {
4210 MSG_PUTS("\n\"/ ");
4211 dis_msg(last_search_pat(), FALSE);
4212 }
4213
4214#ifdef FEAT_EVAL
4215 /*
4216 * display last used expression
4217 */
4218 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4219 && !got_int)
4220 {
4221 MSG_PUTS("\n\"= ");
4222 dis_msg(expr_line, FALSE);
4223 }
4224#endif
4225}
4226
4227/*
4228 * display a string for do_dis()
4229 * truncate at end of screen line
4230 */
4231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004232dis_msg(
4233 char_u *p,
4234 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int n;
4237#ifdef FEAT_MBYTE
4238 int l;
4239#endif
4240
4241 n = (int)Columns - 6;
4242 while (*p != NUL
4243 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4244 && (n -= ptr2cells(p)) >= 0)
4245 {
4246#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004247 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 msg_outtrans_len(p, l);
4250 p += l;
4251 }
4252 else
4253#endif
4254 msg_outtrans_len(p++, 1);
4255 }
4256 ui_breakcheck();
4257}
4258
Bram Moolenaar81340392012-06-06 16:12:59 +02004259#if defined(FEAT_COMMENTS) || defined(PROTO)
4260/*
4261 * If "process" is TRUE and the line begins with a comment leader (possibly
4262 * after some white space), return a pointer to the text after it. Put a boolean
4263 * value indicating whether the line ends with an unclosed comment in
4264 * "is_comment".
4265 * line - line to be processed,
4266 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004267 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004268 * include_space - whether to also skip space following the comment leader,
4269 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004270 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004271 */
4272 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004273skip_comment(
4274 char_u *line,
4275 int process,
4276 int include_space,
4277 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004278{
4279 char_u *comment_flags = NULL;
4280 int lead_len;
4281 int leader_offset = get_last_leader_offset(line, &comment_flags);
4282
4283 *is_comment = FALSE;
4284 if (leader_offset != -1)
4285 {
4286 /* Let's check whether the line ends with an unclosed comment.
4287 * If the last comment leader has COM_END in flags, there's no comment.
4288 */
4289 while (*comment_flags)
4290 {
4291 if (*comment_flags == COM_END
4292 || *comment_flags == ':')
4293 break;
4294 ++comment_flags;
4295 }
4296 if (*comment_flags != COM_END)
4297 *is_comment = TRUE;
4298 }
4299
4300 if (process == FALSE)
4301 return line;
4302
4303 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4304
4305 if (lead_len == 0)
4306 return line;
4307
4308 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004309 * - COM_END,
4310 * - colon,
4311 * whichever comes first.
4312 */
4313 while (*comment_flags)
4314 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004315 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004316 || *comment_flags == ':')
4317 {
4318 break;
4319 }
4320 ++comment_flags;
4321 }
4322
4323 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004324 * starting with a closing part of a three-part comment. That's good,
4325 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004326 */
4327 if (*comment_flags == ':' || *comment_flags == NUL)
4328 line += lead_len;
4329
4330 return line;
4331}
4332#endif
4333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004335 * Join 'count' lines (minimal 2) at cursor position.
4336 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004337 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4338 * leaders should not be removed.
4339 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4340 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004342 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004345do_join(
4346 long count,
4347 int insert_space,
4348 int save_undo,
4349 int use_formatoptions UNUSED,
4350 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004352 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004353 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004354 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004356 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004357 int endcurr1 = NUL;
4358 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004359 int currsize = 0; /* size of the current line */
4360 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004362 colnr_T col = 0;
4363 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004364#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004365 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004366 int remove_comments = (use_formatoptions == TRUE)
4367 && has_format_option(FO_REMOVE_COMS);
4368 int prev_was_comment;
4369#endif
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004372 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4373 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004376 /* Allocate an array to store the number of spaces inserted before each
4377 * line. We will use it to pre-compute the length of the new line and the
4378 * proper placement of each original line in the new one. */
4379 spaces = lalloc_clear((long_u)count, TRUE);
4380 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004382#if defined(FEAT_COMMENTS) || defined(PROTO)
4383 if (remove_comments)
4384 {
4385 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4386 if (comments == NULL)
4387 {
4388 vim_free(spaces);
4389 return FAIL;
4390 }
4391 }
4392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004395 * Don't move anything, just compute the final line length
4396 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004398 for (t = 0; t < count; ++t)
4399 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004400 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004401 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004402 {
4403 /* Set the '[ mark. */
4404 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4405 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4406 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004407#if defined(FEAT_COMMENTS) || defined(PROTO)
4408 if (remove_comments)
4409 {
4410 /* We don't want to remove the comment leader if the
4411 * previous line is not a comment. */
4412 if (t > 0 && prev_was_comment)
4413 {
4414
4415 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4416 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004417 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004418 curr = new_curr;
4419 }
4420 else
4421 curr = skip_comment(curr, FALSE, insert_space,
4422 &prev_was_comment);
4423 }
4424#endif
4425
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004426 if (insert_space && t > 0)
4427 {
4428 curr = skipwhite(curr);
4429 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4430#ifdef FEAT_MBYTE
4431 && (!has_format_option(FO_MBYTE_JOIN)
4432 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4433 && (!has_format_option(FO_MBYTE_JOIN2)
4434 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4435#endif
4436 )
4437 {
4438 /* don't add a space if the line is ending in a space */
4439 if (endcurr1 == ' ')
4440 endcurr1 = endcurr2;
4441 else
4442 ++spaces[t];
4443 /* extra space when 'joinspaces' set and line ends in '.' */
4444 if ( p_js
4445 && (endcurr1 == '.'
4446 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4447 && (endcurr1 == '?' || endcurr1 == '!'))))
4448 ++spaces[t];
4449 }
4450 }
4451 currsize = (int)STRLEN(curr);
4452 sumsize += currsize + spaces[t];
4453 endcurr1 = endcurr2 = NUL;
4454 if (insert_space && currsize > 0)
4455 {
4456#ifdef FEAT_MBYTE
4457 if (has_mbyte)
4458 {
4459 cend = curr + currsize;
4460 mb_ptr_back(curr, cend);
4461 endcurr1 = (*mb_ptr2char)(cend);
4462 if (cend > curr)
4463 {
4464 mb_ptr_back(curr, cend);
4465 endcurr2 = (*mb_ptr2char)(cend);
4466 }
4467 }
4468 else
4469#endif
4470 {
4471 endcurr1 = *(curr + currsize - 1);
4472 if (currsize > 1)
4473 endcurr2 = *(curr + currsize - 2);
4474 }
4475 }
4476 line_breakcheck();
4477 if (got_int)
4478 {
4479 ret = FAIL;
4480 goto theend;
4481 }
4482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004484 /* store the column position before last line */
4485 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004487 /* allocate the space for the new line */
4488 newp = alloc_check((unsigned)(sumsize + 1));
4489 cend = newp + sumsize;
4490 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004492 /*
4493 * Move affected lines to the new long one.
4494 *
4495 * Move marks from each deleted line to the joined line, adjusting the
4496 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4497 * should not really be a problem.
4498 */
4499 for (t = count - 1; ; --t)
4500 {
4501 cend -= currsize;
4502 mch_memmove(cend, curr, (size_t)currsize);
4503 if (spaces[t] > 0)
4504 {
4505 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004506 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004507 }
4508 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004509 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004510 if (t == 0)
4511 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004512 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004513#if defined(FEAT_COMMENTS) || defined(PROTO)
4514 if (remove_comments)
4515 curr += comments[t - 1];
4516#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004517 if (insert_space && t > 1)
4518 curr = skipwhite(curr);
4519 currsize = (int)STRLEN(curr);
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4522
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004523 if (setmark)
4524 {
4525 /* Set the '] mark. */
4526 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4527 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4528 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /* Only report the change in the first line here, del_lines() will report
4531 * the deleted line. */
4532 changed_lines(curwin->w_cursor.lnum, currsize,
4533 curwin->w_cursor.lnum + 1, 0L);
4534
4535 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004536 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * briefly, and then move it back. After del_lines() the cursor may
4538 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
4540 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004542 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 curwin->w_cursor.lnum = t;
4544
4545 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004546 * Set the cursor column:
4547 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004548 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550 curwin->w_cursor.col =
4551 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#ifdef FEAT_VIRTUALEDIT
4555 curwin->w_cursor.coladd = 0;
4556#endif
4557 curwin->w_set_curswant = TRUE;
4558
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004559theend:
4560 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004561#if defined(FEAT_COMMENTS) || defined(PROTO)
4562 if (remove_comments)
4563 vim_free(comments);
4564#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004565 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566}
4567
4568#ifdef FEAT_COMMENTS
4569/*
4570 * Return TRUE if the two comment leaders given are the same. "lnum" is
4571 * the first line. White-space is ignored. Note that the whole of
4572 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4573 */
4574 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004575same_leader(
4576 linenr_T lnum,
4577 int leader1_len,
4578 char_u *leader1_flags,
4579 int leader2_len,
4580 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581{
4582 int idx1 = 0, idx2 = 0;
4583 char_u *p;
4584 char_u *line1;
4585 char_u *line2;
4586
4587 if (leader1_len == 0)
4588 return (leader2_len == 0);
4589
4590 /*
4591 * If first leader has 'f' flag, the lines can be joined only if the
4592 * second line does not have a leader.
4593 * If first leader has 'e' flag, the lines can never be joined.
4594 * If fist leader has 's' flag, the lines can only be joined if there is
4595 * some text after it and the second line has the 'm' flag.
4596 */
4597 if (leader1_flags != NULL)
4598 {
4599 for (p = leader1_flags; *p && *p != ':'; ++p)
4600 {
4601 if (*p == COM_FIRST)
4602 return (leader2_len == 0);
4603 if (*p == COM_END)
4604 return FALSE;
4605 if (*p == COM_START)
4606 {
4607 if (*(ml_get(lnum) + leader1_len) == NUL)
4608 return FALSE;
4609 if (leader2_flags == NULL || leader2_len == 0)
4610 return FALSE;
4611 for (p = leader2_flags; *p && *p != ':'; ++p)
4612 if (*p == COM_MIDDLE)
4613 return TRUE;
4614 return FALSE;
4615 }
4616 }
4617 }
4618
4619 /*
4620 * Get current line and next line, compare the leaders.
4621 * The first line has to be saved, only one line can be locked at a time.
4622 */
4623 line1 = vim_strsave(ml_get(lnum));
4624 if (line1 != NULL)
4625 {
4626 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4627 ;
4628 line2 = ml_get(lnum + 1);
4629 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4630 {
4631 if (!vim_iswhite(line2[idx2]))
4632 {
4633 if (line1[idx1++] != line2[idx2])
4634 break;
4635 }
4636 else
4637 while (vim_iswhite(line1[idx1]))
4638 ++idx1;
4639 }
4640 vim_free(line1);
4641 }
4642 return (idx2 == leader2_len && idx1 == leader1_len);
4643}
4644#endif
4645
4646/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004647 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 */
4649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004650op_format(
4651 oparg_T *oap,
4652 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 long old_line_count = curbuf->b_ml.ml_line_count;
4655
4656 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4657 * can put it back there. */
4658 curwin->w_cursor = oap->cursor_start;
4659
4660 if (u_save((linenr_T)(oap->start.lnum - 1),
4661 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4662 return;
4663 curwin->w_cursor = oap->start;
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (oap->is_VIsual)
4666 /* When there is no change: need to remove the Visual selection */
4667 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /* Set '[ mark at the start of the formatted area */
4670 curbuf->b_op_start = oap->start;
4671
4672 /* For "gw" remember the cursor position and put it back below (adjusted
4673 * for joined and split lines). */
4674 if (keep_cursor)
4675 saved_cursor = oap->cursor_start;
4676
Bram Moolenaar81a82092008-03-12 16:27:00 +00004677 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
4680 * Leave the cursor at the first non-blank of the last formatted line.
4681 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4682 * line, so "." will do the next lines.
4683 */
4684 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4685 ++curwin->w_cursor.lnum;
4686 beginline(BL_WHITE | BL_FIX);
4687 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4688 msgmore(old_line_count);
4689
4690 /* put '] mark on the end of the formatted area */
4691 curbuf->b_op_end = curwin->w_cursor;
4692
4693 if (keep_cursor)
4694 {
4695 curwin->w_cursor = saved_cursor;
4696 saved_cursor.lnum = 0;
4697 }
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (oap->is_VIsual)
4700 {
4701 win_T *wp;
4702
4703 FOR_ALL_WINDOWS(wp)
4704 {
4705 if (wp->w_old_cursor_lnum != 0)
4706 {
4707 /* When lines have been inserted or deleted, adjust the end of
4708 * the Visual area to be redrawn. */
4709 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4710 wp->w_old_cursor_lnum += old_line_count;
4711 else
4712 wp->w_old_visual_lnum += old_line_count;
4713 }
4714 }
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004718#if defined(FEAT_EVAL) || defined(PROTO)
4719/*
4720 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4721 */
4722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004723op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004724{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004725 if (oap->is_VIsual)
4726 /* When there is no change: need to remove the Visual selection */
4727 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004728
Bram Moolenaar700303e2010-07-11 17:35:50 +02004729 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4730 /* As documented: when 'formatexpr' returns non-zero fall back to
4731 * internal formatting. */
4732 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004733}
4734
4735 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004736fex_format(
4737 linenr_T lnum,
4738 long count,
4739 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004740{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004741 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4742 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004743 int r;
4744
4745 /*
4746 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004747 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004748 */
4749 set_vim_var_nr(VV_LNUM, lnum);
4750 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004751 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004752
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004753 /*
4754 * Evaluate the function.
4755 */
4756 if (use_sandbox)
4757 ++sandbox;
4758 r = eval_to_number(curbuf->b_p_fex);
4759 if (use_sandbox)
4760 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004761
4762 set_vim_var_string(VV_CHAR, NULL, -1);
4763
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004764 return r;
4765}
4766#endif
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768/*
4769 * Format "line_count" lines, starting at the cursor position.
4770 * When "line_count" is negative, format until the end of the paragraph.
4771 * Lines after the cursor line are saved for undo, caller must have saved the
4772 * first line.
4773 */
4774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004775format_lines(
4776 linenr_T line_count,
4777 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 int max_len;
4780 int is_not_par; /* current line not part of parag. */
4781 int next_is_not_par; /* next line not part of paragraph */
4782 int is_end_par; /* at end of paragraph */
4783 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4784 int next_is_start_par = FALSE;
4785#ifdef FEAT_COMMENTS
4786 int leader_len = 0; /* leader len of current line */
4787 int next_leader_len; /* leader len of next line */
4788 char_u *leader_flags = NULL; /* flags for leader of current line */
4789 char_u *next_leader_flags; /* flags for leader of next line */
4790 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004791 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#endif
4793 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004794 int second_indent = -1; /* indent for second line (comment
4795 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 int do_second_indent;
4797 int do_number_indent;
4798 int do_trail_white;
4799 int first_par_line = TRUE;
4800 int smd_save;
4801 long count;
4802 int need_set_indent = TRUE; /* set indent of next paragraph */
4803 int force_format = FALSE;
4804 int old_State = State;
4805
4806 /* length of a line to force formatting: 3 * 'tw' */
4807 max_len = comp_textwidth(TRUE) * 3;
4808
4809 /* check for 'q', '2' and '1' in 'formatoptions' */
4810#ifdef FEAT_COMMENTS
4811 do_comments = has_format_option(FO_Q_COMS);
4812#endif
4813 do_second_indent = has_format_option(FO_Q_SECOND);
4814 do_number_indent = has_format_option(FO_Q_NUMBER);
4815 do_trail_white = has_format_option(FO_WHITE_PAR);
4816
4817 /*
4818 * Get info about the previous and current line.
4819 */
4820 if (curwin->w_cursor.lnum > 1)
4821 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4822#ifdef FEAT_COMMENTS
4823 , &leader_len, &leader_flags, do_comments
4824#endif
4825 );
4826 else
4827 is_not_par = TRUE;
4828 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4829#ifdef FEAT_COMMENTS
4830 , &next_leader_len, &next_leader_flags, do_comments
4831#endif
4832 );
4833 is_end_par = (is_not_par || next_is_not_par);
4834 if (!is_end_par && do_trail_white)
4835 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4836
4837 curwin->w_cursor.lnum--;
4838 for (count = line_count; count != 0 && !got_int; --count)
4839 {
4840 /*
4841 * Advance to next paragraph.
4842 */
4843 if (advance)
4844 {
4845 curwin->w_cursor.lnum++;
4846 prev_is_end_par = is_end_par;
4847 is_not_par = next_is_not_par;
4848#ifdef FEAT_COMMENTS
4849 leader_len = next_leader_len;
4850 leader_flags = next_leader_flags;
4851#endif
4852 }
4853
4854 /*
4855 * The last line to be formatted.
4856 */
4857 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4858 {
4859 next_is_not_par = TRUE;
4860#ifdef FEAT_COMMENTS
4861 next_leader_len = 0;
4862 next_leader_flags = NULL;
4863#endif
4864 }
4865 else
4866 {
4867 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4868#ifdef FEAT_COMMENTS
4869 , &next_leader_len, &next_leader_flags, do_comments
4870#endif
4871 );
4872 if (do_number_indent)
4873 next_is_start_par =
4874 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4875 }
4876 advance = TRUE;
4877 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4878 if (!is_end_par && do_trail_white)
4879 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4880
4881 /*
4882 * Skip lines that are not in a paragraph.
4883 */
4884 if (is_not_par)
4885 {
4886 if (line_count < 0)
4887 break;
4888 }
4889 else
4890 {
4891 /*
4892 * For the first line of a paragraph, check indent of second line.
4893 * Don't do this for comments and empty lines.
4894 */
4895 if (first_par_line
4896 && (do_second_indent || do_number_indent)
4897 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004898 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004900 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1))
4901 {
4902#ifdef FEAT_COMMENTS
4903 if (leader_len == 0 && next_leader_len == 0)
4904 {
4905 /* no comment found */
4906#endif
4907 second_indent =
4908 get_indent_lnum(curwin->w_cursor.lnum + 1);
4909#ifdef FEAT_COMMENTS
4910 }
4911 else
4912 {
4913 second_indent = next_leader_len;
4914 do_comments_list = 1;
4915 }
4916#endif
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004919 {
4920#ifdef FEAT_COMMENTS
4921 if (leader_len == 0 && next_leader_len == 0)
4922 {
4923 /* no comment found */
4924#endif
4925 second_indent =
4926 get_number_indent(curwin->w_cursor.lnum);
4927#ifdef FEAT_COMMENTS
4928 }
4929 else
4930 {
4931 /* get_number_indent() is now "comment aware"... */
4932 second_indent =
4933 get_number_indent(curwin->w_cursor.lnum);
4934 do_comments_list = 1;
4935 }
4936#endif
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939
4940 /*
4941 * When the comment leader changes, it's the end of the paragraph.
4942 */
4943 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4944#ifdef FEAT_COMMENTS
4945 || !same_leader(curwin->w_cursor.lnum,
4946 leader_len, leader_flags,
4947 next_leader_len, next_leader_flags)
4948#endif
4949 )
4950 is_end_par = TRUE;
4951
4952 /*
4953 * If we have got to the end of a paragraph, or the line is
4954 * getting long, format it.
4955 */
4956 if (is_end_par || force_format)
4957 {
4958 if (need_set_indent)
4959 /* replace indent in first line with minimal number of
4960 * tabs and spaces, according to current options */
4961 (void)set_indent(get_indent(), SIN_CHANGED);
4962
4963 /* put cursor on last non-space */
4964 State = NORMAL; /* don't go past end-of-line */
4965 coladvance((colnr_T)MAXCOL);
4966 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4967 dec_cursor();
4968
4969 /* do the formatting, without 'showmode' */
4970 State = INSERT; /* for open_line() */
4971 smd_save = p_smd;
4972 p_smd = FALSE;
4973 insertchar(NUL, INSCHAR_FORMAT
4974#ifdef FEAT_COMMENTS
4975 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004976 + (do_comments && do_comments_list
4977 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00004979 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 State = old_State;
4981 p_smd = smd_save;
4982 second_indent = -1;
4983 /* at end of par.: need to set indent of next par. */
4984 need_set_indent = is_end_par;
4985 if (is_end_par)
4986 {
4987 /* When called with a negative line count, break at the
4988 * end of the paragraph. */
4989 if (line_count < 0)
4990 break;
4991 first_par_line = TRUE;
4992 }
4993 force_format = FALSE;
4994 }
4995
4996 /*
4997 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02004998 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 */
5000 if (!is_end_par)
5001 {
5002 advance = FALSE;
5003 curwin->w_cursor.lnum++;
5004 curwin->w_cursor.col = 0;
5005 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005006 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005009 {
5010 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5012 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005013 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005015 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5016 {
5017 char_u *p = ml_get_curline();
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005018 int indent = (int)(skipwhite(p) - p);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005019
5020 if (indent > 0)
5021 {
5022 (void)del_bytes(indent, FALSE, FALSE);
5023 mark_col_adjust(curwin->w_cursor.lnum,
5024 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005025 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005028 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 beep_flush();
5031 break;
5032 }
5033 first_par_line = FALSE;
5034 /* If the line is getting long, format it next time */
5035 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5036 force_format = TRUE;
5037 else
5038 force_format = FALSE;
5039 }
5040 }
5041 line_breakcheck();
5042 }
5043}
5044
5045/*
5046 * Return TRUE if line "lnum" ends in a white character.
5047 */
5048 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 char_u *s = ml_get(lnum);
5052 size_t l;
5053
5054 if (*s == NUL)
5055 return FALSE;
5056 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
5057 * invocation may call function multiple times". */
5058 l = STRLEN(s) - 1;
5059 return vim_iswhite(s[l]);
5060}
5061
5062/*
5063 * Blank lines, and lines containing only the comment leader, are left
5064 * untouched by the formatting. The function returns TRUE in this
5065 * case. It also returns TRUE when a line starts with the end of a comment
5066 * ('e' in comment flags), so that this line is skipped, and not joined to the
5067 * previous line. A new paragraph starts after a blank line, or when the
5068 * comment leader changes -- webb.
5069 */
5070#ifdef FEAT_COMMENTS
5071 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005072fmt_check_par(
5073 linenr_T lnum,
5074 int *leader_len,
5075 char_u **leader_flags,
5076 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 char_u *flags = NULL; /* init for GCC */
5079 char_u *ptr;
5080
5081 ptr = ml_get(lnum);
5082 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005083 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 else
5085 *leader_len = 0;
5086
5087 if (*leader_len > 0)
5088 {
5089 /*
5090 * Search for 'e' flag in comment leader flags.
5091 */
5092 flags = *leader_flags;
5093 while (*flags && *flags != ':' && *flags != COM_END)
5094 ++flags;
5095 }
5096
5097 return (*skipwhite(ptr + *leader_len) == NUL
5098 || (*leader_len > 0 && *flags == COM_END)
5099 || startPS(lnum, NUL, FALSE));
5100}
5101#else
5102 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005103fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
5105 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5106}
5107#endif
5108
5109/*
5110 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5111 * previous line is in the same paragraph. Used for auto-formatting.
5112 */
5113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005114paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
5116 char_u *p;
5117#ifdef FEAT_COMMENTS
5118 int leader_len = 0; /* leader len of current line */
5119 char_u *leader_flags = NULL; /* flags for leader of current line */
5120 int next_leader_len; /* leader len of next line */
5121 char_u *next_leader_flags; /* flags for leader of next line */
5122 int do_comments; /* format comments */
5123#endif
5124
5125 if (lnum <= 1)
5126 return TRUE; /* start of the file */
5127
5128 p = ml_get(lnum - 1);
5129 if (*p == NUL)
5130 return TRUE; /* after empty line */
5131
5132#ifdef FEAT_COMMENTS
5133 do_comments = has_format_option(FO_Q_COMS);
5134#endif
5135 if (fmt_check_par(lnum - 1
5136#ifdef FEAT_COMMENTS
5137 , &leader_len, &leader_flags, do_comments
5138#endif
5139 ))
5140 return TRUE; /* after non-paragraph line */
5141
5142 if (fmt_check_par(lnum
5143#ifdef FEAT_COMMENTS
5144 , &next_leader_len, &next_leader_flags, do_comments
5145#endif
5146 ))
5147 return TRUE; /* "lnum" is not a paragraph line */
5148
5149 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5150 return TRUE; /* missing trailing space in previous line. */
5151
5152 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5153 return TRUE; /* numbered item starts in "lnum". */
5154
5155#ifdef FEAT_COMMENTS
5156 if (!same_leader(lnum - 1, leader_len, leader_flags,
5157 next_leader_len, next_leader_flags))
5158 return TRUE; /* change of comment leader. */
5159#endif
5160
5161 return FALSE;
5162}
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164/*
5165 * prepare a few things for block mode yank/delete/tilde
5166 *
5167 * for delete:
5168 * - textlen includes the first/last char to be (partly) deleted
5169 * - start/endspaces is the number of columns that are taken by the
5170 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005171 * deleted.
5172 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 * - textlen includes the first/last char to be wholly yanked
5174 * - start/endspaces is the number of columns of the first/last yanked char
5175 * that are to be yanked.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178block_prep(
5179 oparg_T *oap,
5180 struct block_def *bdp,
5181 linenr_T lnum,
5182 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 int incr = 0;
5185 char_u *pend;
5186 char_u *pstart;
5187 char_u *line;
5188 char_u *prev_pstart;
5189 char_u *prev_pend;
5190
5191 bdp->startspaces = 0;
5192 bdp->endspaces = 0;
5193 bdp->textlen = 0;
5194 bdp->start_vcol = 0;
5195 bdp->end_vcol = 0;
5196#ifdef FEAT_VISUALEXTRA
5197 bdp->is_short = FALSE;
5198 bdp->is_oneChar = FALSE;
5199 bdp->pre_whitesp = 0;
5200 bdp->pre_whitesp_c = 0;
5201 bdp->end_char_vcols = 0;
5202#endif
5203 bdp->start_char_vcols = 0;
5204
5205 line = ml_get(lnum);
5206 pstart = line;
5207 prev_pstart = line;
5208 while (bdp->start_vcol < oap->start_vcol && *pstart)
5209 {
5210 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005211 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 bdp->start_vcol += incr;
5213#ifdef FEAT_VISUALEXTRA
5214 if (vim_iswhite(*pstart))
5215 {
5216 bdp->pre_whitesp += incr;
5217 bdp->pre_whitesp_c++;
5218 }
5219 else
5220 {
5221 bdp->pre_whitesp = 0;
5222 bdp->pre_whitesp_c = 0;
5223 }
5224#endif
5225 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005226 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228 bdp->start_char_vcols = incr;
5229 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5230 {
5231 bdp->end_vcol = bdp->start_vcol;
5232#ifdef FEAT_VISUALEXTRA
5233 bdp->is_short = TRUE;
5234#endif
5235 if (!is_del || oap->op_type == OP_APPEND)
5236 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5237 }
5238 else
5239 {
5240 /* notice: this converts partly selected Multibyte characters to
5241 * spaces, too. */
5242 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5243 if (is_del && bdp->startspaces)
5244 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5245 pend = pstart;
5246 bdp->end_vcol = bdp->start_vcol;
5247 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5248 {
5249#ifdef FEAT_VISUALEXTRA
5250 bdp->is_oneChar = TRUE;
5251#endif
5252 if (oap->op_type == OP_INSERT)
5253 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5254 else if (oap->op_type == OP_APPEND)
5255 {
5256 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5257 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5258 }
5259 else
5260 {
5261 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5262 if (is_del && oap->op_type != OP_LSHIFT)
5263 {
5264 /* just putting the sum of those two into
5265 * bdp->startspaces doesn't work for Visual replace,
5266 * so we have to split the tab in two */
5267 bdp->startspaces = bdp->start_char_vcols
5268 - (bdp->start_vcol - oap->start_vcol);
5269 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5270 }
5271 }
5272 }
5273 else
5274 {
5275 prev_pend = pend;
5276 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5277 {
5278 /* Count a tab for what it's worth (if list mode not on) */
5279 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005280 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 bdp->end_vcol += incr;
5282 }
5283 if (bdp->end_vcol <= oap->end_vcol
5284 && (!is_del
5285 || oap->op_type == OP_APPEND
5286 || oap->op_type == OP_REPLACE)) /* line too short */
5287 {
5288#ifdef FEAT_VISUALEXTRA
5289 bdp->is_short = TRUE;
5290#endif
5291 /* Alternative: include spaces to fill up the block.
5292 * Disadvantage: can lead to trailing spaces when the line is
5293 * short where the text is put */
5294 /* if (!is_del || oap->op_type == OP_APPEND) */
5295 if (oap->op_type == OP_APPEND || virtual_op)
5296 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005297 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else
5299 bdp->endspaces = 0; /* replace doesn't add characters */
5300 }
5301 else if (bdp->end_vcol > oap->end_vcol)
5302 {
5303 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5304 if (!is_del && bdp->endspaces)
5305 {
5306 bdp->endspaces = incr - bdp->endspaces;
5307 if (pend != pstart)
5308 pend = prev_pend;
5309 }
5310 }
5311 }
5312#ifdef FEAT_VISUALEXTRA
5313 bdp->end_char_vcols = incr;
5314#endif
5315 if (is_del && bdp->startspaces)
5316 pstart = prev_pstart;
5317 bdp->textlen = (int)(pend - pstart);
5318 }
5319 bdp->textcol = (colnr_T) (pstart - line);
5320 bdp->textstart = pstart;
5321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005324 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005327op_addsub(
5328 oparg_T *oap,
5329 linenr_T Prenum1, /* Amount of add/subtract */
5330 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005332 pos_T pos;
5333 struct block_def bd;
5334 int change_cnt = 0;
5335 linenr_T amount = Prenum1;
5336
5337 if (!VIsual_active)
5338 {
5339 pos = curwin->w_cursor;
5340 if (u_save_cursor() == FAIL)
5341 return;
5342 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5343 if (change_cnt)
5344 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5345 }
5346 else
5347 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005348 int one_change;
5349 int length;
5350 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005351
5352 if (u_save((linenr_T)(oap->start.lnum - 1),
5353 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5354 return;
5355
5356 pos = oap->start;
5357 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5358 {
5359 if (oap->block_mode) /* Visual block mode */
5360 {
5361 block_prep(oap, &bd, pos.lnum, FALSE);
5362 pos.col = bd.textcol;
5363 length = bd.textlen;
5364 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005365 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005366 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005367 curwin->w_cursor.col = 0;
5368 pos.col = 0;
5369 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5370 }
5371 else /* oap->motion_type == MCHAR */
5372 {
5373 if (!oap->inclusive)
5374 dec(&(oap->end));
5375 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5376 pos.col = 0;
5377 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005378 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005379 pos.col += oap->start.col;
5380 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005381 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005382 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005383 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005384 length = (int)STRLEN(ml_get(oap->end.lnum));
5385 if (oap->end.col >= length)
5386 oap->end.col = length - 1;
5387 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005388 }
5389 }
5390 one_change = do_addsub(oap->op_type, &pos, length, amount);
5391 if (one_change)
5392 {
5393 /* Remember the start position of the first change. */
5394 if (change_cnt == 0)
5395 startpos = curbuf->b_op_start;
5396 ++change_cnt;
5397 }
5398
5399#ifdef FEAT_NETBEANS_INTG
5400 if (netbeans_active() && one_change)
5401 {
5402 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5403
5404 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5405 netbeans_inserted(curbuf, pos.lnum, pos.col,
5406 &ptr[pos.col], length);
5407 }
5408#endif
5409 if (g_cmd && one_change)
5410 amount += Prenum1;
5411 }
5412 if (change_cnt)
5413 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5414
5415 if (!change_cnt && oap->is_VIsual)
5416 /* No change: need to remove the Visual selection */
5417 redraw_curbuf_later(INVERTED);
5418
5419 /* Set '[ mark if something changed. Keep the last end
5420 * position from do_addsub(). */
5421 if (change_cnt > 0)
5422 curbuf->b_op_start = startpos;
5423
5424 if (change_cnt > p_report)
5425 {
5426 if (change_cnt == 1)
5427 MSG(_("1 line changed"));
5428 else
5429 smsg((char_u *)_("%ld lines changed"), change_cnt);
5430 }
5431 }
5432}
5433
5434/*
5435 * Add or subtract 'Prenum1' from a number in a line
5436 * op_type is OP_NR_ADD or OP_NR_SUB
5437 *
5438 * Returns TRUE if some character was changed.
5439 */
5440 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005441do_addsub(
5442 int op_type,
5443 pos_T *pos,
5444 int length,
5445 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 int col;
5448 char_u *buf1;
5449 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005450 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005452 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 long_u oldn;
5454 char_u *ptr;
5455 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 int todel;
5457 int dohex;
5458 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005459 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 int doalp;
5461 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005463 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005464 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005465 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005466 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005467 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005468 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005469 pos_T startpos;
5470 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5473 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005474 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5476
Bram Moolenaard79e5502016-01-10 22:13:02 +01005477 curwin->w_cursor = *pos;
5478 ptr = ml_get(pos->lnum);
5479 col = pos->col;
5480
5481 if (*ptr == NUL)
5482 goto theend;
5483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 /*
5485 * First check if we are on a hexadecimal number, after the "0x".
5486 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005487 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005489 if (dobin)
5490 while (col > 0 && vim_isbdigit(ptr[col]))
5491 --col;
5492
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005493 if (dohex)
5494 while (col > 0 && vim_isxdigit(ptr[col]))
5495 --col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005496
5497 if ( dobin
5498 && dohex
5499 && ! ((col > 0
5500 && (ptr[col] == 'X'
5501 || ptr[col] == 'x')
5502 && ptr[col - 1] == '0'
5503 && vim_isxdigit(ptr[col + 1]))))
5504 {
5505
5506 /* In case of binary/hexadecimal pattern overlap match, rescan */
5507
Bram Moolenaard79e5502016-01-10 22:13:02 +01005508 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005509
5510 while (col > 0 && vim_isdigit(ptr[col]))
5511 col--;
5512 }
5513
5514 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005515 && col > 0
5516 && (ptr[col] == 'X'
5517 || ptr[col] == 'x')
5518 && ptr[col - 1] == '0'
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005519 && vim_isxdigit(ptr[col + 1])) ||
5520 ( dobin
5521 && col > 0
5522 && (ptr[col] == 'B'
5523 || ptr[col] == 'b')
5524 && ptr[col - 1] == '0'
5525 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005526 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005527 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005528 --col;
5529 }
5530 else
5531 {
5532 /*
5533 * Search forward and then backward to find the start of number.
5534 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005535 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005536
5537 while (ptr[col] != NUL
5538 && !vim_isdigit(ptr[col])
5539 && !(doalp && ASCII_ISALPHA(ptr[col])))
5540 ++col;
5541
5542 while (col > 0
5543 && vim_isdigit(ptr[col - 1])
5544 && !(doalp && ASCII_ISALPHA(ptr[col])))
5545 --col;
5546 }
5547 }
5548
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005549 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005550 {
5551 while (ptr[col] != NUL && length > 0
5552 && !vim_isdigit(ptr[col])
5553 && !(doalp && ASCII_ISALPHA(ptr[col])))
5554 {
5555 ++col;
5556 --length;
5557 }
5558
5559 if (length == 0)
5560 goto theend;
5561
5562 if (col > pos->col && ptr[col - 1] == '-')
5563 {
5564 negative = TRUE;
5565 was_positive = FALSE;
5566 }
5567 }
5568
5569 /*
5570 * If a number was found, and saving for undo works, replace the number.
5571 */
5572 firstdigit = ptr[col];
5573 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5574 {
5575 beep_flush();
5576 goto theend;
5577 }
5578
5579 if (doalp && ASCII_ISALPHA(firstdigit))
5580 {
5581 /* decrement or increment alphabetic character */
5582 if (op_type == OP_NR_SUB)
5583 {
5584 if (CharOrd(firstdigit) < Prenum1)
5585 {
5586 if (isupper(firstdigit))
5587 firstdigit = 'A';
5588 else
5589 firstdigit = 'a';
5590 }
5591 else
5592#ifdef EBCDIC
5593 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5594#else
5595 firstdigit -= Prenum1;
5596#endif
5597 }
5598 else
5599 {
5600 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5601 {
5602 if (isupper(firstdigit))
5603 firstdigit = 'Z';
5604 else
5605 firstdigit = 'z';
5606 }
5607 else
5608#ifdef EBCDIC
5609 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5610#else
5611 firstdigit += Prenum1;
5612#endif
5613 }
5614 curwin->w_cursor.col = col;
5615 if (!did_change)
5616 startpos = curwin->w_cursor;
5617 did_change = TRUE;
5618 (void)del_char(FALSE);
5619 ins_char(firstdigit);
5620 endpos = curwin->w_cursor;
5621 curwin->w_cursor.col = col;
5622 }
5623 else
5624 {
5625 if (col > 0 && ptr[col - 1] == '-' && !visual)
5626 {
5627 /* negative number */
5628 --col;
5629 negative = TRUE;
5630 }
5631 /* get the number value (unsigned) */
5632 if (visual && VIsual_mode != 'V')
5633 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5634 ? (int)STRLEN(ptr) - col
5635 : length);
5636
5637 vim_str2nr(ptr + col, &pre, &length,
5638 0 + (dobin ? STR2NR_BIN : 0)
5639 + (dooct ? STR2NR_OCT : 0)
5640 + (dohex ? STR2NR_HEX : 0),
5641 NULL, &n, maxlen);
5642
5643 /* ignore leading '-' for hex and octal and bin numbers */
5644 if (pre && negative)
5645 {
5646 ++col;
5647 --length;
5648 negative = FALSE;
5649 }
5650 /* add or subtract */
5651 subtract = FALSE;
5652 if (op_type == OP_NR_SUB)
5653 subtract ^= TRUE;
5654 if (negative)
5655 subtract ^= TRUE;
5656
5657 oldn = n;
5658 if (subtract)
5659 n -= (unsigned long)Prenum1;
5660 else
5661 n += (unsigned long)Prenum1;
5662 /* handle wraparound for decimal numbers */
5663 if (!pre)
5664 {
5665 if (subtract)
5666 {
5667 if (n > oldn)
5668 {
5669 n = 1 + (n ^ (unsigned long)-1);
5670 negative ^= TRUE;
5671 }
5672 }
5673 else
5674 {
5675 /* add */
5676 if (n < oldn)
5677 {
5678 n = (n ^ (unsigned long)-1);
5679 negative ^= TRUE;
5680 }
5681 }
5682 if (n == 0)
5683 negative = FALSE;
5684 }
5685
5686 if (visual && !was_positive && !negative && col > 0)
5687 {
5688 /* need to remove the '-' */
5689 col--;
5690 length++;
5691 }
5692
5693 /*
5694 * Delete the old number.
5695 */
5696 curwin->w_cursor.col = col;
5697 if (!did_change)
5698 startpos = curwin->w_cursor;
5699 did_change = TRUE;
5700 todel = length;
5701 c = gchar_cursor();
5702 /*
5703 * Don't include the '-' in the length, only the length of the
5704 * part after it is kept the same.
5705 */
5706 if (c == '-')
5707 --length;
5708 while (todel-- > 0)
5709 {
5710 if (c < 0x100 && isalpha(c))
5711 {
5712 if (isupper(c))
5713 hexupper = TRUE;
5714 else
5715 hexupper = FALSE;
5716 }
5717 /* del_char() will mark line needing displaying */
5718 (void)del_char(FALSE);
5719 c = gchar_cursor();
5720 }
5721
5722 /*
5723 * Prepare the leading characters in buf1[].
5724 * When there are many leading zeros it could be very long.
5725 * Allocate a bit too much.
5726 */
5727 buf1 = alloc((unsigned)length + NUMBUFLEN);
5728 if (buf1 == NULL)
5729 goto theend;
5730 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005731 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005732 {
5733 *ptr++ = '-';
5734 }
5735 if (pre)
5736 {
5737 *ptr++ = '0';
5738 --length;
5739 }
5740 if (pre == 'b' || pre == 'B' ||
5741 pre == 'x' || pre == 'X')
5742 {
5743 *ptr++ = pre;
5744 --length;
5745 }
5746
5747 /*
5748 * Put the number characters in buf2[].
5749 */
5750 if (pre == 'b' || pre == 'B')
5751 {
5752 int i;
5753 int bit = 0;
5754 int bits = sizeof(unsigned long) * 8;
5755
5756 /* leading zeros */
5757 for (bit = bits; bit > 0; bit--)
5758 if ((n >> (bit - 1)) & 0x1) break;
5759
5760 for (i = 0; bit > 0; bit--)
5761 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5762
5763 buf2[i] = '\0';
5764 }
5765 else if (pre == 0)
5766 sprintf((char *)buf2, "%lu", n);
5767 else if (pre == '0')
5768 sprintf((char *)buf2, "%lo", n);
5769 else if (pre && hexupper)
5770 sprintf((char *)buf2, "%lX", n);
5771 else
5772 sprintf((char *)buf2, "%lx", n);
5773 length -= (int)STRLEN(buf2);
5774
5775 /*
5776 * Adjust number of zeros to the new number of digits, so the
5777 * total length of the number remains the same.
5778 * Don't do this when
5779 * the result may look like an octal number.
5780 */
5781 if (firstdigit == '0' && !(dooct && pre == 0))
5782 while (length-- > 0)
5783 *ptr++ = '0';
5784 *ptr = NUL;
5785 STRCAT(buf1, buf2);
5786 ins_str(buf1); /* insert the new number */
5787 vim_free(buf1);
5788 endpos = curwin->w_cursor;
5789 if (did_change && curwin->w_cursor.col)
5790 --curwin->w_cursor.col;
5791 }
5792
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005793 if (did_change)
5794 {
5795 /* set the '[ and '] marks */
5796 curbuf->b_op_start = startpos;
5797 curbuf->b_op_end = endpos;
5798 if (curbuf->b_op_end.col > 0)
5799 --curbuf->b_op_end.col;
5800 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005801
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005802theend:
5803 if (visual)
5804 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005805 else if (did_change)
5806 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005807
Bram Moolenaard79e5502016-01-10 22:13:02 +01005808 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809}
5810
5811#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005812
5813static yankreg_T *y_read_regs = NULL;
5814
5815#define REG_PREVIOUS 1
5816#define REG_EXEC 2
5817
5818/*
5819 * Prepare for reading viminfo registers when writing viminfo later.
5820 */
5821 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005822prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005823{
5824 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
5825 * (int)sizeof(yankreg_T));
5826}
5827
5828 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02005829finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005830{
5831 int i;
5832 int j;
5833
5834 if (y_read_regs != NULL)
5835 {
5836 for (i = 0; i < NUM_REGISTERS; ++i)
5837 if (y_read_regs[i].y_array != NULL)
5838 {
5839 for (j = 0; j < y_read_regs[i].y_size; j++)
5840 vim_free(y_read_regs[i].y_array[j]);
5841 vim_free(y_read_regs[i].y_array);
5842 }
5843 vim_free(y_read_regs);
5844 y_read_regs = NULL;
5845 }
5846}
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005849read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 int eof;
5852 int do_it = TRUE;
5853 int size;
5854 int limit;
5855 int i;
5856 int set_prev = FALSE;
5857 char_u *str;
5858 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005859 int new_type = MCHAR; /* init to shut up compiler */
5860 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862 /* We only get here (hopefully) if line[0] == '"' */
5863 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005864
5865 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (*str == '"')
5867 {
5868 set_prev = TRUE;
5869 str++;
5870 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00005871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (!ASCII_ISALNUM(*str) && *str != '-')
5873 {
5874 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5875 return TRUE; /* too many errors, pretend end-of-file */
5876 do_it = FALSE;
5877 }
5878 get_yank_register(*str++, FALSE);
5879 if (!force && y_current->y_array != NULL)
5880 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00005881
5882 if (*str == '@')
5883 {
5884 /* "x@: register x used for @@ */
5885 if (force || execreg_lastc == NUL)
5886 execreg_lastc = str[-1];
5887 }
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 size = 0;
5890 limit = 100; /* Optimized for registers containing <= 100 lines */
5891 if (do_it)
5892 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005893 /*
5894 * Build the new register in array[].
5895 * y_array is kept as-is until done.
5896 * The "do_it" flag is reset when something is wrong, in which case
5897 * array[] needs to be freed.
5898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 if (set_prev)
5900 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01005901 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00005902 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005904 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005906 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005908 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 /* get the block width; if it's missing we get a zero, which is OK */
5910 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005911 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913
5914 while (!(eof = viminfo_readline(virp))
5915 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5916 {
5917 if (do_it)
5918 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005919 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005921 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01005923
5924 if (new_array == NULL)
5925 {
5926 do_it = FALSE;
5927 break;
5928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005930 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01005932 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 str = viminfo_readstring(virp, 1, TRUE);
5936 if (str != NULL)
5937 array[size++] = str;
5938 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01005939 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 do_it = FALSE;
5941 }
5942 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01005943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 if (do_it)
5945 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005946 /* free y_array[] */
5947 for (i = 0; i < y_current->y_size; i++)
5948 vim_free(y_current->y_array[i]);
5949 vim_free(y_current->y_array);
5950
5951 y_current->y_type = new_type;
5952 y_current->y_width = new_width;
5953 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005954 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (size == 0)
5956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 y_current->y_array = NULL;
5958 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01005961 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 y_current->y_array =
5963 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5964 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01005965 {
5966 if (y_current->y_array == NULL)
5967 vim_free(array[i]);
5968 else
5969 y_current->y_array[i] = array[i];
5970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01005973 else
5974 {
5975 /* Free array[] if it was filled. */
5976 for (i = 0; i < size; i++)
5977 vim_free(array[i]);
5978 }
5979 vim_free(array);
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 return eof;
5982}
5983
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02005984/*
5985 * Accept a new style register line from the viminfo, store it when it's new.
5986 */
5987 void
5988handle_viminfo_register(garray_T *values, int force)
5989{
5990 bval_T *vp = (bval_T *)values->ga_data;
5991 int flags;
5992 int name;
5993 int type;
5994 int linecount;
5995 int width;
5996 time_t timestamp;
5997 yankreg_T *y_ptr;
5998 int i;
5999
6000 /* Check the format:
6001 * |{bartype},{flags},{name},{type},
6002 * {linecount},{width},{timestamp},"line1","line2"
6003 */
6004 if (values->ga_len < 6
6005 || vp[0].bv_type != BVAL_NR
6006 || vp[1].bv_type != BVAL_NR
6007 || vp[2].bv_type != BVAL_NR
6008 || vp[3].bv_type != BVAL_NR
6009 || vp[4].bv_type != BVAL_NR
6010 || vp[5].bv_type != BVAL_NR)
6011 return;
6012 flags = vp[0].bv_nr;
6013 name = vp[1].bv_nr;
6014 if (name < 0 || name > NUM_REGISTERS)
6015 return;
6016 type = vp[2].bv_nr;
6017 if (type != MCHAR && type != MLINE && type != MBLOCK)
6018 return;
6019 linecount = vp[3].bv_nr;
6020 if (values->ga_len < 6 + linecount)
6021 return;
6022 width = vp[4].bv_nr;
6023 if (width < 0)
6024 return;
6025
6026 if (y_read_regs != NULL)
6027 /* Reading viminfo for merging and writing. Store the register
6028 * content, don't update the current registers. */
6029 y_ptr = &y_read_regs[name];
6030 else
6031 y_ptr = &y_regs[name];
6032
6033 /* Do not overwrite unless forced or the timestamp is newer. */
6034 timestamp = (time_t)vp[5].bv_nr;
6035 if (y_ptr->y_array != NULL && !force
6036 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6037 return;
6038
6039 for (i = 0; i < y_ptr->y_size; i++)
6040 vim_free(y_ptr->y_array[i]);
6041 vim_free(y_ptr->y_array);
6042
6043 if (y_read_regs == NULL)
6044 {
6045 if (flags & REG_PREVIOUS)
6046 y_previous = y_ptr;
6047 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6048 execreg_lastc = get_register_name(name);
6049 }
6050 y_ptr->y_type = type;
6051 y_ptr->y_width = width;
6052 y_ptr->y_size = linecount;
6053 y_ptr->y_time_set = timestamp;
6054 if (linecount == 0)
6055 y_ptr->y_array = NULL;
6056 else
6057 {
6058 y_ptr->y_array =
6059 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6060 for (i = 0; i < linecount; i++)
6061 {
6062 if (vp[i + 6].bv_allocated)
6063 {
6064 y_ptr->y_array[i] = vp[i + 6].bv_string;
6065 vp[i + 6].bv_string = NULL;
6066 }
6067 else
6068 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6069 }
6070 }
6071}
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006074write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006076 int i, j;
6077 char_u *type;
6078 char_u c;
6079 int num_lines;
6080 int max_num_lines;
6081 int max_kbyte;
6082 long len;
6083 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Bram Moolenaar64404472010-06-26 06:24:45 +02006085 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 /* Get '<' value, use old '"' value if '<' is not found. */
6088 max_num_lines = get_viminfo_parameter('<');
6089 if (max_num_lines < 0)
6090 max_num_lines = get_viminfo_parameter('"');
6091 if (max_num_lines == 0)
6092 return;
6093 max_kbyte = get_viminfo_parameter('s');
6094 if (max_kbyte == 0)
6095 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 for (i = 0; i < NUM_REGISTERS; i++)
6098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099#ifdef FEAT_CLIPBOARD
6100 /* Skip '*'/'+' register, we don't want them back next time */
6101 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6102 continue;
6103#endif
6104#ifdef FEAT_DND
6105 /* Neither do we want the '~' register */
6106 if (i == TILDE_REGISTER)
6107 continue;
6108#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006109 /* When reading viminfo for merging and writing: Use the register from
6110 * viminfo if it's newer. */
6111 if (y_read_regs != NULL
6112 && y_read_regs[i].y_array != NULL
6113 && (y_regs[i].y_array == NULL ||
6114 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6115 y_ptr = &y_read_regs[i];
6116 else if (y_regs[i].y_array == NULL)
6117 continue;
6118 else
6119 y_ptr = &y_regs[i];
6120
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006121 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006122 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006123 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006124 || (num_lines == 1 && y_ptr->y_type == MCHAR
6125 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006126 continue;
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (max_kbyte > 0)
6129 {
6130 /* Skip register if there is more text than the maximum size. */
6131 len = 0;
6132 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006133 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (len > (long)max_kbyte * 1024L)
6135 continue;
6136 }
6137
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006138 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
6140 case MLINE:
6141 type = (char_u *)"LINE";
6142 break;
6143 case MCHAR:
6144 type = (char_u *)"CHAR";
6145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case MBLOCK:
6147 type = (char_u *)"BLOCK";
6148 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 default:
6150 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006151 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 emsg(IObuff);
6153 type = (char_u *)"LINE";
6154 break;
6155 }
6156 if (y_previous == &y_regs[i])
6157 fprintf(fp, "\"");
6158 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006159 fprintf(fp, "\"%c", c);
6160 if (c == execreg_lastc)
6161 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006162 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 /* If max_num_lines < 0, then we save ALL the lines in the register */
6165 if (max_num_lines > 0 && num_lines > max_num_lines)
6166 num_lines = max_num_lines;
6167 for (j = 0; j < num_lines; j++)
6168 {
6169 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006170 viminfo_writestring(fp, y_ptr->y_array[j]);
6171 }
6172
6173 {
6174 int flags = 0;
6175 int remaining;
6176
6177 /* New style with a bar line. Format:
6178 * |{bartype},{flags},{name},{type},
6179 * {linecount},{width},{timestamp},"line1","line2"
6180 * flags: REG_PREVIOUS - register is y_previous
6181 * REG_EXEC - used for @@
6182 */
6183 if (y_previous == &y_regs[i])
6184 flags |= REG_PREVIOUS;
6185 if (c == execreg_lastc)
6186 flags |= REG_EXEC;
6187 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6188 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6189 (long)y_ptr->y_time_set);
6190 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6191 remaining = LSIZE - 71;
6192 for (j = 0; j < num_lines; j++)
6193 {
6194 putc(',', fp);
6195 --remaining;
6196 remaining = barline_writestring(fp, y_ptr->y_array[j],
6197 remaining);
6198 }
6199 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
6201 }
6202}
6203#endif /* FEAT_VIMINFO */
6204
6205#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6206/*
6207 * SELECTION / PRIMARY ('*')
6208 *
6209 * Text selection stuff that uses the GUI selection register '*'. When using a
6210 * GUI this may be text from another window, otherwise it is the last text we
6211 * had highlighted with VIsual mode. With mouse support, clicking the middle
6212 * button performs the paste, otherwise you will need to do <"*p>. "
6213 * If not under X, it is synonymous with the clipboard register '+'.
6214 *
6215 * X CLIPBOARD ('+')
6216 *
6217 * Text selection stuff that uses the GUI clipboard register '+'.
6218 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6219 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6220 * otherwise you will need to do <"+p>. "
6221 * If not under X, it is synonymous with the selection register '*'.
6222 */
6223
6224/*
6225 * Routine to export any final X selection we had to the environment
6226 * so that the text is still available after vim has exited. X selections
6227 * only exist while the owning application exists, so we write to the
6228 * permanent (while X runs) store CUT_BUFFER0.
6229 * Dump the CLIPBOARD selection if we own it (it's logically the more
6230 * 'permanent' of the two), otherwise the PRIMARY one.
6231 * For now, use a hard-coded sanity limit of 1Mb of data.
6232 */
6233#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
6234 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 Display *dpy;
6238 char_u *str = NULL;
6239 long_u len = 0;
6240 int motion_type = -1;
6241
6242# ifdef FEAT_GUI
6243 if (gui.in_use)
6244 dpy = X_DISPLAY;
6245 else
6246# endif
6247# ifdef FEAT_XCLIPBOARD
6248 dpy = xterm_dpy;
6249# else
6250 return;
6251# endif
6252
6253 /* Get selection to export */
6254 if (clip_plus.owned)
6255 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6256 else if (clip_star.owned)
6257 motion_type = clip_convert_selection(&str, &len, &clip_star);
6258
6259 /* Check it's OK */
6260 if (dpy != NULL && str != NULL && motion_type >= 0
6261 && len < 1024*1024 && len > 0)
6262 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006263#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006264 int ok = TRUE;
6265
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006266 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6267 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6268 * encoding conversion usually doesn't work, so keep the text as-is.
6269 */
6270 if (has_mbyte)
6271 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006272 vimconv_T vc;
6273
6274 vc.vc_type = CONV_NONE;
6275 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6276 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006277 int intlen = len;
6278 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006279
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006280 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006281 conv_str = string_convert(&vc, str, &intlen);
6282 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006283 if (conv_str != NULL)
6284 {
6285 vim_free(str);
6286 str = conv_str;
6287 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006288 else
6289 {
6290 ok = FALSE;
6291 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006292 convert_setup(&vc, NULL, NULL);
6293 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006294 else
6295 {
6296 ok = FALSE;
6297 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006298 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006299
6300 /* Do not store the string if conversion failed. Better to use any
6301 * other selection than garbled text. */
6302 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006303#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006304 {
6305 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6306 XFlush(dpy);
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
6309
6310 vim_free(str);
6311}
6312#endif
6313
6314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006315clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006317 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 if (cbd == &clip_plus)
6320 y_current = &y_regs[PLUS_REGISTER];
6321 else
6322 y_current = &y_regs[STAR_REGISTER];
6323 free_yank_all();
6324 y_current->y_size = 0;
6325 y_current = y_ptr;
6326}
6327
6328/*
6329 * Get the selected text and put it in the gui selection register '*' or '+'.
6330 */
6331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006332clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006334 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 pos_T old_visual;
6337 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 colnr_T old_curswant;
6339 int old_set_curswant;
6340 pos_T old_op_start, old_op_end;
6341 oparg_T oa;
6342 cmdarg_T ca;
6343
6344 if (cbd->owned)
6345 {
6346 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6347 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6348 return;
6349
6350 /* Get the text between clip_star.start & clip_star.end */
6351 old_y_previous = y_previous;
6352 old_y_current = y_current;
6353 old_cursor = curwin->w_cursor;
6354 old_curswant = curwin->w_curswant;
6355 old_set_curswant = curwin->w_set_curswant;
6356 old_op_start = curbuf->b_op_start;
6357 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 old_visual = VIsual;
6359 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 clear_oparg(&oa);
6361 oa.regname = (cbd == &clip_plus ? '+' : '*');
6362 oa.op_type = OP_YANK;
6363 vim_memset(&ca, 0, sizeof(ca));
6364 ca.oap = &oa;
6365 ca.cmdchar = 'y';
6366 ca.count1 = 1;
6367 ca.retval = CA_NO_ADJ_OP_END;
6368 do_pending_operator(&ca, 0, TRUE);
6369 y_previous = old_y_previous;
6370 y_current = old_y_current;
6371 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006372 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 curwin->w_curswant = old_curswant;
6374 curwin->w_set_curswant = old_set_curswant;
6375 curbuf->b_op_start = old_op_start;
6376 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 VIsual = old_visual;
6378 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380 else
6381 {
6382 clip_free_selection(cbd);
6383
6384 /* Try to get selected text from another window */
6385 clip_gen_request_selection(cbd);
6386 }
6387}
6388
Bram Moolenaard44347f2011-06-19 01:14:29 +02006389/*
6390 * Convert from the GUI selection string into the '*'/'+' register.
6391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006393clip_yank_selection(
6394 int type,
6395 char_u *str,
6396 long len,
6397 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006399 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 if (cbd == &clip_plus)
6402 y_ptr = &y_regs[PLUS_REGISTER];
6403 else
6404 y_ptr = &y_regs[STAR_REGISTER];
6405
6406 clip_free_selection(cbd);
6407
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006408 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410
6411/*
6412 * Convert the '*'/'+' register into a GUI selection string returned in *str
6413 * with length *len.
6414 * Returns the motion type, or -1 for failure.
6415 */
6416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 char_u *p;
6420 int lnum;
6421 int i, j;
6422 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006423 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 if (cbd == &clip_plus)
6426 y_ptr = &y_regs[PLUS_REGISTER];
6427 else
6428 y_ptr = &y_regs[STAR_REGISTER];
6429
6430#ifdef USE_CRNL
6431 eolsize = 2;
6432#else
6433 eolsize = 1;
6434#endif
6435
6436 *str = NULL;
6437 *len = 0;
6438 if (y_ptr->y_array == NULL)
6439 return -1;
6440
6441 for (i = 0; i < y_ptr->y_size; i++)
6442 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6443
6444 /*
6445 * Don't want newline character at end of last line if we're in MCHAR mode.
6446 */
6447 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6448 *len -= eolsize;
6449
6450 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6451 if (p == NULL)
6452 return -1;
6453 lnum = 0;
6454 for (i = 0, j = 0; i < (int)*len; i++, j++)
6455 {
6456 if (y_ptr->y_array[lnum][j] == '\n')
6457 p[i] = NUL;
6458 else if (y_ptr->y_array[lnum][j] == NUL)
6459 {
6460#ifdef USE_CRNL
6461 p[i++] = '\r';
6462#endif
6463#ifdef USE_CR
6464 p[i] = '\r';
6465#else
6466 p[i] = '\n';
6467#endif
6468 lnum++;
6469 j = -1;
6470 }
6471 else
6472 p[i] = y_ptr->y_array[lnum][j];
6473 }
6474 return y_ptr->y_type;
6475}
6476
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * If we have written to a clipboard register, send the text to the clipboard.
6480 */
6481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6485 {
6486 clip_own_selection(&clip_star);
6487 clip_gen_set_selection(&clip_star);
6488 }
6489 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6490 {
6491 clip_own_selection(&clip_plus);
6492 clip_gen_set_selection(&clip_plus);
6493 }
6494}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496#endif /* FEAT_CLIPBOARD || PROTO */
6497
6498
6499#if defined(FEAT_DND) || defined(PROTO)
6500/*
6501 * Replace the contents of the '~' register with str.
6502 */
6503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006506 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 curr = y_current;
6509 y_current = &y_regs[TILDE_REGISTER];
6510 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006511 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 y_current = curr;
6513}
6514#endif
6515
6516
6517#if defined(FEAT_EVAL) || defined(PROTO)
6518/*
6519 * Return the type of a register.
6520 * Used for getregtype()
6521 * Returns MAUTO for error.
6522 */
6523 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006524get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 switch (regname)
6527 {
6528 case '%': /* file name */
6529 case '#': /* alternate file name */
6530 case '=': /* expression */
6531 case ':': /* last command line */
6532 case '/': /* last search-pattern */
6533 case '.': /* last inserted text */
6534#ifdef FEAT_SEARCHPATH
6535 case Ctrl_F: /* Filename under cursor */
6536 case Ctrl_P: /* Path under cursor, expand via "path" */
6537#endif
6538 case Ctrl_W: /* word under cursor */
6539 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6540 case '_': /* black hole: always empty */
6541 return MCHAR;
6542 }
6543
6544#ifdef FEAT_CLIPBOARD
6545 regname = may_get_selection(regname);
6546#endif
6547
Bram Moolenaar32b92012014-01-14 12:33:36 +01006548 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006549 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 get_yank_register(regname, FALSE);
6552
6553 if (y_current->y_array != NULL)
6554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (reglen != NULL && y_current->y_type == MBLOCK)
6556 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 return y_current->y_type;
6558 }
6559 return MAUTO;
6560}
6561
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006562static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006563
6564/*
6565 * When "flags" has GREG_LIST return a list with text "s".
6566 * Otherwise just return "s".
6567 */
6568 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006569getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006570{
6571 if (flags & GREG_LIST)
6572 {
6573 list_T *list = list_alloc();
6574
6575 if (list != NULL)
6576 {
6577 if (list_append_string(list, NULL, -1) == FAIL)
6578 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006579 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006580 return NULL;
6581 }
6582 list->lv_first->li_tv.vval.v_string = s;
6583 }
6584 return (char_u *)list;
6585 }
6586 return s;
6587}
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * Return the contents of a register as a single allocated string.
6591 * Used for "@r" in expressions and for getreg().
6592 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006593 * Flags:
6594 * GREG_NO_EXPR Do not allow expression register
6595 * GREG_EXPR_SRC For the expression register: return expression itself,
6596 * not the result of its evaluation.
6597 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 */
6599 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 long i;
6603 char_u *retval;
6604 int allocated;
6605 long len;
6606
6607 /* Don't allow using an expression register inside an expression */
6608 if (regname == '=')
6609 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006610 if (flags & GREG_NO_EXPR)
6611 return NULL;
6612 if (flags & GREG_EXPR_SRC)
6613 return getreg_wrap_one_line(get_expr_line_src(), flags);
6614 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 }
6616
6617 if (regname == '@') /* "@@" is used for unnamed register */
6618 regname = '"';
6619
6620 /* check for valid regname */
6621 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6622 return NULL;
6623
6624#ifdef FEAT_CLIPBOARD
6625 regname = may_get_selection(regname);
6626#endif
6627
6628 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6629 {
6630 if (retval == NULL)
6631 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006632 if (allocated)
6633 return getreg_wrap_one_line(retval, flags);
6634 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
6636
6637 get_yank_register(regname, FALSE);
6638 if (y_current->y_array == NULL)
6639 return NULL;
6640
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006641 if (flags & GREG_LIST)
6642 {
6643 list_T *list = list_alloc();
6644 int error = FALSE;
6645
6646 if (list == NULL)
6647 return NULL;
6648 for (i = 0; i < y_current->y_size; ++i)
6649 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6650 error = TRUE;
6651 if (error)
6652 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006653 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006654 return NULL;
6655 }
6656 return (char_u *)list;
6657 }
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 /*
6660 * Compute length of resulting string.
6661 */
6662 len = 0;
6663 for (i = 0; i < y_current->y_size; ++i)
6664 {
6665 len += (long)STRLEN(y_current->y_array[i]);
6666 /*
6667 * Insert a newline between lines and after last line if
6668 * y_type is MLINE.
6669 */
6670 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6671 ++len;
6672 }
6673
6674 retval = lalloc(len + 1, TRUE);
6675
6676 /*
6677 * Copy the lines of the yank register into the string.
6678 */
6679 if (retval != NULL)
6680 {
6681 len = 0;
6682 for (i = 0; i < y_current->y_size; ++i)
6683 {
6684 STRCPY(retval + len, y_current->y_array[i]);
6685 len += (long)STRLEN(retval + len);
6686
6687 /*
6688 * Insert a NL between lines and after the last line if y_type is
6689 * MLINE.
6690 */
6691 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6692 retval[len++] = '\n';
6693 }
6694 retval[len] = NUL;
6695 }
6696
6697 return retval;
6698}
6699
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006700 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006702 int name,
6703 yankreg_T **old_y_previous,
6704 yankreg_T **old_y_current,
6705 int must_append,
6706 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006707{
6708 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6709 {
6710 emsg_invreg(name);
6711 return FAIL;
6712 }
6713
6714 /* Don't want to change the current (unnamed) register */
6715 *old_y_previous = y_previous;
6716 *old_y_current = y_current;
6717
6718 get_yank_register(name, TRUE);
6719 if (!y_append && !must_append)
6720 free_yank_all();
6721 return OK;
6722}
6723
6724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006726 int name,
6727 yankreg_T *old_y_previous,
6728 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006729{
6730# ifdef FEAT_CLIPBOARD
6731 /* Send text of clipboard register to the clipboard. */
6732 may_set_selection();
6733# endif
6734
6735 /* ':let @" = "val"' should change the meaning of the "" register */
6736 if (name != '"')
6737 y_previous = old_y_previous;
6738 y_current = old_y_current;
6739}
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741/*
6742 * Store string "str" in register "name".
6743 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6744 * If "must_append" is TRUE, always append to the register. Otherwise append
6745 * if "name" is an uppercase letter.
6746 * Note: "maxlen" and "must_append" don't work for the "/" register.
6747 * Careful: 'str' is modified, you may have to use a copy!
6748 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6749 */
6750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751write_reg_contents(
6752 int name,
6753 char_u *str,
6754 int maxlen,
6755 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6758}
6759
6760 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761write_reg_contents_lst(
6762 int name,
6763 char_u **strings,
6764 int maxlen UNUSED,
6765 int must_append,
6766 int yank_type,
6767 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006768{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006769 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006770
6771 if (name == '/'
6772#ifdef FEAT_EVAL
6773 || name == '='
6774#endif
6775 )
6776 {
6777 char_u *s;
6778
6779 if (strings[0] == NULL)
6780 s = (char_u *)"";
6781 else if (strings[1] != NULL)
6782 {
6783 EMSG(_("E883: search pattern and expression register may not "
6784 "contain two or more lines"));
6785 return;
6786 }
6787 else
6788 s = strings[0];
6789 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6790 return;
6791 }
6792
6793 if (name == '_') /* black hole: nothing to do */
6794 return;
6795
6796 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6797 &yank_type) == FAIL)
6798 return;
6799
6800 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6801
6802 finish_write_reg(name, old_y_previous, old_y_current);
6803}
6804
6805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006806write_reg_contents_ex(
6807 int name,
6808 char_u *str,
6809 int maxlen,
6810 int must_append,
6811 int yank_type,
6812 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006814 yankreg_T *old_y_previous, *old_y_current;
6815 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaare7566042005-06-17 22:00:15 +00006817 if (maxlen >= 0)
6818 len = maxlen;
6819 else
6820 len = (long)STRLEN(str);
6821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 /* Special case: '/' search pattern */
6823 if (name == '/')
6824 {
6825 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6826 return;
6827 }
6828
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006829 if (name == '#')
6830 {
6831 buf_T *buf;
6832
6833 if (VIM_ISDIGIT(*str))
6834 {
6835 int num = atoi((char *)str);
6836
6837 buf = buflist_findnr(num);
6838 if (buf == NULL)
6839 EMSGN(_(e_nobufnr), (long)num);
6840 }
6841 else
6842 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6843 TRUE, FALSE, FALSE));
6844 if (buf == NULL)
6845 return;
6846 curwin->w_alt_fnum = buf->b_fnum;
6847 return;
6848 }
6849
Bram Moolenaare7566042005-06-17 22:00:15 +00006850#ifdef FEAT_EVAL
6851 if (name == '=')
6852 {
6853 char_u *p, *s;
6854
6855 p = vim_strnsave(str, (int)len);
6856 if (p == NULL)
6857 return;
6858 if (must_append)
6859 {
6860 s = concat_str(get_expr_line_src(), p);
6861 vim_free(p);
6862 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006863 }
6864 set_expr_line(p);
6865 return;
6866 }
6867#endif
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (name == '_') /* black hole: nothing to do */
6870 return;
6871
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006872 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6873 &yank_type) == FAIL)
6874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006876 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006878 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879}
6880#endif /* FEAT_EVAL */
6881
6882#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6883/*
6884 * Put a string into a register. When the register is not empty, the string
6885 * is appended.
6886 */
6887 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006888str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006889 yankreg_T *y_ptr, /* pointer to yank register */
6890 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6891 char_u *str, /* string to put in register */
6892 long len, /* length of string */
6893 long blocklen, /* width of Visual block */
6894 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006896 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 int lnum;
6898 long start;
6899 long i;
6900 int extra;
6901 int newlines; /* number of lines added */
6902 int extraline = 0; /* extra line at the end */
6903 int append = FALSE; /* append to last line in register */
6904 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006905 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006909 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 y_ptr->y_size = 0;
6911
Bram Moolenaard44347f2011-06-19 01:14:29 +02006912 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006913 type = ((str_list || (len > 0 && (str[len - 1] == NL
6914 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006915 ? MLINE : MCHAR);
6916 else
6917 type = yank_type;
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /*
6920 * Count the number of lines within the string
6921 */
6922 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006923 if (str_list)
6924 {
6925 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006928 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006930 for (i = 0; i < len; i++)
6931 if (str[i] == '\n')
6932 ++newlines;
6933 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6934 {
6935 extraline = 1;
6936 ++newlines; /* count extra newline at the end */
6937 }
6938 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6939 {
6940 append = TRUE;
6941 --newlines; /* uncount newline when appending first line */
6942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006945 /* Without any lines make the register empty. */
6946 if (y_ptr->y_size + newlines == 0)
6947 {
6948 vim_free(y_ptr->y_array);
6949 y_ptr->y_array = NULL;
6950 return;
6951 }
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 /*
6954 * Allocate an array to hold the pointers to the new register lines.
6955 * If the register was not empty, move the existing lines to the new array.
6956 */
6957 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6958 * sizeof(char_u *), TRUE);
6959 if (pp == NULL) /* out of memory */
6960 return;
6961 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6962 pp[lnum] = y_ptr->y_array[lnum];
6963 vim_free(y_ptr->y_array);
6964 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 /*
6968 * Find the end of each line and save it into the array.
6969 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006970 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006972 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6973 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006974 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006975 pp[lnum] = vim_strnsave(*ss, i);
6976 if (i > maxlen)
6977 maxlen = i;
6978 }
6979 }
6980 else
6981 {
6982 for (start = 0; start < len + extraline; start += i + 1)
6983 {
6984 for (i = start; i < len; ++i) /* find the end of the line */
6985 if (str[i] == '\n')
6986 break;
6987 i -= start; /* i is now length of line */
6988 if (i > maxlen)
6989 maxlen = i;
6990 if (append)
6991 {
6992 --lnum;
6993 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6994 }
6995 else
6996 extra = 0;
6997 s = alloc((unsigned)(i + extra + 1));
6998 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007000 if (extra)
7001 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7002 if (append)
7003 vim_free(y_ptr->y_array[lnum]);
7004 if (i)
7005 mch_memmove(s + extra, str + start, (size_t)i);
7006 extra += i;
7007 s[extra] = NUL;
7008 y_ptr->y_array[lnum++] = s;
7009 while (--extra >= 0)
7010 {
7011 if (*s == NUL)
7012 *s = '\n'; /* replace NUL with newline */
7013 ++s;
7014 }
7015 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018 y_ptr->y_type = type;
7019 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (type == MBLOCK)
7021 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7022 else
7023 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007024#ifdef FEAT_VIMINFO
7025 y_ptr->y_time_set = vim_time();
7026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027}
7028#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7029
7030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007031clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 vim_memset(oap, 0, sizeof(oparg_T));
7034}
7035
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007036static long line_count_info(char_u *line, long *wc, long *cc, long limit, int eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007039 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 *
7041 * "Words" are counted by looking for boundaries between non-space and
7042 * space characters. (it seems to produce results that match 'wc'.)
7043 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007044 * Return value is byte count; word count for the line is added to "*wc".
7045 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 *
7047 * The function will only examine the first "limit" characters in the
7048 * line, stopping if it encounters an end-of-line (NUL byte). In that
7049 * case, eol_size will be added to the character count to account for
7050 * the size of the EOL character.
7051 */
7052 static long
Bram Moolenaar9b578142016-01-30 19:39:49 +01007053line_count_info(
7054 char_u *line,
7055 long *wc,
7056 long *cc,
7057 long limit,
7058 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
Bram Moolenaar7c626922005-02-07 22:01:03 +00007060 long i;
7061 long words = 0;
7062 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 int is_word = 0;
7064
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007065 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 {
7067 if (is_word)
7068 {
7069 if (vim_isspace(line[i]))
7070 {
7071 words++;
7072 is_word = 0;
7073 }
7074 }
7075 else if (!vim_isspace(line[i]))
7076 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007077 ++chars;
7078#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007079 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007080#else
7081 ++i;
7082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
7084
7085 if (is_word)
7086 words++;
7087 *wc += words;
7088
7089 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007090 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007093 chars += eol_size;
7094 }
7095 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 return i;
7097}
7098
7099/*
7100 * Give some info about the position of the cursor (for "g CTRL-G").
7101 * In Visual mode, give some info about the selected region. (In this case,
7102 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007103 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 */
7105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007106cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007109 char_u buf1[50];
7110 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007112 long byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007113#ifdef FEAT_MBYTE
Bram Moolenaared767a22016-01-03 22:49:16 +01007114 long bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007115#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007116 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 long char_count = 0;
7118 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 long word_count = 0;
7120 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007121 int eol_size;
7122 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 long line_count_selected = 0;
7124 pos_T min_pos, max_pos;
7125 oparg_T oparg;
7126 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 /*
7129 * Compute the length of the file in characters.
7130 */
7131 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7132 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007133 if (dict == NULL)
7134 {
7135 MSG(_(no_lines_msg));
7136 return;
7137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139 else
7140 {
7141 if (get_fileformat(curbuf) == EOL_DOS)
7142 eol_size = 2;
7143 else
7144 eol_size = 1;
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (VIsual_active)
7147 {
7148 if (lt(VIsual, curwin->w_cursor))
7149 {
7150 min_pos = VIsual;
7151 max_pos = curwin->w_cursor;
7152 }
7153 else
7154 {
7155 min_pos = curwin->w_cursor;
7156 max_pos = VIsual;
7157 }
7158 if (*p_sel == 'e' && max_pos.col > 0)
7159 --max_pos.col;
7160
7161 if (VIsual_mode == Ctrl_V)
7162 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007163#ifdef FEAT_LINEBREAK
7164 char_u * saved_sbr = p_sbr;
7165
7166 /* Make 'sbr' empty for a moment to get the correct size. */
7167 p_sbr = empty_option;
7168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 oparg.is_VIsual = 1;
7170 oparg.block_mode = TRUE;
7171 oparg.op_type = OP_NOP;
7172 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007173 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007174#ifdef FEAT_LINEBREAK
7175 p_sbr = saved_sbr;
7176#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007177 if (curwin->w_curswant == MAXCOL)
7178 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 /* Swap the start, end vcol if needed */
7180 if (oparg.end_vcol < oparg.start_vcol)
7181 {
7182 oparg.end_vcol += oparg.start_vcol;
7183 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7184 oparg.end_vcol -= oparg.start_vcol;
7185 }
7186 }
7187 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7191 {
7192 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007193 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 ui_breakcheck();
7196 if (got_int)
7197 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007198 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 /* Do extra processing for VIsual mode. */
7202 if (VIsual_active
7203 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7204 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007205 char_u *s = NULL;
7206 long len = 0L;
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 switch (VIsual_mode)
7209 {
7210 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007211#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007215#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007217#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007218 s = bd.textstart;
7219 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 break;
7221 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007222 s = ml_get(lnum);
7223 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 break;
7225 case 'v':
7226 {
7227 colnr_T start_col = (lnum == min_pos.lnum)
7228 ? min_pos.col : 0;
7229 colnr_T end_col = (lnum == max_pos.lnum)
7230 ? max_pos.col - start_col + 1 : MAXCOL;
7231
Bram Moolenaardef9e822004-12-31 20:58:58 +00007232 s = ml_get(lnum) + start_col;
7233 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235 break;
7236 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007237 if (s != NULL)
7238 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007239 byte_count_cursor += line_count_info(s, &word_count_cursor,
7240 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007241 if (lnum == curbuf->b_ml.ml_line_count
7242 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007243 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007244 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007245 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 }
7248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
7250 /* In non-visual mode, check for the line the cursor is on */
7251 if (lnum == curwin->w_cursor.lnum)
7252 {
7253 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007254 char_count_cursor += char_count;
7255 byte_count_cursor = byte_count +
7256 line_count_info(ml_get(lnum),
7257 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 (long)(curwin->w_cursor.col + 1), eol_size);
7259 }
7260 }
7261 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007262 byte_count += line_count_info(ml_get(lnum), &word_count,
7263 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265
7266 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007267 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007268 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
Bram Moolenaared767a22016-01-03 22:49:16 +01007270 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007272 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007274 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7275 {
7276 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7277 &max_pos.col);
7278 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7279 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7280 }
7281 else
7282 buf1[0] = NUL;
7283
7284 if (char_count_cursor == byte_count_cursor
7285 && char_count == byte_count)
7286 vim_snprintf((char *)IObuff, IOSIZE,
7287 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
7288 buf1, line_count_selected,
7289 (long)curbuf->b_ml.ml_line_count,
7290 word_count_cursor, word_count,
7291 byte_count_cursor, byte_count);
7292 else
7293 vim_snprintf((char *)IObuff, IOSIZE,
7294 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
7295 buf1, line_count_selected,
7296 (long)curbuf->b_ml.ml_line_count,
7297 word_count_cursor, word_count,
7298 char_count_cursor, char_count,
7299 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
7301 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007302 {
7303 p = ml_get_curline();
7304 validate_virtcol();
7305 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7306 (int)curwin->w_virtcol + 1);
7307 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7308 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309
Bram Moolenaared767a22016-01-03 22:49:16 +01007310 if (char_count_cursor == byte_count_cursor
7311 && char_count == byte_count)
7312 vim_snprintf((char *)IObuff, IOSIZE,
7313 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
7314 (char *)buf1, (char *)buf2,
7315 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 (long)curbuf->b_ml.ml_line_count,
7317 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007318 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007319 else
7320 vim_snprintf((char *)IObuff, IOSIZE,
7321 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
7322 (char *)buf1, (char *)buf2,
7323 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007324 (long)curbuf->b_ml.ml_line_count,
7325 word_count_cursor, word_count,
7326 char_count_cursor, char_count,
7327 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007328 }
7329 }
7330
Bram Moolenaared767a22016-01-03 22:49:16 +01007331#ifdef FEAT_MBYTE
7332 bom_count = bomb_size();
7333 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007334 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7335 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007336#endif
7337 if (dict == NULL)
7338 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007339 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007340 p = p_shm;
7341 p_shm = (char_u *)"";
7342 msg(IObuff);
7343 p_shm = p;
7344 }
7345 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007346#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007347 if (dict != NULL)
7348 {
7349 dict_add_nr_str(dict, "words", (long)word_count, NULL);
7350 dict_add_nr_str(dict, "chars", (long)char_count, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007351 dict_add_nr_str(dict, "bytes", (long)byte_count
7352# ifdef FEAT_MBYTE
7353 + bom_count
7354# endif
7355 , NULL);
7356 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7357 (long)byte_count_cursor, NULL);
7358 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7359 (long)char_count_cursor, NULL);
7360 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
7361 (long)word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364}