blob: 2e08a53291235006141fc0553132a471b1fb1a9e [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
Bram Moolenaarf2732452018-06-03 14:47:35 +0200129// Flags for third item in "opchars".
130#define OPF_LINES 1 // operator always works on lines
131#define OPF_CHANGE 2 // operator changes text
132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * The names of operators.
135 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +0200136 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 */
138static char opchars[][3] =
139{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200140 {NUL, NUL, 0}, // OP_NOP
141 {'d', NUL, OPF_CHANGE}, // OP_DELETE
142 {'y', NUL, 0}, // OP_YANK
143 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
144 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
145 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
146 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
147 {'g', '~', OPF_CHANGE}, // OP_TILDE
148 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
149 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
150 {':', NUL, OPF_LINES}, // OP_COLON
151 {'g', 'U', OPF_CHANGE}, // OP_UPPER
152 {'g', 'u', OPF_CHANGE}, // OP_LOWER
153 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
154 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
155 {'g', '?', OPF_CHANGE}, // OP_ROT13
156 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
157 {'I', NUL, OPF_CHANGE}, // OP_INSERT
158 {'A', NUL, OPF_CHANGE}, // OP_APPEND
159 {'z', 'f', OPF_LINES}, // OP_FOLD
160 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
161 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
162 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
163 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
164 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
165 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
166 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
167 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
168 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
169 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170};
171
172/*
173 * Translate a command name into an operator type.
174 * Must only be called with a valid operator name!
175 */
176 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100177get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178{
179 int i;
180
181 if (char1 == 'r') /* ignore second character */
182 return OP_REPLACE;
183 if (char1 == '~') /* when tilde is an operator */
184 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100185 if (char1 == 'g' && char2 == Ctrl_A) /* add */
186 return OP_NR_ADD;
187 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
188 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (opchars[i][0] == char1 && opchars[i][1] == char2)
192 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100193 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
194 {
195 internal_error("get_op_type()");
196 break;
197 }
198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 return i;
200}
201
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202/*
203 * Return TRUE if operator "op" always works on whole lines.
204 */
205 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100206op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200208 return opchars[op][2] & OPF_LINES;
209}
210
211/*
212 * Return TRUE if operator "op" changes text.
213 */
214 int
215op_is_change(int op)
216{
217 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220/*
221 * Get first operator command character.
222 * Returns 'g' or 'z' if there is another command character.
223 */
224 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 return opchars[optype][0];
228}
229
230/*
231 * Get second operator command character.
232 */
233 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100234get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 return opchars[optype][1];
237}
238
239/*
240 * op_shift - handle a shift operation
241 */
242 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100243op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244{
245 long i;
246 int first_char;
247 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 if (u_save((linenr_T)(oap->start.lnum - 1),
251 (linenr_T)(oap->end.lnum + 1)) == FAIL)
252 return;
253
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 if (oap->block_mode)
255 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257 for (i = oap->line_count; --i >= 0; )
258 {
259 first_char = *ml_get_curline();
260 if (first_char == NUL) /* empty line */
261 curwin->w_cursor.col = 0;
262#ifdef FEAT_VISUALEXTRA
263 else if (oap->block_mode)
264 shift_block(oap, amount);
265#endif
266 else
267 /* Move the line right if it doesn't start with '#', 'smartindent'
268 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
269#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
270 if (first_char != '#' || !preprocs_left())
271#endif
272 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000273 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275 ++curwin->w_cursor.lnum;
276 }
277
278 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 if (oap->block_mode)
280 {
281 curwin->w_cursor.lnum = oap->start.lnum;
282 curwin->w_cursor.col = block_col;
283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100284 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 {
286 curwin->w_cursor.lnum = oap->start.lnum;
287 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
288 }
289 else
290 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
291
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100292#ifdef FEAT_FOLDING
293 /* The cursor line is not in a closed fold */
294 foldOpenCursor();
295#endif
296
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 if (oap->line_count > p_report)
299 {
300 if (oap->op_type == OP_RSHIFT)
301 s = (char_u *)">";
302 else
303 s = (char_u *)"<";
304 if (oap->line_count == 1)
305 {
306 if (amount == 1)
307 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
308 else
309 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
310 }
311 else
312 {
313 if (amount == 1)
314 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
315 oap->line_count, s);
316 else
317 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
318 oap->line_count, s, amount);
319 }
320 msg(IObuff);
321 }
322
323 /*
324 * Set "'[" and "']" marks.
325 */
326 curbuf->b_op_start = oap->start;
327 curbuf->b_op_end.lnum = oap->end.lnum;
328 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
329 if (curbuf->b_op_end.col > 0)
330 --curbuf->b_op_end.col;
331}
332
333/*
334 * shift the current line one shiftwidth left (if left != 0) or right
335 * leaves cursor on first blank in the line
336 */
337 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338shift_line(
339 int left,
340 int round,
341 int amount,
342 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
344 int count;
345 int i, j;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100346 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
348 count = get_indent(); /* get current indent */
349
350 if (round) /* round off indent */
351 {
352 i = count / p_sw; /* number of p_sw rounded down */
353 j = count % p_sw; /* extra spaces */
354 if (j && left) /* first remove extra spaces */
355 --amount;
356 if (left)
357 {
358 i -= amount;
359 if (i < 0)
360 i = 0;
361 }
362 else
363 i += amount;
364 count = i * p_sw;
365 }
366 else /* original vi indent */
367 {
368 if (left)
369 {
370 count -= p_sw * amount;
371 if (count < 0)
372 count = 0;
373 }
374 else
375 count += p_sw * amount;
376 }
377
378 /* Set new indent */
379#ifdef FEAT_VREPLACE
380 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000381 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 else
383#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000384 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385}
386
387#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
388/*
389 * Shift one line of the current block one shiftwidth right or left.
390 * Leaves cursor on first character in block.
391 */
392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100393shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
395 int left = (oap->op_type == OP_LSHIFT);
396 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000397 int total;
398 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 int oldcol = curwin->w_cursor.col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100400 int p_sw = (int)get_sw_value(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200401#ifdef FEAT_VARTABS
402 int *p_vts = curbuf->b_p_vts_array;
403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 int p_ts = (int)curbuf->b_p_ts;
405 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 int i = 0, j = 0;
409 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#ifdef FEAT_RIGHTLEFT
411 int old_p_ri = p_ri;
412
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200413 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#endif
415
416 State = INSERT; /* don't want REPLACE for State */
417 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
418 if (bd.is_short)
419 return;
420
421 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200422 total = (int)((unsigned)amount * (unsigned)p_sw);
423 if ((total / p_sw) != amount)
424 return; /* multiplication overflow */
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 oldp = ml_get_curline();
427
428 if (!left)
429 {
430 /*
431 * 1. Get start vcol
432 * 2. Total ws vcols
433 * 3. Divvy into TABs & spp
434 * 4. Construct new string
435 */
436 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
437 ws_vcol = bd.start_vcol - bd.pre_whitesp;
438 if (bd.startspaces)
439 {
440#ifdef FEAT_MBYTE
441 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100442 {
443 if ((*mb_ptr2len)(bd.textstart) == 1)
444 ++bd.textstart;
445 else
446 {
447 ws_vcol = 0;
448 bd.startspaces = 0;
449 }
450 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000453 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100455 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200457 /* TODO: is passing bd.textstart for start of the line OK? */
458 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
459 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 total += incr;
461 bd.start_vcol += incr;
462 }
463 /* OK, now total=all the VWS reqd, and textstart points at the 1st
464 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200465#ifdef FEAT_VARTABS
466 if (!curbuf->b_p_et)
467 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
468 else
469 j = total;
470#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 if (!curbuf->b_p_et)
472 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
473 if (i)
474 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
475 else
476 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 /* if we're splitting a TAB, allow for it */
479 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
480 len = (int)STRLEN(bd.textstart) + 1;
481 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
482 if (newp == NULL)
483 return;
484 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
485 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200486 vim_memset(newp + bd.textcol, TAB, (size_t)i);
487 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 /* the end */
489 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
490 }
491 else /* left */
492 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000493 colnr_T destination_col; /* column to which text in block will
494 be shifted */
495 char_u *verbatim_copy_end; /* end of the part of the line which is
496 copied verbatim */
497 colnr_T verbatim_copy_width;/* the (displayed) width of this part
498 of line */
499 unsigned fill; /* nr of spaces that replace a TAB */
500 unsigned new_line_len; /* the length of the line after the
501 block shift */
502 size_t block_space_width;
503 size_t shift_amount;
504 char_u *non_white = bd.textstart;
505 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507 /*
508 * Firstly, let's find the first non-whitespace character that is
509 * displayed after the block's start column and the character's column
510 * number. Also, let's calculate the width of all the whitespace
511 * characters that are displayed in the block and precede the searched
512 * non-whitespace character.
513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
516 * the part of which is displayed at the block's beginning. Let's start
517 * searching from the next character. */
518 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100519 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520
521 /* The character's column is in "bd.start_vcol". */
522 non_white_col = bd.start_vcol;
523
Bram Moolenaar1c465442017-03-12 20:10:05 +0100524 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000525 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200526 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000527 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 }
529
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000530 block_space_width = non_white_col - oap->start_vcol;
531 /* We will shift by "total" or "block_space_width", whichever is less.
532 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000533 shift_amount = (block_space_width < (size_t)total
534 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000535
536 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000537 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000538
539 /* Now let's find out how much of the beginning of the line we can
540 * reuse without modification. */
541 verbatim_copy_end = bd.textstart;
542 verbatim_copy_width = bd.start_vcol;
543
544 /* If "bd.startspaces" is set, "bd.textstart" points to the character
545 * preceding the block. We have to subtract its width to obtain its
546 * column number. */
547 if (bd.startspaces)
548 verbatim_copy_width -= bd.start_char_vcols;
549 while (verbatim_copy_width < destination_col)
550 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200551 char_u *line = verbatim_copy_end;
552
553 /* TODO: is passing verbatim_copy_end for start of the line OK? */
554 incr = lbr_chartabsize(line, verbatim_copy_end,
555 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000556 if (verbatim_copy_width + incr > destination_col)
557 break;
558 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100559 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000560 }
561
562 /* If "destination_col" is different from the width of the initial
563 * part of the line that will be copied, it means we encountered a tab
564 * character, which we will have to partly replace with spaces. */
565 fill = destination_col - verbatim_copy_width;
566
567 /* The replacement line will consist of:
568 * - the beginning of the original line up to "verbatim_copy_end",
569 * - "fill" number of spaces,
570 * - the rest of the line, pointed to by non_white. */
571 new_line_len = (unsigned)(verbatim_copy_end - oldp)
572 + fill
573 + (unsigned)STRLEN(non_white) + 1;
574
575 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (newp == NULL)
577 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000578 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200579 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000580 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 }
582 /* replace the line */
583 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
584 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
585 State = oldstate;
586 curwin->w_cursor.col = oldcol;
587#ifdef FEAT_RIGHTLEFT
588 p_ri = old_p_ri;
589#endif
590}
591#endif
592
593#ifdef FEAT_VISUALEXTRA
594/*
595 * Insert string "s" (b_insert ? before : after) block :AKelly
596 * Caller must prepare for undo.
597 */
598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599block_insert(
600 oparg_T *oap,
601 char_u *s,
602 int b_insert,
603 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 int p_ts;
606 int count = 0; /* extra spaces to replace a cut TAB */
607 int spaces = 0; /* non-zero if cutting a TAB */
608 colnr_T offset; /* pointer along new line */
609 unsigned s_len; /* STRLEN(s) */
610 char_u *newp, *oldp; /* new, old lines */
611 linenr_T lnum; /* loop var */
612 int oldstate = State;
613
614 State = INSERT; /* don't want REPLACE for State */
615 s_len = (unsigned)STRLEN(s);
616
617 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
618 {
619 block_prep(oap, bdp, lnum, TRUE);
620 if (bdp->is_short && b_insert)
621 continue; /* OP_INSERT, line ends before block start */
622
623 oldp = ml_get(lnum);
624
625 if (b_insert)
626 {
627 p_ts = bdp->start_char_vcols;
628 spaces = bdp->startspaces;
629 if (spaces != 0)
630 count = p_ts - 1; /* we're cutting a TAB */
631 offset = bdp->textcol;
632 }
633 else /* append */
634 {
635 p_ts = bdp->end_char_vcols;
636 if (!bdp->is_short) /* spaces = padding after block */
637 {
638 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
639 if (spaces != 0)
640 count = p_ts - 1; /* we're cutting a TAB */
641 offset = bdp->textcol + bdp->textlen - (spaces != 0);
642 }
643 else /* spaces = padding to block edge */
644 {
645 /* if $ used, just append to EOL (ie spaces==0) */
646 if (!bdp->is_MAX)
647 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
648 count = spaces;
649 offset = bdp->textcol + bdp->textlen;
650 }
651 }
652
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200653#ifdef FEAT_MBYTE
654 if (has_mbyte && spaces > 0)
655 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100656 int off;
657
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200658 /* Avoid starting halfway a multi-byte character. */
659 if (b_insert)
660 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100661 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200662 }
663 else
664 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100665 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200666 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200667 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100668 spaces -= off;
669 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200670 }
671#endif
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
674 if (newp == NULL)
675 continue;
676
677 /* copy up to shifted part */
678 mch_memmove(newp, oldp, (size_t)(offset));
679 oldp += offset;
680
681 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200682 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 /* copy the new text */
685 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
686 offset += s_len;
687
688 if (spaces && !bdp->is_short)
689 {
690 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200691 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 /* We're splitting a TAB, don't copy it. */
693 oldp++;
694 /* We allowed for that TAB, remember this now */
695 count++;
696 }
697
698 if (spaces > 0)
699 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000700 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 ml_replace(lnum, newp, FALSE);
703
704 if (lnum == oap->end.lnum)
705 {
706 /* Set "']" mark to the end of the block instead of the end of
707 * the insert in the first line. */
708 curbuf->b_op_end.lnum = oap->end.lnum;
709 curbuf->b_op_end.col = offset;
710 }
711 } /* for all lnum */
712
713 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
714
715 State = oldstate;
716}
717#endif
718
719#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
720/*
721 * op_reindent - handle reindenting a block of lines.
722 */
723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100724op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 long i;
727 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200728 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 linenr_T first_changed = 0;
730 linenr_T last_changed = 0;
731 linenr_T start_lnum = curwin->w_cursor.lnum;
732
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000733 /* Don't even try when 'modifiable' is off. */
734 if (!curbuf->b_p_ma)
735 {
736 EMSG(_(e_modifiable));
737 return;
738 }
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 for (i = oap->line_count; --i >= 0 && !got_int; )
741 {
742 /* it's a slow thing to do, so give feedback so there's no worry that
743 * the computer's just hung. */
744
745 if (i > 1
746 && (i % 50 == 0 || i == oap->line_count - 1)
747 && oap->line_count > p_report)
748 smsg((char_u *)_("%ld lines to indent... "), i);
749
750 /*
751 * Be vi-compatible: For lisp indenting the first line is not
752 * indented, unless there is only one line.
753 */
754#ifdef FEAT_LISP
755 if (i != oap->line_count - 1 || oap->line_count == 1
756 || how != get_lisp_indent)
757#endif
758 {
759 l = skipwhite(ml_get_curline());
760 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200761 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200763 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200765 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
767 /* did change the indent, call changed_lines() later */
768 if (first_changed == 0)
769 first_changed = curwin->w_cursor.lnum;
770 last_changed = curwin->w_cursor.lnum;
771 }
772 }
773 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000774 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776
777 /* put cursor on first non-blank of indented line */
778 curwin->w_cursor.lnum = start_lnum;
779 beginline(BL_SOL | BL_FIX);
780
781 /* Mark changed lines so that they will be redrawn. When Visual
782 * highlighting was present, need to continue until the last line. When
783 * there is no change still need to remove the Visual highlighting. */
784 if (last_changed != 0)
785 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 else if (oap->is_VIsual)
789 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 if (oap->line_count > p_report)
792 {
793 i = oap->line_count - (i + 1);
794 if (i == 1)
795 MSG(_("1 line indented "));
796 else
797 smsg((char_u *)_("%ld lines indented "), i);
798 }
799 /* set '[ and '] marks */
800 curbuf->b_op_start = oap->start;
801 curbuf->b_op_end = oap->end;
802}
803#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
804
805#if defined(FEAT_EVAL) || defined(PROTO)
806/*
807 * Keep the last expression line here, for repeating.
808 */
809static char_u *expr_line = NULL;
810
811/*
812 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
813 * Returns '=' when OK, NUL otherwise.
814 */
815 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100816get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 char_u *new_line;
819
820 new_line = getcmdline('=', 0L, 0);
821 if (new_line == NULL)
822 return NUL;
823 if (*new_line == NUL) /* use previous line */
824 vim_free(new_line);
825 else
826 set_expr_line(new_line);
827 return '=';
828}
829
830/*
831 * Set the expression for the '=' register.
832 * Argument must be an allocated string.
833 */
834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100835set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836{
837 vim_free(expr_line);
838 expr_line = new_line;
839}
840
841/*
842 * Get the result of the '=' register expression.
843 * Returns a pointer to allocated memory, or NULL for failure.
844 */
845 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100846get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 char_u *expr_copy;
849 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000850 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 if (expr_line == NULL)
853 return NULL;
854
855 /* Make a copy of the expression, because evaluating it may cause it to be
856 * changed. */
857 expr_copy = vim_strsave(expr_line);
858 if (expr_copy == NULL)
859 return NULL;
860
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000861 /* When we are invoked recursively limit the evaluation to 10 levels.
862 * Then return the string as-is. */
863 if (nested >= 10)
864 return expr_copy;
865
866 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000867 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000868 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 vim_free(expr_copy);
870 return rv;
871}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000872
873/*
874 * Get the '=' register expression itself, without evaluating it.
875 */
876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100877get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000878{
879 if (expr_line == NULL)
880 return NULL;
881 return vim_strsave(expr_line);
882}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif /* FEAT_EVAL */
884
885/*
886 * Check if 'regname' is a valid name of a yank register.
887 * Note: There is no check for 0 (default register), caller should do this
888 */
889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100890valid_yank_reg(
891 int regname,
892 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 if ( (regname > 0 && ASCII_ISALNUM(regname))
895 || (!writing && vim_strchr((char_u *)
896#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100897 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100899 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900#endif
901 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100902 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 || regname == '"'
904 || regname == '-'
905 || regname == '_'
906#ifdef FEAT_CLIPBOARD
907 || regname == '*'
908 || regname == '+'
909#endif
910#ifdef FEAT_DND
911 || (!writing && regname == '~')
912#endif
913 )
914 return TRUE;
915 return FALSE;
916}
917
918/*
919 * Set y_current and y_append, according to the value of "regname".
920 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000921 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 *
923 * If regname is 0 and writing, use register 0
924 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100925 *
926 * Return TRUE when the register should be inserted literally (selection or
927 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100930get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931{
932 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100933 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 y_append = FALSE;
936 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
937 {
938 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100939 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 }
941 i = regname;
942 if (VIM_ISDIGIT(i))
943 i -= '0';
944 else if (ASCII_ISLOWER(i))
945 i = CharOrdLow(i) + 10;
946 else if (ASCII_ISUPPER(i))
947 {
948 i = CharOrdUp(i) + 10;
949 y_append = TRUE;
950 }
951 else if (regname == '-')
952 i = DELETION_REGISTER;
953#ifdef FEAT_CLIPBOARD
954 /* When selection is not available, use register 0 instead of '*' */
955 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100958 ret = TRUE;
959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 /* When clipboard is not available, use register 0 instead of '+' */
961 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100964 ret = TRUE;
965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#endif
967#ifdef FEAT_DND
968 else if (!writing && regname == '~')
969 i = TILDE_REGISTER;
970#endif
971 else /* not 0-9, a-z, A-Z or '-': use register 0 */
972 i = 0;
973 y_current = &(y_regs[i]);
974 if (writing) /* remember the register we write into for do_put() */
975 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100976 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
Bram Moolenaar8299df92004-07-10 09:47:34 +0000979#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * When "regname" is a clipboard register, obtain the selection. If it's not
982 * available return zero, otherwise return "regname".
983 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000984 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100985may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
987 if (regname == '*')
988 {
989 if (!clip_star.available)
990 regname = 0;
991 else
992 clip_get_selection(&clip_star);
993 }
994 else if (regname == '+')
995 {
996 if (!clip_plus.available)
997 regname = 0;
998 else
999 clip_get_selection(&clip_plus);
1000 }
1001 return regname;
1002}
1003#endif
1004
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005/*
1006 * Obtain the contents of a "normal" register. The register is made empty.
1007 * The returned pointer has allocated memory, use put_register() later.
1008 */
1009 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001010get_register(
1011 int name,
1012 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001014 yankreg_T *reg;
1015 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017#ifdef FEAT_CLIPBOARD
1018 /* When Visual area changed, may have to update selection. Obtain the
1019 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +00001020 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001022 if (clip_isautosel_star())
1023 clip_update_selection(&clip_star);
1024 may_get_selection(name);
1025 }
1026 if (name == '+' && clip_plus.available)
1027 {
1028 if (clip_isautosel_plus())
1029 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 may_get_selection(name);
1031 }
1032#endif
1033
1034 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001035 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (reg != NULL)
1037 {
1038 *reg = *y_current;
1039 if (copy)
1040 {
1041 /* If we run out of memory some or all of the lines are empty. */
1042 if (reg->y_size == 0)
1043 reg->y_array = NULL;
1044 else
1045 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1046 * reg->y_size));
1047 if (reg->y_array != NULL)
1048 {
1049 for (i = 0; i < reg->y_size; ++i)
1050 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1051 }
1052 }
1053 else
1054 y_current->y_array = NULL;
1055 }
1056 return (void *)reg;
1057}
1058
1059/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001060 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 */
1062 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001063put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
1065 get_yank_register(name, 0);
1066 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001067 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001068 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001070#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 /* Send text written to clipboard register to the clipboard. */
1072 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001075
1076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001077free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001078{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001079 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001080
1081 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001082 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001083 free_yank_all();
1084 vim_free(reg);
1085 *y_current = tmp;
1086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088#if defined(FEAT_MOUSE) || defined(PROTO)
1089/*
1090 * return TRUE if the current yank register has type MLINE
1091 */
1092 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1096 return FALSE;
1097 if (regname == '_') /* black hole is always empty */
1098 return FALSE;
1099 get_yank_register(regname, FALSE);
1100 return (y_current->y_type == MLINE);
1101}
1102#endif
1103
1104/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001105 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001107 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
1109 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001110do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
Bram Moolenaard55de222007-05-06 13:38:48 +00001112 char_u *p;
1113 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001114 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001115 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001117 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001119 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1121 retval = FAIL;
1122 else
1123 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001124 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 showmode();
1126 regname = c;
1127 retval = OK;
1128 }
1129 }
1130 else /* stop recording */
1131 {
1132 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001133 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1134 * needs to be removed again to put it in a register. exec_reg then
1135 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001137 reg_recording = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 MSG("");
1139 p = get_recorded();
1140 if (p == NULL)
1141 retval = FAIL;
1142 else
1143 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001144 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1145 vim_unescape_csi(p);
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * We don't want to change the default register here, so save and
1149 * restore the current register name.
1150 */
1151 old_y_previous = y_previous;
1152 old_y_current = y_current;
1153
1154 retval = stuff_yank(regname, p);
1155
1156 y_previous = old_y_previous;
1157 y_current = old_y_current;
1158 }
1159 }
1160 return retval;
1161}
1162
1163/*
1164 * Stuff string "p" into yank register "regname" as a single line (append if
1165 * uppercase). "p" must have been alloced.
1166 *
1167 * return FAIL for failure, OK otherwise
1168 */
1169 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001170stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 char_u *lp;
1173 char_u **pp;
1174
1175 /* check for read-only register */
1176 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1177 {
1178 vim_free(p);
1179 return FAIL;
1180 }
1181 if (regname == '_') /* black hole: don't do anything */
1182 {
1183 vim_free(p);
1184 return OK;
1185 }
1186 get_yank_register(regname, TRUE);
1187 if (y_append && y_current->y_array != NULL)
1188 {
1189 pp = &(y_current->y_array[y_current->y_size - 1]);
1190 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1191 if (lp == NULL)
1192 {
1193 vim_free(p);
1194 return FAIL;
1195 }
1196 STRCPY(lp, *pp);
1197 STRCAT(lp, p);
1198 vim_free(p);
1199 vim_free(*pp);
1200 *pp = lp;
1201 }
1202 else
1203 {
1204 free_yank_all();
1205 if ((y_current->y_array =
1206 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1207 {
1208 vim_free(p);
1209 return FAIL;
1210 }
1211 y_current->y_array[0] = p;
1212 y_current->y_size = 1;
1213 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001214#ifdef FEAT_VIMINFO
1215 y_current->y_time_set = vim_time();
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 return OK;
1219}
1220
Bram Moolenaar42b94362009-05-26 16:12:37 +00001221static int execreg_lastc = NUL;
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001224 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001226 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
1228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229do_execreg(
1230 int regname,
1231 int colon, /* insert ':' before each line */
1232 int addcr, /* always add '\n' to end of line */
1233 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 long i;
1236 char_u *p;
1237 int retval = OK;
1238 int remap;
1239
1240 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001241 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001242 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001243 {
1244 EMSG(_("E748: No previously used register"));
1245 return FAIL;
1246 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001247 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 /* check for valid regname */
1250 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001251 {
1252 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001254 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001255 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257#ifdef FEAT_CLIPBOARD
1258 regname = may_get_selection(regname);
1259#endif
1260
1261 if (regname == '_') /* black hole: don't stuff anything */
1262 return OK;
1263
1264#ifdef FEAT_CMDHIST
1265 if (regname == ':') /* use last command line */
1266 {
1267 if (last_cmdline == NULL)
1268 {
1269 EMSG(_(e_nolastcmd));
1270 return FAIL;
1271 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001272 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001273 /* Escape all control characters with a CTRL-V */
1274 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001275 (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 +00001276 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001277 {
1278 /* When in Visual mode "'<,'>" will be prepended to the command.
1279 * Remove it when it's already there. */
1280 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001281 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001282 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001284 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001285 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 }
1287#endif
1288#ifdef FEAT_EVAL
1289 else if (regname == '=')
1290 {
1291 p = get_expr_line();
1292 if (p == NULL)
1293 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001294 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 vim_free(p);
1296 }
1297#endif
1298 else if (regname == '.') /* use last inserted text */
1299 {
1300 p = get_last_insert_save();
1301 if (p == NULL)
1302 {
1303 EMSG(_(e_noinstext));
1304 return FAIL;
1305 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001306 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 vim_free(p);
1308 }
1309 else
1310 {
1311 get_yank_register(regname, FALSE);
1312 if (y_current->y_array == NULL)
1313 return FAIL;
1314
1315 /* Disallow remaping for ":@r". */
1316 remap = colon ? REMAP_NONE : REMAP_YES;
1317
1318 /*
1319 * Insert lines into typeahead buffer, from last one to first one.
1320 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001321 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 for (i = y_current->y_size; --i >= 0; )
1323 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001324 char_u *escaped;
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 /* insert NL between lines and after last line if type is MLINE */
1327 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1328 || addcr)
1329 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 return FAIL;
1332 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001333 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1334 if (escaped == NULL)
1335 return FAIL;
1336 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1337 vim_free(escaped);
1338 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001340 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 == FAIL)
1342 return FAIL;
1343 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001344 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 return retval;
1347}
1348
1349/*
1350 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1351 * used only after other typeahead has been processed.
1352 */
1353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001354put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356 char_u buf[3];
1357
1358 if (restart_edit != NUL)
1359 {
1360 if (restart_edit == 'V')
1361 {
1362 buf[0] = 'g';
1363 buf[1] = 'R';
1364 buf[2] = NUL;
1365 }
1366 else
1367 {
1368 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1369 buf[1] = NUL;
1370 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001371 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 restart_edit = NUL;
1373 }
1374}
1375
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001376/*
1377 * Insert register contents "s" into the typeahead buffer, so that it will be
1378 * executed again.
1379 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1380 * no remapping.
1381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001383put_in_typebuf(
1384 char_u *s,
1385 int esc,
1386 int colon, /* add ':' before the line */
1387 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 int retval = OK;
1390
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001391 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001393 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001395 {
1396 char_u *p;
1397
1398 if (esc)
1399 p = vim_strsave_escape_csi(s);
1400 else
1401 p = s;
1402 if (p == NULL)
1403 retval = FAIL;
1404 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001405 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1406 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001407 if (esc)
1408 vim_free(p);
1409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001411 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 return retval;
1413}
1414
1415/*
1416 * Insert a yank register: copy it into the Read buffer.
1417 * Used by CTRL-R command and middle mouse button in insert mode.
1418 *
1419 * return FAIL for failure, OK otherwise
1420 */
1421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001422insert_reg(
1423 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001424 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
1426 long i;
1427 int retval = OK;
1428 char_u *arg;
1429 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001430 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
1432 /*
1433 * It is possible to get into an endless loop by having CTRL-R a in
1434 * register a and then, in insert mode, doing CTRL-R a.
1435 * If you hit CTRL-C, the loop will be broken here.
1436 */
1437 ui_breakcheck();
1438 if (got_int)
1439 return FAIL;
1440
1441 /* check for valid regname */
1442 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1443 return FAIL;
1444
1445#ifdef FEAT_CLIPBOARD
1446 regname = may_get_selection(regname);
1447#endif
1448
1449 if (regname == '.') /* insert last inserted text */
1450 retval = stuff_inserted(NUL, 1L, TRUE);
1451 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1452 {
1453 if (arg == NULL)
1454 return FAIL;
1455 stuffescaped(arg, literally);
1456 if (allocated)
1457 vim_free(arg);
1458 }
1459 else /* name or number register */
1460 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001461 if (get_yank_register(regname, FALSE))
1462 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (y_current->y_array == NULL)
1464 retval = FAIL;
1465 else
1466 {
1467 for (i = 0; i < y_current->y_size; ++i)
1468 {
1469 stuffescaped(y_current->y_array[i], literally);
1470 /*
1471 * Insert a newline between lines and after last line if
1472 * y_type is MLINE.
1473 */
1474 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1475 stuffcharReadbuff('\n');
1476 }
1477 }
1478 }
1479
1480 return retval;
1481}
1482
1483/*
1484 * Stuff a string into the typeahead buffer, such that edit() will insert it
1485 * literally ("literally" TRUE) or interpret is as typed characters.
1486 */
1487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001488stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 int c;
1491 char_u *start;
1492
1493 while (*arg != NUL)
1494 {
1495 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1496 * stuff K_SPECIAL to get the effect of a special key when "literally"
1497 * is TRUE. */
1498 start = arg;
1499 while ((*arg >= ' '
1500#ifndef EBCDIC
1501 && *arg < DEL /* EBCDIC: chars above space are normal */
1502#endif
1503 )
1504 || (*arg == K_SPECIAL && !literally))
1505 ++arg;
1506 if (arg > start)
1507 stuffReadbuffLen(start, (long)(arg - start));
1508
1509 /* stuff a single special character */
1510 if (*arg != NUL)
1511 {
1512#ifdef FEAT_MBYTE
1513 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001514 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 else
1516#endif
1517 c = *arg++;
1518 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1519 stuffcharReadbuff(Ctrl_V);
1520 stuffcharReadbuff(c);
1521 }
1522 }
1523}
1524
1525/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001526 * If "regname" is a special register, return TRUE and store a pointer to its
1527 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001529 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001530get_spec_reg(
1531 int regname,
1532 char_u **argp,
1533 int *allocated, /* return: TRUE when value was allocated */
1534 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
1536 int cnt;
1537
1538 *argp = NULL;
1539 *allocated = FALSE;
1540 switch (regname)
1541 {
1542 case '%': /* file name */
1543 if (errmsg)
1544 check_fname(); /* will give emsg if not set */
1545 *argp = curbuf->b_fname;
1546 return TRUE;
1547
1548 case '#': /* alternate file name */
1549 *argp = getaltfname(errmsg); /* may give emsg if not set */
1550 return TRUE;
1551
1552#ifdef FEAT_EVAL
1553 case '=': /* result of expression */
1554 *argp = get_expr_line();
1555 *allocated = TRUE;
1556 return TRUE;
1557#endif
1558
1559 case ':': /* last command line */
1560 if (last_cmdline == NULL && errmsg)
1561 EMSG(_(e_nolastcmd));
1562 *argp = last_cmdline;
1563 return TRUE;
1564
1565 case '/': /* last search-pattern */
1566 if (last_search_pat() == NULL && errmsg)
1567 EMSG(_(e_noprevre));
1568 *argp = last_search_pat();
1569 return TRUE;
1570
1571 case '.': /* last inserted text */
1572 *argp = get_last_insert_save();
1573 *allocated = TRUE;
1574 if (*argp == NULL && errmsg)
1575 EMSG(_(e_noinstext));
1576 return TRUE;
1577
1578#ifdef FEAT_SEARCHPATH
1579 case Ctrl_F: /* Filename under cursor */
1580 case Ctrl_P: /* Path under cursor, expand via "path" */
1581 if (!errmsg)
1582 return FALSE;
1583 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001584 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 *allocated = TRUE;
1586 return TRUE;
1587#endif
1588
1589 case Ctrl_W: /* word under cursor */
1590 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1591 if (!errmsg)
1592 return FALSE;
1593 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1594 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1595 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1596 *allocated = TRUE;
1597 return TRUE;
1598
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001599 case Ctrl_L: /* Line under cursor */
1600 if (!errmsg)
1601 return FALSE;
1602
1603 *argp = ml_get_buf(curwin->w_buffer,
1604 curwin->w_cursor.lnum, FALSE);
1605 return TRUE;
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 case '_': /* black hole: always empty */
1608 *argp = (char_u *)"";
1609 return TRUE;
1610 }
1611
1612 return FALSE;
1613}
1614
1615/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001616 * Paste a yank register into the command line.
1617 * Only for non-special registers.
1618 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 * insert_reg() can't be used here, because special characters from the
1620 * register contents will be interpreted as commands.
1621 *
1622 * return FAIL for failure, OK otherwise
1623 */
1624 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001625cmdline_paste_reg(
1626 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001627 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001628 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001631 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001633 if (get_yank_register(regname, FALSE))
1634 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (y_current->y_array == NULL)
1636 return FAIL;
1637
1638 for (i = 0; i < y_current->y_size; ++i)
1639 {
1640 cmdline_paste_str(y_current->y_array[i], literally);
1641
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001642 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001643 * Don't do this when "remcr" is TRUE. */
1644 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 cmdline_paste_str((char_u *)"\r", literally);
1646
1647 /* Check for CTRL-C, in case someone tries to paste a few thousand
1648 * lines and gets bored. */
1649 ui_breakcheck();
1650 if (got_int)
1651 return FAIL;
1652 }
1653 return OK;
1654}
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1657/*
1658 * Adjust the register name pointed to with "rp" for the clipboard being
1659 * used always and the clipboard being available.
1660 */
1661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001662adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001664 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1665 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001666 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1667 {
1668 if (clip_unnamed != 0)
1669 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001670 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001671 else
1672 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1673 ? '+' : '*';
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (!clip_star.available && *rp == '*')
1676 *rp = 0;
1677 if (!clip_plus.available && *rp == '+')
1678 *rp = 0;
1679}
1680#endif
1681
1682/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001683 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1684 */
1685 void
1686shift_delete_registers()
1687{
1688 int n;
1689
1690 y_current = &y_regs[9];
1691 free_yank_all(); /* free register nine */
1692 for (n = 9; n > 1; --n)
1693 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001694 y_current = &y_regs[1];
1695 if (!y_append)
1696 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001697 y_regs[1].y_array = NULL; /* set register one to empty */
1698}
1699
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001700#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001701 static void
1702yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1703{
1704 static int recursive = FALSE;
1705 dict_T *v_event;
1706 list_T *list;
1707 int n;
1708 char_u buf[NUMBUFLEN + 2];
1709 long reglen = 0;
1710
1711 if (recursive)
1712 return;
1713
1714 v_event = get_vim_var_dict(VV_EVENT);
1715
1716 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001717 if (list == NULL)
1718 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001719 for (n = 0; n < reg->y_size; n++)
1720 list_append_string(list, reg->y_array[n], -1);
1721 list->lv_lock = VAR_FIXED;
1722 dict_add_list(v_event, "regcontents", list);
1723
1724 buf[0] = (char_u)oap->regname;
1725 buf[1] = NUL;
1726 dict_add_nr_str(v_event, "regname", 0, buf);
1727
1728 buf[0] = get_op_char(oap->op_type);
1729 buf[1] = get_extra_op_char(oap->op_type);
1730 buf[2] = NUL;
1731 dict_add_nr_str(v_event, "operator", 0, buf);
1732
1733 buf[0] = NUL;
1734 buf[1] = NUL;
1735 switch (get_reg_type(oap->regname, &reglen))
1736 {
1737 case MLINE: buf[0] = 'V'; break;
1738 case MCHAR: buf[0] = 'v'; break;
1739 case MBLOCK:
1740 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1741 reglen + 1);
1742 break;
1743 }
1744 dict_add_nr_str(v_event, "regtype", 0, buf);
1745
1746 /* Lock the dictionary and its keys */
1747 dict_set_items_ro(v_event);
1748
1749 recursive = TRUE;
1750 textlock++;
1751 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1752 textlock--;
1753 recursive = FALSE;
1754
1755 /* Empty the dictionary, v:event is still valid */
1756 dict_free_contents(v_event);
1757 hash_init(&v_event->dv_hashtab);
1758}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001759#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001760
Bram Moolenaara1891842017-02-04 21:34:31 +01001761/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001762 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001764 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 */
1766 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001767op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 int n;
1770 linenr_T lnum;
1771 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 char_u *newp, *oldp;
1773 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1775 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001776 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1779 return OK;
1780
1781 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1782 if (oap->empty)
1783 return u_save_cursor();
1784
1785 if (!curbuf->b_p_ma)
1786 {
1787 EMSG(_(e_modifiable));
1788 return FAIL;
1789 }
1790
1791#ifdef FEAT_CLIPBOARD
1792 adjust_clip_reg(&oap->regname);
1793#endif
1794
1795#ifdef FEAT_MBYTE
1796 if (has_mbyte)
1797 mb_adjust_opend(oap);
1798#endif
1799
Bram Moolenaard04b7502010-07-08 22:27:55 +02001800 /*
1801 * Imitate the strange Vi behaviour: If the delete spans more than one
1802 * line and motion_type == MCHAR and the result is a blank line, make the
1803 * delete linewise. Don't do this for the change command or Visual mode.
1804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001807 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001809 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 && oap->op_type == OP_DELETE)
1811 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001812 ptr = ml_get(oap->end.lnum) + oap->end.col;
1813 if (*ptr != NUL)
1814 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 ptr = skipwhite(ptr);
1816 if (*ptr == NUL && inindent(0))
1817 oap->motion_type = MLINE;
1818 }
1819
Bram Moolenaard04b7502010-07-08 22:27:55 +02001820 /*
1821 * Check for trying to delete (e.g. "D") in an empty line.
1822 * Note: For the change operator it is ok.
1823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if ( oap->motion_type == MCHAR
1825 && oap->line_count == 1
1826 && oap->op_type == OP_DELETE
1827 && *ml_get(oap->start.lnum) == NUL)
1828 {
1829 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001830 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 * 'cpoptions' (Vi compatible).
1832 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001833#ifdef FEAT_VIRTUALEDIT
1834 if (virtual_op)
1835 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1836 * marks as if it happened. */
1837 goto setmarks;
1838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1840 beep_flush();
1841 return OK;
1842 }
1843
Bram Moolenaard04b7502010-07-08 22:27:55 +02001844 /*
1845 * Do a yank of whatever we're about to delete.
1846 * If a yank register was specified, put the deleted text into that
1847 * register. For the black hole register '_' don't yank anything.
1848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (oap->regname != '_')
1850 {
1851 if (oap->regname != 0)
1852 {
1853 /* check for read-only register */
1854 if (!valid_yank_reg(oap->regname, TRUE))
1855 {
1856 beep_flush();
1857 return OK;
1858 }
1859 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1860 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1861 did_yank = TRUE;
1862 }
1863
1864 /*
1865 * Put deleted text into register 1 and shift number registers if the
1866 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001867 * Use the register name from before adjust_clip_reg() may have
1868 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001870 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 || oap->line_count > 1 || oap->use_reg_one)
1872 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001873 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (op_yank(oap, TRUE, FALSE) == OK)
1875 did_yank = TRUE;
1876 }
1877
Bram Moolenaar84298db2012-04-20 13:46:08 +02001878 /* Yank into small delete register when no named register specified
1879 * and the delete is within one line. */
1880 if ((
1881#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001882 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1883 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001884#endif
1885 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 && oap->line_count == 1)
1887 {
1888 oap->regname = '-';
1889 get_yank_register(oap->regname, TRUE);
1890 if (op_yank(oap, TRUE, FALSE) == OK)
1891 did_yank = TRUE;
1892 oap->regname = 0;
1893 }
1894
1895 /*
1896 * If there's too much stuff to fit in the yank register, then get a
1897 * confirmation before doing the delete. This is crude, but simple.
1898 * And it avoids doing a delete of something we can't put back if we
1899 * want.
1900 */
1901 if (!did_yank)
1902 {
1903 int msg_silent_save = msg_silent;
1904
1905 msg_silent = 0; /* must display the prompt */
1906 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1907 msg_silent = msg_silent_save;
1908 if (n != 'y')
1909 {
1910 EMSG(_(e_abort));
1911 return FAIL;
1912 }
1913 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001914
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001915#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001916 if (did_yank && has_textyankpost())
1917 yank_do_autocmd(oap, y_current);
1918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920
Bram Moolenaard04b7502010-07-08 22:27:55 +02001921 /*
1922 * block mode delete
1923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (oap->block_mode)
1925 {
1926 if (u_save((linenr_T)(oap->start.lnum - 1),
1927 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1928 return FAIL;
1929
1930 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1931 {
1932 block_prep(oap, &bd, lnum, TRUE);
1933 if (bd.textlen == 0) /* nothing to delete */
1934 continue;
1935
1936 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1937 if (lnum == curwin->w_cursor.lnum)
1938 {
1939 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1940# ifdef FEAT_VIRTUALEDIT
1941 curwin->w_cursor.coladd = 0;
1942# endif
1943 }
1944
1945 /* n == number of chars deleted
1946 * If we delete a TAB, it may be replaced by several characters.
1947 * Thus the number of characters may increase!
1948 */
1949 n = bd.textlen - bd.startspaces - bd.endspaces;
1950 oldp = ml_get(lnum);
1951 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1952 if (newp == NULL)
1953 continue;
1954 /* copy up to deleted part */
1955 mch_memmove(newp, oldp, (size_t)bd.textcol);
1956 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001957 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 (size_t)(bd.startspaces + bd.endspaces));
1959 /* copy the part after the deleted part */
1960 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001961 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* replace the line */
1963 ml_replace(lnum, newp, FALSE);
1964 }
1965
1966 check_cursor_col();
1967 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1968 oap->end.lnum + 1, 0L);
1969 oap->line_count = 0; /* no lines deleted */
1970 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001971 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 if (oap->op_type == OP_CHANGE)
1974 {
1975 /* Delete the lines except the first one. Temporarily move the
1976 * cursor to the next line. Save the current line number, if the
1977 * last line is deleted it may be changed.
1978 */
1979 if (oap->line_count > 1)
1980 {
1981 lnum = curwin->w_cursor.lnum;
1982 ++curwin->w_cursor.lnum;
1983 del_lines((long)(oap->line_count - 1), TRUE);
1984 curwin->w_cursor.lnum = lnum;
1985 }
1986 if (u_save_cursor() == FAIL)
1987 return FAIL;
1988 if (curbuf->b_p_ai) /* don't delete indent */
1989 {
1990 beginline(BL_WHITE); /* cursor on first non-white */
1991 did_ai = TRUE; /* delete the indent when ESC hit */
1992 ai_col = curwin->w_cursor.col;
1993 }
1994 else
1995 beginline(0); /* cursor in column 0 */
1996 truncate_line(FALSE); /* delete the rest of the line */
1997 /* leave cursor past last char in line */
1998 if (oap->line_count > 1)
1999 u_clearline(); /* "U" command not possible after "2cc" */
2000 }
2001 else
2002 {
2003 del_lines(oap->line_count, TRUE);
2004 beginline(BL_WHITE | BL_FIX);
2005 u_clearline(); /* "U" command not possible after "dd" */
2006 }
2007 }
2008 else
2009 {
2010#ifdef FEAT_VIRTUALEDIT
2011 if (virtual_op)
2012 {
2013 int endcol = 0;
2014
2015 /* For virtualedit: break the tabs that are partly included. */
2016 if (gchar_pos(&oap->start) == '\t')
2017 {
2018 if (u_save_cursor() == FAIL) /* save first line for undo */
2019 return FAIL;
2020 if (oap->line_count == 1)
2021 endcol = getviscol2(oap->end.col, oap->end.coladd);
2022 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2023 oap->start = curwin->w_cursor;
2024 if (oap->line_count == 1)
2025 {
2026 coladvance(endcol);
2027 oap->end.col = curwin->w_cursor.col;
2028 oap->end.coladd = curwin->w_cursor.coladd;
2029 curwin->w_cursor = oap->start;
2030 }
2031 }
2032
2033 /* Break a tab only when it's included in the area. */
2034 if (gchar_pos(&oap->end) == '\t'
2035 && (int)oap->end.coladd < oap->inclusive)
2036 {
2037 /* save last line for undo */
2038 if (u_save((linenr_T)(oap->end.lnum - 1),
2039 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2040 return FAIL;
2041 curwin->w_cursor = oap->end;
2042 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2043 oap->end = curwin->w_cursor;
2044 curwin->w_cursor = oap->start;
2045 }
2046 }
2047#endif
2048
2049 if (oap->line_count == 1) /* delete characters within one line */
2050 {
2051 if (u_save_cursor() == FAIL) /* save line for undo */
2052 return FAIL;
2053
2054 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002055 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 && oap->op_type == OP_CHANGE
2057 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002058 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 display_dollar(oap->end.col - !oap->inclusive);
2060
2061 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2062
2063#ifdef FEAT_VIRTUALEDIT
2064 if (virtual_op)
2065 {
2066 /* fix up things for virtualedit-delete:
2067 * break the tabs which are going to get in our way
2068 */
2069 char_u *curline = ml_get_curline();
2070 int len = (int)STRLEN(curline);
2071
2072 if (oap->end.coladd != 0
2073 && (int)oap->end.col >= len - 1
2074 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2075 n++;
2076 /* Delete at least one char (e.g, when on a control char). */
2077 if (n == 0 && oap->start.coladd != oap->end.coladd)
2078 n = 1;
2079
2080 /* When deleted a char in the line, reset coladd. */
2081 if (gchar_cursor() != NUL)
2082 curwin->w_cursor.coladd = 0;
2083 }
2084#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002085 (void)del_bytes((long)n, !virtual_op,
2086 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 else /* delete characters between lines */
2089 {
2090 pos_T curpos;
2091
2092 /* save deleted and changed lines for undo */
2093 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2094 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2095 return FAIL;
2096
2097 truncate_line(TRUE); /* delete from cursor to end of line */
2098
2099 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2100 ++curwin->w_cursor.lnum;
2101 del_lines((long)(oap->line_count - 2), FALSE);
2102
Bram Moolenaard009e862015-06-09 20:20:03 +02002103 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002104 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002105 curwin->w_cursor.col = 0;
2106 (void)del_bytes((long)n, !virtual_op,
2107 oap->op_type == OP_DELETE && !oap->is_VIsual);
2108 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2109 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111 }
2112
2113 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2114
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002115#ifdef FEAT_VIRTUALEDIT
2116setmarks:
2117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (oap->block_mode)
2119 {
2120 curbuf->b_op_end.lnum = oap->end.lnum;
2121 curbuf->b_op_end.col = oap->start.col;
2122 }
2123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 curbuf->b_op_end = oap->start;
2125 curbuf->b_op_start = oap->start;
2126
2127 return OK;
2128}
2129
2130#ifdef FEAT_MBYTE
2131/*
2132 * Adjust end of operating area for ending on a multi-byte character.
2133 * Used for deletion.
2134 */
2135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 char_u *p;
2139
2140 if (oap->inclusive)
2141 {
2142 p = ml_get(oap->end.lnum);
2143 oap->end.col += mb_tail_off(p, p + oap->end.col);
2144 }
2145}
2146#endif
2147
2148#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
Bram Moolenaar630afe82018-06-28 19:26:28 +02002149
2150# ifdef FEAT_MBYTE
2151/*
2152 * Replace the character under the cursor with "c".
2153 * This takes care of multi-byte characters.
2154 */
2155 static void
2156replace_character(int c)
2157{
2158 int n = State;
2159
2160 State = REPLACE;
2161 ins_char(c);
2162 State = n;
2163 /* Backup to the replaced character. */
2164 dec_cursor();
2165}
2166
2167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168/*
2169 * Replace a whole area with one character.
2170 */
2171 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002172op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 int n, numc;
2175#ifdef FEAT_MBYTE
2176 int num_chars;
2177#endif
2178 char_u *newp, *oldp;
2179 size_t oldlen;
2180 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002181 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002182 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2185 return OK; /* nothing to do */
2186
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002187 if (c == REPLACE_CR_NCHAR)
2188 {
2189 had_ctrl_v_cr = TRUE;
2190 c = CAR;
2191 }
2192 else if (c == REPLACE_NL_NCHAR)
2193 {
2194 had_ctrl_v_cr = TRUE;
2195 c = NL;
2196 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_MBYTE
2199 if (has_mbyte)
2200 mb_adjust_opend(oap);
2201#endif
2202
2203 if (u_save((linenr_T)(oap->start.lnum - 1),
2204 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2205 return FAIL;
2206
2207 /*
2208 * block mode replace
2209 */
2210 if (oap->block_mode)
2211 {
2212 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2213 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2214 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002215 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2217 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2218 continue; /* nothing to replace */
2219
2220 /* n == number of extra chars required
2221 * If we split a TAB, it may be replaced by several characters.
2222 * Thus the number of characters may increase!
2223 */
2224#ifdef FEAT_VIRTUALEDIT
2225 /* If the range starts in virtual space, count the initial
2226 * coladd offset as part of "startspaces" */
2227 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2228 {
2229 pos_T vpos;
2230
Bram Moolenaara1381de2009-11-03 15:44:21 +00002231 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 getvpos(&vpos, oap->start_vcol);
2233 bd.startspaces += vpos.coladd;
2234 n = bd.startspaces;
2235 }
2236 else
2237#endif
2238 /* allow for pre spaces */
2239 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2240
2241 /* allow for post spp */
2242 n += (bd.endspaces
2243#ifdef FEAT_VIRTUALEDIT
2244 && !bd.is_oneChar
2245#endif
2246 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2247 /* Figure out how many characters to replace. */
2248 numc = oap->end_vcol - oap->start_vcol + 1;
2249 if (bd.is_short && (!virtual_op || bd.is_MAX))
2250 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2251
2252#ifdef FEAT_MBYTE
2253 /* A double-wide character can be replaced only up to half the
2254 * times. */
2255 if ((*mb_char2cells)(c) > 1)
2256 {
2257 if ((numc & 1) && !bd.is_short)
2258 {
2259 ++bd.endspaces;
2260 ++n;
2261 }
2262 numc = numc / 2;
2263 }
2264
2265 /* Compute bytes needed, move character count to num_chars. */
2266 num_chars = numc;
2267 numc *= (*mb_char2len)(c);
2268#endif
2269 /* oldlen includes textlen, so don't double count */
2270 n += numc - bd.textlen;
2271
2272 oldp = ml_get_curline();
2273 oldlen = STRLEN(oldp);
2274 newp = alloc_check((unsigned)oldlen + 1 + n);
2275 if (newp == NULL)
2276 continue;
2277 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2278 /* copy up to deleted part */
2279 mch_memmove(newp, oldp, (size_t)bd.textcol);
2280 oldp += bd.textcol + bd.textlen;
2281 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002282 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002284 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2285 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002286 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002288#ifdef FEAT_MBYTE
2289 if (has_mbyte)
2290 {
2291 n = (int)STRLEN(newp);
2292 while (--num_chars >= 0)
2293 n += (*mb_char2bytes)(c, newp + n);
2294 }
2295 else
2296#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002297 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002298 if (!bd.is_short)
2299 {
2300 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002301 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002302 /* copy the part after the changed part */
2303 STRMOVE(newp + STRLEN(newp), oldp);
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002308 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002309 after_p = alloc_check(
2310 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002311 if (after_p != NULL)
2312 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314 /* replace the line */
2315 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002316 if (after_p != NULL)
2317 {
2318 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2319 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2320 oap->end.lnum++;
2321 vim_free(after_p);
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324 }
2325 else
2326 {
2327 /*
2328 * MCHAR and MLINE motion replace.
2329 */
2330 if (oap->motion_type == MLINE)
2331 {
2332 oap->start.col = 0;
2333 curwin->w_cursor.col = 0;
2334 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2335 if (oap->end.col)
2336 --oap->end.col;
2337 }
2338 else if (!oap->inclusive)
2339 dec(&(oap->end));
2340
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002341 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 n = gchar_cursor();
2344 if (n != NUL)
2345 {
2346#ifdef FEAT_MBYTE
2347 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2348 {
2349 /* This is slow, but it handles replacing a single-byte
2350 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002351 if (curwin->w_cursor.lnum == oap->end.lnum)
2352 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002353 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355 else
2356#endif
2357 {
2358#ifdef FEAT_VIRTUALEDIT
2359 if (n == TAB)
2360 {
2361 int end_vcol = 0;
2362
2363 if (curwin->w_cursor.lnum == oap->end.lnum)
2364 {
2365 /* oap->end has to be recalculated when
2366 * the tab breaks */
2367 end_vcol = getviscol2(oap->end.col,
2368 oap->end.coladd);
2369 }
2370 coladvance_force(getviscol());
2371 if (curwin->w_cursor.lnum == oap->end.lnum)
2372 getvpos(&oap->end, end_vcol);
2373 }
2374#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002375 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 }
2378#ifdef FEAT_VIRTUALEDIT
2379 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2380 {
2381 int virtcols = oap->end.coladd;
2382
2383 if (curwin->w_cursor.lnum == oap->start.lnum
2384 && oap->start.col == oap->end.col && oap->start.coladd)
2385 virtcols -= oap->start.coladd;
2386
2387 /* oap->end has been trimmed so it's effectively inclusive;
2388 * as a result an extra +1 must be counted so we don't
2389 * trample the NUL byte. */
2390 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2391 curwin->w_cursor.col -= (virtcols + 1);
2392 for (; virtcols >= 0; virtcols--)
2393 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002394#ifdef FEAT_MBYTE
2395 if ((*mb_char2len)(c) > 1)
2396 replace_character(c);
2397 else
2398 #endif
2399 PBYTE(curwin->w_cursor, c);
2400 if (inc(&curwin->w_cursor) == -1)
2401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 }
2404#endif
2405
2406 /* Advance to next character, stop at the end of the file. */
2407 if (inc_cursor() == -1)
2408 break;
2409 }
2410 }
2411
2412 curwin->w_cursor = oap->start;
2413 check_cursor();
2414 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2415
2416 /* Set "'[" and "']" marks. */
2417 curbuf->b_op_start = oap->start;
2418 curbuf->b_op_end = oap->end;
2419
2420 return OK;
2421}
2422#endif
2423
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002424static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426/*
2427 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2428 */
2429 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002430op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002434 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 if (u_save((linenr_T)(oap->start.lnum - 1),
2437 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2438 return;
2439
2440 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (oap->block_mode) /* Visual block mode */
2442 {
2443 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2444 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002445 int one_change;
2446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 block_prep(oap, &bd, pos.lnum, FALSE);
2448 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002449 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2450 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002451
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002452#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002453 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2456
Bram Moolenaar009b2592004-10-24 19:18:58 +00002457 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2458 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002460 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464 if (did_change)
2465 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2466 }
2467 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 if (oap->motion_type == MLINE)
2470 {
2471 oap->start.col = 0;
2472 pos.col = 0;
2473 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2474 if (oap->end.col)
2475 --oap->end.col;
2476 }
2477 else if (!oap->inclusive)
2478 dec(&(oap->end));
2479
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002480 if (pos.lnum == oap->end.lnum)
2481 did_change = swapchars(oap->op_type, &pos,
2482 oap->end.col - pos.col + 1);
2483 else
2484 for (;;)
2485 {
2486 did_change |= swapchars(oap->op_type, &pos,
2487 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2488 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002489 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002490 break;
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (did_change)
2493 {
2494 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2495 0L);
2496#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002497 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 char_u *ptr;
2500 int count;
2501
2502 pos = oap->start;
2503 while (pos.lnum < oap->end.lnum)
2504 {
2505 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002506 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002507 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002509 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 pos.col = 0;
2511 pos.lnum++;
2512 }
2513 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2514 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002515 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002517 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519#endif
2520 }
2521 }
2522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (!did_change && oap->is_VIsual)
2524 /* No change: need to remove the Visual selection */
2525 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
2527 /*
2528 * Set '[ and '] marks.
2529 */
2530 curbuf->b_op_start = oap->start;
2531 curbuf->b_op_end = oap->end;
2532
2533 if (oap->line_count > p_report)
2534 {
2535 if (oap->line_count == 1)
2536 MSG(_("1 line changed"));
2537 else
2538 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2539 }
2540}
2541
2542/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002543 * Invoke swapchar() on "length" bytes at position "pos".
2544 * "pos" is advanced to just after the changed characters.
2545 * "length" is rounded up to include the whole last multi-byte character.
2546 * Also works correctly when the number of bytes changes.
2547 * Returns TRUE if some character was changed.
2548 */
2549 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002550swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002551{
2552 int todo;
2553 int did_change = 0;
2554
2555 for (todo = length; todo > 0; --todo)
2556 {
2557# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002558 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002559 {
2560 int len = (*mb_ptr2len)(ml_get_pos(pos));
2561
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002562 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002563 if (len > 0)
2564 todo -= len - 1;
2565 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002566# endif
2567 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002568 if (inc(pos) == -1) /* at end of file */
2569 break;
2570 }
2571 return did_change;
2572}
2573
2574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 * If op_type == OP_UPPER: make uppercase,
2576 * if op_type == OP_LOWER: make lowercase,
2577 * if op_type == OP_ROT13: do rot13 encoding,
2578 * else swap case of character at 'pos'
2579 * returns TRUE when something actually changed.
2580 */
2581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002582swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 int c;
2585 int nc;
2586
2587 c = gchar_pos(pos);
2588
2589 /* Only do rot13 encoding for ASCII characters. */
2590 if (c >= 0x80 && op_type == OP_ROT13)
2591 return FALSE;
2592
2593#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002594 if (op_type == OP_UPPER && c == 0xdf
2595 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002596 {
2597 pos_T sp = curwin->w_cursor;
2598
2599 /* Special handling of German sharp s: change to "SS". */
2600 curwin->w_cursor = *pos;
2601 del_char(FALSE);
2602 ins_char('S');
2603 ins_char('S');
2604 curwin->w_cursor = sp;
2605 inc(pos);
2606 }
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2609 return FALSE;
2610#endif
2611 nc = c;
2612 if (MB_ISLOWER(c))
2613 {
2614 if (op_type == OP_ROT13)
2615 nc = ROT13(c, 'a');
2616 else if (op_type != OP_LOWER)
2617 nc = MB_TOUPPER(c);
2618 }
2619 else if (MB_ISUPPER(c))
2620 {
2621 if (op_type == OP_ROT13)
2622 nc = ROT13(c, 'A');
2623 else if (op_type != OP_UPPER)
2624 nc = MB_TOLOWER(c);
2625 }
2626 if (nc != c)
2627 {
2628#ifdef FEAT_MBYTE
2629 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2630 {
2631 pos_T sp = curwin->w_cursor;
2632
2633 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002634 /* don't use del_char(), it also removes composing chars */
2635 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ins_char(nc);
2637 curwin->w_cursor = sp;
2638 }
2639 else
2640#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002641 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return TRUE;
2643 }
2644 return FALSE;
2645}
2646
2647#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2648/*
2649 * op_insert - Insert and append operators for Visual mode.
2650 */
2651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002652op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653{
2654 long ins_len, pre_textlen = 0;
2655 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002656 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 struct block_def bd;
2658 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002659 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /* edit() changes this - record it for OP_APPEND */
2662 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2663
2664 /* vis block is still marked. Get rid of it now. */
2665 curwin->w_cursor.lnum = oap->start.lnum;
2666 update_screen(INVERTED);
2667
2668 if (oap->block_mode)
2669 {
2670#ifdef FEAT_VIRTUALEDIT
2671 /* When 'virtualedit' is used, need to insert the extra spaces before
2672 * doing block_prep(). When only "block" is used, virtual edit is
2673 * already disabled, but still need it when calling
2674 * coladvance_force(). */
2675 if (curwin->w_cursor.coladd > 0)
2676 {
2677 int old_ve_flags = ve_flags;
2678
2679 ve_flags = VE_ALL;
2680 if (u_save_cursor() == FAIL)
2681 return;
2682 coladvance_force(oap->op_type == OP_APPEND
2683 ? oap->end_vcol + 1 : getviscol());
2684 if (oap->op_type == OP_APPEND)
2685 --curwin->w_cursor.col;
2686 ve_flags = old_ve_flags;
2687 }
2688#endif
2689 /* Get the info about the block before entering the text */
2690 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002691 /* Get indent information */
2692 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if (oap->op_type == OP_APPEND)
2696 firstline += bd.textlen;
2697 pre_textlen = (long)STRLEN(firstline);
2698 }
2699
2700 if (oap->op_type == OP_APPEND)
2701 {
2702 if (oap->block_mode
2703#ifdef FEAT_VIRTUALEDIT
2704 && curwin->w_cursor.coladd == 0
2705#endif
2706 )
2707 {
2708 /* Move the cursor to the character right of the block. */
2709 curwin->w_set_curswant = TRUE;
2710 while (*ml_get_cursor() != NUL
2711 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2712 ++curwin->w_cursor.col;
2713 if (bd.is_short && !bd.is_MAX)
2714 {
2715 /* First line was too short, make it longer and adjust the
2716 * values in "bd". */
2717 if (u_save_cursor() == FAIL)
2718 return;
2719 for (i = 0; i < bd.endspaces; ++i)
2720 ins_char(' ');
2721 bd.textlen += bd.endspaces;
2722 }
2723 }
2724 else
2725 {
2726 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002727 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002730 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 && oap->start_vcol != oap->end_vcol)
2732 inc_cursor();
2733 }
2734 }
2735
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002736 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002737 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002739 /* When a tab was inserted, and the characters in front of the tab
2740 * have been converted to a tab as well, the column of the cursor
2741 * might have actually been reduced, so need to adjust here. */
2742 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002743 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002744 oap->start = curbuf->b_op_start_orig;
2745
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002746 /* If user has moved off this line, we don't know what to do, so do
2747 * nothing.
2748 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2749 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return;
2751
2752 if (oap->block_mode)
2753 {
2754 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002755 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002756 size_t len;
2757 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002759 /* If indent kicked in, the firstline might have changed
2760 * but only do that, if the indent actually increased. */
2761 ind_post = (colnr_T)getwhitecols_curline();
2762 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2763 {
2764 bd.textcol += ind_post - ind_pre;
2765 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002766 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002767 }
2768
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002769 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002770 * to adjust the block for that. But only do it, if the difference
2771 * does not come from indent kicking in. */
2772 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2773 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002774 {
2775 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002776 && oap->start.col
2777#ifdef FEAT_VIRTUALEDIT
2778 + oap->start.coladd
2779#endif
2780 != curbuf->b_op_start_orig.col
2781#ifdef FEAT_VIRTUALEDIT
2782 + curbuf->b_op_start_orig.coladd
2783#endif
2784 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002785 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002786 int t = getviscol2(curbuf->b_op_start_orig.col,
2787 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002788 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002789 pre_textlen -= t - oap->start_vcol;
2790 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002791 }
2792 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002793 && oap->end.col
2794#ifdef FEAT_VIRTUALEDIT
2795 + oap->end.coladd
2796#endif
2797 >= curbuf->b_op_start_orig.col
2798#ifdef FEAT_VIRTUALEDIT
2799 + curbuf->b_op_start_orig.coladd
2800#endif
2801 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002802 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002803 int t = getviscol2(curbuf->b_op_start_orig.col,
2804 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002805 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002806 /* reset pre_textlen to the value of OP_INSERT */
2807 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002808 pre_textlen -= t - oap->start_vcol;
2809 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002810 oap->op_type = OP_INSERT;
2811 }
2812 }
2813
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 /*
2815 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002816 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 * Don't do this when "$" used, end-of-line will have changed.
2818 */
2819 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2820 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2821 {
2822 if (oap->op_type == OP_APPEND)
2823 {
2824 pre_textlen += bd2.textlen - bd.textlen;
2825 if (bd2.endspaces)
2826 --bd2.textlen;
2827 }
2828 bd.textcol = bd2.textcol;
2829 bd.textlen = bd2.textlen;
2830 }
2831
2832 /*
2833 * Subsequent calls to ml_get() flush the firstline data - take a
2834 * copy of the required string.
2835 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002836 firstline = ml_get(oap->start.lnum);
2837 len = STRLEN(firstline);
2838 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002840 add += bd.textlen;
2841 if ((size_t)add > len)
2842 firstline += len; // short line, point to the NUL
2843 else
2844 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002845 if (pre_textlen >= 0
2846 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 ins_text = vim_strnsave(firstline, (int)ins_len);
2849 if (ins_text != NULL)
2850 {
2851 /* block handled here */
2852 if (u_save(oap->start.lnum,
2853 (linenr_T)(oap->end.lnum + 1)) == OK)
2854 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2855 &bd);
2856
2857 curwin->w_cursor.col = oap->start.col;
2858 check_cursor();
2859 vim_free(ins_text);
2860 }
2861 }
2862 }
2863}
2864#endif
2865
2866/*
2867 * op_change - handle a change operation
2868 *
2869 * return TRUE if edit() returns because of a CTRL-O command
2870 */
2871 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002872op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 colnr_T l;
2875 int retval;
2876#ifdef FEAT_VISUALEXTRA
2877 long offset;
2878 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002879 long ins_len;
2880 long pre_textlen = 0;
2881 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 char_u *firstline;
2883 char_u *ins_text, *newp, *oldp;
2884 struct block_def bd;
2885#endif
2886
2887 l = oap->start.col;
2888 if (oap->motion_type == MLINE)
2889 {
2890 l = 0;
2891#ifdef FEAT_SMARTINDENT
2892 if (!p_paste && curbuf->b_p_si
2893# ifdef FEAT_CINDENT
2894 && !curbuf->b_p_cin
2895# endif
2896 )
2897 can_si = TRUE; /* It's like opening a new line, do si */
2898#endif
2899 }
2900
2901 /* First delete the text in the region. In an empty buffer only need to
2902 * save for undo */
2903 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2904 {
2905 if (u_save_cursor() == FAIL)
2906 return FALSE;
2907 }
2908 else if (op_delete(oap) == FAIL)
2909 return FALSE;
2910
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002911 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 && !virtual_op)
2913 inc_cursor();
2914
2915#ifdef FEAT_VISUALEXTRA
2916 /* check for still on same line (<CR> in inserted text meaningless) */
2917 /* skip blank lines too */
2918 if (oap->block_mode)
2919 {
2920# ifdef FEAT_VIRTUALEDIT
2921 /* Add spaces before getting the current line length. */
2922 if (virtual_op && (curwin->w_cursor.coladd > 0
2923 || gchar_cursor() == NUL))
2924 coladvance_force(getviscol());
2925# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002926 firstline = ml_get(oap->start.lnum);
2927 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002928 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 bd.textcol = curwin->w_cursor.col;
2930 }
2931#endif
2932
2933#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2934 if (oap->motion_type == MLINE)
2935 fix_indent();
2936#endif
2937
2938 retval = edit(NUL, FALSE, (linenr_T)1);
2939
2940#ifdef FEAT_VISUALEXTRA
2941 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002942 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002944 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002946 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002948 /* Auto-indenting may have changed the indent. If the cursor was past
2949 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002951 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002953 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002954
2955 pre_textlen += new_indent - pre_indent;
2956 bd.textcol += new_indent - pre_indent;
2957 }
2958
2959 ins_len = (long)STRLEN(firstline) - pre_textlen;
2960 if (ins_len > 0)
2961 {
2962 /* Subsequent calls to ml_get() flush the firstline data - take a
2963 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2965 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002966 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2968 linenr++)
2969 {
2970 block_prep(oap, &bd, linenr, TRUE);
2971 if (!bd.is_short || virtual_op)
2972 {
2973# ifdef FEAT_VIRTUALEDIT
2974 pos_T vpos;
2975
2976 /* If the block starts in virtual space, count the
2977 * initial coladd offset as part of "startspaces" */
2978 if (bd.is_short)
2979 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002980 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983 else
2984 vpos.coladd = 0;
2985# endif
2986 oldp = ml_get(linenr);
2987 newp = alloc_check((unsigned)(STRLEN(oldp)
2988# ifdef FEAT_VIRTUALEDIT
2989 + vpos.coladd
2990# endif
2991 + ins_len + 1));
2992 if (newp == NULL)
2993 continue;
2994 /* copy up to block start */
2995 mch_memmove(newp, oldp, (size_t)bd.textcol);
2996 offset = bd.textcol;
2997# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002998 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 offset += vpos.coladd;
3000# endif
3001 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
3002 offset += ins_len;
3003 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00003004 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 ml_replace(linenr, newp, FALSE);
3006 }
3007 }
3008 check_cursor();
3009
3010 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
3011 }
3012 vim_free(ins_text);
3013 }
3014 }
3015#endif
3016
3017 return retval;
3018}
3019
3020/*
3021 * set all the yank registers to empty (called from main())
3022 */
3023 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003024init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 int i;
3027
3028 for (i = 0; i < NUM_REGISTERS; ++i)
3029 y_regs[i].y_array = NULL;
3030}
3031
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003032#if defined(EXITFREE) || defined(PROTO)
3033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003034clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003035{
3036 int i;
3037
3038 for (i = 0; i < NUM_REGISTERS; ++i)
3039 {
3040 y_current = &y_regs[i];
3041 if (y_current->y_array != NULL)
3042 free_yank_all();
3043 }
3044}
3045#endif
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047/*
3048 * Free "n" lines from the current yank register.
3049 * Called for normal freeing and in case of error.
3050 */
3051 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003052free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 if (y_current->y_array != NULL)
3055 {
3056 long i;
3057
3058 for (i = n; --i >= 0; )
3059 {
3060#ifdef AMIGA /* only for very slow machines */
3061 if ((i & 1023) == 1023) /* this may take a while */
3062 {
3063 /*
3064 * This message should never cause a hit-return message.
3065 * Overwrite this message with any next message.
3066 */
3067 ++no_wait_return;
3068 smsg((char_u *)_("freeing %ld lines"), i + 1);
3069 --no_wait_return;
3070 msg_didout = FALSE;
3071 msg_col = 0;
3072 }
3073#endif
3074 vim_free(y_current->y_array[i]);
3075 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003076 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077#ifdef AMIGA
3078 if (n >= 1000)
3079 MSG("");
3080#endif
3081 }
3082}
3083
3084 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003085free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 free_yank(y_current->y_size);
3088}
3089
3090/*
3091 * Yank the text between "oap->start" and "oap->end" into a yank register.
3092 * If we are to append (uppercase register), we first yank into a new yank
3093 * register and then concatenate the old and the new one (so we keep the old
3094 * one in case of out-of-memory).
3095 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003096 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
3098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003099op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
3101 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003102 yankreg_T *curr; /* copy of y_current */
3103 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 char_u **new_ptr;
3105 linenr_T lnum; /* current line number */
3106 long j;
3107 int yanktype = oap->motion_type;
3108 long yanklines = oap->line_count;
3109 linenr_T yankendlnum = oap->end.lnum;
3110 char_u *p;
3111 char_u *pnew;
3112 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003113#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003114 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 /* check for read-only register */
3118 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3119 {
3120 beep_flush();
3121 return FAIL;
3122 }
3123 if (oap->regname == '_') /* black hole: nothing to do */
3124 return OK;
3125
3126#ifdef FEAT_CLIPBOARD
3127 if (!clip_star.available && oap->regname == '*')
3128 oap->regname = 0;
3129 else if (!clip_plus.available && oap->regname == '+')
3130 oap->regname = 0;
3131#endif
3132
3133 if (!deleting) /* op_delete() already set y_current */
3134 get_yank_register(oap->regname, TRUE);
3135
3136 curr = y_current;
3137 /* append to existing contents */
3138 if (y_append && y_current->y_array != NULL)
3139 y_current = &newreg;
3140 else
3141 free_yank_all(); /* free previously yanked lines */
3142
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003143 /*
3144 * If the cursor was in column 1 before and after the movement, and the
3145 * operator is not inclusive, the yank is always linewise.
3146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 if ( oap->motion_type == MCHAR
3148 && oap->start.col == 0
3149 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003151 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 && oap->end.col == 0
3153 && yanklines > 1)
3154 {
3155 yanktype = MLINE;
3156 --yankendlnum;
3157 --yanklines;
3158 }
3159
3160 y_current->y_size = yanklines;
3161 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3164 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (y_current->y_array == NULL)
3166 {
3167 y_current = curr;
3168 return FAIL;
3169 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003170#ifdef FEAT_VIMINFO
3171 y_current->y_time_set = vim_time();
3172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 y_idx = 0;
3175 lnum = oap->start.lnum;
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (oap->block_mode)
3178 {
3179 /* Visual block mode */
3180 y_current->y_type = MBLOCK; /* set the yank register type */
3181 y_current->y_width = oap->end_vcol - oap->start_vcol;
3182
3183 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3184 y_current->y_width--;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3188 {
3189 switch (y_current->y_type)
3190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 case MBLOCK:
3192 block_prep(oap, &bd, lnum, FALSE);
3193 if (yank_copy_line(&bd, y_idx) == FAIL)
3194 goto fail;
3195 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 case MLINE:
3198 if ((y_current->y_array[y_idx] =
3199 vim_strsave(ml_get(lnum))) == NULL)
3200 goto fail;
3201 break;
3202
3203 case MCHAR:
3204 {
3205 colnr_T startcol = 0, endcol = MAXCOL;
3206#ifdef FEAT_VIRTUALEDIT
3207 int is_oneChar = FALSE;
3208 colnr_T cs, ce;
3209#endif
3210 p = ml_get(lnum);
3211 bd.startspaces = 0;
3212 bd.endspaces = 0;
3213
3214 if (lnum == oap->start.lnum)
3215 {
3216 startcol = oap->start.col;
3217#ifdef FEAT_VIRTUALEDIT
3218 if (virtual_op)
3219 {
3220 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3221 if (ce != cs && oap->start.coladd > 0)
3222 {
3223 /* Part of a tab selected -- but don't
3224 * double-count it. */
3225 bd.startspaces = (ce - cs + 1)
3226 - oap->start.coladd;
3227 startcol++;
3228 }
3229 }
3230#endif
3231 }
3232
3233 if (lnum == oap->end.lnum)
3234 {
3235 endcol = oap->end.col;
3236#ifdef FEAT_VIRTUALEDIT
3237 if (virtual_op)
3238 {
3239 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3240 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3241# ifdef FEAT_MBYTE
3242 /* Don't add space for double-wide
3243 * char; endcol will be on last byte
3244 * of multi-byte char. */
3245 && (*mb_head_off)(p, p + endcol) == 0
3246# endif
3247 ))
3248 {
3249 if (oap->start.lnum == oap->end.lnum
3250 && oap->start.col == oap->end.col)
3251 {
3252 /* Special case: inside a single char */
3253 is_oneChar = TRUE;
3254 bd.startspaces = oap->end.coladd
3255 - oap->start.coladd + oap->inclusive;
3256 endcol = startcol;
3257 }
3258 else
3259 {
3260 bd.endspaces = oap->end.coladd
3261 + oap->inclusive;
3262 endcol -= oap->inclusive;
3263 }
3264 }
3265 }
3266#endif
3267 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003268 if (endcol == MAXCOL)
3269 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (startcol > endcol
3271#ifdef FEAT_VIRTUALEDIT
3272 || is_oneChar
3273#endif
3274 )
3275 bd.textlen = 0;
3276 else
3277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 bd.textlen = endcol - startcol + oap->inclusive;
3279 }
3280 bd.textstart = p + startcol;
3281 if (yank_copy_line(&bd, y_idx) == FAIL)
3282 goto fail;
3283 break;
3284 }
3285 /* NOTREACHED */
3286 }
3287 }
3288
3289 if (curr != y_current) /* append the new block to the old block */
3290 {
3291 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3292 (curr->y_size + y_current->y_size)), TRUE);
3293 if (new_ptr == NULL)
3294 goto fail;
3295 for (j = 0; j < curr->y_size; ++j)
3296 new_ptr[j] = curr->y_array[j];
3297 vim_free(curr->y_array);
3298 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003299#ifdef FEAT_VIMINFO
3300 curr->y_time_set = vim_time();
3301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3304 curr->y_type = MLINE;
3305
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003306 /* Concatenate the last line of the old block with the first line of
3307 * the new block, unless being Vi compatible. */
3308 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3311 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3312 if (pnew == NULL)
3313 {
3314 y_idx = y_current->y_size - 1;
3315 goto fail;
3316 }
3317 STRCPY(pnew, curr->y_array[--j]);
3318 STRCAT(pnew, y_current->y_array[0]);
3319 vim_free(curr->y_array[j]);
3320 vim_free(y_current->y_array[0]);
3321 curr->y_array[j++] = pnew;
3322 y_idx = 1;
3323 }
3324 else
3325 y_idx = 0;
3326 while (y_idx < y_current->y_size)
3327 curr->y_array[j++] = y_current->y_array[y_idx++];
3328 curr->y_size = j;
3329 vim_free(y_current->y_array);
3330 y_current = curr;
3331 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003332 if (curwin->w_p_rnu)
3333 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (mess) /* Display message about yank? */
3335 {
3336 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 && yanklines == 1)
3339 yanklines = 0;
3340 /* Some versions of Vi use ">=" here, some don't... */
3341 if (yanklines > p_report)
3342 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003343 char namebuf[100];
3344
3345 if (oap->regname == NUL)
3346 *namebuf = NUL;
3347 else
3348 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003349 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 /* redisplay now, so message is not deleted */
3352 update_topline_redraw();
3353 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003354 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003355 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003356 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003357 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003358 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003359 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003360 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003361 smsg((char_u *)_("block of %ld lines yanked%s"),
3362 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003364 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3365 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367 }
3368
3369 /*
3370 * Set "'[" and "']" marks.
3371 */
3372 curbuf->b_op_start = oap->start;
3373 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003374 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003375 {
3376 curbuf->b_op_start.col = 0;
3377 curbuf->b_op_end.col = MAXCOL;
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
3380#ifdef FEAT_CLIPBOARD
3381 /*
3382 * If we were yanking to the '*' register, send result to clipboard.
3383 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3384 * to the '*' register.
3385 */
3386 if (clip_star.available
3387 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003388 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003389 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 if (curr != &(y_regs[STAR_REGISTER]))
3392 /* Copy the text from register 0 to the clipboard register. */
3393 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3394
3395 clip_own_selection(&clip_star);
3396 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003397# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003398 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401
3402# ifdef FEAT_X11
3403 /*
3404 * If we were yanking to the '+' register, send result to selection.
3405 * Also copy to the '*' register, in case auto-select is off.
3406 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003407 if (clip_plus.available
3408 && (curr == &(y_regs[PLUS_REGISTER])
3409 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003410 && ((clip_unnamed | clip_unnamed_saved) &
3411 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003413 if (curr != &(y_regs[PLUS_REGISTER]))
3414 /* Copy the text from register 0 to the clipboard register. */
3415 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 clip_own_selection(&clip_plus);
3418 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003419 if (!clip_isautosel_star() && !clip_isautosel_plus()
3420 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
3422 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3423 clip_own_selection(&clip_star);
3424 clip_gen_set_selection(&clip_star);
3425 }
3426 }
3427# endif
3428#endif
3429
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003430#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003431 if (!deleting && has_textyankpost())
3432 yank_do_autocmd(oap, y_current);
3433#endif
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 return OK;
3436
3437fail: /* free the allocated lines */
3438 free_yank(y_idx + 1);
3439 y_current = curr;
3440 return FAIL;
3441}
3442
3443 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003444yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 char_u *pnew;
3447
3448 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3449 == NULL)
3450 return FAIL;
3451 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003452 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 pnew += bd->startspaces;
3454 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3455 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003456 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 pnew += bd->endspaces;
3458 *pnew = NUL;
3459 return OK;
3460}
3461
3462#ifdef FEAT_CLIPBOARD
3463/*
3464 * Make a copy of the y_current register to register "reg".
3465 */
3466 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003467copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003469 yankreg_T *curr = y_current;
3470 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 y_current = reg;
3473 free_yank_all();
3474 *y_current = *curr;
3475 y_current->y_array = (char_u **)lalloc_clear(
3476 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3477 if (y_current->y_array == NULL)
3478 y_current->y_size = 0;
3479 else
3480 for (j = 0; j < y_current->y_size; ++j)
3481 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3482 {
3483 free_yank(j);
3484 y_current->y_size = 0;
3485 break;
3486 }
3487 y_current = curr;
3488}
3489#endif
3490
3491/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003492 * Put contents of register "regname" into the text.
3493 * Caller must check "regname" to be valid!
3494 * "flags": PUT_FIXINDENT make indent look nice
3495 * PUT_CURSEND leave cursor after end of new text
3496 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499do_put(
3500 int regname,
3501 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3502 long count,
3503 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 char_u *ptr;
3506 char_u *newp, *oldp;
3507 int yanklen;
3508 int totlen = 0; /* init for gcc */
3509 linenr_T lnum;
3510 colnr_T col;
3511 long i; /* index in y_array[] */
3512 int y_type;
3513 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int oldlen;
3515 long y_width = 0;
3516 colnr_T vcol;
3517 int delcount;
3518 int incr = 0;
3519 long j;
3520 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u **y_array = NULL;
3522 long nr_lines = 0;
3523 pos_T new_cursor;
3524 int indent;
3525 int orig_indent = 0; /* init for gcc */
3526 int indent_diff = 0; /* init for gcc */
3527 int first_indent = TRUE;
3528 int lendiff = 0;
3529 pos_T old_pos;
3530 char_u *insert_string = NULL;
3531 int allocated = FALSE;
3532 long cnt;
3533
3534#ifdef FEAT_CLIPBOARD
3535 /* Adjust register name for "unnamed" in 'clipboard'. */
3536 adjust_clip_reg(&regname);
3537 (void)may_get_selection(regname);
3538#endif
3539
3540 if (flags & PUT_FIXINDENT)
3541 orig_indent = get_indent();
3542
3543 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3544 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3545
3546 /*
3547 * Using inserted text works differently, because the register includes
3548 * special characters (newlines, etc.).
3549 */
3550 if (regname == '.')
3551 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003552 if (VIsual_active)
3553 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3555 (count == -1 ? 'O' : 'i')), count, FALSE);
3556 /* Putting the text is done later, so can't really move the cursor to
3557 * the next character. Use "l" to simulate it. */
3558 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3559 stuffcharReadbuff('l');
3560 return;
3561 }
3562
3563 /*
3564 * For special registers '%' (file name), '#' (alternate file name) and
3565 * ':' (last command line), etc. we have to create a fake yank register.
3566 */
3567 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3568 {
3569 if (insert_string == NULL)
3570 return;
3571 }
3572
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003573 /* Autocommands may be executed when saving lines for undo. This might
3574 * make "y_array" invalid, so we start undo now to avoid that. */
3575 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3576 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003577
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (insert_string != NULL)
3579 {
3580 y_type = MCHAR;
3581#ifdef FEAT_EVAL
3582 if (regname == '=')
3583 {
3584 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003585 * characters.
3586 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 for (;;)
3588 {
3589 y_size = 0;
3590 ptr = insert_string;
3591 while (ptr != NULL)
3592 {
3593 if (y_array != NULL)
3594 y_array[y_size] = ptr;
3595 ++y_size;
3596 ptr = vim_strchr(ptr, '\n');
3597 if (ptr != NULL)
3598 {
3599 if (y_array != NULL)
3600 *ptr = NUL;
3601 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003602 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (*ptr == NUL)
3604 {
3605 y_type = MLINE;
3606 break;
3607 }
3608 }
3609 }
3610 if (y_array != NULL)
3611 break;
3612 y_array = (char_u **)alloc((unsigned)
3613 (y_size * sizeof(char_u *)));
3614 if (y_array == NULL)
3615 goto end;
3616 }
3617 }
3618 else
3619#endif
3620 {
3621 y_size = 1; /* use fake one-line yank register */
3622 y_array = &insert_string;
3623 }
3624 }
3625 else
3626 {
3627 get_yank_register(regname, FALSE);
3628
3629 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 y_size = y_current->y_size;
3632 y_array = y_current->y_array;
3633 }
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (y_type == MLINE)
3636 {
3637 if (flags & PUT_LINE_SPLIT)
3638 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003639 char_u *p;
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 /* "p" or "P" in Visual mode: split the lines to put the text in
3642 * between. */
3643 if (u_save_cursor() == FAIL)
3644 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003645 p = ml_get_cursor();
3646 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003647 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003648 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (ptr == NULL)
3650 goto end;
3651 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3652 vim_free(ptr);
3653
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003654 oldp = ml_get_curline();
3655 p = oldp + curwin->w_cursor.col;
3656 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003657 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003658 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (ptr == NULL)
3660 goto end;
3661 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3662 ++nr_lines;
3663 dir = FORWARD;
3664 }
3665 if (flags & PUT_LINE_FORWARD)
3666 {
3667 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003668 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 dir = FORWARD;
3670 }
3671 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3672 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3676 y_type = MLINE;
3677
3678 if (y_size == 0 || y_array == NULL)
3679 {
3680 EMSG2(_("E353: Nothing in register %s"),
3681 regname == 0 ? (char_u *)"\"" : transchar(regname));
3682 goto end;
3683 }
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (y_type == MBLOCK)
3686 {
3687 lnum = curwin->w_cursor.lnum + y_size + 1;
3688 if (lnum > curbuf->b_ml.ml_line_count)
3689 lnum = curbuf->b_ml.ml_line_count + 1;
3690 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3691 goto end;
3692 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003693 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 lnum = curwin->w_cursor.lnum;
3696#ifdef FEAT_FOLDING
3697 /* Correct line number for closed fold. Don't move the cursor yet,
3698 * u_save() uses it. */
3699 if (dir == BACKWARD)
3700 (void)hasFolding(lnum, &lnum, NULL);
3701 else
3702 (void)hasFolding(lnum, NULL, &lnum);
3703#endif
3704 if (dir == FORWARD)
3705 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003706 /* In an empty buffer the empty line is going to be replaced, include
3707 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003708 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 goto end;
3710#ifdef FEAT_FOLDING
3711 if (dir == FORWARD)
3712 curwin->w_cursor.lnum = lnum - 1;
3713 else
3714 curwin->w_cursor.lnum = lnum;
3715 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3716#endif
3717 }
3718 else if (u_save_cursor() == FAIL)
3719 goto end;
3720
3721 yanklen = (int)STRLEN(y_array[0]);
3722
3723#ifdef FEAT_VIRTUALEDIT
3724 if (ve_flags == VE_ALL && y_type == MCHAR)
3725 {
3726 if (gchar_cursor() == TAB)
3727 {
3728 /* Don't need to insert spaces when "p" on the last position of a
3729 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003730#ifdef FEAT_VARTABS
3731 int viscol = getviscol();
3732 if (dir == FORWARD
3733 ? tabstop_padding(viscol, curbuf->b_p_ts,
3734 curbuf->b_p_vts_array) != 1
3735 : curwin->w_cursor.coladd > 0)
3736 coladvance_force(viscol);
3737#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (dir == FORWARD
3739 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3740 : curwin->w_cursor.coladd > 0)
3741 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 else
3744 curwin->w_cursor.coladd = 0;
3745 }
3746 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3747 coladvance_force(getviscol() + (dir == FORWARD));
3748 }
3749#endif
3750
3751 lnum = curwin->w_cursor.lnum;
3752 col = curwin->w_cursor.col;
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 /*
3755 * Block mode
3756 */
3757 if (y_type == MBLOCK)
3758 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003759 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 colnr_T endcol2 = 0;
3761
3762 if (dir == FORWARD && c != NUL)
3763 {
3764#ifdef FEAT_VIRTUALEDIT
3765 if (ve_flags == VE_ALL)
3766 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3767 else
3768#endif
3769 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3770
3771#ifdef FEAT_MBYTE
3772 if (has_mbyte)
3773 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003774 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 else
3776#endif
3777#ifdef FEAT_VIRTUALEDIT
3778 if (c != TAB || ve_flags != VE_ALL)
3779#endif
3780 ++curwin->w_cursor.col;
3781 ++col;
3782 }
3783 else
3784 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3785
3786#ifdef FEAT_VIRTUALEDIT
3787 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003788 if (ve_flags == VE_ALL
3789 && (curwin->w_cursor.coladd > 0
3790 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 {
3792 if (dir == FORWARD && c == NUL)
3793 ++col;
3794 if (dir != FORWARD && c != NUL)
3795 ++curwin->w_cursor.col;
3796 if (c == TAB)
3797 {
3798 if (dir == BACKWARD && curwin->w_cursor.col)
3799 curwin->w_cursor.col--;
3800 if (dir == FORWARD && col - 1 == endcol2)
3801 curwin->w_cursor.col++;
3802 }
3803 }
3804 curwin->w_cursor.coladd = 0;
3805#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003806 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 for (i = 0; i < y_size; ++i)
3808 {
3809 int spaces;
3810 char shortline;
3811
3812 bd.startspaces = 0;
3813 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 vcol = 0;
3815 delcount = 0;
3816
3817 /* add a new line */
3818 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3819 {
3820 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3821 (colnr_T)1, FALSE) == FAIL)
3822 break;
3823 ++nr_lines;
3824 }
3825 /* get the old line and advance to the position to insert at */
3826 oldp = ml_get_curline();
3827 oldlen = (int)STRLEN(oldp);
3828 for (ptr = oldp; vcol < col && *ptr; )
3829 {
3830 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003831 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 vcol += incr;
3833 }
3834 bd.textcol = (colnr_T)(ptr - oldp);
3835
3836 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3837
3838 if (vcol < col) /* line too short, padd with spaces */
3839 bd.startspaces = col - vcol;
3840 else if (vcol > col)
3841 {
3842 bd.endspaces = vcol - col;
3843 bd.startspaces = incr - bd.endspaces;
3844 --bd.textcol;
3845 delcount = 1;
3846#ifdef FEAT_MBYTE
3847 if (has_mbyte)
3848 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3849#endif
3850 if (oldp[bd.textcol] != TAB)
3851 {
3852 /* Only a Tab can be split into spaces. Other
3853 * characters will have to be moved to after the
3854 * block, causing misalignment. */
3855 delcount = 0;
3856 bd.endspaces = 0;
3857 }
3858 }
3859
3860 yanklen = (int)STRLEN(y_array[i]);
3861
3862 /* calculate number of spaces required to fill right side of block*/
3863 spaces = y_width + 1;
3864 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003865 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (spaces < 0)
3867 spaces = 0;
3868
3869 /* insert the new text */
3870 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3871 newp = alloc_check((unsigned)totlen + oldlen + 1);
3872 if (newp == NULL)
3873 break;
3874 /* copy part up to cursor to new line */
3875 ptr = newp;
3876 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3877 ptr += bd.textcol;
3878 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003879 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 ptr += bd.startspaces;
3881 /* insert the new text */
3882 for (j = 0; j < count; ++j)
3883 {
3884 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3885 ptr += yanklen;
3886
3887 /* insert block's trailing spaces only if there's text behind */
3888 if ((j < count - 1 || !shortline) && spaces)
3889 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003890 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 ptr += spaces;
3892 }
3893 }
3894 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003895 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 ptr += bd.endspaces;
3897 /* move the text after the cursor to the end of the line. */
3898 mch_memmove(ptr, oldp + bd.textcol + delcount,
3899 (size_t)(oldlen - bd.textcol - delcount + 1));
3900 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3901
3902 ++curwin->w_cursor.lnum;
3903 if (i == 0)
3904 curwin->w_cursor.col += bd.startspaces;
3905 }
3906
3907 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3908
3909 /* Set '[ mark. */
3910 curbuf->b_op_start = curwin->w_cursor;
3911 curbuf->b_op_start.lnum = lnum;
3912
3913 /* adjust '] mark */
3914 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3915 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003916# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (flags & PUT_CURSEND)
3920 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003921 colnr_T len;
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 curwin->w_cursor = curbuf->b_op_end;
3924 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003925
3926 /* in Insert mode we might be after the NUL, correct for that */
3927 len = (colnr_T)STRLEN(ml_get_curline());
3928 if (curwin->w_cursor.col > len)
3929 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 else
3932 curwin->w_cursor.lnum = lnum;
3933 }
3934 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 /*
3937 * Character or Line mode
3938 */
3939 if (y_type == MCHAR)
3940 {
3941 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3942 * char */
3943 if (dir == FORWARD && gchar_cursor() != NUL)
3944 {
3945#ifdef FEAT_MBYTE
3946 if (has_mbyte)
3947 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003948 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 /* put it on the next of the multi-byte character. */
3951 col += bytelen;
3952 if (yanklen)
3953 {
3954 curwin->w_cursor.col += bytelen;
3955 curbuf->b_op_end.col += bytelen;
3956 }
3957 }
3958 else
3959#endif
3960 {
3961 ++col;
3962 if (yanklen)
3963 {
3964 ++curwin->w_cursor.col;
3965 ++curbuf->b_op_end.col;
3966 }
3967 }
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 curbuf->b_op_start = curwin->w_cursor;
3970 }
3971 /*
3972 * Line mode: BACKWARD is the same as FORWARD on the previous line
3973 */
3974 else if (dir == BACKWARD)
3975 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003976 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978 /*
3979 * simple case: insert into current line
3980 */
3981 if (y_type == MCHAR && y_size == 1)
3982 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003983 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003984
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003985 if (VIsual_active)
3986 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003987 end_lnum = curbuf->b_visual.vi_end.lnum;
3988 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3989 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003990 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003991
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003992 do {
3993 totlen = count * yanklen;
3994 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003996 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003997 if (VIsual_active && col > (int)STRLEN(oldp))
3998 {
3999 lnum++;
4000 continue;
4001 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004002 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
4003 if (newp == NULL)
4004 goto end; /* alloc() gave an error message */
4005 mch_memmove(newp, oldp, (size_t)col);
4006 ptr = newp + col;
4007 for (i = 0; i < count; ++i)
4008 {
4009 mch_memmove(ptr, y_array[0], (size_t)yanklen);
4010 ptr += yanklen;
4011 }
4012 STRMOVE(ptr, oldp + col);
4013 ml_replace(lnum, newp, FALSE);
4014 /* Place cursor on last putted char. */
4015 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01004016 {
4017 /* make sure curwin->w_virtcol is updated */
4018 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004019 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01004020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004022 if (VIsual_active)
4023 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01004024 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004025
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01004026 if (VIsual_active) /* reset lnum to the last visual line */
4027 lnum--;
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 curbuf->b_op_end = curwin->w_cursor;
4030 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4031 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4032 ++curwin->w_cursor.col;
4033 changed_bytes(lnum, col);
4034 }
4035 else
4036 {
4037 /*
4038 * Insert at least one line. When y_type is MCHAR, break the first
4039 * line in two.
4040 */
4041 for (cnt = 1; cnt <= count; ++cnt)
4042 {
4043 i = 0;
4044 if (y_type == MCHAR)
4045 {
4046 /*
4047 * Split the current line in two at the insert position.
4048 * First insert y_array[size - 1] in front of second line.
4049 * Then append y_array[0] to first line.
4050 */
4051 lnum = new_cursor.lnum;
4052 ptr = ml_get(lnum) + col;
4053 totlen = (int)STRLEN(y_array[y_size - 1]);
4054 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4055 if (newp == NULL)
4056 goto error;
4057 STRCPY(newp, y_array[y_size - 1]);
4058 STRCAT(newp, ptr);
4059 /* insert second line */
4060 ml_append(lnum, newp, (colnr_T)0, FALSE);
4061 vim_free(newp);
4062
4063 oldp = ml_get(lnum);
4064 newp = alloc_check((unsigned)(col + yanklen + 1));
4065 if (newp == NULL)
4066 goto error;
4067 /* copy first part of line */
4068 mch_memmove(newp, oldp, (size_t)col);
4069 /* append to first line */
4070 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4071 ml_replace(lnum, newp, FALSE);
4072
4073 curwin->w_cursor.lnum = lnum;
4074 i = 1;
4075 }
4076
4077 for (; i < y_size; ++i)
4078 {
4079 if ((y_type != MCHAR || i < y_size - 1)
4080 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4081 == FAIL)
4082 goto error;
4083 lnum++;
4084 ++nr_lines;
4085 if (flags & PUT_FIXINDENT)
4086 {
4087 old_pos = curwin->w_cursor;
4088 curwin->w_cursor.lnum = lnum;
4089 ptr = ml_get(lnum);
4090 if (cnt == count && i == y_size - 1)
4091 lendiff = (int)STRLEN(ptr);
4092#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4093 if (*ptr == '#' && preprocs_left())
4094 indent = 0; /* Leave # lines at start */
4095 else
4096#endif
4097 if (*ptr == NUL)
4098 indent = 0; /* Ignore empty lines */
4099 else if (first_indent)
4100 {
4101 indent_diff = orig_indent - get_indent();
4102 indent = orig_indent;
4103 first_indent = FALSE;
4104 }
4105 else if ((indent = get_indent() + indent_diff) < 0)
4106 indent = 0;
4107 (void)set_indent(indent, 0);
4108 curwin->w_cursor = old_pos;
4109 /* remember how many chars were removed */
4110 if (cnt == count && i == y_size - 1)
4111 lendiff -= (int)STRLEN(ml_get(lnum));
4112 }
4113 }
4114 }
4115
4116error:
4117 /* Adjust marks. */
4118 if (y_type == MLINE)
4119 {
4120 curbuf->b_op_start.col = 0;
4121 if (dir == FORWARD)
4122 curbuf->b_op_start.lnum++;
4123 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004124 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004125 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004126 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004127 < curbuf->b_ml.ml_line_count
4128#ifdef FEAT_DIFF
4129 || curwin->w_p_diff
4130#endif
4131 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004132 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 (linenr_T)MAXLNUM, nr_lines, 0L);
4134
4135 /* note changed text for displaying and folding */
4136 if (y_type == MCHAR)
4137 changed_lines(curwin->w_cursor.lnum, col,
4138 curwin->w_cursor.lnum + 1, nr_lines);
4139 else
4140 changed_lines(curbuf->b_op_start.lnum, 0,
4141 curbuf->b_op_start.lnum, nr_lines);
4142
4143 /* put '] mark at last inserted character */
4144 curbuf->b_op_end.lnum = lnum;
4145 /* correct length for change in indent */
4146 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4147 if (col > 1)
4148 curbuf->b_op_end.col = col - 1;
4149 else
4150 curbuf->b_op_end.col = 0;
4151
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004152 if (flags & PUT_CURSLINE)
4153 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004154 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004155 curwin->w_cursor.lnum = lnum;
4156 beginline(BL_WHITE | BL_FIX);
4157 }
4158 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 /* put cursor after inserted text */
4161 if (y_type == MLINE)
4162 {
4163 if (lnum >= curbuf->b_ml.ml_line_count)
4164 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4165 else
4166 curwin->w_cursor.lnum = lnum + 1;
4167 curwin->w_cursor.col = 0;
4168 }
4169 else
4170 {
4171 curwin->w_cursor.lnum = lnum;
4172 curwin->w_cursor.col = col;
4173 }
4174 }
4175 else if (y_type == MLINE)
4176 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004177 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 curwin->w_cursor.col = 0;
4179 if (dir == FORWARD)
4180 ++curwin->w_cursor.lnum;
4181 beginline(BL_WHITE | BL_FIX);
4182 }
4183 else /* put cursor on first inserted character */
4184 curwin->w_cursor = new_cursor;
4185 }
4186 }
4187
4188 msgmore(nr_lines);
4189 curwin->w_set_curswant = TRUE;
4190
4191end:
4192 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004194 if (regname == '=')
4195 vim_free(y_array);
4196
Bram Moolenaar033d8882013-09-25 23:24:57 +02004197 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004198
Bram Moolenaar677ee682005-01-27 14:41:15 +00004199 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004200 adjust_cursor_eol();
4201}
4202
4203/*
4204 * When the cursor is on the NUL past the end of the line and it should not be
4205 * there move it left.
4206 */
4207 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004208adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004209{
4210 if (curwin->w_cursor.col > 0
4211 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004212#ifdef FEAT_VIRTUALEDIT
4213 && (ve_flags & VE_ONEMORE) == 0
4214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 && !(restart_edit || (State & INSERT)))
4216 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004217 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004218 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220#ifdef FEAT_VIRTUALEDIT
4221 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004222 {
4223 colnr_T scol, ecol;
4224
4225 /* Coladd is set to the width of the last character. */
4226 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4227 curwin->w_cursor.coladd = ecol - scol + 1;
4228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#endif
4230 }
4231}
4232
4233#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4234/*
4235 * Return TRUE if lines starting with '#' should be left aligned.
4236 */
4237 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004238preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 return
4241# ifdef FEAT_SMARTINDENT
4242# ifdef FEAT_CINDENT
4243 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4244# else
4245 curbuf->b_p_si
4246# endif
4247# endif
4248# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004249 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4250 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251# endif
4252 ;
4253}
4254#endif
4255
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004256/*
4257 * Return the character name of the register with the given number.
4258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004260get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 if (num == -1)
4263 return '"';
4264 else if (num < 10)
4265 return num + '0';
4266 else if (num == DELETION_REGISTER)
4267 return '-';
4268#ifdef FEAT_CLIPBOARD
4269 else if (num == STAR_REGISTER)
4270 return '*';
4271 else if (num == PLUS_REGISTER)
4272 return '+';
4273#endif
4274 else
4275 {
4276#ifdef EBCDIC
4277 int i;
4278
4279 /* EBCDIC is really braindead ... */
4280 i = 'a' + (num - 10);
4281 if (i > 'i')
4282 i += 7;
4283 if (i > 'r')
4284 i += 8;
4285 return i;
4286#else
4287 return num + 'a' - 10;
4288#endif
4289 }
4290}
4291
4292/*
4293 * ":dis" and ":registers": Display the contents of the yank registers.
4294 */
4295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004296ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004298 int i, n;
4299 long j;
4300 char_u *p;
4301 yankreg_T *yb;
4302 int name;
4303 int attr;
4304 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004305#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004306 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004307#else
4308# define clen 1
4309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 if (arg != NULL && *arg == NUL)
4312 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004313 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
4315 /* Highlight title */
4316 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4317 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4318 {
4319 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004320 if (arg != NULL && vim_strchr(arg, name) == NULL
4321#ifdef ONE_CLIPBOARD
4322 /* Star register and plus register contain the same thing. */
4323 && (name != '*' || vim_strchr(arg, '+') == NULL)
4324#endif
4325 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 continue; /* did not ask for this register */
4327
4328#ifdef FEAT_CLIPBOARD
4329 /* Adjust register name for "unnamed" in 'clipboard'.
4330 * When it's a clipboard register, fill it with the current contents
4331 * of the clipboard. */
4332 adjust_clip_reg(&name);
4333 (void)may_get_selection(name);
4334#endif
4335
4336 if (i == -1)
4337 {
4338 if (y_previous != NULL)
4339 yb = y_previous;
4340 else
4341 yb = &(y_regs[0]);
4342 }
4343 else
4344 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004345
4346#ifdef FEAT_EVAL
4347 if (name == MB_TOLOWER(redir_reg)
4348 || (redir_reg == '"' && yb == y_previous))
4349 continue; /* do not list register being written to, the
4350 * pointer can be freed */
4351#endif
4352
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (yb->y_array != NULL)
4354 {
4355 msg_putchar('\n');
4356 msg_putchar('"');
4357 msg_putchar(name);
4358 MSG_PUTS(" ");
4359
4360 n = (int)Columns - 6;
4361 for (j = 0; j < yb->y_size && n > 1; ++j)
4362 {
4363 if (j)
4364 {
4365 MSG_PUTS_ATTR("^J", attr);
4366 n -= 2;
4367 }
4368 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004371 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004372#endif
4373 msg_outtrans_len(p, clen);
4374#ifdef FEAT_MBYTE
4375 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#endif
4377 }
4378 }
4379 if (n > 1 && yb->y_type == MLINE)
4380 MSG_PUTS_ATTR("^J", attr);
4381 out_flush(); /* show one line at a time */
4382 }
4383 ui_breakcheck();
4384 }
4385
4386 /*
4387 * display last inserted text
4388 */
4389 if ((p = get_last_insert()) != NULL
4390 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4391 {
4392 MSG_PUTS("\n\". ");
4393 dis_msg(p, TRUE);
4394 }
4395
4396 /*
4397 * display last command line
4398 */
4399 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4400 && !got_int)
4401 {
4402 MSG_PUTS("\n\": ");
4403 dis_msg(last_cmdline, FALSE);
4404 }
4405
4406 /*
4407 * display current file name
4408 */
4409 if (curbuf->b_fname != NULL
4410 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4411 {
4412 MSG_PUTS("\n\"% ");
4413 dis_msg(curbuf->b_fname, FALSE);
4414 }
4415
4416 /*
4417 * display alternate file name
4418 */
4419 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4420 {
4421 char_u *fname;
4422 linenr_T dummy;
4423
4424 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4425 {
4426 MSG_PUTS("\n\"# ");
4427 dis_msg(fname, FALSE);
4428 }
4429 }
4430
4431 /*
4432 * display last search pattern
4433 */
4434 if (last_search_pat() != NULL
4435 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4436 {
4437 MSG_PUTS("\n\"/ ");
4438 dis_msg(last_search_pat(), FALSE);
4439 }
4440
4441#ifdef FEAT_EVAL
4442 /*
4443 * display last used expression
4444 */
4445 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4446 && !got_int)
4447 {
4448 MSG_PUTS("\n\"= ");
4449 dis_msg(expr_line, FALSE);
4450 }
4451#endif
4452}
4453
4454/*
4455 * display a string for do_dis()
4456 * truncate at end of screen line
4457 */
4458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004459dis_msg(
4460 char_u *p,
4461 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 int n;
4464#ifdef FEAT_MBYTE
4465 int l;
4466#endif
4467
4468 n = (int)Columns - 6;
4469 while (*p != NUL
4470 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4471 && (n -= ptr2cells(p)) >= 0)
4472 {
4473#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004474 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
4476 msg_outtrans_len(p, l);
4477 p += l;
4478 }
4479 else
4480#endif
4481 msg_outtrans_len(p++, 1);
4482 }
4483 ui_breakcheck();
4484}
4485
Bram Moolenaar81340392012-06-06 16:12:59 +02004486#if defined(FEAT_COMMENTS) || defined(PROTO)
4487/*
4488 * If "process" is TRUE and the line begins with a comment leader (possibly
4489 * after some white space), return a pointer to the text after it. Put a boolean
4490 * value indicating whether the line ends with an unclosed comment in
4491 * "is_comment".
4492 * line - line to be processed,
4493 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004494 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004495 * include_space - whether to also skip space following the comment leader,
4496 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004497 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004498 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004499 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004500skip_comment(
4501 char_u *line,
4502 int process,
4503 int include_space,
4504 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004505{
4506 char_u *comment_flags = NULL;
4507 int lead_len;
4508 int leader_offset = get_last_leader_offset(line, &comment_flags);
4509
4510 *is_comment = FALSE;
4511 if (leader_offset != -1)
4512 {
4513 /* Let's check whether the line ends with an unclosed comment.
4514 * If the last comment leader has COM_END in flags, there's no comment.
4515 */
4516 while (*comment_flags)
4517 {
4518 if (*comment_flags == COM_END
4519 || *comment_flags == ':')
4520 break;
4521 ++comment_flags;
4522 }
4523 if (*comment_flags != COM_END)
4524 *is_comment = TRUE;
4525 }
4526
4527 if (process == FALSE)
4528 return line;
4529
4530 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4531
4532 if (lead_len == 0)
4533 return line;
4534
4535 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004536 * - COM_END,
4537 * - colon,
4538 * whichever comes first.
4539 */
4540 while (*comment_flags)
4541 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004542 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004543 || *comment_flags == ':')
4544 {
4545 break;
4546 }
4547 ++comment_flags;
4548 }
4549
4550 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004551 * starting with a closing part of a three-part comment. That's good,
4552 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004553 */
4554 if (*comment_flags == ':' || *comment_flags == NUL)
4555 line += lead_len;
4556
4557 return line;
4558}
4559#endif
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004562 * Join 'count' lines (minimal 2) at cursor position.
4563 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004564 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4565 * leaders should not be removed.
4566 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4567 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004569 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 */
4571 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004572do_join(
4573 long count,
4574 int insert_space,
4575 int save_undo,
4576 int use_formatoptions UNUSED,
4577 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004579 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004580 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004581 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004583 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004584 int endcurr1 = NUL;
4585 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004586 int currsize = 0; /* size of the current line */
4587 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004589 colnr_T col = 0;
4590 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004591#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004592 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004593 int remove_comments = (use_formatoptions == TRUE)
4594 && has_format_option(FO_REMOVE_COMS);
4595 int prev_was_comment;
4596#endif
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004599 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4600 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4601 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004603 /* Allocate an array to store the number of spaces inserted before each
4604 * line. We will use it to pre-compute the length of the new line and the
4605 * proper placement of each original line in the new one. */
4606 spaces = lalloc_clear((long_u)count, TRUE);
4607 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004609#if defined(FEAT_COMMENTS) || defined(PROTO)
4610 if (remove_comments)
4611 {
4612 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4613 if (comments == NULL)
4614 {
4615 vim_free(spaces);
4616 return FAIL;
4617 }
4618 }
4619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004622 * Don't move anything, just compute the final line length
4623 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004625 for (t = 0; t < count; ++t)
4626 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004627 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004628 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004629 {
4630 /* Set the '[ mark. */
4631 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4632 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4633 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004634#if defined(FEAT_COMMENTS) || defined(PROTO)
4635 if (remove_comments)
4636 {
4637 /* We don't want to remove the comment leader if the
4638 * previous line is not a comment. */
4639 if (t > 0 && prev_was_comment)
4640 {
4641
4642 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4643 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004644 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004645 curr = new_curr;
4646 }
4647 else
4648 curr = skip_comment(curr, FALSE, insert_space,
4649 &prev_was_comment);
4650 }
4651#endif
4652
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004653 if (insert_space && t > 0)
4654 {
4655 curr = skipwhite(curr);
4656 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4657#ifdef FEAT_MBYTE
4658 && (!has_format_option(FO_MBYTE_JOIN)
4659 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4660 && (!has_format_option(FO_MBYTE_JOIN2)
4661 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4662#endif
4663 )
4664 {
4665 /* don't add a space if the line is ending in a space */
4666 if (endcurr1 == ' ')
4667 endcurr1 = endcurr2;
4668 else
4669 ++spaces[t];
4670 /* extra space when 'joinspaces' set and line ends in '.' */
4671 if ( p_js
4672 && (endcurr1 == '.'
4673 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4674 && (endcurr1 == '?' || endcurr1 == '!'))))
4675 ++spaces[t];
4676 }
4677 }
4678 currsize = (int)STRLEN(curr);
4679 sumsize += currsize + spaces[t];
4680 endcurr1 = endcurr2 = NUL;
4681 if (insert_space && currsize > 0)
4682 {
4683#ifdef FEAT_MBYTE
4684 if (has_mbyte)
4685 {
4686 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004687 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004688 endcurr1 = (*mb_ptr2char)(cend);
4689 if (cend > curr)
4690 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004691 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004692 endcurr2 = (*mb_ptr2char)(cend);
4693 }
4694 }
4695 else
4696#endif
4697 {
4698 endcurr1 = *(curr + currsize - 1);
4699 if (currsize > 1)
4700 endcurr2 = *(curr + currsize - 2);
4701 }
4702 }
4703 line_breakcheck();
4704 if (got_int)
4705 {
4706 ret = FAIL;
4707 goto theend;
4708 }
4709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004711 /* store the column position before last line */
4712 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004714 /* allocate the space for the new line */
4715 newp = alloc_check((unsigned)(sumsize + 1));
4716 cend = newp + sumsize;
4717 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004719 /*
4720 * Move affected lines to the new long one.
4721 *
4722 * Move marks from each deleted line to the joined line, adjusting the
4723 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4724 * should not really be a problem.
4725 */
4726 for (t = count - 1; ; --t)
4727 {
4728 cend -= currsize;
4729 mch_memmove(cend, curr, (size_t)currsize);
4730 if (spaces[t] > 0)
4731 {
4732 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004733 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004734 }
4735 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004736 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004737 if (t == 0)
4738 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004739 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004740#if defined(FEAT_COMMENTS) || defined(PROTO)
4741 if (remove_comments)
4742 curr += comments[t - 1];
4743#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004744 if (insert_space && t > 1)
4745 curr = skipwhite(curr);
4746 currsize = (int)STRLEN(curr);
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4749
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004750 if (setmark)
4751 {
4752 /* Set the '] mark. */
4753 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4754 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4755 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 /* Only report the change in the first line here, del_lines() will report
4758 * the deleted line. */
4759 changed_lines(curwin->w_cursor.lnum, currsize,
4760 curwin->w_cursor.lnum + 1, 0L);
4761
4762 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004763 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 * briefly, and then move it back. After del_lines() the cursor may
4765 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 */
4767 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004769 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 curwin->w_cursor.lnum = t;
4771
4772 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004773 * Set the cursor column:
4774 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004775 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004777 curwin->w_cursor.col =
4778 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781#ifdef FEAT_VIRTUALEDIT
4782 curwin->w_cursor.coladd = 0;
4783#endif
4784 curwin->w_set_curswant = TRUE;
4785
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004786theend:
4787 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004788#if defined(FEAT_COMMENTS) || defined(PROTO)
4789 if (remove_comments)
4790 vim_free(comments);
4791#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004792 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793}
4794
4795#ifdef FEAT_COMMENTS
4796/*
4797 * Return TRUE if the two comment leaders given are the same. "lnum" is
4798 * the first line. White-space is ignored. Note that the whole of
4799 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4800 */
4801 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004802same_leader(
4803 linenr_T lnum,
4804 int leader1_len,
4805 char_u *leader1_flags,
4806 int leader2_len,
4807 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 int idx1 = 0, idx2 = 0;
4810 char_u *p;
4811 char_u *line1;
4812 char_u *line2;
4813
4814 if (leader1_len == 0)
4815 return (leader2_len == 0);
4816
4817 /*
4818 * If first leader has 'f' flag, the lines can be joined only if the
4819 * second line does not have a leader.
4820 * If first leader has 'e' flag, the lines can never be joined.
4821 * If fist leader has 's' flag, the lines can only be joined if there is
4822 * some text after it and the second line has the 'm' flag.
4823 */
4824 if (leader1_flags != NULL)
4825 {
4826 for (p = leader1_flags; *p && *p != ':'; ++p)
4827 {
4828 if (*p == COM_FIRST)
4829 return (leader2_len == 0);
4830 if (*p == COM_END)
4831 return FALSE;
4832 if (*p == COM_START)
4833 {
4834 if (*(ml_get(lnum) + leader1_len) == NUL)
4835 return FALSE;
4836 if (leader2_flags == NULL || leader2_len == 0)
4837 return FALSE;
4838 for (p = leader2_flags; *p && *p != ':'; ++p)
4839 if (*p == COM_MIDDLE)
4840 return TRUE;
4841 return FALSE;
4842 }
4843 }
4844 }
4845
4846 /*
4847 * Get current line and next line, compare the leaders.
4848 * The first line has to be saved, only one line can be locked at a time.
4849 */
4850 line1 = vim_strsave(ml_get(lnum));
4851 if (line1 != NULL)
4852 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004853 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 ;
4855 line2 = ml_get(lnum + 1);
4856 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4857 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004858 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 if (line1[idx1++] != line2[idx2])
4861 break;
4862 }
4863 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004864 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 ++idx1;
4866 }
4867 vim_free(line1);
4868 }
4869 return (idx2 == leader2_len && idx1 == leader1_len);
4870}
4871#endif
4872
4873/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004874 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
4876 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004877op_format(
4878 oparg_T *oap,
4879 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 long old_line_count = curbuf->b_ml.ml_line_count;
4882
4883 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4884 * can put it back there. */
4885 curwin->w_cursor = oap->cursor_start;
4886
4887 if (u_save((linenr_T)(oap->start.lnum - 1),
4888 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4889 return;
4890 curwin->w_cursor = oap->start;
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (oap->is_VIsual)
4893 /* When there is no change: need to remove the Visual selection */
4894 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896 /* Set '[ mark at the start of the formatted area */
4897 curbuf->b_op_start = oap->start;
4898
4899 /* For "gw" remember the cursor position and put it back below (adjusted
4900 * for joined and split lines). */
4901 if (keep_cursor)
4902 saved_cursor = oap->cursor_start;
4903
Bram Moolenaar81a82092008-03-12 16:27:00 +00004904 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 /*
4907 * Leave the cursor at the first non-blank of the last formatted line.
4908 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4909 * line, so "." will do the next lines.
4910 */
4911 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4912 ++curwin->w_cursor.lnum;
4913 beginline(BL_WHITE | BL_FIX);
4914 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4915 msgmore(old_line_count);
4916
4917 /* put '] mark on the end of the formatted area */
4918 curbuf->b_op_end = curwin->w_cursor;
4919
4920 if (keep_cursor)
4921 {
4922 curwin->w_cursor = saved_cursor;
4923 saved_cursor.lnum = 0;
4924 }
4925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (oap->is_VIsual)
4927 {
4928 win_T *wp;
4929
4930 FOR_ALL_WINDOWS(wp)
4931 {
4932 if (wp->w_old_cursor_lnum != 0)
4933 {
4934 /* When lines have been inserted or deleted, adjust the end of
4935 * the Visual area to be redrawn. */
4936 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4937 wp->w_old_cursor_lnum += old_line_count;
4938 else
4939 wp->w_old_visual_lnum += old_line_count;
4940 }
4941 }
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943}
4944
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004945#if defined(FEAT_EVAL) || defined(PROTO)
4946/*
4947 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4948 */
4949 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004951{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004952 if (oap->is_VIsual)
4953 /* When there is no change: need to remove the Visual selection */
4954 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004955
Bram Moolenaar700303e2010-07-11 17:35:50 +02004956 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4957 /* As documented: when 'formatexpr' returns non-zero fall back to
4958 * internal formatting. */
4959 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004960}
4961
4962 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963fex_format(
4964 linenr_T lnum,
4965 long count,
4966 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004967{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004968 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4969 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004970 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004971 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004972
4973 /*
4974 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004975 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004976 */
4977 set_vim_var_nr(VV_LNUM, lnum);
4978 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004979 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004980
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004981 /* Make a copy, the option could be changed while calling it. */
4982 fex = vim_strsave(curbuf->b_p_fex);
4983 if (fex == NULL)
4984 return 0;
4985
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004986 /*
4987 * Evaluate the function.
4988 */
4989 if (use_sandbox)
4990 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004991 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004992 if (use_sandbox)
4993 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004994
4995 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004996 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004997
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004998 return r;
4999}
5000#endif
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002/*
5003 * Format "line_count" lines, starting at the cursor position.
5004 * When "line_count" is negative, format until the end of the paragraph.
5005 * Lines after the cursor line are saved for undo, caller must have saved the
5006 * first line.
5007 */
5008 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005009format_lines(
5010 linenr_T line_count,
5011 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 int max_len;
5014 int is_not_par; /* current line not part of parag. */
5015 int next_is_not_par; /* next line not part of paragraph */
5016 int is_end_par; /* at end of paragraph */
5017 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
5018 int next_is_start_par = FALSE;
5019#ifdef FEAT_COMMENTS
5020 int leader_len = 0; /* leader len of current line */
5021 int next_leader_len; /* leader len of next line */
5022 char_u *leader_flags = NULL; /* flags for leader of current line */
5023 char_u *next_leader_flags; /* flags for leader of next line */
5024 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005025 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026#endif
5027 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005028 int second_indent = -1; /* indent for second line (comment
5029 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 int do_second_indent;
5031 int do_number_indent;
5032 int do_trail_white;
5033 int first_par_line = TRUE;
5034 int smd_save;
5035 long count;
5036 int need_set_indent = TRUE; /* set indent of next paragraph */
5037 int force_format = FALSE;
5038 int old_State = State;
5039
5040 /* length of a line to force formatting: 3 * 'tw' */
5041 max_len = comp_textwidth(TRUE) * 3;
5042
5043 /* check for 'q', '2' and '1' in 'formatoptions' */
5044#ifdef FEAT_COMMENTS
5045 do_comments = has_format_option(FO_Q_COMS);
5046#endif
5047 do_second_indent = has_format_option(FO_Q_SECOND);
5048 do_number_indent = has_format_option(FO_Q_NUMBER);
5049 do_trail_white = has_format_option(FO_WHITE_PAR);
5050
5051 /*
5052 * Get info about the previous and current line.
5053 */
5054 if (curwin->w_cursor.lnum > 1)
5055 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5056#ifdef FEAT_COMMENTS
5057 , &leader_len, &leader_flags, do_comments
5058#endif
5059 );
5060 else
5061 is_not_par = TRUE;
5062 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5063#ifdef FEAT_COMMENTS
5064 , &next_leader_len, &next_leader_flags, do_comments
5065#endif
5066 );
5067 is_end_par = (is_not_par || next_is_not_par);
5068 if (!is_end_par && do_trail_white)
5069 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5070
5071 curwin->w_cursor.lnum--;
5072 for (count = line_count; count != 0 && !got_int; --count)
5073 {
5074 /*
5075 * Advance to next paragraph.
5076 */
5077 if (advance)
5078 {
5079 curwin->w_cursor.lnum++;
5080 prev_is_end_par = is_end_par;
5081 is_not_par = next_is_not_par;
5082#ifdef FEAT_COMMENTS
5083 leader_len = next_leader_len;
5084 leader_flags = next_leader_flags;
5085#endif
5086 }
5087
5088 /*
5089 * The last line to be formatted.
5090 */
5091 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5092 {
5093 next_is_not_par = TRUE;
5094#ifdef FEAT_COMMENTS
5095 next_leader_len = 0;
5096 next_leader_flags = NULL;
5097#endif
5098 }
5099 else
5100 {
5101 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5102#ifdef FEAT_COMMENTS
5103 , &next_leader_len, &next_leader_flags, do_comments
5104#endif
5105 );
5106 if (do_number_indent)
5107 next_is_start_par =
5108 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5109 }
5110 advance = TRUE;
5111 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5112 if (!is_end_par && do_trail_white)
5113 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5114
5115 /*
5116 * Skip lines that are not in a paragraph.
5117 */
5118 if (is_not_par)
5119 {
5120 if (line_count < 0)
5121 break;
5122 }
5123 else
5124 {
5125 /*
5126 * For the first line of a paragraph, check indent of second line.
5127 * Don't do this for comments and empty lines.
5128 */
5129 if (first_par_line
5130 && (do_second_indent || do_number_indent)
5131 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005132 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005134 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005135 {
5136#ifdef FEAT_COMMENTS
5137 if (leader_len == 0 && next_leader_len == 0)
5138 {
5139 /* no comment found */
5140#endif
5141 second_indent =
5142 get_indent_lnum(curwin->w_cursor.lnum + 1);
5143#ifdef FEAT_COMMENTS
5144 }
5145 else
5146 {
5147 second_indent = next_leader_len;
5148 do_comments_list = 1;
5149 }
5150#endif
5151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005153 {
5154#ifdef FEAT_COMMENTS
5155 if (leader_len == 0 && next_leader_len == 0)
5156 {
5157 /* no comment found */
5158#endif
5159 second_indent =
5160 get_number_indent(curwin->w_cursor.lnum);
5161#ifdef FEAT_COMMENTS
5162 }
5163 else
5164 {
5165 /* get_number_indent() is now "comment aware"... */
5166 second_indent =
5167 get_number_indent(curwin->w_cursor.lnum);
5168 do_comments_list = 1;
5169 }
5170#endif
5171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
5174 /*
5175 * When the comment leader changes, it's the end of the paragraph.
5176 */
5177 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5178#ifdef FEAT_COMMENTS
5179 || !same_leader(curwin->w_cursor.lnum,
5180 leader_len, leader_flags,
5181 next_leader_len, next_leader_flags)
5182#endif
5183 )
5184 is_end_par = TRUE;
5185
5186 /*
5187 * If we have got to the end of a paragraph, or the line is
5188 * getting long, format it.
5189 */
5190 if (is_end_par || force_format)
5191 {
5192 if (need_set_indent)
5193 /* replace indent in first line with minimal number of
5194 * tabs and spaces, according to current options */
5195 (void)set_indent(get_indent(), SIN_CHANGED);
5196
5197 /* put cursor on last non-space */
5198 State = NORMAL; /* don't go past end-of-line */
5199 coladvance((colnr_T)MAXCOL);
5200 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5201 dec_cursor();
5202
5203 /* do the formatting, without 'showmode' */
5204 State = INSERT; /* for open_line() */
5205 smd_save = p_smd;
5206 p_smd = FALSE;
5207 insertchar(NUL, INSCHAR_FORMAT
5208#ifdef FEAT_COMMENTS
5209 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005210 + (do_comments && do_comments_list
5211 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005213 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 State = old_State;
5215 p_smd = smd_save;
5216 second_indent = -1;
5217 /* at end of par.: need to set indent of next par. */
5218 need_set_indent = is_end_par;
5219 if (is_end_par)
5220 {
5221 /* When called with a negative line count, break at the
5222 * end of the paragraph. */
5223 if (line_count < 0)
5224 break;
5225 first_par_line = TRUE;
5226 }
5227 force_format = FALSE;
5228 }
5229
5230 /*
5231 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005232 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 */
5234 if (!is_end_par)
5235 {
5236 advance = FALSE;
5237 curwin->w_cursor.lnum++;
5238 curwin->w_cursor.col = 0;
5239 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005243 {
5244 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5246 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005247 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005249 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5250 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005251 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005252
5253 if (indent > 0)
5254 {
5255 (void)del_bytes(indent, FALSE, FALSE);
5256 mark_col_adjust(curwin->w_cursor.lnum,
5257 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005258 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005261 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 beep_flush();
5264 break;
5265 }
5266 first_par_line = FALSE;
5267 /* If the line is getting long, format it next time */
5268 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5269 force_format = TRUE;
5270 else
5271 force_format = FALSE;
5272 }
5273 }
5274 line_breakcheck();
5275 }
5276}
5277
5278/*
5279 * Return TRUE if line "lnum" ends in a white character.
5280 */
5281 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005282ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 char_u *s = ml_get(lnum);
5285 size_t l;
5286
5287 if (*s == NUL)
5288 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005289 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 * invocation may call function multiple times". */
5291 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005292 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293}
5294
5295/*
5296 * Blank lines, and lines containing only the comment leader, are left
5297 * untouched by the formatting. The function returns TRUE in this
5298 * case. It also returns TRUE when a line starts with the end of a comment
5299 * ('e' in comment flags), so that this line is skipped, and not joined to the
5300 * previous line. A new paragraph starts after a blank line, or when the
5301 * comment leader changes -- webb.
5302 */
5303#ifdef FEAT_COMMENTS
5304 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305fmt_check_par(
5306 linenr_T lnum,
5307 int *leader_len,
5308 char_u **leader_flags,
5309 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 char_u *flags = NULL; /* init for GCC */
5312 char_u *ptr;
5313
5314 ptr = ml_get(lnum);
5315 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005316 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 else
5318 *leader_len = 0;
5319
5320 if (*leader_len > 0)
5321 {
5322 /*
5323 * Search for 'e' flag in comment leader flags.
5324 */
5325 flags = *leader_flags;
5326 while (*flags && *flags != ':' && *flags != COM_END)
5327 ++flags;
5328 }
5329
5330 return (*skipwhite(ptr + *leader_len) == NUL
5331 || (*leader_len > 0 && *flags == COM_END)
5332 || startPS(lnum, NUL, FALSE));
5333}
5334#else
5335 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5339}
5340#endif
5341
5342/*
5343 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5344 * previous line is in the same paragraph. Used for auto-formatting.
5345 */
5346 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005347paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 char_u *p;
5350#ifdef FEAT_COMMENTS
5351 int leader_len = 0; /* leader len of current line */
5352 char_u *leader_flags = NULL; /* flags for leader of current line */
5353 int next_leader_len; /* leader len of next line */
5354 char_u *next_leader_flags; /* flags for leader of next line */
5355 int do_comments; /* format comments */
5356#endif
5357
5358 if (lnum <= 1)
5359 return TRUE; /* start of the file */
5360
5361 p = ml_get(lnum - 1);
5362 if (*p == NUL)
5363 return TRUE; /* after empty line */
5364
5365#ifdef FEAT_COMMENTS
5366 do_comments = has_format_option(FO_Q_COMS);
5367#endif
5368 if (fmt_check_par(lnum - 1
5369#ifdef FEAT_COMMENTS
5370 , &leader_len, &leader_flags, do_comments
5371#endif
5372 ))
5373 return TRUE; /* after non-paragraph line */
5374
5375 if (fmt_check_par(lnum
5376#ifdef FEAT_COMMENTS
5377 , &next_leader_len, &next_leader_flags, do_comments
5378#endif
5379 ))
5380 return TRUE; /* "lnum" is not a paragraph line */
5381
5382 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5383 return TRUE; /* missing trailing space in previous line. */
5384
5385 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5386 return TRUE; /* numbered item starts in "lnum". */
5387
5388#ifdef FEAT_COMMENTS
5389 if (!same_leader(lnum - 1, leader_len, leader_flags,
5390 next_leader_len, next_leader_flags))
5391 return TRUE; /* change of comment leader. */
5392#endif
5393
5394 return FALSE;
5395}
5396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397/*
5398 * prepare a few things for block mode yank/delete/tilde
5399 *
5400 * for delete:
5401 * - textlen includes the first/last char to be (partly) deleted
5402 * - start/endspaces is the number of columns that are taken by the
5403 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005404 * deleted.
5405 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 * - textlen includes the first/last char to be wholly yanked
5407 * - start/endspaces is the number of columns of the first/last yanked char
5408 * that are to be yanked.
5409 */
5410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005411block_prep(
5412 oparg_T *oap,
5413 struct block_def *bdp,
5414 linenr_T lnum,
5415 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 int incr = 0;
5418 char_u *pend;
5419 char_u *pstart;
5420 char_u *line;
5421 char_u *prev_pstart;
5422 char_u *prev_pend;
5423
5424 bdp->startspaces = 0;
5425 bdp->endspaces = 0;
5426 bdp->textlen = 0;
5427 bdp->start_vcol = 0;
5428 bdp->end_vcol = 0;
5429#ifdef FEAT_VISUALEXTRA
5430 bdp->is_short = FALSE;
5431 bdp->is_oneChar = FALSE;
5432 bdp->pre_whitesp = 0;
5433 bdp->pre_whitesp_c = 0;
5434 bdp->end_char_vcols = 0;
5435#endif
5436 bdp->start_char_vcols = 0;
5437
5438 line = ml_get(lnum);
5439 pstart = line;
5440 prev_pstart = line;
5441 while (bdp->start_vcol < oap->start_vcol && *pstart)
5442 {
5443 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005444 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 bdp->start_vcol += incr;
5446#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005447 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 bdp->pre_whitesp += incr;
5450 bdp->pre_whitesp_c++;
5451 }
5452 else
5453 {
5454 bdp->pre_whitesp = 0;
5455 bdp->pre_whitesp_c = 0;
5456 }
5457#endif
5458 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005459 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461 bdp->start_char_vcols = incr;
5462 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5463 {
5464 bdp->end_vcol = bdp->start_vcol;
5465#ifdef FEAT_VISUALEXTRA
5466 bdp->is_short = TRUE;
5467#endif
5468 if (!is_del || oap->op_type == OP_APPEND)
5469 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5470 }
5471 else
5472 {
5473 /* notice: this converts partly selected Multibyte characters to
5474 * spaces, too. */
5475 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5476 if (is_del && bdp->startspaces)
5477 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5478 pend = pstart;
5479 bdp->end_vcol = bdp->start_vcol;
5480 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5481 {
5482#ifdef FEAT_VISUALEXTRA
5483 bdp->is_oneChar = TRUE;
5484#endif
5485 if (oap->op_type == OP_INSERT)
5486 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5487 else if (oap->op_type == OP_APPEND)
5488 {
5489 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5490 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5491 }
5492 else
5493 {
5494 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5495 if (is_del && oap->op_type != OP_LSHIFT)
5496 {
5497 /* just putting the sum of those two into
5498 * bdp->startspaces doesn't work for Visual replace,
5499 * so we have to split the tab in two */
5500 bdp->startspaces = bdp->start_char_vcols
5501 - (bdp->start_vcol - oap->start_vcol);
5502 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5503 }
5504 }
5505 }
5506 else
5507 {
5508 prev_pend = pend;
5509 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5510 {
5511 /* Count a tab for what it's worth (if list mode not on) */
5512 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005513 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 bdp->end_vcol += incr;
5515 }
5516 if (bdp->end_vcol <= oap->end_vcol
5517 && (!is_del
5518 || oap->op_type == OP_APPEND
5519 || oap->op_type == OP_REPLACE)) /* line too short */
5520 {
5521#ifdef FEAT_VISUALEXTRA
5522 bdp->is_short = TRUE;
5523#endif
5524 /* Alternative: include spaces to fill up the block.
5525 * Disadvantage: can lead to trailing spaces when the line is
5526 * short where the text is put */
5527 /* if (!is_del || oap->op_type == OP_APPEND) */
5528 if (oap->op_type == OP_APPEND || virtual_op)
5529 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005530 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 else
5532 bdp->endspaces = 0; /* replace doesn't add characters */
5533 }
5534 else if (bdp->end_vcol > oap->end_vcol)
5535 {
5536 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5537 if (!is_del && bdp->endspaces)
5538 {
5539 bdp->endspaces = incr - bdp->endspaces;
5540 if (pend != pstart)
5541 pend = prev_pend;
5542 }
5543 }
5544 }
5545#ifdef FEAT_VISUALEXTRA
5546 bdp->end_char_vcols = incr;
5547#endif
5548 if (is_del && bdp->startspaces)
5549 pstart = prev_pstart;
5550 bdp->textlen = (int)(pend - pstart);
5551 }
5552 bdp->textcol = (colnr_T) (pstart - line);
5553 bdp->textstart = pstart;
5554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005557 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005560op_addsub(
5561 oparg_T *oap,
5562 linenr_T Prenum1, /* Amount of add/subtract */
5563 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005565 pos_T pos;
5566 struct block_def bd;
5567 int change_cnt = 0;
5568 linenr_T amount = Prenum1;
5569
5570 if (!VIsual_active)
5571 {
5572 pos = curwin->w_cursor;
5573 if (u_save_cursor() == FAIL)
5574 return;
5575 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5576 if (change_cnt)
5577 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5578 }
5579 else
5580 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005581 int one_change;
5582 int length;
5583 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005584
5585 if (u_save((linenr_T)(oap->start.lnum - 1),
5586 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5587 return;
5588
5589 pos = oap->start;
5590 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5591 {
5592 if (oap->block_mode) /* Visual block mode */
5593 {
5594 block_prep(oap, &bd, pos.lnum, FALSE);
5595 pos.col = bd.textcol;
5596 length = bd.textlen;
5597 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005598 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005599 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005600 curwin->w_cursor.col = 0;
5601 pos.col = 0;
5602 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5603 }
5604 else /* oap->motion_type == MCHAR */
5605 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005606 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005607 dec(&(oap->end));
5608 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5609 pos.col = 0;
5610 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005611 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005612 pos.col += oap->start.col;
5613 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005614 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005615 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005616 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005617 length = (int)STRLEN(ml_get(oap->end.lnum));
5618 if (oap->end.col >= length)
5619 oap->end.col = length - 1;
5620 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005621 }
5622 }
5623 one_change = do_addsub(oap->op_type, &pos, length, amount);
5624 if (one_change)
5625 {
5626 /* Remember the start position of the first change. */
5627 if (change_cnt == 0)
5628 startpos = curbuf->b_op_start;
5629 ++change_cnt;
5630 }
5631
5632#ifdef FEAT_NETBEANS_INTG
5633 if (netbeans_active() && one_change)
5634 {
5635 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5636
5637 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5638 netbeans_inserted(curbuf, pos.lnum, pos.col,
5639 &ptr[pos.col], length);
5640 }
5641#endif
5642 if (g_cmd && one_change)
5643 amount += Prenum1;
5644 }
5645 if (change_cnt)
5646 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5647
5648 if (!change_cnt && oap->is_VIsual)
5649 /* No change: need to remove the Visual selection */
5650 redraw_curbuf_later(INVERTED);
5651
5652 /* Set '[ mark if something changed. Keep the last end
5653 * position from do_addsub(). */
5654 if (change_cnt > 0)
5655 curbuf->b_op_start = startpos;
5656
5657 if (change_cnt > p_report)
5658 {
5659 if (change_cnt == 1)
5660 MSG(_("1 line changed"));
5661 else
5662 smsg((char_u *)_("%ld lines changed"), change_cnt);
5663 }
5664 }
5665}
5666
5667/*
5668 * Add or subtract 'Prenum1' from a number in a line
5669 * op_type is OP_NR_ADD or OP_NR_SUB
5670 *
5671 * Returns TRUE if some character was changed.
5672 */
5673 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005674do_addsub(
5675 int op_type,
5676 pos_T *pos,
5677 int length,
5678 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005679{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int col;
5681 char_u *buf1;
5682 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005683 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005685 uvarnumber_T n;
5686 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 char_u *ptr;
5688 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 int todel;
5690 int dohex;
5691 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005692 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 int doalp;
5694 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005696 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005697 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005698 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005699 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005700 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005701 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005702 pos_T startpos;
5703 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
5705 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5706 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005707 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5709
Bram Moolenaard79e5502016-01-10 22:13:02 +01005710 curwin->w_cursor = *pos;
5711 ptr = ml_get(pos->lnum);
5712 col = pos->col;
5713
5714 if (*ptr == NUL)
5715 goto theend;
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 /*
5718 * First check if we are on a hexadecimal number, after the "0x".
5719 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005720 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005722 if (dobin)
5723 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005724 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005725 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005726#ifdef FEAT_MBYTE
5727 if (has_mbyte)
5728 col -= (*mb_head_off)(ptr, ptr + col);
5729#endif
5730 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005731
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005732 if (dohex)
5733 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005734 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005735 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005736#ifdef FEAT_MBYTE
5737 if (has_mbyte)
5738 col -= (*mb_head_off)(ptr, ptr + col);
5739#endif
5740 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005741
5742 if ( dobin
5743 && dohex
5744 && ! ((col > 0
5745 && (ptr[col] == 'X'
5746 || ptr[col] == 'x')
5747 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005748#ifdef FEAT_MBYTE
5749 && (!has_mbyte ||
5750 !(*mb_head_off)(ptr, ptr + col - 1))
5751#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005752 && vim_isxdigit(ptr[col + 1]))))
5753 {
5754
5755 /* In case of binary/hexadecimal pattern overlap match, rescan */
5756
Bram Moolenaard79e5502016-01-10 22:13:02 +01005757 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005758
5759 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005760 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005761 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 Moolenaar887c1fe2016-01-02 17:56:35 +01005767 }
5768
5769 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005770 && col > 0
5771 && (ptr[col] == 'X'
5772 || ptr[col] == 'x')
5773 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005774#ifdef FEAT_MBYTE
5775 && (!has_mbyte ||
5776 !(*mb_head_off)(ptr, ptr + col - 1))
5777#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005778 && vim_isxdigit(ptr[col + 1])) ||
5779 ( dobin
5780 && col > 0
5781 && (ptr[col] == 'B'
5782 || ptr[col] == 'b')
5783 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005784#ifdef FEAT_MBYTE
5785 && (!has_mbyte ||
5786 !(*mb_head_off)(ptr, ptr + col - 1))
5787#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005788 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005789 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005790 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005791 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005792#ifdef FEAT_MBYTE
5793 if (has_mbyte)
5794 col -= (*mb_head_off)(ptr, ptr + col);
5795#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005796 }
5797 else
5798 {
5799 /*
5800 * Search forward and then backward to find the start of number.
5801 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005802 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005803
5804 while (ptr[col] != NUL
5805 && !vim_isdigit(ptr[col])
5806 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005807 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005808
5809 while (col > 0
5810 && vim_isdigit(ptr[col - 1])
5811 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005812 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005813 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005814#ifdef FEAT_MBYTE
5815 if (has_mbyte)
5816 col -= (*mb_head_off)(ptr, ptr + col);
5817#endif
5818 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005819 }
5820 }
5821
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005822 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005823 {
5824 while (ptr[col] != NUL && length > 0
5825 && !vim_isdigit(ptr[col])
5826 && !(doalp && ASCII_ISALPHA(ptr[col])))
5827 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005828 int mb_len = MB_PTR2LEN(ptr + col);
5829
5830 col += mb_len;
5831 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005832 }
5833
5834 if (length == 0)
5835 goto theend;
5836
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005837 if (col > pos->col && ptr[col - 1] == '-'
5838#ifdef FEAT_MBYTE
5839 && (!has_mbyte ||
5840 !(*mb_head_off)(ptr, ptr + col - 1))
5841#endif
5842 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005843 {
5844 negative = TRUE;
5845 was_positive = FALSE;
5846 }
5847 }
5848
5849 /*
5850 * If a number was found, and saving for undo works, replace the number.
5851 */
5852 firstdigit = ptr[col];
5853 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5854 {
5855 beep_flush();
5856 goto theend;
5857 }
5858
5859 if (doalp && ASCII_ISALPHA(firstdigit))
5860 {
5861 /* decrement or increment alphabetic character */
5862 if (op_type == OP_NR_SUB)
5863 {
5864 if (CharOrd(firstdigit) < Prenum1)
5865 {
5866 if (isupper(firstdigit))
5867 firstdigit = 'A';
5868 else
5869 firstdigit = 'a';
5870 }
5871 else
5872#ifdef EBCDIC
5873 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5874#else
5875 firstdigit -= Prenum1;
5876#endif
5877 }
5878 else
5879 {
5880 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5881 {
5882 if (isupper(firstdigit))
5883 firstdigit = 'Z';
5884 else
5885 firstdigit = 'z';
5886 }
5887 else
5888#ifdef EBCDIC
5889 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5890#else
5891 firstdigit += Prenum1;
5892#endif
5893 }
5894 curwin->w_cursor.col = col;
5895 if (!did_change)
5896 startpos = curwin->w_cursor;
5897 did_change = TRUE;
5898 (void)del_char(FALSE);
5899 ins_char(firstdigit);
5900 endpos = curwin->w_cursor;
5901 curwin->w_cursor.col = col;
5902 }
5903 else
5904 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005905 if (col > 0 && ptr[col - 1] == '-'
5906#ifdef FEAT_MBYTE
5907 && (!has_mbyte ||
5908 !(*mb_head_off)(ptr, ptr + col - 1))
5909#endif
5910 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005911 {
5912 /* negative number */
5913 --col;
5914 negative = TRUE;
5915 }
5916 /* get the number value (unsigned) */
5917 if (visual && VIsual_mode != 'V')
5918 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5919 ? (int)STRLEN(ptr) - col
5920 : length);
5921
5922 vim_str2nr(ptr + col, &pre, &length,
5923 0 + (dobin ? STR2NR_BIN : 0)
5924 + (dooct ? STR2NR_OCT : 0)
5925 + (dohex ? STR2NR_HEX : 0),
5926 NULL, &n, maxlen);
5927
5928 /* ignore leading '-' for hex and octal and bin numbers */
5929 if (pre && negative)
5930 {
5931 ++col;
5932 --length;
5933 negative = FALSE;
5934 }
5935 /* add or subtract */
5936 subtract = FALSE;
5937 if (op_type == OP_NR_SUB)
5938 subtract ^= TRUE;
5939 if (negative)
5940 subtract ^= TRUE;
5941
5942 oldn = n;
5943 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005944 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005945 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005946 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005947 /* handle wraparound for decimal numbers */
5948 if (!pre)
5949 {
5950 if (subtract)
5951 {
5952 if (n > oldn)
5953 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005954 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005955 negative ^= TRUE;
5956 }
5957 }
5958 else
5959 {
5960 /* add */
5961 if (n < oldn)
5962 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005963 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005964 negative ^= TRUE;
5965 }
5966 }
5967 if (n == 0)
5968 negative = FALSE;
5969 }
5970
5971 if (visual && !was_positive && !negative && col > 0)
5972 {
5973 /* need to remove the '-' */
5974 col--;
5975 length++;
5976 }
5977
5978 /*
5979 * Delete the old number.
5980 */
5981 curwin->w_cursor.col = col;
5982 if (!did_change)
5983 startpos = curwin->w_cursor;
5984 did_change = TRUE;
5985 todel = length;
5986 c = gchar_cursor();
5987 /*
5988 * Don't include the '-' in the length, only the length of the
5989 * part after it is kept the same.
5990 */
5991 if (c == '-')
5992 --length;
5993 while (todel-- > 0)
5994 {
5995 if (c < 0x100 && isalpha(c))
5996 {
5997 if (isupper(c))
5998 hexupper = TRUE;
5999 else
6000 hexupper = FALSE;
6001 }
6002 /* del_char() will mark line needing displaying */
6003 (void)del_char(FALSE);
6004 c = gchar_cursor();
6005 }
6006
6007 /*
6008 * Prepare the leading characters in buf1[].
6009 * When there are many leading zeros it could be very long.
6010 * Allocate a bit too much.
6011 */
6012 buf1 = alloc((unsigned)length + NUMBUFLEN);
6013 if (buf1 == NULL)
6014 goto theend;
6015 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02006016 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01006017 {
6018 *ptr++ = '-';
6019 }
6020 if (pre)
6021 {
6022 *ptr++ = '0';
6023 --length;
6024 }
6025 if (pre == 'b' || pre == 'B' ||
6026 pre == 'x' || pre == 'X')
6027 {
6028 *ptr++ = pre;
6029 --length;
6030 }
6031
6032 /*
6033 * Put the number characters in buf2[].
6034 */
6035 if (pre == 'b' || pre == 'B')
6036 {
6037 int i;
6038 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006039 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006040
6041 /* leading zeros */
6042 for (bit = bits; bit > 0; bit--)
6043 if ((n >> (bit - 1)) & 0x1) break;
6044
6045 for (i = 0; bit > 0; bit--)
6046 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6047
6048 buf2[i] = '\0';
6049 }
6050 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006051 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6052 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006053 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006054 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6055 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006056 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006057 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6058 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006059 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006060 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6061 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006062 length -= (int)STRLEN(buf2);
6063
6064 /*
6065 * Adjust number of zeros to the new number of digits, so the
6066 * total length of the number remains the same.
6067 * Don't do this when
6068 * the result may look like an octal number.
6069 */
6070 if (firstdigit == '0' && !(dooct && pre == 0))
6071 while (length-- > 0)
6072 *ptr++ = '0';
6073 *ptr = NUL;
6074 STRCAT(buf1, buf2);
6075 ins_str(buf1); /* insert the new number */
6076 vim_free(buf1);
6077 endpos = curwin->w_cursor;
6078 if (did_change && curwin->w_cursor.col)
6079 --curwin->w_cursor.col;
6080 }
6081
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006082 if (did_change)
6083 {
6084 /* set the '[ and '] marks */
6085 curbuf->b_op_start = startpos;
6086 curbuf->b_op_end = endpos;
6087 if (curbuf->b_op_end.col > 0)
6088 --curbuf->b_op_end.col;
6089 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006090
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006091theend:
6092 if (visual)
6093 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006094 else if (did_change)
6095 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006096
Bram Moolenaard79e5502016-01-10 22:13:02 +01006097 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098}
6099
6100#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006101
6102static yankreg_T *y_read_regs = NULL;
6103
6104#define REG_PREVIOUS 1
6105#define REG_EXEC 2
6106
6107/*
6108 * Prepare for reading viminfo registers when writing viminfo later.
6109 */
6110 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006111prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006112{
6113 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6114 * (int)sizeof(yankreg_T));
6115}
6116
6117 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006118finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006119{
6120 int i;
6121 int j;
6122
6123 if (y_read_regs != NULL)
6124 {
6125 for (i = 0; i < NUM_REGISTERS; ++i)
6126 if (y_read_regs[i].y_array != NULL)
6127 {
6128 for (j = 0; j < y_read_regs[i].y_size; j++)
6129 vim_free(y_read_regs[i].y_array[j]);
6130 vim_free(y_read_regs[i].y_array);
6131 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006132 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006133 }
6134}
6135
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006137read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 int eof;
6140 int do_it = TRUE;
6141 int size;
6142 int limit;
6143 int i;
6144 int set_prev = FALSE;
6145 char_u *str;
6146 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006147 int new_type = MCHAR; /* init to shut up compiler */
6148 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 /* We only get here (hopefully) if line[0] == '"' */
6151 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006152
6153 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (*str == '"')
6155 {
6156 set_prev = TRUE;
6157 str++;
6158 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (!ASCII_ISALNUM(*str) && *str != '-')
6161 {
6162 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6163 return TRUE; /* too many errors, pretend end-of-file */
6164 do_it = FALSE;
6165 }
6166 get_yank_register(*str++, FALSE);
6167 if (!force && y_current->y_array != NULL)
6168 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006169
6170 if (*str == '@')
6171 {
6172 /* "x@: register x used for @@ */
6173 if (force || execreg_lastc == NUL)
6174 execreg_lastc = str[-1];
6175 }
6176
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 size = 0;
6178 limit = 100; /* Optimized for registers containing <= 100 lines */
6179 if (do_it)
6180 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006181 /*
6182 * Build the new register in array[].
6183 * y_array is kept as-is until done.
6184 * The "do_it" flag is reset when something is wrong, in which case
6185 * array[] needs to be freed.
6186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (set_prev)
6188 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006189 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006190 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006192 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006194 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006196 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 /* get the block width; if it's missing we get a zero, which is OK */
6198 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006199 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
6201
6202 while (!(eof = viminfo_readline(virp))
6203 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6204 {
6205 if (do_it)
6206 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006207 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006209 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006211
6212 if (new_array == NULL)
6213 {
6214 do_it = FALSE;
6215 break;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006218 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006220 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223 str = viminfo_readstring(virp, 1, TRUE);
6224 if (str != NULL)
6225 array[size++] = str;
6226 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006227 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 do_it = FALSE;
6229 }
6230 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (do_it)
6233 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006234 /* free y_array[] */
6235 for (i = 0; i < y_current->y_size; i++)
6236 vim_free(y_current->y_array[i]);
6237 vim_free(y_current->y_array);
6238
6239 y_current->y_type = new_type;
6240 y_current->y_width = new_width;
6241 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006242 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (size == 0)
6244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 y_current->y_array = NULL;
6246 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006249 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 y_current->y_array =
6251 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6252 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006253 {
6254 if (y_current->y_array == NULL)
6255 vim_free(array[i]);
6256 else
6257 y_current->y_array[i] = array[i];
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006261 else
6262 {
6263 /* Free array[] if it was filled. */
6264 for (i = 0; i < size; i++)
6265 vim_free(array[i]);
6266 }
6267 vim_free(array);
6268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 return eof;
6270}
6271
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006272/*
6273 * Accept a new style register line from the viminfo, store it when it's new.
6274 */
6275 void
6276handle_viminfo_register(garray_T *values, int force)
6277{
6278 bval_T *vp = (bval_T *)values->ga_data;
6279 int flags;
6280 int name;
6281 int type;
6282 int linecount;
6283 int width;
6284 time_t timestamp;
6285 yankreg_T *y_ptr;
6286 int i;
6287
6288 /* Check the format:
6289 * |{bartype},{flags},{name},{type},
6290 * {linecount},{width},{timestamp},"line1","line2"
6291 */
6292 if (values->ga_len < 6
6293 || vp[0].bv_type != BVAL_NR
6294 || vp[1].bv_type != BVAL_NR
6295 || vp[2].bv_type != BVAL_NR
6296 || vp[3].bv_type != BVAL_NR
6297 || vp[4].bv_type != BVAL_NR
6298 || vp[5].bv_type != BVAL_NR)
6299 return;
6300 flags = vp[0].bv_nr;
6301 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006302 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006303 return;
6304 type = vp[2].bv_nr;
6305 if (type != MCHAR && type != MLINE && type != MBLOCK)
6306 return;
6307 linecount = vp[3].bv_nr;
6308 if (values->ga_len < 6 + linecount)
6309 return;
6310 width = vp[4].bv_nr;
6311 if (width < 0)
6312 return;
6313
6314 if (y_read_regs != NULL)
6315 /* Reading viminfo for merging and writing. Store the register
6316 * content, don't update the current registers. */
6317 y_ptr = &y_read_regs[name];
6318 else
6319 y_ptr = &y_regs[name];
6320
6321 /* Do not overwrite unless forced or the timestamp is newer. */
6322 timestamp = (time_t)vp[5].bv_nr;
6323 if (y_ptr->y_array != NULL && !force
6324 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6325 return;
6326
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006327 if (y_ptr->y_array != NULL)
6328 for (i = 0; i < y_ptr->y_size; i++)
6329 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006330 vim_free(y_ptr->y_array);
6331
6332 if (y_read_regs == NULL)
6333 {
6334 if (flags & REG_PREVIOUS)
6335 y_previous = y_ptr;
6336 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6337 execreg_lastc = get_register_name(name);
6338 }
6339 y_ptr->y_type = type;
6340 y_ptr->y_width = width;
6341 y_ptr->y_size = linecount;
6342 y_ptr->y_time_set = timestamp;
6343 if (linecount == 0)
6344 y_ptr->y_array = NULL;
6345 else
6346 {
6347 y_ptr->y_array =
6348 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6349 for (i = 0; i < linecount; i++)
6350 {
6351 if (vp[i + 6].bv_allocated)
6352 {
6353 y_ptr->y_array[i] = vp[i + 6].bv_string;
6354 vp[i + 6].bv_string = NULL;
6355 }
6356 else
6357 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6358 }
6359 }
6360}
6361
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006363write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006365 int i, j;
6366 char_u *type;
6367 char_u c;
6368 int num_lines;
6369 int max_num_lines;
6370 int max_kbyte;
6371 long len;
6372 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
Bram Moolenaar64404472010-06-26 06:24:45 +02006374 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 /* Get '<' value, use old '"' value if '<' is not found. */
6377 max_num_lines = get_viminfo_parameter('<');
6378 if (max_num_lines < 0)
6379 max_num_lines = get_viminfo_parameter('"');
6380 if (max_num_lines == 0)
6381 return;
6382 max_kbyte = get_viminfo_parameter('s');
6383 if (max_kbyte == 0)
6384 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 for (i = 0; i < NUM_REGISTERS; i++)
6387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388#ifdef FEAT_CLIPBOARD
6389 /* Skip '*'/'+' register, we don't want them back next time */
6390 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6391 continue;
6392#endif
6393#ifdef FEAT_DND
6394 /* Neither do we want the '~' register */
6395 if (i == TILDE_REGISTER)
6396 continue;
6397#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006398 /* When reading viminfo for merging and writing: Use the register from
6399 * viminfo if it's newer. */
6400 if (y_read_regs != NULL
6401 && y_read_regs[i].y_array != NULL
6402 && (y_regs[i].y_array == NULL ||
6403 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6404 y_ptr = &y_read_regs[i];
6405 else if (y_regs[i].y_array == NULL)
6406 continue;
6407 else
6408 y_ptr = &y_regs[i];
6409
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006410 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006411 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006412 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006413 || (num_lines == 1 && y_ptr->y_type == MCHAR
6414 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006415 continue;
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (max_kbyte > 0)
6418 {
6419 /* Skip register if there is more text than the maximum size. */
6420 len = 0;
6421 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006422 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (len > (long)max_kbyte * 1024L)
6424 continue;
6425 }
6426
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006427 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
6429 case MLINE:
6430 type = (char_u *)"LINE";
6431 break;
6432 case MCHAR:
6433 type = (char_u *)"CHAR";
6434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 case MBLOCK:
6436 type = (char_u *)"BLOCK";
6437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 default:
6439 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006440 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 emsg(IObuff);
6442 type = (char_u *)"LINE";
6443 break;
6444 }
6445 if (y_previous == &y_regs[i])
6446 fprintf(fp, "\"");
6447 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006448 fprintf(fp, "\"%c", c);
6449 if (c == execreg_lastc)
6450 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006451 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /* If max_num_lines < 0, then we save ALL the lines in the register */
6454 if (max_num_lines > 0 && num_lines > max_num_lines)
6455 num_lines = max_num_lines;
6456 for (j = 0; j < num_lines; j++)
6457 {
6458 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006459 viminfo_writestring(fp, y_ptr->y_array[j]);
6460 }
6461
6462 {
6463 int flags = 0;
6464 int remaining;
6465
6466 /* New style with a bar line. Format:
6467 * |{bartype},{flags},{name},{type},
6468 * {linecount},{width},{timestamp},"line1","line2"
6469 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006470 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006471 */
6472 if (y_previous == &y_regs[i])
6473 flags |= REG_PREVIOUS;
6474 if (c == execreg_lastc)
6475 flags |= REG_EXEC;
6476 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6477 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6478 (long)y_ptr->y_time_set);
6479 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6480 remaining = LSIZE - 71;
6481 for (j = 0; j < num_lines; j++)
6482 {
6483 putc(',', fp);
6484 --remaining;
6485 remaining = barline_writestring(fp, y_ptr->y_array[j],
6486 remaining);
6487 }
6488 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 }
6490 }
6491}
6492#endif /* FEAT_VIMINFO */
6493
6494#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6495/*
6496 * SELECTION / PRIMARY ('*')
6497 *
6498 * Text selection stuff that uses the GUI selection register '*'. When using a
6499 * GUI this may be text from another window, otherwise it is the last text we
6500 * had highlighted with VIsual mode. With mouse support, clicking the middle
6501 * button performs the paste, otherwise you will need to do <"*p>. "
6502 * If not under X, it is synonymous with the clipboard register '+'.
6503 *
6504 * X CLIPBOARD ('+')
6505 *
6506 * Text selection stuff that uses the GUI clipboard register '+'.
6507 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6508 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6509 * otherwise you will need to do <"+p>. "
6510 * If not under X, it is synonymous with the selection register '*'.
6511 */
6512
6513/*
6514 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006515 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 * only exist while the owning application exists, so we write to the
6517 * permanent (while X runs) store CUT_BUFFER0.
6518 * Dump the CLIPBOARD selection if we own it (it's logically the more
6519 * 'permanent' of the two), otherwise the PRIMARY one.
6520 * For now, use a hard-coded sanity limit of 1Mb of data.
6521 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006522#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006524x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 Display *dpy;
6527 char_u *str = NULL;
6528 long_u len = 0;
6529 int motion_type = -1;
6530
6531# ifdef FEAT_GUI
6532 if (gui.in_use)
6533 dpy = X_DISPLAY;
6534 else
6535# endif
6536# ifdef FEAT_XCLIPBOARD
6537 dpy = xterm_dpy;
6538# else
6539 return;
6540# endif
6541
6542 /* Get selection to export */
6543 if (clip_plus.owned)
6544 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6545 else if (clip_star.owned)
6546 motion_type = clip_convert_selection(&str, &len, &clip_star);
6547
6548 /* Check it's OK */
6549 if (dpy != NULL && str != NULL && motion_type >= 0
6550 && len < 1024*1024 && len > 0)
6551 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006552#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006553 int ok = TRUE;
6554
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006555 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6556 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6557 * encoding conversion usually doesn't work, so keep the text as-is.
6558 */
6559 if (has_mbyte)
6560 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006561 vimconv_T vc;
6562
6563 vc.vc_type = CONV_NONE;
6564 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6565 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006566 int intlen = len;
6567 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006568
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006569 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006570 conv_str = string_convert(&vc, str, &intlen);
6571 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006572 if (conv_str != NULL)
6573 {
6574 vim_free(str);
6575 str = conv_str;
6576 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006577 else
6578 {
6579 ok = FALSE;
6580 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006581 convert_setup(&vc, NULL, NULL);
6582 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006583 else
6584 {
6585 ok = FALSE;
6586 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006587 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006588
6589 /* Do not store the string if conversion failed. Better to use any
6590 * other selection than garbled text. */
6591 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006592#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006593 {
6594 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6595 XFlush(dpy);
6596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 }
6598
6599 vim_free(str);
6600}
6601#endif
6602
6603 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006604clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006606 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608 if (cbd == &clip_plus)
6609 y_current = &y_regs[PLUS_REGISTER];
6610 else
6611 y_current = &y_regs[STAR_REGISTER];
6612 free_yank_all();
6613 y_current->y_size = 0;
6614 y_current = y_ptr;
6615}
6616
6617/*
6618 * Get the selected text and put it in the gui selection register '*' or '+'.
6619 */
6620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006621clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006623 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 pos_T old_visual;
6626 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 colnr_T old_curswant;
6628 int old_set_curswant;
6629 pos_T old_op_start, old_op_end;
6630 oparg_T oa;
6631 cmdarg_T ca;
6632
6633 if (cbd->owned)
6634 {
6635 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6636 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6637 return;
6638
6639 /* Get the text between clip_star.start & clip_star.end */
6640 old_y_previous = y_previous;
6641 old_y_current = y_current;
6642 old_cursor = curwin->w_cursor;
6643 old_curswant = curwin->w_curswant;
6644 old_set_curswant = curwin->w_set_curswant;
6645 old_op_start = curbuf->b_op_start;
6646 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 old_visual = VIsual;
6648 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 clear_oparg(&oa);
6650 oa.regname = (cbd == &clip_plus ? '+' : '*');
6651 oa.op_type = OP_YANK;
6652 vim_memset(&ca, 0, sizeof(ca));
6653 ca.oap = &oa;
6654 ca.cmdchar = 'y';
6655 ca.count1 = 1;
6656 ca.retval = CA_NO_ADJ_OP_END;
6657 do_pending_operator(&ca, 0, TRUE);
6658 y_previous = old_y_previous;
6659 y_current = old_y_current;
6660 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006661 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 curwin->w_curswant = old_curswant;
6663 curwin->w_set_curswant = old_set_curswant;
6664 curbuf->b_op_start = old_op_start;
6665 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 VIsual = old_visual;
6667 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006669 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 clip_free_selection(cbd);
6672
6673 /* Try to get selected text from another window */
6674 clip_gen_request_selection(cbd);
6675 }
6676}
6677
Bram Moolenaard44347f2011-06-19 01:14:29 +02006678/*
6679 * Convert from the GUI selection string into the '*'/'+' register.
6680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682clip_yank_selection(
6683 int type,
6684 char_u *str,
6685 long len,
6686 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006688 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690 if (cbd == &clip_plus)
6691 y_ptr = &y_regs[PLUS_REGISTER];
6692 else
6693 y_ptr = &y_regs[STAR_REGISTER];
6694
6695 clip_free_selection(cbd);
6696
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006697 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
6700/*
6701 * Convert the '*'/'+' register into a GUI selection string returned in *str
6702 * with length *len.
6703 * Returns the motion type, or -1 for failure.
6704 */
6705 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 char_u *p;
6709 int lnum;
6710 int i, j;
6711 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006712 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714 if (cbd == &clip_plus)
6715 y_ptr = &y_regs[PLUS_REGISTER];
6716 else
6717 y_ptr = &y_regs[STAR_REGISTER];
6718
6719#ifdef USE_CRNL
6720 eolsize = 2;
6721#else
6722 eolsize = 1;
6723#endif
6724
6725 *str = NULL;
6726 *len = 0;
6727 if (y_ptr->y_array == NULL)
6728 return -1;
6729
6730 for (i = 0; i < y_ptr->y_size; i++)
6731 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6732
6733 /*
6734 * Don't want newline character at end of last line if we're in MCHAR mode.
6735 */
6736 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6737 *len -= eolsize;
6738
6739 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6740 if (p == NULL)
6741 return -1;
6742 lnum = 0;
6743 for (i = 0, j = 0; i < (int)*len; i++, j++)
6744 {
6745 if (y_ptr->y_array[lnum][j] == '\n')
6746 p[i] = NUL;
6747 else if (y_ptr->y_array[lnum][j] == NUL)
6748 {
6749#ifdef USE_CRNL
6750 p[i++] = '\r';
6751#endif
6752#ifdef USE_CR
6753 p[i] = '\r';
6754#else
6755 p[i] = '\n';
6756#endif
6757 lnum++;
6758 j = -1;
6759 }
6760 else
6761 p[i] = y_ptr->y_array[lnum][j];
6762 }
6763 return y_ptr->y_type;
6764}
6765
6766
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767/*
6768 * If we have written to a clipboard register, send the text to the clipboard.
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6774 {
6775 clip_own_selection(&clip_star);
6776 clip_gen_set_selection(&clip_star);
6777 }
6778 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6779 {
6780 clip_own_selection(&clip_plus);
6781 clip_gen_set_selection(&clip_plus);
6782 }
6783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785#endif /* FEAT_CLIPBOARD || PROTO */
6786
6787
6788#if defined(FEAT_DND) || defined(PROTO)
6789/*
6790 * Replace the contents of the '~' register with str.
6791 */
6792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006795 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796
6797 curr = y_current;
6798 y_current = &y_regs[TILDE_REGISTER];
6799 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006800 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 y_current = curr;
6802}
6803#endif
6804
6805
6806#if defined(FEAT_EVAL) || defined(PROTO)
6807/*
6808 * Return the type of a register.
6809 * Used for getregtype()
6810 * Returns MAUTO for error.
6811 */
6812 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006813get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 switch (regname)
6816 {
6817 case '%': /* file name */
6818 case '#': /* alternate file name */
6819 case '=': /* expression */
6820 case ':': /* last command line */
6821 case '/': /* last search-pattern */
6822 case '.': /* last inserted text */
6823#ifdef FEAT_SEARCHPATH
6824 case Ctrl_F: /* Filename under cursor */
6825 case Ctrl_P: /* Path under cursor, expand via "path" */
6826#endif
6827 case Ctrl_W: /* word under cursor */
6828 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6829 case '_': /* black hole: always empty */
6830 return MCHAR;
6831 }
6832
6833#ifdef FEAT_CLIPBOARD
6834 regname = may_get_selection(regname);
6835#endif
6836
Bram Moolenaar32b92012014-01-14 12:33:36 +01006837 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006838 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 get_yank_register(regname, FALSE);
6841
6842 if (y_current->y_array != NULL)
6843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (reglen != NULL && y_current->y_type == MBLOCK)
6845 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 return y_current->y_type;
6847 }
6848 return MAUTO;
6849}
6850
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006851static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006852
6853/*
6854 * When "flags" has GREG_LIST return a list with text "s".
6855 * Otherwise just return "s".
6856 */
6857 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006859{
6860 if (flags & GREG_LIST)
6861 {
6862 list_T *list = list_alloc();
6863
6864 if (list != NULL)
6865 {
6866 if (list_append_string(list, NULL, -1) == FAIL)
6867 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006868 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006869 return NULL;
6870 }
6871 list->lv_first->li_tv.vval.v_string = s;
6872 }
6873 return (char_u *)list;
6874 }
6875 return s;
6876}
6877
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878/*
6879 * Return the contents of a register as a single allocated string.
6880 * Used for "@r" in expressions and for getreg().
6881 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006882 * Flags:
6883 * GREG_NO_EXPR Do not allow expression register
6884 * GREG_EXPR_SRC For the expression register: return expression itself,
6885 * not the result of its evaluation.
6886 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 */
6888 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 long i;
6892 char_u *retval;
6893 int allocated;
6894 long len;
6895
6896 /* Don't allow using an expression register inside an expression */
6897 if (regname == '=')
6898 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006899 if (flags & GREG_NO_EXPR)
6900 return NULL;
6901 if (flags & GREG_EXPR_SRC)
6902 return getreg_wrap_one_line(get_expr_line_src(), flags);
6903 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
6906 if (regname == '@') /* "@@" is used for unnamed register */
6907 regname = '"';
6908
6909 /* check for valid regname */
6910 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6911 return NULL;
6912
6913#ifdef FEAT_CLIPBOARD
6914 regname = may_get_selection(regname);
6915#endif
6916
6917 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6918 {
6919 if (retval == NULL)
6920 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006921 if (allocated)
6922 return getreg_wrap_one_line(retval, flags);
6923 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
6925
6926 get_yank_register(regname, FALSE);
6927 if (y_current->y_array == NULL)
6928 return NULL;
6929
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006930 if (flags & GREG_LIST)
6931 {
6932 list_T *list = list_alloc();
6933 int error = FALSE;
6934
6935 if (list == NULL)
6936 return NULL;
6937 for (i = 0; i < y_current->y_size; ++i)
6938 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6939 error = TRUE;
6940 if (error)
6941 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006942 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006943 return NULL;
6944 }
6945 return (char_u *)list;
6946 }
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 /*
6949 * Compute length of resulting string.
6950 */
6951 len = 0;
6952 for (i = 0; i < y_current->y_size; ++i)
6953 {
6954 len += (long)STRLEN(y_current->y_array[i]);
6955 /*
6956 * Insert a newline between lines and after last line if
6957 * y_type is MLINE.
6958 */
6959 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6960 ++len;
6961 }
6962
6963 retval = lalloc(len + 1, TRUE);
6964
6965 /*
6966 * Copy the lines of the yank register into the string.
6967 */
6968 if (retval != NULL)
6969 {
6970 len = 0;
6971 for (i = 0; i < y_current->y_size; ++i)
6972 {
6973 STRCPY(retval + len, y_current->y_array[i]);
6974 len += (long)STRLEN(retval + len);
6975
6976 /*
6977 * Insert a NL between lines and after the last line if y_type is
6978 * MLINE.
6979 */
6980 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6981 retval[len++] = '\n';
6982 }
6983 retval[len] = NUL;
6984 }
6985
6986 return retval;
6987}
6988
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006989 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006990init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006991 int name,
6992 yankreg_T **old_y_previous,
6993 yankreg_T **old_y_current,
6994 int must_append,
6995 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006996{
6997 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6998 {
6999 emsg_invreg(name);
7000 return FAIL;
7001 }
7002
7003 /* Don't want to change the current (unnamed) register */
7004 *old_y_previous = y_previous;
7005 *old_y_current = y_current;
7006
7007 get_yank_register(name, TRUE);
7008 if (!y_append && !must_append)
7009 free_yank_all();
7010 return OK;
7011}
7012
7013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007014finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007015 int name,
7016 yankreg_T *old_y_previous,
7017 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007018{
7019# ifdef FEAT_CLIPBOARD
7020 /* Send text of clipboard register to the clipboard. */
7021 may_set_selection();
7022# endif
7023
7024 /* ':let @" = "val"' should change the meaning of the "" register */
7025 if (name != '"')
7026 y_previous = old_y_previous;
7027 y_current = old_y_current;
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * Store string "str" in register "name".
7032 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7033 * If "must_append" is TRUE, always append to the register. Otherwise append
7034 * if "name" is an uppercase letter.
7035 * Note: "maxlen" and "must_append" don't work for the "/" register.
7036 * Careful: 'str' is modified, you may have to use a copy!
7037 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7038 */
7039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007040write_reg_contents(
7041 int name,
7042 char_u *str,
7043 int maxlen,
7044 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7047}
7048
7049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007050write_reg_contents_lst(
7051 int name,
7052 char_u **strings,
7053 int maxlen UNUSED,
7054 int must_append,
7055 int yank_type,
7056 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007057{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007058 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007059
7060 if (name == '/'
7061#ifdef FEAT_EVAL
7062 || name == '='
7063#endif
7064 )
7065 {
7066 char_u *s;
7067
7068 if (strings[0] == NULL)
7069 s = (char_u *)"";
7070 else if (strings[1] != NULL)
7071 {
7072 EMSG(_("E883: search pattern and expression register may not "
7073 "contain two or more lines"));
7074 return;
7075 }
7076 else
7077 s = strings[0];
7078 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7079 return;
7080 }
7081
7082 if (name == '_') /* black hole: nothing to do */
7083 return;
7084
7085 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7086 &yank_type) == FAIL)
7087 return;
7088
7089 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7090
7091 finish_write_reg(name, old_y_previous, old_y_current);
7092}
7093
7094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007095write_reg_contents_ex(
7096 int name,
7097 char_u *str,
7098 int maxlen,
7099 int must_append,
7100 int yank_type,
7101 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007103 yankreg_T *old_y_previous, *old_y_current;
7104 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaare7566042005-06-17 22:00:15 +00007106 if (maxlen >= 0)
7107 len = maxlen;
7108 else
7109 len = (long)STRLEN(str);
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 /* Special case: '/' search pattern */
7112 if (name == '/')
7113 {
7114 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7115 return;
7116 }
7117
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007118 if (name == '#')
7119 {
7120 buf_T *buf;
7121
7122 if (VIM_ISDIGIT(*str))
7123 {
7124 int num = atoi((char *)str);
7125
7126 buf = buflist_findnr(num);
7127 if (buf == NULL)
7128 EMSGN(_(e_nobufnr), (long)num);
7129 }
7130 else
7131 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7132 TRUE, FALSE, FALSE));
7133 if (buf == NULL)
7134 return;
7135 curwin->w_alt_fnum = buf->b_fnum;
7136 return;
7137 }
7138
Bram Moolenaare7566042005-06-17 22:00:15 +00007139#ifdef FEAT_EVAL
7140 if (name == '=')
7141 {
7142 char_u *p, *s;
7143
7144 p = vim_strnsave(str, (int)len);
7145 if (p == NULL)
7146 return;
7147 if (must_append)
7148 {
7149 s = concat_str(get_expr_line_src(), p);
7150 vim_free(p);
7151 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007152 }
7153 set_expr_line(p);
7154 return;
7155 }
7156#endif
7157
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (name == '_') /* black hole: nothing to do */
7159 return;
7160
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007161 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7162 &yank_type) == FAIL)
7163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007165 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007167 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169#endif /* FEAT_EVAL */
7170
7171#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7172/*
7173 * Put a string into a register. When the register is not empty, the string
7174 * is appended.
7175 */
7176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007177str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007178 yankreg_T *y_ptr, /* pointer to yank register */
7179 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7180 char_u *str, /* string to put in register */
7181 long len, /* length of string */
7182 long blocklen, /* width of Visual block */
7183 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007185 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int lnum;
7187 long start;
7188 long i;
7189 int extra;
7190 int newlines; /* number of lines added */
7191 int extraline = 0; /* extra line at the end */
7192 int append = FALSE; /* append to last line in register */
7193 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007194 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007198 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 y_ptr->y_size = 0;
7200
Bram Moolenaard44347f2011-06-19 01:14:29 +02007201 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007202 type = ((str_list || (len > 0 && (str[len - 1] == NL
7203 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007204 ? MLINE : MCHAR);
7205 else
7206 type = yank_type;
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /*
7209 * Count the number of lines within the string
7210 */
7211 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007212 if (str_list)
7213 {
7214 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007219 for (i = 0; i < len; i++)
7220 if (str[i] == '\n')
7221 ++newlines;
7222 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7223 {
7224 extraline = 1;
7225 ++newlines; /* count extra newline at the end */
7226 }
7227 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7228 {
7229 append = TRUE;
7230 --newlines; /* uncount newline when appending first line */
7231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007234 /* Without any lines make the register empty. */
7235 if (y_ptr->y_size + newlines == 0)
7236 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007237 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007238 return;
7239 }
7240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 /*
7242 * Allocate an array to hold the pointers to the new register lines.
7243 * If the register was not empty, move the existing lines to the new array.
7244 */
7245 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7246 * sizeof(char_u *), TRUE);
7247 if (pp == NULL) /* out of memory */
7248 return;
7249 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7250 pp[lnum] = y_ptr->y_array[lnum];
7251 vim_free(y_ptr->y_array);
7252 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 /*
7256 * Find the end of each line and save it into the array.
7257 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007258 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007260 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7261 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007262 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007263 pp[lnum] = vim_strnsave(*ss, i);
7264 if (i > maxlen)
7265 maxlen = i;
7266 }
7267 }
7268 else
7269 {
7270 for (start = 0; start < len + extraline; start += i + 1)
7271 {
7272 for (i = start; i < len; ++i) /* find the end of the line */
7273 if (str[i] == '\n')
7274 break;
7275 i -= start; /* i is now length of line */
7276 if (i > maxlen)
7277 maxlen = i;
7278 if (append)
7279 {
7280 --lnum;
7281 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7282 }
7283 else
7284 extra = 0;
7285 s = alloc((unsigned)(i + extra + 1));
7286 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007288 if (extra)
7289 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7290 if (append)
7291 vim_free(y_ptr->y_array[lnum]);
7292 if (i)
7293 mch_memmove(s + extra, str + start, (size_t)i);
7294 extra += i;
7295 s[extra] = NUL;
7296 y_ptr->y_array[lnum++] = s;
7297 while (--extra >= 0)
7298 {
7299 if (*s == NUL)
7300 *s = '\n'; /* replace NUL with newline */
7301 ++s;
7302 }
7303 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
7306 y_ptr->y_type = type;
7307 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (type == MBLOCK)
7309 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7310 else
7311 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007312#ifdef FEAT_VIMINFO
7313 y_ptr->y_time_set = vim_time();
7314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7317
7318 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007319clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 vim_memset(oap, 0, sizeof(oparg_T));
7322}
7323
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007324static 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 +00007325
7326/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007327 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 *
7329 * "Words" are counted by looking for boundaries between non-space and
7330 * space characters. (it seems to produce results that match 'wc'.)
7331 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007332 * Return value is byte count; word count for the line is added to "*wc".
7333 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 *
7335 * The function will only examine the first "limit" characters in the
7336 * line, stopping if it encounters an end-of-line (NUL byte). In that
7337 * case, eol_size will be added to the character count to account for
7338 * the size of the EOL character.
7339 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007340 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007341line_count_info(
7342 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007343 varnumber_T *wc,
7344 varnumber_T *cc,
7345 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007346 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007348 varnumber_T i;
7349 varnumber_T words = 0;
7350 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 int is_word = 0;
7352
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007353 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 if (is_word)
7356 {
7357 if (vim_isspace(line[i]))
7358 {
7359 words++;
7360 is_word = 0;
7361 }
7362 }
7363 else if (!vim_isspace(line[i]))
7364 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007365 ++chars;
7366#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007367 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007368#else
7369 ++i;
7370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
7372
7373 if (is_word)
7374 words++;
7375 *wc += words;
7376
7377 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007378 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007381 chars += eol_size;
7382 }
7383 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 return i;
7385}
7386
7387/*
7388 * Give some info about the position of the cursor (for "g CTRL-G").
7389 * In Visual mode, give some info about the selected region. (In this case,
7390 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007391 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 */
7393 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007394cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007397 char_u buf1[50];
7398 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007400 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007401#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007402 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007403#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007404 varnumber_T byte_count_cursor = 0;
7405 varnumber_T char_count = 0;
7406 varnumber_T char_count_cursor = 0;
7407 varnumber_T word_count = 0;
7408 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007409 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007410 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 long line_count_selected = 0;
7412 pos_T min_pos, max_pos;
7413 oparg_T oparg;
7414 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416 /*
7417 * Compute the length of the file in characters.
7418 */
7419 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7420 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007421 if (dict == NULL)
7422 {
7423 MSG(_(no_lines_msg));
7424 return;
7425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 else
7428 {
7429 if (get_fileformat(curbuf) == EOL_DOS)
7430 eol_size = 2;
7431 else
7432 eol_size = 1;
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (VIsual_active)
7435 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007436 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
7438 min_pos = VIsual;
7439 max_pos = curwin->w_cursor;
7440 }
7441 else
7442 {
7443 min_pos = curwin->w_cursor;
7444 max_pos = VIsual;
7445 }
7446 if (*p_sel == 'e' && max_pos.col > 0)
7447 --max_pos.col;
7448
7449 if (VIsual_mode == Ctrl_V)
7450 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007451#ifdef FEAT_LINEBREAK
7452 char_u * saved_sbr = p_sbr;
7453
7454 /* Make 'sbr' empty for a moment to get the correct size. */
7455 p_sbr = empty_option;
7456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 oparg.is_VIsual = 1;
7458 oparg.block_mode = TRUE;
7459 oparg.op_type = OP_NOP;
7460 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007461 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007462#ifdef FEAT_LINEBREAK
7463 p_sbr = saved_sbr;
7464#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007465 if (curwin->w_curswant == MAXCOL)
7466 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 /* Swap the start, end vcol if needed */
7468 if (oparg.end_vcol < oparg.start_vcol)
7469 {
7470 oparg.end_vcol += oparg.start_vcol;
7471 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7472 oparg.end_vcol -= oparg.start_vcol;
7473 }
7474 }
7475 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477
7478 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7479 {
7480 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007481 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
7483 ui_breakcheck();
7484 if (got_int)
7485 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007486 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
7488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 /* Do extra processing for VIsual mode. */
7490 if (VIsual_active
7491 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7492 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007493 char_u *s = NULL;
7494 long len = 0L;
7495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 switch (VIsual_mode)
7497 {
7498 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007499#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007503#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007505#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007506 s = bd.textstart;
7507 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 break;
7509 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007510 s = ml_get(lnum);
7511 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 break;
7513 case 'v':
7514 {
7515 colnr_T start_col = (lnum == min_pos.lnum)
7516 ? min_pos.col : 0;
7517 colnr_T end_col = (lnum == max_pos.lnum)
7518 ? max_pos.col - start_col + 1 : MAXCOL;
7519
Bram Moolenaardef9e822004-12-31 20:58:58 +00007520 s = ml_get(lnum) + start_col;
7521 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523 break;
7524 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007525 if (s != NULL)
7526 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007527 byte_count_cursor += line_count_info(s, &word_count_cursor,
7528 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007529 if (lnum == curbuf->b_ml.ml_line_count
7530 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007531 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007532 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007533 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 {
7538 /* In non-visual mode, check for the line the cursor is on */
7539 if (lnum == curwin->w_cursor.lnum)
7540 {
7541 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007542 char_count_cursor += char_count;
7543 byte_count_cursor = byte_count +
7544 line_count_info(ml_get(lnum),
7545 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007546 (varnumber_T)(curwin->w_cursor.col + 1),
7547 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 }
7549 }
7550 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007551 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007552 &char_count, (varnumber_T)MAXCOL,
7553 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 }
7555
7556 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007557 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007558 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaared767a22016-01-03 22:49:16 +01007560 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007562 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007564 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7565 {
7566 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7567 &max_pos.col);
7568 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7569 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7570 }
7571 else
7572 buf1[0] = NUL;
7573
7574 if (char_count_cursor == byte_count_cursor
7575 && char_count == byte_count)
7576 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007577 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007578 buf1, line_count_selected,
7579 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007580 (long long)word_count_cursor,
7581 (long long)word_count,
7582 (long long)byte_count_cursor,
7583 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007584 else
7585 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007586 _("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 +01007587 buf1, line_count_selected,
7588 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007589 (long long)word_count_cursor,
7590 (long long)word_count,
7591 (long long)char_count_cursor,
7592 (long long)char_count,
7593 (long long)byte_count_cursor,
7594 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007597 {
7598 p = ml_get_curline();
7599 validate_virtcol();
7600 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7601 (int)curwin->w_virtcol + 1);
7602 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7603 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
Bram Moolenaared767a22016-01-03 22:49:16 +01007605 if (char_count_cursor == byte_count_cursor
7606 && char_count == byte_count)
7607 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007608 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007609 (char *)buf1, (char *)buf2,
7610 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007612 (long long)word_count_cursor, (long long)word_count,
7613 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007614 else
7615 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007616 _("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 +01007617 (char *)buf1, (char *)buf2,
7618 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007619 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007620 (long long)word_count_cursor, (long long)word_count,
7621 (long long)char_count_cursor, (long long)char_count,
7622 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007623 }
7624 }
7625
Bram Moolenaared767a22016-01-03 22:49:16 +01007626#ifdef FEAT_MBYTE
7627 bom_count = bomb_size();
7628 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007629 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007630 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007631#endif
7632 if (dict == NULL)
7633 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007634 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007635 p = p_shm;
7636 p_shm = (char_u *)"";
7637 msg(IObuff);
7638 p_shm = p;
7639 }
7640 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007641#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007642 if (dict != NULL)
7643 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007644 dict_add_nr_str(dict, "words", word_count, NULL);
7645 dict_add_nr_str(dict, "chars", char_count, NULL);
7646 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007647# ifdef FEAT_MBYTE
7648 + bom_count
7649# endif
7650 , NULL);
7651 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007652 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007653 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007654 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007655 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007656 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659}