blob: 2c80c592d942b29e00ad03f227c68c9da3e71c1b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100116static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
117static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200119static 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 +0000120#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100121static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static int same_leader(linenr_T lnum, int, char_u *, int, char_u *);
124static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
128
129/*
130 * The names of operators.
131 * IMPORTANT: Index must correspond with defines in vim.h!!!
132 * The third field indicates whether the operator always works on lines.
133 */
134static char opchars[][3] =
135{
136 {NUL, NUL, FALSE}, /* OP_NOP */
137 {'d', NUL, FALSE}, /* OP_DELETE */
138 {'y', NUL, FALSE}, /* OP_YANK */
139 {'c', NUL, FALSE}, /* OP_CHANGE */
140 {'<', NUL, TRUE}, /* OP_LSHIFT */
141 {'>', NUL, TRUE}, /* OP_RSHIFT */
142 {'!', NUL, TRUE}, /* OP_FILTER */
143 {'g', '~', FALSE}, /* OP_TILDE */
144 {'=', NUL, TRUE}, /* OP_INDENT */
145 {'g', 'q', TRUE}, /* OP_FORMAT */
146 {':', NUL, TRUE}, /* OP_COLON */
147 {'g', 'U', FALSE}, /* OP_UPPER */
148 {'g', 'u', FALSE}, /* OP_LOWER */
149 {'J', NUL, TRUE}, /* DO_JOIN */
150 {'g', 'J', TRUE}, /* DO_JOIN_NS */
151 {'g', '?', FALSE}, /* OP_ROT13 */
152 {'r', NUL, FALSE}, /* OP_REPLACE */
153 {'I', NUL, FALSE}, /* OP_INSERT */
154 {'A', NUL, FALSE}, /* OP_APPEND */
155 {'z', 'f', TRUE}, /* OP_FOLD */
156 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
157 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
158 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
159 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
160 {'z', 'd', TRUE}, /* OP_FOLDDEL */
161 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
162 {'g', 'w', TRUE}, /* OP_FORMAT2 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000163 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaard79e5502016-01-10 22:13:02 +0100164 {Ctrl_A, NUL, FALSE}, /* OP_NR_ADD */
165 {Ctrl_X, NUL, FALSE}, /* OP_NR_SUB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166};
167
168/*
169 * Translate a command name into an operator type.
170 * Must only be called with a valid operator name!
171 */
172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100173get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174{
175 int i;
176
177 if (char1 == 'r') /* ignore second character */
178 return OP_REPLACE;
179 if (char1 == '~') /* when tilde is an operator */
180 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100181 if (char1 == 'g' && char2 == Ctrl_A) /* add */
182 return OP_NR_ADD;
183 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
184 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (opchars[i][0] == char1 && opchars[i][1] == char2)
188 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100189 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
190 {
191 internal_error("get_op_type()");
192 break;
193 }
194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 return i;
196}
197
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198/*
199 * Return TRUE if operator "op" always works on whole lines.
200 */
201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100202op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 return opchars[op][2];
205}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
208 * Get first operator command character.
209 * Returns 'g' or 'z' if there is another command character.
210 */
211 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100212get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 return opchars[optype][0];
215}
216
217/*
218 * Get second operator command character.
219 */
220 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100221get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 return opchars[optype][1];
224}
225
226/*
227 * op_shift - handle a shift operation
228 */
229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100230op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 long i;
233 int first_char;
234 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
237 if (u_save((linenr_T)(oap->start.lnum - 1),
238 (linenr_T)(oap->end.lnum + 1)) == FAIL)
239 return;
240
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244 for (i = oap->line_count; --i >= 0; )
245 {
246 first_char = *ml_get_curline();
247 if (first_char == NUL) /* empty line */
248 curwin->w_cursor.col = 0;
249#ifdef FEAT_VISUALEXTRA
250 else if (oap->block_mode)
251 shift_block(oap, amount);
252#endif
253 else
254 /* Move the line right if it doesn't start with '#', 'smartindent'
255 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
256#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
257 if (first_char != '#' || !preprocs_left())
258#endif
259 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000260 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 }
262 ++curwin->w_cursor.lnum;
263 }
264
265 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (oap->block_mode)
267 {
268 curwin->w_cursor.lnum = oap->start.lnum;
269 curwin->w_cursor.col = block_col;
270 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100271 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 {
273 curwin->w_cursor.lnum = oap->start.lnum;
274 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
275 }
276 else
277 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
278
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100279#ifdef FEAT_FOLDING
280 /* The cursor line is not in a closed fold */
281 foldOpenCursor();
282#endif
283
284
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 if (oap->line_count > p_report)
286 {
287 if (oap->op_type == OP_RSHIFT)
288 s = (char_u *)">";
289 else
290 s = (char_u *)"<";
291 if (oap->line_count == 1)
292 {
293 if (amount == 1)
294 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
295 else
296 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
297 }
298 else
299 {
300 if (amount == 1)
301 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
302 oap->line_count, s);
303 else
304 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
305 oap->line_count, s, amount);
306 }
307 msg(IObuff);
308 }
309
310 /*
311 * Set "'[" and "']" marks.
312 */
313 curbuf->b_op_start = oap->start;
314 curbuf->b_op_end.lnum = oap->end.lnum;
315 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
316 if (curbuf->b_op_end.col > 0)
317 --curbuf->b_op_end.col;
318}
319
320/*
321 * shift the current line one shiftwidth left (if left != 0) or right
322 * leaves cursor on first blank in the line
323 */
324 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100325shift_line(
326 int left,
327 int round,
328 int amount,
329 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int count;
332 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100333 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335 count = get_indent(); /* get current indent */
336
337 if (round) /* round off indent */
338 {
339 i = count / p_sw; /* number of p_sw rounded down */
340 j = count % p_sw; /* extra spaces */
341 if (j && left) /* first remove extra spaces */
342 --amount;
343 if (left)
344 {
345 i -= amount;
346 if (i < 0)
347 i = 0;
348 }
349 else
350 i += amount;
351 count = i * p_sw;
352 }
353 else /* original vi indent */
354 {
355 if (left)
356 {
357 count -= p_sw * amount;
358 if (count < 0)
359 count = 0;
360 }
361 else
362 count += p_sw * amount;
363 }
364
365 /* Set new indent */
366#ifdef FEAT_VREPLACE
367 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000368 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 else
370#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000371 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372}
373
374#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
375/*
376 * Shift one line of the current block one shiftwidth right or left.
377 * Leaves cursor on first character in block.
378 */
379 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100380shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
382 int left = (oap->op_type == OP_LSHIFT);
383 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000384 int total;
385 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100387 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000391 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 int i = 0, j = 0;
393 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#ifdef FEAT_RIGHTLEFT
395 int old_p_ri = p_ri;
396
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200397 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#endif
399
400 State = INSERT; /* don't want REPLACE for State */
401 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
402 if (bd.is_short)
403 return;
404
405 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200406 total = (int)((unsigned)amount * (unsigned)p_sw);
407 if ((total / p_sw) != amount)
408 return; /* multiplication overflow */
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 oldp = ml_get_curline();
411
412 if (!left)
413 {
414 /*
415 * 1. Get start vcol
416 * 2. Total ws vcols
417 * 3. Divvy into TABs & spp
418 * 4. Construct new string
419 */
420 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
421 ws_vcol = bd.start_vcol - bd.pre_whitesp;
422 if (bd.startspaces)
423 {
424#ifdef FEAT_MBYTE
425 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100426 {
427 if ((*mb_ptr2len)(bd.textstart) == 1)
428 ++bd.textstart;
429 else
430 {
431 ws_vcol = 0;
432 bd.startspaces = 0;
433 }
434 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000437 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100439 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200441 /* TODO: is passing bd.textstart for start of the line OK? */
442 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
443 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 total += incr;
445 bd.start_vcol += incr;
446 }
447 /* OK, now total=all the VWS reqd, and textstart points at the 1st
448 * non-ws char in the block. */
449 if (!curbuf->b_p_et)
450 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
451 if (i)
452 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
453 else
454 j = total;
455 /* if we're splitting a TAB, allow for it */
456 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
457 len = (int)STRLEN(bd.textstart) + 1;
458 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
459 if (newp == NULL)
460 return;
461 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
462 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200463 vim_memset(newp + bd.textcol, TAB, (size_t)i);
464 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 /* the end */
466 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
467 }
468 else /* left */
469 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000470 colnr_T destination_col; /* column to which text in block will
471 be shifted */
472 char_u *verbatim_copy_end; /* end of the part of the line which is
473 copied verbatim */
474 colnr_T verbatim_copy_width;/* the (displayed) width of this part
475 of line */
476 unsigned fill; /* nr of spaces that replace a TAB */
477 unsigned new_line_len; /* the length of the line after the
478 block shift */
479 size_t block_space_width;
480 size_t shift_amount;
481 char_u *non_white = bd.textstart;
482 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000484 /*
485 * Firstly, let's find the first non-whitespace character that is
486 * displayed after the block's start column and the character's column
487 * number. Also, let's calculate the width of all the whitespace
488 * characters that are displayed in the block and precede the searched
489 * non-whitespace character.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
493 * the part of which is displayed at the block's beginning. Let's start
494 * searching from the next character. */
495 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100496 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497
498 /* The character's column is in "bd.start_vcol". */
499 non_white_col = bd.start_vcol;
500
Bram Moolenaar1c465442017-03-12 20:10:05 +0100501 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200503 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000504 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507 block_space_width = non_white_col - oap->start_vcol;
508 /* We will shift by "total" or "block_space_width", whichever is less.
509 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000510 shift_amount = (block_space_width < (size_t)total
511 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000512
513 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000514 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515
516 /* Now let's find out how much of the beginning of the line we can
517 * reuse without modification. */
518 verbatim_copy_end = bd.textstart;
519 verbatim_copy_width = bd.start_vcol;
520
521 /* If "bd.startspaces" is set, "bd.textstart" points to the character
522 * preceding the block. We have to subtract its width to obtain its
523 * column number. */
524 if (bd.startspaces)
525 verbatim_copy_width -= bd.start_char_vcols;
526 while (verbatim_copy_width < destination_col)
527 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200528 char_u *line = verbatim_copy_end;
529
530 /* TODO: is passing verbatim_copy_end for start of the line OK? */
531 incr = lbr_chartabsize(line, verbatim_copy_end,
532 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000533 if (verbatim_copy_width + incr > destination_col)
534 break;
535 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100536 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000537 }
538
539 /* If "destination_col" is different from the width of the initial
540 * part of the line that will be copied, it means we encountered a tab
541 * character, which we will have to partly replace with spaces. */
542 fill = destination_col - verbatim_copy_width;
543
544 /* The replacement line will consist of:
545 * - the beginning of the original line up to "verbatim_copy_end",
546 * - "fill" number of spaces,
547 * - the rest of the line, pointed to by non_white. */
548 new_line_len = (unsigned)(verbatim_copy_end - oldp)
549 + fill
550 + (unsigned)STRLEN(non_white) + 1;
551
552 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (newp == NULL)
554 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000555 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200556 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000557 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
559 /* replace the line */
560 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
561 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
562 State = oldstate;
563 curwin->w_cursor.col = oldcol;
564#ifdef FEAT_RIGHTLEFT
565 p_ri = old_p_ri;
566#endif
567}
568#endif
569
570#ifdef FEAT_VISUALEXTRA
571/*
572 * Insert string "s" (b_insert ? before : after) block :AKelly
573 * Caller must prepare for undo.
574 */
575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100576block_insert(
577 oparg_T *oap,
578 char_u *s,
579 int b_insert,
580 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 int p_ts;
583 int count = 0; /* extra spaces to replace a cut TAB */
584 int spaces = 0; /* non-zero if cutting a TAB */
585 colnr_T offset; /* pointer along new line */
586 unsigned s_len; /* STRLEN(s) */
587 char_u *newp, *oldp; /* new, old lines */
588 linenr_T lnum; /* loop var */
589 int oldstate = State;
590
591 State = INSERT; /* don't want REPLACE for State */
592 s_len = (unsigned)STRLEN(s);
593
594 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
595 {
596 block_prep(oap, bdp, lnum, TRUE);
597 if (bdp->is_short && b_insert)
598 continue; /* OP_INSERT, line ends before block start */
599
600 oldp = ml_get(lnum);
601
602 if (b_insert)
603 {
604 p_ts = bdp->start_char_vcols;
605 spaces = bdp->startspaces;
606 if (spaces != 0)
607 count = p_ts - 1; /* we're cutting a TAB */
608 offset = bdp->textcol;
609 }
610 else /* append */
611 {
612 p_ts = bdp->end_char_vcols;
613 if (!bdp->is_short) /* spaces = padding after block */
614 {
615 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
616 if (spaces != 0)
617 count = p_ts - 1; /* we're cutting a TAB */
618 offset = bdp->textcol + bdp->textlen - (spaces != 0);
619 }
620 else /* spaces = padding to block edge */
621 {
622 /* if $ used, just append to EOL (ie spaces==0) */
623 if (!bdp->is_MAX)
624 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
625 count = spaces;
626 offset = bdp->textcol + bdp->textlen;
627 }
628 }
629
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200630#ifdef FEAT_MBYTE
631 if (has_mbyte && spaces > 0)
632 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100633 int off;
634
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635 /* Avoid starting halfway a multi-byte character. */
636 if (b_insert)
637 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100638 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200639 }
640 else
641 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100642 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200643 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200644 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100645 spaces -= off;
646 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200647 }
648#endif
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
651 if (newp == NULL)
652 continue;
653
654 /* copy up to shifted part */
655 mch_memmove(newp, oldp, (size_t)(offset));
656 oldp += offset;
657
658 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200659 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 /* copy the new text */
662 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
663 offset += s_len;
664
665 if (spaces && !bdp->is_short)
666 {
667 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200668 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /* We're splitting a TAB, don't copy it. */
670 oldp++;
671 /* We allowed for that TAB, remember this now */
672 count++;
673 }
674
675 if (spaces > 0)
676 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000677 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 ml_replace(lnum, newp, FALSE);
680
681 if (lnum == oap->end.lnum)
682 {
683 /* Set "']" mark to the end of the block instead of the end of
684 * the insert in the first line. */
685 curbuf->b_op_end.lnum = oap->end.lnum;
686 curbuf->b_op_end.col = offset;
687 }
688 } /* for all lnum */
689
690 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
691
692 State = oldstate;
693}
694#endif
695
696#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
697/*
698 * op_reindent - handle reindenting a block of lines.
699 */
700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100701op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
703 long i;
704 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200705 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 linenr_T first_changed = 0;
707 linenr_T last_changed = 0;
708 linenr_T start_lnum = curwin->w_cursor.lnum;
709
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000710 /* Don't even try when 'modifiable' is off. */
711 if (!curbuf->b_p_ma)
712 {
713 EMSG(_(e_modifiable));
714 return;
715 }
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 for (i = oap->line_count; --i >= 0 && !got_int; )
718 {
719 /* it's a slow thing to do, so give feedback so there's no worry that
720 * the computer's just hung. */
721
722 if (i > 1
723 && (i % 50 == 0 || i == oap->line_count - 1)
724 && oap->line_count > p_report)
725 smsg((char_u *)_("%ld lines to indent... "), i);
726
727 /*
728 * Be vi-compatible: For lisp indenting the first line is not
729 * indented, unless there is only one line.
730 */
731#ifdef FEAT_LISP
732 if (i != oap->line_count - 1 || oap->line_count == 1
733 || how != get_lisp_indent)
734#endif
735 {
736 l = skipwhite(ml_get_curline());
737 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200738 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200740 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200742 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 /* did change the indent, call changed_lines() later */
745 if (first_changed == 0)
746 first_changed = curwin->w_cursor.lnum;
747 last_changed = curwin->w_cursor.lnum;
748 }
749 }
750 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000751 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753
754 /* put cursor on first non-blank of indented line */
755 curwin->w_cursor.lnum = start_lnum;
756 beginline(BL_SOL | BL_FIX);
757
758 /* Mark changed lines so that they will be redrawn. When Visual
759 * highlighting was present, need to continue until the last line. When
760 * there is no change still need to remove the Visual highlighting. */
761 if (last_changed != 0)
762 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 else if (oap->is_VIsual)
766 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 if (oap->line_count > p_report)
769 {
770 i = oap->line_count - (i + 1);
771 if (i == 1)
772 MSG(_("1 line indented "));
773 else
774 smsg((char_u *)_("%ld lines indented "), i);
775 }
776 /* set '[ and '] marks */
777 curbuf->b_op_start = oap->start;
778 curbuf->b_op_end = oap->end;
779}
780#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
781
782#if defined(FEAT_EVAL) || defined(PROTO)
783/*
784 * Keep the last expression line here, for repeating.
785 */
786static char_u *expr_line = NULL;
787
788/*
789 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
790 * Returns '=' when OK, NUL otherwise.
791 */
792 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 char_u *new_line;
796
797 new_line = getcmdline('=', 0L, 0);
798 if (new_line == NULL)
799 return NUL;
800 if (*new_line == NUL) /* use previous line */
801 vim_free(new_line);
802 else
803 set_expr_line(new_line);
804 return '=';
805}
806
807/*
808 * Set the expression for the '=' register.
809 * Argument must be an allocated string.
810 */
811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 vim_free(expr_line);
815 expr_line = new_line;
816}
817
818/*
819 * Get the result of the '=' register expression.
820 * Returns a pointer to allocated memory, or NULL for failure.
821 */
822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 char_u *expr_copy;
826 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000827 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 if (expr_line == NULL)
830 return NULL;
831
832 /* Make a copy of the expression, because evaluating it may cause it to be
833 * changed. */
834 expr_copy = vim_strsave(expr_line);
835 if (expr_copy == NULL)
836 return NULL;
837
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000838 /* When we are invoked recursively limit the evaluation to 10 levels.
839 * Then return the string as-is. */
840 if (nested >= 10)
841 return expr_copy;
842
843 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000845 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 vim_free(expr_copy);
847 return rv;
848}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000849
850/*
851 * Get the '=' register expression itself, without evaluating it.
852 */
853 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000855{
856 if (expr_line == NULL)
857 return NULL;
858 return vim_strsave(expr_line);
859}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif /* FEAT_EVAL */
861
862/*
863 * Check if 'regname' is a valid name of a yank register.
864 * Note: There is no check for 0 (default register), caller should do this
865 */
866 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867valid_yank_reg(
868 int regname,
869 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 if ( (regname > 0 && ASCII_ISALNUM(regname))
872 || (!writing && vim_strchr((char_u *)
873#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100874 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100876 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100879 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 || regname == '"'
881 || regname == '-'
882 || regname == '_'
883#ifdef FEAT_CLIPBOARD
884 || regname == '*'
885 || regname == '+'
886#endif
887#ifdef FEAT_DND
888 || (!writing && regname == '~')
889#endif
890 )
891 return TRUE;
892 return FALSE;
893}
894
895/*
896 * Set y_current and y_append, according to the value of "regname".
897 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000898 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *
900 * If regname is 0 and writing, use register 0
901 * If regname is 0 and reading, use previous register
902 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100904get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 int i;
907
908 y_append = FALSE;
909 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
910 {
911 y_current = y_previous;
912 return;
913 }
914 i = regname;
915 if (VIM_ISDIGIT(i))
916 i -= '0';
917 else if (ASCII_ISLOWER(i))
918 i = CharOrdLow(i) + 10;
919 else if (ASCII_ISUPPER(i))
920 {
921 i = CharOrdUp(i) + 10;
922 y_append = TRUE;
923 }
924 else if (regname == '-')
925 i = DELETION_REGISTER;
926#ifdef FEAT_CLIPBOARD
927 /* When selection is not available, use register 0 instead of '*' */
928 else if (clip_star.available && regname == '*')
929 i = STAR_REGISTER;
930 /* When clipboard is not available, use register 0 instead of '+' */
931 else if (clip_plus.available && regname == '+')
932 i = PLUS_REGISTER;
933#endif
934#ifdef FEAT_DND
935 else if (!writing && regname == '~')
936 i = TILDE_REGISTER;
937#endif
938 else /* not 0-9, a-z, A-Z or '-': use register 0 */
939 i = 0;
940 y_current = &(y_regs[i]);
941 if (writing) /* remember the register we write into for do_put() */
942 y_previous = y_current;
943}
944
Bram Moolenaar8299df92004-07-10 09:47:34 +0000945#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946/*
947 * When "regname" is a clipboard register, obtain the selection. If it's not
948 * available return zero, otherwise return "regname".
949 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100951may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 if (regname == '*')
954 {
955 if (!clip_star.available)
956 regname = 0;
957 else
958 clip_get_selection(&clip_star);
959 }
960 else if (regname == '+')
961 {
962 if (!clip_plus.available)
963 regname = 0;
964 else
965 clip_get_selection(&clip_plus);
966 }
967 return regname;
968}
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Obtain the contents of a "normal" register. The register is made empty.
973 * The returned pointer has allocated memory, use put_register() later.
974 */
975 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100976get_register(
977 int name,
978 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200980 yankreg_T *reg;
981 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983#ifdef FEAT_CLIPBOARD
984 /* When Visual area changed, may have to update selection. Obtain the
985 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000986 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200988 if (clip_isautosel_star())
989 clip_update_selection(&clip_star);
990 may_get_selection(name);
991 }
992 if (name == '+' && clip_plus.available)
993 {
994 if (clip_isautosel_plus())
995 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 may_get_selection(name);
997 }
998#endif
999
1000 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001001 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (reg != NULL)
1003 {
1004 *reg = *y_current;
1005 if (copy)
1006 {
1007 /* If we run out of memory some or all of the lines are empty. */
1008 if (reg->y_size == 0)
1009 reg->y_array = NULL;
1010 else
1011 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1012 * reg->y_size));
1013 if (reg->y_array != NULL)
1014 {
1015 for (i = 0; i < reg->y_size; ++i)
1016 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1017 }
1018 }
1019 else
1020 y_current->y_array = NULL;
1021 }
1022 return (void *)reg;
1023}
1024
1025/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001026 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
1028 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001029put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
1031 get_yank_register(name, 0);
1032 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001033 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001034 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001036#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /* Send text written to clipboard register to the clipboard. */
1038 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041
1042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001043free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001044{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001045 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001046
1047 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001048 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001049 free_yank_all();
1050 vim_free(reg);
1051 *y_current = tmp;
1052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
1054#if defined(FEAT_MOUSE) || defined(PROTO)
1055/*
1056 * return TRUE if the current yank register has type MLINE
1057 */
1058 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001059yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
1061 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1062 return FALSE;
1063 if (regname == '_') /* black hole is always empty */
1064 return FALSE;
1065 get_yank_register(regname, FALSE);
1066 return (y_current->y_type == MLINE);
1067}
1068#endif
1069
1070/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001073 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
1075 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaard55de222007-05-06 13:38:48 +00001078 char_u *p;
1079 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001080 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001081 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 if (Recording == FALSE) /* start recording */
1084 {
1085 /* registers 0-9, a-z and " are allowed */
1086 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1087 retval = FAIL;
1088 else
1089 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +01001090 Recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 showmode();
1092 regname = c;
1093 retval = OK;
1094 }
1095 }
1096 else /* stop recording */
1097 {
1098 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001099 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1100 * needs to be removed again to put it in a register. exec_reg then
1101 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 */
1103 Recording = FALSE;
1104 MSG("");
1105 p = get_recorded();
1106 if (p == NULL)
1107 retval = FAIL;
1108 else
1109 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001110 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1111 vim_unescape_csi(p);
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 /*
1114 * We don't want to change the default register here, so save and
1115 * restore the current register name.
1116 */
1117 old_y_previous = y_previous;
1118 old_y_current = y_current;
1119
1120 retval = stuff_yank(regname, p);
1121
1122 y_previous = old_y_previous;
1123 y_current = old_y_current;
1124 }
1125 }
1126 return retval;
1127}
1128
1129/*
1130 * Stuff string "p" into yank register "regname" as a single line (append if
1131 * uppercase). "p" must have been alloced.
1132 *
1133 * return FAIL for failure, OK otherwise
1134 */
1135 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001136stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 char_u *lp;
1139 char_u **pp;
1140
1141 /* check for read-only register */
1142 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1143 {
1144 vim_free(p);
1145 return FAIL;
1146 }
1147 if (regname == '_') /* black hole: don't do anything */
1148 {
1149 vim_free(p);
1150 return OK;
1151 }
1152 get_yank_register(regname, TRUE);
1153 if (y_append && y_current->y_array != NULL)
1154 {
1155 pp = &(y_current->y_array[y_current->y_size - 1]);
1156 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1157 if (lp == NULL)
1158 {
1159 vim_free(p);
1160 return FAIL;
1161 }
1162 STRCPY(lp, *pp);
1163 STRCAT(lp, p);
1164 vim_free(p);
1165 vim_free(*pp);
1166 *pp = lp;
1167 }
1168 else
1169 {
1170 free_yank_all();
1171 if ((y_current->y_array =
1172 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1173 {
1174 vim_free(p);
1175 return FAIL;
1176 }
1177 y_current->y_array[0] = p;
1178 y_current->y_size = 1;
1179 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001180#ifdef FEAT_VIMINFO
1181 y_current->y_time_set = vim_time();
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 return OK;
1185}
1186
Bram Moolenaar42b94362009-05-26 16:12:37 +00001187static int execreg_lastc = NUL;
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001190 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001192 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 */
1194 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001195do_execreg(
1196 int regname,
1197 int colon, /* insert ':' before each line */
1198 int addcr, /* always add '\n' to end of line */
1199 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 long i;
1202 char_u *p;
1203 int retval = OK;
1204 int remap;
1205
1206 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001207 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001208 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001209 {
1210 EMSG(_("E748: No previously used register"));
1211 return FAIL;
1212 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 /* check for valid regname */
1216 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001217 {
1218 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001220 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001221 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
1223#ifdef FEAT_CLIPBOARD
1224 regname = may_get_selection(regname);
1225#endif
1226
1227 if (regname == '_') /* black hole: don't stuff anything */
1228 return OK;
1229
1230#ifdef FEAT_CMDHIST
1231 if (regname == ':') /* use last command line */
1232 {
1233 if (last_cmdline == NULL)
1234 {
1235 EMSG(_(e_nolastcmd));
1236 return FAIL;
1237 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001238 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001239 /* Escape all control characters with a CTRL-V */
1240 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001241 (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 +00001242 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001243 {
1244 /* When in Visual mode "'<,'>" will be prepended to the command.
1245 * Remove it when it's already there. */
1246 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001247 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001248 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001249 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001250 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001251 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253#endif
1254#ifdef FEAT_EVAL
1255 else if (regname == '=')
1256 {
1257 p = get_expr_line();
1258 if (p == NULL)
1259 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001260 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 vim_free(p);
1262 }
1263#endif
1264 else if (regname == '.') /* use last inserted text */
1265 {
1266 p = get_last_insert_save();
1267 if (p == NULL)
1268 {
1269 EMSG(_(e_noinstext));
1270 return FAIL;
1271 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001272 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 vim_free(p);
1274 }
1275 else
1276 {
1277 get_yank_register(regname, FALSE);
1278 if (y_current->y_array == NULL)
1279 return FAIL;
1280
1281 /* Disallow remaping for ":@r". */
1282 remap = colon ? REMAP_NONE : REMAP_YES;
1283
1284 /*
1285 * Insert lines into typeahead buffer, from last one to first one.
1286 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001287 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 for (i = y_current->y_size; --i >= 0; )
1289 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001290 char_u *escaped;
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 /* insert NL between lines and after last line if type is MLINE */
1293 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1294 || addcr)
1295 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001296 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 return FAIL;
1298 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001299 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1300 if (escaped == NULL)
1301 return FAIL;
1302 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1303 vim_free(escaped);
1304 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001306 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 == FAIL)
1308 return FAIL;
1309 }
1310 Exec_reg = TRUE; /* disable the 'q' command */
1311 }
1312 return retval;
1313}
1314
1315/*
1316 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1317 * used only after other typeahead has been processed.
1318 */
1319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001320put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 char_u buf[3];
1323
1324 if (restart_edit != NUL)
1325 {
1326 if (restart_edit == 'V')
1327 {
1328 buf[0] = 'g';
1329 buf[1] = 'R';
1330 buf[2] = NUL;
1331 }
1332 else
1333 {
1334 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1335 buf[1] = NUL;
1336 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001337 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 restart_edit = NUL;
1339 }
1340}
1341
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001342/*
1343 * Insert register contents "s" into the typeahead buffer, so that it will be
1344 * executed again.
1345 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1346 * no remapping.
1347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001349put_in_typebuf(
1350 char_u *s,
1351 int esc,
1352 int colon, /* add ':' before the line */
1353 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 int retval = OK;
1356
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001357 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001359 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001361 {
1362 char_u *p;
1363
1364 if (esc)
1365 p = vim_strsave_escape_csi(s);
1366 else
1367 p = s;
1368 if (p == NULL)
1369 retval = FAIL;
1370 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001371 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1372 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001373 if (esc)
1374 vim_free(p);
1375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001377 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 return retval;
1379}
1380
1381/*
1382 * Insert a yank register: copy it into the Read buffer.
1383 * Used by CTRL-R command and middle mouse button in insert mode.
1384 *
1385 * return FAIL for failure, OK otherwise
1386 */
1387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001388insert_reg(
1389 int regname,
1390 int literally) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 long i;
1393 int retval = OK;
1394 char_u *arg;
1395 int allocated;
1396
1397 /*
1398 * It is possible to get into an endless loop by having CTRL-R a in
1399 * register a and then, in insert mode, doing CTRL-R a.
1400 * If you hit CTRL-C, the loop will be broken here.
1401 */
1402 ui_breakcheck();
1403 if (got_int)
1404 return FAIL;
1405
1406 /* check for valid regname */
1407 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1408 return FAIL;
1409
1410#ifdef FEAT_CLIPBOARD
1411 regname = may_get_selection(regname);
1412#endif
1413
1414 if (regname == '.') /* insert last inserted text */
1415 retval = stuff_inserted(NUL, 1L, TRUE);
1416 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1417 {
1418 if (arg == NULL)
1419 return FAIL;
1420 stuffescaped(arg, literally);
1421 if (allocated)
1422 vim_free(arg);
1423 }
1424 else /* name or number register */
1425 {
1426 get_yank_register(regname, FALSE);
1427 if (y_current->y_array == NULL)
1428 retval = FAIL;
1429 else
1430 {
1431 for (i = 0; i < y_current->y_size; ++i)
1432 {
1433 stuffescaped(y_current->y_array[i], literally);
1434 /*
1435 * Insert a newline between lines and after last line if
1436 * y_type is MLINE.
1437 */
1438 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1439 stuffcharReadbuff('\n');
1440 }
1441 }
1442 }
1443
1444 return retval;
1445}
1446
1447/*
1448 * Stuff a string into the typeahead buffer, such that edit() will insert it
1449 * literally ("literally" TRUE) or interpret is as typed characters.
1450 */
1451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001452stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 int c;
1455 char_u *start;
1456
1457 while (*arg != NUL)
1458 {
1459 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1460 * stuff K_SPECIAL to get the effect of a special key when "literally"
1461 * is TRUE. */
1462 start = arg;
1463 while ((*arg >= ' '
1464#ifndef EBCDIC
1465 && *arg < DEL /* EBCDIC: chars above space are normal */
1466#endif
1467 )
1468 || (*arg == K_SPECIAL && !literally))
1469 ++arg;
1470 if (arg > start)
1471 stuffReadbuffLen(start, (long)(arg - start));
1472
1473 /* stuff a single special character */
1474 if (*arg != NUL)
1475 {
1476#ifdef FEAT_MBYTE
1477 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001478 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 else
1480#endif
1481 c = *arg++;
1482 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1483 stuffcharReadbuff(Ctrl_V);
1484 stuffcharReadbuff(c);
1485 }
1486 }
1487}
1488
1489/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001490 * If "regname" is a special register, return TRUE and store a pointer to its
1491 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001493 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001494get_spec_reg(
1495 int regname,
1496 char_u **argp,
1497 int *allocated, /* return: TRUE when value was allocated */
1498 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 int cnt;
1501
1502 *argp = NULL;
1503 *allocated = FALSE;
1504 switch (regname)
1505 {
1506 case '%': /* file name */
1507 if (errmsg)
1508 check_fname(); /* will give emsg if not set */
1509 *argp = curbuf->b_fname;
1510 return TRUE;
1511
1512 case '#': /* alternate file name */
1513 *argp = getaltfname(errmsg); /* may give emsg if not set */
1514 return TRUE;
1515
1516#ifdef FEAT_EVAL
1517 case '=': /* result of expression */
1518 *argp = get_expr_line();
1519 *allocated = TRUE;
1520 return TRUE;
1521#endif
1522
1523 case ':': /* last command line */
1524 if (last_cmdline == NULL && errmsg)
1525 EMSG(_(e_nolastcmd));
1526 *argp = last_cmdline;
1527 return TRUE;
1528
1529 case '/': /* last search-pattern */
1530 if (last_search_pat() == NULL && errmsg)
1531 EMSG(_(e_noprevre));
1532 *argp = last_search_pat();
1533 return TRUE;
1534
1535 case '.': /* last inserted text */
1536 *argp = get_last_insert_save();
1537 *allocated = TRUE;
1538 if (*argp == NULL && errmsg)
1539 EMSG(_(e_noinstext));
1540 return TRUE;
1541
1542#ifdef FEAT_SEARCHPATH
1543 case Ctrl_F: /* Filename under cursor */
1544 case Ctrl_P: /* Path under cursor, expand via "path" */
1545 if (!errmsg)
1546 return FALSE;
1547 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001548 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 *allocated = TRUE;
1550 return TRUE;
1551#endif
1552
1553 case Ctrl_W: /* word under cursor */
1554 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1555 if (!errmsg)
1556 return FALSE;
1557 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1558 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1559 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1560 *allocated = TRUE;
1561 return TRUE;
1562
1563 case '_': /* black hole: always empty */
1564 *argp = (char_u *)"";
1565 return TRUE;
1566 }
1567
1568 return FALSE;
1569}
1570
1571/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001572 * Paste a yank register into the command line.
1573 * Only for non-special registers.
1574 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * insert_reg() can't be used here, because special characters from the
1576 * register contents will be interpreted as commands.
1577 *
1578 * return FAIL for failure, OK otherwise
1579 */
1580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001581cmdline_paste_reg(
1582 int regname,
1583 int literally, /* Insert text literally instead of "as typed" */
1584 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 get_yank_register(regname, FALSE);
1589 if (y_current->y_array == NULL)
1590 return FAIL;
1591
1592 for (i = 0; i < y_current->y_size; ++i)
1593 {
1594 cmdline_paste_str(y_current->y_array[i], literally);
1595
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001596 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001597 * Don't do this when "remcr" is TRUE. */
1598 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 cmdline_paste_str((char_u *)"\r", literally);
1600
1601 /* Check for CTRL-C, in case someone tries to paste a few thousand
1602 * lines and gets bored. */
1603 ui_breakcheck();
1604 if (got_int)
1605 return FAIL;
1606 }
1607 return OK;
1608}
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1611/*
1612 * Adjust the register name pointed to with "rp" for the clipboard being
1613 * used always and the clipboard being available.
1614 */
1615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001616adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001618 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1619 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001620 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1621 {
1622 if (clip_unnamed != 0)
1623 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001624 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001625 else
1626 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1627 ? '+' : '*';
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (!clip_star.available && *rp == '*')
1630 *rp = 0;
1631 if (!clip_plus.available && *rp == '+')
1632 *rp = 0;
1633}
1634#endif
1635
1636/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001637 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1638 */
1639 void
1640shift_delete_registers()
1641{
1642 int n;
1643
1644 y_current = &y_regs[9];
1645 free_yank_all(); /* free register nine */
1646 for (n = 9; n > 1; --n)
1647 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001648 y_current = &y_regs[1];
1649 if (!y_append)
1650 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001651 y_regs[1].y_array = NULL; /* set register one to empty */
1652}
1653
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001654#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001655 static void
1656yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1657{
1658 static int recursive = FALSE;
1659 dict_T *v_event;
1660 list_T *list;
1661 int n;
1662 char_u buf[NUMBUFLEN + 2];
1663 long reglen = 0;
1664
1665 if (recursive)
1666 return;
1667
1668 v_event = get_vim_var_dict(VV_EVENT);
1669
1670 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001671 if (list == NULL)
1672 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001673 for (n = 0; n < reg->y_size; n++)
1674 list_append_string(list, reg->y_array[n], -1);
1675 list->lv_lock = VAR_FIXED;
1676 dict_add_list(v_event, "regcontents", list);
1677
1678 buf[0] = (char_u)oap->regname;
1679 buf[1] = NUL;
1680 dict_add_nr_str(v_event, "regname", 0, buf);
1681
1682 buf[0] = get_op_char(oap->op_type);
1683 buf[1] = get_extra_op_char(oap->op_type);
1684 buf[2] = NUL;
1685 dict_add_nr_str(v_event, "operator", 0, buf);
1686
1687 buf[0] = NUL;
1688 buf[1] = NUL;
1689 switch (get_reg_type(oap->regname, &reglen))
1690 {
1691 case MLINE: buf[0] = 'V'; break;
1692 case MCHAR: buf[0] = 'v'; break;
1693 case MBLOCK:
1694 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1695 reglen + 1);
1696 break;
1697 }
1698 dict_add_nr_str(v_event, "regtype", 0, buf);
1699
1700 /* Lock the dictionary and its keys */
1701 dict_set_items_ro(v_event);
1702
1703 recursive = TRUE;
1704 textlock++;
1705 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1706 textlock--;
1707 recursive = FALSE;
1708
1709 /* Empty the dictionary, v:event is still valid */
1710 dict_free_contents(v_event);
1711 hash_init(&v_event->dv_hashtab);
1712}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001713#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001714
Bram Moolenaara1891842017-02-04 21:34:31 +01001715/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001716 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001718 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 */
1720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001721op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 int n;
1724 linenr_T lnum;
1725 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 char_u *newp, *oldp;
1727 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1729 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001730 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1733 return OK;
1734
1735 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1736 if (oap->empty)
1737 return u_save_cursor();
1738
1739 if (!curbuf->b_p_ma)
1740 {
1741 EMSG(_(e_modifiable));
1742 return FAIL;
1743 }
1744
1745#ifdef FEAT_CLIPBOARD
1746 adjust_clip_reg(&oap->regname);
1747#endif
1748
1749#ifdef FEAT_MBYTE
1750 if (has_mbyte)
1751 mb_adjust_opend(oap);
1752#endif
1753
Bram Moolenaard04b7502010-07-08 22:27:55 +02001754 /*
1755 * Imitate the strange Vi behaviour: If the delete spans more than one
1756 * line and motion_type == MCHAR and the result is a blank line, make the
1757 * delete linewise. Don't do this for the change command or Visual mode.
1758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001761 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001763 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 && oap->op_type == OP_DELETE)
1765 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001766 ptr = ml_get(oap->end.lnum) + oap->end.col;
1767 if (*ptr != NUL)
1768 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 ptr = skipwhite(ptr);
1770 if (*ptr == NUL && inindent(0))
1771 oap->motion_type = MLINE;
1772 }
1773
Bram Moolenaard04b7502010-07-08 22:27:55 +02001774 /*
1775 * Check for trying to delete (e.g. "D") in an empty line.
1776 * Note: For the change operator it is ok.
1777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if ( oap->motion_type == MCHAR
1779 && oap->line_count == 1
1780 && oap->op_type == OP_DELETE
1781 && *ml_get(oap->start.lnum) == NUL)
1782 {
1783 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001784 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 * 'cpoptions' (Vi compatible).
1786 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001787#ifdef FEAT_VIRTUALEDIT
1788 if (virtual_op)
1789 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1790 * marks as if it happened. */
1791 goto setmarks;
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1794 beep_flush();
1795 return OK;
1796 }
1797
Bram Moolenaard04b7502010-07-08 22:27:55 +02001798 /*
1799 * Do a yank of whatever we're about to delete.
1800 * If a yank register was specified, put the deleted text into that
1801 * register. For the black hole register '_' don't yank anything.
1802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (oap->regname != '_')
1804 {
1805 if (oap->regname != 0)
1806 {
1807 /* check for read-only register */
1808 if (!valid_yank_reg(oap->regname, TRUE))
1809 {
1810 beep_flush();
1811 return OK;
1812 }
1813 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1814 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1815 did_yank = TRUE;
1816 }
1817
1818 /*
1819 * Put deleted text into register 1 and shift number registers if the
1820 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001821 * Use the register name from before adjust_clip_reg() may have
1822 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001824 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 || oap->line_count > 1 || oap->use_reg_one)
1826 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001827 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (op_yank(oap, TRUE, FALSE) == OK)
1829 did_yank = TRUE;
1830 }
1831
Bram Moolenaar84298db2012-04-20 13:46:08 +02001832 /* Yank into small delete register when no named register specified
1833 * and the delete is within one line. */
1834 if ((
1835#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001836 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1837 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001838#endif
1839 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 && oap->line_count == 1)
1841 {
1842 oap->regname = '-';
1843 get_yank_register(oap->regname, TRUE);
1844 if (op_yank(oap, TRUE, FALSE) == OK)
1845 did_yank = TRUE;
1846 oap->regname = 0;
1847 }
1848
1849 /*
1850 * If there's too much stuff to fit in the yank register, then get a
1851 * confirmation before doing the delete. This is crude, but simple.
1852 * And it avoids doing a delete of something we can't put back if we
1853 * want.
1854 */
1855 if (!did_yank)
1856 {
1857 int msg_silent_save = msg_silent;
1858
1859 msg_silent = 0; /* must display the prompt */
1860 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1861 msg_silent = msg_silent_save;
1862 if (n != 'y')
1863 {
1864 EMSG(_(e_abort));
1865 return FAIL;
1866 }
1867 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001868
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001869#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001870 if (did_yank && has_textyankpost())
1871 yank_do_autocmd(oap, y_current);
1872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874
Bram Moolenaard04b7502010-07-08 22:27:55 +02001875 /*
1876 * block mode delete
1877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (oap->block_mode)
1879 {
1880 if (u_save((linenr_T)(oap->start.lnum - 1),
1881 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1882 return FAIL;
1883
1884 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1885 {
1886 block_prep(oap, &bd, lnum, TRUE);
1887 if (bd.textlen == 0) /* nothing to delete */
1888 continue;
1889
1890 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1891 if (lnum == curwin->w_cursor.lnum)
1892 {
1893 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1894# ifdef FEAT_VIRTUALEDIT
1895 curwin->w_cursor.coladd = 0;
1896# endif
1897 }
1898
1899 /* n == number of chars deleted
1900 * If we delete a TAB, it may be replaced by several characters.
1901 * Thus the number of characters may increase!
1902 */
1903 n = bd.textlen - bd.startspaces - bd.endspaces;
1904 oldp = ml_get(lnum);
1905 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1906 if (newp == NULL)
1907 continue;
1908 /* copy up to deleted part */
1909 mch_memmove(newp, oldp, (size_t)bd.textcol);
1910 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001911 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 (size_t)(bd.startspaces + bd.endspaces));
1913 /* copy the part after the deleted part */
1914 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001915 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 /* replace the line */
1917 ml_replace(lnum, newp, FALSE);
1918 }
1919
1920 check_cursor_col();
1921 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1922 oap->end.lnum + 1, 0L);
1923 oap->line_count = 0; /* no lines deleted */
1924 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001925 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 if (oap->op_type == OP_CHANGE)
1928 {
1929 /* Delete the lines except the first one. Temporarily move the
1930 * cursor to the next line. Save the current line number, if the
1931 * last line is deleted it may be changed.
1932 */
1933 if (oap->line_count > 1)
1934 {
1935 lnum = curwin->w_cursor.lnum;
1936 ++curwin->w_cursor.lnum;
1937 del_lines((long)(oap->line_count - 1), TRUE);
1938 curwin->w_cursor.lnum = lnum;
1939 }
1940 if (u_save_cursor() == FAIL)
1941 return FAIL;
1942 if (curbuf->b_p_ai) /* don't delete indent */
1943 {
1944 beginline(BL_WHITE); /* cursor on first non-white */
1945 did_ai = TRUE; /* delete the indent when ESC hit */
1946 ai_col = curwin->w_cursor.col;
1947 }
1948 else
1949 beginline(0); /* cursor in column 0 */
1950 truncate_line(FALSE); /* delete the rest of the line */
1951 /* leave cursor past last char in line */
1952 if (oap->line_count > 1)
1953 u_clearline(); /* "U" command not possible after "2cc" */
1954 }
1955 else
1956 {
1957 del_lines(oap->line_count, TRUE);
1958 beginline(BL_WHITE | BL_FIX);
1959 u_clearline(); /* "U" command not possible after "dd" */
1960 }
1961 }
1962 else
1963 {
1964#ifdef FEAT_VIRTUALEDIT
1965 if (virtual_op)
1966 {
1967 int endcol = 0;
1968
1969 /* For virtualedit: break the tabs that are partly included. */
1970 if (gchar_pos(&oap->start) == '\t')
1971 {
1972 if (u_save_cursor() == FAIL) /* save first line for undo */
1973 return FAIL;
1974 if (oap->line_count == 1)
1975 endcol = getviscol2(oap->end.col, oap->end.coladd);
1976 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1977 oap->start = curwin->w_cursor;
1978 if (oap->line_count == 1)
1979 {
1980 coladvance(endcol);
1981 oap->end.col = curwin->w_cursor.col;
1982 oap->end.coladd = curwin->w_cursor.coladd;
1983 curwin->w_cursor = oap->start;
1984 }
1985 }
1986
1987 /* Break a tab only when it's included in the area. */
1988 if (gchar_pos(&oap->end) == '\t'
1989 && (int)oap->end.coladd < oap->inclusive)
1990 {
1991 /* save last line for undo */
1992 if (u_save((linenr_T)(oap->end.lnum - 1),
1993 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1994 return FAIL;
1995 curwin->w_cursor = oap->end;
1996 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1997 oap->end = curwin->w_cursor;
1998 curwin->w_cursor = oap->start;
1999 }
2000 }
2001#endif
2002
2003 if (oap->line_count == 1) /* delete characters within one line */
2004 {
2005 if (u_save_cursor() == FAIL) /* save line for undo */
2006 return FAIL;
2007
2008 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002009 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 && oap->op_type == OP_CHANGE
2011 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002012 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 display_dollar(oap->end.col - !oap->inclusive);
2014
2015 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2016
2017#ifdef FEAT_VIRTUALEDIT
2018 if (virtual_op)
2019 {
2020 /* fix up things for virtualedit-delete:
2021 * break the tabs which are going to get in our way
2022 */
2023 char_u *curline = ml_get_curline();
2024 int len = (int)STRLEN(curline);
2025
2026 if (oap->end.coladd != 0
2027 && (int)oap->end.col >= len - 1
2028 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2029 n++;
2030 /* Delete at least one char (e.g, when on a control char). */
2031 if (n == 0 && oap->start.coladd != oap->end.coladd)
2032 n = 1;
2033
2034 /* When deleted a char in the line, reset coladd. */
2035 if (gchar_cursor() != NUL)
2036 curwin->w_cursor.coladd = 0;
2037 }
2038#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002039 (void)del_bytes((long)n, !virtual_op,
2040 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042 else /* delete characters between lines */
2043 {
2044 pos_T curpos;
2045
2046 /* save deleted and changed lines for undo */
2047 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2048 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2049 return FAIL;
2050
2051 truncate_line(TRUE); /* delete from cursor to end of line */
2052
2053 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2054 ++curwin->w_cursor.lnum;
2055 del_lines((long)(oap->line_count - 2), FALSE);
2056
Bram Moolenaard009e862015-06-09 20:20:03 +02002057 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002058 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002059 curwin->w_cursor.col = 0;
2060 (void)del_bytes((long)n, !virtual_op,
2061 oap->op_type == OP_DELETE && !oap->is_VIsual);
2062 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2063 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 }
2066
2067 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2068
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002069#ifdef FEAT_VIRTUALEDIT
2070setmarks:
2071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (oap->block_mode)
2073 {
2074 curbuf->b_op_end.lnum = oap->end.lnum;
2075 curbuf->b_op_end.col = oap->start.col;
2076 }
2077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 curbuf->b_op_end = oap->start;
2079 curbuf->b_op_start = oap->start;
2080
2081 return OK;
2082}
2083
2084#ifdef FEAT_MBYTE
2085/*
2086 * Adjust end of operating area for ending on a multi-byte character.
2087 * Used for deletion.
2088 */
2089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002090mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 char_u *p;
2093
2094 if (oap->inclusive)
2095 {
2096 p = ml_get(oap->end.lnum);
2097 oap->end.col += mb_tail_off(p, p + oap->end.col);
2098 }
2099}
2100#endif
2101
2102#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2103/*
2104 * Replace a whole area with one character.
2105 */
2106 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002107op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 int n, numc;
2110#ifdef FEAT_MBYTE
2111 int num_chars;
2112#endif
2113 char_u *newp, *oldp;
2114 size_t oldlen;
2115 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002116 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002117 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2120 return OK; /* nothing to do */
2121
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002122 if (c == REPLACE_CR_NCHAR)
2123 {
2124 had_ctrl_v_cr = TRUE;
2125 c = CAR;
2126 }
2127 else if (c == REPLACE_NL_NCHAR)
2128 {
2129 had_ctrl_v_cr = TRUE;
2130 c = NL;
2131 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133#ifdef FEAT_MBYTE
2134 if (has_mbyte)
2135 mb_adjust_opend(oap);
2136#endif
2137
2138 if (u_save((linenr_T)(oap->start.lnum - 1),
2139 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2140 return FAIL;
2141
2142 /*
2143 * block mode replace
2144 */
2145 if (oap->block_mode)
2146 {
2147 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2148 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2149 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002150 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2152 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2153 continue; /* nothing to replace */
2154
2155 /* n == number of extra chars required
2156 * If we split a TAB, it may be replaced by several characters.
2157 * Thus the number of characters may increase!
2158 */
2159#ifdef FEAT_VIRTUALEDIT
2160 /* If the range starts in virtual space, count the initial
2161 * coladd offset as part of "startspaces" */
2162 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2163 {
2164 pos_T vpos;
2165
Bram Moolenaara1381de2009-11-03 15:44:21 +00002166 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 getvpos(&vpos, oap->start_vcol);
2168 bd.startspaces += vpos.coladd;
2169 n = bd.startspaces;
2170 }
2171 else
2172#endif
2173 /* allow for pre spaces */
2174 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2175
2176 /* allow for post spp */
2177 n += (bd.endspaces
2178#ifdef FEAT_VIRTUALEDIT
2179 && !bd.is_oneChar
2180#endif
2181 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2182 /* Figure out how many characters to replace. */
2183 numc = oap->end_vcol - oap->start_vcol + 1;
2184 if (bd.is_short && (!virtual_op || bd.is_MAX))
2185 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2186
2187#ifdef FEAT_MBYTE
2188 /* A double-wide character can be replaced only up to half the
2189 * times. */
2190 if ((*mb_char2cells)(c) > 1)
2191 {
2192 if ((numc & 1) && !bd.is_short)
2193 {
2194 ++bd.endspaces;
2195 ++n;
2196 }
2197 numc = numc / 2;
2198 }
2199
2200 /* Compute bytes needed, move character count to num_chars. */
2201 num_chars = numc;
2202 numc *= (*mb_char2len)(c);
2203#endif
2204 /* oldlen includes textlen, so don't double count */
2205 n += numc - bd.textlen;
2206
2207 oldp = ml_get_curline();
2208 oldlen = STRLEN(oldp);
2209 newp = alloc_check((unsigned)oldlen + 1 + n);
2210 if (newp == NULL)
2211 continue;
2212 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2213 /* copy up to deleted part */
2214 mch_memmove(newp, oldp, (size_t)bd.textcol);
2215 oldp += bd.textcol + bd.textlen;
2216 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002217 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002219 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2220 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002221 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002223#ifdef FEAT_MBYTE
2224 if (has_mbyte)
2225 {
2226 n = (int)STRLEN(newp);
2227 while (--num_chars >= 0)
2228 n += (*mb_char2bytes)(c, newp + n);
2229 }
2230 else
2231#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002232 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002233 if (!bd.is_short)
2234 {
2235 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002236 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002237 /* copy the part after the changed part */
2238 STRMOVE(newp + STRLEN(newp), oldp);
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002243 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002244 after_p = alloc_check(
2245 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002246 if (after_p != NULL)
2247 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249 /* replace the line */
2250 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002251 if (after_p != NULL)
2252 {
2253 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2254 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2255 oap->end.lnum++;
2256 vim_free(after_p);
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259 }
2260 else
2261 {
2262 /*
2263 * MCHAR and MLINE motion replace.
2264 */
2265 if (oap->motion_type == MLINE)
2266 {
2267 oap->start.col = 0;
2268 curwin->w_cursor.col = 0;
2269 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2270 if (oap->end.col)
2271 --oap->end.col;
2272 }
2273 else if (!oap->inclusive)
2274 dec(&(oap->end));
2275
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002276 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 n = gchar_cursor();
2279 if (n != NUL)
2280 {
2281#ifdef FEAT_MBYTE
2282 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2283 {
2284 /* This is slow, but it handles replacing a single-byte
2285 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002286 if (curwin->w_cursor.lnum == oap->end.lnum)
2287 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 n = State;
2289 State = REPLACE;
2290 ins_char(c);
2291 State = n;
2292 /* Backup to the replaced character. */
2293 dec_cursor();
2294 }
2295 else
2296#endif
2297 {
2298#ifdef FEAT_VIRTUALEDIT
2299 if (n == TAB)
2300 {
2301 int end_vcol = 0;
2302
2303 if (curwin->w_cursor.lnum == oap->end.lnum)
2304 {
2305 /* oap->end has to be recalculated when
2306 * the tab breaks */
2307 end_vcol = getviscol2(oap->end.col,
2308 oap->end.coladd);
2309 }
2310 coladvance_force(getviscol());
2311 if (curwin->w_cursor.lnum == oap->end.lnum)
2312 getvpos(&oap->end, end_vcol);
2313 }
2314#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002315 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317 }
2318#ifdef FEAT_VIRTUALEDIT
2319 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2320 {
2321 int virtcols = oap->end.coladd;
2322
2323 if (curwin->w_cursor.lnum == oap->start.lnum
2324 && oap->start.col == oap->end.col && oap->start.coladd)
2325 virtcols -= oap->start.coladd;
2326
2327 /* oap->end has been trimmed so it's effectively inclusive;
2328 * as a result an extra +1 must be counted so we don't
2329 * trample the NUL byte. */
2330 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2331 curwin->w_cursor.col -= (virtcols + 1);
2332 for (; virtcols >= 0; virtcols--)
2333 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002334 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (inc(&curwin->w_cursor) == -1)
2336 break;
2337 }
2338 }
2339#endif
2340
2341 /* Advance to next character, stop at the end of the file. */
2342 if (inc_cursor() == -1)
2343 break;
2344 }
2345 }
2346
2347 curwin->w_cursor = oap->start;
2348 check_cursor();
2349 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2350
2351 /* Set "'[" and "']" marks. */
2352 curbuf->b_op_start = oap->start;
2353 curbuf->b_op_end = oap->end;
2354
2355 return OK;
2356}
2357#endif
2358
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002359static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361/*
2362 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2363 */
2364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002365op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002369 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 if (u_save((linenr_T)(oap->start.lnum - 1),
2372 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2373 return;
2374
2375 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (oap->block_mode) /* Visual block mode */
2377 {
2378 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2379 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002380 int one_change;
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 block_prep(oap, &bd, pos.lnum, FALSE);
2383 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002384 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2385 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002386
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002387#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002388 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
2390 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2391
Bram Moolenaar009b2592004-10-24 19:18:58 +00002392 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2393 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002395 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 if (did_change)
2400 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2401 }
2402 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 if (oap->motion_type == MLINE)
2405 {
2406 oap->start.col = 0;
2407 pos.col = 0;
2408 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2409 if (oap->end.col)
2410 --oap->end.col;
2411 }
2412 else if (!oap->inclusive)
2413 dec(&(oap->end));
2414
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002415 if (pos.lnum == oap->end.lnum)
2416 did_change = swapchars(oap->op_type, &pos,
2417 oap->end.col - pos.col + 1);
2418 else
2419 for (;;)
2420 {
2421 did_change |= swapchars(oap->op_type, &pos,
2422 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2423 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002424 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002425 break;
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (did_change)
2428 {
2429 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2430 0L);
2431#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002432 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 char_u *ptr;
2435 int count;
2436
2437 pos = oap->start;
2438 while (pos.lnum < oap->end.lnum)
2439 {
2440 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002441 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002442 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002444 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 pos.col = 0;
2446 pos.lnum++;
2447 }
2448 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2449 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002450 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002452 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
2454#endif
2455 }
2456 }
2457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (!did_change && oap->is_VIsual)
2459 /* No change: need to remove the Visual selection */
2460 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /*
2463 * Set '[ and '] marks.
2464 */
2465 curbuf->b_op_start = oap->start;
2466 curbuf->b_op_end = oap->end;
2467
2468 if (oap->line_count > p_report)
2469 {
2470 if (oap->line_count == 1)
2471 MSG(_("1 line changed"));
2472 else
2473 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2474 }
2475}
2476
2477/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002478 * Invoke swapchar() on "length" bytes at position "pos".
2479 * "pos" is advanced to just after the changed characters.
2480 * "length" is rounded up to include the whole last multi-byte character.
2481 * Also works correctly when the number of bytes changes.
2482 * Returns TRUE if some character was changed.
2483 */
2484 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002485swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002486{
2487 int todo;
2488 int did_change = 0;
2489
2490 for (todo = length; todo > 0; --todo)
2491 {
2492# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002493 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002494 {
2495 int len = (*mb_ptr2len)(ml_get_pos(pos));
2496
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002497 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002498 if (len > 0)
2499 todo -= len - 1;
2500 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002501# endif
2502 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002503 if (inc(pos) == -1) /* at end of file */
2504 break;
2505 }
2506 return did_change;
2507}
2508
2509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 * If op_type == OP_UPPER: make uppercase,
2511 * if op_type == OP_LOWER: make lowercase,
2512 * if op_type == OP_ROT13: do rot13 encoding,
2513 * else swap case of character at 'pos'
2514 * returns TRUE when something actually changed.
2515 */
2516 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002517swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 int c;
2520 int nc;
2521
2522 c = gchar_pos(pos);
2523
2524 /* Only do rot13 encoding for ASCII characters. */
2525 if (c >= 0x80 && op_type == OP_ROT13)
2526 return FALSE;
2527
2528#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002529 if (op_type == OP_UPPER && c == 0xdf
2530 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002531 {
2532 pos_T sp = curwin->w_cursor;
2533
2534 /* Special handling of German sharp s: change to "SS". */
2535 curwin->w_cursor = *pos;
2536 del_char(FALSE);
2537 ins_char('S');
2538 ins_char('S');
2539 curwin->w_cursor = sp;
2540 inc(pos);
2541 }
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2544 return FALSE;
2545#endif
2546 nc = c;
2547 if (MB_ISLOWER(c))
2548 {
2549 if (op_type == OP_ROT13)
2550 nc = ROT13(c, 'a');
2551 else if (op_type != OP_LOWER)
2552 nc = MB_TOUPPER(c);
2553 }
2554 else if (MB_ISUPPER(c))
2555 {
2556 if (op_type == OP_ROT13)
2557 nc = ROT13(c, 'A');
2558 else if (op_type != OP_UPPER)
2559 nc = MB_TOLOWER(c);
2560 }
2561 if (nc != c)
2562 {
2563#ifdef FEAT_MBYTE
2564 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2565 {
2566 pos_T sp = curwin->w_cursor;
2567
2568 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002569 /* don't use del_char(), it also removes composing chars */
2570 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 ins_char(nc);
2572 curwin->w_cursor = sp;
2573 }
2574 else
2575#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002576 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return TRUE;
2578 }
2579 return FALSE;
2580}
2581
2582#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2583/*
2584 * op_insert - Insert and append operators for Visual mode.
2585 */
2586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002587op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 long ins_len, pre_textlen = 0;
2590 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002591 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 struct block_def bd;
2593 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002594 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 /* edit() changes this - record it for OP_APPEND */
2597 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2598
2599 /* vis block is still marked. Get rid of it now. */
2600 curwin->w_cursor.lnum = oap->start.lnum;
2601 update_screen(INVERTED);
2602
2603 if (oap->block_mode)
2604 {
2605#ifdef FEAT_VIRTUALEDIT
2606 /* When 'virtualedit' is used, need to insert the extra spaces before
2607 * doing block_prep(). When only "block" is used, virtual edit is
2608 * already disabled, but still need it when calling
2609 * coladvance_force(). */
2610 if (curwin->w_cursor.coladd > 0)
2611 {
2612 int old_ve_flags = ve_flags;
2613
2614 ve_flags = VE_ALL;
2615 if (u_save_cursor() == FAIL)
2616 return;
2617 coladvance_force(oap->op_type == OP_APPEND
2618 ? oap->end_vcol + 1 : getviscol());
2619 if (oap->op_type == OP_APPEND)
2620 --curwin->w_cursor.col;
2621 ve_flags = old_ve_flags;
2622 }
2623#endif
2624 /* Get the info about the block before entering the text */
2625 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002626 /* Get indent information */
2627 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (oap->op_type == OP_APPEND)
2631 firstline += bd.textlen;
2632 pre_textlen = (long)STRLEN(firstline);
2633 }
2634
2635 if (oap->op_type == OP_APPEND)
2636 {
2637 if (oap->block_mode
2638#ifdef FEAT_VIRTUALEDIT
2639 && curwin->w_cursor.coladd == 0
2640#endif
2641 )
2642 {
2643 /* Move the cursor to the character right of the block. */
2644 curwin->w_set_curswant = TRUE;
2645 while (*ml_get_cursor() != NUL
2646 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2647 ++curwin->w_cursor.col;
2648 if (bd.is_short && !bd.is_MAX)
2649 {
2650 /* First line was too short, make it longer and adjust the
2651 * values in "bd". */
2652 if (u_save_cursor() == FAIL)
2653 return;
2654 for (i = 0; i < bd.endspaces; ++i)
2655 ins_char(' ');
2656 bd.textlen += bd.endspaces;
2657 }
2658 }
2659 else
2660 {
2661 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002662 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002665 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 && oap->start_vcol != oap->end_vcol)
2667 inc_cursor();
2668 }
2669 }
2670
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002671 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002672 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002674 /* When a tab was inserted, and the characters in front of the tab
2675 * have been converted to a tab as well, the column of the cursor
2676 * might have actually been reduced, so need to adjust here. */
2677 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002678 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002679 oap->start = curbuf->b_op_start_orig;
2680
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002681 /* If user has moved off this line, we don't know what to do, so do
2682 * nothing.
2683 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2684 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return;
2686
2687 if (oap->block_mode)
2688 {
2689 struct block_def bd2;
2690
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002691 /* If indent kicked in, the firstline might have changed
2692 * but only do that, if the indent actually increased. */
2693 ind_post = (colnr_T)getwhitecols_curline();
2694 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2695 {
2696 bd.textcol += ind_post - ind_pre;
2697 bd.start_vcol += ind_post - ind_pre;
2698 }
2699
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002700 /* The user may have moved the cursor before inserting something, try
2701 * to adjust the block for that. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002702 if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002703 {
2704 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002705 && oap->start.col
2706#ifdef FEAT_VIRTUALEDIT
2707 + oap->start.coladd
2708#endif
2709 != curbuf->b_op_start_orig.col
2710#ifdef FEAT_VIRTUALEDIT
2711 + curbuf->b_op_start_orig.coladd
2712#endif
2713 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002714 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002715 int t = getviscol2(curbuf->b_op_start_orig.col,
2716 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002717 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002718 pre_textlen -= t - oap->start_vcol;
2719 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002720 }
2721 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002722 && oap->end.col
2723#ifdef FEAT_VIRTUALEDIT
2724 + oap->end.coladd
2725#endif
2726 >= curbuf->b_op_start_orig.col
2727#ifdef FEAT_VIRTUALEDIT
2728 + curbuf->b_op_start_orig.coladd
2729#endif
2730 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002731 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002732 int t = getviscol2(curbuf->b_op_start_orig.col,
2733 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002734 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002735 /* reset pre_textlen to the value of OP_INSERT */
2736 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002737 pre_textlen -= t - oap->start_vcol;
2738 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002739 oap->op_type = OP_INSERT;
2740 }
2741 }
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 /*
2744 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002745 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 * Don't do this when "$" used, end-of-line will have changed.
2747 */
2748 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2749 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2750 {
2751 if (oap->op_type == OP_APPEND)
2752 {
2753 pre_textlen += bd2.textlen - bd.textlen;
2754 if (bd2.endspaces)
2755 --bd2.textlen;
2756 }
2757 bd.textcol = bd2.textcol;
2758 bd.textlen = bd2.textlen;
2759 }
2760
2761 /*
2762 * Subsequent calls to ml_get() flush the firstline data - take a
2763 * copy of the required string.
2764 */
2765 firstline = ml_get(oap->start.lnum) + bd.textcol;
2766 if (oap->op_type == OP_APPEND)
2767 firstline += bd.textlen;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002768 if (pre_textlen >= 0
2769 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
2771 ins_text = vim_strnsave(firstline, (int)ins_len);
2772 if (ins_text != NULL)
2773 {
2774 /* block handled here */
2775 if (u_save(oap->start.lnum,
2776 (linenr_T)(oap->end.lnum + 1)) == OK)
2777 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2778 &bd);
2779
2780 curwin->w_cursor.col = oap->start.col;
2781 check_cursor();
2782 vim_free(ins_text);
2783 }
2784 }
2785 }
2786}
2787#endif
2788
2789/*
2790 * op_change - handle a change operation
2791 *
2792 * return TRUE if edit() returns because of a CTRL-O command
2793 */
2794 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002795op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796{
2797 colnr_T l;
2798 int retval;
2799#ifdef FEAT_VISUALEXTRA
2800 long offset;
2801 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002802 long ins_len;
2803 long pre_textlen = 0;
2804 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 char_u *firstline;
2806 char_u *ins_text, *newp, *oldp;
2807 struct block_def bd;
2808#endif
2809
2810 l = oap->start.col;
2811 if (oap->motion_type == MLINE)
2812 {
2813 l = 0;
2814#ifdef FEAT_SMARTINDENT
2815 if (!p_paste && curbuf->b_p_si
2816# ifdef FEAT_CINDENT
2817 && !curbuf->b_p_cin
2818# endif
2819 )
2820 can_si = TRUE; /* It's like opening a new line, do si */
2821#endif
2822 }
2823
2824 /* First delete the text in the region. In an empty buffer only need to
2825 * save for undo */
2826 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2827 {
2828 if (u_save_cursor() == FAIL)
2829 return FALSE;
2830 }
2831 else if (op_delete(oap) == FAIL)
2832 return FALSE;
2833
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002834 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 && !virtual_op)
2836 inc_cursor();
2837
2838#ifdef FEAT_VISUALEXTRA
2839 /* check for still on same line (<CR> in inserted text meaningless) */
2840 /* skip blank lines too */
2841 if (oap->block_mode)
2842 {
2843# ifdef FEAT_VIRTUALEDIT
2844 /* Add spaces before getting the current line length. */
2845 if (virtual_op && (curwin->w_cursor.coladd > 0
2846 || gchar_cursor() == NUL))
2847 coladvance_force(getviscol());
2848# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002849 firstline = ml_get(oap->start.lnum);
2850 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002851 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 bd.textcol = curwin->w_cursor.col;
2853 }
2854#endif
2855
2856#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2857 if (oap->motion_type == MLINE)
2858 fix_indent();
2859#endif
2860
2861 retval = edit(NUL, FALSE, (linenr_T)1);
2862
2863#ifdef FEAT_VISUALEXTRA
2864 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002865 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002867 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002869 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002871 /* Auto-indenting may have changed the indent. If the cursor was past
2872 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002874 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002876 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002877
2878 pre_textlen += new_indent - pre_indent;
2879 bd.textcol += new_indent - pre_indent;
2880 }
2881
2882 ins_len = (long)STRLEN(firstline) - pre_textlen;
2883 if (ins_len > 0)
2884 {
2885 /* Subsequent calls to ml_get() flush the firstline data - take a
2886 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2888 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002889 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2891 linenr++)
2892 {
2893 block_prep(oap, &bd, linenr, TRUE);
2894 if (!bd.is_short || virtual_op)
2895 {
2896# ifdef FEAT_VIRTUALEDIT
2897 pos_T vpos;
2898
2899 /* If the block starts in virtual space, count the
2900 * initial coladd offset as part of "startspaces" */
2901 if (bd.is_short)
2902 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002903 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 else
2907 vpos.coladd = 0;
2908# endif
2909 oldp = ml_get(linenr);
2910 newp = alloc_check((unsigned)(STRLEN(oldp)
2911# ifdef FEAT_VIRTUALEDIT
2912 + vpos.coladd
2913# endif
2914 + ins_len + 1));
2915 if (newp == NULL)
2916 continue;
2917 /* copy up to block start */
2918 mch_memmove(newp, oldp, (size_t)bd.textcol);
2919 offset = bd.textcol;
2920# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002921 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 offset += vpos.coladd;
2923# endif
2924 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2925 offset += ins_len;
2926 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002927 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 ml_replace(linenr, newp, FALSE);
2929 }
2930 }
2931 check_cursor();
2932
2933 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2934 }
2935 vim_free(ins_text);
2936 }
2937 }
2938#endif
2939
2940 return retval;
2941}
2942
2943/*
2944 * set all the yank registers to empty (called from main())
2945 */
2946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
2949 int i;
2950
2951 for (i = 0; i < NUM_REGISTERS; ++i)
2952 y_regs[i].y_array = NULL;
2953}
2954
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002955#if defined(EXITFREE) || defined(PROTO)
2956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002957clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002958{
2959 int i;
2960
2961 for (i = 0; i < NUM_REGISTERS; ++i)
2962 {
2963 y_current = &y_regs[i];
2964 if (y_current->y_array != NULL)
2965 free_yank_all();
2966 }
2967}
2968#endif
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * Free "n" lines from the current yank register.
2972 * Called for normal freeing and in case of error.
2973 */
2974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002975free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 if (y_current->y_array != NULL)
2978 {
2979 long i;
2980
2981 for (i = n; --i >= 0; )
2982 {
2983#ifdef AMIGA /* only for very slow machines */
2984 if ((i & 1023) == 1023) /* this may take a while */
2985 {
2986 /*
2987 * This message should never cause a hit-return message.
2988 * Overwrite this message with any next message.
2989 */
2990 ++no_wait_return;
2991 smsg((char_u *)_("freeing %ld lines"), i + 1);
2992 --no_wait_return;
2993 msg_didout = FALSE;
2994 msg_col = 0;
2995 }
2996#endif
2997 vim_free(y_current->y_array[i]);
2998 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002999 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#ifdef AMIGA
3001 if (n >= 1000)
3002 MSG("");
3003#endif
3004 }
3005}
3006
3007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003008free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 free_yank(y_current->y_size);
3011}
3012
3013/*
3014 * Yank the text between "oap->start" and "oap->end" into a yank register.
3015 * If we are to append (uppercase register), we first yank into a new yank
3016 * register and then concatenate the old and the new one (so we keep the old
3017 * one in case of out-of-memory).
3018 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003019 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
3021 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003022op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003025 yankreg_T *curr; /* copy of y_current */
3026 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 char_u **new_ptr;
3028 linenr_T lnum; /* current line number */
3029 long j;
3030 int yanktype = oap->motion_type;
3031 long yanklines = oap->line_count;
3032 linenr_T yankendlnum = oap->end.lnum;
3033 char_u *p;
3034 char_u *pnew;
3035 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003036#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003037 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /* check for read-only register */
3041 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3042 {
3043 beep_flush();
3044 return FAIL;
3045 }
3046 if (oap->regname == '_') /* black hole: nothing to do */
3047 return OK;
3048
3049#ifdef FEAT_CLIPBOARD
3050 if (!clip_star.available && oap->regname == '*')
3051 oap->regname = 0;
3052 else if (!clip_plus.available && oap->regname == '+')
3053 oap->regname = 0;
3054#endif
3055
3056 if (!deleting) /* op_delete() already set y_current */
3057 get_yank_register(oap->regname, TRUE);
3058
3059 curr = y_current;
3060 /* append to existing contents */
3061 if (y_append && y_current->y_array != NULL)
3062 y_current = &newreg;
3063 else
3064 free_yank_all(); /* free previously yanked lines */
3065
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003066 /*
3067 * If the cursor was in column 1 before and after the movement, and the
3068 * operator is not inclusive, the yank is always linewise.
3069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if ( oap->motion_type == MCHAR
3071 && oap->start.col == 0
3072 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003074 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 && oap->end.col == 0
3076 && yanklines > 1)
3077 {
3078 yanktype = MLINE;
3079 --yankendlnum;
3080 --yanklines;
3081 }
3082
3083 y_current->y_size = yanklines;
3084 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3087 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (y_current->y_array == NULL)
3089 {
3090 y_current = curr;
3091 return FAIL;
3092 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003093#ifdef FEAT_VIMINFO
3094 y_current->y_time_set = vim_time();
3095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096
3097 y_idx = 0;
3098 lnum = oap->start.lnum;
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (oap->block_mode)
3101 {
3102 /* Visual block mode */
3103 y_current->y_type = MBLOCK; /* set the yank register type */
3104 y_current->y_width = oap->end_vcol - oap->start_vcol;
3105
3106 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3107 y_current->y_width--;
3108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3111 {
3112 switch (y_current->y_type)
3113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 case MBLOCK:
3115 block_prep(oap, &bd, lnum, FALSE);
3116 if (yank_copy_line(&bd, y_idx) == FAIL)
3117 goto fail;
3118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 case MLINE:
3121 if ((y_current->y_array[y_idx] =
3122 vim_strsave(ml_get(lnum))) == NULL)
3123 goto fail;
3124 break;
3125
3126 case MCHAR:
3127 {
3128 colnr_T startcol = 0, endcol = MAXCOL;
3129#ifdef FEAT_VIRTUALEDIT
3130 int is_oneChar = FALSE;
3131 colnr_T cs, ce;
3132#endif
3133 p = ml_get(lnum);
3134 bd.startspaces = 0;
3135 bd.endspaces = 0;
3136
3137 if (lnum == oap->start.lnum)
3138 {
3139 startcol = oap->start.col;
3140#ifdef FEAT_VIRTUALEDIT
3141 if (virtual_op)
3142 {
3143 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3144 if (ce != cs && oap->start.coladd > 0)
3145 {
3146 /* Part of a tab selected -- but don't
3147 * double-count it. */
3148 bd.startspaces = (ce - cs + 1)
3149 - oap->start.coladd;
3150 startcol++;
3151 }
3152 }
3153#endif
3154 }
3155
3156 if (lnum == oap->end.lnum)
3157 {
3158 endcol = oap->end.col;
3159#ifdef FEAT_VIRTUALEDIT
3160 if (virtual_op)
3161 {
3162 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3163 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3164# ifdef FEAT_MBYTE
3165 /* Don't add space for double-wide
3166 * char; endcol will be on last byte
3167 * of multi-byte char. */
3168 && (*mb_head_off)(p, p + endcol) == 0
3169# endif
3170 ))
3171 {
3172 if (oap->start.lnum == oap->end.lnum
3173 && oap->start.col == oap->end.col)
3174 {
3175 /* Special case: inside a single char */
3176 is_oneChar = TRUE;
3177 bd.startspaces = oap->end.coladd
3178 - oap->start.coladd + oap->inclusive;
3179 endcol = startcol;
3180 }
3181 else
3182 {
3183 bd.endspaces = oap->end.coladd
3184 + oap->inclusive;
3185 endcol -= oap->inclusive;
3186 }
3187 }
3188 }
3189#endif
3190 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003191 if (endcol == MAXCOL)
3192 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (startcol > endcol
3194#ifdef FEAT_VIRTUALEDIT
3195 || is_oneChar
3196#endif
3197 )
3198 bd.textlen = 0;
3199 else
3200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 bd.textlen = endcol - startcol + oap->inclusive;
3202 }
3203 bd.textstart = p + startcol;
3204 if (yank_copy_line(&bd, y_idx) == FAIL)
3205 goto fail;
3206 break;
3207 }
3208 /* NOTREACHED */
3209 }
3210 }
3211
3212 if (curr != y_current) /* append the new block to the old block */
3213 {
3214 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3215 (curr->y_size + y_current->y_size)), TRUE);
3216 if (new_ptr == NULL)
3217 goto fail;
3218 for (j = 0; j < curr->y_size; ++j)
3219 new_ptr[j] = curr->y_array[j];
3220 vim_free(curr->y_array);
3221 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003222#ifdef FEAT_VIMINFO
3223 curr->y_time_set = vim_time();
3224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3227 curr->y_type = MLINE;
3228
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003229 /* Concatenate the last line of the old block with the first line of
3230 * the new block, unless being Vi compatible. */
3231 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3234 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3235 if (pnew == NULL)
3236 {
3237 y_idx = y_current->y_size - 1;
3238 goto fail;
3239 }
3240 STRCPY(pnew, curr->y_array[--j]);
3241 STRCAT(pnew, y_current->y_array[0]);
3242 vim_free(curr->y_array[j]);
3243 vim_free(y_current->y_array[0]);
3244 curr->y_array[j++] = pnew;
3245 y_idx = 1;
3246 }
3247 else
3248 y_idx = 0;
3249 while (y_idx < y_current->y_size)
3250 curr->y_array[j++] = y_current->y_array[y_idx++];
3251 curr->y_size = j;
3252 vim_free(y_current->y_array);
3253 y_current = curr;
3254 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003255 if (curwin->w_p_rnu)
3256 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (mess) /* Display message about yank? */
3258 {
3259 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 && yanklines == 1)
3262 yanklines = 0;
3263 /* Some versions of Vi use ">=" here, some don't... */
3264 if (yanklines > p_report)
3265 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003266 char namebuf[100];
3267
3268 if (oap->regname == NUL)
3269 *namebuf = NUL;
3270 else
3271 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003272 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 /* redisplay now, so message is not deleted */
3275 update_topline_redraw();
3276 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003277 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003278 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003279 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003280 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003281 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003282 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003283 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003284 smsg((char_u *)_("block of %ld lines yanked%s"),
3285 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003287 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3288 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290 }
3291
3292 /*
3293 * Set "'[" and "']" marks.
3294 */
3295 curbuf->b_op_start = oap->start;
3296 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003297 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003298 {
3299 curbuf->b_op_start.col = 0;
3300 curbuf->b_op_end.col = MAXCOL;
3301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303#ifdef FEAT_CLIPBOARD
3304 /*
3305 * If we were yanking to the '*' register, send result to clipboard.
3306 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3307 * to the '*' register.
3308 */
3309 if (clip_star.available
3310 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003311 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003312 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 if (curr != &(y_regs[STAR_REGISTER]))
3315 /* Copy the text from register 0 to the clipboard register. */
3316 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3317
3318 clip_own_selection(&clip_star);
3319 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003320# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003321 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324
3325# ifdef FEAT_X11
3326 /*
3327 * If we were yanking to the '+' register, send result to selection.
3328 * Also copy to the '*' register, in case auto-select is off.
3329 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003330 if (clip_plus.available
3331 && (curr == &(y_regs[PLUS_REGISTER])
3332 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003333 && ((clip_unnamed | clip_unnamed_saved) &
3334 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003336 if (curr != &(y_regs[PLUS_REGISTER]))
3337 /* Copy the text from register 0 to the clipboard register. */
3338 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 clip_own_selection(&clip_plus);
3341 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003342 if (!clip_isautosel_star() && !clip_isautosel_plus()
3343 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3346 clip_own_selection(&clip_star);
3347 clip_gen_set_selection(&clip_star);
3348 }
3349 }
3350# endif
3351#endif
3352
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003353#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003354 if (!deleting && has_textyankpost())
3355 yank_do_autocmd(oap, y_current);
3356#endif
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 return OK;
3359
3360fail: /* free the allocated lines */
3361 free_yank(y_idx + 1);
3362 y_current = curr;
3363 return FAIL;
3364}
3365
3366 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003367yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 char_u *pnew;
3370
3371 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3372 == NULL)
3373 return FAIL;
3374 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003375 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 pnew += bd->startspaces;
3377 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3378 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003379 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 pnew += bd->endspaces;
3381 *pnew = NUL;
3382 return OK;
3383}
3384
3385#ifdef FEAT_CLIPBOARD
3386/*
3387 * Make a copy of the y_current register to register "reg".
3388 */
3389 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003390copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003392 yankreg_T *curr = y_current;
3393 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 y_current = reg;
3396 free_yank_all();
3397 *y_current = *curr;
3398 y_current->y_array = (char_u **)lalloc_clear(
3399 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3400 if (y_current->y_array == NULL)
3401 y_current->y_size = 0;
3402 else
3403 for (j = 0; j < y_current->y_size; ++j)
3404 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3405 {
3406 free_yank(j);
3407 y_current->y_size = 0;
3408 break;
3409 }
3410 y_current = curr;
3411}
3412#endif
3413
3414/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003415 * Put contents of register "regname" into the text.
3416 * Caller must check "regname" to be valid!
3417 * "flags": PUT_FIXINDENT make indent look nice
3418 * PUT_CURSEND leave cursor after end of new text
3419 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
3421 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003422do_put(
3423 int regname,
3424 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3425 long count,
3426 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 char_u *ptr;
3429 char_u *newp, *oldp;
3430 int yanklen;
3431 int totlen = 0; /* init for gcc */
3432 linenr_T lnum;
3433 colnr_T col;
3434 long i; /* index in y_array[] */
3435 int y_type;
3436 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int oldlen;
3438 long y_width = 0;
3439 colnr_T vcol;
3440 int delcount;
3441 int incr = 0;
3442 long j;
3443 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 char_u **y_array = NULL;
3445 long nr_lines = 0;
3446 pos_T new_cursor;
3447 int indent;
3448 int orig_indent = 0; /* init for gcc */
3449 int indent_diff = 0; /* init for gcc */
3450 int first_indent = TRUE;
3451 int lendiff = 0;
3452 pos_T old_pos;
3453 char_u *insert_string = NULL;
3454 int allocated = FALSE;
3455 long cnt;
3456
3457#ifdef FEAT_CLIPBOARD
3458 /* Adjust register name for "unnamed" in 'clipboard'. */
3459 adjust_clip_reg(&regname);
3460 (void)may_get_selection(regname);
3461#endif
3462
3463 if (flags & PUT_FIXINDENT)
3464 orig_indent = get_indent();
3465
3466 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3467 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3468
3469 /*
3470 * Using inserted text works differently, because the register includes
3471 * special characters (newlines, etc.).
3472 */
3473 if (regname == '.')
3474 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003475 if (VIsual_active)
3476 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3478 (count == -1 ? 'O' : 'i')), count, FALSE);
3479 /* Putting the text is done later, so can't really move the cursor to
3480 * the next character. Use "l" to simulate it. */
3481 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3482 stuffcharReadbuff('l');
3483 return;
3484 }
3485
3486 /*
3487 * For special registers '%' (file name), '#' (alternate file name) and
3488 * ':' (last command line), etc. we have to create a fake yank register.
3489 */
3490 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3491 {
3492 if (insert_string == NULL)
3493 return;
3494 }
3495
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003496 /* Autocommands may be executed when saving lines for undo, which may make
3497 * y_array invalid. Start undo now to avoid that. */
3498 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (insert_string != NULL)
3501 {
3502 y_type = MCHAR;
3503#ifdef FEAT_EVAL
3504 if (regname == '=')
3505 {
3506 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003507 * characters.
3508 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 for (;;)
3510 {
3511 y_size = 0;
3512 ptr = insert_string;
3513 while (ptr != NULL)
3514 {
3515 if (y_array != NULL)
3516 y_array[y_size] = ptr;
3517 ++y_size;
3518 ptr = vim_strchr(ptr, '\n');
3519 if (ptr != NULL)
3520 {
3521 if (y_array != NULL)
3522 *ptr = NUL;
3523 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003524 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (*ptr == NUL)
3526 {
3527 y_type = MLINE;
3528 break;
3529 }
3530 }
3531 }
3532 if (y_array != NULL)
3533 break;
3534 y_array = (char_u **)alloc((unsigned)
3535 (y_size * sizeof(char_u *)));
3536 if (y_array == NULL)
3537 goto end;
3538 }
3539 }
3540 else
3541#endif
3542 {
3543 y_size = 1; /* use fake one-line yank register */
3544 y_array = &insert_string;
3545 }
3546 }
3547 else
3548 {
3549 get_yank_register(regname, FALSE);
3550
3551 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 y_size = y_current->y_size;
3554 y_array = y_current->y_array;
3555 }
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (y_type == MLINE)
3558 {
3559 if (flags & PUT_LINE_SPLIT)
3560 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003561 char_u *p;
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 /* "p" or "P" in Visual mode: split the lines to put the text in
3564 * between. */
3565 if (u_save_cursor() == FAIL)
3566 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003567 p = ml_get_cursor();
3568 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003569 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003570 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (ptr == NULL)
3572 goto end;
3573 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3574 vim_free(ptr);
3575
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003576 oldp = ml_get_curline();
3577 p = oldp + curwin->w_cursor.col;
3578 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003579 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003580 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (ptr == NULL)
3582 goto end;
3583 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3584 ++nr_lines;
3585 dir = FORWARD;
3586 }
3587 if (flags & PUT_LINE_FORWARD)
3588 {
3589 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003590 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 dir = FORWARD;
3592 }
3593 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3594 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3598 y_type = MLINE;
3599
3600 if (y_size == 0 || y_array == NULL)
3601 {
3602 EMSG2(_("E353: Nothing in register %s"),
3603 regname == 0 ? (char_u *)"\"" : transchar(regname));
3604 goto end;
3605 }
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (y_type == MBLOCK)
3608 {
3609 lnum = curwin->w_cursor.lnum + y_size + 1;
3610 if (lnum > curbuf->b_ml.ml_line_count)
3611 lnum = curbuf->b_ml.ml_line_count + 1;
3612 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3613 goto end;
3614 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003615 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 lnum = curwin->w_cursor.lnum;
3618#ifdef FEAT_FOLDING
3619 /* Correct line number for closed fold. Don't move the cursor yet,
3620 * u_save() uses it. */
3621 if (dir == BACKWARD)
3622 (void)hasFolding(lnum, &lnum, NULL);
3623 else
3624 (void)hasFolding(lnum, NULL, &lnum);
3625#endif
3626 if (dir == FORWARD)
3627 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003628 /* In an empty buffer the empty line is going to be replaced, include
3629 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003630 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 goto end;
3632#ifdef FEAT_FOLDING
3633 if (dir == FORWARD)
3634 curwin->w_cursor.lnum = lnum - 1;
3635 else
3636 curwin->w_cursor.lnum = lnum;
3637 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3638#endif
3639 }
3640 else if (u_save_cursor() == FAIL)
3641 goto end;
3642
3643 yanklen = (int)STRLEN(y_array[0]);
3644
3645#ifdef FEAT_VIRTUALEDIT
3646 if (ve_flags == VE_ALL && y_type == MCHAR)
3647 {
3648 if (gchar_cursor() == TAB)
3649 {
3650 /* Don't need to insert spaces when "p" on the last position of a
3651 * tab or "P" on the first position. */
3652 if (dir == FORWARD
3653 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3654 : curwin->w_cursor.coladd > 0)
3655 coladvance_force(getviscol());
3656 else
3657 curwin->w_cursor.coladd = 0;
3658 }
3659 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3660 coladvance_force(getviscol() + (dir == FORWARD));
3661 }
3662#endif
3663
3664 lnum = curwin->w_cursor.lnum;
3665 col = curwin->w_cursor.col;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 /*
3668 * Block mode
3669 */
3670 if (y_type == MBLOCK)
3671 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003672 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 colnr_T endcol2 = 0;
3674
3675 if (dir == FORWARD && c != NUL)
3676 {
3677#ifdef FEAT_VIRTUALEDIT
3678 if (ve_flags == VE_ALL)
3679 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3680 else
3681#endif
3682 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3683
3684#ifdef FEAT_MBYTE
3685 if (has_mbyte)
3686 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003687 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
3689#endif
3690#ifdef FEAT_VIRTUALEDIT
3691 if (c != TAB || ve_flags != VE_ALL)
3692#endif
3693 ++curwin->w_cursor.col;
3694 ++col;
3695 }
3696 else
3697 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3698
3699#ifdef FEAT_VIRTUALEDIT
3700 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003701 if (ve_flags == VE_ALL
3702 && (curwin->w_cursor.coladd > 0
3703 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
3705 if (dir == FORWARD && c == NUL)
3706 ++col;
3707 if (dir != FORWARD && c != NUL)
3708 ++curwin->w_cursor.col;
3709 if (c == TAB)
3710 {
3711 if (dir == BACKWARD && curwin->w_cursor.col)
3712 curwin->w_cursor.col--;
3713 if (dir == FORWARD && col - 1 == endcol2)
3714 curwin->w_cursor.col++;
3715 }
3716 }
3717 curwin->w_cursor.coladd = 0;
3718#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003719 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 for (i = 0; i < y_size; ++i)
3721 {
3722 int spaces;
3723 char shortline;
3724
3725 bd.startspaces = 0;
3726 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 vcol = 0;
3728 delcount = 0;
3729
3730 /* add a new line */
3731 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3732 {
3733 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3734 (colnr_T)1, FALSE) == FAIL)
3735 break;
3736 ++nr_lines;
3737 }
3738 /* get the old line and advance to the position to insert at */
3739 oldp = ml_get_curline();
3740 oldlen = (int)STRLEN(oldp);
3741 for (ptr = oldp; vcol < col && *ptr; )
3742 {
3743 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003744 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 vcol += incr;
3746 }
3747 bd.textcol = (colnr_T)(ptr - oldp);
3748
3749 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3750
3751 if (vcol < col) /* line too short, padd with spaces */
3752 bd.startspaces = col - vcol;
3753 else if (vcol > col)
3754 {
3755 bd.endspaces = vcol - col;
3756 bd.startspaces = incr - bd.endspaces;
3757 --bd.textcol;
3758 delcount = 1;
3759#ifdef FEAT_MBYTE
3760 if (has_mbyte)
3761 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3762#endif
3763 if (oldp[bd.textcol] != TAB)
3764 {
3765 /* Only a Tab can be split into spaces. Other
3766 * characters will have to be moved to after the
3767 * block, causing misalignment. */
3768 delcount = 0;
3769 bd.endspaces = 0;
3770 }
3771 }
3772
3773 yanklen = (int)STRLEN(y_array[i]);
3774
3775 /* calculate number of spaces required to fill right side of block*/
3776 spaces = y_width + 1;
3777 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003778 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (spaces < 0)
3780 spaces = 0;
3781
3782 /* insert the new text */
3783 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3784 newp = alloc_check((unsigned)totlen + oldlen + 1);
3785 if (newp == NULL)
3786 break;
3787 /* copy part up to cursor to new line */
3788 ptr = newp;
3789 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3790 ptr += bd.textcol;
3791 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003792 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 ptr += bd.startspaces;
3794 /* insert the new text */
3795 for (j = 0; j < count; ++j)
3796 {
3797 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3798 ptr += yanklen;
3799
3800 /* insert block's trailing spaces only if there's text behind */
3801 if ((j < count - 1 || !shortline) && spaces)
3802 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003803 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 ptr += spaces;
3805 }
3806 }
3807 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003808 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 ptr += bd.endspaces;
3810 /* move the text after the cursor to the end of the line. */
3811 mch_memmove(ptr, oldp + bd.textcol + delcount,
3812 (size_t)(oldlen - bd.textcol - delcount + 1));
3813 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3814
3815 ++curwin->w_cursor.lnum;
3816 if (i == 0)
3817 curwin->w_cursor.col += bd.startspaces;
3818 }
3819
3820 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3821
3822 /* Set '[ mark. */
3823 curbuf->b_op_start = curwin->w_cursor;
3824 curbuf->b_op_start.lnum = lnum;
3825
3826 /* adjust '] mark */
3827 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3828 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003829# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (flags & PUT_CURSEND)
3833 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003834 colnr_T len;
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 curwin->w_cursor = curbuf->b_op_end;
3837 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003838
3839 /* in Insert mode we might be after the NUL, correct for that */
3840 len = (colnr_T)STRLEN(ml_get_curline());
3841 if (curwin->w_cursor.col > len)
3842 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844 else
3845 curwin->w_cursor.lnum = lnum;
3846 }
3847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849 /*
3850 * Character or Line mode
3851 */
3852 if (y_type == MCHAR)
3853 {
3854 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3855 * char */
3856 if (dir == FORWARD && gchar_cursor() != NUL)
3857 {
3858#ifdef FEAT_MBYTE
3859 if (has_mbyte)
3860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003861 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
3863 /* put it on the next of the multi-byte character. */
3864 col += bytelen;
3865 if (yanklen)
3866 {
3867 curwin->w_cursor.col += bytelen;
3868 curbuf->b_op_end.col += bytelen;
3869 }
3870 }
3871 else
3872#endif
3873 {
3874 ++col;
3875 if (yanklen)
3876 {
3877 ++curwin->w_cursor.col;
3878 ++curbuf->b_op_end.col;
3879 }
3880 }
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 curbuf->b_op_start = curwin->w_cursor;
3883 }
3884 /*
3885 * Line mode: BACKWARD is the same as FORWARD on the previous line
3886 */
3887 else if (dir == BACKWARD)
3888 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003889 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 /*
3892 * simple case: insert into current line
3893 */
3894 if (y_type == MCHAR && y_size == 1)
3895 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003896 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003897
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003898 if (VIsual_active)
3899 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003900 end_lnum = curbuf->b_visual.vi_end.lnum;
3901 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3902 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003903 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003904
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003905 do {
3906 totlen = count * yanklen;
3907 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003909 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003910 if (VIsual_active && col > (int)STRLEN(oldp))
3911 {
3912 lnum++;
3913 continue;
3914 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003915 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3916 if (newp == NULL)
3917 goto end; /* alloc() gave an error message */
3918 mch_memmove(newp, oldp, (size_t)col);
3919 ptr = newp + col;
3920 for (i = 0; i < count; ++i)
3921 {
3922 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3923 ptr += yanklen;
3924 }
3925 STRMOVE(ptr, oldp + col);
3926 ml_replace(lnum, newp, FALSE);
3927 /* Place cursor on last putted char. */
3928 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003929 {
3930 /* make sure curwin->w_virtcol is updated */
3931 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003932 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003935 if (VIsual_active)
3936 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003937 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003938
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003939 if (VIsual_active) /* reset lnum to the last visual line */
3940 lnum--;
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 curbuf->b_op_end = curwin->w_cursor;
3943 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3944 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3945 ++curwin->w_cursor.col;
3946 changed_bytes(lnum, col);
3947 }
3948 else
3949 {
3950 /*
3951 * Insert at least one line. When y_type is MCHAR, break the first
3952 * line in two.
3953 */
3954 for (cnt = 1; cnt <= count; ++cnt)
3955 {
3956 i = 0;
3957 if (y_type == MCHAR)
3958 {
3959 /*
3960 * Split the current line in two at the insert position.
3961 * First insert y_array[size - 1] in front of second line.
3962 * Then append y_array[0] to first line.
3963 */
3964 lnum = new_cursor.lnum;
3965 ptr = ml_get(lnum) + col;
3966 totlen = (int)STRLEN(y_array[y_size - 1]);
3967 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3968 if (newp == NULL)
3969 goto error;
3970 STRCPY(newp, y_array[y_size - 1]);
3971 STRCAT(newp, ptr);
3972 /* insert second line */
3973 ml_append(lnum, newp, (colnr_T)0, FALSE);
3974 vim_free(newp);
3975
3976 oldp = ml_get(lnum);
3977 newp = alloc_check((unsigned)(col + yanklen + 1));
3978 if (newp == NULL)
3979 goto error;
3980 /* copy first part of line */
3981 mch_memmove(newp, oldp, (size_t)col);
3982 /* append to first line */
3983 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3984 ml_replace(lnum, newp, FALSE);
3985
3986 curwin->w_cursor.lnum = lnum;
3987 i = 1;
3988 }
3989
3990 for (; i < y_size; ++i)
3991 {
3992 if ((y_type != MCHAR || i < y_size - 1)
3993 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3994 == FAIL)
3995 goto error;
3996 lnum++;
3997 ++nr_lines;
3998 if (flags & PUT_FIXINDENT)
3999 {
4000 old_pos = curwin->w_cursor;
4001 curwin->w_cursor.lnum = lnum;
4002 ptr = ml_get(lnum);
4003 if (cnt == count && i == y_size - 1)
4004 lendiff = (int)STRLEN(ptr);
4005#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4006 if (*ptr == '#' && preprocs_left())
4007 indent = 0; /* Leave # lines at start */
4008 else
4009#endif
4010 if (*ptr == NUL)
4011 indent = 0; /* Ignore empty lines */
4012 else if (first_indent)
4013 {
4014 indent_diff = orig_indent - get_indent();
4015 indent = orig_indent;
4016 first_indent = FALSE;
4017 }
4018 else if ((indent = get_indent() + indent_diff) < 0)
4019 indent = 0;
4020 (void)set_indent(indent, 0);
4021 curwin->w_cursor = old_pos;
4022 /* remember how many chars were removed */
4023 if (cnt == count && i == y_size - 1)
4024 lendiff -= (int)STRLEN(ml_get(lnum));
4025 }
4026 }
4027 }
4028
4029error:
4030 /* Adjust marks. */
4031 if (y_type == MLINE)
4032 {
4033 curbuf->b_op_start.col = 0;
4034 if (dir == FORWARD)
4035 curbuf->b_op_start.lnum++;
4036 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004037 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004038 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004039 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004040 < curbuf->b_ml.ml_line_count
4041#ifdef FEAT_DIFF
4042 || curwin->w_p_diff
4043#endif
4044 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004045 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 (linenr_T)MAXLNUM, nr_lines, 0L);
4047
4048 /* note changed text for displaying and folding */
4049 if (y_type == MCHAR)
4050 changed_lines(curwin->w_cursor.lnum, col,
4051 curwin->w_cursor.lnum + 1, nr_lines);
4052 else
4053 changed_lines(curbuf->b_op_start.lnum, 0,
4054 curbuf->b_op_start.lnum, nr_lines);
4055
4056 /* put '] mark at last inserted character */
4057 curbuf->b_op_end.lnum = lnum;
4058 /* correct length for change in indent */
4059 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4060 if (col > 1)
4061 curbuf->b_op_end.col = col - 1;
4062 else
4063 curbuf->b_op_end.col = 0;
4064
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004065 if (flags & PUT_CURSLINE)
4066 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004067 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004068 curwin->w_cursor.lnum = lnum;
4069 beginline(BL_WHITE | BL_FIX);
4070 }
4071 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
4073 /* put cursor after inserted text */
4074 if (y_type == MLINE)
4075 {
4076 if (lnum >= curbuf->b_ml.ml_line_count)
4077 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4078 else
4079 curwin->w_cursor.lnum = lnum + 1;
4080 curwin->w_cursor.col = 0;
4081 }
4082 else
4083 {
4084 curwin->w_cursor.lnum = lnum;
4085 curwin->w_cursor.col = col;
4086 }
4087 }
4088 else if (y_type == MLINE)
4089 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004090 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 curwin->w_cursor.col = 0;
4092 if (dir == FORWARD)
4093 ++curwin->w_cursor.lnum;
4094 beginline(BL_WHITE | BL_FIX);
4095 }
4096 else /* put cursor on first inserted character */
4097 curwin->w_cursor = new_cursor;
4098 }
4099 }
4100
4101 msgmore(nr_lines);
4102 curwin->w_set_curswant = TRUE;
4103
4104end:
4105 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004107 if (regname == '=')
4108 vim_free(y_array);
4109
Bram Moolenaar033d8882013-09-25 23:24:57 +02004110 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004111
Bram Moolenaar677ee682005-01-27 14:41:15 +00004112 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004113 adjust_cursor_eol();
4114}
4115
4116/*
4117 * When the cursor is on the NUL past the end of the line and it should not be
4118 * there move it left.
4119 */
4120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004122{
4123 if (curwin->w_cursor.col > 0
4124 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004125#ifdef FEAT_VIRTUALEDIT
4126 && (ve_flags & VE_ONEMORE) == 0
4127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 && !(restart_edit || (State & INSERT)))
4129 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004130 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004131 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#ifdef FEAT_VIRTUALEDIT
4134 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004135 {
4136 colnr_T scol, ecol;
4137
4138 /* Coladd is set to the width of the last character. */
4139 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4140 curwin->w_cursor.coladd = ecol - scol + 1;
4141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#endif
4143 }
4144}
4145
4146#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4147/*
4148 * Return TRUE if lines starting with '#' should be left aligned.
4149 */
4150 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004151preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 return
4154# ifdef FEAT_SMARTINDENT
4155# ifdef FEAT_CINDENT
4156 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4157# else
4158 curbuf->b_p_si
4159# endif
4160# endif
4161# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004162 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4163 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164# endif
4165 ;
4166}
4167#endif
4168
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004169/*
4170 * Return the character name of the register with the given number.
4171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004173get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 if (num == -1)
4176 return '"';
4177 else if (num < 10)
4178 return num + '0';
4179 else if (num == DELETION_REGISTER)
4180 return '-';
4181#ifdef FEAT_CLIPBOARD
4182 else if (num == STAR_REGISTER)
4183 return '*';
4184 else if (num == PLUS_REGISTER)
4185 return '+';
4186#endif
4187 else
4188 {
4189#ifdef EBCDIC
4190 int i;
4191
4192 /* EBCDIC is really braindead ... */
4193 i = 'a' + (num - 10);
4194 if (i > 'i')
4195 i += 7;
4196 if (i > 'r')
4197 i += 8;
4198 return i;
4199#else
4200 return num + 'a' - 10;
4201#endif
4202 }
4203}
4204
4205/*
4206 * ":dis" and ":registers": Display the contents of the yank registers.
4207 */
4208 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004209ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004211 int i, n;
4212 long j;
4213 char_u *p;
4214 yankreg_T *yb;
4215 int name;
4216 int attr;
4217 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004218#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004219 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004220#else
4221# define clen 1
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 if (arg != NULL && *arg == NUL)
4225 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004226 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /* Highlight title */
4229 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4230 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4231 {
4232 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004233 if (arg != NULL && vim_strchr(arg, name) == NULL
4234#ifdef ONE_CLIPBOARD
4235 /* Star register and plus register contain the same thing. */
4236 && (name != '*' || vim_strchr(arg, '+') == NULL)
4237#endif
4238 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 continue; /* did not ask for this register */
4240
4241#ifdef FEAT_CLIPBOARD
4242 /* Adjust register name for "unnamed" in 'clipboard'.
4243 * When it's a clipboard register, fill it with the current contents
4244 * of the clipboard. */
4245 adjust_clip_reg(&name);
4246 (void)may_get_selection(name);
4247#endif
4248
4249 if (i == -1)
4250 {
4251 if (y_previous != NULL)
4252 yb = y_previous;
4253 else
4254 yb = &(y_regs[0]);
4255 }
4256 else
4257 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004258
4259#ifdef FEAT_EVAL
4260 if (name == MB_TOLOWER(redir_reg)
4261 || (redir_reg == '"' && yb == y_previous))
4262 continue; /* do not list register being written to, the
4263 * pointer can be freed */
4264#endif
4265
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (yb->y_array != NULL)
4267 {
4268 msg_putchar('\n');
4269 msg_putchar('"');
4270 msg_putchar(name);
4271 MSG_PUTS(" ");
4272
4273 n = (int)Columns - 6;
4274 for (j = 0; j < yb->y_size && n > 1; ++j)
4275 {
4276 if (j)
4277 {
4278 MSG_PUTS_ATTR("^J", attr);
4279 n -= 2;
4280 }
4281 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004284 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004285#endif
4286 msg_outtrans_len(p, clen);
4287#ifdef FEAT_MBYTE
4288 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#endif
4290 }
4291 }
4292 if (n > 1 && yb->y_type == MLINE)
4293 MSG_PUTS_ATTR("^J", attr);
4294 out_flush(); /* show one line at a time */
4295 }
4296 ui_breakcheck();
4297 }
4298
4299 /*
4300 * display last inserted text
4301 */
4302 if ((p = get_last_insert()) != NULL
4303 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4304 {
4305 MSG_PUTS("\n\". ");
4306 dis_msg(p, TRUE);
4307 }
4308
4309 /*
4310 * display last command line
4311 */
4312 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4313 && !got_int)
4314 {
4315 MSG_PUTS("\n\": ");
4316 dis_msg(last_cmdline, FALSE);
4317 }
4318
4319 /*
4320 * display current file name
4321 */
4322 if (curbuf->b_fname != NULL
4323 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4324 {
4325 MSG_PUTS("\n\"% ");
4326 dis_msg(curbuf->b_fname, FALSE);
4327 }
4328
4329 /*
4330 * display alternate file name
4331 */
4332 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4333 {
4334 char_u *fname;
4335 linenr_T dummy;
4336
4337 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4338 {
4339 MSG_PUTS("\n\"# ");
4340 dis_msg(fname, FALSE);
4341 }
4342 }
4343
4344 /*
4345 * display last search pattern
4346 */
4347 if (last_search_pat() != NULL
4348 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4349 {
4350 MSG_PUTS("\n\"/ ");
4351 dis_msg(last_search_pat(), FALSE);
4352 }
4353
4354#ifdef FEAT_EVAL
4355 /*
4356 * display last used expression
4357 */
4358 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4359 && !got_int)
4360 {
4361 MSG_PUTS("\n\"= ");
4362 dis_msg(expr_line, FALSE);
4363 }
4364#endif
4365}
4366
4367/*
4368 * display a string for do_dis()
4369 * truncate at end of screen line
4370 */
4371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004372dis_msg(
4373 char_u *p,
4374 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 int n;
4377#ifdef FEAT_MBYTE
4378 int l;
4379#endif
4380
4381 n = (int)Columns - 6;
4382 while (*p != NUL
4383 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4384 && (n -= ptr2cells(p)) >= 0)
4385 {
4386#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004387 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
4389 msg_outtrans_len(p, l);
4390 p += l;
4391 }
4392 else
4393#endif
4394 msg_outtrans_len(p++, 1);
4395 }
4396 ui_breakcheck();
4397}
4398
Bram Moolenaar81340392012-06-06 16:12:59 +02004399#if defined(FEAT_COMMENTS) || defined(PROTO)
4400/*
4401 * If "process" is TRUE and the line begins with a comment leader (possibly
4402 * after some white space), return a pointer to the text after it. Put a boolean
4403 * value indicating whether the line ends with an unclosed comment in
4404 * "is_comment".
4405 * line - line to be processed,
4406 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004407 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004408 * include_space - whether to also skip space following the comment leader,
4409 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004410 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004411 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004412 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004413skip_comment(
4414 char_u *line,
4415 int process,
4416 int include_space,
4417 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004418{
4419 char_u *comment_flags = NULL;
4420 int lead_len;
4421 int leader_offset = get_last_leader_offset(line, &comment_flags);
4422
4423 *is_comment = FALSE;
4424 if (leader_offset != -1)
4425 {
4426 /* Let's check whether the line ends with an unclosed comment.
4427 * If the last comment leader has COM_END in flags, there's no comment.
4428 */
4429 while (*comment_flags)
4430 {
4431 if (*comment_flags == COM_END
4432 || *comment_flags == ':')
4433 break;
4434 ++comment_flags;
4435 }
4436 if (*comment_flags != COM_END)
4437 *is_comment = TRUE;
4438 }
4439
4440 if (process == FALSE)
4441 return line;
4442
4443 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4444
4445 if (lead_len == 0)
4446 return line;
4447
4448 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004449 * - COM_END,
4450 * - colon,
4451 * whichever comes first.
4452 */
4453 while (*comment_flags)
4454 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004455 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004456 || *comment_flags == ':')
4457 {
4458 break;
4459 }
4460 ++comment_flags;
4461 }
4462
4463 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004464 * starting with a closing part of a three-part comment. That's good,
4465 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004466 */
4467 if (*comment_flags == ':' || *comment_flags == NUL)
4468 line += lead_len;
4469
4470 return line;
4471}
4472#endif
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004475 * Join 'count' lines (minimal 2) at cursor position.
4476 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004477 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4478 * leaders should not be removed.
4479 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4480 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004482 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
4484 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004485do_join(
4486 long count,
4487 int insert_space,
4488 int save_undo,
4489 int use_formatoptions UNUSED,
4490 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004492 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004493 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004494 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004496 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004497 int endcurr1 = NUL;
4498 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004499 int currsize = 0; /* size of the current line */
4500 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 colnr_T col = 0;
4503 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004504#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004505 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004506 int remove_comments = (use_formatoptions == TRUE)
4507 && has_format_option(FO_REMOVE_COMS);
4508 int prev_was_comment;
4509#endif
4510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004512 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4513 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4514 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004516 /* Allocate an array to store the number of spaces inserted before each
4517 * line. We will use it to pre-compute the length of the new line and the
4518 * proper placement of each original line in the new one. */
4519 spaces = lalloc_clear((long_u)count, TRUE);
4520 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004522#if defined(FEAT_COMMENTS) || defined(PROTO)
4523 if (remove_comments)
4524 {
4525 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4526 if (comments == NULL)
4527 {
4528 vim_free(spaces);
4529 return FAIL;
4530 }
4531 }
4532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004535 * Don't move anything, just compute the final line length
4536 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538 for (t = 0; t < count; ++t)
4539 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004540 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004541 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004542 {
4543 /* Set the '[ mark. */
4544 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4545 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4546 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004547#if defined(FEAT_COMMENTS) || defined(PROTO)
4548 if (remove_comments)
4549 {
4550 /* We don't want to remove the comment leader if the
4551 * previous line is not a comment. */
4552 if (t > 0 && prev_was_comment)
4553 {
4554
4555 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4556 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004557 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004558 curr = new_curr;
4559 }
4560 else
4561 curr = skip_comment(curr, FALSE, insert_space,
4562 &prev_was_comment);
4563 }
4564#endif
4565
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004566 if (insert_space && t > 0)
4567 {
4568 curr = skipwhite(curr);
4569 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4570#ifdef FEAT_MBYTE
4571 && (!has_format_option(FO_MBYTE_JOIN)
4572 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4573 && (!has_format_option(FO_MBYTE_JOIN2)
4574 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4575#endif
4576 )
4577 {
4578 /* don't add a space if the line is ending in a space */
4579 if (endcurr1 == ' ')
4580 endcurr1 = endcurr2;
4581 else
4582 ++spaces[t];
4583 /* extra space when 'joinspaces' set and line ends in '.' */
4584 if ( p_js
4585 && (endcurr1 == '.'
4586 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4587 && (endcurr1 == '?' || endcurr1 == '!'))))
4588 ++spaces[t];
4589 }
4590 }
4591 currsize = (int)STRLEN(curr);
4592 sumsize += currsize + spaces[t];
4593 endcurr1 = endcurr2 = NUL;
4594 if (insert_space && currsize > 0)
4595 {
4596#ifdef FEAT_MBYTE
4597 if (has_mbyte)
4598 {
4599 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004600 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004601 endcurr1 = (*mb_ptr2char)(cend);
4602 if (cend > curr)
4603 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004604 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004605 endcurr2 = (*mb_ptr2char)(cend);
4606 }
4607 }
4608 else
4609#endif
4610 {
4611 endcurr1 = *(curr + currsize - 1);
4612 if (currsize > 1)
4613 endcurr2 = *(curr + currsize - 2);
4614 }
4615 }
4616 line_breakcheck();
4617 if (got_int)
4618 {
4619 ret = FAIL;
4620 goto theend;
4621 }
4622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004624 /* store the column position before last line */
4625 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004627 /* allocate the space for the new line */
4628 newp = alloc_check((unsigned)(sumsize + 1));
4629 cend = newp + sumsize;
4630 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004632 /*
4633 * Move affected lines to the new long one.
4634 *
4635 * Move marks from each deleted line to the joined line, adjusting the
4636 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4637 * should not really be a problem.
4638 */
4639 for (t = count - 1; ; --t)
4640 {
4641 cend -= currsize;
4642 mch_memmove(cend, curr, (size_t)currsize);
4643 if (spaces[t] > 0)
4644 {
4645 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004646 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004647 }
4648 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004649 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004650 if (t == 0)
4651 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004652 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004653#if defined(FEAT_COMMENTS) || defined(PROTO)
4654 if (remove_comments)
4655 curr += comments[t - 1];
4656#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004657 if (insert_space && t > 1)
4658 curr = skipwhite(curr);
4659 currsize = (int)STRLEN(curr);
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4662
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004663 if (setmark)
4664 {
4665 /* Set the '] mark. */
4666 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4667 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4668 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 /* Only report the change in the first line here, del_lines() will report
4671 * the deleted line. */
4672 changed_lines(curwin->w_cursor.lnum, currsize,
4673 curwin->w_cursor.lnum + 1, 0L);
4674
4675 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004676 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * briefly, and then move it back. After del_lines() the cursor may
4678 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004682 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_cursor.lnum = t;
4684
4685 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004686 * Set the cursor column:
4687 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004688 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004690 curwin->w_cursor.col =
4691 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#ifdef FEAT_VIRTUALEDIT
4695 curwin->w_cursor.coladd = 0;
4696#endif
4697 curwin->w_set_curswant = TRUE;
4698
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004699theend:
4700 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004701#if defined(FEAT_COMMENTS) || defined(PROTO)
4702 if (remove_comments)
4703 vim_free(comments);
4704#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004705 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706}
4707
4708#ifdef FEAT_COMMENTS
4709/*
4710 * Return TRUE if the two comment leaders given are the same. "lnum" is
4711 * the first line. White-space is ignored. Note that the whole of
4712 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4713 */
4714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004715same_leader(
4716 linenr_T lnum,
4717 int leader1_len,
4718 char_u *leader1_flags,
4719 int leader2_len,
4720 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 int idx1 = 0, idx2 = 0;
4723 char_u *p;
4724 char_u *line1;
4725 char_u *line2;
4726
4727 if (leader1_len == 0)
4728 return (leader2_len == 0);
4729
4730 /*
4731 * If first leader has 'f' flag, the lines can be joined only if the
4732 * second line does not have a leader.
4733 * If first leader has 'e' flag, the lines can never be joined.
4734 * If fist leader has 's' flag, the lines can only be joined if there is
4735 * some text after it and the second line has the 'm' flag.
4736 */
4737 if (leader1_flags != NULL)
4738 {
4739 for (p = leader1_flags; *p && *p != ':'; ++p)
4740 {
4741 if (*p == COM_FIRST)
4742 return (leader2_len == 0);
4743 if (*p == COM_END)
4744 return FALSE;
4745 if (*p == COM_START)
4746 {
4747 if (*(ml_get(lnum) + leader1_len) == NUL)
4748 return FALSE;
4749 if (leader2_flags == NULL || leader2_len == 0)
4750 return FALSE;
4751 for (p = leader2_flags; *p && *p != ':'; ++p)
4752 if (*p == COM_MIDDLE)
4753 return TRUE;
4754 return FALSE;
4755 }
4756 }
4757 }
4758
4759 /*
4760 * Get current line and next line, compare the leaders.
4761 * The first line has to be saved, only one line can be locked at a time.
4762 */
4763 line1 = vim_strsave(ml_get(lnum));
4764 if (line1 != NULL)
4765 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004766 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 ;
4768 line2 = ml_get(lnum + 1);
4769 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4770 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004771 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
4773 if (line1[idx1++] != line2[idx2])
4774 break;
4775 }
4776 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004777 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 ++idx1;
4779 }
4780 vim_free(line1);
4781 }
4782 return (idx2 == leader2_len && idx1 == leader1_len);
4783}
4784#endif
4785
4786/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004787 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 */
4789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004790op_format(
4791 oparg_T *oap,
4792 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 long old_line_count = curbuf->b_ml.ml_line_count;
4795
4796 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4797 * can put it back there. */
4798 curwin->w_cursor = oap->cursor_start;
4799
4800 if (u_save((linenr_T)(oap->start.lnum - 1),
4801 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4802 return;
4803 curwin->w_cursor = oap->start;
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (oap->is_VIsual)
4806 /* When there is no change: need to remove the Visual selection */
4807 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 /* Set '[ mark at the start of the formatted area */
4810 curbuf->b_op_start = oap->start;
4811
4812 /* For "gw" remember the cursor position and put it back below (adjusted
4813 * for joined and split lines). */
4814 if (keep_cursor)
4815 saved_cursor = oap->cursor_start;
4816
Bram Moolenaar81a82092008-03-12 16:27:00 +00004817 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819 /*
4820 * Leave the cursor at the first non-blank of the last formatted line.
4821 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4822 * line, so "." will do the next lines.
4823 */
4824 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4825 ++curwin->w_cursor.lnum;
4826 beginline(BL_WHITE | BL_FIX);
4827 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4828 msgmore(old_line_count);
4829
4830 /* put '] mark on the end of the formatted area */
4831 curbuf->b_op_end = curwin->w_cursor;
4832
4833 if (keep_cursor)
4834 {
4835 curwin->w_cursor = saved_cursor;
4836 saved_cursor.lnum = 0;
4837 }
4838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (oap->is_VIsual)
4840 {
4841 win_T *wp;
4842
4843 FOR_ALL_WINDOWS(wp)
4844 {
4845 if (wp->w_old_cursor_lnum != 0)
4846 {
4847 /* When lines have been inserted or deleted, adjust the end of
4848 * the Visual area to be redrawn. */
4849 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4850 wp->w_old_cursor_lnum += old_line_count;
4851 else
4852 wp->w_old_visual_lnum += old_line_count;
4853 }
4854 }
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856}
4857
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004858#if defined(FEAT_EVAL) || defined(PROTO)
4859/*
4860 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4861 */
4862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004863op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004864{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004865 if (oap->is_VIsual)
4866 /* When there is no change: need to remove the Visual selection */
4867 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004868
Bram Moolenaar700303e2010-07-11 17:35:50 +02004869 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4870 /* As documented: when 'formatexpr' returns non-zero fall back to
4871 * internal formatting. */
4872 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004873}
4874
4875 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004876fex_format(
4877 linenr_T lnum,
4878 long count,
4879 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004880{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004881 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4882 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004883 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004884 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004885
4886 /*
4887 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004888 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004889 */
4890 set_vim_var_nr(VV_LNUM, lnum);
4891 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004892 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004893
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004894 /* Make a copy, the option could be changed while calling it. */
4895 fex = vim_strsave(curbuf->b_p_fex);
4896 if (fex == NULL)
4897 return 0;
4898
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004899 /*
4900 * Evaluate the function.
4901 */
4902 if (use_sandbox)
4903 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004904 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004905 if (use_sandbox)
4906 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004907
4908 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004909 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004910
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004911 return r;
4912}
4913#endif
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915/*
4916 * Format "line_count" lines, starting at the cursor position.
4917 * When "line_count" is negative, format until the end of the paragraph.
4918 * Lines after the cursor line are saved for undo, caller must have saved the
4919 * first line.
4920 */
4921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004922format_lines(
4923 linenr_T line_count,
4924 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 int max_len;
4927 int is_not_par; /* current line not part of parag. */
4928 int next_is_not_par; /* next line not part of paragraph */
4929 int is_end_par; /* at end of paragraph */
4930 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4931 int next_is_start_par = FALSE;
4932#ifdef FEAT_COMMENTS
4933 int leader_len = 0; /* leader len of current line */
4934 int next_leader_len; /* leader len of next line */
4935 char_u *leader_flags = NULL; /* flags for leader of current line */
4936 char_u *next_leader_flags; /* flags for leader of next line */
4937 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004938 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#endif
4940 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004941 int second_indent = -1; /* indent for second line (comment
4942 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 int do_second_indent;
4944 int do_number_indent;
4945 int do_trail_white;
4946 int first_par_line = TRUE;
4947 int smd_save;
4948 long count;
4949 int need_set_indent = TRUE; /* set indent of next paragraph */
4950 int force_format = FALSE;
4951 int old_State = State;
4952
4953 /* length of a line to force formatting: 3 * 'tw' */
4954 max_len = comp_textwidth(TRUE) * 3;
4955
4956 /* check for 'q', '2' and '1' in 'formatoptions' */
4957#ifdef FEAT_COMMENTS
4958 do_comments = has_format_option(FO_Q_COMS);
4959#endif
4960 do_second_indent = has_format_option(FO_Q_SECOND);
4961 do_number_indent = has_format_option(FO_Q_NUMBER);
4962 do_trail_white = has_format_option(FO_WHITE_PAR);
4963
4964 /*
4965 * Get info about the previous and current line.
4966 */
4967 if (curwin->w_cursor.lnum > 1)
4968 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4969#ifdef FEAT_COMMENTS
4970 , &leader_len, &leader_flags, do_comments
4971#endif
4972 );
4973 else
4974 is_not_par = TRUE;
4975 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4976#ifdef FEAT_COMMENTS
4977 , &next_leader_len, &next_leader_flags, do_comments
4978#endif
4979 );
4980 is_end_par = (is_not_par || next_is_not_par);
4981 if (!is_end_par && do_trail_white)
4982 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4983
4984 curwin->w_cursor.lnum--;
4985 for (count = line_count; count != 0 && !got_int; --count)
4986 {
4987 /*
4988 * Advance to next paragraph.
4989 */
4990 if (advance)
4991 {
4992 curwin->w_cursor.lnum++;
4993 prev_is_end_par = is_end_par;
4994 is_not_par = next_is_not_par;
4995#ifdef FEAT_COMMENTS
4996 leader_len = next_leader_len;
4997 leader_flags = next_leader_flags;
4998#endif
4999 }
5000
5001 /*
5002 * The last line to be formatted.
5003 */
5004 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5005 {
5006 next_is_not_par = TRUE;
5007#ifdef FEAT_COMMENTS
5008 next_leader_len = 0;
5009 next_leader_flags = NULL;
5010#endif
5011 }
5012 else
5013 {
5014 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5015#ifdef FEAT_COMMENTS
5016 , &next_leader_len, &next_leader_flags, do_comments
5017#endif
5018 );
5019 if (do_number_indent)
5020 next_is_start_par =
5021 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5022 }
5023 advance = TRUE;
5024 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5025 if (!is_end_par && do_trail_white)
5026 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5027
5028 /*
5029 * Skip lines that are not in a paragraph.
5030 */
5031 if (is_not_par)
5032 {
5033 if (line_count < 0)
5034 break;
5035 }
5036 else
5037 {
5038 /*
5039 * For the first line of a paragraph, check indent of second line.
5040 * Don't do this for comments and empty lines.
5041 */
5042 if (first_par_line
5043 && (do_second_indent || do_number_indent)
5044 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005045 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005047 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005048 {
5049#ifdef FEAT_COMMENTS
5050 if (leader_len == 0 && next_leader_len == 0)
5051 {
5052 /* no comment found */
5053#endif
5054 second_indent =
5055 get_indent_lnum(curwin->w_cursor.lnum + 1);
5056#ifdef FEAT_COMMENTS
5057 }
5058 else
5059 {
5060 second_indent = next_leader_len;
5061 do_comments_list = 1;
5062 }
5063#endif
5064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005066 {
5067#ifdef FEAT_COMMENTS
5068 if (leader_len == 0 && next_leader_len == 0)
5069 {
5070 /* no comment found */
5071#endif
5072 second_indent =
5073 get_number_indent(curwin->w_cursor.lnum);
5074#ifdef FEAT_COMMENTS
5075 }
5076 else
5077 {
5078 /* get_number_indent() is now "comment aware"... */
5079 second_indent =
5080 get_number_indent(curwin->w_cursor.lnum);
5081 do_comments_list = 1;
5082 }
5083#endif
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086
5087 /*
5088 * When the comment leader changes, it's the end of the paragraph.
5089 */
5090 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5091#ifdef FEAT_COMMENTS
5092 || !same_leader(curwin->w_cursor.lnum,
5093 leader_len, leader_flags,
5094 next_leader_len, next_leader_flags)
5095#endif
5096 )
5097 is_end_par = TRUE;
5098
5099 /*
5100 * If we have got to the end of a paragraph, or the line is
5101 * getting long, format it.
5102 */
5103 if (is_end_par || force_format)
5104 {
5105 if (need_set_indent)
5106 /* replace indent in first line with minimal number of
5107 * tabs and spaces, according to current options */
5108 (void)set_indent(get_indent(), SIN_CHANGED);
5109
5110 /* put cursor on last non-space */
5111 State = NORMAL; /* don't go past end-of-line */
5112 coladvance((colnr_T)MAXCOL);
5113 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5114 dec_cursor();
5115
5116 /* do the formatting, without 'showmode' */
5117 State = INSERT; /* for open_line() */
5118 smd_save = p_smd;
5119 p_smd = FALSE;
5120 insertchar(NUL, INSCHAR_FORMAT
5121#ifdef FEAT_COMMENTS
5122 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005123 + (do_comments && do_comments_list
5124 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005126 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 State = old_State;
5128 p_smd = smd_save;
5129 second_indent = -1;
5130 /* at end of par.: need to set indent of next par. */
5131 need_set_indent = is_end_par;
5132 if (is_end_par)
5133 {
5134 /* When called with a negative line count, break at the
5135 * end of the paragraph. */
5136 if (line_count < 0)
5137 break;
5138 first_par_line = TRUE;
5139 }
5140 force_format = FALSE;
5141 }
5142
5143 /*
5144 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005145 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 */
5147 if (!is_end_par)
5148 {
5149 advance = FALSE;
5150 curwin->w_cursor.lnum++;
5151 curwin->w_cursor.col = 0;
5152 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005156 {
5157 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5159 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005160 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005162 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5163 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005164 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005165
5166 if (indent > 0)
5167 {
5168 (void)del_bytes(indent, FALSE, FALSE);
5169 mark_col_adjust(curwin->w_cursor.lnum,
5170 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005171 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005174 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 beep_flush();
5177 break;
5178 }
5179 first_par_line = FALSE;
5180 /* If the line is getting long, format it next time */
5181 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5182 force_format = TRUE;
5183 else
5184 force_format = FALSE;
5185 }
5186 }
5187 line_breakcheck();
5188 }
5189}
5190
5191/*
5192 * Return TRUE if line "lnum" ends in a white character.
5193 */
5194 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005195ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 char_u *s = ml_get(lnum);
5198 size_t l;
5199
5200 if (*s == NUL)
5201 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005202 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 * invocation may call function multiple times". */
5204 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005205 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206}
5207
5208/*
5209 * Blank lines, and lines containing only the comment leader, are left
5210 * untouched by the formatting. The function returns TRUE in this
5211 * case. It also returns TRUE when a line starts with the end of a comment
5212 * ('e' in comment flags), so that this line is skipped, and not joined to the
5213 * previous line. A new paragraph starts after a blank line, or when the
5214 * comment leader changes -- webb.
5215 */
5216#ifdef FEAT_COMMENTS
5217 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005218fmt_check_par(
5219 linenr_T lnum,
5220 int *leader_len,
5221 char_u **leader_flags,
5222 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 char_u *flags = NULL; /* init for GCC */
5225 char_u *ptr;
5226
5227 ptr = ml_get(lnum);
5228 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005229 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 else
5231 *leader_len = 0;
5232
5233 if (*leader_len > 0)
5234 {
5235 /*
5236 * Search for 'e' flag in comment leader flags.
5237 */
5238 flags = *leader_flags;
5239 while (*flags && *flags != ':' && *flags != COM_END)
5240 ++flags;
5241 }
5242
5243 return (*skipwhite(ptr + *leader_len) == NUL
5244 || (*leader_len > 0 && *flags == COM_END)
5245 || startPS(lnum, NUL, FALSE));
5246}
5247#else
5248 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005249fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5252}
5253#endif
5254
5255/*
5256 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5257 * previous line is in the same paragraph. Used for auto-formatting.
5258 */
5259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 char_u *p;
5263#ifdef FEAT_COMMENTS
5264 int leader_len = 0; /* leader len of current line */
5265 char_u *leader_flags = NULL; /* flags for leader of current line */
5266 int next_leader_len; /* leader len of next line */
5267 char_u *next_leader_flags; /* flags for leader of next line */
5268 int do_comments; /* format comments */
5269#endif
5270
5271 if (lnum <= 1)
5272 return TRUE; /* start of the file */
5273
5274 p = ml_get(lnum - 1);
5275 if (*p == NUL)
5276 return TRUE; /* after empty line */
5277
5278#ifdef FEAT_COMMENTS
5279 do_comments = has_format_option(FO_Q_COMS);
5280#endif
5281 if (fmt_check_par(lnum - 1
5282#ifdef FEAT_COMMENTS
5283 , &leader_len, &leader_flags, do_comments
5284#endif
5285 ))
5286 return TRUE; /* after non-paragraph line */
5287
5288 if (fmt_check_par(lnum
5289#ifdef FEAT_COMMENTS
5290 , &next_leader_len, &next_leader_flags, do_comments
5291#endif
5292 ))
5293 return TRUE; /* "lnum" is not a paragraph line */
5294
5295 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5296 return TRUE; /* missing trailing space in previous line. */
5297
5298 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5299 return TRUE; /* numbered item starts in "lnum". */
5300
5301#ifdef FEAT_COMMENTS
5302 if (!same_leader(lnum - 1, leader_len, leader_flags,
5303 next_leader_len, next_leader_flags))
5304 return TRUE; /* change of comment leader. */
5305#endif
5306
5307 return FALSE;
5308}
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310/*
5311 * prepare a few things for block mode yank/delete/tilde
5312 *
5313 * for delete:
5314 * - textlen includes the first/last char to be (partly) deleted
5315 * - start/endspaces is the number of columns that are taken by the
5316 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005317 * deleted.
5318 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 * - textlen includes the first/last char to be wholly yanked
5320 * - start/endspaces is the number of columns of the first/last yanked char
5321 * that are to be yanked.
5322 */
5323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005324block_prep(
5325 oparg_T *oap,
5326 struct block_def *bdp,
5327 linenr_T lnum,
5328 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 int incr = 0;
5331 char_u *pend;
5332 char_u *pstart;
5333 char_u *line;
5334 char_u *prev_pstart;
5335 char_u *prev_pend;
5336
5337 bdp->startspaces = 0;
5338 bdp->endspaces = 0;
5339 bdp->textlen = 0;
5340 bdp->start_vcol = 0;
5341 bdp->end_vcol = 0;
5342#ifdef FEAT_VISUALEXTRA
5343 bdp->is_short = FALSE;
5344 bdp->is_oneChar = FALSE;
5345 bdp->pre_whitesp = 0;
5346 bdp->pre_whitesp_c = 0;
5347 bdp->end_char_vcols = 0;
5348#endif
5349 bdp->start_char_vcols = 0;
5350
5351 line = ml_get(lnum);
5352 pstart = line;
5353 prev_pstart = line;
5354 while (bdp->start_vcol < oap->start_vcol && *pstart)
5355 {
5356 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005357 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 bdp->start_vcol += incr;
5359#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005360 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 bdp->pre_whitesp += incr;
5363 bdp->pre_whitesp_c++;
5364 }
5365 else
5366 {
5367 bdp->pre_whitesp = 0;
5368 bdp->pre_whitesp_c = 0;
5369 }
5370#endif
5371 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005372 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 bdp->start_char_vcols = incr;
5375 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5376 {
5377 bdp->end_vcol = bdp->start_vcol;
5378#ifdef FEAT_VISUALEXTRA
5379 bdp->is_short = TRUE;
5380#endif
5381 if (!is_del || oap->op_type == OP_APPEND)
5382 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5383 }
5384 else
5385 {
5386 /* notice: this converts partly selected Multibyte characters to
5387 * spaces, too. */
5388 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5389 if (is_del && bdp->startspaces)
5390 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5391 pend = pstart;
5392 bdp->end_vcol = bdp->start_vcol;
5393 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5394 {
5395#ifdef FEAT_VISUALEXTRA
5396 bdp->is_oneChar = TRUE;
5397#endif
5398 if (oap->op_type == OP_INSERT)
5399 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5400 else if (oap->op_type == OP_APPEND)
5401 {
5402 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5403 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5404 }
5405 else
5406 {
5407 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5408 if (is_del && oap->op_type != OP_LSHIFT)
5409 {
5410 /* just putting the sum of those two into
5411 * bdp->startspaces doesn't work for Visual replace,
5412 * so we have to split the tab in two */
5413 bdp->startspaces = bdp->start_char_vcols
5414 - (bdp->start_vcol - oap->start_vcol);
5415 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5416 }
5417 }
5418 }
5419 else
5420 {
5421 prev_pend = pend;
5422 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5423 {
5424 /* Count a tab for what it's worth (if list mode not on) */
5425 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005426 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 bdp->end_vcol += incr;
5428 }
5429 if (bdp->end_vcol <= oap->end_vcol
5430 && (!is_del
5431 || oap->op_type == OP_APPEND
5432 || oap->op_type == OP_REPLACE)) /* line too short */
5433 {
5434#ifdef FEAT_VISUALEXTRA
5435 bdp->is_short = TRUE;
5436#endif
5437 /* Alternative: include spaces to fill up the block.
5438 * Disadvantage: can lead to trailing spaces when the line is
5439 * short where the text is put */
5440 /* if (!is_del || oap->op_type == OP_APPEND) */
5441 if (oap->op_type == OP_APPEND || virtual_op)
5442 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005443 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 else
5445 bdp->endspaces = 0; /* replace doesn't add characters */
5446 }
5447 else if (bdp->end_vcol > oap->end_vcol)
5448 {
5449 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5450 if (!is_del && bdp->endspaces)
5451 {
5452 bdp->endspaces = incr - bdp->endspaces;
5453 if (pend != pstart)
5454 pend = prev_pend;
5455 }
5456 }
5457 }
5458#ifdef FEAT_VISUALEXTRA
5459 bdp->end_char_vcols = incr;
5460#endif
5461 if (is_del && bdp->startspaces)
5462 pstart = prev_pstart;
5463 bdp->textlen = (int)(pend - pstart);
5464 }
5465 bdp->textcol = (colnr_T) (pstart - line);
5466 bdp->textstart = pstart;
5467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005470 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005472 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473op_addsub(
5474 oparg_T *oap,
5475 linenr_T Prenum1, /* Amount of add/subtract */
5476 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005478 pos_T pos;
5479 struct block_def bd;
5480 int change_cnt = 0;
5481 linenr_T amount = Prenum1;
5482
5483 if (!VIsual_active)
5484 {
5485 pos = curwin->w_cursor;
5486 if (u_save_cursor() == FAIL)
5487 return;
5488 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5489 if (change_cnt)
5490 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5491 }
5492 else
5493 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005494 int one_change;
5495 int length;
5496 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005497
5498 if (u_save((linenr_T)(oap->start.lnum - 1),
5499 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5500 return;
5501
5502 pos = oap->start;
5503 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5504 {
5505 if (oap->block_mode) /* Visual block mode */
5506 {
5507 block_prep(oap, &bd, pos.lnum, FALSE);
5508 pos.col = bd.textcol;
5509 length = bd.textlen;
5510 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005511 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005512 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005513 curwin->w_cursor.col = 0;
5514 pos.col = 0;
5515 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5516 }
5517 else /* oap->motion_type == MCHAR */
5518 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005519 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005520 dec(&(oap->end));
5521 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5522 pos.col = 0;
5523 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005524 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005525 pos.col += oap->start.col;
5526 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005527 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005528 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005529 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005530 length = (int)STRLEN(ml_get(oap->end.lnum));
5531 if (oap->end.col >= length)
5532 oap->end.col = length - 1;
5533 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005534 }
5535 }
5536 one_change = do_addsub(oap->op_type, &pos, length, amount);
5537 if (one_change)
5538 {
5539 /* Remember the start position of the first change. */
5540 if (change_cnt == 0)
5541 startpos = curbuf->b_op_start;
5542 ++change_cnt;
5543 }
5544
5545#ifdef FEAT_NETBEANS_INTG
5546 if (netbeans_active() && one_change)
5547 {
5548 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5549
5550 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5551 netbeans_inserted(curbuf, pos.lnum, pos.col,
5552 &ptr[pos.col], length);
5553 }
5554#endif
5555 if (g_cmd && one_change)
5556 amount += Prenum1;
5557 }
5558 if (change_cnt)
5559 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5560
5561 if (!change_cnt && oap->is_VIsual)
5562 /* No change: need to remove the Visual selection */
5563 redraw_curbuf_later(INVERTED);
5564
5565 /* Set '[ mark if something changed. Keep the last end
5566 * position from do_addsub(). */
5567 if (change_cnt > 0)
5568 curbuf->b_op_start = startpos;
5569
5570 if (change_cnt > p_report)
5571 {
5572 if (change_cnt == 1)
5573 MSG(_("1 line changed"));
5574 else
5575 smsg((char_u *)_("%ld lines changed"), change_cnt);
5576 }
5577 }
5578}
5579
5580/*
5581 * Add or subtract 'Prenum1' from a number in a line
5582 * op_type is OP_NR_ADD or OP_NR_SUB
5583 *
5584 * Returns TRUE if some character was changed.
5585 */
5586 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005587do_addsub(
5588 int op_type,
5589 pos_T *pos,
5590 int length,
5591 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005592{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 int col;
5594 char_u *buf1;
5595 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005596 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005598 uvarnumber_T n;
5599 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 char_u *ptr;
5601 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 int todel;
5603 int dohex;
5604 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005605 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 int doalp;
5607 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005609 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005610 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005611 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005612 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005613 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005614 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005615 pos_T startpos;
5616 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5619 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005620 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5622
Bram Moolenaard79e5502016-01-10 22:13:02 +01005623 curwin->w_cursor = *pos;
5624 ptr = ml_get(pos->lnum);
5625 col = pos->col;
5626
5627 if (*ptr == NUL)
5628 goto theend;
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 /*
5631 * First check if we are on a hexadecimal number, after the "0x".
5632 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005633 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005635 if (dobin)
5636 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005637 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005638 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005639#ifdef FEAT_MBYTE
5640 if (has_mbyte)
5641 col -= (*mb_head_off)(ptr, ptr + col);
5642#endif
5643 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005644
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005645 if (dohex)
5646 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005647 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005648 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005649#ifdef FEAT_MBYTE
5650 if (has_mbyte)
5651 col -= (*mb_head_off)(ptr, ptr + col);
5652#endif
5653 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005654
5655 if ( dobin
5656 && dohex
5657 && ! ((col > 0
5658 && (ptr[col] == 'X'
5659 || ptr[col] == 'x')
5660 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005661#ifdef FEAT_MBYTE
5662 && (!has_mbyte ||
5663 !(*mb_head_off)(ptr, ptr + col - 1))
5664#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005665 && vim_isxdigit(ptr[col + 1]))))
5666 {
5667
5668 /* In case of binary/hexadecimal pattern overlap match, rescan */
5669
Bram Moolenaard79e5502016-01-10 22:13:02 +01005670 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005671
5672 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005673 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005674 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005675#ifdef FEAT_MBYTE
5676 if (has_mbyte)
5677 col -= (*mb_head_off)(ptr, ptr + col);
5678#endif
5679 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005680 }
5681
5682 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005683 && col > 0
5684 && (ptr[col] == 'X'
5685 || ptr[col] == 'x')
5686 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005687#ifdef FEAT_MBYTE
5688 && (!has_mbyte ||
5689 !(*mb_head_off)(ptr, ptr + col - 1))
5690#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005691 && vim_isxdigit(ptr[col + 1])) ||
5692 ( dobin
5693 && col > 0
5694 && (ptr[col] == 'B'
5695 || ptr[col] == 'b')
5696 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005697#ifdef FEAT_MBYTE
5698 && (!has_mbyte ||
5699 !(*mb_head_off)(ptr, ptr + col - 1))
5700#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005701 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005702 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005703 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005704 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005705#ifdef FEAT_MBYTE
5706 if (has_mbyte)
5707 col -= (*mb_head_off)(ptr, ptr + col);
5708#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005709 }
5710 else
5711 {
5712 /*
5713 * Search forward and then backward to find the start of number.
5714 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005715 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005716
5717 while (ptr[col] != NUL
5718 && !vim_isdigit(ptr[col])
5719 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005720 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005721
5722 while (col > 0
5723 && vim_isdigit(ptr[col - 1])
5724 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005725 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005726 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005727#ifdef FEAT_MBYTE
5728 if (has_mbyte)
5729 col -= (*mb_head_off)(ptr, ptr + col);
5730#endif
5731 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005732 }
5733 }
5734
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005735 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005736 {
5737 while (ptr[col] != NUL && length > 0
5738 && !vim_isdigit(ptr[col])
5739 && !(doalp && ASCII_ISALPHA(ptr[col])))
5740 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005741 int mb_len = MB_PTR2LEN(ptr + col);
5742
5743 col += mb_len;
5744 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005745 }
5746
5747 if (length == 0)
5748 goto theend;
5749
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005750 if (col > pos->col && ptr[col - 1] == '-'
5751#ifdef FEAT_MBYTE
5752 && (!has_mbyte ||
5753 !(*mb_head_off)(ptr, ptr + col - 1))
5754#endif
5755 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005756 {
5757 negative = TRUE;
5758 was_positive = FALSE;
5759 }
5760 }
5761
5762 /*
5763 * If a number was found, and saving for undo works, replace the number.
5764 */
5765 firstdigit = ptr[col];
5766 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5767 {
5768 beep_flush();
5769 goto theend;
5770 }
5771
5772 if (doalp && ASCII_ISALPHA(firstdigit))
5773 {
5774 /* decrement or increment alphabetic character */
5775 if (op_type == OP_NR_SUB)
5776 {
5777 if (CharOrd(firstdigit) < Prenum1)
5778 {
5779 if (isupper(firstdigit))
5780 firstdigit = 'A';
5781 else
5782 firstdigit = 'a';
5783 }
5784 else
5785#ifdef EBCDIC
5786 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5787#else
5788 firstdigit -= Prenum1;
5789#endif
5790 }
5791 else
5792 {
5793 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5794 {
5795 if (isupper(firstdigit))
5796 firstdigit = 'Z';
5797 else
5798 firstdigit = 'z';
5799 }
5800 else
5801#ifdef EBCDIC
5802 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5803#else
5804 firstdigit += Prenum1;
5805#endif
5806 }
5807 curwin->w_cursor.col = col;
5808 if (!did_change)
5809 startpos = curwin->w_cursor;
5810 did_change = TRUE;
5811 (void)del_char(FALSE);
5812 ins_char(firstdigit);
5813 endpos = curwin->w_cursor;
5814 curwin->w_cursor.col = col;
5815 }
5816 else
5817 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005818 if (col > 0 && ptr[col - 1] == '-'
5819#ifdef FEAT_MBYTE
5820 && (!has_mbyte ||
5821 !(*mb_head_off)(ptr, ptr + col - 1))
5822#endif
5823 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005824 {
5825 /* negative number */
5826 --col;
5827 negative = TRUE;
5828 }
5829 /* get the number value (unsigned) */
5830 if (visual && VIsual_mode != 'V')
5831 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5832 ? (int)STRLEN(ptr) - col
5833 : length);
5834
5835 vim_str2nr(ptr + col, &pre, &length,
5836 0 + (dobin ? STR2NR_BIN : 0)
5837 + (dooct ? STR2NR_OCT : 0)
5838 + (dohex ? STR2NR_HEX : 0),
5839 NULL, &n, maxlen);
5840
5841 /* ignore leading '-' for hex and octal and bin numbers */
5842 if (pre && negative)
5843 {
5844 ++col;
5845 --length;
5846 negative = FALSE;
5847 }
5848 /* add or subtract */
5849 subtract = FALSE;
5850 if (op_type == OP_NR_SUB)
5851 subtract ^= TRUE;
5852 if (negative)
5853 subtract ^= TRUE;
5854
5855 oldn = n;
5856 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005857 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005858 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005859 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005860 /* handle wraparound for decimal numbers */
5861 if (!pre)
5862 {
5863 if (subtract)
5864 {
5865 if (n > oldn)
5866 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005867 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005868 negative ^= TRUE;
5869 }
5870 }
5871 else
5872 {
5873 /* add */
5874 if (n < oldn)
5875 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005876 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005877 negative ^= TRUE;
5878 }
5879 }
5880 if (n == 0)
5881 negative = FALSE;
5882 }
5883
5884 if (visual && !was_positive && !negative && col > 0)
5885 {
5886 /* need to remove the '-' */
5887 col--;
5888 length++;
5889 }
5890
5891 /*
5892 * Delete the old number.
5893 */
5894 curwin->w_cursor.col = col;
5895 if (!did_change)
5896 startpos = curwin->w_cursor;
5897 did_change = TRUE;
5898 todel = length;
5899 c = gchar_cursor();
5900 /*
5901 * Don't include the '-' in the length, only the length of the
5902 * part after it is kept the same.
5903 */
5904 if (c == '-')
5905 --length;
5906 while (todel-- > 0)
5907 {
5908 if (c < 0x100 && isalpha(c))
5909 {
5910 if (isupper(c))
5911 hexupper = TRUE;
5912 else
5913 hexupper = FALSE;
5914 }
5915 /* del_char() will mark line needing displaying */
5916 (void)del_char(FALSE);
5917 c = gchar_cursor();
5918 }
5919
5920 /*
5921 * Prepare the leading characters in buf1[].
5922 * When there are many leading zeros it could be very long.
5923 * Allocate a bit too much.
5924 */
5925 buf1 = alloc((unsigned)length + NUMBUFLEN);
5926 if (buf1 == NULL)
5927 goto theend;
5928 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005929 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005930 {
5931 *ptr++ = '-';
5932 }
5933 if (pre)
5934 {
5935 *ptr++ = '0';
5936 --length;
5937 }
5938 if (pre == 'b' || pre == 'B' ||
5939 pre == 'x' || pre == 'X')
5940 {
5941 *ptr++ = pre;
5942 --length;
5943 }
5944
5945 /*
5946 * Put the number characters in buf2[].
5947 */
5948 if (pre == 'b' || pre == 'B')
5949 {
5950 int i;
5951 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005952 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005953
5954 /* leading zeros */
5955 for (bit = bits; bit > 0; bit--)
5956 if ((n >> (bit - 1)) & 0x1) break;
5957
5958 for (i = 0; bit > 0; bit--)
5959 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5960
5961 buf2[i] = '\0';
5962 }
5963 else if (pre == 0)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005964 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005965 else if (pre == '0')
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005966 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005967 else if (pre && hexupper)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005968 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005969 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005970 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005971 length -= (int)STRLEN(buf2);
5972
5973 /*
5974 * Adjust number of zeros to the new number of digits, so the
5975 * total length of the number remains the same.
5976 * Don't do this when
5977 * the result may look like an octal number.
5978 */
5979 if (firstdigit == '0' && !(dooct && pre == 0))
5980 while (length-- > 0)
5981 *ptr++ = '0';
5982 *ptr = NUL;
5983 STRCAT(buf1, buf2);
5984 ins_str(buf1); /* insert the new number */
5985 vim_free(buf1);
5986 endpos = curwin->w_cursor;
5987 if (did_change && curwin->w_cursor.col)
5988 --curwin->w_cursor.col;
5989 }
5990
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005991 if (did_change)
5992 {
5993 /* set the '[ and '] marks */
5994 curbuf->b_op_start = startpos;
5995 curbuf->b_op_end = endpos;
5996 if (curbuf->b_op_end.col > 0)
5997 --curbuf->b_op_end.col;
5998 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005999
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006000theend:
6001 if (visual)
6002 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006003 else if (did_change)
6004 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006005
Bram Moolenaard79e5502016-01-10 22:13:02 +01006006 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007}
6008
6009#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006010
6011static yankreg_T *y_read_regs = NULL;
6012
6013#define REG_PREVIOUS 1
6014#define REG_EXEC 2
6015
6016/*
6017 * Prepare for reading viminfo registers when writing viminfo later.
6018 */
6019 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006020prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006021{
6022 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6023 * (int)sizeof(yankreg_T));
6024}
6025
6026 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006027finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006028{
6029 int i;
6030 int j;
6031
6032 if (y_read_regs != NULL)
6033 {
6034 for (i = 0; i < NUM_REGISTERS; ++i)
6035 if (y_read_regs[i].y_array != NULL)
6036 {
6037 for (j = 0; j < y_read_regs[i].y_size; j++)
6038 vim_free(y_read_regs[i].y_array[j]);
6039 vim_free(y_read_regs[i].y_array);
6040 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006041 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006042 }
6043}
6044
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006046read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 int eof;
6049 int do_it = TRUE;
6050 int size;
6051 int limit;
6052 int i;
6053 int set_prev = FALSE;
6054 char_u *str;
6055 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006056 int new_type = MCHAR; /* init to shut up compiler */
6057 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058
6059 /* We only get here (hopefully) if line[0] == '"' */
6060 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006061
6062 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (*str == '"')
6064 {
6065 set_prev = TRUE;
6066 str++;
6067 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (!ASCII_ISALNUM(*str) && *str != '-')
6070 {
6071 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6072 return TRUE; /* too many errors, pretend end-of-file */
6073 do_it = FALSE;
6074 }
6075 get_yank_register(*str++, FALSE);
6076 if (!force && y_current->y_array != NULL)
6077 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006078
6079 if (*str == '@')
6080 {
6081 /* "x@: register x used for @@ */
6082 if (force || execreg_lastc == NUL)
6083 execreg_lastc = str[-1];
6084 }
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 size = 0;
6087 limit = 100; /* Optimized for registers containing <= 100 lines */
6088 if (do_it)
6089 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006090 /*
6091 * Build the new register in array[].
6092 * y_array is kept as-is until done.
6093 * The "do_it" flag is reset when something is wrong, in which case
6094 * array[] needs to be freed.
6095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (set_prev)
6097 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006098 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006099 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006101 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006103 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006105 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 /* get the block width; if it's missing we get a zero, which is OK */
6107 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006108 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110
6111 while (!(eof = viminfo_readline(virp))
6112 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6113 {
6114 if (do_it)
6115 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006116 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006118 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006120
6121 if (new_array == NULL)
6122 {
6123 do_it = FALSE;
6124 break;
6125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006127 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006129 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
6132 str = viminfo_readstring(virp, 1, TRUE);
6133 if (str != NULL)
6134 array[size++] = str;
6135 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006136 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 do_it = FALSE;
6138 }
6139 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (do_it)
6142 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006143 /* free y_array[] */
6144 for (i = 0; i < y_current->y_size; i++)
6145 vim_free(y_current->y_array[i]);
6146 vim_free(y_current->y_array);
6147
6148 y_current->y_type = new_type;
6149 y_current->y_width = new_width;
6150 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006151 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (size == 0)
6153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 y_current->y_array = NULL;
6155 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006158 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 y_current->y_array =
6160 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6161 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006162 {
6163 if (y_current->y_array == NULL)
6164 vim_free(array[i]);
6165 else
6166 y_current->y_array[i] = array[i];
6167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006170 else
6171 {
6172 /* Free array[] if it was filled. */
6173 for (i = 0; i < size; i++)
6174 vim_free(array[i]);
6175 }
6176 vim_free(array);
6177
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return eof;
6179}
6180
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006181/*
6182 * Accept a new style register line from the viminfo, store it when it's new.
6183 */
6184 void
6185handle_viminfo_register(garray_T *values, int force)
6186{
6187 bval_T *vp = (bval_T *)values->ga_data;
6188 int flags;
6189 int name;
6190 int type;
6191 int linecount;
6192 int width;
6193 time_t timestamp;
6194 yankreg_T *y_ptr;
6195 int i;
6196
6197 /* Check the format:
6198 * |{bartype},{flags},{name},{type},
6199 * {linecount},{width},{timestamp},"line1","line2"
6200 */
6201 if (values->ga_len < 6
6202 || vp[0].bv_type != BVAL_NR
6203 || vp[1].bv_type != BVAL_NR
6204 || vp[2].bv_type != BVAL_NR
6205 || vp[3].bv_type != BVAL_NR
6206 || vp[4].bv_type != BVAL_NR
6207 || vp[5].bv_type != BVAL_NR)
6208 return;
6209 flags = vp[0].bv_nr;
6210 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006211 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006212 return;
6213 type = vp[2].bv_nr;
6214 if (type != MCHAR && type != MLINE && type != MBLOCK)
6215 return;
6216 linecount = vp[3].bv_nr;
6217 if (values->ga_len < 6 + linecount)
6218 return;
6219 width = vp[4].bv_nr;
6220 if (width < 0)
6221 return;
6222
6223 if (y_read_regs != NULL)
6224 /* Reading viminfo for merging and writing. Store the register
6225 * content, don't update the current registers. */
6226 y_ptr = &y_read_regs[name];
6227 else
6228 y_ptr = &y_regs[name];
6229
6230 /* Do not overwrite unless forced or the timestamp is newer. */
6231 timestamp = (time_t)vp[5].bv_nr;
6232 if (y_ptr->y_array != NULL && !force
6233 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6234 return;
6235
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006236 if (y_ptr->y_array != NULL)
6237 for (i = 0; i < y_ptr->y_size; i++)
6238 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006239 vim_free(y_ptr->y_array);
6240
6241 if (y_read_regs == NULL)
6242 {
6243 if (flags & REG_PREVIOUS)
6244 y_previous = y_ptr;
6245 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6246 execreg_lastc = get_register_name(name);
6247 }
6248 y_ptr->y_type = type;
6249 y_ptr->y_width = width;
6250 y_ptr->y_size = linecount;
6251 y_ptr->y_time_set = timestamp;
6252 if (linecount == 0)
6253 y_ptr->y_array = NULL;
6254 else
6255 {
6256 y_ptr->y_array =
6257 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6258 for (i = 0; i < linecount; i++)
6259 {
6260 if (vp[i + 6].bv_allocated)
6261 {
6262 y_ptr->y_array[i] = vp[i + 6].bv_string;
6263 vp[i + 6].bv_string = NULL;
6264 }
6265 else
6266 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6267 }
6268 }
6269}
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006272write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006274 int i, j;
6275 char_u *type;
6276 char_u c;
6277 int num_lines;
6278 int max_num_lines;
6279 int max_kbyte;
6280 long len;
6281 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282
Bram Moolenaar64404472010-06-26 06:24:45 +02006283 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284
6285 /* Get '<' value, use old '"' value if '<' is not found. */
6286 max_num_lines = get_viminfo_parameter('<');
6287 if (max_num_lines < 0)
6288 max_num_lines = get_viminfo_parameter('"');
6289 if (max_num_lines == 0)
6290 return;
6291 max_kbyte = get_viminfo_parameter('s');
6292 if (max_kbyte == 0)
6293 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006294
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 for (i = 0; i < NUM_REGISTERS; i++)
6296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297#ifdef FEAT_CLIPBOARD
6298 /* Skip '*'/'+' register, we don't want them back next time */
6299 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6300 continue;
6301#endif
6302#ifdef FEAT_DND
6303 /* Neither do we want the '~' register */
6304 if (i == TILDE_REGISTER)
6305 continue;
6306#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006307 /* When reading viminfo for merging and writing: Use the register from
6308 * viminfo if it's newer. */
6309 if (y_read_regs != NULL
6310 && y_read_regs[i].y_array != NULL
6311 && (y_regs[i].y_array == NULL ||
6312 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6313 y_ptr = &y_read_regs[i];
6314 else if (y_regs[i].y_array == NULL)
6315 continue;
6316 else
6317 y_ptr = &y_regs[i];
6318
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006319 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006320 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006321 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006322 || (num_lines == 1 && y_ptr->y_type == MCHAR
6323 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006324 continue;
6325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (max_kbyte > 0)
6327 {
6328 /* Skip register if there is more text than the maximum size. */
6329 len = 0;
6330 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006331 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 if (len > (long)max_kbyte * 1024L)
6333 continue;
6334 }
6335
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006336 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338 case MLINE:
6339 type = (char_u *)"LINE";
6340 break;
6341 case MCHAR:
6342 type = (char_u *)"CHAR";
6343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case MBLOCK:
6345 type = (char_u *)"BLOCK";
6346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 default:
6348 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006349 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 emsg(IObuff);
6351 type = (char_u *)"LINE";
6352 break;
6353 }
6354 if (y_previous == &y_regs[i])
6355 fprintf(fp, "\"");
6356 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006357 fprintf(fp, "\"%c", c);
6358 if (c == execreg_lastc)
6359 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006360 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 /* If max_num_lines < 0, then we save ALL the lines in the register */
6363 if (max_num_lines > 0 && num_lines > max_num_lines)
6364 num_lines = max_num_lines;
6365 for (j = 0; j < num_lines; j++)
6366 {
6367 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006368 viminfo_writestring(fp, y_ptr->y_array[j]);
6369 }
6370
6371 {
6372 int flags = 0;
6373 int remaining;
6374
6375 /* New style with a bar line. Format:
6376 * |{bartype},{flags},{name},{type},
6377 * {linecount},{width},{timestamp},"line1","line2"
6378 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006379 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006380 */
6381 if (y_previous == &y_regs[i])
6382 flags |= REG_PREVIOUS;
6383 if (c == execreg_lastc)
6384 flags |= REG_EXEC;
6385 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6386 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6387 (long)y_ptr->y_time_set);
6388 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6389 remaining = LSIZE - 71;
6390 for (j = 0; j < num_lines; j++)
6391 {
6392 putc(',', fp);
6393 --remaining;
6394 remaining = barline_writestring(fp, y_ptr->y_array[j],
6395 remaining);
6396 }
6397 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 }
6400}
6401#endif /* FEAT_VIMINFO */
6402
6403#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6404/*
6405 * SELECTION / PRIMARY ('*')
6406 *
6407 * Text selection stuff that uses the GUI selection register '*'. When using a
6408 * GUI this may be text from another window, otherwise it is the last text we
6409 * had highlighted with VIsual mode. With mouse support, clicking the middle
6410 * button performs the paste, otherwise you will need to do <"*p>. "
6411 * If not under X, it is synonymous with the clipboard register '+'.
6412 *
6413 * X CLIPBOARD ('+')
6414 *
6415 * Text selection stuff that uses the GUI clipboard register '+'.
6416 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6417 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6418 * otherwise you will need to do <"+p>. "
6419 * If not under X, it is synonymous with the selection register '*'.
6420 */
6421
6422/*
6423 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006424 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 * only exist while the owning application exists, so we write to the
6426 * permanent (while X runs) store CUT_BUFFER0.
6427 * Dump the CLIPBOARD selection if we own it (it's logically the more
6428 * 'permanent' of the two), otherwise the PRIMARY one.
6429 * For now, use a hard-coded sanity limit of 1Mb of data.
6430 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006431#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 Display *dpy;
6436 char_u *str = NULL;
6437 long_u len = 0;
6438 int motion_type = -1;
6439
6440# ifdef FEAT_GUI
6441 if (gui.in_use)
6442 dpy = X_DISPLAY;
6443 else
6444# endif
6445# ifdef FEAT_XCLIPBOARD
6446 dpy = xterm_dpy;
6447# else
6448 return;
6449# endif
6450
6451 /* Get selection to export */
6452 if (clip_plus.owned)
6453 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6454 else if (clip_star.owned)
6455 motion_type = clip_convert_selection(&str, &len, &clip_star);
6456
6457 /* Check it's OK */
6458 if (dpy != NULL && str != NULL && motion_type >= 0
6459 && len < 1024*1024 && len > 0)
6460 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006461#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006462 int ok = TRUE;
6463
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006464 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6465 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6466 * encoding conversion usually doesn't work, so keep the text as-is.
6467 */
6468 if (has_mbyte)
6469 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006470 vimconv_T vc;
6471
6472 vc.vc_type = CONV_NONE;
6473 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6474 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006475 int intlen = len;
6476 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006477
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006478 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006479 conv_str = string_convert(&vc, str, &intlen);
6480 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006481 if (conv_str != NULL)
6482 {
6483 vim_free(str);
6484 str = conv_str;
6485 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006486 else
6487 {
6488 ok = FALSE;
6489 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006490 convert_setup(&vc, NULL, NULL);
6491 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006492 else
6493 {
6494 ok = FALSE;
6495 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006496 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006497
6498 /* Do not store the string if conversion failed. Better to use any
6499 * other selection than garbled text. */
6500 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006501#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006502 {
6503 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6504 XFlush(dpy);
6505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 }
6507
6508 vim_free(str);
6509}
6510#endif
6511
6512 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006513clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006515 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
6517 if (cbd == &clip_plus)
6518 y_current = &y_regs[PLUS_REGISTER];
6519 else
6520 y_current = &y_regs[STAR_REGISTER];
6521 free_yank_all();
6522 y_current->y_size = 0;
6523 y_current = y_ptr;
6524}
6525
6526/*
6527 * Get the selected text and put it in the gui selection register '*' or '+'.
6528 */
6529 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006530clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006532 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 pos_T old_visual;
6535 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 colnr_T old_curswant;
6537 int old_set_curswant;
6538 pos_T old_op_start, old_op_end;
6539 oparg_T oa;
6540 cmdarg_T ca;
6541
6542 if (cbd->owned)
6543 {
6544 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6545 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6546 return;
6547
6548 /* Get the text between clip_star.start & clip_star.end */
6549 old_y_previous = y_previous;
6550 old_y_current = y_current;
6551 old_cursor = curwin->w_cursor;
6552 old_curswant = curwin->w_curswant;
6553 old_set_curswant = curwin->w_set_curswant;
6554 old_op_start = curbuf->b_op_start;
6555 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 old_visual = VIsual;
6557 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 clear_oparg(&oa);
6559 oa.regname = (cbd == &clip_plus ? '+' : '*');
6560 oa.op_type = OP_YANK;
6561 vim_memset(&ca, 0, sizeof(ca));
6562 ca.oap = &oa;
6563 ca.cmdchar = 'y';
6564 ca.count1 = 1;
6565 ca.retval = CA_NO_ADJ_OP_END;
6566 do_pending_operator(&ca, 0, TRUE);
6567 y_previous = old_y_previous;
6568 y_current = old_y_current;
6569 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006570 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 curwin->w_curswant = old_curswant;
6572 curwin->w_set_curswant = old_set_curswant;
6573 curbuf->b_op_start = old_op_start;
6574 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 VIsual = old_visual;
6576 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006578 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 clip_free_selection(cbd);
6581
6582 /* Try to get selected text from another window */
6583 clip_gen_request_selection(cbd);
6584 }
6585}
6586
Bram Moolenaard44347f2011-06-19 01:14:29 +02006587/*
6588 * Convert from the GUI selection string into the '*'/'+' register.
6589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591clip_yank_selection(
6592 int type,
6593 char_u *str,
6594 long len,
6595 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006597 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598
6599 if (cbd == &clip_plus)
6600 y_ptr = &y_regs[PLUS_REGISTER];
6601 else
6602 y_ptr = &y_regs[STAR_REGISTER];
6603
6604 clip_free_selection(cbd);
6605
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006606 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607}
6608
6609/*
6610 * Convert the '*'/'+' register into a GUI selection string returned in *str
6611 * with length *len.
6612 * Returns the motion type, or -1 for failure.
6613 */
6614 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006615clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 char_u *p;
6618 int lnum;
6619 int i, j;
6620 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006621 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
6623 if (cbd == &clip_plus)
6624 y_ptr = &y_regs[PLUS_REGISTER];
6625 else
6626 y_ptr = &y_regs[STAR_REGISTER];
6627
6628#ifdef USE_CRNL
6629 eolsize = 2;
6630#else
6631 eolsize = 1;
6632#endif
6633
6634 *str = NULL;
6635 *len = 0;
6636 if (y_ptr->y_array == NULL)
6637 return -1;
6638
6639 for (i = 0; i < y_ptr->y_size; i++)
6640 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6641
6642 /*
6643 * Don't want newline character at end of last line if we're in MCHAR mode.
6644 */
6645 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6646 *len -= eolsize;
6647
6648 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6649 if (p == NULL)
6650 return -1;
6651 lnum = 0;
6652 for (i = 0, j = 0; i < (int)*len; i++, j++)
6653 {
6654 if (y_ptr->y_array[lnum][j] == '\n')
6655 p[i] = NUL;
6656 else if (y_ptr->y_array[lnum][j] == NUL)
6657 {
6658#ifdef USE_CRNL
6659 p[i++] = '\r';
6660#endif
6661#ifdef USE_CR
6662 p[i] = '\r';
6663#else
6664 p[i] = '\n';
6665#endif
6666 lnum++;
6667 j = -1;
6668 }
6669 else
6670 p[i] = y_ptr->y_array[lnum][j];
6671 }
6672 return y_ptr->y_type;
6673}
6674
6675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676/*
6677 * If we have written to a clipboard register, send the text to the clipboard.
6678 */
6679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006680may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6683 {
6684 clip_own_selection(&clip_star);
6685 clip_gen_set_selection(&clip_star);
6686 }
6687 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6688 {
6689 clip_own_selection(&clip_plus);
6690 clip_gen_set_selection(&clip_plus);
6691 }
6692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694#endif /* FEAT_CLIPBOARD || PROTO */
6695
6696
6697#if defined(FEAT_DND) || defined(PROTO)
6698/*
6699 * Replace the contents of the '~' register with str.
6700 */
6701 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006704 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706 curr = y_current;
6707 y_current = &y_regs[TILDE_REGISTER];
6708 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006709 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 y_current = curr;
6711}
6712#endif
6713
6714
6715#if defined(FEAT_EVAL) || defined(PROTO)
6716/*
6717 * Return the type of a register.
6718 * Used for getregtype()
6719 * Returns MAUTO for error.
6720 */
6721 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 switch (regname)
6725 {
6726 case '%': /* file name */
6727 case '#': /* alternate file name */
6728 case '=': /* expression */
6729 case ':': /* last command line */
6730 case '/': /* last search-pattern */
6731 case '.': /* last inserted text */
6732#ifdef FEAT_SEARCHPATH
6733 case Ctrl_F: /* Filename under cursor */
6734 case Ctrl_P: /* Path under cursor, expand via "path" */
6735#endif
6736 case Ctrl_W: /* word under cursor */
6737 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6738 case '_': /* black hole: always empty */
6739 return MCHAR;
6740 }
6741
6742#ifdef FEAT_CLIPBOARD
6743 regname = may_get_selection(regname);
6744#endif
6745
Bram Moolenaar32b92012014-01-14 12:33:36 +01006746 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006747 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 get_yank_register(regname, FALSE);
6750
6751 if (y_current->y_array != NULL)
6752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (reglen != NULL && y_current->y_type == MBLOCK)
6754 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 return y_current->y_type;
6756 }
6757 return MAUTO;
6758}
6759
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006760static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006761
6762/*
6763 * When "flags" has GREG_LIST return a list with text "s".
6764 * Otherwise just return "s".
6765 */
6766 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006767getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006768{
6769 if (flags & GREG_LIST)
6770 {
6771 list_T *list = list_alloc();
6772
6773 if (list != NULL)
6774 {
6775 if (list_append_string(list, NULL, -1) == FAIL)
6776 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006777 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006778 return NULL;
6779 }
6780 list->lv_first->li_tv.vval.v_string = s;
6781 }
6782 return (char_u *)list;
6783 }
6784 return s;
6785}
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787/*
6788 * Return the contents of a register as a single allocated string.
6789 * Used for "@r" in expressions and for getreg().
6790 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006791 * Flags:
6792 * GREG_NO_EXPR Do not allow expression register
6793 * GREG_EXPR_SRC For the expression register: return expression itself,
6794 * not the result of its evaluation.
6795 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 */
6797 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006798get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 long i;
6801 char_u *retval;
6802 int allocated;
6803 long len;
6804
6805 /* Don't allow using an expression register inside an expression */
6806 if (regname == '=')
6807 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006808 if (flags & GREG_NO_EXPR)
6809 return NULL;
6810 if (flags & GREG_EXPR_SRC)
6811 return getreg_wrap_one_line(get_expr_line_src(), flags);
6812 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814
6815 if (regname == '@') /* "@@" is used for unnamed register */
6816 regname = '"';
6817
6818 /* check for valid regname */
6819 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6820 return NULL;
6821
6822#ifdef FEAT_CLIPBOARD
6823 regname = may_get_selection(regname);
6824#endif
6825
6826 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6827 {
6828 if (retval == NULL)
6829 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006830 if (allocated)
6831 return getreg_wrap_one_line(retval, flags);
6832 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834
6835 get_yank_register(regname, FALSE);
6836 if (y_current->y_array == NULL)
6837 return NULL;
6838
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006839 if (flags & GREG_LIST)
6840 {
6841 list_T *list = list_alloc();
6842 int error = FALSE;
6843
6844 if (list == NULL)
6845 return NULL;
6846 for (i = 0; i < y_current->y_size; ++i)
6847 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6848 error = TRUE;
6849 if (error)
6850 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006851 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006852 return NULL;
6853 }
6854 return (char_u *)list;
6855 }
6856
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 /*
6858 * Compute length of resulting string.
6859 */
6860 len = 0;
6861 for (i = 0; i < y_current->y_size; ++i)
6862 {
6863 len += (long)STRLEN(y_current->y_array[i]);
6864 /*
6865 * Insert a newline between lines and after last line if
6866 * y_type is MLINE.
6867 */
6868 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6869 ++len;
6870 }
6871
6872 retval = lalloc(len + 1, TRUE);
6873
6874 /*
6875 * Copy the lines of the yank register into the string.
6876 */
6877 if (retval != NULL)
6878 {
6879 len = 0;
6880 for (i = 0; i < y_current->y_size; ++i)
6881 {
6882 STRCPY(retval + len, y_current->y_array[i]);
6883 len += (long)STRLEN(retval + len);
6884
6885 /*
6886 * Insert a NL between lines and after the last line if y_type is
6887 * MLINE.
6888 */
6889 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6890 retval[len++] = '\n';
6891 }
6892 retval[len] = NUL;
6893 }
6894
6895 return retval;
6896}
6897
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006898 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006899init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006900 int name,
6901 yankreg_T **old_y_previous,
6902 yankreg_T **old_y_current,
6903 int must_append,
6904 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006905{
6906 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6907 {
6908 emsg_invreg(name);
6909 return FAIL;
6910 }
6911
6912 /* Don't want to change the current (unnamed) register */
6913 *old_y_previous = y_previous;
6914 *old_y_current = y_current;
6915
6916 get_yank_register(name, TRUE);
6917 if (!y_append && !must_append)
6918 free_yank_all();
6919 return OK;
6920}
6921
6922 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006923finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006924 int name,
6925 yankreg_T *old_y_previous,
6926 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006927{
6928# ifdef FEAT_CLIPBOARD
6929 /* Send text of clipboard register to the clipboard. */
6930 may_set_selection();
6931# endif
6932
6933 /* ':let @" = "val"' should change the meaning of the "" register */
6934 if (name != '"')
6935 y_previous = old_y_previous;
6936 y_current = old_y_current;
6937}
6938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939/*
6940 * Store string "str" in register "name".
6941 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6942 * If "must_append" is TRUE, always append to the register. Otherwise append
6943 * if "name" is an uppercase letter.
6944 * Note: "maxlen" and "must_append" don't work for the "/" register.
6945 * Careful: 'str' is modified, you may have to use a copy!
6946 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6947 */
6948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006949write_reg_contents(
6950 int name,
6951 char_u *str,
6952 int maxlen,
6953 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6956}
6957
6958 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006959write_reg_contents_lst(
6960 int name,
6961 char_u **strings,
6962 int maxlen UNUSED,
6963 int must_append,
6964 int yank_type,
6965 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006966{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006967 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006968
6969 if (name == '/'
6970#ifdef FEAT_EVAL
6971 || name == '='
6972#endif
6973 )
6974 {
6975 char_u *s;
6976
6977 if (strings[0] == NULL)
6978 s = (char_u *)"";
6979 else if (strings[1] != NULL)
6980 {
6981 EMSG(_("E883: search pattern and expression register may not "
6982 "contain two or more lines"));
6983 return;
6984 }
6985 else
6986 s = strings[0];
6987 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6988 return;
6989 }
6990
6991 if (name == '_') /* black hole: nothing to do */
6992 return;
6993
6994 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6995 &yank_type) == FAIL)
6996 return;
6997
6998 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6999
7000 finish_write_reg(name, old_y_previous, old_y_current);
7001}
7002
7003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004write_reg_contents_ex(
7005 int name,
7006 char_u *str,
7007 int maxlen,
7008 int must_append,
7009 int yank_type,
7010 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007012 yankreg_T *old_y_previous, *old_y_current;
7013 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaare7566042005-06-17 22:00:15 +00007015 if (maxlen >= 0)
7016 len = maxlen;
7017 else
7018 len = (long)STRLEN(str);
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 /* Special case: '/' search pattern */
7021 if (name == '/')
7022 {
7023 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7024 return;
7025 }
7026
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007027 if (name == '#')
7028 {
7029 buf_T *buf;
7030
7031 if (VIM_ISDIGIT(*str))
7032 {
7033 int num = atoi((char *)str);
7034
7035 buf = buflist_findnr(num);
7036 if (buf == NULL)
7037 EMSGN(_(e_nobufnr), (long)num);
7038 }
7039 else
7040 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7041 TRUE, FALSE, FALSE));
7042 if (buf == NULL)
7043 return;
7044 curwin->w_alt_fnum = buf->b_fnum;
7045 return;
7046 }
7047
Bram Moolenaare7566042005-06-17 22:00:15 +00007048#ifdef FEAT_EVAL
7049 if (name == '=')
7050 {
7051 char_u *p, *s;
7052
7053 p = vim_strnsave(str, (int)len);
7054 if (p == NULL)
7055 return;
7056 if (must_append)
7057 {
7058 s = concat_str(get_expr_line_src(), p);
7059 vim_free(p);
7060 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007061 }
7062 set_expr_line(p);
7063 return;
7064 }
7065#endif
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (name == '_') /* black hole: nothing to do */
7068 return;
7069
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007070 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7071 &yank_type) == FAIL)
7072 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007074 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007076 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077}
7078#endif /* FEAT_EVAL */
7079
7080#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7081/*
7082 * Put a string into a register. When the register is not empty, the string
7083 * is appended.
7084 */
7085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007086str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007087 yankreg_T *y_ptr, /* pointer to yank register */
7088 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7089 char_u *str, /* string to put in register */
7090 long len, /* length of string */
7091 long blocklen, /* width of Visual block */
7092 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007094 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 int lnum;
7096 long start;
7097 long i;
7098 int extra;
7099 int newlines; /* number of lines added */
7100 int extraline = 0; /* extra line at the end */
7101 int append = FALSE; /* append to last line in register */
7102 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007103 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007107 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 y_ptr->y_size = 0;
7109
Bram Moolenaard44347f2011-06-19 01:14:29 +02007110 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007111 type = ((str_list || (len > 0 && (str[len - 1] == NL
7112 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007113 ? MLINE : MCHAR);
7114 else
7115 type = yank_type;
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 /*
7118 * Count the number of lines within the string
7119 */
7120 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007121 if (str_list)
7122 {
7123 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007126 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007128 for (i = 0; i < len; i++)
7129 if (str[i] == '\n')
7130 ++newlines;
7131 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7132 {
7133 extraline = 1;
7134 ++newlines; /* count extra newline at the end */
7135 }
7136 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7137 {
7138 append = TRUE;
7139 --newlines; /* uncount newline when appending first line */
7140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007143 /* Without any lines make the register empty. */
7144 if (y_ptr->y_size + newlines == 0)
7145 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007146 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007147 return;
7148 }
7149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 /*
7151 * Allocate an array to hold the pointers to the new register lines.
7152 * If the register was not empty, move the existing lines to the new array.
7153 */
7154 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7155 * sizeof(char_u *), TRUE);
7156 if (pp == NULL) /* out of memory */
7157 return;
7158 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7159 pp[lnum] = y_ptr->y_array[lnum];
7160 vim_free(y_ptr->y_array);
7161 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164 /*
7165 * Find the end of each line and save it into the array.
7166 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007167 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007169 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7170 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007171 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007172 pp[lnum] = vim_strnsave(*ss, i);
7173 if (i > maxlen)
7174 maxlen = i;
7175 }
7176 }
7177 else
7178 {
7179 for (start = 0; start < len + extraline; start += i + 1)
7180 {
7181 for (i = start; i < len; ++i) /* find the end of the line */
7182 if (str[i] == '\n')
7183 break;
7184 i -= start; /* i is now length of line */
7185 if (i > maxlen)
7186 maxlen = i;
7187 if (append)
7188 {
7189 --lnum;
7190 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7191 }
7192 else
7193 extra = 0;
7194 s = alloc((unsigned)(i + extra + 1));
7195 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007197 if (extra)
7198 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7199 if (append)
7200 vim_free(y_ptr->y_array[lnum]);
7201 if (i)
7202 mch_memmove(s + extra, str + start, (size_t)i);
7203 extra += i;
7204 s[extra] = NUL;
7205 y_ptr->y_array[lnum++] = s;
7206 while (--extra >= 0)
7207 {
7208 if (*s == NUL)
7209 *s = '\n'; /* replace NUL with newline */
7210 ++s;
7211 }
7212 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215 y_ptr->y_type = type;
7216 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (type == MBLOCK)
7218 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7219 else
7220 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007221#ifdef FEAT_VIMINFO
7222 y_ptr->y_time_set = vim_time();
7223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224}
7225#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7226
7227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 vim_memset(oap, 0, sizeof(oparg_T));
7231}
7232
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007233static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, varnumber_T limit, int eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007236 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 *
7238 * "Words" are counted by looking for boundaries between non-space and
7239 * space characters. (it seems to produce results that match 'wc'.)
7240 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007241 * Return value is byte count; word count for the line is added to "*wc".
7242 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 *
7244 * The function will only examine the first "limit" characters in the
7245 * line, stopping if it encounters an end-of-line (NUL byte). In that
7246 * case, eol_size will be added to the character count to account for
7247 * the size of the EOL character.
7248 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007249 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007250line_count_info(
7251 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007252 varnumber_T *wc,
7253 varnumber_T *cc,
7254 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007255 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007257 varnumber_T i;
7258 varnumber_T words = 0;
7259 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 int is_word = 0;
7261
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007262 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
7264 if (is_word)
7265 {
7266 if (vim_isspace(line[i]))
7267 {
7268 words++;
7269 is_word = 0;
7270 }
7271 }
7272 else if (!vim_isspace(line[i]))
7273 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007274 ++chars;
7275#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007276 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007277#else
7278 ++i;
7279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281
7282 if (is_word)
7283 words++;
7284 *wc += words;
7285
7286 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007287 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007290 chars += eol_size;
7291 }
7292 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 return i;
7294}
7295
7296/*
7297 * Give some info about the position of the cursor (for "g CTRL-G").
7298 * In Visual mode, give some info about the selected region. (In this case,
7299 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007300 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 */
7302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007303cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
7305 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007306 char_u buf1[50];
7307 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007309 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007310#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007311 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007312#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007313 varnumber_T byte_count_cursor = 0;
7314 varnumber_T char_count = 0;
7315 varnumber_T char_count_cursor = 0;
7316 varnumber_T word_count = 0;
7317 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007318 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007319 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 long line_count_selected = 0;
7321 pos_T min_pos, max_pos;
7322 oparg_T oparg;
7323 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
7325 /*
7326 * Compute the length of the file in characters.
7327 */
7328 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7329 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007330 if (dict == NULL)
7331 {
7332 MSG(_(no_lines_msg));
7333 return;
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336 else
7337 {
7338 if (get_fileformat(curbuf) == EOL_DOS)
7339 eol_size = 2;
7340 else
7341 eol_size = 1;
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (VIsual_active)
7344 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007345 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 min_pos = VIsual;
7348 max_pos = curwin->w_cursor;
7349 }
7350 else
7351 {
7352 min_pos = curwin->w_cursor;
7353 max_pos = VIsual;
7354 }
7355 if (*p_sel == 'e' && max_pos.col > 0)
7356 --max_pos.col;
7357
7358 if (VIsual_mode == Ctrl_V)
7359 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007360#ifdef FEAT_LINEBREAK
7361 char_u * saved_sbr = p_sbr;
7362
7363 /* Make 'sbr' empty for a moment to get the correct size. */
7364 p_sbr = empty_option;
7365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 oparg.is_VIsual = 1;
7367 oparg.block_mode = TRUE;
7368 oparg.op_type = OP_NOP;
7369 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007370 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007371#ifdef FEAT_LINEBREAK
7372 p_sbr = saved_sbr;
7373#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007374 if (curwin->w_curswant == MAXCOL)
7375 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 /* Swap the start, end vcol if needed */
7377 if (oparg.end_vcol < oparg.start_vcol)
7378 {
7379 oparg.end_vcol += oparg.start_vcol;
7380 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7381 oparg.end_vcol -= oparg.start_vcol;
7382 }
7383 }
7384 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
7387 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7388 {
7389 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007390 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
7392 ui_breakcheck();
7393 if (got_int)
7394 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007395 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 /* Do extra processing for VIsual mode. */
7399 if (VIsual_active
7400 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7401 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007402 char_u *s = NULL;
7403 long len = 0L;
7404
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 switch (VIsual_mode)
7406 {
7407 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007408#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007412#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007414#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007415 s = bd.textstart;
7416 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 break;
7418 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007419 s = ml_get(lnum);
7420 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 break;
7422 case 'v':
7423 {
7424 colnr_T start_col = (lnum == min_pos.lnum)
7425 ? min_pos.col : 0;
7426 colnr_T end_col = (lnum == max_pos.lnum)
7427 ? max_pos.col - start_col + 1 : MAXCOL;
7428
Bram Moolenaardef9e822004-12-31 20:58:58 +00007429 s = ml_get(lnum) + start_col;
7430 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 break;
7433 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007434 if (s != NULL)
7435 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007436 byte_count_cursor += line_count_info(s, &word_count_cursor,
7437 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007438 if (lnum == curbuf->b_ml.ml_line_count
7439 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007440 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007441 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007442 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
7445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
7447 /* In non-visual mode, check for the line the cursor is on */
7448 if (lnum == curwin->w_cursor.lnum)
7449 {
7450 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007451 char_count_cursor += char_count;
7452 byte_count_cursor = byte_count +
7453 line_count_info(ml_get(lnum),
7454 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007455 (varnumber_T)(curwin->w_cursor.col + 1),
7456 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 }
7458 }
7459 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007460 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007461 &char_count, (varnumber_T)MAXCOL,
7462 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464
7465 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007466 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007467 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
Bram Moolenaared767a22016-01-03 22:49:16 +01007469 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007471 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007473 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7474 {
7475 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7476 &max_pos.col);
7477 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7478 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7479 }
7480 else
7481 buf1[0] = NUL;
7482
7483 if (char_count_cursor == byte_count_cursor
7484 && char_count == byte_count)
7485 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007486 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007487 buf1, line_count_selected,
7488 (long)curbuf->b_ml.ml_line_count,
7489 word_count_cursor, word_count,
7490 byte_count_cursor, byte_count);
7491 else
7492 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007493 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007494 buf1, line_count_selected,
7495 (long)curbuf->b_ml.ml_line_count,
7496 word_count_cursor, word_count,
7497 char_count_cursor, char_count,
7498 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007501 {
7502 p = ml_get_curline();
7503 validate_virtcol();
7504 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7505 (int)curwin->w_virtcol + 1);
7506 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7507 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
Bram Moolenaared767a22016-01-03 22:49:16 +01007509 if (char_count_cursor == byte_count_cursor
7510 && char_count == byte_count)
7511 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007512 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007513 (char *)buf1, (char *)buf2,
7514 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 (long)curbuf->b_ml.ml_line_count,
7516 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007517 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007518 else
7519 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007520 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007521 (char *)buf1, (char *)buf2,
7522 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007523 (long)curbuf->b_ml.ml_line_count,
7524 word_count_cursor, word_count,
7525 char_count_cursor, char_count,
7526 byte_count_cursor, byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007527 }
7528 }
7529
Bram Moolenaared767a22016-01-03 22:49:16 +01007530#ifdef FEAT_MBYTE
7531 bom_count = bomb_size();
7532 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007533 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
7534 _("(+%ld for BOM)"), bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007535#endif
7536 if (dict == NULL)
7537 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007538 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007539 p = p_shm;
7540 p_shm = (char_u *)"";
7541 msg(IObuff);
7542 p_shm = p;
7543 }
7544 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007545#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007546 if (dict != NULL)
7547 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007548 dict_add_nr_str(dict, "words", word_count, NULL);
7549 dict_add_nr_str(dict, "chars", char_count, NULL);
7550 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007551# ifdef FEAT_MBYTE
7552 + bom_count
7553# endif
7554 , NULL);
7555 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007556 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007557 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007558 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007559 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007560 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563}