blob: 1bc51d84809ab17b8e40dcfbd54f3578c95e9305 [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
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100902 *
903 * Return TRUE when the register should be inserted literally (selection or
904 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100910 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 y_append = FALSE;
913 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
914 {
915 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100916 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918 i = regname;
919 if (VIM_ISDIGIT(i))
920 i -= '0';
921 else if (ASCII_ISLOWER(i))
922 i = CharOrdLow(i) + 10;
923 else if (ASCII_ISUPPER(i))
924 {
925 i = CharOrdUp(i) + 10;
926 y_append = TRUE;
927 }
928 else if (regname == '-')
929 i = DELETION_REGISTER;
930#ifdef FEAT_CLIPBOARD
931 /* When selection is not available, use register 0 instead of '*' */
932 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100935 ret = TRUE;
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 /* When clipboard is not available, use register 0 instead of '+' */
938 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100941 ret = TRUE;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944#ifdef FEAT_DND
945 else if (!writing && regname == '~')
946 i = TILDE_REGISTER;
947#endif
948 else /* not 0-9, a-z, A-Z or '-': use register 0 */
949 i = 0;
950 y_current = &(y_regs[i]);
951 if (writing) /* remember the register we write into for do_put() */
952 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100953 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
Bram Moolenaar8299df92004-07-10 09:47:34 +0000956#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * When "regname" is a clipboard register, obtain the selection. If it's not
959 * available return zero, otherwise return "regname".
960 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100962may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 if (regname == '*')
965 {
966 if (!clip_star.available)
967 regname = 0;
968 else
969 clip_get_selection(&clip_star);
970 }
971 else if (regname == '+')
972 {
973 if (!clip_plus.available)
974 regname = 0;
975 else
976 clip_get_selection(&clip_plus);
977 }
978 return regname;
979}
980#endif
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Obtain the contents of a "normal" register. The register is made empty.
984 * The returned pointer has allocated memory, use put_register() later.
985 */
986 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987get_register(
988 int name,
989 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200991 yankreg_T *reg;
992 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994#ifdef FEAT_CLIPBOARD
995 /* When Visual area changed, may have to update selection. Obtain the
996 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000997 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200999 if (clip_isautosel_star())
1000 clip_update_selection(&clip_star);
1001 may_get_selection(name);
1002 }
1003 if (name == '+' && clip_plus.available)
1004 {
1005 if (clip_isautosel_plus())
1006 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 may_get_selection(name);
1008 }
1009#endif
1010
1011 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001012 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (reg != NULL)
1014 {
1015 *reg = *y_current;
1016 if (copy)
1017 {
1018 /* If we run out of memory some or all of the lines are empty. */
1019 if (reg->y_size == 0)
1020 reg->y_array = NULL;
1021 else
1022 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1023 * reg->y_size));
1024 if (reg->y_array != NULL)
1025 {
1026 for (i = 0; i < reg->y_size; ++i)
1027 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1028 }
1029 }
1030 else
1031 y_current->y_array = NULL;
1032 }
1033 return (void *)reg;
1034}
1035
1036/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001037 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 */
1039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 get_yank_register(name, 0);
1043 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001044 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001045 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001047#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /* Send text written to clipboard register to the clipboard. */
1049 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001052
1053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001054free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001055{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001056 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001057
1058 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001059 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001060 free_yank_all();
1061 vim_free(reg);
1062 *y_current = tmp;
1063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065#if defined(FEAT_MOUSE) || defined(PROTO)
1066/*
1067 * return TRUE if the current yank register has type MLINE
1068 */
1069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1073 return FALSE;
1074 if (regname == '_') /* black hole is always empty */
1075 return FALSE;
1076 get_yank_register(regname, FALSE);
1077 return (y_current->y_type == MLINE);
1078}
1079#endif
1080
1081/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001082 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001084 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
1086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaard55de222007-05-06 13:38:48 +00001089 char_u *p;
1090 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001091 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001094 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001096 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1098 retval = FAIL;
1099 else
1100 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001101 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 showmode();
1103 regname = c;
1104 retval = OK;
1105 }
1106 }
1107 else /* stop recording */
1108 {
1109 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001110 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1111 * needs to be removed again to put it in a register. exec_reg then
1112 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001114 reg_recording = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 MSG("");
1116 p = get_recorded();
1117 if (p == NULL)
1118 retval = FAIL;
1119 else
1120 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001121 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1122 vim_unescape_csi(p);
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 /*
1125 * We don't want to change the default register here, so save and
1126 * restore the current register name.
1127 */
1128 old_y_previous = y_previous;
1129 old_y_current = y_current;
1130
1131 retval = stuff_yank(regname, p);
1132
1133 y_previous = old_y_previous;
1134 y_current = old_y_current;
1135 }
1136 }
1137 return retval;
1138}
1139
1140/*
1141 * Stuff string "p" into yank register "regname" as a single line (append if
1142 * uppercase). "p" must have been alloced.
1143 *
1144 * return FAIL for failure, OK otherwise
1145 */
1146 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001147stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 char_u *lp;
1150 char_u **pp;
1151
1152 /* check for read-only register */
1153 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1154 {
1155 vim_free(p);
1156 return FAIL;
1157 }
1158 if (regname == '_') /* black hole: don't do anything */
1159 {
1160 vim_free(p);
1161 return OK;
1162 }
1163 get_yank_register(regname, TRUE);
1164 if (y_append && y_current->y_array != NULL)
1165 {
1166 pp = &(y_current->y_array[y_current->y_size - 1]);
1167 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1168 if (lp == NULL)
1169 {
1170 vim_free(p);
1171 return FAIL;
1172 }
1173 STRCPY(lp, *pp);
1174 STRCAT(lp, p);
1175 vim_free(p);
1176 vim_free(*pp);
1177 *pp = lp;
1178 }
1179 else
1180 {
1181 free_yank_all();
1182 if ((y_current->y_array =
1183 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1184 {
1185 vim_free(p);
1186 return FAIL;
1187 }
1188 y_current->y_array[0] = p;
1189 y_current->y_size = 1;
1190 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001191#ifdef FEAT_VIMINFO
1192 y_current->y_time_set = vim_time();
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 return OK;
1196}
1197
Bram Moolenaar42b94362009-05-26 16:12:37 +00001198static int execreg_lastc = NUL;
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001201 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001203 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
1205 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001206do_execreg(
1207 int regname,
1208 int colon, /* insert ':' before each line */
1209 int addcr, /* always add '\n' to end of line */
1210 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 long i;
1213 char_u *p;
1214 int retval = OK;
1215 int remap;
1216
1217 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001218 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001219 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001220 {
1221 EMSG(_("E748: No previously used register"));
1222 return FAIL;
1223 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001224 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* check for valid regname */
1227 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001228 {
1229 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001231 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001232 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234#ifdef FEAT_CLIPBOARD
1235 regname = may_get_selection(regname);
1236#endif
1237
1238 if (regname == '_') /* black hole: don't stuff anything */
1239 return OK;
1240
1241#ifdef FEAT_CMDHIST
1242 if (regname == ':') /* use last command line */
1243 {
1244 if (last_cmdline == NULL)
1245 {
1246 EMSG(_(e_nolastcmd));
1247 return FAIL;
1248 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001249 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001250 /* Escape all control characters with a CTRL-V */
1251 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001252 (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 +00001253 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001254 {
1255 /* When in Visual mode "'<,'>" will be prepended to the command.
1256 * Remove it when it's already there. */
1257 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001258 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001259 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001260 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001261 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001262 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264#endif
1265#ifdef FEAT_EVAL
1266 else if (regname == '=')
1267 {
1268 p = get_expr_line();
1269 if (p == NULL)
1270 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 vim_free(p);
1273 }
1274#endif
1275 else if (regname == '.') /* use last inserted text */
1276 {
1277 p = get_last_insert_save();
1278 if (p == NULL)
1279 {
1280 EMSG(_(e_noinstext));
1281 return FAIL;
1282 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(p);
1285 }
1286 else
1287 {
1288 get_yank_register(regname, FALSE);
1289 if (y_current->y_array == NULL)
1290 return FAIL;
1291
1292 /* Disallow remaping for ":@r". */
1293 remap = colon ? REMAP_NONE : REMAP_YES;
1294
1295 /*
1296 * Insert lines into typeahead buffer, from last one to first one.
1297 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001298 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 for (i = y_current->y_size; --i >= 0; )
1300 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001301 char_u *escaped;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /* insert NL between lines and after last line if type is MLINE */
1304 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1305 || addcr)
1306 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 return FAIL;
1309 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001310 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1311 if (escaped == NULL)
1312 return FAIL;
1313 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1314 vim_free(escaped);
1315 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001317 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 == FAIL)
1319 return FAIL;
1320 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001321 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 return retval;
1324}
1325
1326/*
1327 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1328 * used only after other typeahead has been processed.
1329 */
1330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001331put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 char_u buf[3];
1334
1335 if (restart_edit != NUL)
1336 {
1337 if (restart_edit == 'V')
1338 {
1339 buf[0] = 'g';
1340 buf[1] = 'R';
1341 buf[2] = NUL;
1342 }
1343 else
1344 {
1345 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1346 buf[1] = NUL;
1347 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001348 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 restart_edit = NUL;
1350 }
1351}
1352
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353/*
1354 * Insert register contents "s" into the typeahead buffer, so that it will be
1355 * executed again.
1356 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1357 * no remapping.
1358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360put_in_typebuf(
1361 char_u *s,
1362 int esc,
1363 int colon, /* add ':' before the line */
1364 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int retval = OK;
1367
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001368 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001372 {
1373 char_u *p;
1374
1375 if (esc)
1376 p = vim_strsave_escape_csi(s);
1377 else
1378 p = s;
1379 if (p == NULL)
1380 retval = FAIL;
1381 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001382 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1383 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001384 if (esc)
1385 vim_free(p);
1386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001388 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return retval;
1390}
1391
1392/*
1393 * Insert a yank register: copy it into the Read buffer.
1394 * Used by CTRL-R command and middle mouse button in insert mode.
1395 *
1396 * return FAIL for failure, OK otherwise
1397 */
1398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001399insert_reg(
1400 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001401 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 long i;
1404 int retval = OK;
1405 char_u *arg;
1406 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001407 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 /*
1410 * It is possible to get into an endless loop by having CTRL-R a in
1411 * register a and then, in insert mode, doing CTRL-R a.
1412 * If you hit CTRL-C, the loop will be broken here.
1413 */
1414 ui_breakcheck();
1415 if (got_int)
1416 return FAIL;
1417
1418 /* check for valid regname */
1419 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1420 return FAIL;
1421
1422#ifdef FEAT_CLIPBOARD
1423 regname = may_get_selection(regname);
1424#endif
1425
1426 if (regname == '.') /* insert last inserted text */
1427 retval = stuff_inserted(NUL, 1L, TRUE);
1428 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1429 {
1430 if (arg == NULL)
1431 return FAIL;
1432 stuffescaped(arg, literally);
1433 if (allocated)
1434 vim_free(arg);
1435 }
1436 else /* name or number register */
1437 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001438 if (get_yank_register(regname, FALSE))
1439 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (y_current->y_array == NULL)
1441 retval = FAIL;
1442 else
1443 {
1444 for (i = 0; i < y_current->y_size; ++i)
1445 {
1446 stuffescaped(y_current->y_array[i], literally);
1447 /*
1448 * Insert a newline between lines and after last line if
1449 * y_type is MLINE.
1450 */
1451 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1452 stuffcharReadbuff('\n');
1453 }
1454 }
1455 }
1456
1457 return retval;
1458}
1459
1460/*
1461 * Stuff a string into the typeahead buffer, such that edit() will insert it
1462 * literally ("literally" TRUE) or interpret is as typed characters.
1463 */
1464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001465stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 int c;
1468 char_u *start;
1469
1470 while (*arg != NUL)
1471 {
1472 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1473 * stuff K_SPECIAL to get the effect of a special key when "literally"
1474 * is TRUE. */
1475 start = arg;
1476 while ((*arg >= ' '
1477#ifndef EBCDIC
1478 && *arg < DEL /* EBCDIC: chars above space are normal */
1479#endif
1480 )
1481 || (*arg == K_SPECIAL && !literally))
1482 ++arg;
1483 if (arg > start)
1484 stuffReadbuffLen(start, (long)(arg - start));
1485
1486 /* stuff a single special character */
1487 if (*arg != NUL)
1488 {
1489#ifdef FEAT_MBYTE
1490 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001491 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 else
1493#endif
1494 c = *arg++;
1495 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1496 stuffcharReadbuff(Ctrl_V);
1497 stuffcharReadbuff(c);
1498 }
1499 }
1500}
1501
1502/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001503 * If "regname" is a special register, return TRUE and store a pointer to its
1504 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507get_spec_reg(
1508 int regname,
1509 char_u **argp,
1510 int *allocated, /* return: TRUE when value was allocated */
1511 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int cnt;
1514
1515 *argp = NULL;
1516 *allocated = FALSE;
1517 switch (regname)
1518 {
1519 case '%': /* file name */
1520 if (errmsg)
1521 check_fname(); /* will give emsg if not set */
1522 *argp = curbuf->b_fname;
1523 return TRUE;
1524
1525 case '#': /* alternate file name */
1526 *argp = getaltfname(errmsg); /* may give emsg if not set */
1527 return TRUE;
1528
1529#ifdef FEAT_EVAL
1530 case '=': /* result of expression */
1531 *argp = get_expr_line();
1532 *allocated = TRUE;
1533 return TRUE;
1534#endif
1535
1536 case ':': /* last command line */
1537 if (last_cmdline == NULL && errmsg)
1538 EMSG(_(e_nolastcmd));
1539 *argp = last_cmdline;
1540 return TRUE;
1541
1542 case '/': /* last search-pattern */
1543 if (last_search_pat() == NULL && errmsg)
1544 EMSG(_(e_noprevre));
1545 *argp = last_search_pat();
1546 return TRUE;
1547
1548 case '.': /* last inserted text */
1549 *argp = get_last_insert_save();
1550 *allocated = TRUE;
1551 if (*argp == NULL && errmsg)
1552 EMSG(_(e_noinstext));
1553 return TRUE;
1554
1555#ifdef FEAT_SEARCHPATH
1556 case Ctrl_F: /* Filename under cursor */
1557 case Ctrl_P: /* Path under cursor, expand via "path" */
1558 if (!errmsg)
1559 return FALSE;
1560 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001561 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 *allocated = TRUE;
1563 return TRUE;
1564#endif
1565
1566 case Ctrl_W: /* word under cursor */
1567 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1568 if (!errmsg)
1569 return FALSE;
1570 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1571 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1572 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1573 *allocated = TRUE;
1574 return TRUE;
1575
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001576 case Ctrl_L: /* Line under cursor */
1577 if (!errmsg)
1578 return FALSE;
1579
1580 *argp = ml_get_buf(curwin->w_buffer,
1581 curwin->w_cursor.lnum, FALSE);
1582 return TRUE;
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 case '_': /* black hole: always empty */
1585 *argp = (char_u *)"";
1586 return TRUE;
1587 }
1588
1589 return FALSE;
1590}
1591
1592/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001593 * Paste a yank register into the command line.
1594 * Only for non-special registers.
1595 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * insert_reg() can't be used here, because special characters from the
1597 * register contents will be interpreted as commands.
1598 *
1599 * return FAIL for failure, OK otherwise
1600 */
1601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602cmdline_paste_reg(
1603 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001604 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001605 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001608 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001610 if (get_yank_register(regname, FALSE))
1611 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (y_current->y_array == NULL)
1613 return FAIL;
1614
1615 for (i = 0; i < y_current->y_size; ++i)
1616 {
1617 cmdline_paste_str(y_current->y_array[i], literally);
1618
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001619 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001620 * Don't do this when "remcr" is TRUE. */
1621 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 cmdline_paste_str((char_u *)"\r", literally);
1623
1624 /* Check for CTRL-C, in case someone tries to paste a few thousand
1625 * lines and gets bored. */
1626 ui_breakcheck();
1627 if (got_int)
1628 return FAIL;
1629 }
1630 return OK;
1631}
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1634/*
1635 * Adjust the register name pointed to with "rp" for the clipboard being
1636 * used always and the clipboard being available.
1637 */
1638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001639adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001641 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1642 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001643 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1644 {
1645 if (clip_unnamed != 0)
1646 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001647 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001648 else
1649 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1650 ? '+' : '*';
1651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (!clip_star.available && *rp == '*')
1653 *rp = 0;
1654 if (!clip_plus.available && *rp == '+')
1655 *rp = 0;
1656}
1657#endif
1658
1659/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001660 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1661 */
1662 void
1663shift_delete_registers()
1664{
1665 int n;
1666
1667 y_current = &y_regs[9];
1668 free_yank_all(); /* free register nine */
1669 for (n = 9; n > 1; --n)
1670 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001671 y_current = &y_regs[1];
1672 if (!y_append)
1673 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001674 y_regs[1].y_array = NULL; /* set register one to empty */
1675}
1676
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001678 static void
1679yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1680{
1681 static int recursive = FALSE;
1682 dict_T *v_event;
1683 list_T *list;
1684 int n;
1685 char_u buf[NUMBUFLEN + 2];
1686 long reglen = 0;
1687
1688 if (recursive)
1689 return;
1690
1691 v_event = get_vim_var_dict(VV_EVENT);
1692
1693 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001694 if (list == NULL)
1695 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001696 for (n = 0; n < reg->y_size; n++)
1697 list_append_string(list, reg->y_array[n], -1);
1698 list->lv_lock = VAR_FIXED;
1699 dict_add_list(v_event, "regcontents", list);
1700
1701 buf[0] = (char_u)oap->regname;
1702 buf[1] = NUL;
1703 dict_add_nr_str(v_event, "regname", 0, buf);
1704
1705 buf[0] = get_op_char(oap->op_type);
1706 buf[1] = get_extra_op_char(oap->op_type);
1707 buf[2] = NUL;
1708 dict_add_nr_str(v_event, "operator", 0, buf);
1709
1710 buf[0] = NUL;
1711 buf[1] = NUL;
1712 switch (get_reg_type(oap->regname, &reglen))
1713 {
1714 case MLINE: buf[0] = 'V'; break;
1715 case MCHAR: buf[0] = 'v'; break;
1716 case MBLOCK:
1717 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1718 reglen + 1);
1719 break;
1720 }
1721 dict_add_nr_str(v_event, "regtype", 0, buf);
1722
1723 /* Lock the dictionary and its keys */
1724 dict_set_items_ro(v_event);
1725
1726 recursive = TRUE;
1727 textlock++;
1728 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1729 textlock--;
1730 recursive = FALSE;
1731
1732 /* Empty the dictionary, v:event is still valid */
1733 dict_free_contents(v_event);
1734 hash_init(&v_event->dv_hashtab);
1735}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001736#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001737
Bram Moolenaara1891842017-02-04 21:34:31 +01001738/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001739 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001741 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 */
1743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001744op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int n;
1747 linenr_T lnum;
1748 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 char_u *newp, *oldp;
1750 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1752 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001753 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1756 return OK;
1757
1758 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1759 if (oap->empty)
1760 return u_save_cursor();
1761
1762 if (!curbuf->b_p_ma)
1763 {
1764 EMSG(_(e_modifiable));
1765 return FAIL;
1766 }
1767
1768#ifdef FEAT_CLIPBOARD
1769 adjust_clip_reg(&oap->regname);
1770#endif
1771
1772#ifdef FEAT_MBYTE
1773 if (has_mbyte)
1774 mb_adjust_opend(oap);
1775#endif
1776
Bram Moolenaard04b7502010-07-08 22:27:55 +02001777 /*
1778 * Imitate the strange Vi behaviour: If the delete spans more than one
1779 * line and motion_type == MCHAR and the result is a blank line, make the
1780 * delete linewise. Don't do this for the change command or Visual mode.
1781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001784 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001786 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 && oap->op_type == OP_DELETE)
1788 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001789 ptr = ml_get(oap->end.lnum) + oap->end.col;
1790 if (*ptr != NUL)
1791 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ptr = skipwhite(ptr);
1793 if (*ptr == NUL && inindent(0))
1794 oap->motion_type = MLINE;
1795 }
1796
Bram Moolenaard04b7502010-07-08 22:27:55 +02001797 /*
1798 * Check for trying to delete (e.g. "D") in an empty line.
1799 * Note: For the change operator it is ok.
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if ( oap->motion_type == MCHAR
1802 && oap->line_count == 1
1803 && oap->op_type == OP_DELETE
1804 && *ml_get(oap->start.lnum) == NUL)
1805 {
1806 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001807 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 * 'cpoptions' (Vi compatible).
1809 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001810#ifdef FEAT_VIRTUALEDIT
1811 if (virtual_op)
1812 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1813 * marks as if it happened. */
1814 goto setmarks;
1815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1817 beep_flush();
1818 return OK;
1819 }
1820
Bram Moolenaard04b7502010-07-08 22:27:55 +02001821 /*
1822 * Do a yank of whatever we're about to delete.
1823 * If a yank register was specified, put the deleted text into that
1824 * register. For the black hole register '_' don't yank anything.
1825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (oap->regname != '_')
1827 {
1828 if (oap->regname != 0)
1829 {
1830 /* check for read-only register */
1831 if (!valid_yank_reg(oap->regname, TRUE))
1832 {
1833 beep_flush();
1834 return OK;
1835 }
1836 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1837 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1838 did_yank = TRUE;
1839 }
1840
1841 /*
1842 * Put deleted text into register 1 and shift number registers if the
1843 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001844 * Use the register name from before adjust_clip_reg() may have
1845 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001847 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 || oap->line_count > 1 || oap->use_reg_one)
1849 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001850 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (op_yank(oap, TRUE, FALSE) == OK)
1852 did_yank = TRUE;
1853 }
1854
Bram Moolenaar84298db2012-04-20 13:46:08 +02001855 /* Yank into small delete register when no named register specified
1856 * and the delete is within one line. */
1857 if ((
1858#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001859 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1860 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001861#endif
1862 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && oap->line_count == 1)
1864 {
1865 oap->regname = '-';
1866 get_yank_register(oap->regname, TRUE);
1867 if (op_yank(oap, TRUE, FALSE) == OK)
1868 did_yank = TRUE;
1869 oap->regname = 0;
1870 }
1871
1872 /*
1873 * If there's too much stuff to fit in the yank register, then get a
1874 * confirmation before doing the delete. This is crude, but simple.
1875 * And it avoids doing a delete of something we can't put back if we
1876 * want.
1877 */
1878 if (!did_yank)
1879 {
1880 int msg_silent_save = msg_silent;
1881
1882 msg_silent = 0; /* must display the prompt */
1883 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1884 msg_silent = msg_silent_save;
1885 if (n != 'y')
1886 {
1887 EMSG(_(e_abort));
1888 return FAIL;
1889 }
1890 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001891
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001892#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001893 if (did_yank && has_textyankpost())
1894 yank_do_autocmd(oap, y_current);
1895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897
Bram Moolenaard04b7502010-07-08 22:27:55 +02001898 /*
1899 * block mode delete
1900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (oap->block_mode)
1902 {
1903 if (u_save((linenr_T)(oap->start.lnum - 1),
1904 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1905 return FAIL;
1906
1907 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1908 {
1909 block_prep(oap, &bd, lnum, TRUE);
1910 if (bd.textlen == 0) /* nothing to delete */
1911 continue;
1912
1913 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1914 if (lnum == curwin->w_cursor.lnum)
1915 {
1916 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1917# ifdef FEAT_VIRTUALEDIT
1918 curwin->w_cursor.coladd = 0;
1919# endif
1920 }
1921
1922 /* n == number of chars deleted
1923 * If we delete a TAB, it may be replaced by several characters.
1924 * Thus the number of characters may increase!
1925 */
1926 n = bd.textlen - bd.startspaces - bd.endspaces;
1927 oldp = ml_get(lnum);
1928 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1929 if (newp == NULL)
1930 continue;
1931 /* copy up to deleted part */
1932 mch_memmove(newp, oldp, (size_t)bd.textcol);
1933 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001934 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 (size_t)(bd.startspaces + bd.endspaces));
1936 /* copy the part after the deleted part */
1937 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001938 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 /* replace the line */
1940 ml_replace(lnum, newp, FALSE);
1941 }
1942
1943 check_cursor_col();
1944 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1945 oap->end.lnum + 1, 0L);
1946 oap->line_count = 0; /* no lines deleted */
1947 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001948 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 if (oap->op_type == OP_CHANGE)
1951 {
1952 /* Delete the lines except the first one. Temporarily move the
1953 * cursor to the next line. Save the current line number, if the
1954 * last line is deleted it may be changed.
1955 */
1956 if (oap->line_count > 1)
1957 {
1958 lnum = curwin->w_cursor.lnum;
1959 ++curwin->w_cursor.lnum;
1960 del_lines((long)(oap->line_count - 1), TRUE);
1961 curwin->w_cursor.lnum = lnum;
1962 }
1963 if (u_save_cursor() == FAIL)
1964 return FAIL;
1965 if (curbuf->b_p_ai) /* don't delete indent */
1966 {
1967 beginline(BL_WHITE); /* cursor on first non-white */
1968 did_ai = TRUE; /* delete the indent when ESC hit */
1969 ai_col = curwin->w_cursor.col;
1970 }
1971 else
1972 beginline(0); /* cursor in column 0 */
1973 truncate_line(FALSE); /* delete the rest of the line */
1974 /* leave cursor past last char in line */
1975 if (oap->line_count > 1)
1976 u_clearline(); /* "U" command not possible after "2cc" */
1977 }
1978 else
1979 {
1980 del_lines(oap->line_count, TRUE);
1981 beginline(BL_WHITE | BL_FIX);
1982 u_clearline(); /* "U" command not possible after "dd" */
1983 }
1984 }
1985 else
1986 {
1987#ifdef FEAT_VIRTUALEDIT
1988 if (virtual_op)
1989 {
1990 int endcol = 0;
1991
1992 /* For virtualedit: break the tabs that are partly included. */
1993 if (gchar_pos(&oap->start) == '\t')
1994 {
1995 if (u_save_cursor() == FAIL) /* save first line for undo */
1996 return FAIL;
1997 if (oap->line_count == 1)
1998 endcol = getviscol2(oap->end.col, oap->end.coladd);
1999 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2000 oap->start = curwin->w_cursor;
2001 if (oap->line_count == 1)
2002 {
2003 coladvance(endcol);
2004 oap->end.col = curwin->w_cursor.col;
2005 oap->end.coladd = curwin->w_cursor.coladd;
2006 curwin->w_cursor = oap->start;
2007 }
2008 }
2009
2010 /* Break a tab only when it's included in the area. */
2011 if (gchar_pos(&oap->end) == '\t'
2012 && (int)oap->end.coladd < oap->inclusive)
2013 {
2014 /* save last line for undo */
2015 if (u_save((linenr_T)(oap->end.lnum - 1),
2016 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2017 return FAIL;
2018 curwin->w_cursor = oap->end;
2019 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2020 oap->end = curwin->w_cursor;
2021 curwin->w_cursor = oap->start;
2022 }
2023 }
2024#endif
2025
2026 if (oap->line_count == 1) /* delete characters within one line */
2027 {
2028 if (u_save_cursor() == FAIL) /* save line for undo */
2029 return FAIL;
2030
2031 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002032 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 && oap->op_type == OP_CHANGE
2034 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002035 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 display_dollar(oap->end.col - !oap->inclusive);
2037
2038 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2039
2040#ifdef FEAT_VIRTUALEDIT
2041 if (virtual_op)
2042 {
2043 /* fix up things for virtualedit-delete:
2044 * break the tabs which are going to get in our way
2045 */
2046 char_u *curline = ml_get_curline();
2047 int len = (int)STRLEN(curline);
2048
2049 if (oap->end.coladd != 0
2050 && (int)oap->end.col >= len - 1
2051 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2052 n++;
2053 /* Delete at least one char (e.g, when on a control char). */
2054 if (n == 0 && oap->start.coladd != oap->end.coladd)
2055 n = 1;
2056
2057 /* When deleted a char in the line, reset coladd. */
2058 if (gchar_cursor() != NUL)
2059 curwin->w_cursor.coladd = 0;
2060 }
2061#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002062 (void)del_bytes((long)n, !virtual_op,
2063 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 else /* delete characters between lines */
2066 {
2067 pos_T curpos;
2068
2069 /* save deleted and changed lines for undo */
2070 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2071 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2072 return FAIL;
2073
2074 truncate_line(TRUE); /* delete from cursor to end of line */
2075
2076 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2077 ++curwin->w_cursor.lnum;
2078 del_lines((long)(oap->line_count - 2), FALSE);
2079
Bram Moolenaard009e862015-06-09 20:20:03 +02002080 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002081 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002082 curwin->w_cursor.col = 0;
2083 (void)del_bytes((long)n, !virtual_op,
2084 oap->op_type == OP_DELETE && !oap->is_VIsual);
2085 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2086 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 }
2089
2090 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2091
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092#ifdef FEAT_VIRTUALEDIT
2093setmarks:
2094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (oap->block_mode)
2096 {
2097 curbuf->b_op_end.lnum = oap->end.lnum;
2098 curbuf->b_op_end.col = oap->start.col;
2099 }
2100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 curbuf->b_op_end = oap->start;
2102 curbuf->b_op_start = oap->start;
2103
2104 return OK;
2105}
2106
2107#ifdef FEAT_MBYTE
2108/*
2109 * Adjust end of operating area for ending on a multi-byte character.
2110 * Used for deletion.
2111 */
2112 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002113mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 char_u *p;
2116
2117 if (oap->inclusive)
2118 {
2119 p = ml_get(oap->end.lnum);
2120 oap->end.col += mb_tail_off(p, p + oap->end.col);
2121 }
2122}
2123#endif
2124
2125#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2126/*
2127 * Replace a whole area with one character.
2128 */
2129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 int n, numc;
2133#ifdef FEAT_MBYTE
2134 int num_chars;
2135#endif
2136 char_u *newp, *oldp;
2137 size_t oldlen;
2138 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002139 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002140 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2143 return OK; /* nothing to do */
2144
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002145 if (c == REPLACE_CR_NCHAR)
2146 {
2147 had_ctrl_v_cr = TRUE;
2148 c = CAR;
2149 }
2150 else if (c == REPLACE_NL_NCHAR)
2151 {
2152 had_ctrl_v_cr = TRUE;
2153 c = NL;
2154 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156#ifdef FEAT_MBYTE
2157 if (has_mbyte)
2158 mb_adjust_opend(oap);
2159#endif
2160
2161 if (u_save((linenr_T)(oap->start.lnum - 1),
2162 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2163 return FAIL;
2164
2165 /*
2166 * block mode replace
2167 */
2168 if (oap->block_mode)
2169 {
2170 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2171 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2172 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002173 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2175 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2176 continue; /* nothing to replace */
2177
2178 /* n == number of extra chars required
2179 * If we split a TAB, it may be replaced by several characters.
2180 * Thus the number of characters may increase!
2181 */
2182#ifdef FEAT_VIRTUALEDIT
2183 /* If the range starts in virtual space, count the initial
2184 * coladd offset as part of "startspaces" */
2185 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2186 {
2187 pos_T vpos;
2188
Bram Moolenaara1381de2009-11-03 15:44:21 +00002189 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 getvpos(&vpos, oap->start_vcol);
2191 bd.startspaces += vpos.coladd;
2192 n = bd.startspaces;
2193 }
2194 else
2195#endif
2196 /* allow for pre spaces */
2197 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2198
2199 /* allow for post spp */
2200 n += (bd.endspaces
2201#ifdef FEAT_VIRTUALEDIT
2202 && !bd.is_oneChar
2203#endif
2204 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2205 /* Figure out how many characters to replace. */
2206 numc = oap->end_vcol - oap->start_vcol + 1;
2207 if (bd.is_short && (!virtual_op || bd.is_MAX))
2208 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2209
2210#ifdef FEAT_MBYTE
2211 /* A double-wide character can be replaced only up to half the
2212 * times. */
2213 if ((*mb_char2cells)(c) > 1)
2214 {
2215 if ((numc & 1) && !bd.is_short)
2216 {
2217 ++bd.endspaces;
2218 ++n;
2219 }
2220 numc = numc / 2;
2221 }
2222
2223 /* Compute bytes needed, move character count to num_chars. */
2224 num_chars = numc;
2225 numc *= (*mb_char2len)(c);
2226#endif
2227 /* oldlen includes textlen, so don't double count */
2228 n += numc - bd.textlen;
2229
2230 oldp = ml_get_curline();
2231 oldlen = STRLEN(oldp);
2232 newp = alloc_check((unsigned)oldlen + 1 + n);
2233 if (newp == NULL)
2234 continue;
2235 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2236 /* copy up to deleted part */
2237 mch_memmove(newp, oldp, (size_t)bd.textcol);
2238 oldp += bd.textcol + bd.textlen;
2239 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002240 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002242 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2243 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002244 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002246#ifdef FEAT_MBYTE
2247 if (has_mbyte)
2248 {
2249 n = (int)STRLEN(newp);
2250 while (--num_chars >= 0)
2251 n += (*mb_char2bytes)(c, newp + n);
2252 }
2253 else
2254#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002255 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002256 if (!bd.is_short)
2257 {
2258 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002259 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002260 /* copy the part after the changed part */
2261 STRMOVE(newp + STRLEN(newp), oldp);
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002266 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002267 after_p = alloc_check(
2268 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002269 if (after_p != NULL)
2270 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 /* replace the line */
2273 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002274 if (after_p != NULL)
2275 {
2276 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2277 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2278 oap->end.lnum++;
2279 vim_free(after_p);
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 }
2283 else
2284 {
2285 /*
2286 * MCHAR and MLINE motion replace.
2287 */
2288 if (oap->motion_type == MLINE)
2289 {
2290 oap->start.col = 0;
2291 curwin->w_cursor.col = 0;
2292 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2293 if (oap->end.col)
2294 --oap->end.col;
2295 }
2296 else if (!oap->inclusive)
2297 dec(&(oap->end));
2298
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002299 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 n = gchar_cursor();
2302 if (n != NUL)
2303 {
2304#ifdef FEAT_MBYTE
2305 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2306 {
2307 /* This is slow, but it handles replacing a single-byte
2308 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002309 if (curwin->w_cursor.lnum == oap->end.lnum)
2310 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 n = State;
2312 State = REPLACE;
2313 ins_char(c);
2314 State = n;
2315 /* Backup to the replaced character. */
2316 dec_cursor();
2317 }
2318 else
2319#endif
2320 {
2321#ifdef FEAT_VIRTUALEDIT
2322 if (n == TAB)
2323 {
2324 int end_vcol = 0;
2325
2326 if (curwin->w_cursor.lnum == oap->end.lnum)
2327 {
2328 /* oap->end has to be recalculated when
2329 * the tab breaks */
2330 end_vcol = getviscol2(oap->end.col,
2331 oap->end.coladd);
2332 }
2333 coladvance_force(getviscol());
2334 if (curwin->w_cursor.lnum == oap->end.lnum)
2335 getvpos(&oap->end, end_vcol);
2336 }
2337#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002338 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340 }
2341#ifdef FEAT_VIRTUALEDIT
2342 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2343 {
2344 int virtcols = oap->end.coladd;
2345
2346 if (curwin->w_cursor.lnum == oap->start.lnum
2347 && oap->start.col == oap->end.col && oap->start.coladd)
2348 virtcols -= oap->start.coladd;
2349
2350 /* oap->end has been trimmed so it's effectively inclusive;
2351 * as a result an extra +1 must be counted so we don't
2352 * trample the NUL byte. */
2353 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2354 curwin->w_cursor.col -= (virtcols + 1);
2355 for (; virtcols >= 0; virtcols--)
2356 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002357 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (inc(&curwin->w_cursor) == -1)
2359 break;
2360 }
2361 }
2362#endif
2363
2364 /* Advance to next character, stop at the end of the file. */
2365 if (inc_cursor() == -1)
2366 break;
2367 }
2368 }
2369
2370 curwin->w_cursor = oap->start;
2371 check_cursor();
2372 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2373
2374 /* Set "'[" and "']" marks. */
2375 curbuf->b_op_start = oap->start;
2376 curbuf->b_op_end = oap->end;
2377
2378 return OK;
2379}
2380#endif
2381
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002382static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384/*
2385 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2386 */
2387 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002388op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002392 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 if (u_save((linenr_T)(oap->start.lnum - 1),
2395 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2396 return;
2397
2398 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (oap->block_mode) /* Visual block mode */
2400 {
2401 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2402 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002403 int one_change;
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 block_prep(oap, &bd, pos.lnum, FALSE);
2406 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002407 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2408 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002409
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002410#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002411 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
2413 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2414
Bram Moolenaar009b2592004-10-24 19:18:58 +00002415 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2416 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002418 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 if (did_change)
2423 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2424 }
2425 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 if (oap->motion_type == MLINE)
2428 {
2429 oap->start.col = 0;
2430 pos.col = 0;
2431 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2432 if (oap->end.col)
2433 --oap->end.col;
2434 }
2435 else if (!oap->inclusive)
2436 dec(&(oap->end));
2437
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002438 if (pos.lnum == oap->end.lnum)
2439 did_change = swapchars(oap->op_type, &pos,
2440 oap->end.col - pos.col + 1);
2441 else
2442 for (;;)
2443 {
2444 did_change |= swapchars(oap->op_type, &pos,
2445 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2446 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002447 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002448 break;
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 if (did_change)
2451 {
2452 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2453 0L);
2454#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002455 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 char_u *ptr;
2458 int count;
2459
2460 pos = oap->start;
2461 while (pos.lnum < oap->end.lnum)
2462 {
2463 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002464 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002465 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002467 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 pos.col = 0;
2469 pos.lnum++;
2470 }
2471 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2472 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002473 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002475 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477#endif
2478 }
2479 }
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (!did_change && oap->is_VIsual)
2482 /* No change: need to remove the Visual selection */
2483 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /*
2486 * Set '[ and '] marks.
2487 */
2488 curbuf->b_op_start = oap->start;
2489 curbuf->b_op_end = oap->end;
2490
2491 if (oap->line_count > p_report)
2492 {
2493 if (oap->line_count == 1)
2494 MSG(_("1 line changed"));
2495 else
2496 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2497 }
2498}
2499
2500/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002501 * Invoke swapchar() on "length" bytes at position "pos".
2502 * "pos" is advanced to just after the changed characters.
2503 * "length" is rounded up to include the whole last multi-byte character.
2504 * Also works correctly when the number of bytes changes.
2505 * Returns TRUE if some character was changed.
2506 */
2507 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002508swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002509{
2510 int todo;
2511 int did_change = 0;
2512
2513 for (todo = length; todo > 0; --todo)
2514 {
2515# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002516 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002517 {
2518 int len = (*mb_ptr2len)(ml_get_pos(pos));
2519
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002520 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002521 if (len > 0)
2522 todo -= len - 1;
2523 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002524# endif
2525 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002526 if (inc(pos) == -1) /* at end of file */
2527 break;
2528 }
2529 return did_change;
2530}
2531
2532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 * If op_type == OP_UPPER: make uppercase,
2534 * if op_type == OP_LOWER: make lowercase,
2535 * if op_type == OP_ROT13: do rot13 encoding,
2536 * else swap case of character at 'pos'
2537 * returns TRUE when something actually changed.
2538 */
2539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002540swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 int c;
2543 int nc;
2544
2545 c = gchar_pos(pos);
2546
2547 /* Only do rot13 encoding for ASCII characters. */
2548 if (c >= 0x80 && op_type == OP_ROT13)
2549 return FALSE;
2550
2551#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002552 if (op_type == OP_UPPER && c == 0xdf
2553 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002554 {
2555 pos_T sp = curwin->w_cursor;
2556
2557 /* Special handling of German sharp s: change to "SS". */
2558 curwin->w_cursor = *pos;
2559 del_char(FALSE);
2560 ins_char('S');
2561 ins_char('S');
2562 curwin->w_cursor = sp;
2563 inc(pos);
2564 }
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2567 return FALSE;
2568#endif
2569 nc = c;
2570 if (MB_ISLOWER(c))
2571 {
2572 if (op_type == OP_ROT13)
2573 nc = ROT13(c, 'a');
2574 else if (op_type != OP_LOWER)
2575 nc = MB_TOUPPER(c);
2576 }
2577 else if (MB_ISUPPER(c))
2578 {
2579 if (op_type == OP_ROT13)
2580 nc = ROT13(c, 'A');
2581 else if (op_type != OP_UPPER)
2582 nc = MB_TOLOWER(c);
2583 }
2584 if (nc != c)
2585 {
2586#ifdef FEAT_MBYTE
2587 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2588 {
2589 pos_T sp = curwin->w_cursor;
2590
2591 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002592 /* don't use del_char(), it also removes composing chars */
2593 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 ins_char(nc);
2595 curwin->w_cursor = sp;
2596 }
2597 else
2598#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002599 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return TRUE;
2601 }
2602 return FALSE;
2603}
2604
2605#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2606/*
2607 * op_insert - Insert and append operators for Visual mode.
2608 */
2609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002610op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 long ins_len, pre_textlen = 0;
2613 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002614 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 struct block_def bd;
2616 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002617 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /* edit() changes this - record it for OP_APPEND */
2620 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2621
2622 /* vis block is still marked. Get rid of it now. */
2623 curwin->w_cursor.lnum = oap->start.lnum;
2624 update_screen(INVERTED);
2625
2626 if (oap->block_mode)
2627 {
2628#ifdef FEAT_VIRTUALEDIT
2629 /* When 'virtualedit' is used, need to insert the extra spaces before
2630 * doing block_prep(). When only "block" is used, virtual edit is
2631 * already disabled, but still need it when calling
2632 * coladvance_force(). */
2633 if (curwin->w_cursor.coladd > 0)
2634 {
2635 int old_ve_flags = ve_flags;
2636
2637 ve_flags = VE_ALL;
2638 if (u_save_cursor() == FAIL)
2639 return;
2640 coladvance_force(oap->op_type == OP_APPEND
2641 ? oap->end_vcol + 1 : getviscol());
2642 if (oap->op_type == OP_APPEND)
2643 --curwin->w_cursor.col;
2644 ve_flags = old_ve_flags;
2645 }
2646#endif
2647 /* Get the info about the block before entering the text */
2648 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002649 /* Get indent information */
2650 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (oap->op_type == OP_APPEND)
2654 firstline += bd.textlen;
2655 pre_textlen = (long)STRLEN(firstline);
2656 }
2657
2658 if (oap->op_type == OP_APPEND)
2659 {
2660 if (oap->block_mode
2661#ifdef FEAT_VIRTUALEDIT
2662 && curwin->w_cursor.coladd == 0
2663#endif
2664 )
2665 {
2666 /* Move the cursor to the character right of the block. */
2667 curwin->w_set_curswant = TRUE;
2668 while (*ml_get_cursor() != NUL
2669 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2670 ++curwin->w_cursor.col;
2671 if (bd.is_short && !bd.is_MAX)
2672 {
2673 /* First line was too short, make it longer and adjust the
2674 * values in "bd". */
2675 if (u_save_cursor() == FAIL)
2676 return;
2677 for (i = 0; i < bd.endspaces; ++i)
2678 ins_char(' ');
2679 bd.textlen += bd.endspaces;
2680 }
2681 }
2682 else
2683 {
2684 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002685 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002688 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 && oap->start_vcol != oap->end_vcol)
2690 inc_cursor();
2691 }
2692 }
2693
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002694 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002695 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002697 /* When a tab was inserted, and the characters in front of the tab
2698 * have been converted to a tab as well, the column of the cursor
2699 * might have actually been reduced, so need to adjust here. */
2700 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002701 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002702 oap->start = curbuf->b_op_start_orig;
2703
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002704 /* If user has moved off this line, we don't know what to do, so do
2705 * nothing.
2706 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2707 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return;
2709
2710 if (oap->block_mode)
2711 {
2712 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002713 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002714 size_t len;
2715 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002717 /* If indent kicked in, the firstline might have changed
2718 * but only do that, if the indent actually increased. */
2719 ind_post = (colnr_T)getwhitecols_curline();
2720 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2721 {
2722 bd.textcol += ind_post - ind_pre;
2723 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002724 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002725 }
2726
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002727 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002728 * to adjust the block for that. But only do it, if the difference
2729 * does not come from indent kicking in. */
2730 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2731 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002732 {
2733 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002734 && oap->start.col
2735#ifdef FEAT_VIRTUALEDIT
2736 + oap->start.coladd
2737#endif
2738 != curbuf->b_op_start_orig.col
2739#ifdef FEAT_VIRTUALEDIT
2740 + curbuf->b_op_start_orig.coladd
2741#endif
2742 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002743 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002744 int t = getviscol2(curbuf->b_op_start_orig.col,
2745 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002746 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002747 pre_textlen -= t - oap->start_vcol;
2748 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002749 }
2750 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002751 && oap->end.col
2752#ifdef FEAT_VIRTUALEDIT
2753 + oap->end.coladd
2754#endif
2755 >= curbuf->b_op_start_orig.col
2756#ifdef FEAT_VIRTUALEDIT
2757 + curbuf->b_op_start_orig.coladd
2758#endif
2759 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002760 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002761 int t = getviscol2(curbuf->b_op_start_orig.col,
2762 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002763 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002764 /* reset pre_textlen to the value of OP_INSERT */
2765 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002766 pre_textlen -= t - oap->start_vcol;
2767 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002768 oap->op_type = OP_INSERT;
2769 }
2770 }
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 /*
2773 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002774 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 * Don't do this when "$" used, end-of-line will have changed.
2776 */
2777 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2778 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2779 {
2780 if (oap->op_type == OP_APPEND)
2781 {
2782 pre_textlen += bd2.textlen - bd.textlen;
2783 if (bd2.endspaces)
2784 --bd2.textlen;
2785 }
2786 bd.textcol = bd2.textcol;
2787 bd.textlen = bd2.textlen;
2788 }
2789
2790 /*
2791 * Subsequent calls to ml_get() flush the firstline data - take a
2792 * copy of the required string.
2793 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002794 firstline = ml_get(oap->start.lnum);
2795 len = STRLEN(firstline);
2796 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002798 add += bd.textlen;
2799 if ((size_t)add > len)
2800 firstline += len; // short line, point to the NUL
2801 else
2802 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002803 if (pre_textlen >= 0
2804 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
2806 ins_text = vim_strnsave(firstline, (int)ins_len);
2807 if (ins_text != NULL)
2808 {
2809 /* block handled here */
2810 if (u_save(oap->start.lnum,
2811 (linenr_T)(oap->end.lnum + 1)) == OK)
2812 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2813 &bd);
2814
2815 curwin->w_cursor.col = oap->start.col;
2816 check_cursor();
2817 vim_free(ins_text);
2818 }
2819 }
2820 }
2821}
2822#endif
2823
2824/*
2825 * op_change - handle a change operation
2826 *
2827 * return TRUE if edit() returns because of a CTRL-O command
2828 */
2829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 colnr_T l;
2833 int retval;
2834#ifdef FEAT_VISUALEXTRA
2835 long offset;
2836 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002837 long ins_len;
2838 long pre_textlen = 0;
2839 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 char_u *firstline;
2841 char_u *ins_text, *newp, *oldp;
2842 struct block_def bd;
2843#endif
2844
2845 l = oap->start.col;
2846 if (oap->motion_type == MLINE)
2847 {
2848 l = 0;
2849#ifdef FEAT_SMARTINDENT
2850 if (!p_paste && curbuf->b_p_si
2851# ifdef FEAT_CINDENT
2852 && !curbuf->b_p_cin
2853# endif
2854 )
2855 can_si = TRUE; /* It's like opening a new line, do si */
2856#endif
2857 }
2858
2859 /* First delete the text in the region. In an empty buffer only need to
2860 * save for undo */
2861 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2862 {
2863 if (u_save_cursor() == FAIL)
2864 return FALSE;
2865 }
2866 else if (op_delete(oap) == FAIL)
2867 return FALSE;
2868
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002869 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 && !virtual_op)
2871 inc_cursor();
2872
2873#ifdef FEAT_VISUALEXTRA
2874 /* check for still on same line (<CR> in inserted text meaningless) */
2875 /* skip blank lines too */
2876 if (oap->block_mode)
2877 {
2878# ifdef FEAT_VIRTUALEDIT
2879 /* Add spaces before getting the current line length. */
2880 if (virtual_op && (curwin->w_cursor.coladd > 0
2881 || gchar_cursor() == NUL))
2882 coladvance_force(getviscol());
2883# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002884 firstline = ml_get(oap->start.lnum);
2885 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002886 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 bd.textcol = curwin->w_cursor.col;
2888 }
2889#endif
2890
2891#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2892 if (oap->motion_type == MLINE)
2893 fix_indent();
2894#endif
2895
2896 retval = edit(NUL, FALSE, (linenr_T)1);
2897
2898#ifdef FEAT_VISUALEXTRA
2899 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002900 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002902 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002904 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002906 /* Auto-indenting may have changed the indent. If the cursor was past
2907 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002909 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002911 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002912
2913 pre_textlen += new_indent - pre_indent;
2914 bd.textcol += new_indent - pre_indent;
2915 }
2916
2917 ins_len = (long)STRLEN(firstline) - pre_textlen;
2918 if (ins_len > 0)
2919 {
2920 /* Subsequent calls to ml_get() flush the firstline data - take a
2921 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2923 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002924 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2926 linenr++)
2927 {
2928 block_prep(oap, &bd, linenr, TRUE);
2929 if (!bd.is_short || virtual_op)
2930 {
2931# ifdef FEAT_VIRTUALEDIT
2932 pos_T vpos;
2933
2934 /* If the block starts in virtual space, count the
2935 * initial coladd offset as part of "startspaces" */
2936 if (bd.is_short)
2937 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002938 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 else
2942 vpos.coladd = 0;
2943# endif
2944 oldp = ml_get(linenr);
2945 newp = alloc_check((unsigned)(STRLEN(oldp)
2946# ifdef FEAT_VIRTUALEDIT
2947 + vpos.coladd
2948# endif
2949 + ins_len + 1));
2950 if (newp == NULL)
2951 continue;
2952 /* copy up to block start */
2953 mch_memmove(newp, oldp, (size_t)bd.textcol);
2954 offset = bd.textcol;
2955# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002956 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 offset += vpos.coladd;
2958# endif
2959 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2960 offset += ins_len;
2961 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002962 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 ml_replace(linenr, newp, FALSE);
2964 }
2965 }
2966 check_cursor();
2967
2968 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2969 }
2970 vim_free(ins_text);
2971 }
2972 }
2973#endif
2974
2975 return retval;
2976}
2977
2978/*
2979 * set all the yank registers to empty (called from main())
2980 */
2981 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002982init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 int i;
2985
2986 for (i = 0; i < NUM_REGISTERS; ++i)
2987 y_regs[i].y_array = NULL;
2988}
2989
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002990#if defined(EXITFREE) || defined(PROTO)
2991 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002992clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002993{
2994 int i;
2995
2996 for (i = 0; i < NUM_REGISTERS; ++i)
2997 {
2998 y_current = &y_regs[i];
2999 if (y_current->y_array != NULL)
3000 free_yank_all();
3001 }
3002}
3003#endif
3004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005/*
3006 * Free "n" lines from the current yank register.
3007 * Called for normal freeing and in case of error.
3008 */
3009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 if (y_current->y_array != NULL)
3013 {
3014 long i;
3015
3016 for (i = n; --i >= 0; )
3017 {
3018#ifdef AMIGA /* only for very slow machines */
3019 if ((i & 1023) == 1023) /* this may take a while */
3020 {
3021 /*
3022 * This message should never cause a hit-return message.
3023 * Overwrite this message with any next message.
3024 */
3025 ++no_wait_return;
3026 smsg((char_u *)_("freeing %ld lines"), i + 1);
3027 --no_wait_return;
3028 msg_didout = FALSE;
3029 msg_col = 0;
3030 }
3031#endif
3032 vim_free(y_current->y_array[i]);
3033 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003034 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#ifdef AMIGA
3036 if (n >= 1000)
3037 MSG("");
3038#endif
3039 }
3040}
3041
3042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 free_yank(y_current->y_size);
3046}
3047
3048/*
3049 * Yank the text between "oap->start" and "oap->end" into a yank register.
3050 * If we are to append (uppercase register), we first yank into a new yank
3051 * register and then concatenate the old and the new one (so we keep the old
3052 * one in case of out-of-memory).
3053 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003054 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
3056 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003057op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003060 yankreg_T *curr; /* copy of y_current */
3061 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 char_u **new_ptr;
3063 linenr_T lnum; /* current line number */
3064 long j;
3065 int yanktype = oap->motion_type;
3066 long yanklines = oap->line_count;
3067 linenr_T yankendlnum = oap->end.lnum;
3068 char_u *p;
3069 char_u *pnew;
3070 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003071#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003072 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
3075 /* check for read-only register */
3076 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3077 {
3078 beep_flush();
3079 return FAIL;
3080 }
3081 if (oap->regname == '_') /* black hole: nothing to do */
3082 return OK;
3083
3084#ifdef FEAT_CLIPBOARD
3085 if (!clip_star.available && oap->regname == '*')
3086 oap->regname = 0;
3087 else if (!clip_plus.available && oap->regname == '+')
3088 oap->regname = 0;
3089#endif
3090
3091 if (!deleting) /* op_delete() already set y_current */
3092 get_yank_register(oap->regname, TRUE);
3093
3094 curr = y_current;
3095 /* append to existing contents */
3096 if (y_append && y_current->y_array != NULL)
3097 y_current = &newreg;
3098 else
3099 free_yank_all(); /* free previously yanked lines */
3100
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003101 /*
3102 * If the cursor was in column 1 before and after the movement, and the
3103 * operator is not inclusive, the yank is always linewise.
3104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 if ( oap->motion_type == MCHAR
3106 && oap->start.col == 0
3107 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003109 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 && oap->end.col == 0
3111 && yanklines > 1)
3112 {
3113 yanktype = MLINE;
3114 --yankendlnum;
3115 --yanklines;
3116 }
3117
3118 y_current->y_size = yanklines;
3119 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3122 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (y_current->y_array == NULL)
3124 {
3125 y_current = curr;
3126 return FAIL;
3127 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003128#ifdef FEAT_VIMINFO
3129 y_current->y_time_set = vim_time();
3130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 y_idx = 0;
3133 lnum = oap->start.lnum;
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (oap->block_mode)
3136 {
3137 /* Visual block mode */
3138 y_current->y_type = MBLOCK; /* set the yank register type */
3139 y_current->y_width = oap->end_vcol - oap->start_vcol;
3140
3141 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3142 y_current->y_width--;
3143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3146 {
3147 switch (y_current->y_type)
3148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 case MBLOCK:
3150 block_prep(oap, &bd, lnum, FALSE);
3151 if (yank_copy_line(&bd, y_idx) == FAIL)
3152 goto fail;
3153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 case MLINE:
3156 if ((y_current->y_array[y_idx] =
3157 vim_strsave(ml_get(lnum))) == NULL)
3158 goto fail;
3159 break;
3160
3161 case MCHAR:
3162 {
3163 colnr_T startcol = 0, endcol = MAXCOL;
3164#ifdef FEAT_VIRTUALEDIT
3165 int is_oneChar = FALSE;
3166 colnr_T cs, ce;
3167#endif
3168 p = ml_get(lnum);
3169 bd.startspaces = 0;
3170 bd.endspaces = 0;
3171
3172 if (lnum == oap->start.lnum)
3173 {
3174 startcol = oap->start.col;
3175#ifdef FEAT_VIRTUALEDIT
3176 if (virtual_op)
3177 {
3178 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3179 if (ce != cs && oap->start.coladd > 0)
3180 {
3181 /* Part of a tab selected -- but don't
3182 * double-count it. */
3183 bd.startspaces = (ce - cs + 1)
3184 - oap->start.coladd;
3185 startcol++;
3186 }
3187 }
3188#endif
3189 }
3190
3191 if (lnum == oap->end.lnum)
3192 {
3193 endcol = oap->end.col;
3194#ifdef FEAT_VIRTUALEDIT
3195 if (virtual_op)
3196 {
3197 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3198 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3199# ifdef FEAT_MBYTE
3200 /* Don't add space for double-wide
3201 * char; endcol will be on last byte
3202 * of multi-byte char. */
3203 && (*mb_head_off)(p, p + endcol) == 0
3204# endif
3205 ))
3206 {
3207 if (oap->start.lnum == oap->end.lnum
3208 && oap->start.col == oap->end.col)
3209 {
3210 /* Special case: inside a single char */
3211 is_oneChar = TRUE;
3212 bd.startspaces = oap->end.coladd
3213 - oap->start.coladd + oap->inclusive;
3214 endcol = startcol;
3215 }
3216 else
3217 {
3218 bd.endspaces = oap->end.coladd
3219 + oap->inclusive;
3220 endcol -= oap->inclusive;
3221 }
3222 }
3223 }
3224#endif
3225 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003226 if (endcol == MAXCOL)
3227 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (startcol > endcol
3229#ifdef FEAT_VIRTUALEDIT
3230 || is_oneChar
3231#endif
3232 )
3233 bd.textlen = 0;
3234 else
3235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 bd.textlen = endcol - startcol + oap->inclusive;
3237 }
3238 bd.textstart = p + startcol;
3239 if (yank_copy_line(&bd, y_idx) == FAIL)
3240 goto fail;
3241 break;
3242 }
3243 /* NOTREACHED */
3244 }
3245 }
3246
3247 if (curr != y_current) /* append the new block to the old block */
3248 {
3249 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3250 (curr->y_size + y_current->y_size)), TRUE);
3251 if (new_ptr == NULL)
3252 goto fail;
3253 for (j = 0; j < curr->y_size; ++j)
3254 new_ptr[j] = curr->y_array[j];
3255 vim_free(curr->y_array);
3256 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003257#ifdef FEAT_VIMINFO
3258 curr->y_time_set = vim_time();
3259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
3261 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3262 curr->y_type = MLINE;
3263
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003264 /* Concatenate the last line of the old block with the first line of
3265 * the new block, unless being Vi compatible. */
3266 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3269 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3270 if (pnew == NULL)
3271 {
3272 y_idx = y_current->y_size - 1;
3273 goto fail;
3274 }
3275 STRCPY(pnew, curr->y_array[--j]);
3276 STRCAT(pnew, y_current->y_array[0]);
3277 vim_free(curr->y_array[j]);
3278 vim_free(y_current->y_array[0]);
3279 curr->y_array[j++] = pnew;
3280 y_idx = 1;
3281 }
3282 else
3283 y_idx = 0;
3284 while (y_idx < y_current->y_size)
3285 curr->y_array[j++] = y_current->y_array[y_idx++];
3286 curr->y_size = j;
3287 vim_free(y_current->y_array);
3288 y_current = curr;
3289 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003290 if (curwin->w_p_rnu)
3291 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (mess) /* Display message about yank? */
3293 {
3294 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 && yanklines == 1)
3297 yanklines = 0;
3298 /* Some versions of Vi use ">=" here, some don't... */
3299 if (yanklines > p_report)
3300 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003301 char namebuf[100];
3302
3303 if (oap->regname == NUL)
3304 *namebuf = NUL;
3305 else
3306 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003307 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 /* redisplay now, so message is not deleted */
3310 update_topline_redraw();
3311 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003312 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003313 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003314 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003315 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003316 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003317 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003318 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003319 smsg((char_u *)_("block of %ld lines yanked%s"),
3320 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003322 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3323 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
3325 }
3326
3327 /*
3328 * Set "'[" and "']" marks.
3329 */
3330 curbuf->b_op_start = oap->start;
3331 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003332 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003333 {
3334 curbuf->b_op_start.col = 0;
3335 curbuf->b_op_end.col = MAXCOL;
3336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338#ifdef FEAT_CLIPBOARD
3339 /*
3340 * If we were yanking to the '*' register, send result to clipboard.
3341 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3342 * to the '*' register.
3343 */
3344 if (clip_star.available
3345 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003346 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003347 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 if (curr != &(y_regs[STAR_REGISTER]))
3350 /* Copy the text from register 0 to the clipboard register. */
3351 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3352
3353 clip_own_selection(&clip_star);
3354 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003355# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003356 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359
3360# ifdef FEAT_X11
3361 /*
3362 * If we were yanking to the '+' register, send result to selection.
3363 * Also copy to the '*' register, in case auto-select is off.
3364 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003365 if (clip_plus.available
3366 && (curr == &(y_regs[PLUS_REGISTER])
3367 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003368 && ((clip_unnamed | clip_unnamed_saved) &
3369 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003371 if (curr != &(y_regs[PLUS_REGISTER]))
3372 /* Copy the text from register 0 to the clipboard register. */
3373 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 clip_own_selection(&clip_plus);
3376 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003377 if (!clip_isautosel_star() && !clip_isautosel_plus()
3378 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
3380 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3381 clip_own_selection(&clip_star);
3382 clip_gen_set_selection(&clip_star);
3383 }
3384 }
3385# endif
3386#endif
3387
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003388#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003389 if (!deleting && has_textyankpost())
3390 yank_do_autocmd(oap, y_current);
3391#endif
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 return OK;
3394
3395fail: /* free the allocated lines */
3396 free_yank(y_idx + 1);
3397 y_current = curr;
3398 return FAIL;
3399}
3400
3401 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003402yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
3404 char_u *pnew;
3405
3406 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3407 == NULL)
3408 return FAIL;
3409 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003410 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 pnew += bd->startspaces;
3412 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3413 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003414 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 pnew += bd->endspaces;
3416 *pnew = NUL;
3417 return OK;
3418}
3419
3420#ifdef FEAT_CLIPBOARD
3421/*
3422 * Make a copy of the y_current register to register "reg".
3423 */
3424 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003425copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003427 yankreg_T *curr = y_current;
3428 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 y_current = reg;
3431 free_yank_all();
3432 *y_current = *curr;
3433 y_current->y_array = (char_u **)lalloc_clear(
3434 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3435 if (y_current->y_array == NULL)
3436 y_current->y_size = 0;
3437 else
3438 for (j = 0; j < y_current->y_size; ++j)
3439 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3440 {
3441 free_yank(j);
3442 y_current->y_size = 0;
3443 break;
3444 }
3445 y_current = curr;
3446}
3447#endif
3448
3449/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003450 * Put contents of register "regname" into the text.
3451 * Caller must check "regname" to be valid!
3452 * "flags": PUT_FIXINDENT make indent look nice
3453 * PUT_CURSEND leave cursor after end of new text
3454 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
3456 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003457do_put(
3458 int regname,
3459 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3460 long count,
3461 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
3463 char_u *ptr;
3464 char_u *newp, *oldp;
3465 int yanklen;
3466 int totlen = 0; /* init for gcc */
3467 linenr_T lnum;
3468 colnr_T col;
3469 long i; /* index in y_array[] */
3470 int y_type;
3471 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 int oldlen;
3473 long y_width = 0;
3474 colnr_T vcol;
3475 int delcount;
3476 int incr = 0;
3477 long j;
3478 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u **y_array = NULL;
3480 long nr_lines = 0;
3481 pos_T new_cursor;
3482 int indent;
3483 int orig_indent = 0; /* init for gcc */
3484 int indent_diff = 0; /* init for gcc */
3485 int first_indent = TRUE;
3486 int lendiff = 0;
3487 pos_T old_pos;
3488 char_u *insert_string = NULL;
3489 int allocated = FALSE;
3490 long cnt;
3491
3492#ifdef FEAT_CLIPBOARD
3493 /* Adjust register name for "unnamed" in 'clipboard'. */
3494 adjust_clip_reg(&regname);
3495 (void)may_get_selection(regname);
3496#endif
3497
3498 if (flags & PUT_FIXINDENT)
3499 orig_indent = get_indent();
3500
3501 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3502 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3503
3504 /*
3505 * Using inserted text works differently, because the register includes
3506 * special characters (newlines, etc.).
3507 */
3508 if (regname == '.')
3509 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003510 if (VIsual_active)
3511 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3513 (count == -1 ? 'O' : 'i')), count, FALSE);
3514 /* Putting the text is done later, so can't really move the cursor to
3515 * the next character. Use "l" to simulate it. */
3516 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3517 stuffcharReadbuff('l');
3518 return;
3519 }
3520
3521 /*
3522 * For special registers '%' (file name), '#' (alternate file name) and
3523 * ':' (last command line), etc. we have to create a fake yank register.
3524 */
3525 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3526 {
3527 if (insert_string == NULL)
3528 return;
3529 }
3530
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003531 /* Autocommands may be executed when saving lines for undo, which may make
3532 * y_array invalid. Start undo now to avoid that. */
3533 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (insert_string != NULL)
3536 {
3537 y_type = MCHAR;
3538#ifdef FEAT_EVAL
3539 if (regname == '=')
3540 {
3541 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003542 * characters.
3543 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 for (;;)
3545 {
3546 y_size = 0;
3547 ptr = insert_string;
3548 while (ptr != NULL)
3549 {
3550 if (y_array != NULL)
3551 y_array[y_size] = ptr;
3552 ++y_size;
3553 ptr = vim_strchr(ptr, '\n');
3554 if (ptr != NULL)
3555 {
3556 if (y_array != NULL)
3557 *ptr = NUL;
3558 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003559 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (*ptr == NUL)
3561 {
3562 y_type = MLINE;
3563 break;
3564 }
3565 }
3566 }
3567 if (y_array != NULL)
3568 break;
3569 y_array = (char_u **)alloc((unsigned)
3570 (y_size * sizeof(char_u *)));
3571 if (y_array == NULL)
3572 goto end;
3573 }
3574 }
3575 else
3576#endif
3577 {
3578 y_size = 1; /* use fake one-line yank register */
3579 y_array = &insert_string;
3580 }
3581 }
3582 else
3583 {
3584 get_yank_register(regname, FALSE);
3585
3586 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 y_size = y_current->y_size;
3589 y_array = y_current->y_array;
3590 }
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (y_type == MLINE)
3593 {
3594 if (flags & PUT_LINE_SPLIT)
3595 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003596 char_u *p;
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /* "p" or "P" in Visual mode: split the lines to put the text in
3599 * between. */
3600 if (u_save_cursor() == FAIL)
3601 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003602 p = ml_get_cursor();
3603 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003604 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003605 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (ptr == NULL)
3607 goto end;
3608 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3609 vim_free(ptr);
3610
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003611 oldp = ml_get_curline();
3612 p = oldp + curwin->w_cursor.col;
3613 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003614 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003615 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (ptr == NULL)
3617 goto end;
3618 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3619 ++nr_lines;
3620 dir = FORWARD;
3621 }
3622 if (flags & PUT_LINE_FORWARD)
3623 {
3624 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003625 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 dir = FORWARD;
3627 }
3628 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3629 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3633 y_type = MLINE;
3634
3635 if (y_size == 0 || y_array == NULL)
3636 {
3637 EMSG2(_("E353: Nothing in register %s"),
3638 regname == 0 ? (char_u *)"\"" : transchar(regname));
3639 goto end;
3640 }
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (y_type == MBLOCK)
3643 {
3644 lnum = curwin->w_cursor.lnum + y_size + 1;
3645 if (lnum > curbuf->b_ml.ml_line_count)
3646 lnum = curbuf->b_ml.ml_line_count + 1;
3647 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3648 goto end;
3649 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003650 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 lnum = curwin->w_cursor.lnum;
3653#ifdef FEAT_FOLDING
3654 /* Correct line number for closed fold. Don't move the cursor yet,
3655 * u_save() uses it. */
3656 if (dir == BACKWARD)
3657 (void)hasFolding(lnum, &lnum, NULL);
3658 else
3659 (void)hasFolding(lnum, NULL, &lnum);
3660#endif
3661 if (dir == FORWARD)
3662 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003663 /* In an empty buffer the empty line is going to be replaced, include
3664 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003665 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 goto end;
3667#ifdef FEAT_FOLDING
3668 if (dir == FORWARD)
3669 curwin->w_cursor.lnum = lnum - 1;
3670 else
3671 curwin->w_cursor.lnum = lnum;
3672 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3673#endif
3674 }
3675 else if (u_save_cursor() == FAIL)
3676 goto end;
3677
3678 yanklen = (int)STRLEN(y_array[0]);
3679
3680#ifdef FEAT_VIRTUALEDIT
3681 if (ve_flags == VE_ALL && y_type == MCHAR)
3682 {
3683 if (gchar_cursor() == TAB)
3684 {
3685 /* Don't need to insert spaces when "p" on the last position of a
3686 * tab or "P" on the first position. */
3687 if (dir == FORWARD
3688 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3689 : curwin->w_cursor.coladd > 0)
3690 coladvance_force(getviscol());
3691 else
3692 curwin->w_cursor.coladd = 0;
3693 }
3694 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3695 coladvance_force(getviscol() + (dir == FORWARD));
3696 }
3697#endif
3698
3699 lnum = curwin->w_cursor.lnum;
3700 col = curwin->w_cursor.col;
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 /*
3703 * Block mode
3704 */
3705 if (y_type == MBLOCK)
3706 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003707 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 colnr_T endcol2 = 0;
3709
3710 if (dir == FORWARD && c != NUL)
3711 {
3712#ifdef FEAT_VIRTUALEDIT
3713 if (ve_flags == VE_ALL)
3714 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3715 else
3716#endif
3717 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3718
3719#ifdef FEAT_MBYTE
3720 if (has_mbyte)
3721 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003722 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else
3724#endif
3725#ifdef FEAT_VIRTUALEDIT
3726 if (c != TAB || ve_flags != VE_ALL)
3727#endif
3728 ++curwin->w_cursor.col;
3729 ++col;
3730 }
3731 else
3732 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3733
3734#ifdef FEAT_VIRTUALEDIT
3735 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003736 if (ve_flags == VE_ALL
3737 && (curwin->w_cursor.coladd > 0
3738 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 if (dir == FORWARD && c == NUL)
3741 ++col;
3742 if (dir != FORWARD && c != NUL)
3743 ++curwin->w_cursor.col;
3744 if (c == TAB)
3745 {
3746 if (dir == BACKWARD && curwin->w_cursor.col)
3747 curwin->w_cursor.col--;
3748 if (dir == FORWARD && col - 1 == endcol2)
3749 curwin->w_cursor.col++;
3750 }
3751 }
3752 curwin->w_cursor.coladd = 0;
3753#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003754 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 for (i = 0; i < y_size; ++i)
3756 {
3757 int spaces;
3758 char shortline;
3759
3760 bd.startspaces = 0;
3761 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 vcol = 0;
3763 delcount = 0;
3764
3765 /* add a new line */
3766 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3767 {
3768 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3769 (colnr_T)1, FALSE) == FAIL)
3770 break;
3771 ++nr_lines;
3772 }
3773 /* get the old line and advance to the position to insert at */
3774 oldp = ml_get_curline();
3775 oldlen = (int)STRLEN(oldp);
3776 for (ptr = oldp; vcol < col && *ptr; )
3777 {
3778 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003779 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 vcol += incr;
3781 }
3782 bd.textcol = (colnr_T)(ptr - oldp);
3783
3784 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3785
3786 if (vcol < col) /* line too short, padd with spaces */
3787 bd.startspaces = col - vcol;
3788 else if (vcol > col)
3789 {
3790 bd.endspaces = vcol - col;
3791 bd.startspaces = incr - bd.endspaces;
3792 --bd.textcol;
3793 delcount = 1;
3794#ifdef FEAT_MBYTE
3795 if (has_mbyte)
3796 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3797#endif
3798 if (oldp[bd.textcol] != TAB)
3799 {
3800 /* Only a Tab can be split into spaces. Other
3801 * characters will have to be moved to after the
3802 * block, causing misalignment. */
3803 delcount = 0;
3804 bd.endspaces = 0;
3805 }
3806 }
3807
3808 yanklen = (int)STRLEN(y_array[i]);
3809
3810 /* calculate number of spaces required to fill right side of block*/
3811 spaces = y_width + 1;
3812 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003813 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (spaces < 0)
3815 spaces = 0;
3816
3817 /* insert the new text */
3818 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3819 newp = alloc_check((unsigned)totlen + oldlen + 1);
3820 if (newp == NULL)
3821 break;
3822 /* copy part up to cursor to new line */
3823 ptr = newp;
3824 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3825 ptr += bd.textcol;
3826 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003827 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 ptr += bd.startspaces;
3829 /* insert the new text */
3830 for (j = 0; j < count; ++j)
3831 {
3832 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3833 ptr += yanklen;
3834
3835 /* insert block's trailing spaces only if there's text behind */
3836 if ((j < count - 1 || !shortline) && spaces)
3837 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003838 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 ptr += spaces;
3840 }
3841 }
3842 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003843 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 ptr += bd.endspaces;
3845 /* move the text after the cursor to the end of the line. */
3846 mch_memmove(ptr, oldp + bd.textcol + delcount,
3847 (size_t)(oldlen - bd.textcol - delcount + 1));
3848 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3849
3850 ++curwin->w_cursor.lnum;
3851 if (i == 0)
3852 curwin->w_cursor.col += bd.startspaces;
3853 }
3854
3855 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3856
3857 /* Set '[ mark. */
3858 curbuf->b_op_start = curwin->w_cursor;
3859 curbuf->b_op_start.lnum = lnum;
3860
3861 /* adjust '] mark */
3862 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3863 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003864# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (flags & PUT_CURSEND)
3868 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003869 colnr_T len;
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 curwin->w_cursor = curbuf->b_op_end;
3872 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003873
3874 /* in Insert mode we might be after the NUL, correct for that */
3875 len = (colnr_T)STRLEN(ml_get_curline());
3876 if (curwin->w_cursor.col > len)
3877 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 }
3879 else
3880 curwin->w_cursor.lnum = lnum;
3881 }
3882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
3884 /*
3885 * Character or Line mode
3886 */
3887 if (y_type == MCHAR)
3888 {
3889 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3890 * char */
3891 if (dir == FORWARD && gchar_cursor() != NUL)
3892 {
3893#ifdef FEAT_MBYTE
3894 if (has_mbyte)
3895 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003896 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 /* put it on the next of the multi-byte character. */
3899 col += bytelen;
3900 if (yanklen)
3901 {
3902 curwin->w_cursor.col += bytelen;
3903 curbuf->b_op_end.col += bytelen;
3904 }
3905 }
3906 else
3907#endif
3908 {
3909 ++col;
3910 if (yanklen)
3911 {
3912 ++curwin->w_cursor.col;
3913 ++curbuf->b_op_end.col;
3914 }
3915 }
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 curbuf->b_op_start = curwin->w_cursor;
3918 }
3919 /*
3920 * Line mode: BACKWARD is the same as FORWARD on the previous line
3921 */
3922 else if (dir == BACKWARD)
3923 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003924 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 /*
3927 * simple case: insert into current line
3928 */
3929 if (y_type == MCHAR && y_size == 1)
3930 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003931 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003932
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003933 if (VIsual_active)
3934 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003935 end_lnum = curbuf->b_visual.vi_end.lnum;
3936 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3937 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003938 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003939
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003940 do {
3941 totlen = count * yanklen;
3942 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003944 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003945 if (VIsual_active && col > (int)STRLEN(oldp))
3946 {
3947 lnum++;
3948 continue;
3949 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003950 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3951 if (newp == NULL)
3952 goto end; /* alloc() gave an error message */
3953 mch_memmove(newp, oldp, (size_t)col);
3954 ptr = newp + col;
3955 for (i = 0; i < count; ++i)
3956 {
3957 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3958 ptr += yanklen;
3959 }
3960 STRMOVE(ptr, oldp + col);
3961 ml_replace(lnum, newp, FALSE);
3962 /* Place cursor on last putted char. */
3963 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003964 {
3965 /* make sure curwin->w_virtcol is updated */
3966 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003967 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003970 if (VIsual_active)
3971 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003972 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003973
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003974 if (VIsual_active) /* reset lnum to the last visual line */
3975 lnum--;
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 curbuf->b_op_end = curwin->w_cursor;
3978 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3979 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3980 ++curwin->w_cursor.col;
3981 changed_bytes(lnum, col);
3982 }
3983 else
3984 {
3985 /*
3986 * Insert at least one line. When y_type is MCHAR, break the first
3987 * line in two.
3988 */
3989 for (cnt = 1; cnt <= count; ++cnt)
3990 {
3991 i = 0;
3992 if (y_type == MCHAR)
3993 {
3994 /*
3995 * Split the current line in two at the insert position.
3996 * First insert y_array[size - 1] in front of second line.
3997 * Then append y_array[0] to first line.
3998 */
3999 lnum = new_cursor.lnum;
4000 ptr = ml_get(lnum) + col;
4001 totlen = (int)STRLEN(y_array[y_size - 1]);
4002 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4003 if (newp == NULL)
4004 goto error;
4005 STRCPY(newp, y_array[y_size - 1]);
4006 STRCAT(newp, ptr);
4007 /* insert second line */
4008 ml_append(lnum, newp, (colnr_T)0, FALSE);
4009 vim_free(newp);
4010
4011 oldp = ml_get(lnum);
4012 newp = alloc_check((unsigned)(col + yanklen + 1));
4013 if (newp == NULL)
4014 goto error;
4015 /* copy first part of line */
4016 mch_memmove(newp, oldp, (size_t)col);
4017 /* append to first line */
4018 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4019 ml_replace(lnum, newp, FALSE);
4020
4021 curwin->w_cursor.lnum = lnum;
4022 i = 1;
4023 }
4024
4025 for (; i < y_size; ++i)
4026 {
4027 if ((y_type != MCHAR || i < y_size - 1)
4028 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4029 == FAIL)
4030 goto error;
4031 lnum++;
4032 ++nr_lines;
4033 if (flags & PUT_FIXINDENT)
4034 {
4035 old_pos = curwin->w_cursor;
4036 curwin->w_cursor.lnum = lnum;
4037 ptr = ml_get(lnum);
4038 if (cnt == count && i == y_size - 1)
4039 lendiff = (int)STRLEN(ptr);
4040#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4041 if (*ptr == '#' && preprocs_left())
4042 indent = 0; /* Leave # lines at start */
4043 else
4044#endif
4045 if (*ptr == NUL)
4046 indent = 0; /* Ignore empty lines */
4047 else if (first_indent)
4048 {
4049 indent_diff = orig_indent - get_indent();
4050 indent = orig_indent;
4051 first_indent = FALSE;
4052 }
4053 else if ((indent = get_indent() + indent_diff) < 0)
4054 indent = 0;
4055 (void)set_indent(indent, 0);
4056 curwin->w_cursor = old_pos;
4057 /* remember how many chars were removed */
4058 if (cnt == count && i == y_size - 1)
4059 lendiff -= (int)STRLEN(ml_get(lnum));
4060 }
4061 }
4062 }
4063
4064error:
4065 /* Adjust marks. */
4066 if (y_type == MLINE)
4067 {
4068 curbuf->b_op_start.col = 0;
4069 if (dir == FORWARD)
4070 curbuf->b_op_start.lnum++;
4071 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004072 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004073 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004074 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004075 < curbuf->b_ml.ml_line_count
4076#ifdef FEAT_DIFF
4077 || curwin->w_p_diff
4078#endif
4079 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004080 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 (linenr_T)MAXLNUM, nr_lines, 0L);
4082
4083 /* note changed text for displaying and folding */
4084 if (y_type == MCHAR)
4085 changed_lines(curwin->w_cursor.lnum, col,
4086 curwin->w_cursor.lnum + 1, nr_lines);
4087 else
4088 changed_lines(curbuf->b_op_start.lnum, 0,
4089 curbuf->b_op_start.lnum, nr_lines);
4090
4091 /* put '] mark at last inserted character */
4092 curbuf->b_op_end.lnum = lnum;
4093 /* correct length for change in indent */
4094 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4095 if (col > 1)
4096 curbuf->b_op_end.col = col - 1;
4097 else
4098 curbuf->b_op_end.col = 0;
4099
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004100 if (flags & PUT_CURSLINE)
4101 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004102 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004103 curwin->w_cursor.lnum = lnum;
4104 beginline(BL_WHITE | BL_FIX);
4105 }
4106 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
4108 /* put cursor after inserted text */
4109 if (y_type == MLINE)
4110 {
4111 if (lnum >= curbuf->b_ml.ml_line_count)
4112 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4113 else
4114 curwin->w_cursor.lnum = lnum + 1;
4115 curwin->w_cursor.col = 0;
4116 }
4117 else
4118 {
4119 curwin->w_cursor.lnum = lnum;
4120 curwin->w_cursor.col = col;
4121 }
4122 }
4123 else if (y_type == MLINE)
4124 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004125 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 curwin->w_cursor.col = 0;
4127 if (dir == FORWARD)
4128 ++curwin->w_cursor.lnum;
4129 beginline(BL_WHITE | BL_FIX);
4130 }
4131 else /* put cursor on first inserted character */
4132 curwin->w_cursor = new_cursor;
4133 }
4134 }
4135
4136 msgmore(nr_lines);
4137 curwin->w_set_curswant = TRUE;
4138
4139end:
4140 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004142 if (regname == '=')
4143 vim_free(y_array);
4144
Bram Moolenaar033d8882013-09-25 23:24:57 +02004145 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004146
Bram Moolenaar677ee682005-01-27 14:41:15 +00004147 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004148 adjust_cursor_eol();
4149}
4150
4151/*
4152 * When the cursor is on the NUL past the end of the line and it should not be
4153 * there move it left.
4154 */
4155 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004156adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004157{
4158 if (curwin->w_cursor.col > 0
4159 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004160#ifdef FEAT_VIRTUALEDIT
4161 && (ve_flags & VE_ONEMORE) == 0
4162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 && !(restart_edit || (State & INSERT)))
4164 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004165 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004166 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#ifdef FEAT_VIRTUALEDIT
4169 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004170 {
4171 colnr_T scol, ecol;
4172
4173 /* Coladd is set to the width of the last character. */
4174 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4175 curwin->w_cursor.coladd = ecol - scol + 1;
4176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#endif
4178 }
4179}
4180
4181#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4182/*
4183 * Return TRUE if lines starting with '#' should be left aligned.
4184 */
4185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 return
4189# ifdef FEAT_SMARTINDENT
4190# ifdef FEAT_CINDENT
4191 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4192# else
4193 curbuf->b_p_si
4194# endif
4195# endif
4196# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004197 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4198 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199# endif
4200 ;
4201}
4202#endif
4203
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004204/*
4205 * Return the character name of the register with the given number.
4206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004208get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 if (num == -1)
4211 return '"';
4212 else if (num < 10)
4213 return num + '0';
4214 else if (num == DELETION_REGISTER)
4215 return '-';
4216#ifdef FEAT_CLIPBOARD
4217 else if (num == STAR_REGISTER)
4218 return '*';
4219 else if (num == PLUS_REGISTER)
4220 return '+';
4221#endif
4222 else
4223 {
4224#ifdef EBCDIC
4225 int i;
4226
4227 /* EBCDIC is really braindead ... */
4228 i = 'a' + (num - 10);
4229 if (i > 'i')
4230 i += 7;
4231 if (i > 'r')
4232 i += 8;
4233 return i;
4234#else
4235 return num + 'a' - 10;
4236#endif
4237 }
4238}
4239
4240/*
4241 * ":dis" and ":registers": Display the contents of the yank registers.
4242 */
4243 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004244ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004246 int i, n;
4247 long j;
4248 char_u *p;
4249 yankreg_T *yb;
4250 int name;
4251 int attr;
4252 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004253#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004254 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004255#else
4256# define clen 1
4257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 if (arg != NULL && *arg == NUL)
4260 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004261 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /* Highlight title */
4264 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4265 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4266 {
4267 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004268 if (arg != NULL && vim_strchr(arg, name) == NULL
4269#ifdef ONE_CLIPBOARD
4270 /* Star register and plus register contain the same thing. */
4271 && (name != '*' || vim_strchr(arg, '+') == NULL)
4272#endif
4273 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 continue; /* did not ask for this register */
4275
4276#ifdef FEAT_CLIPBOARD
4277 /* Adjust register name for "unnamed" in 'clipboard'.
4278 * When it's a clipboard register, fill it with the current contents
4279 * of the clipboard. */
4280 adjust_clip_reg(&name);
4281 (void)may_get_selection(name);
4282#endif
4283
4284 if (i == -1)
4285 {
4286 if (y_previous != NULL)
4287 yb = y_previous;
4288 else
4289 yb = &(y_regs[0]);
4290 }
4291 else
4292 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004293
4294#ifdef FEAT_EVAL
4295 if (name == MB_TOLOWER(redir_reg)
4296 || (redir_reg == '"' && yb == y_previous))
4297 continue; /* do not list register being written to, the
4298 * pointer can be freed */
4299#endif
4300
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (yb->y_array != NULL)
4302 {
4303 msg_putchar('\n');
4304 msg_putchar('"');
4305 msg_putchar(name);
4306 MSG_PUTS(" ");
4307
4308 n = (int)Columns - 6;
4309 for (j = 0; j < yb->y_size && n > 1; ++j)
4310 {
4311 if (j)
4312 {
4313 MSG_PUTS_ATTR("^J", attr);
4314 n -= 2;
4315 }
4316 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004319 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004320#endif
4321 msg_outtrans_len(p, clen);
4322#ifdef FEAT_MBYTE
4323 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324#endif
4325 }
4326 }
4327 if (n > 1 && yb->y_type == MLINE)
4328 MSG_PUTS_ATTR("^J", attr);
4329 out_flush(); /* show one line at a time */
4330 }
4331 ui_breakcheck();
4332 }
4333
4334 /*
4335 * display last inserted text
4336 */
4337 if ((p = get_last_insert()) != NULL
4338 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4339 {
4340 MSG_PUTS("\n\". ");
4341 dis_msg(p, TRUE);
4342 }
4343
4344 /*
4345 * display last command line
4346 */
4347 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4348 && !got_int)
4349 {
4350 MSG_PUTS("\n\": ");
4351 dis_msg(last_cmdline, FALSE);
4352 }
4353
4354 /*
4355 * display current file name
4356 */
4357 if (curbuf->b_fname != NULL
4358 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4359 {
4360 MSG_PUTS("\n\"% ");
4361 dis_msg(curbuf->b_fname, FALSE);
4362 }
4363
4364 /*
4365 * display alternate file name
4366 */
4367 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4368 {
4369 char_u *fname;
4370 linenr_T dummy;
4371
4372 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4373 {
4374 MSG_PUTS("\n\"# ");
4375 dis_msg(fname, FALSE);
4376 }
4377 }
4378
4379 /*
4380 * display last search pattern
4381 */
4382 if (last_search_pat() != NULL
4383 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4384 {
4385 MSG_PUTS("\n\"/ ");
4386 dis_msg(last_search_pat(), FALSE);
4387 }
4388
4389#ifdef FEAT_EVAL
4390 /*
4391 * display last used expression
4392 */
4393 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4394 && !got_int)
4395 {
4396 MSG_PUTS("\n\"= ");
4397 dis_msg(expr_line, FALSE);
4398 }
4399#endif
4400}
4401
4402/*
4403 * display a string for do_dis()
4404 * truncate at end of screen line
4405 */
4406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004407dis_msg(
4408 char_u *p,
4409 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int n;
4412#ifdef FEAT_MBYTE
4413 int l;
4414#endif
4415
4416 n = (int)Columns - 6;
4417 while (*p != NUL
4418 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4419 && (n -= ptr2cells(p)) >= 0)
4420 {
4421#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004422 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 msg_outtrans_len(p, l);
4425 p += l;
4426 }
4427 else
4428#endif
4429 msg_outtrans_len(p++, 1);
4430 }
4431 ui_breakcheck();
4432}
4433
Bram Moolenaar81340392012-06-06 16:12:59 +02004434#if defined(FEAT_COMMENTS) || defined(PROTO)
4435/*
4436 * If "process" is TRUE and the line begins with a comment leader (possibly
4437 * after some white space), return a pointer to the text after it. Put a boolean
4438 * value indicating whether the line ends with an unclosed comment in
4439 * "is_comment".
4440 * line - line to be processed,
4441 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004442 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004443 * include_space - whether to also skip space following the comment leader,
4444 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004445 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004446 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004447 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004448skip_comment(
4449 char_u *line,
4450 int process,
4451 int include_space,
4452 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004453{
4454 char_u *comment_flags = NULL;
4455 int lead_len;
4456 int leader_offset = get_last_leader_offset(line, &comment_flags);
4457
4458 *is_comment = FALSE;
4459 if (leader_offset != -1)
4460 {
4461 /* Let's check whether the line ends with an unclosed comment.
4462 * If the last comment leader has COM_END in flags, there's no comment.
4463 */
4464 while (*comment_flags)
4465 {
4466 if (*comment_flags == COM_END
4467 || *comment_flags == ':')
4468 break;
4469 ++comment_flags;
4470 }
4471 if (*comment_flags != COM_END)
4472 *is_comment = TRUE;
4473 }
4474
4475 if (process == FALSE)
4476 return line;
4477
4478 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4479
4480 if (lead_len == 0)
4481 return line;
4482
4483 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004484 * - COM_END,
4485 * - colon,
4486 * whichever comes first.
4487 */
4488 while (*comment_flags)
4489 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004490 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004491 || *comment_flags == ':')
4492 {
4493 break;
4494 }
4495 ++comment_flags;
4496 }
4497
4498 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004499 * starting with a closing part of a three-part comment. That's good,
4500 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004501 */
4502 if (*comment_flags == ':' || *comment_flags == NUL)
4503 line += lead_len;
4504
4505 return line;
4506}
4507#endif
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004510 * Join 'count' lines (minimal 2) at cursor position.
4511 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004512 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4513 * leaders should not be removed.
4514 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4515 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004517 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 */
4519 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004520do_join(
4521 long count,
4522 int insert_space,
4523 int save_undo,
4524 int use_formatoptions UNUSED,
4525 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004527 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004528 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004529 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004531 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004532 int endcurr1 = NUL;
4533 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004534 int currsize = 0; /* size of the current line */
4535 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004537 colnr_T col = 0;
4538 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004539#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004540 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004541 int remove_comments = (use_formatoptions == TRUE)
4542 && has_format_option(FO_REMOVE_COMS);
4543 int prev_was_comment;
4544#endif
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004547 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4548 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4549 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004551 /* Allocate an array to store the number of spaces inserted before each
4552 * line. We will use it to pre-compute the length of the new line and the
4553 * proper placement of each original line in the new one. */
4554 spaces = lalloc_clear((long_u)count, TRUE);
4555 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004557#if defined(FEAT_COMMENTS) || defined(PROTO)
4558 if (remove_comments)
4559 {
4560 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4561 if (comments == NULL)
4562 {
4563 vim_free(spaces);
4564 return FAIL;
4565 }
4566 }
4567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004570 * Don't move anything, just compute the final line length
4571 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004573 for (t = 0; t < count; ++t)
4574 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004575 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004576 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004577 {
4578 /* Set the '[ mark. */
4579 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4580 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4581 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004582#if defined(FEAT_COMMENTS) || defined(PROTO)
4583 if (remove_comments)
4584 {
4585 /* We don't want to remove the comment leader if the
4586 * previous line is not a comment. */
4587 if (t > 0 && prev_was_comment)
4588 {
4589
4590 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4591 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004592 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004593 curr = new_curr;
4594 }
4595 else
4596 curr = skip_comment(curr, FALSE, insert_space,
4597 &prev_was_comment);
4598 }
4599#endif
4600
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004601 if (insert_space && t > 0)
4602 {
4603 curr = skipwhite(curr);
4604 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4605#ifdef FEAT_MBYTE
4606 && (!has_format_option(FO_MBYTE_JOIN)
4607 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4608 && (!has_format_option(FO_MBYTE_JOIN2)
4609 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4610#endif
4611 )
4612 {
4613 /* don't add a space if the line is ending in a space */
4614 if (endcurr1 == ' ')
4615 endcurr1 = endcurr2;
4616 else
4617 ++spaces[t];
4618 /* extra space when 'joinspaces' set and line ends in '.' */
4619 if ( p_js
4620 && (endcurr1 == '.'
4621 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4622 && (endcurr1 == '?' || endcurr1 == '!'))))
4623 ++spaces[t];
4624 }
4625 }
4626 currsize = (int)STRLEN(curr);
4627 sumsize += currsize + spaces[t];
4628 endcurr1 = endcurr2 = NUL;
4629 if (insert_space && currsize > 0)
4630 {
4631#ifdef FEAT_MBYTE
4632 if (has_mbyte)
4633 {
4634 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004635 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004636 endcurr1 = (*mb_ptr2char)(cend);
4637 if (cend > curr)
4638 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004639 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004640 endcurr2 = (*mb_ptr2char)(cend);
4641 }
4642 }
4643 else
4644#endif
4645 {
4646 endcurr1 = *(curr + currsize - 1);
4647 if (currsize > 1)
4648 endcurr2 = *(curr + currsize - 2);
4649 }
4650 }
4651 line_breakcheck();
4652 if (got_int)
4653 {
4654 ret = FAIL;
4655 goto theend;
4656 }
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004659 /* store the column position before last line */
4660 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004662 /* allocate the space for the new line */
4663 newp = alloc_check((unsigned)(sumsize + 1));
4664 cend = newp + sumsize;
4665 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004667 /*
4668 * Move affected lines to the new long one.
4669 *
4670 * Move marks from each deleted line to the joined line, adjusting the
4671 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4672 * should not really be a problem.
4673 */
4674 for (t = count - 1; ; --t)
4675 {
4676 cend -= currsize;
4677 mch_memmove(cend, curr, (size_t)currsize);
4678 if (spaces[t] > 0)
4679 {
4680 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004681 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004682 }
4683 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004684 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004685 if (t == 0)
4686 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004687 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004688#if defined(FEAT_COMMENTS) || defined(PROTO)
4689 if (remove_comments)
4690 curr += comments[t - 1];
4691#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004692 if (insert_space && t > 1)
4693 curr = skipwhite(curr);
4694 currsize = (int)STRLEN(curr);
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4697
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004698 if (setmark)
4699 {
4700 /* Set the '] mark. */
4701 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4702 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4703 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 /* Only report the change in the first line here, del_lines() will report
4706 * the deleted line. */
4707 changed_lines(curwin->w_cursor.lnum, currsize,
4708 curwin->w_cursor.lnum + 1, 0L);
4709
4710 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004711 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 * briefly, and then move it back. After del_lines() the cursor may
4713 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 */
4715 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004717 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 curwin->w_cursor.lnum = t;
4719
4720 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004721 * Set the cursor column:
4722 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004723 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004725 curwin->w_cursor.col =
4726 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#ifdef FEAT_VIRTUALEDIT
4730 curwin->w_cursor.coladd = 0;
4731#endif
4732 curwin->w_set_curswant = TRUE;
4733
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004734theend:
4735 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004736#if defined(FEAT_COMMENTS) || defined(PROTO)
4737 if (remove_comments)
4738 vim_free(comments);
4739#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004740 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741}
4742
4743#ifdef FEAT_COMMENTS
4744/*
4745 * Return TRUE if the two comment leaders given are the same. "lnum" is
4746 * the first line. White-space is ignored. Note that the whole of
4747 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4748 */
4749 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004750same_leader(
4751 linenr_T lnum,
4752 int leader1_len,
4753 char_u *leader1_flags,
4754 int leader2_len,
4755 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 int idx1 = 0, idx2 = 0;
4758 char_u *p;
4759 char_u *line1;
4760 char_u *line2;
4761
4762 if (leader1_len == 0)
4763 return (leader2_len == 0);
4764
4765 /*
4766 * If first leader has 'f' flag, the lines can be joined only if the
4767 * second line does not have a leader.
4768 * If first leader has 'e' flag, the lines can never be joined.
4769 * If fist leader has 's' flag, the lines can only be joined if there is
4770 * some text after it and the second line has the 'm' flag.
4771 */
4772 if (leader1_flags != NULL)
4773 {
4774 for (p = leader1_flags; *p && *p != ':'; ++p)
4775 {
4776 if (*p == COM_FIRST)
4777 return (leader2_len == 0);
4778 if (*p == COM_END)
4779 return FALSE;
4780 if (*p == COM_START)
4781 {
4782 if (*(ml_get(lnum) + leader1_len) == NUL)
4783 return FALSE;
4784 if (leader2_flags == NULL || leader2_len == 0)
4785 return FALSE;
4786 for (p = leader2_flags; *p && *p != ':'; ++p)
4787 if (*p == COM_MIDDLE)
4788 return TRUE;
4789 return FALSE;
4790 }
4791 }
4792 }
4793
4794 /*
4795 * Get current line and next line, compare the leaders.
4796 * The first line has to be saved, only one line can be locked at a time.
4797 */
4798 line1 = vim_strsave(ml_get(lnum));
4799 if (line1 != NULL)
4800 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004801 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 ;
4803 line2 = ml_get(lnum + 1);
4804 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4805 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004806 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 if (line1[idx1++] != line2[idx2])
4809 break;
4810 }
4811 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004812 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 ++idx1;
4814 }
4815 vim_free(line1);
4816 }
4817 return (idx2 == leader2_len && idx1 == leader1_len);
4818}
4819#endif
4820
4821/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004822 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004825op_format(
4826 oparg_T *oap,
4827 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 long old_line_count = curbuf->b_ml.ml_line_count;
4830
4831 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4832 * can put it back there. */
4833 curwin->w_cursor = oap->cursor_start;
4834
4835 if (u_save((linenr_T)(oap->start.lnum - 1),
4836 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4837 return;
4838 curwin->w_cursor = oap->start;
4839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (oap->is_VIsual)
4841 /* When there is no change: need to remove the Visual selection */
4842 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 /* Set '[ mark at the start of the formatted area */
4845 curbuf->b_op_start = oap->start;
4846
4847 /* For "gw" remember the cursor position and put it back below (adjusted
4848 * for joined and split lines). */
4849 if (keep_cursor)
4850 saved_cursor = oap->cursor_start;
4851
Bram Moolenaar81a82092008-03-12 16:27:00 +00004852 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 /*
4855 * Leave the cursor at the first non-blank of the last formatted line.
4856 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4857 * line, so "." will do the next lines.
4858 */
4859 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4860 ++curwin->w_cursor.lnum;
4861 beginline(BL_WHITE | BL_FIX);
4862 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4863 msgmore(old_line_count);
4864
4865 /* put '] mark on the end of the formatted area */
4866 curbuf->b_op_end = curwin->w_cursor;
4867
4868 if (keep_cursor)
4869 {
4870 curwin->w_cursor = saved_cursor;
4871 saved_cursor.lnum = 0;
4872 }
4873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (oap->is_VIsual)
4875 {
4876 win_T *wp;
4877
4878 FOR_ALL_WINDOWS(wp)
4879 {
4880 if (wp->w_old_cursor_lnum != 0)
4881 {
4882 /* When lines have been inserted or deleted, adjust the end of
4883 * the Visual area to be redrawn. */
4884 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4885 wp->w_old_cursor_lnum += old_line_count;
4886 else
4887 wp->w_old_visual_lnum += old_line_count;
4888 }
4889 }
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891}
4892
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004893#if defined(FEAT_EVAL) || defined(PROTO)
4894/*
4895 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4896 */
4897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004898op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004899{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004900 if (oap->is_VIsual)
4901 /* When there is no change: need to remove the Visual selection */
4902 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004903
Bram Moolenaar700303e2010-07-11 17:35:50 +02004904 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4905 /* As documented: when 'formatexpr' returns non-zero fall back to
4906 * internal formatting. */
4907 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004908}
4909
4910 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911fex_format(
4912 linenr_T lnum,
4913 long count,
4914 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004915{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004916 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4917 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004918 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004919 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004920
4921 /*
4922 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004923 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004924 */
4925 set_vim_var_nr(VV_LNUM, lnum);
4926 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004927 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004928
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004929 /* Make a copy, the option could be changed while calling it. */
4930 fex = vim_strsave(curbuf->b_p_fex);
4931 if (fex == NULL)
4932 return 0;
4933
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004934 /*
4935 * Evaluate the function.
4936 */
4937 if (use_sandbox)
4938 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004939 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004940 if (use_sandbox)
4941 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004942
4943 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004944 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004946 return r;
4947}
4948#endif
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950/*
4951 * Format "line_count" lines, starting at the cursor position.
4952 * When "line_count" is negative, format until the end of the paragraph.
4953 * Lines after the cursor line are saved for undo, caller must have saved the
4954 * first line.
4955 */
4956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957format_lines(
4958 linenr_T line_count,
4959 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 int max_len;
4962 int is_not_par; /* current line not part of parag. */
4963 int next_is_not_par; /* next line not part of paragraph */
4964 int is_end_par; /* at end of paragraph */
4965 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4966 int next_is_start_par = FALSE;
4967#ifdef FEAT_COMMENTS
4968 int leader_len = 0; /* leader len of current line */
4969 int next_leader_len; /* leader len of next line */
4970 char_u *leader_flags = NULL; /* flags for leader of current line */
4971 char_u *next_leader_flags; /* flags for leader of next line */
4972 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004973 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
4975 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004976 int second_indent = -1; /* indent for second line (comment
4977 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 int do_second_indent;
4979 int do_number_indent;
4980 int do_trail_white;
4981 int first_par_line = TRUE;
4982 int smd_save;
4983 long count;
4984 int need_set_indent = TRUE; /* set indent of next paragraph */
4985 int force_format = FALSE;
4986 int old_State = State;
4987
4988 /* length of a line to force formatting: 3 * 'tw' */
4989 max_len = comp_textwidth(TRUE) * 3;
4990
4991 /* check for 'q', '2' and '1' in 'formatoptions' */
4992#ifdef FEAT_COMMENTS
4993 do_comments = has_format_option(FO_Q_COMS);
4994#endif
4995 do_second_indent = has_format_option(FO_Q_SECOND);
4996 do_number_indent = has_format_option(FO_Q_NUMBER);
4997 do_trail_white = has_format_option(FO_WHITE_PAR);
4998
4999 /*
5000 * Get info about the previous and current line.
5001 */
5002 if (curwin->w_cursor.lnum > 1)
5003 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5004#ifdef FEAT_COMMENTS
5005 , &leader_len, &leader_flags, do_comments
5006#endif
5007 );
5008 else
5009 is_not_par = TRUE;
5010 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5011#ifdef FEAT_COMMENTS
5012 , &next_leader_len, &next_leader_flags, do_comments
5013#endif
5014 );
5015 is_end_par = (is_not_par || next_is_not_par);
5016 if (!is_end_par && do_trail_white)
5017 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5018
5019 curwin->w_cursor.lnum--;
5020 for (count = line_count; count != 0 && !got_int; --count)
5021 {
5022 /*
5023 * Advance to next paragraph.
5024 */
5025 if (advance)
5026 {
5027 curwin->w_cursor.lnum++;
5028 prev_is_end_par = is_end_par;
5029 is_not_par = next_is_not_par;
5030#ifdef FEAT_COMMENTS
5031 leader_len = next_leader_len;
5032 leader_flags = next_leader_flags;
5033#endif
5034 }
5035
5036 /*
5037 * The last line to be formatted.
5038 */
5039 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5040 {
5041 next_is_not_par = TRUE;
5042#ifdef FEAT_COMMENTS
5043 next_leader_len = 0;
5044 next_leader_flags = NULL;
5045#endif
5046 }
5047 else
5048 {
5049 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5050#ifdef FEAT_COMMENTS
5051 , &next_leader_len, &next_leader_flags, do_comments
5052#endif
5053 );
5054 if (do_number_indent)
5055 next_is_start_par =
5056 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5057 }
5058 advance = TRUE;
5059 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5060 if (!is_end_par && do_trail_white)
5061 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5062
5063 /*
5064 * Skip lines that are not in a paragraph.
5065 */
5066 if (is_not_par)
5067 {
5068 if (line_count < 0)
5069 break;
5070 }
5071 else
5072 {
5073 /*
5074 * For the first line of a paragraph, check indent of second line.
5075 * Don't do this for comments and empty lines.
5076 */
5077 if (first_par_line
5078 && (do_second_indent || do_number_indent)
5079 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005080 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005082 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005083 {
5084#ifdef FEAT_COMMENTS
5085 if (leader_len == 0 && next_leader_len == 0)
5086 {
5087 /* no comment found */
5088#endif
5089 second_indent =
5090 get_indent_lnum(curwin->w_cursor.lnum + 1);
5091#ifdef FEAT_COMMENTS
5092 }
5093 else
5094 {
5095 second_indent = next_leader_len;
5096 do_comments_list = 1;
5097 }
5098#endif
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005101 {
5102#ifdef FEAT_COMMENTS
5103 if (leader_len == 0 && next_leader_len == 0)
5104 {
5105 /* no comment found */
5106#endif
5107 second_indent =
5108 get_number_indent(curwin->w_cursor.lnum);
5109#ifdef FEAT_COMMENTS
5110 }
5111 else
5112 {
5113 /* get_number_indent() is now "comment aware"... */
5114 second_indent =
5115 get_number_indent(curwin->w_cursor.lnum);
5116 do_comments_list = 1;
5117 }
5118#endif
5119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
5122 /*
5123 * When the comment leader changes, it's the end of the paragraph.
5124 */
5125 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5126#ifdef FEAT_COMMENTS
5127 || !same_leader(curwin->w_cursor.lnum,
5128 leader_len, leader_flags,
5129 next_leader_len, next_leader_flags)
5130#endif
5131 )
5132 is_end_par = TRUE;
5133
5134 /*
5135 * If we have got to the end of a paragraph, or the line is
5136 * getting long, format it.
5137 */
5138 if (is_end_par || force_format)
5139 {
5140 if (need_set_indent)
5141 /* replace indent in first line with minimal number of
5142 * tabs and spaces, according to current options */
5143 (void)set_indent(get_indent(), SIN_CHANGED);
5144
5145 /* put cursor on last non-space */
5146 State = NORMAL; /* don't go past end-of-line */
5147 coladvance((colnr_T)MAXCOL);
5148 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5149 dec_cursor();
5150
5151 /* do the formatting, without 'showmode' */
5152 State = INSERT; /* for open_line() */
5153 smd_save = p_smd;
5154 p_smd = FALSE;
5155 insertchar(NUL, INSCHAR_FORMAT
5156#ifdef FEAT_COMMENTS
5157 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005158 + (do_comments && do_comments_list
5159 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005161 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 State = old_State;
5163 p_smd = smd_save;
5164 second_indent = -1;
5165 /* at end of par.: need to set indent of next par. */
5166 need_set_indent = is_end_par;
5167 if (is_end_par)
5168 {
5169 /* When called with a negative line count, break at the
5170 * end of the paragraph. */
5171 if (line_count < 0)
5172 break;
5173 first_par_line = TRUE;
5174 }
5175 force_format = FALSE;
5176 }
5177
5178 /*
5179 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005180 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
5182 if (!is_end_par)
5183 {
5184 advance = FALSE;
5185 curwin->w_cursor.lnum++;
5186 curwin->w_cursor.col = 0;
5187 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005188 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005191 {
5192 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5194 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005195 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005197 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5198 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005199 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005200
5201 if (indent > 0)
5202 {
5203 (void)del_bytes(indent, FALSE, FALSE);
5204 mark_col_adjust(curwin->w_cursor.lnum,
5205 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005206 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005209 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 beep_flush();
5212 break;
5213 }
5214 first_par_line = FALSE;
5215 /* If the line is getting long, format it next time */
5216 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5217 force_format = TRUE;
5218 else
5219 force_format = FALSE;
5220 }
5221 }
5222 line_breakcheck();
5223 }
5224}
5225
5226/*
5227 * Return TRUE if line "lnum" ends in a white character.
5228 */
5229 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005230ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 char_u *s = ml_get(lnum);
5233 size_t l;
5234
5235 if (*s == NUL)
5236 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005237 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 * invocation may call function multiple times". */
5239 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005240 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241}
5242
5243/*
5244 * Blank lines, and lines containing only the comment leader, are left
5245 * untouched by the formatting. The function returns TRUE in this
5246 * case. It also returns TRUE when a line starts with the end of a comment
5247 * ('e' in comment flags), so that this line is skipped, and not joined to the
5248 * previous line. A new paragraph starts after a blank line, or when the
5249 * comment leader changes -- webb.
5250 */
5251#ifdef FEAT_COMMENTS
5252 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005253fmt_check_par(
5254 linenr_T lnum,
5255 int *leader_len,
5256 char_u **leader_flags,
5257 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 char_u *flags = NULL; /* init for GCC */
5260 char_u *ptr;
5261
5262 ptr = ml_get(lnum);
5263 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005264 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 else
5266 *leader_len = 0;
5267
5268 if (*leader_len > 0)
5269 {
5270 /*
5271 * Search for 'e' flag in comment leader flags.
5272 */
5273 flags = *leader_flags;
5274 while (*flags && *flags != ':' && *flags != COM_END)
5275 ++flags;
5276 }
5277
5278 return (*skipwhite(ptr + *leader_len) == NUL
5279 || (*leader_len > 0 && *flags == COM_END)
5280 || startPS(lnum, NUL, FALSE));
5281}
5282#else
5283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5287}
5288#endif
5289
5290/*
5291 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5292 * previous line is in the same paragraph. Used for auto-formatting.
5293 */
5294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005295paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 char_u *p;
5298#ifdef FEAT_COMMENTS
5299 int leader_len = 0; /* leader len of current line */
5300 char_u *leader_flags = NULL; /* flags for leader of current line */
5301 int next_leader_len; /* leader len of next line */
5302 char_u *next_leader_flags; /* flags for leader of next line */
5303 int do_comments; /* format comments */
5304#endif
5305
5306 if (lnum <= 1)
5307 return TRUE; /* start of the file */
5308
5309 p = ml_get(lnum - 1);
5310 if (*p == NUL)
5311 return TRUE; /* after empty line */
5312
5313#ifdef FEAT_COMMENTS
5314 do_comments = has_format_option(FO_Q_COMS);
5315#endif
5316 if (fmt_check_par(lnum - 1
5317#ifdef FEAT_COMMENTS
5318 , &leader_len, &leader_flags, do_comments
5319#endif
5320 ))
5321 return TRUE; /* after non-paragraph line */
5322
5323 if (fmt_check_par(lnum
5324#ifdef FEAT_COMMENTS
5325 , &next_leader_len, &next_leader_flags, do_comments
5326#endif
5327 ))
5328 return TRUE; /* "lnum" is not a paragraph line */
5329
5330 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5331 return TRUE; /* missing trailing space in previous line. */
5332
5333 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5334 return TRUE; /* numbered item starts in "lnum". */
5335
5336#ifdef FEAT_COMMENTS
5337 if (!same_leader(lnum - 1, leader_len, leader_flags,
5338 next_leader_len, next_leader_flags))
5339 return TRUE; /* change of comment leader. */
5340#endif
5341
5342 return FALSE;
5343}
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * prepare a few things for block mode yank/delete/tilde
5347 *
5348 * for delete:
5349 * - textlen includes the first/last char to be (partly) deleted
5350 * - start/endspaces is the number of columns that are taken by the
5351 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005352 * deleted.
5353 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 * - textlen includes the first/last char to be wholly yanked
5355 * - start/endspaces is the number of columns of the first/last yanked char
5356 * that are to be yanked.
5357 */
5358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005359block_prep(
5360 oparg_T *oap,
5361 struct block_def *bdp,
5362 linenr_T lnum,
5363 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 int incr = 0;
5366 char_u *pend;
5367 char_u *pstart;
5368 char_u *line;
5369 char_u *prev_pstart;
5370 char_u *prev_pend;
5371
5372 bdp->startspaces = 0;
5373 bdp->endspaces = 0;
5374 bdp->textlen = 0;
5375 bdp->start_vcol = 0;
5376 bdp->end_vcol = 0;
5377#ifdef FEAT_VISUALEXTRA
5378 bdp->is_short = FALSE;
5379 bdp->is_oneChar = FALSE;
5380 bdp->pre_whitesp = 0;
5381 bdp->pre_whitesp_c = 0;
5382 bdp->end_char_vcols = 0;
5383#endif
5384 bdp->start_char_vcols = 0;
5385
5386 line = ml_get(lnum);
5387 pstart = line;
5388 prev_pstart = line;
5389 while (bdp->start_vcol < oap->start_vcol && *pstart)
5390 {
5391 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005392 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 bdp->start_vcol += incr;
5394#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005395 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 bdp->pre_whitesp += incr;
5398 bdp->pre_whitesp_c++;
5399 }
5400 else
5401 {
5402 bdp->pre_whitesp = 0;
5403 bdp->pre_whitesp_c = 0;
5404 }
5405#endif
5406 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005407 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409 bdp->start_char_vcols = incr;
5410 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5411 {
5412 bdp->end_vcol = bdp->start_vcol;
5413#ifdef FEAT_VISUALEXTRA
5414 bdp->is_short = TRUE;
5415#endif
5416 if (!is_del || oap->op_type == OP_APPEND)
5417 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5418 }
5419 else
5420 {
5421 /* notice: this converts partly selected Multibyte characters to
5422 * spaces, too. */
5423 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5424 if (is_del && bdp->startspaces)
5425 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5426 pend = pstart;
5427 bdp->end_vcol = bdp->start_vcol;
5428 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5429 {
5430#ifdef FEAT_VISUALEXTRA
5431 bdp->is_oneChar = TRUE;
5432#endif
5433 if (oap->op_type == OP_INSERT)
5434 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5435 else if (oap->op_type == OP_APPEND)
5436 {
5437 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5438 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5439 }
5440 else
5441 {
5442 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5443 if (is_del && oap->op_type != OP_LSHIFT)
5444 {
5445 /* just putting the sum of those two into
5446 * bdp->startspaces doesn't work for Visual replace,
5447 * so we have to split the tab in two */
5448 bdp->startspaces = bdp->start_char_vcols
5449 - (bdp->start_vcol - oap->start_vcol);
5450 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5451 }
5452 }
5453 }
5454 else
5455 {
5456 prev_pend = pend;
5457 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5458 {
5459 /* Count a tab for what it's worth (if list mode not on) */
5460 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005461 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 bdp->end_vcol += incr;
5463 }
5464 if (bdp->end_vcol <= oap->end_vcol
5465 && (!is_del
5466 || oap->op_type == OP_APPEND
5467 || oap->op_type == OP_REPLACE)) /* line too short */
5468 {
5469#ifdef FEAT_VISUALEXTRA
5470 bdp->is_short = TRUE;
5471#endif
5472 /* Alternative: include spaces to fill up the block.
5473 * Disadvantage: can lead to trailing spaces when the line is
5474 * short where the text is put */
5475 /* if (!is_del || oap->op_type == OP_APPEND) */
5476 if (oap->op_type == OP_APPEND || virtual_op)
5477 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005478 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else
5480 bdp->endspaces = 0; /* replace doesn't add characters */
5481 }
5482 else if (bdp->end_vcol > oap->end_vcol)
5483 {
5484 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5485 if (!is_del && bdp->endspaces)
5486 {
5487 bdp->endspaces = incr - bdp->endspaces;
5488 if (pend != pstart)
5489 pend = prev_pend;
5490 }
5491 }
5492 }
5493#ifdef FEAT_VISUALEXTRA
5494 bdp->end_char_vcols = incr;
5495#endif
5496 if (is_del && bdp->startspaces)
5497 pstart = prev_pstart;
5498 bdp->textlen = (int)(pend - pstart);
5499 }
5500 bdp->textcol = (colnr_T) (pstart - line);
5501 bdp->textstart = pstart;
5502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005505 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005507 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005508op_addsub(
5509 oparg_T *oap,
5510 linenr_T Prenum1, /* Amount of add/subtract */
5511 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005513 pos_T pos;
5514 struct block_def bd;
5515 int change_cnt = 0;
5516 linenr_T amount = Prenum1;
5517
5518 if (!VIsual_active)
5519 {
5520 pos = curwin->w_cursor;
5521 if (u_save_cursor() == FAIL)
5522 return;
5523 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5524 if (change_cnt)
5525 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5526 }
5527 else
5528 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005529 int one_change;
5530 int length;
5531 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005532
5533 if (u_save((linenr_T)(oap->start.lnum - 1),
5534 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5535 return;
5536
5537 pos = oap->start;
5538 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5539 {
5540 if (oap->block_mode) /* Visual block mode */
5541 {
5542 block_prep(oap, &bd, pos.lnum, FALSE);
5543 pos.col = bd.textcol;
5544 length = bd.textlen;
5545 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005546 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005547 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005548 curwin->w_cursor.col = 0;
5549 pos.col = 0;
5550 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5551 }
5552 else /* oap->motion_type == MCHAR */
5553 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005554 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005555 dec(&(oap->end));
5556 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5557 pos.col = 0;
5558 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005559 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005560 pos.col += oap->start.col;
5561 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005562 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005563 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005564 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005565 length = (int)STRLEN(ml_get(oap->end.lnum));
5566 if (oap->end.col >= length)
5567 oap->end.col = length - 1;
5568 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005569 }
5570 }
5571 one_change = do_addsub(oap->op_type, &pos, length, amount);
5572 if (one_change)
5573 {
5574 /* Remember the start position of the first change. */
5575 if (change_cnt == 0)
5576 startpos = curbuf->b_op_start;
5577 ++change_cnt;
5578 }
5579
5580#ifdef FEAT_NETBEANS_INTG
5581 if (netbeans_active() && one_change)
5582 {
5583 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5584
5585 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5586 netbeans_inserted(curbuf, pos.lnum, pos.col,
5587 &ptr[pos.col], length);
5588 }
5589#endif
5590 if (g_cmd && one_change)
5591 amount += Prenum1;
5592 }
5593 if (change_cnt)
5594 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5595
5596 if (!change_cnt && oap->is_VIsual)
5597 /* No change: need to remove the Visual selection */
5598 redraw_curbuf_later(INVERTED);
5599
5600 /* Set '[ mark if something changed. Keep the last end
5601 * position from do_addsub(). */
5602 if (change_cnt > 0)
5603 curbuf->b_op_start = startpos;
5604
5605 if (change_cnt > p_report)
5606 {
5607 if (change_cnt == 1)
5608 MSG(_("1 line changed"));
5609 else
5610 smsg((char_u *)_("%ld lines changed"), change_cnt);
5611 }
5612 }
5613}
5614
5615/*
5616 * Add or subtract 'Prenum1' from a number in a line
5617 * op_type is OP_NR_ADD or OP_NR_SUB
5618 *
5619 * Returns TRUE if some character was changed.
5620 */
5621 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005622do_addsub(
5623 int op_type,
5624 pos_T *pos,
5625 int length,
5626 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 int col;
5629 char_u *buf1;
5630 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005631 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005633 uvarnumber_T n;
5634 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 char_u *ptr;
5636 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 int todel;
5638 int dohex;
5639 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005640 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 int doalp;
5642 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005644 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005645 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005646 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005647 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005648 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005649 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005650 pos_T startpos;
5651 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5654 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005655 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5657
Bram Moolenaard79e5502016-01-10 22:13:02 +01005658 curwin->w_cursor = *pos;
5659 ptr = ml_get(pos->lnum);
5660 col = pos->col;
5661
5662 if (*ptr == NUL)
5663 goto theend;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 /*
5666 * First check if we are on a hexadecimal number, after the "0x".
5667 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005668 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005670 if (dobin)
5671 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005672 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005673 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005674#ifdef FEAT_MBYTE
5675 if (has_mbyte)
5676 col -= (*mb_head_off)(ptr, ptr + col);
5677#endif
5678 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005679
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005680 if (dohex)
5681 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005682 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005683 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005684#ifdef FEAT_MBYTE
5685 if (has_mbyte)
5686 col -= (*mb_head_off)(ptr, ptr + col);
5687#endif
5688 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005689
5690 if ( dobin
5691 && dohex
5692 && ! ((col > 0
5693 && (ptr[col] == 'X'
5694 || ptr[col] == 'x')
5695 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005696#ifdef FEAT_MBYTE
5697 && (!has_mbyte ||
5698 !(*mb_head_off)(ptr, ptr + col - 1))
5699#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005700 && vim_isxdigit(ptr[col + 1]))))
5701 {
5702
5703 /* In case of binary/hexadecimal pattern overlap match, rescan */
5704
Bram Moolenaard79e5502016-01-10 22:13:02 +01005705 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005706
5707 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005708 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005709 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005710#ifdef FEAT_MBYTE
5711 if (has_mbyte)
5712 col -= (*mb_head_off)(ptr, ptr + col);
5713#endif
5714 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005715 }
5716
5717 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005718 && col > 0
5719 && (ptr[col] == 'X'
5720 || ptr[col] == 'x')
5721 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005722#ifdef FEAT_MBYTE
5723 && (!has_mbyte ||
5724 !(*mb_head_off)(ptr, ptr + col - 1))
5725#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005726 && vim_isxdigit(ptr[col + 1])) ||
5727 ( dobin
5728 && col > 0
5729 && (ptr[col] == 'B'
5730 || ptr[col] == 'b')
5731 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005732#ifdef FEAT_MBYTE
5733 && (!has_mbyte ||
5734 !(*mb_head_off)(ptr, ptr + col - 1))
5735#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005736 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005737 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005738 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005739 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005740#ifdef FEAT_MBYTE
5741 if (has_mbyte)
5742 col -= (*mb_head_off)(ptr, ptr + col);
5743#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005744 }
5745 else
5746 {
5747 /*
5748 * Search forward and then backward to find the start of number.
5749 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005750 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005751
5752 while (ptr[col] != NUL
5753 && !vim_isdigit(ptr[col])
5754 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005755 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005756
5757 while (col > 0
5758 && vim_isdigit(ptr[col - 1])
5759 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005760 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005761 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005762#ifdef FEAT_MBYTE
5763 if (has_mbyte)
5764 col -= (*mb_head_off)(ptr, ptr + col);
5765#endif
5766 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005767 }
5768 }
5769
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005770 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005771 {
5772 while (ptr[col] != NUL && length > 0
5773 && !vim_isdigit(ptr[col])
5774 && !(doalp && ASCII_ISALPHA(ptr[col])))
5775 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005776 int mb_len = MB_PTR2LEN(ptr + col);
5777
5778 col += mb_len;
5779 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005780 }
5781
5782 if (length == 0)
5783 goto theend;
5784
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005785 if (col > pos->col && ptr[col - 1] == '-'
5786#ifdef FEAT_MBYTE
5787 && (!has_mbyte ||
5788 !(*mb_head_off)(ptr, ptr + col - 1))
5789#endif
5790 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005791 {
5792 negative = TRUE;
5793 was_positive = FALSE;
5794 }
5795 }
5796
5797 /*
5798 * If a number was found, and saving for undo works, replace the number.
5799 */
5800 firstdigit = ptr[col];
5801 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5802 {
5803 beep_flush();
5804 goto theend;
5805 }
5806
5807 if (doalp && ASCII_ISALPHA(firstdigit))
5808 {
5809 /* decrement or increment alphabetic character */
5810 if (op_type == OP_NR_SUB)
5811 {
5812 if (CharOrd(firstdigit) < Prenum1)
5813 {
5814 if (isupper(firstdigit))
5815 firstdigit = 'A';
5816 else
5817 firstdigit = 'a';
5818 }
5819 else
5820#ifdef EBCDIC
5821 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5822#else
5823 firstdigit -= Prenum1;
5824#endif
5825 }
5826 else
5827 {
5828 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5829 {
5830 if (isupper(firstdigit))
5831 firstdigit = 'Z';
5832 else
5833 firstdigit = 'z';
5834 }
5835 else
5836#ifdef EBCDIC
5837 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5838#else
5839 firstdigit += Prenum1;
5840#endif
5841 }
5842 curwin->w_cursor.col = col;
5843 if (!did_change)
5844 startpos = curwin->w_cursor;
5845 did_change = TRUE;
5846 (void)del_char(FALSE);
5847 ins_char(firstdigit);
5848 endpos = curwin->w_cursor;
5849 curwin->w_cursor.col = col;
5850 }
5851 else
5852 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005853 if (col > 0 && ptr[col - 1] == '-'
5854#ifdef FEAT_MBYTE
5855 && (!has_mbyte ||
5856 !(*mb_head_off)(ptr, ptr + col - 1))
5857#endif
5858 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005859 {
5860 /* negative number */
5861 --col;
5862 negative = TRUE;
5863 }
5864 /* get the number value (unsigned) */
5865 if (visual && VIsual_mode != 'V')
5866 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5867 ? (int)STRLEN(ptr) - col
5868 : length);
5869
5870 vim_str2nr(ptr + col, &pre, &length,
5871 0 + (dobin ? STR2NR_BIN : 0)
5872 + (dooct ? STR2NR_OCT : 0)
5873 + (dohex ? STR2NR_HEX : 0),
5874 NULL, &n, maxlen);
5875
5876 /* ignore leading '-' for hex and octal and bin numbers */
5877 if (pre && negative)
5878 {
5879 ++col;
5880 --length;
5881 negative = FALSE;
5882 }
5883 /* add or subtract */
5884 subtract = FALSE;
5885 if (op_type == OP_NR_SUB)
5886 subtract ^= TRUE;
5887 if (negative)
5888 subtract ^= TRUE;
5889
5890 oldn = n;
5891 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005892 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005893 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005894 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005895 /* handle wraparound for decimal numbers */
5896 if (!pre)
5897 {
5898 if (subtract)
5899 {
5900 if (n > oldn)
5901 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005902 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005903 negative ^= TRUE;
5904 }
5905 }
5906 else
5907 {
5908 /* add */
5909 if (n < oldn)
5910 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005911 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005912 negative ^= TRUE;
5913 }
5914 }
5915 if (n == 0)
5916 negative = FALSE;
5917 }
5918
5919 if (visual && !was_positive && !negative && col > 0)
5920 {
5921 /* need to remove the '-' */
5922 col--;
5923 length++;
5924 }
5925
5926 /*
5927 * Delete the old number.
5928 */
5929 curwin->w_cursor.col = col;
5930 if (!did_change)
5931 startpos = curwin->w_cursor;
5932 did_change = TRUE;
5933 todel = length;
5934 c = gchar_cursor();
5935 /*
5936 * Don't include the '-' in the length, only the length of the
5937 * part after it is kept the same.
5938 */
5939 if (c == '-')
5940 --length;
5941 while (todel-- > 0)
5942 {
5943 if (c < 0x100 && isalpha(c))
5944 {
5945 if (isupper(c))
5946 hexupper = TRUE;
5947 else
5948 hexupper = FALSE;
5949 }
5950 /* del_char() will mark line needing displaying */
5951 (void)del_char(FALSE);
5952 c = gchar_cursor();
5953 }
5954
5955 /*
5956 * Prepare the leading characters in buf1[].
5957 * When there are many leading zeros it could be very long.
5958 * Allocate a bit too much.
5959 */
5960 buf1 = alloc((unsigned)length + NUMBUFLEN);
5961 if (buf1 == NULL)
5962 goto theend;
5963 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005964 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005965 {
5966 *ptr++ = '-';
5967 }
5968 if (pre)
5969 {
5970 *ptr++ = '0';
5971 --length;
5972 }
5973 if (pre == 'b' || pre == 'B' ||
5974 pre == 'x' || pre == 'X')
5975 {
5976 *ptr++ = pre;
5977 --length;
5978 }
5979
5980 /*
5981 * Put the number characters in buf2[].
5982 */
5983 if (pre == 'b' || pre == 'B')
5984 {
5985 int i;
5986 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005987 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005988
5989 /* leading zeros */
5990 for (bit = bits; bit > 0; bit--)
5991 if ((n >> (bit - 1)) & 0x1) break;
5992
5993 for (i = 0; bit > 0; bit--)
5994 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5995
5996 buf2[i] = '\0';
5997 }
5998 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005999 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6000 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006001 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006002 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6003 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006004 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006005 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6006 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006007 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006008 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6009 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006010 length -= (int)STRLEN(buf2);
6011
6012 /*
6013 * Adjust number of zeros to the new number of digits, so the
6014 * total length of the number remains the same.
6015 * Don't do this when
6016 * the result may look like an octal number.
6017 */
6018 if (firstdigit == '0' && !(dooct && pre == 0))
6019 while (length-- > 0)
6020 *ptr++ = '0';
6021 *ptr = NUL;
6022 STRCAT(buf1, buf2);
6023 ins_str(buf1); /* insert the new number */
6024 vim_free(buf1);
6025 endpos = curwin->w_cursor;
6026 if (did_change && curwin->w_cursor.col)
6027 --curwin->w_cursor.col;
6028 }
6029
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006030 if (did_change)
6031 {
6032 /* set the '[ and '] marks */
6033 curbuf->b_op_start = startpos;
6034 curbuf->b_op_end = endpos;
6035 if (curbuf->b_op_end.col > 0)
6036 --curbuf->b_op_end.col;
6037 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006038
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006039theend:
6040 if (visual)
6041 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006042 else if (did_change)
6043 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006044
Bram Moolenaard79e5502016-01-10 22:13:02 +01006045 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046}
6047
6048#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006049
6050static yankreg_T *y_read_regs = NULL;
6051
6052#define REG_PREVIOUS 1
6053#define REG_EXEC 2
6054
6055/*
6056 * Prepare for reading viminfo registers when writing viminfo later.
6057 */
6058 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006059prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006060{
6061 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6062 * (int)sizeof(yankreg_T));
6063}
6064
6065 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006066finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006067{
6068 int i;
6069 int j;
6070
6071 if (y_read_regs != NULL)
6072 {
6073 for (i = 0; i < NUM_REGISTERS; ++i)
6074 if (y_read_regs[i].y_array != NULL)
6075 {
6076 for (j = 0; j < y_read_regs[i].y_size; j++)
6077 vim_free(y_read_regs[i].y_array[j]);
6078 vim_free(y_read_regs[i].y_array);
6079 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006080 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006081 }
6082}
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006085read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 int eof;
6088 int do_it = TRUE;
6089 int size;
6090 int limit;
6091 int i;
6092 int set_prev = FALSE;
6093 char_u *str;
6094 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006095 int new_type = MCHAR; /* init to shut up compiler */
6096 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 /* We only get here (hopefully) if line[0] == '"' */
6099 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006100
6101 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (*str == '"')
6103 {
6104 set_prev = TRUE;
6105 str++;
6106 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (!ASCII_ISALNUM(*str) && *str != '-')
6109 {
6110 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6111 return TRUE; /* too many errors, pretend end-of-file */
6112 do_it = FALSE;
6113 }
6114 get_yank_register(*str++, FALSE);
6115 if (!force && y_current->y_array != NULL)
6116 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006117
6118 if (*str == '@')
6119 {
6120 /* "x@: register x used for @@ */
6121 if (force || execreg_lastc == NUL)
6122 execreg_lastc = str[-1];
6123 }
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 size = 0;
6126 limit = 100; /* Optimized for registers containing <= 100 lines */
6127 if (do_it)
6128 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006129 /*
6130 * Build the new register in array[].
6131 * y_array is kept as-is until done.
6132 * The "do_it" flag is reset when something is wrong, in which case
6133 * array[] needs to be freed.
6134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (set_prev)
6136 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006137 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006138 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006140 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006142 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006144 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 /* get the block width; if it's missing we get a zero, which is OK */
6146 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006147 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149
6150 while (!(eof = viminfo_readline(virp))
6151 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6152 {
6153 if (do_it)
6154 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006155 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006157 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006159
6160 if (new_array == NULL)
6161 {
6162 do_it = FALSE;
6163 break;
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006166 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006168 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 }
6171 str = viminfo_readstring(virp, 1, TRUE);
6172 if (str != NULL)
6173 array[size++] = str;
6174 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006175 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 do_it = FALSE;
6177 }
6178 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (do_it)
6181 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006182 /* free y_array[] */
6183 for (i = 0; i < y_current->y_size; i++)
6184 vim_free(y_current->y_array[i]);
6185 vim_free(y_current->y_array);
6186
6187 y_current->y_type = new_type;
6188 y_current->y_width = new_width;
6189 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006190 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 if (size == 0)
6192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 y_current->y_array = NULL;
6194 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006197 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 y_current->y_array =
6199 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6200 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006201 {
6202 if (y_current->y_array == NULL)
6203 vim_free(array[i]);
6204 else
6205 y_current->y_array[i] = array[i];
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006209 else
6210 {
6211 /* Free array[] if it was filled. */
6212 for (i = 0; i < size; i++)
6213 vim_free(array[i]);
6214 }
6215 vim_free(array);
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 return eof;
6218}
6219
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006220/*
6221 * Accept a new style register line from the viminfo, store it when it's new.
6222 */
6223 void
6224handle_viminfo_register(garray_T *values, int force)
6225{
6226 bval_T *vp = (bval_T *)values->ga_data;
6227 int flags;
6228 int name;
6229 int type;
6230 int linecount;
6231 int width;
6232 time_t timestamp;
6233 yankreg_T *y_ptr;
6234 int i;
6235
6236 /* Check the format:
6237 * |{bartype},{flags},{name},{type},
6238 * {linecount},{width},{timestamp},"line1","line2"
6239 */
6240 if (values->ga_len < 6
6241 || vp[0].bv_type != BVAL_NR
6242 || vp[1].bv_type != BVAL_NR
6243 || vp[2].bv_type != BVAL_NR
6244 || vp[3].bv_type != BVAL_NR
6245 || vp[4].bv_type != BVAL_NR
6246 || vp[5].bv_type != BVAL_NR)
6247 return;
6248 flags = vp[0].bv_nr;
6249 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006250 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006251 return;
6252 type = vp[2].bv_nr;
6253 if (type != MCHAR && type != MLINE && type != MBLOCK)
6254 return;
6255 linecount = vp[3].bv_nr;
6256 if (values->ga_len < 6 + linecount)
6257 return;
6258 width = vp[4].bv_nr;
6259 if (width < 0)
6260 return;
6261
6262 if (y_read_regs != NULL)
6263 /* Reading viminfo for merging and writing. Store the register
6264 * content, don't update the current registers. */
6265 y_ptr = &y_read_regs[name];
6266 else
6267 y_ptr = &y_regs[name];
6268
6269 /* Do not overwrite unless forced or the timestamp is newer. */
6270 timestamp = (time_t)vp[5].bv_nr;
6271 if (y_ptr->y_array != NULL && !force
6272 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6273 return;
6274
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006275 if (y_ptr->y_array != NULL)
6276 for (i = 0; i < y_ptr->y_size; i++)
6277 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006278 vim_free(y_ptr->y_array);
6279
6280 if (y_read_regs == NULL)
6281 {
6282 if (flags & REG_PREVIOUS)
6283 y_previous = y_ptr;
6284 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6285 execreg_lastc = get_register_name(name);
6286 }
6287 y_ptr->y_type = type;
6288 y_ptr->y_width = width;
6289 y_ptr->y_size = linecount;
6290 y_ptr->y_time_set = timestamp;
6291 if (linecount == 0)
6292 y_ptr->y_array = NULL;
6293 else
6294 {
6295 y_ptr->y_array =
6296 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6297 for (i = 0; i < linecount; i++)
6298 {
6299 if (vp[i + 6].bv_allocated)
6300 {
6301 y_ptr->y_array[i] = vp[i + 6].bv_string;
6302 vp[i + 6].bv_string = NULL;
6303 }
6304 else
6305 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6306 }
6307 }
6308}
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006313 int i, j;
6314 char_u *type;
6315 char_u c;
6316 int num_lines;
6317 int max_num_lines;
6318 int max_kbyte;
6319 long len;
6320 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
Bram Moolenaar64404472010-06-26 06:24:45 +02006322 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
6324 /* Get '<' value, use old '"' value if '<' is not found. */
6325 max_num_lines = get_viminfo_parameter('<');
6326 if (max_num_lines < 0)
6327 max_num_lines = get_viminfo_parameter('"');
6328 if (max_num_lines == 0)
6329 return;
6330 max_kbyte = get_viminfo_parameter('s');
6331 if (max_kbyte == 0)
6332 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 for (i = 0; i < NUM_REGISTERS; i++)
6335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336#ifdef FEAT_CLIPBOARD
6337 /* Skip '*'/'+' register, we don't want them back next time */
6338 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6339 continue;
6340#endif
6341#ifdef FEAT_DND
6342 /* Neither do we want the '~' register */
6343 if (i == TILDE_REGISTER)
6344 continue;
6345#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006346 /* When reading viminfo for merging and writing: Use the register from
6347 * viminfo if it's newer. */
6348 if (y_read_regs != NULL
6349 && y_read_regs[i].y_array != NULL
6350 && (y_regs[i].y_array == NULL ||
6351 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6352 y_ptr = &y_read_regs[i];
6353 else if (y_regs[i].y_array == NULL)
6354 continue;
6355 else
6356 y_ptr = &y_regs[i];
6357
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006358 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006359 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006360 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006361 || (num_lines == 1 && y_ptr->y_type == MCHAR
6362 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006363 continue;
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (max_kbyte > 0)
6366 {
6367 /* Skip register if there is more text than the maximum size. */
6368 len = 0;
6369 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006370 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (len > (long)max_kbyte * 1024L)
6372 continue;
6373 }
6374
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006375 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {
6377 case MLINE:
6378 type = (char_u *)"LINE";
6379 break;
6380 case MCHAR:
6381 type = (char_u *)"CHAR";
6382 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 case MBLOCK:
6384 type = (char_u *)"BLOCK";
6385 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 default:
6387 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006388 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 emsg(IObuff);
6390 type = (char_u *)"LINE";
6391 break;
6392 }
6393 if (y_previous == &y_regs[i])
6394 fprintf(fp, "\"");
6395 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006396 fprintf(fp, "\"%c", c);
6397 if (c == execreg_lastc)
6398 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006399 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 /* If max_num_lines < 0, then we save ALL the lines in the register */
6402 if (max_num_lines > 0 && num_lines > max_num_lines)
6403 num_lines = max_num_lines;
6404 for (j = 0; j < num_lines; j++)
6405 {
6406 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006407 viminfo_writestring(fp, y_ptr->y_array[j]);
6408 }
6409
6410 {
6411 int flags = 0;
6412 int remaining;
6413
6414 /* New style with a bar line. Format:
6415 * |{bartype},{flags},{name},{type},
6416 * {linecount},{width},{timestamp},"line1","line2"
6417 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006418 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006419 */
6420 if (y_previous == &y_regs[i])
6421 flags |= REG_PREVIOUS;
6422 if (c == execreg_lastc)
6423 flags |= REG_EXEC;
6424 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6425 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6426 (long)y_ptr->y_time_set);
6427 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6428 remaining = LSIZE - 71;
6429 for (j = 0; j < num_lines; j++)
6430 {
6431 putc(',', fp);
6432 --remaining;
6433 remaining = barline_writestring(fp, y_ptr->y_array[j],
6434 remaining);
6435 }
6436 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 }
6438 }
6439}
6440#endif /* FEAT_VIMINFO */
6441
6442#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6443/*
6444 * SELECTION / PRIMARY ('*')
6445 *
6446 * Text selection stuff that uses the GUI selection register '*'. When using a
6447 * GUI this may be text from another window, otherwise it is the last text we
6448 * had highlighted with VIsual mode. With mouse support, clicking the middle
6449 * button performs the paste, otherwise you will need to do <"*p>. "
6450 * If not under X, it is synonymous with the clipboard register '+'.
6451 *
6452 * X CLIPBOARD ('+')
6453 *
6454 * Text selection stuff that uses the GUI clipboard register '+'.
6455 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6456 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6457 * otherwise you will need to do <"+p>. "
6458 * If not under X, it is synonymous with the selection register '*'.
6459 */
6460
6461/*
6462 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006463 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 * only exist while the owning application exists, so we write to the
6465 * permanent (while X runs) store CUT_BUFFER0.
6466 * Dump the CLIPBOARD selection if we own it (it's logically the more
6467 * 'permanent' of the two), otherwise the PRIMARY one.
6468 * For now, use a hard-coded sanity limit of 1Mb of data.
6469 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006470#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 Display *dpy;
6475 char_u *str = NULL;
6476 long_u len = 0;
6477 int motion_type = -1;
6478
6479# ifdef FEAT_GUI
6480 if (gui.in_use)
6481 dpy = X_DISPLAY;
6482 else
6483# endif
6484# ifdef FEAT_XCLIPBOARD
6485 dpy = xterm_dpy;
6486# else
6487 return;
6488# endif
6489
6490 /* Get selection to export */
6491 if (clip_plus.owned)
6492 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6493 else if (clip_star.owned)
6494 motion_type = clip_convert_selection(&str, &len, &clip_star);
6495
6496 /* Check it's OK */
6497 if (dpy != NULL && str != NULL && motion_type >= 0
6498 && len < 1024*1024 && len > 0)
6499 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006500#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006501 int ok = TRUE;
6502
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006503 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6504 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6505 * encoding conversion usually doesn't work, so keep the text as-is.
6506 */
6507 if (has_mbyte)
6508 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006509 vimconv_T vc;
6510
6511 vc.vc_type = CONV_NONE;
6512 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6513 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006514 int intlen = len;
6515 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006516
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006517 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006518 conv_str = string_convert(&vc, str, &intlen);
6519 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006520 if (conv_str != NULL)
6521 {
6522 vim_free(str);
6523 str = conv_str;
6524 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006525 else
6526 {
6527 ok = FALSE;
6528 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006529 convert_setup(&vc, NULL, NULL);
6530 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006531 else
6532 {
6533 ok = FALSE;
6534 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006535 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006536
6537 /* Do not store the string if conversion failed. Better to use any
6538 * other selection than garbled text. */
6539 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006540#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006541 {
6542 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6543 XFlush(dpy);
6544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546
6547 vim_free(str);
6548}
6549#endif
6550
6551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006552clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006554 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556 if (cbd == &clip_plus)
6557 y_current = &y_regs[PLUS_REGISTER];
6558 else
6559 y_current = &y_regs[STAR_REGISTER];
6560 free_yank_all();
6561 y_current->y_size = 0;
6562 y_current = y_ptr;
6563}
6564
6565/*
6566 * Get the selected text and put it in the gui selection register '*' or '+'.
6567 */
6568 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006569clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006571 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 pos_T old_visual;
6574 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 colnr_T old_curswant;
6576 int old_set_curswant;
6577 pos_T old_op_start, old_op_end;
6578 oparg_T oa;
6579 cmdarg_T ca;
6580
6581 if (cbd->owned)
6582 {
6583 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6584 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6585 return;
6586
6587 /* Get the text between clip_star.start & clip_star.end */
6588 old_y_previous = y_previous;
6589 old_y_current = y_current;
6590 old_cursor = curwin->w_cursor;
6591 old_curswant = curwin->w_curswant;
6592 old_set_curswant = curwin->w_set_curswant;
6593 old_op_start = curbuf->b_op_start;
6594 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 old_visual = VIsual;
6596 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 clear_oparg(&oa);
6598 oa.regname = (cbd == &clip_plus ? '+' : '*');
6599 oa.op_type = OP_YANK;
6600 vim_memset(&ca, 0, sizeof(ca));
6601 ca.oap = &oa;
6602 ca.cmdchar = 'y';
6603 ca.count1 = 1;
6604 ca.retval = CA_NO_ADJ_OP_END;
6605 do_pending_operator(&ca, 0, TRUE);
6606 y_previous = old_y_previous;
6607 y_current = old_y_current;
6608 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006609 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 curwin->w_curswant = old_curswant;
6611 curwin->w_set_curswant = old_set_curswant;
6612 curbuf->b_op_start = old_op_start;
6613 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 VIsual = old_visual;
6615 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006617 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
6619 clip_free_selection(cbd);
6620
6621 /* Try to get selected text from another window */
6622 clip_gen_request_selection(cbd);
6623 }
6624}
6625
Bram Moolenaard44347f2011-06-19 01:14:29 +02006626/*
6627 * Convert from the GUI selection string into the '*'/'+' register.
6628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630clip_yank_selection(
6631 int type,
6632 char_u *str,
6633 long len,
6634 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006636 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 if (cbd == &clip_plus)
6639 y_ptr = &y_regs[PLUS_REGISTER];
6640 else
6641 y_ptr = &y_regs[STAR_REGISTER];
6642
6643 clip_free_selection(cbd);
6644
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006645 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
6648/*
6649 * Convert the '*'/'+' register into a GUI selection string returned in *str
6650 * with length *len.
6651 * Returns the motion type, or -1 for failure.
6652 */
6653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006654clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 char_u *p;
6657 int lnum;
6658 int i, j;
6659 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006660 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 if (cbd == &clip_plus)
6663 y_ptr = &y_regs[PLUS_REGISTER];
6664 else
6665 y_ptr = &y_regs[STAR_REGISTER];
6666
6667#ifdef USE_CRNL
6668 eolsize = 2;
6669#else
6670 eolsize = 1;
6671#endif
6672
6673 *str = NULL;
6674 *len = 0;
6675 if (y_ptr->y_array == NULL)
6676 return -1;
6677
6678 for (i = 0; i < y_ptr->y_size; i++)
6679 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6680
6681 /*
6682 * Don't want newline character at end of last line if we're in MCHAR mode.
6683 */
6684 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6685 *len -= eolsize;
6686
6687 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6688 if (p == NULL)
6689 return -1;
6690 lnum = 0;
6691 for (i = 0, j = 0; i < (int)*len; i++, j++)
6692 {
6693 if (y_ptr->y_array[lnum][j] == '\n')
6694 p[i] = NUL;
6695 else if (y_ptr->y_array[lnum][j] == NUL)
6696 {
6697#ifdef USE_CRNL
6698 p[i++] = '\r';
6699#endif
6700#ifdef USE_CR
6701 p[i] = '\r';
6702#else
6703 p[i] = '\n';
6704#endif
6705 lnum++;
6706 j = -1;
6707 }
6708 else
6709 p[i] = y_ptr->y_array[lnum][j];
6710 }
6711 return y_ptr->y_type;
6712}
6713
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715/*
6716 * If we have written to a clipboard register, send the text to the clipboard.
6717 */
6718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006719may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6722 {
6723 clip_own_selection(&clip_star);
6724 clip_gen_set_selection(&clip_star);
6725 }
6726 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6727 {
6728 clip_own_selection(&clip_plus);
6729 clip_gen_set_selection(&clip_plus);
6730 }
6731}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733#endif /* FEAT_CLIPBOARD || PROTO */
6734
6735
6736#if defined(FEAT_DND) || defined(PROTO)
6737/*
6738 * Replace the contents of the '~' register with str.
6739 */
6740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006743 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745 curr = y_current;
6746 y_current = &y_regs[TILDE_REGISTER];
6747 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006748 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 y_current = curr;
6750}
6751#endif
6752
6753
6754#if defined(FEAT_EVAL) || defined(PROTO)
6755/*
6756 * Return the type of a register.
6757 * Used for getregtype()
6758 * Returns MAUTO for error.
6759 */
6760 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 switch (regname)
6764 {
6765 case '%': /* file name */
6766 case '#': /* alternate file name */
6767 case '=': /* expression */
6768 case ':': /* last command line */
6769 case '/': /* last search-pattern */
6770 case '.': /* last inserted text */
6771#ifdef FEAT_SEARCHPATH
6772 case Ctrl_F: /* Filename under cursor */
6773 case Ctrl_P: /* Path under cursor, expand via "path" */
6774#endif
6775 case Ctrl_W: /* word under cursor */
6776 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6777 case '_': /* black hole: always empty */
6778 return MCHAR;
6779 }
6780
6781#ifdef FEAT_CLIPBOARD
6782 regname = may_get_selection(regname);
6783#endif
6784
Bram Moolenaar32b92012014-01-14 12:33:36 +01006785 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006786 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 get_yank_register(regname, FALSE);
6789
6790 if (y_current->y_array != NULL)
6791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (reglen != NULL && y_current->y_type == MBLOCK)
6793 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 return y_current->y_type;
6795 }
6796 return MAUTO;
6797}
6798
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006799static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006800
6801/*
6802 * When "flags" has GREG_LIST return a list with text "s".
6803 * Otherwise just return "s".
6804 */
6805 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006806getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006807{
6808 if (flags & GREG_LIST)
6809 {
6810 list_T *list = list_alloc();
6811
6812 if (list != NULL)
6813 {
6814 if (list_append_string(list, NULL, -1) == FAIL)
6815 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006816 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006817 return NULL;
6818 }
6819 list->lv_first->li_tv.vval.v_string = s;
6820 }
6821 return (char_u *)list;
6822 }
6823 return s;
6824}
6825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826/*
6827 * Return the contents of a register as a single allocated string.
6828 * Used for "@r" in expressions and for getreg().
6829 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006830 * Flags:
6831 * GREG_NO_EXPR Do not allow expression register
6832 * GREG_EXPR_SRC For the expression register: return expression itself,
6833 * not the result of its evaluation.
6834 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 */
6836 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006837get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 long i;
6840 char_u *retval;
6841 int allocated;
6842 long len;
6843
6844 /* Don't allow using an expression register inside an expression */
6845 if (regname == '=')
6846 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006847 if (flags & GREG_NO_EXPR)
6848 return NULL;
6849 if (flags & GREG_EXPR_SRC)
6850 return getreg_wrap_one_line(get_expr_line_src(), flags);
6851 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853
6854 if (regname == '@') /* "@@" is used for unnamed register */
6855 regname = '"';
6856
6857 /* check for valid regname */
6858 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6859 return NULL;
6860
6861#ifdef FEAT_CLIPBOARD
6862 regname = may_get_selection(regname);
6863#endif
6864
6865 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6866 {
6867 if (retval == NULL)
6868 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006869 if (allocated)
6870 return getreg_wrap_one_line(retval, flags);
6871 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873
6874 get_yank_register(regname, FALSE);
6875 if (y_current->y_array == NULL)
6876 return NULL;
6877
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006878 if (flags & GREG_LIST)
6879 {
6880 list_T *list = list_alloc();
6881 int error = FALSE;
6882
6883 if (list == NULL)
6884 return NULL;
6885 for (i = 0; i < y_current->y_size; ++i)
6886 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6887 error = TRUE;
6888 if (error)
6889 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006890 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006891 return NULL;
6892 }
6893 return (char_u *)list;
6894 }
6895
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 /*
6897 * Compute length of resulting string.
6898 */
6899 len = 0;
6900 for (i = 0; i < y_current->y_size; ++i)
6901 {
6902 len += (long)STRLEN(y_current->y_array[i]);
6903 /*
6904 * Insert a newline between lines and after last line if
6905 * y_type is MLINE.
6906 */
6907 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6908 ++len;
6909 }
6910
6911 retval = lalloc(len + 1, TRUE);
6912
6913 /*
6914 * Copy the lines of the yank register into the string.
6915 */
6916 if (retval != NULL)
6917 {
6918 len = 0;
6919 for (i = 0; i < y_current->y_size; ++i)
6920 {
6921 STRCPY(retval + len, y_current->y_array[i]);
6922 len += (long)STRLEN(retval + len);
6923
6924 /*
6925 * Insert a NL between lines and after the last line if y_type is
6926 * MLINE.
6927 */
6928 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6929 retval[len++] = '\n';
6930 }
6931 retval[len] = NUL;
6932 }
6933
6934 return retval;
6935}
6936
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006937 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006938init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006939 int name,
6940 yankreg_T **old_y_previous,
6941 yankreg_T **old_y_current,
6942 int must_append,
6943 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006944{
6945 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6946 {
6947 emsg_invreg(name);
6948 return FAIL;
6949 }
6950
6951 /* Don't want to change the current (unnamed) register */
6952 *old_y_previous = y_previous;
6953 *old_y_current = y_current;
6954
6955 get_yank_register(name, TRUE);
6956 if (!y_append && !must_append)
6957 free_yank_all();
6958 return OK;
6959}
6960
6961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006962finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006963 int name,
6964 yankreg_T *old_y_previous,
6965 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006966{
6967# ifdef FEAT_CLIPBOARD
6968 /* Send text of clipboard register to the clipboard. */
6969 may_set_selection();
6970# endif
6971
6972 /* ':let @" = "val"' should change the meaning of the "" register */
6973 if (name != '"')
6974 y_previous = old_y_previous;
6975 y_current = old_y_current;
6976}
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978/*
6979 * Store string "str" in register "name".
6980 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6981 * If "must_append" is TRUE, always append to the register. Otherwise append
6982 * if "name" is an uppercase letter.
6983 * Note: "maxlen" and "must_append" don't work for the "/" register.
6984 * Careful: 'str' is modified, you may have to use a copy!
6985 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6986 */
6987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006988write_reg_contents(
6989 int name,
6990 char_u *str,
6991 int maxlen,
6992 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
6994 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6995}
6996
6997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006998write_reg_contents_lst(
6999 int name,
7000 char_u **strings,
7001 int maxlen UNUSED,
7002 int must_append,
7003 int yank_type,
7004 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007005{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007006 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007007
7008 if (name == '/'
7009#ifdef FEAT_EVAL
7010 || name == '='
7011#endif
7012 )
7013 {
7014 char_u *s;
7015
7016 if (strings[0] == NULL)
7017 s = (char_u *)"";
7018 else if (strings[1] != NULL)
7019 {
7020 EMSG(_("E883: search pattern and expression register may not "
7021 "contain two or more lines"));
7022 return;
7023 }
7024 else
7025 s = strings[0];
7026 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7027 return;
7028 }
7029
7030 if (name == '_') /* black hole: nothing to do */
7031 return;
7032
7033 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7034 &yank_type) == FAIL)
7035 return;
7036
7037 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7038
7039 finish_write_reg(name, old_y_previous, old_y_current);
7040}
7041
7042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007043write_reg_contents_ex(
7044 int name,
7045 char_u *str,
7046 int maxlen,
7047 int must_append,
7048 int yank_type,
7049 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007051 yankreg_T *old_y_previous, *old_y_current;
7052 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
Bram Moolenaare7566042005-06-17 22:00:15 +00007054 if (maxlen >= 0)
7055 len = maxlen;
7056 else
7057 len = (long)STRLEN(str);
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 /* Special case: '/' search pattern */
7060 if (name == '/')
7061 {
7062 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7063 return;
7064 }
7065
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007066 if (name == '#')
7067 {
7068 buf_T *buf;
7069
7070 if (VIM_ISDIGIT(*str))
7071 {
7072 int num = atoi((char *)str);
7073
7074 buf = buflist_findnr(num);
7075 if (buf == NULL)
7076 EMSGN(_(e_nobufnr), (long)num);
7077 }
7078 else
7079 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7080 TRUE, FALSE, FALSE));
7081 if (buf == NULL)
7082 return;
7083 curwin->w_alt_fnum = buf->b_fnum;
7084 return;
7085 }
7086
Bram Moolenaare7566042005-06-17 22:00:15 +00007087#ifdef FEAT_EVAL
7088 if (name == '=')
7089 {
7090 char_u *p, *s;
7091
7092 p = vim_strnsave(str, (int)len);
7093 if (p == NULL)
7094 return;
7095 if (must_append)
7096 {
7097 s = concat_str(get_expr_line_src(), p);
7098 vim_free(p);
7099 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007100 }
7101 set_expr_line(p);
7102 return;
7103 }
7104#endif
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (name == '_') /* black hole: nothing to do */
7107 return;
7108
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007109 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7110 &yank_type) == FAIL)
7111 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007113 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007115 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116}
7117#endif /* FEAT_EVAL */
7118
7119#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7120/*
7121 * Put a string into a register. When the register is not empty, the string
7122 * is appended.
7123 */
7124 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007125str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007126 yankreg_T *y_ptr, /* pointer to yank register */
7127 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7128 char_u *str, /* string to put in register */
7129 long len, /* length of string */
7130 long blocklen, /* width of Visual block */
7131 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007133 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 int lnum;
7135 long start;
7136 long i;
7137 int extra;
7138 int newlines; /* number of lines added */
7139 int extraline = 0; /* extra line at the end */
7140 int append = FALSE; /* append to last line in register */
7141 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007142 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007146 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 y_ptr->y_size = 0;
7148
Bram Moolenaard44347f2011-06-19 01:14:29 +02007149 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007150 type = ((str_list || (len > 0 && (str[len - 1] == NL
7151 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007152 ? MLINE : MCHAR);
7153 else
7154 type = yank_type;
7155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 /*
7157 * Count the number of lines within the string
7158 */
7159 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007160 if (str_list)
7161 {
7162 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007167 for (i = 0; i < len; i++)
7168 if (str[i] == '\n')
7169 ++newlines;
7170 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7171 {
7172 extraline = 1;
7173 ++newlines; /* count extra newline at the end */
7174 }
7175 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7176 {
7177 append = TRUE;
7178 --newlines; /* uncount newline when appending first line */
7179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
7181
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007182 /* Without any lines make the register empty. */
7183 if (y_ptr->y_size + newlines == 0)
7184 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007185 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007186 return;
7187 }
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 /*
7190 * Allocate an array to hold the pointers to the new register lines.
7191 * If the register was not empty, move the existing lines to the new array.
7192 */
7193 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7194 * sizeof(char_u *), TRUE);
7195 if (pp == NULL) /* out of memory */
7196 return;
7197 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7198 pp[lnum] = y_ptr->y_array[lnum];
7199 vim_free(y_ptr->y_array);
7200 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203 /*
7204 * Find the end of each line and save it into the array.
7205 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007206 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007208 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7209 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007210 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007211 pp[lnum] = vim_strnsave(*ss, i);
7212 if (i > maxlen)
7213 maxlen = i;
7214 }
7215 }
7216 else
7217 {
7218 for (start = 0; start < len + extraline; start += i + 1)
7219 {
7220 for (i = start; i < len; ++i) /* find the end of the line */
7221 if (str[i] == '\n')
7222 break;
7223 i -= start; /* i is now length of line */
7224 if (i > maxlen)
7225 maxlen = i;
7226 if (append)
7227 {
7228 --lnum;
7229 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7230 }
7231 else
7232 extra = 0;
7233 s = alloc((unsigned)(i + extra + 1));
7234 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007236 if (extra)
7237 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7238 if (append)
7239 vim_free(y_ptr->y_array[lnum]);
7240 if (i)
7241 mch_memmove(s + extra, str + start, (size_t)i);
7242 extra += i;
7243 s[extra] = NUL;
7244 y_ptr->y_array[lnum++] = s;
7245 while (--extra >= 0)
7246 {
7247 if (*s == NUL)
7248 *s = '\n'; /* replace NUL with newline */
7249 ++s;
7250 }
7251 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254 y_ptr->y_type = type;
7255 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 if (type == MBLOCK)
7257 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7258 else
7259 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007260#ifdef FEAT_VIMINFO
7261 y_ptr->y_time_set = vim_time();
7262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7265
7266 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 vim_memset(oap, 0, sizeof(oparg_T));
7270}
7271
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007272static 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 +00007273
7274/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007275 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 *
7277 * "Words" are counted by looking for boundaries between non-space and
7278 * space characters. (it seems to produce results that match 'wc'.)
7279 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007280 * Return value is byte count; word count for the line is added to "*wc".
7281 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 *
7283 * The function will only examine the first "limit" characters in the
7284 * line, stopping if it encounters an end-of-line (NUL byte). In that
7285 * case, eol_size will be added to the character count to account for
7286 * the size of the EOL character.
7287 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007288 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007289line_count_info(
7290 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007291 varnumber_T *wc,
7292 varnumber_T *cc,
7293 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007294 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007296 varnumber_T i;
7297 varnumber_T words = 0;
7298 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 int is_word = 0;
7300
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007301 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 if (is_word)
7304 {
7305 if (vim_isspace(line[i]))
7306 {
7307 words++;
7308 is_word = 0;
7309 }
7310 }
7311 else if (!vim_isspace(line[i]))
7312 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 ++chars;
7314#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007315 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007316#else
7317 ++i;
7318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320
7321 if (is_word)
7322 words++;
7323 *wc += words;
7324
7325 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007326 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007329 chars += eol_size;
7330 }
7331 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 return i;
7333}
7334
7335/*
7336 * Give some info about the position of the cursor (for "g CTRL-G").
7337 * In Visual mode, give some info about the selected region. (In this case,
7338 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007339 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 */
7341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007342cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007345 char_u buf1[50];
7346 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007348 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007349#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007350 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007351#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007352 varnumber_T byte_count_cursor = 0;
7353 varnumber_T char_count = 0;
7354 varnumber_T char_count_cursor = 0;
7355 varnumber_T word_count = 0;
7356 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007357 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007358 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 long line_count_selected = 0;
7360 pos_T min_pos, max_pos;
7361 oparg_T oparg;
7362 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 /*
7365 * Compute the length of the file in characters.
7366 */
7367 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7368 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007369 if (dict == NULL)
7370 {
7371 MSG(_(no_lines_msg));
7372 return;
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 else
7376 {
7377 if (get_fileformat(curbuf) == EOL_DOS)
7378 eol_size = 2;
7379 else
7380 eol_size = 1;
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 if (VIsual_active)
7383 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007384 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 min_pos = VIsual;
7387 max_pos = curwin->w_cursor;
7388 }
7389 else
7390 {
7391 min_pos = curwin->w_cursor;
7392 max_pos = VIsual;
7393 }
7394 if (*p_sel == 'e' && max_pos.col > 0)
7395 --max_pos.col;
7396
7397 if (VIsual_mode == Ctrl_V)
7398 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007399#ifdef FEAT_LINEBREAK
7400 char_u * saved_sbr = p_sbr;
7401
7402 /* Make 'sbr' empty for a moment to get the correct size. */
7403 p_sbr = empty_option;
7404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 oparg.is_VIsual = 1;
7406 oparg.block_mode = TRUE;
7407 oparg.op_type = OP_NOP;
7408 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007409 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007410#ifdef FEAT_LINEBREAK
7411 p_sbr = saved_sbr;
7412#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007413 if (curwin->w_curswant == MAXCOL)
7414 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 /* Swap the start, end vcol if needed */
7416 if (oparg.end_vcol < oparg.start_vcol)
7417 {
7418 oparg.end_vcol += oparg.start_vcol;
7419 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7420 oparg.end_vcol -= oparg.start_vcol;
7421 }
7422 }
7423 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7427 {
7428 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007429 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
7431 ui_breakcheck();
7432 if (got_int)
7433 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007434 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 /* Do extra processing for VIsual mode. */
7438 if (VIsual_active
7439 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7440 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007441 char_u *s = NULL;
7442 long len = 0L;
7443
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 switch (VIsual_mode)
7445 {
7446 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007447#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007451#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007453#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007454 s = bd.textstart;
7455 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 break;
7457 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007458 s = ml_get(lnum);
7459 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 break;
7461 case 'v':
7462 {
7463 colnr_T start_col = (lnum == min_pos.lnum)
7464 ? min_pos.col : 0;
7465 colnr_T end_col = (lnum == max_pos.lnum)
7466 ? max_pos.col - start_col + 1 : MAXCOL;
7467
Bram Moolenaardef9e822004-12-31 20:58:58 +00007468 s = ml_get(lnum) + start_col;
7469 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 }
7471 break;
7472 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007473 if (s != NULL)
7474 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007475 byte_count_cursor += line_count_info(s, &word_count_cursor,
7476 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007477 if (lnum == curbuf->b_ml.ml_line_count
7478 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007479 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007480 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007481 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
7486 /* In non-visual mode, check for the line the cursor is on */
7487 if (lnum == curwin->w_cursor.lnum)
7488 {
7489 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007490 char_count_cursor += char_count;
7491 byte_count_cursor = byte_count +
7492 line_count_info(ml_get(lnum),
7493 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007494 (varnumber_T)(curwin->w_cursor.col + 1),
7495 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497 }
7498 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007499 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007500 &char_count, (varnumber_T)MAXCOL,
7501 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503
7504 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007505 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007506 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
Bram Moolenaared767a22016-01-03 22:49:16 +01007508 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007510 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007512 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7513 {
7514 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7515 &max_pos.col);
7516 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7517 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7518 }
7519 else
7520 buf1[0] = NUL;
7521
7522 if (char_count_cursor == byte_count_cursor
7523 && char_count == byte_count)
7524 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007525 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007526 buf1, line_count_selected,
7527 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007528 (long long)word_count_cursor,
7529 (long long)word_count,
7530 (long long)byte_count_cursor,
7531 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007532 else
7533 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007534 _("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 +01007535 buf1, line_count_selected,
7536 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007537 (long long)word_count_cursor,
7538 (long long)word_count,
7539 (long long)char_count_cursor,
7540 (long long)char_count,
7541 (long long)byte_count_cursor,
7542 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007545 {
7546 p = ml_get_curline();
7547 validate_virtcol();
7548 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7549 (int)curwin->w_virtcol + 1);
7550 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7551 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552
Bram Moolenaared767a22016-01-03 22:49:16 +01007553 if (char_count_cursor == byte_count_cursor
7554 && char_count == byte_count)
7555 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007556 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007557 (char *)buf1, (char *)buf2,
7558 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007560 (long long)word_count_cursor, (long long)word_count,
7561 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007562 else
7563 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007564 _("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 +01007565 (char *)buf1, (char *)buf2,
7566 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007567 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007568 (long long)word_count_cursor, (long long)word_count,
7569 (long long)char_count_cursor, (long long)char_count,
7570 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007571 }
7572 }
7573
Bram Moolenaared767a22016-01-03 22:49:16 +01007574#ifdef FEAT_MBYTE
7575 bom_count = bomb_size();
7576 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007577 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007578 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007579#endif
7580 if (dict == NULL)
7581 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007582 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007583 p = p_shm;
7584 p_shm = (char_u *)"";
7585 msg(IObuff);
7586 p_shm = p;
7587 }
7588 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007589#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007590 if (dict != NULL)
7591 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007592 dict_add_nr_str(dict, "words", word_count, NULL);
7593 dict_add_nr_str(dict, "chars", char_count, NULL);
7594 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007595# ifdef FEAT_MBYTE
7596 + bom_count
7597# endif
7598 , NULL);
7599 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007600 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007601 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007602 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007603 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007604 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607}