blob: 73f73d2f92d600da35650919364797068df0486c [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)
2149/*
2150 * Replace a whole area with one character.
2151 */
2152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002153op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int n, numc;
2156#ifdef FEAT_MBYTE
2157 int num_chars;
2158#endif
2159 char_u *newp, *oldp;
2160 size_t oldlen;
2161 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002162 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002163 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2166 return OK; /* nothing to do */
2167
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002168 if (c == REPLACE_CR_NCHAR)
2169 {
2170 had_ctrl_v_cr = TRUE;
2171 c = CAR;
2172 }
2173 else if (c == REPLACE_NL_NCHAR)
2174 {
2175 had_ctrl_v_cr = TRUE;
2176 c = NL;
2177 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef FEAT_MBYTE
2180 if (has_mbyte)
2181 mb_adjust_opend(oap);
2182#endif
2183
2184 if (u_save((linenr_T)(oap->start.lnum - 1),
2185 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2186 return FAIL;
2187
2188 /*
2189 * block mode replace
2190 */
2191 if (oap->block_mode)
2192 {
2193 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2194 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2195 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002196 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2198 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2199 continue; /* nothing to replace */
2200
2201 /* n == number of extra chars required
2202 * If we split a TAB, it may be replaced by several characters.
2203 * Thus the number of characters may increase!
2204 */
2205#ifdef FEAT_VIRTUALEDIT
2206 /* If the range starts in virtual space, count the initial
2207 * coladd offset as part of "startspaces" */
2208 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2209 {
2210 pos_T vpos;
2211
Bram Moolenaara1381de2009-11-03 15:44:21 +00002212 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 getvpos(&vpos, oap->start_vcol);
2214 bd.startspaces += vpos.coladd;
2215 n = bd.startspaces;
2216 }
2217 else
2218#endif
2219 /* allow for pre spaces */
2220 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2221
2222 /* allow for post spp */
2223 n += (bd.endspaces
2224#ifdef FEAT_VIRTUALEDIT
2225 && !bd.is_oneChar
2226#endif
2227 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2228 /* Figure out how many characters to replace. */
2229 numc = oap->end_vcol - oap->start_vcol + 1;
2230 if (bd.is_short && (!virtual_op || bd.is_MAX))
2231 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2232
2233#ifdef FEAT_MBYTE
2234 /* A double-wide character can be replaced only up to half the
2235 * times. */
2236 if ((*mb_char2cells)(c) > 1)
2237 {
2238 if ((numc & 1) && !bd.is_short)
2239 {
2240 ++bd.endspaces;
2241 ++n;
2242 }
2243 numc = numc / 2;
2244 }
2245
2246 /* Compute bytes needed, move character count to num_chars. */
2247 num_chars = numc;
2248 numc *= (*mb_char2len)(c);
2249#endif
2250 /* oldlen includes textlen, so don't double count */
2251 n += numc - bd.textlen;
2252
2253 oldp = ml_get_curline();
2254 oldlen = STRLEN(oldp);
2255 newp = alloc_check((unsigned)oldlen + 1 + n);
2256 if (newp == NULL)
2257 continue;
2258 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2259 /* copy up to deleted part */
2260 mch_memmove(newp, oldp, (size_t)bd.textcol);
2261 oldp += bd.textcol + bd.textlen;
2262 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002263 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002265 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2266 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002267 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002269#ifdef FEAT_MBYTE
2270 if (has_mbyte)
2271 {
2272 n = (int)STRLEN(newp);
2273 while (--num_chars >= 0)
2274 n += (*mb_char2bytes)(c, newp + n);
2275 }
2276 else
2277#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002278 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002279 if (!bd.is_short)
2280 {
2281 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002282 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002283 /* copy the part after the changed part */
2284 STRMOVE(newp + STRLEN(newp), oldp);
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002289 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002290 after_p = alloc_check(
2291 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002292 if (after_p != NULL)
2293 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295 /* replace the line */
2296 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002297 if (after_p != NULL)
2298 {
2299 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2300 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2301 oap->end.lnum++;
2302 vim_free(after_p);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 }
2306 else
2307 {
2308 /*
2309 * MCHAR and MLINE motion replace.
2310 */
2311 if (oap->motion_type == MLINE)
2312 {
2313 oap->start.col = 0;
2314 curwin->w_cursor.col = 0;
2315 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2316 if (oap->end.col)
2317 --oap->end.col;
2318 }
2319 else if (!oap->inclusive)
2320 dec(&(oap->end));
2321
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002322 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 n = gchar_cursor();
2325 if (n != NUL)
2326 {
2327#ifdef FEAT_MBYTE
2328 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2329 {
2330 /* This is slow, but it handles replacing a single-byte
2331 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002332 if (curwin->w_cursor.lnum == oap->end.lnum)
2333 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 n = State;
2335 State = REPLACE;
2336 ins_char(c);
2337 State = n;
2338 /* Backup to the replaced character. */
2339 dec_cursor();
2340 }
2341 else
2342#endif
2343 {
2344#ifdef FEAT_VIRTUALEDIT
2345 if (n == TAB)
2346 {
2347 int end_vcol = 0;
2348
2349 if (curwin->w_cursor.lnum == oap->end.lnum)
2350 {
2351 /* oap->end has to be recalculated when
2352 * the tab breaks */
2353 end_vcol = getviscol2(oap->end.col,
2354 oap->end.coladd);
2355 }
2356 coladvance_force(getviscol());
2357 if (curwin->w_cursor.lnum == oap->end.lnum)
2358 getvpos(&oap->end, end_vcol);
2359 }
2360#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002361 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363 }
2364#ifdef FEAT_VIRTUALEDIT
2365 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2366 {
2367 int virtcols = oap->end.coladd;
2368
2369 if (curwin->w_cursor.lnum == oap->start.lnum
2370 && oap->start.col == oap->end.col && oap->start.coladd)
2371 virtcols -= oap->start.coladd;
2372
2373 /* oap->end has been trimmed so it's effectively inclusive;
2374 * as a result an extra +1 must be counted so we don't
2375 * trample the NUL byte. */
2376 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2377 curwin->w_cursor.col -= (virtcols + 1);
2378 for (; virtcols >= 0; virtcols--)
2379 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002380 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 if (inc(&curwin->w_cursor) == -1)
2382 break;
2383 }
2384 }
2385#endif
2386
2387 /* Advance to next character, stop at the end of the file. */
2388 if (inc_cursor() == -1)
2389 break;
2390 }
2391 }
2392
2393 curwin->w_cursor = oap->start;
2394 check_cursor();
2395 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2396
2397 /* Set "'[" and "']" marks. */
2398 curbuf->b_op_start = oap->start;
2399 curbuf->b_op_end = oap->end;
2400
2401 return OK;
2402}
2403#endif
2404
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002405static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407/*
2408 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2409 */
2410 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002411op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
2413 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002415 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
2417 if (u_save((linenr_T)(oap->start.lnum - 1),
2418 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2419 return;
2420
2421 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (oap->block_mode) /* Visual block mode */
2423 {
2424 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2425 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002426 int one_change;
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 block_prep(oap, &bd, pos.lnum, FALSE);
2429 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002430 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2431 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002432
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002433#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002434 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2437
Bram Moolenaar009b2592004-10-24 19:18:58 +00002438 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2439 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002441 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 if (did_change)
2446 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2447 }
2448 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 if (oap->motion_type == MLINE)
2451 {
2452 oap->start.col = 0;
2453 pos.col = 0;
2454 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2455 if (oap->end.col)
2456 --oap->end.col;
2457 }
2458 else if (!oap->inclusive)
2459 dec(&(oap->end));
2460
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002461 if (pos.lnum == oap->end.lnum)
2462 did_change = swapchars(oap->op_type, &pos,
2463 oap->end.col - pos.col + 1);
2464 else
2465 for (;;)
2466 {
2467 did_change |= swapchars(oap->op_type, &pos,
2468 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2469 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002470 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002471 break;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (did_change)
2474 {
2475 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2476 0L);
2477#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002478 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 char_u *ptr;
2481 int count;
2482
2483 pos = oap->start;
2484 while (pos.lnum < oap->end.lnum)
2485 {
2486 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002487 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002488 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002490 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 pos.col = 0;
2492 pos.lnum++;
2493 }
2494 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2495 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002496 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002498 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500#endif
2501 }
2502 }
2503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (!did_change && oap->is_VIsual)
2505 /* No change: need to remove the Visual selection */
2506 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508 /*
2509 * Set '[ and '] marks.
2510 */
2511 curbuf->b_op_start = oap->start;
2512 curbuf->b_op_end = oap->end;
2513
2514 if (oap->line_count > p_report)
2515 {
2516 if (oap->line_count == 1)
2517 MSG(_("1 line changed"));
2518 else
2519 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2520 }
2521}
2522
2523/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002524 * Invoke swapchar() on "length" bytes at position "pos".
2525 * "pos" is advanced to just after the changed characters.
2526 * "length" is rounded up to include the whole last multi-byte character.
2527 * Also works correctly when the number of bytes changes.
2528 * Returns TRUE if some character was changed.
2529 */
2530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002531swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002532{
2533 int todo;
2534 int did_change = 0;
2535
2536 for (todo = length; todo > 0; --todo)
2537 {
2538# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002539 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002540 {
2541 int len = (*mb_ptr2len)(ml_get_pos(pos));
2542
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002543 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002544 if (len > 0)
2545 todo -= len - 1;
2546 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002547# endif
2548 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002549 if (inc(pos) == -1) /* at end of file */
2550 break;
2551 }
2552 return did_change;
2553}
2554
2555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 * If op_type == OP_UPPER: make uppercase,
2557 * if op_type == OP_LOWER: make lowercase,
2558 * if op_type == OP_ROT13: do rot13 encoding,
2559 * else swap case of character at 'pos'
2560 * returns TRUE when something actually changed.
2561 */
2562 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002563swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 int c;
2566 int nc;
2567
2568 c = gchar_pos(pos);
2569
2570 /* Only do rot13 encoding for ASCII characters. */
2571 if (c >= 0x80 && op_type == OP_ROT13)
2572 return FALSE;
2573
2574#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002575 if (op_type == OP_UPPER && c == 0xdf
2576 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002577 {
2578 pos_T sp = curwin->w_cursor;
2579
2580 /* Special handling of German sharp s: change to "SS". */
2581 curwin->w_cursor = *pos;
2582 del_char(FALSE);
2583 ins_char('S');
2584 ins_char('S');
2585 curwin->w_cursor = sp;
2586 inc(pos);
2587 }
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2590 return FALSE;
2591#endif
2592 nc = c;
2593 if (MB_ISLOWER(c))
2594 {
2595 if (op_type == OP_ROT13)
2596 nc = ROT13(c, 'a');
2597 else if (op_type != OP_LOWER)
2598 nc = MB_TOUPPER(c);
2599 }
2600 else if (MB_ISUPPER(c))
2601 {
2602 if (op_type == OP_ROT13)
2603 nc = ROT13(c, 'A');
2604 else if (op_type != OP_UPPER)
2605 nc = MB_TOLOWER(c);
2606 }
2607 if (nc != c)
2608 {
2609#ifdef FEAT_MBYTE
2610 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2611 {
2612 pos_T sp = curwin->w_cursor;
2613
2614 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002615 /* don't use del_char(), it also removes composing chars */
2616 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 ins_char(nc);
2618 curwin->w_cursor = sp;
2619 }
2620 else
2621#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002622 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 return TRUE;
2624 }
2625 return FALSE;
2626}
2627
2628#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2629/*
2630 * op_insert - Insert and append operators for Visual mode.
2631 */
2632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002633op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 long ins_len, pre_textlen = 0;
2636 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002637 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 struct block_def bd;
2639 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002640 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /* edit() changes this - record it for OP_APPEND */
2643 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2644
2645 /* vis block is still marked. Get rid of it now. */
2646 curwin->w_cursor.lnum = oap->start.lnum;
2647 update_screen(INVERTED);
2648
2649 if (oap->block_mode)
2650 {
2651#ifdef FEAT_VIRTUALEDIT
2652 /* When 'virtualedit' is used, need to insert the extra spaces before
2653 * doing block_prep(). When only "block" is used, virtual edit is
2654 * already disabled, but still need it when calling
2655 * coladvance_force(). */
2656 if (curwin->w_cursor.coladd > 0)
2657 {
2658 int old_ve_flags = ve_flags;
2659
2660 ve_flags = VE_ALL;
2661 if (u_save_cursor() == FAIL)
2662 return;
2663 coladvance_force(oap->op_type == OP_APPEND
2664 ? oap->end_vcol + 1 : getviscol());
2665 if (oap->op_type == OP_APPEND)
2666 --curwin->w_cursor.col;
2667 ve_flags = old_ve_flags;
2668 }
2669#endif
2670 /* Get the info about the block before entering the text */
2671 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002672 /* Get indent information */
2673 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (oap->op_type == OP_APPEND)
2677 firstline += bd.textlen;
2678 pre_textlen = (long)STRLEN(firstline);
2679 }
2680
2681 if (oap->op_type == OP_APPEND)
2682 {
2683 if (oap->block_mode
2684#ifdef FEAT_VIRTUALEDIT
2685 && curwin->w_cursor.coladd == 0
2686#endif
2687 )
2688 {
2689 /* Move the cursor to the character right of the block. */
2690 curwin->w_set_curswant = TRUE;
2691 while (*ml_get_cursor() != NUL
2692 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2693 ++curwin->w_cursor.col;
2694 if (bd.is_short && !bd.is_MAX)
2695 {
2696 /* First line was too short, make it longer and adjust the
2697 * values in "bd". */
2698 if (u_save_cursor() == FAIL)
2699 return;
2700 for (i = 0; i < bd.endspaces; ++i)
2701 ins_char(' ');
2702 bd.textlen += bd.endspaces;
2703 }
2704 }
2705 else
2706 {
2707 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002708 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002711 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 && oap->start_vcol != oap->end_vcol)
2713 inc_cursor();
2714 }
2715 }
2716
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002717 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002718 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002720 /* When a tab was inserted, and the characters in front of the tab
2721 * have been converted to a tab as well, the column of the cursor
2722 * might have actually been reduced, so need to adjust here. */
2723 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002724 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002725 oap->start = curbuf->b_op_start_orig;
2726
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002727 /* If user has moved off this line, we don't know what to do, so do
2728 * nothing.
2729 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2730 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return;
2732
2733 if (oap->block_mode)
2734 {
2735 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002736 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002737 size_t len;
2738 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002740 /* If indent kicked in, the firstline might have changed
2741 * but only do that, if the indent actually increased. */
2742 ind_post = (colnr_T)getwhitecols_curline();
2743 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2744 {
2745 bd.textcol += ind_post - ind_pre;
2746 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002747 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002748 }
2749
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002750 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002751 * to adjust the block for that. But only do it, if the difference
2752 * does not come from indent kicking in. */
2753 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2754 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002755 {
2756 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002757 && oap->start.col
2758#ifdef FEAT_VIRTUALEDIT
2759 + oap->start.coladd
2760#endif
2761 != curbuf->b_op_start_orig.col
2762#ifdef FEAT_VIRTUALEDIT
2763 + curbuf->b_op_start_orig.coladd
2764#endif
2765 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002766 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002767 int t = getviscol2(curbuf->b_op_start_orig.col,
2768 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002769 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002770 pre_textlen -= t - oap->start_vcol;
2771 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002772 }
2773 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002774 && oap->end.col
2775#ifdef FEAT_VIRTUALEDIT
2776 + oap->end.coladd
2777#endif
2778 >= curbuf->b_op_start_orig.col
2779#ifdef FEAT_VIRTUALEDIT
2780 + curbuf->b_op_start_orig.coladd
2781#endif
2782 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002783 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002784 int t = getviscol2(curbuf->b_op_start_orig.col,
2785 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002786 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002787 /* reset pre_textlen to the value of OP_INSERT */
2788 pre_textlen += bd.textlen;
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 oap->op_type = OP_INSERT;
2792 }
2793 }
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 /*
2796 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002797 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 * Don't do this when "$" used, end-of-line will have changed.
2799 */
2800 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2801 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2802 {
2803 if (oap->op_type == OP_APPEND)
2804 {
2805 pre_textlen += bd2.textlen - bd.textlen;
2806 if (bd2.endspaces)
2807 --bd2.textlen;
2808 }
2809 bd.textcol = bd2.textcol;
2810 bd.textlen = bd2.textlen;
2811 }
2812
2813 /*
2814 * Subsequent calls to ml_get() flush the firstline data - take a
2815 * copy of the required string.
2816 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002817 firstline = ml_get(oap->start.lnum);
2818 len = STRLEN(firstline);
2819 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002821 add += bd.textlen;
2822 if ((size_t)add > len)
2823 firstline += len; // short line, point to the NUL
2824 else
2825 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002826 if (pre_textlen >= 0
2827 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 ins_text = vim_strnsave(firstline, (int)ins_len);
2830 if (ins_text != NULL)
2831 {
2832 /* block handled here */
2833 if (u_save(oap->start.lnum,
2834 (linenr_T)(oap->end.lnum + 1)) == OK)
2835 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2836 &bd);
2837
2838 curwin->w_cursor.col = oap->start.col;
2839 check_cursor();
2840 vim_free(ins_text);
2841 }
2842 }
2843 }
2844}
2845#endif
2846
2847/*
2848 * op_change - handle a change operation
2849 *
2850 * return TRUE if edit() returns because of a CTRL-O command
2851 */
2852 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002853op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 colnr_T l;
2856 int retval;
2857#ifdef FEAT_VISUALEXTRA
2858 long offset;
2859 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002860 long ins_len;
2861 long pre_textlen = 0;
2862 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 char_u *firstline;
2864 char_u *ins_text, *newp, *oldp;
2865 struct block_def bd;
2866#endif
2867
2868 l = oap->start.col;
2869 if (oap->motion_type == MLINE)
2870 {
2871 l = 0;
2872#ifdef FEAT_SMARTINDENT
2873 if (!p_paste && curbuf->b_p_si
2874# ifdef FEAT_CINDENT
2875 && !curbuf->b_p_cin
2876# endif
2877 )
2878 can_si = TRUE; /* It's like opening a new line, do si */
2879#endif
2880 }
2881
2882 /* First delete the text in the region. In an empty buffer only need to
2883 * save for undo */
2884 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2885 {
2886 if (u_save_cursor() == FAIL)
2887 return FALSE;
2888 }
2889 else if (op_delete(oap) == FAIL)
2890 return FALSE;
2891
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002892 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 && !virtual_op)
2894 inc_cursor();
2895
2896#ifdef FEAT_VISUALEXTRA
2897 /* check for still on same line (<CR> in inserted text meaningless) */
2898 /* skip blank lines too */
2899 if (oap->block_mode)
2900 {
2901# ifdef FEAT_VIRTUALEDIT
2902 /* Add spaces before getting the current line length. */
2903 if (virtual_op && (curwin->w_cursor.coladd > 0
2904 || gchar_cursor() == NUL))
2905 coladvance_force(getviscol());
2906# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002907 firstline = ml_get(oap->start.lnum);
2908 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002909 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 bd.textcol = curwin->w_cursor.col;
2911 }
2912#endif
2913
2914#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2915 if (oap->motion_type == MLINE)
2916 fix_indent();
2917#endif
2918
2919 retval = edit(NUL, FALSE, (linenr_T)1);
2920
2921#ifdef FEAT_VISUALEXTRA
2922 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002923 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002925 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002927 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002929 /* Auto-indenting may have changed the indent. If the cursor was past
2930 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002932 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002934 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002935
2936 pre_textlen += new_indent - pre_indent;
2937 bd.textcol += new_indent - pre_indent;
2938 }
2939
2940 ins_len = (long)STRLEN(firstline) - pre_textlen;
2941 if (ins_len > 0)
2942 {
2943 /* Subsequent calls to ml_get() flush the firstline data - take a
2944 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2946 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002947 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2949 linenr++)
2950 {
2951 block_prep(oap, &bd, linenr, TRUE);
2952 if (!bd.is_short || virtual_op)
2953 {
2954# ifdef FEAT_VIRTUALEDIT
2955 pos_T vpos;
2956
2957 /* If the block starts in virtual space, count the
2958 * initial coladd offset as part of "startspaces" */
2959 if (bd.is_short)
2960 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002961 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 else
2965 vpos.coladd = 0;
2966# endif
2967 oldp = ml_get(linenr);
2968 newp = alloc_check((unsigned)(STRLEN(oldp)
2969# ifdef FEAT_VIRTUALEDIT
2970 + vpos.coladd
2971# endif
2972 + ins_len + 1));
2973 if (newp == NULL)
2974 continue;
2975 /* copy up to block start */
2976 mch_memmove(newp, oldp, (size_t)bd.textcol);
2977 offset = bd.textcol;
2978# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002979 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 offset += vpos.coladd;
2981# endif
2982 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2983 offset += ins_len;
2984 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002985 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 ml_replace(linenr, newp, FALSE);
2987 }
2988 }
2989 check_cursor();
2990
2991 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2992 }
2993 vim_free(ins_text);
2994 }
2995 }
2996#endif
2997
2998 return retval;
2999}
3000
3001/*
3002 * set all the yank registers to empty (called from main())
3003 */
3004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003005init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 int i;
3008
3009 for (i = 0; i < NUM_REGISTERS; ++i)
3010 y_regs[i].y_array = NULL;
3011}
3012
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003013#if defined(EXITFREE) || defined(PROTO)
3014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003015clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003016{
3017 int i;
3018
3019 for (i = 0; i < NUM_REGISTERS; ++i)
3020 {
3021 y_current = &y_regs[i];
3022 if (y_current->y_array != NULL)
3023 free_yank_all();
3024 }
3025}
3026#endif
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028/*
3029 * Free "n" lines from the current yank register.
3030 * Called for normal freeing and in case of error.
3031 */
3032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003033free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 if (y_current->y_array != NULL)
3036 {
3037 long i;
3038
3039 for (i = n; --i >= 0; )
3040 {
3041#ifdef AMIGA /* only for very slow machines */
3042 if ((i & 1023) == 1023) /* this may take a while */
3043 {
3044 /*
3045 * This message should never cause a hit-return message.
3046 * Overwrite this message with any next message.
3047 */
3048 ++no_wait_return;
3049 smsg((char_u *)_("freeing %ld lines"), i + 1);
3050 --no_wait_return;
3051 msg_didout = FALSE;
3052 msg_col = 0;
3053 }
3054#endif
3055 vim_free(y_current->y_array[i]);
3056 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003057 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef AMIGA
3059 if (n >= 1000)
3060 MSG("");
3061#endif
3062 }
3063}
3064
3065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003066free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 free_yank(y_current->y_size);
3069}
3070
3071/*
3072 * Yank the text between "oap->start" and "oap->end" into a yank register.
3073 * If we are to append (uppercase register), we first yank into a new yank
3074 * register and then concatenate the old and the new one (so we keep the old
3075 * one in case of out-of-memory).
3076 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003077 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003083 yankreg_T *curr; /* copy of y_current */
3084 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 char_u **new_ptr;
3086 linenr_T lnum; /* current line number */
3087 long j;
3088 int yanktype = oap->motion_type;
3089 long yanklines = oap->line_count;
3090 linenr_T yankendlnum = oap->end.lnum;
3091 char_u *p;
3092 char_u *pnew;
3093 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003094#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003095 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 /* check for read-only register */
3099 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3100 {
3101 beep_flush();
3102 return FAIL;
3103 }
3104 if (oap->regname == '_') /* black hole: nothing to do */
3105 return OK;
3106
3107#ifdef FEAT_CLIPBOARD
3108 if (!clip_star.available && oap->regname == '*')
3109 oap->regname = 0;
3110 else if (!clip_plus.available && oap->regname == '+')
3111 oap->regname = 0;
3112#endif
3113
3114 if (!deleting) /* op_delete() already set y_current */
3115 get_yank_register(oap->regname, TRUE);
3116
3117 curr = y_current;
3118 /* append to existing contents */
3119 if (y_append && y_current->y_array != NULL)
3120 y_current = &newreg;
3121 else
3122 free_yank_all(); /* free previously yanked lines */
3123
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003124 /*
3125 * If the cursor was in column 1 before and after the movement, and the
3126 * operator is not inclusive, the yank is always linewise.
3127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if ( oap->motion_type == MCHAR
3129 && oap->start.col == 0
3130 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003132 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 && oap->end.col == 0
3134 && yanklines > 1)
3135 {
3136 yanktype = MLINE;
3137 --yankendlnum;
3138 --yanklines;
3139 }
3140
3141 y_current->y_size = yanklines;
3142 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3145 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (y_current->y_array == NULL)
3147 {
3148 y_current = curr;
3149 return FAIL;
3150 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003151#ifdef FEAT_VIMINFO
3152 y_current->y_time_set = vim_time();
3153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 y_idx = 0;
3156 lnum = oap->start.lnum;
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (oap->block_mode)
3159 {
3160 /* Visual block mode */
3161 y_current->y_type = MBLOCK; /* set the yank register type */
3162 y_current->y_width = oap->end_vcol - oap->start_vcol;
3163
3164 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3165 y_current->y_width--;
3166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3169 {
3170 switch (y_current->y_type)
3171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 case MBLOCK:
3173 block_prep(oap, &bd, lnum, FALSE);
3174 if (yank_copy_line(&bd, y_idx) == FAIL)
3175 goto fail;
3176 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 case MLINE:
3179 if ((y_current->y_array[y_idx] =
3180 vim_strsave(ml_get(lnum))) == NULL)
3181 goto fail;
3182 break;
3183
3184 case MCHAR:
3185 {
3186 colnr_T startcol = 0, endcol = MAXCOL;
3187#ifdef FEAT_VIRTUALEDIT
3188 int is_oneChar = FALSE;
3189 colnr_T cs, ce;
3190#endif
3191 p = ml_get(lnum);
3192 bd.startspaces = 0;
3193 bd.endspaces = 0;
3194
3195 if (lnum == oap->start.lnum)
3196 {
3197 startcol = oap->start.col;
3198#ifdef FEAT_VIRTUALEDIT
3199 if (virtual_op)
3200 {
3201 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3202 if (ce != cs && oap->start.coladd > 0)
3203 {
3204 /* Part of a tab selected -- but don't
3205 * double-count it. */
3206 bd.startspaces = (ce - cs + 1)
3207 - oap->start.coladd;
3208 startcol++;
3209 }
3210 }
3211#endif
3212 }
3213
3214 if (lnum == oap->end.lnum)
3215 {
3216 endcol = oap->end.col;
3217#ifdef FEAT_VIRTUALEDIT
3218 if (virtual_op)
3219 {
3220 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3221 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3222# ifdef FEAT_MBYTE
3223 /* Don't add space for double-wide
3224 * char; endcol will be on last byte
3225 * of multi-byte char. */
3226 && (*mb_head_off)(p, p + endcol) == 0
3227# endif
3228 ))
3229 {
3230 if (oap->start.lnum == oap->end.lnum
3231 && oap->start.col == oap->end.col)
3232 {
3233 /* Special case: inside a single char */
3234 is_oneChar = TRUE;
3235 bd.startspaces = oap->end.coladd
3236 - oap->start.coladd + oap->inclusive;
3237 endcol = startcol;
3238 }
3239 else
3240 {
3241 bd.endspaces = oap->end.coladd
3242 + oap->inclusive;
3243 endcol -= oap->inclusive;
3244 }
3245 }
3246 }
3247#endif
3248 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003249 if (endcol == MAXCOL)
3250 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (startcol > endcol
3252#ifdef FEAT_VIRTUALEDIT
3253 || is_oneChar
3254#endif
3255 )
3256 bd.textlen = 0;
3257 else
3258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 bd.textlen = endcol - startcol + oap->inclusive;
3260 }
3261 bd.textstart = p + startcol;
3262 if (yank_copy_line(&bd, y_idx) == FAIL)
3263 goto fail;
3264 break;
3265 }
3266 /* NOTREACHED */
3267 }
3268 }
3269
3270 if (curr != y_current) /* append the new block to the old block */
3271 {
3272 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3273 (curr->y_size + y_current->y_size)), TRUE);
3274 if (new_ptr == NULL)
3275 goto fail;
3276 for (j = 0; j < curr->y_size; ++j)
3277 new_ptr[j] = curr->y_array[j];
3278 vim_free(curr->y_array);
3279 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003280#ifdef FEAT_VIMINFO
3281 curr->y_time_set = vim_time();
3282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3285 curr->y_type = MLINE;
3286
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003287 /* Concatenate the last line of the old block with the first line of
3288 * the new block, unless being Vi compatible. */
3289 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
3291 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3292 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3293 if (pnew == NULL)
3294 {
3295 y_idx = y_current->y_size - 1;
3296 goto fail;
3297 }
3298 STRCPY(pnew, curr->y_array[--j]);
3299 STRCAT(pnew, y_current->y_array[0]);
3300 vim_free(curr->y_array[j]);
3301 vim_free(y_current->y_array[0]);
3302 curr->y_array[j++] = pnew;
3303 y_idx = 1;
3304 }
3305 else
3306 y_idx = 0;
3307 while (y_idx < y_current->y_size)
3308 curr->y_array[j++] = y_current->y_array[y_idx++];
3309 curr->y_size = j;
3310 vim_free(y_current->y_array);
3311 y_current = curr;
3312 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003313 if (curwin->w_p_rnu)
3314 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (mess) /* Display message about yank? */
3316 {
3317 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 && yanklines == 1)
3320 yanklines = 0;
3321 /* Some versions of Vi use ">=" here, some don't... */
3322 if (yanklines > p_report)
3323 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003324 char namebuf[100];
3325
3326 if (oap->regname == NUL)
3327 *namebuf = NUL;
3328 else
3329 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003330 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 /* redisplay now, so message is not deleted */
3333 update_topline_redraw();
3334 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003335 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003336 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003337 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003338 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003339 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003340 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003341 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003342 smsg((char_u *)_("block of %ld lines yanked%s"),
3343 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003345 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3346 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 }
3349
3350 /*
3351 * Set "'[" and "']" marks.
3352 */
3353 curbuf->b_op_start = oap->start;
3354 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003355 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003356 {
3357 curbuf->b_op_start.col = 0;
3358 curbuf->b_op_end.col = MAXCOL;
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
3361#ifdef FEAT_CLIPBOARD
3362 /*
3363 * If we were yanking to the '*' register, send result to clipboard.
3364 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3365 * to the '*' register.
3366 */
3367 if (clip_star.available
3368 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003369 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003370 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 if (curr != &(y_regs[STAR_REGISTER]))
3373 /* Copy the text from register 0 to the clipboard register. */
3374 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3375
3376 clip_own_selection(&clip_star);
3377 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003378# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003379 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003380# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382
3383# ifdef FEAT_X11
3384 /*
3385 * If we were yanking to the '+' register, send result to selection.
3386 * Also copy to the '*' register, in case auto-select is off.
3387 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003388 if (clip_plus.available
3389 && (curr == &(y_regs[PLUS_REGISTER])
3390 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003391 && ((clip_unnamed | clip_unnamed_saved) &
3392 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003394 if (curr != &(y_regs[PLUS_REGISTER]))
3395 /* Copy the text from register 0 to the clipboard register. */
3396 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 clip_own_selection(&clip_plus);
3399 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003400 if (!clip_isautosel_star() && !clip_isautosel_plus()
3401 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3404 clip_own_selection(&clip_star);
3405 clip_gen_set_selection(&clip_star);
3406 }
3407 }
3408# endif
3409#endif
3410
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003411#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003412 if (!deleting && has_textyankpost())
3413 yank_do_autocmd(oap, y_current);
3414#endif
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 return OK;
3417
3418fail: /* free the allocated lines */
3419 free_yank(y_idx + 1);
3420 y_current = curr;
3421 return FAIL;
3422}
3423
3424 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 char_u *pnew;
3428
3429 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3430 == NULL)
3431 return FAIL;
3432 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003433 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 pnew += bd->startspaces;
3435 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3436 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003437 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 pnew += bd->endspaces;
3439 *pnew = NUL;
3440 return OK;
3441}
3442
3443#ifdef FEAT_CLIPBOARD
3444/*
3445 * Make a copy of the y_current register to register "reg".
3446 */
3447 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003448copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003450 yankreg_T *curr = y_current;
3451 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 y_current = reg;
3454 free_yank_all();
3455 *y_current = *curr;
3456 y_current->y_array = (char_u **)lalloc_clear(
3457 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3458 if (y_current->y_array == NULL)
3459 y_current->y_size = 0;
3460 else
3461 for (j = 0; j < y_current->y_size; ++j)
3462 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3463 {
3464 free_yank(j);
3465 y_current->y_size = 0;
3466 break;
3467 }
3468 y_current = curr;
3469}
3470#endif
3471
3472/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003473 * Put contents of register "regname" into the text.
3474 * Caller must check "regname" to be valid!
3475 * "flags": PUT_FIXINDENT make indent look nice
3476 * PUT_CURSEND leave cursor after end of new text
3477 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 */
3479 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003480do_put(
3481 int regname,
3482 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3483 long count,
3484 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 char_u *ptr;
3487 char_u *newp, *oldp;
3488 int yanklen;
3489 int totlen = 0; /* init for gcc */
3490 linenr_T lnum;
3491 colnr_T col;
3492 long i; /* index in y_array[] */
3493 int y_type;
3494 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 int oldlen;
3496 long y_width = 0;
3497 colnr_T vcol;
3498 int delcount;
3499 int incr = 0;
3500 long j;
3501 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 char_u **y_array = NULL;
3503 long nr_lines = 0;
3504 pos_T new_cursor;
3505 int indent;
3506 int orig_indent = 0; /* init for gcc */
3507 int indent_diff = 0; /* init for gcc */
3508 int first_indent = TRUE;
3509 int lendiff = 0;
3510 pos_T old_pos;
3511 char_u *insert_string = NULL;
3512 int allocated = FALSE;
3513 long cnt;
3514
3515#ifdef FEAT_CLIPBOARD
3516 /* Adjust register name for "unnamed" in 'clipboard'. */
3517 adjust_clip_reg(&regname);
3518 (void)may_get_selection(regname);
3519#endif
3520
3521 if (flags & PUT_FIXINDENT)
3522 orig_indent = get_indent();
3523
3524 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3525 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3526
3527 /*
3528 * Using inserted text works differently, because the register includes
3529 * special characters (newlines, etc.).
3530 */
3531 if (regname == '.')
3532 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003533 if (VIsual_active)
3534 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3536 (count == -1 ? 'O' : 'i')), count, FALSE);
3537 /* Putting the text is done later, so can't really move the cursor to
3538 * the next character. Use "l" to simulate it. */
3539 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3540 stuffcharReadbuff('l');
3541 return;
3542 }
3543
3544 /*
3545 * For special registers '%' (file name), '#' (alternate file name) and
3546 * ':' (last command line), etc. we have to create a fake yank register.
3547 */
3548 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3549 {
3550 if (insert_string == NULL)
3551 return;
3552 }
3553
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003554 /* Autocommands may be executed when saving lines for undo. This might
3555 * make "y_array" invalid, so we start undo now to avoid that. */
3556 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3557 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (insert_string != NULL)
3560 {
3561 y_type = MCHAR;
3562#ifdef FEAT_EVAL
3563 if (regname == '=')
3564 {
3565 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003566 * characters.
3567 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 for (;;)
3569 {
3570 y_size = 0;
3571 ptr = insert_string;
3572 while (ptr != NULL)
3573 {
3574 if (y_array != NULL)
3575 y_array[y_size] = ptr;
3576 ++y_size;
3577 ptr = vim_strchr(ptr, '\n');
3578 if (ptr != NULL)
3579 {
3580 if (y_array != NULL)
3581 *ptr = NUL;
3582 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003583 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 if (*ptr == NUL)
3585 {
3586 y_type = MLINE;
3587 break;
3588 }
3589 }
3590 }
3591 if (y_array != NULL)
3592 break;
3593 y_array = (char_u **)alloc((unsigned)
3594 (y_size * sizeof(char_u *)));
3595 if (y_array == NULL)
3596 goto end;
3597 }
3598 }
3599 else
3600#endif
3601 {
3602 y_size = 1; /* use fake one-line yank register */
3603 y_array = &insert_string;
3604 }
3605 }
3606 else
3607 {
3608 get_yank_register(regname, FALSE);
3609
3610 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 y_size = y_current->y_size;
3613 y_array = y_current->y_array;
3614 }
3615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (y_type == MLINE)
3617 {
3618 if (flags & PUT_LINE_SPLIT)
3619 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003620 char_u *p;
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 /* "p" or "P" in Visual mode: split the lines to put the text in
3623 * between. */
3624 if (u_save_cursor() == FAIL)
3625 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003626 p = ml_get_cursor();
3627 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003629 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (ptr == NULL)
3631 goto end;
3632 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3633 vim_free(ptr);
3634
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003635 oldp = ml_get_curline();
3636 p = oldp + curwin->w_cursor.col;
3637 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003638 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003639 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (ptr == NULL)
3641 goto end;
3642 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3643 ++nr_lines;
3644 dir = FORWARD;
3645 }
3646 if (flags & PUT_LINE_FORWARD)
3647 {
3648 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003649 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 dir = FORWARD;
3651 }
3652 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3653 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3657 y_type = MLINE;
3658
3659 if (y_size == 0 || y_array == NULL)
3660 {
3661 EMSG2(_("E353: Nothing in register %s"),
3662 regname == 0 ? (char_u *)"\"" : transchar(regname));
3663 goto end;
3664 }
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (y_type == MBLOCK)
3667 {
3668 lnum = curwin->w_cursor.lnum + y_size + 1;
3669 if (lnum > curbuf->b_ml.ml_line_count)
3670 lnum = curbuf->b_ml.ml_line_count + 1;
3671 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3672 goto end;
3673 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003674 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 lnum = curwin->w_cursor.lnum;
3677#ifdef FEAT_FOLDING
3678 /* Correct line number for closed fold. Don't move the cursor yet,
3679 * u_save() uses it. */
3680 if (dir == BACKWARD)
3681 (void)hasFolding(lnum, &lnum, NULL);
3682 else
3683 (void)hasFolding(lnum, NULL, &lnum);
3684#endif
3685 if (dir == FORWARD)
3686 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003687 /* In an empty buffer the empty line is going to be replaced, include
3688 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003689 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 goto end;
3691#ifdef FEAT_FOLDING
3692 if (dir == FORWARD)
3693 curwin->w_cursor.lnum = lnum - 1;
3694 else
3695 curwin->w_cursor.lnum = lnum;
3696 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3697#endif
3698 }
3699 else if (u_save_cursor() == FAIL)
3700 goto end;
3701
3702 yanklen = (int)STRLEN(y_array[0]);
3703
3704#ifdef FEAT_VIRTUALEDIT
3705 if (ve_flags == VE_ALL && y_type == MCHAR)
3706 {
3707 if (gchar_cursor() == TAB)
3708 {
3709 /* Don't need to insert spaces when "p" on the last position of a
3710 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003711#ifdef FEAT_VARTABS
3712 int viscol = getviscol();
3713 if (dir == FORWARD
3714 ? tabstop_padding(viscol, curbuf->b_p_ts,
3715 curbuf->b_p_vts_array) != 1
3716 : curwin->w_cursor.coladd > 0)
3717 coladvance_force(viscol);
3718#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (dir == FORWARD
3720 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3721 : curwin->w_cursor.coladd > 0)
3722 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
3725 curwin->w_cursor.coladd = 0;
3726 }
3727 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3728 coladvance_force(getviscol() + (dir == FORWARD));
3729 }
3730#endif
3731
3732 lnum = curwin->w_cursor.lnum;
3733 col = curwin->w_cursor.col;
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 /*
3736 * Block mode
3737 */
3738 if (y_type == MBLOCK)
3739 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003740 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 colnr_T endcol2 = 0;
3742
3743 if (dir == FORWARD && c != NUL)
3744 {
3745#ifdef FEAT_VIRTUALEDIT
3746 if (ve_flags == VE_ALL)
3747 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3748 else
3749#endif
3750 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3751
3752#ifdef FEAT_MBYTE
3753 if (has_mbyte)
3754 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003755 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else
3757#endif
3758#ifdef FEAT_VIRTUALEDIT
3759 if (c != TAB || ve_flags != VE_ALL)
3760#endif
3761 ++curwin->w_cursor.col;
3762 ++col;
3763 }
3764 else
3765 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3766
3767#ifdef FEAT_VIRTUALEDIT
3768 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003769 if (ve_flags == VE_ALL
3770 && (curwin->w_cursor.coladd > 0
3771 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 if (dir == FORWARD && c == NUL)
3774 ++col;
3775 if (dir != FORWARD && c != NUL)
3776 ++curwin->w_cursor.col;
3777 if (c == TAB)
3778 {
3779 if (dir == BACKWARD && curwin->w_cursor.col)
3780 curwin->w_cursor.col--;
3781 if (dir == FORWARD && col - 1 == endcol2)
3782 curwin->w_cursor.col++;
3783 }
3784 }
3785 curwin->w_cursor.coladd = 0;
3786#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003787 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 for (i = 0; i < y_size; ++i)
3789 {
3790 int spaces;
3791 char shortline;
3792
3793 bd.startspaces = 0;
3794 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 vcol = 0;
3796 delcount = 0;
3797
3798 /* add a new line */
3799 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3800 {
3801 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3802 (colnr_T)1, FALSE) == FAIL)
3803 break;
3804 ++nr_lines;
3805 }
3806 /* get the old line and advance to the position to insert at */
3807 oldp = ml_get_curline();
3808 oldlen = (int)STRLEN(oldp);
3809 for (ptr = oldp; vcol < col && *ptr; )
3810 {
3811 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003812 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 vcol += incr;
3814 }
3815 bd.textcol = (colnr_T)(ptr - oldp);
3816
3817 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3818
3819 if (vcol < col) /* line too short, padd with spaces */
3820 bd.startspaces = col - vcol;
3821 else if (vcol > col)
3822 {
3823 bd.endspaces = vcol - col;
3824 bd.startspaces = incr - bd.endspaces;
3825 --bd.textcol;
3826 delcount = 1;
3827#ifdef FEAT_MBYTE
3828 if (has_mbyte)
3829 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3830#endif
3831 if (oldp[bd.textcol] != TAB)
3832 {
3833 /* Only a Tab can be split into spaces. Other
3834 * characters will have to be moved to after the
3835 * block, causing misalignment. */
3836 delcount = 0;
3837 bd.endspaces = 0;
3838 }
3839 }
3840
3841 yanklen = (int)STRLEN(y_array[i]);
3842
3843 /* calculate number of spaces required to fill right side of block*/
3844 spaces = y_width + 1;
3845 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003846 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (spaces < 0)
3848 spaces = 0;
3849
3850 /* insert the new text */
3851 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3852 newp = alloc_check((unsigned)totlen + oldlen + 1);
3853 if (newp == NULL)
3854 break;
3855 /* copy part up to cursor to new line */
3856 ptr = newp;
3857 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3858 ptr += bd.textcol;
3859 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003860 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 ptr += bd.startspaces;
3862 /* insert the new text */
3863 for (j = 0; j < count; ++j)
3864 {
3865 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3866 ptr += yanklen;
3867
3868 /* insert block's trailing spaces only if there's text behind */
3869 if ((j < count - 1 || !shortline) && spaces)
3870 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003871 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 ptr += spaces;
3873 }
3874 }
3875 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003876 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 ptr += bd.endspaces;
3878 /* move the text after the cursor to the end of the line. */
3879 mch_memmove(ptr, oldp + bd.textcol + delcount,
3880 (size_t)(oldlen - bd.textcol - delcount + 1));
3881 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3882
3883 ++curwin->w_cursor.lnum;
3884 if (i == 0)
3885 curwin->w_cursor.col += bd.startspaces;
3886 }
3887
3888 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3889
3890 /* Set '[ mark. */
3891 curbuf->b_op_start = curwin->w_cursor;
3892 curbuf->b_op_start.lnum = lnum;
3893
3894 /* adjust '] mark */
3895 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3896 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003897# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003899# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (flags & PUT_CURSEND)
3901 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003902 colnr_T len;
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 curwin->w_cursor = curbuf->b_op_end;
3905 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003906
3907 /* in Insert mode we might be after the NUL, correct for that */
3908 len = (colnr_T)STRLEN(ml_get_curline());
3909 if (curwin->w_cursor.col > len)
3910 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912 else
3913 curwin->w_cursor.lnum = lnum;
3914 }
3915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
3917 /*
3918 * Character or Line mode
3919 */
3920 if (y_type == MCHAR)
3921 {
3922 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3923 * char */
3924 if (dir == FORWARD && gchar_cursor() != NUL)
3925 {
3926#ifdef FEAT_MBYTE
3927 if (has_mbyte)
3928 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003929 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 /* put it on the next of the multi-byte character. */
3932 col += bytelen;
3933 if (yanklen)
3934 {
3935 curwin->w_cursor.col += bytelen;
3936 curbuf->b_op_end.col += bytelen;
3937 }
3938 }
3939 else
3940#endif
3941 {
3942 ++col;
3943 if (yanklen)
3944 {
3945 ++curwin->w_cursor.col;
3946 ++curbuf->b_op_end.col;
3947 }
3948 }
3949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 curbuf->b_op_start = curwin->w_cursor;
3951 }
3952 /*
3953 * Line mode: BACKWARD is the same as FORWARD on the previous line
3954 */
3955 else if (dir == BACKWARD)
3956 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003957 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 /*
3960 * simple case: insert into current line
3961 */
3962 if (y_type == MCHAR && y_size == 1)
3963 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003964 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003965
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003966 if (VIsual_active)
3967 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003968 end_lnum = curbuf->b_visual.vi_end.lnum;
3969 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3970 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003971 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003972
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003973 do {
3974 totlen = count * yanklen;
3975 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003977 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003978 if (VIsual_active && col > (int)STRLEN(oldp))
3979 {
3980 lnum++;
3981 continue;
3982 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003983 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3984 if (newp == NULL)
3985 goto end; /* alloc() gave an error message */
3986 mch_memmove(newp, oldp, (size_t)col);
3987 ptr = newp + col;
3988 for (i = 0; i < count; ++i)
3989 {
3990 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3991 ptr += yanklen;
3992 }
3993 STRMOVE(ptr, oldp + col);
3994 ml_replace(lnum, newp, FALSE);
3995 /* Place cursor on last putted char. */
3996 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003997 {
3998 /* make sure curwin->w_virtcol is updated */
3999 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004000 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01004001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004003 if (VIsual_active)
4004 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01004005 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004006
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01004007 if (VIsual_active) /* reset lnum to the last visual line */
4008 lnum--;
4009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 curbuf->b_op_end = curwin->w_cursor;
4011 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4012 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4013 ++curwin->w_cursor.col;
4014 changed_bytes(lnum, col);
4015 }
4016 else
4017 {
4018 /*
4019 * Insert at least one line. When y_type is MCHAR, break the first
4020 * line in two.
4021 */
4022 for (cnt = 1; cnt <= count; ++cnt)
4023 {
4024 i = 0;
4025 if (y_type == MCHAR)
4026 {
4027 /*
4028 * Split the current line in two at the insert position.
4029 * First insert y_array[size - 1] in front of second line.
4030 * Then append y_array[0] to first line.
4031 */
4032 lnum = new_cursor.lnum;
4033 ptr = ml_get(lnum) + col;
4034 totlen = (int)STRLEN(y_array[y_size - 1]);
4035 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4036 if (newp == NULL)
4037 goto error;
4038 STRCPY(newp, y_array[y_size - 1]);
4039 STRCAT(newp, ptr);
4040 /* insert second line */
4041 ml_append(lnum, newp, (colnr_T)0, FALSE);
4042 vim_free(newp);
4043
4044 oldp = ml_get(lnum);
4045 newp = alloc_check((unsigned)(col + yanklen + 1));
4046 if (newp == NULL)
4047 goto error;
4048 /* copy first part of line */
4049 mch_memmove(newp, oldp, (size_t)col);
4050 /* append to first line */
4051 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4052 ml_replace(lnum, newp, FALSE);
4053
4054 curwin->w_cursor.lnum = lnum;
4055 i = 1;
4056 }
4057
4058 for (; i < y_size; ++i)
4059 {
4060 if ((y_type != MCHAR || i < y_size - 1)
4061 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4062 == FAIL)
4063 goto error;
4064 lnum++;
4065 ++nr_lines;
4066 if (flags & PUT_FIXINDENT)
4067 {
4068 old_pos = curwin->w_cursor;
4069 curwin->w_cursor.lnum = lnum;
4070 ptr = ml_get(lnum);
4071 if (cnt == count && i == y_size - 1)
4072 lendiff = (int)STRLEN(ptr);
4073#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4074 if (*ptr == '#' && preprocs_left())
4075 indent = 0; /* Leave # lines at start */
4076 else
4077#endif
4078 if (*ptr == NUL)
4079 indent = 0; /* Ignore empty lines */
4080 else if (first_indent)
4081 {
4082 indent_diff = orig_indent - get_indent();
4083 indent = orig_indent;
4084 first_indent = FALSE;
4085 }
4086 else if ((indent = get_indent() + indent_diff) < 0)
4087 indent = 0;
4088 (void)set_indent(indent, 0);
4089 curwin->w_cursor = old_pos;
4090 /* remember how many chars were removed */
4091 if (cnt == count && i == y_size - 1)
4092 lendiff -= (int)STRLEN(ml_get(lnum));
4093 }
4094 }
4095 }
4096
4097error:
4098 /* Adjust marks. */
4099 if (y_type == MLINE)
4100 {
4101 curbuf->b_op_start.col = 0;
4102 if (dir == FORWARD)
4103 curbuf->b_op_start.lnum++;
4104 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004105 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004106 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004107 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004108 < curbuf->b_ml.ml_line_count
4109#ifdef FEAT_DIFF
4110 || curwin->w_p_diff
4111#endif
4112 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004113 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 (linenr_T)MAXLNUM, nr_lines, 0L);
4115
4116 /* note changed text for displaying and folding */
4117 if (y_type == MCHAR)
4118 changed_lines(curwin->w_cursor.lnum, col,
4119 curwin->w_cursor.lnum + 1, nr_lines);
4120 else
4121 changed_lines(curbuf->b_op_start.lnum, 0,
4122 curbuf->b_op_start.lnum, nr_lines);
4123
4124 /* put '] mark at last inserted character */
4125 curbuf->b_op_end.lnum = lnum;
4126 /* correct length for change in indent */
4127 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4128 if (col > 1)
4129 curbuf->b_op_end.col = col - 1;
4130 else
4131 curbuf->b_op_end.col = 0;
4132
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004133 if (flags & PUT_CURSLINE)
4134 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004135 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004136 curwin->w_cursor.lnum = lnum;
4137 beginline(BL_WHITE | BL_FIX);
4138 }
4139 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141 /* put cursor after inserted text */
4142 if (y_type == MLINE)
4143 {
4144 if (lnum >= curbuf->b_ml.ml_line_count)
4145 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4146 else
4147 curwin->w_cursor.lnum = lnum + 1;
4148 curwin->w_cursor.col = 0;
4149 }
4150 else
4151 {
4152 curwin->w_cursor.lnum = lnum;
4153 curwin->w_cursor.col = col;
4154 }
4155 }
4156 else if (y_type == MLINE)
4157 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004158 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 curwin->w_cursor.col = 0;
4160 if (dir == FORWARD)
4161 ++curwin->w_cursor.lnum;
4162 beginline(BL_WHITE | BL_FIX);
4163 }
4164 else /* put cursor on first inserted character */
4165 curwin->w_cursor = new_cursor;
4166 }
4167 }
4168
4169 msgmore(nr_lines);
4170 curwin->w_set_curswant = TRUE;
4171
4172end:
4173 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004175 if (regname == '=')
4176 vim_free(y_array);
4177
Bram Moolenaar033d8882013-09-25 23:24:57 +02004178 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004179
Bram Moolenaar677ee682005-01-27 14:41:15 +00004180 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004181 adjust_cursor_eol();
4182}
4183
4184/*
4185 * When the cursor is on the NUL past the end of the line and it should not be
4186 * there move it left.
4187 */
4188 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004189adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004190{
4191 if (curwin->w_cursor.col > 0
4192 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004193#ifdef FEAT_VIRTUALEDIT
4194 && (ve_flags & VE_ONEMORE) == 0
4195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 && !(restart_edit || (State & INSERT)))
4197 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004198 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004199 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#ifdef FEAT_VIRTUALEDIT
4202 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004203 {
4204 colnr_T scol, ecol;
4205
4206 /* Coladd is set to the width of the last character. */
4207 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4208 curwin->w_cursor.coladd = ecol - scol + 1;
4209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210#endif
4211 }
4212}
4213
4214#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4215/*
4216 * Return TRUE if lines starting with '#' should be left aligned.
4217 */
4218 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004219preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 return
4222# ifdef FEAT_SMARTINDENT
4223# ifdef FEAT_CINDENT
4224 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4225# else
4226 curbuf->b_p_si
4227# endif
4228# endif
4229# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004230 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4231 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232# endif
4233 ;
4234}
4235#endif
4236
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004237/*
4238 * Return the character name of the register with the given number.
4239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004241get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
4243 if (num == -1)
4244 return '"';
4245 else if (num < 10)
4246 return num + '0';
4247 else if (num == DELETION_REGISTER)
4248 return '-';
4249#ifdef FEAT_CLIPBOARD
4250 else if (num == STAR_REGISTER)
4251 return '*';
4252 else if (num == PLUS_REGISTER)
4253 return '+';
4254#endif
4255 else
4256 {
4257#ifdef EBCDIC
4258 int i;
4259
4260 /* EBCDIC is really braindead ... */
4261 i = 'a' + (num - 10);
4262 if (i > 'i')
4263 i += 7;
4264 if (i > 'r')
4265 i += 8;
4266 return i;
4267#else
4268 return num + 'a' - 10;
4269#endif
4270 }
4271}
4272
4273/*
4274 * ":dis" and ":registers": Display the contents of the yank registers.
4275 */
4276 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004277ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004279 int i, n;
4280 long j;
4281 char_u *p;
4282 yankreg_T *yb;
4283 int name;
4284 int attr;
4285 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004286#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004287 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004288#else
4289# define clen 1
4290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 if (arg != NULL && *arg == NUL)
4293 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004294 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /* Highlight title */
4297 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4298 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4299 {
4300 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004301 if (arg != NULL && vim_strchr(arg, name) == NULL
4302#ifdef ONE_CLIPBOARD
4303 /* Star register and plus register contain the same thing. */
4304 && (name != '*' || vim_strchr(arg, '+') == NULL)
4305#endif
4306 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 continue; /* did not ask for this register */
4308
4309#ifdef FEAT_CLIPBOARD
4310 /* Adjust register name for "unnamed" in 'clipboard'.
4311 * When it's a clipboard register, fill it with the current contents
4312 * of the clipboard. */
4313 adjust_clip_reg(&name);
4314 (void)may_get_selection(name);
4315#endif
4316
4317 if (i == -1)
4318 {
4319 if (y_previous != NULL)
4320 yb = y_previous;
4321 else
4322 yb = &(y_regs[0]);
4323 }
4324 else
4325 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004326
4327#ifdef FEAT_EVAL
4328 if (name == MB_TOLOWER(redir_reg)
4329 || (redir_reg == '"' && yb == y_previous))
4330 continue; /* do not list register being written to, the
4331 * pointer can be freed */
4332#endif
4333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (yb->y_array != NULL)
4335 {
4336 msg_putchar('\n');
4337 msg_putchar('"');
4338 msg_putchar(name);
4339 MSG_PUTS(" ");
4340
4341 n = (int)Columns - 6;
4342 for (j = 0; j < yb->y_size && n > 1; ++j)
4343 {
4344 if (j)
4345 {
4346 MSG_PUTS_ATTR("^J", attr);
4347 n -= 2;
4348 }
4349 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004352 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004353#endif
4354 msg_outtrans_len(p, clen);
4355#ifdef FEAT_MBYTE
4356 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357#endif
4358 }
4359 }
4360 if (n > 1 && yb->y_type == MLINE)
4361 MSG_PUTS_ATTR("^J", attr);
4362 out_flush(); /* show one line at a time */
4363 }
4364 ui_breakcheck();
4365 }
4366
4367 /*
4368 * display last inserted text
4369 */
4370 if ((p = get_last_insert()) != NULL
4371 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4372 {
4373 MSG_PUTS("\n\". ");
4374 dis_msg(p, TRUE);
4375 }
4376
4377 /*
4378 * display last command line
4379 */
4380 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4381 && !got_int)
4382 {
4383 MSG_PUTS("\n\": ");
4384 dis_msg(last_cmdline, FALSE);
4385 }
4386
4387 /*
4388 * display current file name
4389 */
4390 if (curbuf->b_fname != NULL
4391 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4392 {
4393 MSG_PUTS("\n\"% ");
4394 dis_msg(curbuf->b_fname, FALSE);
4395 }
4396
4397 /*
4398 * display alternate file name
4399 */
4400 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4401 {
4402 char_u *fname;
4403 linenr_T dummy;
4404
4405 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4406 {
4407 MSG_PUTS("\n\"# ");
4408 dis_msg(fname, FALSE);
4409 }
4410 }
4411
4412 /*
4413 * display last search pattern
4414 */
4415 if (last_search_pat() != NULL
4416 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4417 {
4418 MSG_PUTS("\n\"/ ");
4419 dis_msg(last_search_pat(), FALSE);
4420 }
4421
4422#ifdef FEAT_EVAL
4423 /*
4424 * display last used expression
4425 */
4426 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4427 && !got_int)
4428 {
4429 MSG_PUTS("\n\"= ");
4430 dis_msg(expr_line, FALSE);
4431 }
4432#endif
4433}
4434
4435/*
4436 * display a string for do_dis()
4437 * truncate at end of screen line
4438 */
4439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004440dis_msg(
4441 char_u *p,
4442 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int n;
4445#ifdef FEAT_MBYTE
4446 int l;
4447#endif
4448
4449 n = (int)Columns - 6;
4450 while (*p != NUL
4451 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4452 && (n -= ptr2cells(p)) >= 0)
4453 {
4454#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004455 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 msg_outtrans_len(p, l);
4458 p += l;
4459 }
4460 else
4461#endif
4462 msg_outtrans_len(p++, 1);
4463 }
4464 ui_breakcheck();
4465}
4466
Bram Moolenaar81340392012-06-06 16:12:59 +02004467#if defined(FEAT_COMMENTS) || defined(PROTO)
4468/*
4469 * If "process" is TRUE and the line begins with a comment leader (possibly
4470 * after some white space), return a pointer to the text after it. Put a boolean
4471 * value indicating whether the line ends with an unclosed comment in
4472 * "is_comment".
4473 * line - line to be processed,
4474 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004475 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004476 * include_space - whether to also skip space following the comment leader,
4477 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004478 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004479 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004480 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004481skip_comment(
4482 char_u *line,
4483 int process,
4484 int include_space,
4485 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004486{
4487 char_u *comment_flags = NULL;
4488 int lead_len;
4489 int leader_offset = get_last_leader_offset(line, &comment_flags);
4490
4491 *is_comment = FALSE;
4492 if (leader_offset != -1)
4493 {
4494 /* Let's check whether the line ends with an unclosed comment.
4495 * If the last comment leader has COM_END in flags, there's no comment.
4496 */
4497 while (*comment_flags)
4498 {
4499 if (*comment_flags == COM_END
4500 || *comment_flags == ':')
4501 break;
4502 ++comment_flags;
4503 }
4504 if (*comment_flags != COM_END)
4505 *is_comment = TRUE;
4506 }
4507
4508 if (process == FALSE)
4509 return line;
4510
4511 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4512
4513 if (lead_len == 0)
4514 return line;
4515
4516 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004517 * - COM_END,
4518 * - colon,
4519 * whichever comes first.
4520 */
4521 while (*comment_flags)
4522 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004523 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004524 || *comment_flags == ':')
4525 {
4526 break;
4527 }
4528 ++comment_flags;
4529 }
4530
4531 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004532 * starting with a closing part of a three-part comment. That's good,
4533 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004534 */
4535 if (*comment_flags == ':' || *comment_flags == NUL)
4536 line += lead_len;
4537
4538 return line;
4539}
4540#endif
4541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004543 * Join 'count' lines (minimal 2) at cursor position.
4544 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004545 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4546 * leaders should not be removed.
4547 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4548 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004550 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
4552 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004553do_join(
4554 long count,
4555 int insert_space,
4556 int save_undo,
4557 int use_formatoptions UNUSED,
4558 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004560 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004561 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004562 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004564 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004565 int endcurr1 = NUL;
4566 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004567 int currsize = 0; /* size of the current line */
4568 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004570 colnr_T col = 0;
4571 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004572#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004573 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004574 int remove_comments = (use_formatoptions == TRUE)
4575 && has_format_option(FO_REMOVE_COMS);
4576 int prev_was_comment;
4577#endif
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004580 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4581 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004584 /* Allocate an array to store the number of spaces inserted before each
4585 * line. We will use it to pre-compute the length of the new line and the
4586 * proper placement of each original line in the new one. */
4587 spaces = lalloc_clear((long_u)count, TRUE);
4588 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004590#if defined(FEAT_COMMENTS) || defined(PROTO)
4591 if (remove_comments)
4592 {
4593 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4594 if (comments == NULL)
4595 {
4596 vim_free(spaces);
4597 return FAIL;
4598 }
4599 }
4600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
4602 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004603 * Don't move anything, just compute the final line length
4604 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004606 for (t = 0; t < count; ++t)
4607 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004608 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004609 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004610 {
4611 /* Set the '[ mark. */
4612 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4613 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4614 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004615#if defined(FEAT_COMMENTS) || defined(PROTO)
4616 if (remove_comments)
4617 {
4618 /* We don't want to remove the comment leader if the
4619 * previous line is not a comment. */
4620 if (t > 0 && prev_was_comment)
4621 {
4622
4623 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4624 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004625 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004626 curr = new_curr;
4627 }
4628 else
4629 curr = skip_comment(curr, FALSE, insert_space,
4630 &prev_was_comment);
4631 }
4632#endif
4633
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004634 if (insert_space && t > 0)
4635 {
4636 curr = skipwhite(curr);
4637 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4638#ifdef FEAT_MBYTE
4639 && (!has_format_option(FO_MBYTE_JOIN)
4640 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4641 && (!has_format_option(FO_MBYTE_JOIN2)
4642 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4643#endif
4644 )
4645 {
4646 /* don't add a space if the line is ending in a space */
4647 if (endcurr1 == ' ')
4648 endcurr1 = endcurr2;
4649 else
4650 ++spaces[t];
4651 /* extra space when 'joinspaces' set and line ends in '.' */
4652 if ( p_js
4653 && (endcurr1 == '.'
4654 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4655 && (endcurr1 == '?' || endcurr1 == '!'))))
4656 ++spaces[t];
4657 }
4658 }
4659 currsize = (int)STRLEN(curr);
4660 sumsize += currsize + spaces[t];
4661 endcurr1 = endcurr2 = NUL;
4662 if (insert_space && currsize > 0)
4663 {
4664#ifdef FEAT_MBYTE
4665 if (has_mbyte)
4666 {
4667 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004668 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004669 endcurr1 = (*mb_ptr2char)(cend);
4670 if (cend > curr)
4671 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004672 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004673 endcurr2 = (*mb_ptr2char)(cend);
4674 }
4675 }
4676 else
4677#endif
4678 {
4679 endcurr1 = *(curr + currsize - 1);
4680 if (currsize > 1)
4681 endcurr2 = *(curr + currsize - 2);
4682 }
4683 }
4684 line_breakcheck();
4685 if (got_int)
4686 {
4687 ret = FAIL;
4688 goto theend;
4689 }
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004692 /* store the column position before last line */
4693 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004695 /* allocate the space for the new line */
4696 newp = alloc_check((unsigned)(sumsize + 1));
4697 cend = newp + sumsize;
4698 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004700 /*
4701 * Move affected lines to the new long one.
4702 *
4703 * Move marks from each deleted line to the joined line, adjusting the
4704 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4705 * should not really be a problem.
4706 */
4707 for (t = count - 1; ; --t)
4708 {
4709 cend -= currsize;
4710 mch_memmove(cend, curr, (size_t)currsize);
4711 if (spaces[t] > 0)
4712 {
4713 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004714 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004715 }
4716 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004717 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004718 if (t == 0)
4719 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004720 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004721#if defined(FEAT_COMMENTS) || defined(PROTO)
4722 if (remove_comments)
4723 curr += comments[t - 1];
4724#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004725 if (insert_space && t > 1)
4726 curr = skipwhite(curr);
4727 currsize = (int)STRLEN(curr);
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4730
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004731 if (setmark)
4732 {
4733 /* Set the '] mark. */
4734 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4735 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4736 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /* Only report the change in the first line here, del_lines() will report
4739 * the deleted line. */
4740 changed_lines(curwin->w_cursor.lnum, currsize,
4741 curwin->w_cursor.lnum + 1, 0L);
4742
4743 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004744 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 * briefly, and then move it back. After del_lines() the cursor may
4746 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 */
4748 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004750 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 curwin->w_cursor.lnum = t;
4752
4753 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004754 * Set the cursor column:
4755 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004756 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004758 curwin->w_cursor.col =
4759 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#ifdef FEAT_VIRTUALEDIT
4763 curwin->w_cursor.coladd = 0;
4764#endif
4765 curwin->w_set_curswant = TRUE;
4766
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004767theend:
4768 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004769#if defined(FEAT_COMMENTS) || defined(PROTO)
4770 if (remove_comments)
4771 vim_free(comments);
4772#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004773 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
4775
4776#ifdef FEAT_COMMENTS
4777/*
4778 * Return TRUE if the two comment leaders given are the same. "lnum" is
4779 * the first line. White-space is ignored. Note that the whole of
4780 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4781 */
4782 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004783same_leader(
4784 linenr_T lnum,
4785 int leader1_len,
4786 char_u *leader1_flags,
4787 int leader2_len,
4788 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 int idx1 = 0, idx2 = 0;
4791 char_u *p;
4792 char_u *line1;
4793 char_u *line2;
4794
4795 if (leader1_len == 0)
4796 return (leader2_len == 0);
4797
4798 /*
4799 * If first leader has 'f' flag, the lines can be joined only if the
4800 * second line does not have a leader.
4801 * If first leader has 'e' flag, the lines can never be joined.
4802 * If fist leader has 's' flag, the lines can only be joined if there is
4803 * some text after it and the second line has the 'm' flag.
4804 */
4805 if (leader1_flags != NULL)
4806 {
4807 for (p = leader1_flags; *p && *p != ':'; ++p)
4808 {
4809 if (*p == COM_FIRST)
4810 return (leader2_len == 0);
4811 if (*p == COM_END)
4812 return FALSE;
4813 if (*p == COM_START)
4814 {
4815 if (*(ml_get(lnum) + leader1_len) == NUL)
4816 return FALSE;
4817 if (leader2_flags == NULL || leader2_len == 0)
4818 return FALSE;
4819 for (p = leader2_flags; *p && *p != ':'; ++p)
4820 if (*p == COM_MIDDLE)
4821 return TRUE;
4822 return FALSE;
4823 }
4824 }
4825 }
4826
4827 /*
4828 * Get current line and next line, compare the leaders.
4829 * The first line has to be saved, only one line can be locked at a time.
4830 */
4831 line1 = vim_strsave(ml_get(lnum));
4832 if (line1 != NULL)
4833 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004834 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 ;
4836 line2 = ml_get(lnum + 1);
4837 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4838 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004839 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 if (line1[idx1++] != line2[idx2])
4842 break;
4843 }
4844 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004845 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 ++idx1;
4847 }
4848 vim_free(line1);
4849 }
4850 return (idx2 == leader2_len && idx1 == leader1_len);
4851}
4852#endif
4853
4854/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004855 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 */
4857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004858op_format(
4859 oparg_T *oap,
4860 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 long old_line_count = curbuf->b_ml.ml_line_count;
4863
4864 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4865 * can put it back there. */
4866 curwin->w_cursor = oap->cursor_start;
4867
4868 if (u_save((linenr_T)(oap->start.lnum - 1),
4869 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4870 return;
4871 curwin->w_cursor = oap->start;
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (oap->is_VIsual)
4874 /* When there is no change: need to remove the Visual selection */
4875 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 /* Set '[ mark at the start of the formatted area */
4878 curbuf->b_op_start = oap->start;
4879
4880 /* For "gw" remember the cursor position and put it back below (adjusted
4881 * for joined and split lines). */
4882 if (keep_cursor)
4883 saved_cursor = oap->cursor_start;
4884
Bram Moolenaar81a82092008-03-12 16:27:00 +00004885 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Leave the cursor at the first non-blank of the last formatted line.
4889 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4890 * line, so "." will do the next lines.
4891 */
4892 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4893 ++curwin->w_cursor.lnum;
4894 beginline(BL_WHITE | BL_FIX);
4895 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4896 msgmore(old_line_count);
4897
4898 /* put '] mark on the end of the formatted area */
4899 curbuf->b_op_end = curwin->w_cursor;
4900
4901 if (keep_cursor)
4902 {
4903 curwin->w_cursor = saved_cursor;
4904 saved_cursor.lnum = 0;
4905 }
4906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (oap->is_VIsual)
4908 {
4909 win_T *wp;
4910
4911 FOR_ALL_WINDOWS(wp)
4912 {
4913 if (wp->w_old_cursor_lnum != 0)
4914 {
4915 /* When lines have been inserted or deleted, adjust the end of
4916 * the Visual area to be redrawn. */
4917 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4918 wp->w_old_cursor_lnum += old_line_count;
4919 else
4920 wp->w_old_visual_lnum += old_line_count;
4921 }
4922 }
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924}
4925
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004926#if defined(FEAT_EVAL) || defined(PROTO)
4927/*
4928 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4929 */
4930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004931op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004932{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004933 if (oap->is_VIsual)
4934 /* When there is no change: need to remove the Visual selection */
4935 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004936
Bram Moolenaar700303e2010-07-11 17:35:50 +02004937 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4938 /* As documented: when 'formatexpr' returns non-zero fall back to
4939 * internal formatting. */
4940 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004941}
4942
4943 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004944fex_format(
4945 linenr_T lnum,
4946 long count,
4947 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004948{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004949 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4950 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004951 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004952 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004953
4954 /*
4955 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004956 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004957 */
4958 set_vim_var_nr(VV_LNUM, lnum);
4959 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004960 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004961
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004962 /* Make a copy, the option could be changed while calling it. */
4963 fex = vim_strsave(curbuf->b_p_fex);
4964 if (fex == NULL)
4965 return 0;
4966
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004967 /*
4968 * Evaluate the function.
4969 */
4970 if (use_sandbox)
4971 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004972 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004973 if (use_sandbox)
4974 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004975
4976 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004977 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004978
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004979 return r;
4980}
4981#endif
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983/*
4984 * Format "line_count" lines, starting at the cursor position.
4985 * When "line_count" is negative, format until the end of the paragraph.
4986 * Lines after the cursor line are saved for undo, caller must have saved the
4987 * first line.
4988 */
4989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004990format_lines(
4991 linenr_T line_count,
4992 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
4994 int max_len;
4995 int is_not_par; /* current line not part of parag. */
4996 int next_is_not_par; /* next line not part of paragraph */
4997 int is_end_par; /* at end of paragraph */
4998 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4999 int next_is_start_par = FALSE;
5000#ifdef FEAT_COMMENTS
5001 int leader_len = 0; /* leader len of current line */
5002 int next_leader_len; /* leader len of next line */
5003 char_u *leader_flags = NULL; /* flags for leader of current line */
5004 char_u *next_leader_flags; /* flags for leader of next line */
5005 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005006 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
5008 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005009 int second_indent = -1; /* indent for second line (comment
5010 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 int do_second_indent;
5012 int do_number_indent;
5013 int do_trail_white;
5014 int first_par_line = TRUE;
5015 int smd_save;
5016 long count;
5017 int need_set_indent = TRUE; /* set indent of next paragraph */
5018 int force_format = FALSE;
5019 int old_State = State;
5020
5021 /* length of a line to force formatting: 3 * 'tw' */
5022 max_len = comp_textwidth(TRUE) * 3;
5023
5024 /* check for 'q', '2' and '1' in 'formatoptions' */
5025#ifdef FEAT_COMMENTS
5026 do_comments = has_format_option(FO_Q_COMS);
5027#endif
5028 do_second_indent = has_format_option(FO_Q_SECOND);
5029 do_number_indent = has_format_option(FO_Q_NUMBER);
5030 do_trail_white = has_format_option(FO_WHITE_PAR);
5031
5032 /*
5033 * Get info about the previous and current line.
5034 */
5035 if (curwin->w_cursor.lnum > 1)
5036 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5037#ifdef FEAT_COMMENTS
5038 , &leader_len, &leader_flags, do_comments
5039#endif
5040 );
5041 else
5042 is_not_par = TRUE;
5043 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5044#ifdef FEAT_COMMENTS
5045 , &next_leader_len, &next_leader_flags, do_comments
5046#endif
5047 );
5048 is_end_par = (is_not_par || next_is_not_par);
5049 if (!is_end_par && do_trail_white)
5050 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5051
5052 curwin->w_cursor.lnum--;
5053 for (count = line_count; count != 0 && !got_int; --count)
5054 {
5055 /*
5056 * Advance to next paragraph.
5057 */
5058 if (advance)
5059 {
5060 curwin->w_cursor.lnum++;
5061 prev_is_end_par = is_end_par;
5062 is_not_par = next_is_not_par;
5063#ifdef FEAT_COMMENTS
5064 leader_len = next_leader_len;
5065 leader_flags = next_leader_flags;
5066#endif
5067 }
5068
5069 /*
5070 * The last line to be formatted.
5071 */
5072 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5073 {
5074 next_is_not_par = TRUE;
5075#ifdef FEAT_COMMENTS
5076 next_leader_len = 0;
5077 next_leader_flags = NULL;
5078#endif
5079 }
5080 else
5081 {
5082 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5083#ifdef FEAT_COMMENTS
5084 , &next_leader_len, &next_leader_flags, do_comments
5085#endif
5086 );
5087 if (do_number_indent)
5088 next_is_start_par =
5089 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5090 }
5091 advance = TRUE;
5092 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5093 if (!is_end_par && do_trail_white)
5094 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5095
5096 /*
5097 * Skip lines that are not in a paragraph.
5098 */
5099 if (is_not_par)
5100 {
5101 if (line_count < 0)
5102 break;
5103 }
5104 else
5105 {
5106 /*
5107 * For the first line of a paragraph, check indent of second line.
5108 * Don't do this for comments and empty lines.
5109 */
5110 if (first_par_line
5111 && (do_second_indent || do_number_indent)
5112 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005113 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005115 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005116 {
5117#ifdef FEAT_COMMENTS
5118 if (leader_len == 0 && next_leader_len == 0)
5119 {
5120 /* no comment found */
5121#endif
5122 second_indent =
5123 get_indent_lnum(curwin->w_cursor.lnum + 1);
5124#ifdef FEAT_COMMENTS
5125 }
5126 else
5127 {
5128 second_indent = next_leader_len;
5129 do_comments_list = 1;
5130 }
5131#endif
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005134 {
5135#ifdef FEAT_COMMENTS
5136 if (leader_len == 0 && next_leader_len == 0)
5137 {
5138 /* no comment found */
5139#endif
5140 second_indent =
5141 get_number_indent(curwin->w_cursor.lnum);
5142#ifdef FEAT_COMMENTS
5143 }
5144 else
5145 {
5146 /* get_number_indent() is now "comment aware"... */
5147 second_indent =
5148 get_number_indent(curwin->w_cursor.lnum);
5149 do_comments_list = 1;
5150 }
5151#endif
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154
5155 /*
5156 * When the comment leader changes, it's the end of the paragraph.
5157 */
5158 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5159#ifdef FEAT_COMMENTS
5160 || !same_leader(curwin->w_cursor.lnum,
5161 leader_len, leader_flags,
5162 next_leader_len, next_leader_flags)
5163#endif
5164 )
5165 is_end_par = TRUE;
5166
5167 /*
5168 * If we have got to the end of a paragraph, or the line is
5169 * getting long, format it.
5170 */
5171 if (is_end_par || force_format)
5172 {
5173 if (need_set_indent)
5174 /* replace indent in first line with minimal number of
5175 * tabs and spaces, according to current options */
5176 (void)set_indent(get_indent(), SIN_CHANGED);
5177
5178 /* put cursor on last non-space */
5179 State = NORMAL; /* don't go past end-of-line */
5180 coladvance((colnr_T)MAXCOL);
5181 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5182 dec_cursor();
5183
5184 /* do the formatting, without 'showmode' */
5185 State = INSERT; /* for open_line() */
5186 smd_save = p_smd;
5187 p_smd = FALSE;
5188 insertchar(NUL, INSCHAR_FORMAT
5189#ifdef FEAT_COMMENTS
5190 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005191 + (do_comments && do_comments_list
5192 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005194 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 State = old_State;
5196 p_smd = smd_save;
5197 second_indent = -1;
5198 /* at end of par.: need to set indent of next par. */
5199 need_set_indent = is_end_par;
5200 if (is_end_par)
5201 {
5202 /* When called with a negative line count, break at the
5203 * end of the paragraph. */
5204 if (line_count < 0)
5205 break;
5206 first_par_line = TRUE;
5207 }
5208 force_format = FALSE;
5209 }
5210
5211 /*
5212 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005213 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 */
5215 if (!is_end_par)
5216 {
5217 advance = FALSE;
5218 curwin->w_cursor.lnum++;
5219 curwin->w_cursor.col = 0;
5220 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005224 {
5225 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5227 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005228 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005230 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5231 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005232 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005233
5234 if (indent > 0)
5235 {
5236 (void)del_bytes(indent, FALSE, FALSE);
5237 mark_col_adjust(curwin->w_cursor.lnum,
5238 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005239 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005242 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 beep_flush();
5245 break;
5246 }
5247 first_par_line = FALSE;
5248 /* If the line is getting long, format it next time */
5249 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5250 force_format = TRUE;
5251 else
5252 force_format = FALSE;
5253 }
5254 }
5255 line_breakcheck();
5256 }
5257}
5258
5259/*
5260 * Return TRUE if line "lnum" ends in a white character.
5261 */
5262 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005263ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *s = ml_get(lnum);
5266 size_t l;
5267
5268 if (*s == NUL)
5269 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005270 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 * invocation may call function multiple times". */
5272 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005273 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274}
5275
5276/*
5277 * Blank lines, and lines containing only the comment leader, are left
5278 * untouched by the formatting. The function returns TRUE in this
5279 * case. It also returns TRUE when a line starts with the end of a comment
5280 * ('e' in comment flags), so that this line is skipped, and not joined to the
5281 * previous line. A new paragraph starts after a blank line, or when the
5282 * comment leader changes -- webb.
5283 */
5284#ifdef FEAT_COMMENTS
5285 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286fmt_check_par(
5287 linenr_T lnum,
5288 int *leader_len,
5289 char_u **leader_flags,
5290 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 char_u *flags = NULL; /* init for GCC */
5293 char_u *ptr;
5294
5295 ptr = ml_get(lnum);
5296 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005297 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else
5299 *leader_len = 0;
5300
5301 if (*leader_len > 0)
5302 {
5303 /*
5304 * Search for 'e' flag in comment leader flags.
5305 */
5306 flags = *leader_flags;
5307 while (*flags && *flags != ':' && *flags != COM_END)
5308 ++flags;
5309 }
5310
5311 return (*skipwhite(ptr + *leader_len) == NUL
5312 || (*leader_len > 0 && *flags == COM_END)
5313 || startPS(lnum, NUL, FALSE));
5314}
5315#else
5316 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005317fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5320}
5321#endif
5322
5323/*
5324 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5325 * previous line is in the same paragraph. Used for auto-formatting.
5326 */
5327 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005328paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
5330 char_u *p;
5331#ifdef FEAT_COMMENTS
5332 int leader_len = 0; /* leader len of current line */
5333 char_u *leader_flags = NULL; /* flags for leader of current line */
5334 int next_leader_len; /* leader len of next line */
5335 char_u *next_leader_flags; /* flags for leader of next line */
5336 int do_comments; /* format comments */
5337#endif
5338
5339 if (lnum <= 1)
5340 return TRUE; /* start of the file */
5341
5342 p = ml_get(lnum - 1);
5343 if (*p == NUL)
5344 return TRUE; /* after empty line */
5345
5346#ifdef FEAT_COMMENTS
5347 do_comments = has_format_option(FO_Q_COMS);
5348#endif
5349 if (fmt_check_par(lnum - 1
5350#ifdef FEAT_COMMENTS
5351 , &leader_len, &leader_flags, do_comments
5352#endif
5353 ))
5354 return TRUE; /* after non-paragraph line */
5355
5356 if (fmt_check_par(lnum
5357#ifdef FEAT_COMMENTS
5358 , &next_leader_len, &next_leader_flags, do_comments
5359#endif
5360 ))
5361 return TRUE; /* "lnum" is not a paragraph line */
5362
5363 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5364 return TRUE; /* missing trailing space in previous line. */
5365
5366 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5367 return TRUE; /* numbered item starts in "lnum". */
5368
5369#ifdef FEAT_COMMENTS
5370 if (!same_leader(lnum - 1, leader_len, leader_flags,
5371 next_leader_len, next_leader_flags))
5372 return TRUE; /* change of comment leader. */
5373#endif
5374
5375 return FALSE;
5376}
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378/*
5379 * prepare a few things for block mode yank/delete/tilde
5380 *
5381 * for delete:
5382 * - textlen includes the first/last char to be (partly) deleted
5383 * - start/endspaces is the number of columns that are taken by the
5384 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005385 * deleted.
5386 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * - textlen includes the first/last char to be wholly yanked
5388 * - start/endspaces is the number of columns of the first/last yanked char
5389 * that are to be yanked.
5390 */
5391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005392block_prep(
5393 oparg_T *oap,
5394 struct block_def *bdp,
5395 linenr_T lnum,
5396 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 int incr = 0;
5399 char_u *pend;
5400 char_u *pstart;
5401 char_u *line;
5402 char_u *prev_pstart;
5403 char_u *prev_pend;
5404
5405 bdp->startspaces = 0;
5406 bdp->endspaces = 0;
5407 bdp->textlen = 0;
5408 bdp->start_vcol = 0;
5409 bdp->end_vcol = 0;
5410#ifdef FEAT_VISUALEXTRA
5411 bdp->is_short = FALSE;
5412 bdp->is_oneChar = FALSE;
5413 bdp->pre_whitesp = 0;
5414 bdp->pre_whitesp_c = 0;
5415 bdp->end_char_vcols = 0;
5416#endif
5417 bdp->start_char_vcols = 0;
5418
5419 line = ml_get(lnum);
5420 pstart = line;
5421 prev_pstart = line;
5422 while (bdp->start_vcol < oap->start_vcol && *pstart)
5423 {
5424 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005425 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 bdp->start_vcol += incr;
5427#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005428 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 bdp->pre_whitesp += incr;
5431 bdp->pre_whitesp_c++;
5432 }
5433 else
5434 {
5435 bdp->pre_whitesp = 0;
5436 bdp->pre_whitesp_c = 0;
5437 }
5438#endif
5439 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005440 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 }
5442 bdp->start_char_vcols = incr;
5443 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5444 {
5445 bdp->end_vcol = bdp->start_vcol;
5446#ifdef FEAT_VISUALEXTRA
5447 bdp->is_short = TRUE;
5448#endif
5449 if (!is_del || oap->op_type == OP_APPEND)
5450 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5451 }
5452 else
5453 {
5454 /* notice: this converts partly selected Multibyte characters to
5455 * spaces, too. */
5456 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5457 if (is_del && bdp->startspaces)
5458 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5459 pend = pstart;
5460 bdp->end_vcol = bdp->start_vcol;
5461 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5462 {
5463#ifdef FEAT_VISUALEXTRA
5464 bdp->is_oneChar = TRUE;
5465#endif
5466 if (oap->op_type == OP_INSERT)
5467 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5468 else if (oap->op_type == OP_APPEND)
5469 {
5470 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5471 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5472 }
5473 else
5474 {
5475 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5476 if (is_del && oap->op_type != OP_LSHIFT)
5477 {
5478 /* just putting the sum of those two into
5479 * bdp->startspaces doesn't work for Visual replace,
5480 * so we have to split the tab in two */
5481 bdp->startspaces = bdp->start_char_vcols
5482 - (bdp->start_vcol - oap->start_vcol);
5483 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5484 }
5485 }
5486 }
5487 else
5488 {
5489 prev_pend = pend;
5490 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5491 {
5492 /* Count a tab for what it's worth (if list mode not on) */
5493 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005494 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 bdp->end_vcol += incr;
5496 }
5497 if (bdp->end_vcol <= oap->end_vcol
5498 && (!is_del
5499 || oap->op_type == OP_APPEND
5500 || oap->op_type == OP_REPLACE)) /* line too short */
5501 {
5502#ifdef FEAT_VISUALEXTRA
5503 bdp->is_short = TRUE;
5504#endif
5505 /* Alternative: include spaces to fill up the block.
5506 * Disadvantage: can lead to trailing spaces when the line is
5507 * short where the text is put */
5508 /* if (!is_del || oap->op_type == OP_APPEND) */
5509 if (oap->op_type == OP_APPEND || virtual_op)
5510 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005511 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 else
5513 bdp->endspaces = 0; /* replace doesn't add characters */
5514 }
5515 else if (bdp->end_vcol > oap->end_vcol)
5516 {
5517 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5518 if (!is_del && bdp->endspaces)
5519 {
5520 bdp->endspaces = incr - bdp->endspaces;
5521 if (pend != pstart)
5522 pend = prev_pend;
5523 }
5524 }
5525 }
5526#ifdef FEAT_VISUALEXTRA
5527 bdp->end_char_vcols = incr;
5528#endif
5529 if (is_del && bdp->startspaces)
5530 pstart = prev_pstart;
5531 bdp->textlen = (int)(pend - pstart);
5532 }
5533 bdp->textcol = (colnr_T) (pstart - line);
5534 bdp->textstart = pstart;
5535}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005538 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005541op_addsub(
5542 oparg_T *oap,
5543 linenr_T Prenum1, /* Amount of add/subtract */
5544 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005546 pos_T pos;
5547 struct block_def bd;
5548 int change_cnt = 0;
5549 linenr_T amount = Prenum1;
5550
5551 if (!VIsual_active)
5552 {
5553 pos = curwin->w_cursor;
5554 if (u_save_cursor() == FAIL)
5555 return;
5556 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5557 if (change_cnt)
5558 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5559 }
5560 else
5561 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005562 int one_change;
5563 int length;
5564 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005565
5566 if (u_save((linenr_T)(oap->start.lnum - 1),
5567 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5568 return;
5569
5570 pos = oap->start;
5571 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5572 {
5573 if (oap->block_mode) /* Visual block mode */
5574 {
5575 block_prep(oap, &bd, pos.lnum, FALSE);
5576 pos.col = bd.textcol;
5577 length = bd.textlen;
5578 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005579 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005580 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005581 curwin->w_cursor.col = 0;
5582 pos.col = 0;
5583 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5584 }
5585 else /* oap->motion_type == MCHAR */
5586 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005587 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005588 dec(&(oap->end));
5589 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5590 pos.col = 0;
5591 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005592 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005593 pos.col += oap->start.col;
5594 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005595 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005596 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005597 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005598 length = (int)STRLEN(ml_get(oap->end.lnum));
5599 if (oap->end.col >= length)
5600 oap->end.col = length - 1;
5601 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005602 }
5603 }
5604 one_change = do_addsub(oap->op_type, &pos, length, amount);
5605 if (one_change)
5606 {
5607 /* Remember the start position of the first change. */
5608 if (change_cnt == 0)
5609 startpos = curbuf->b_op_start;
5610 ++change_cnt;
5611 }
5612
5613#ifdef FEAT_NETBEANS_INTG
5614 if (netbeans_active() && one_change)
5615 {
5616 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5617
5618 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5619 netbeans_inserted(curbuf, pos.lnum, pos.col,
5620 &ptr[pos.col], length);
5621 }
5622#endif
5623 if (g_cmd && one_change)
5624 amount += Prenum1;
5625 }
5626 if (change_cnt)
5627 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5628
5629 if (!change_cnt && oap->is_VIsual)
5630 /* No change: need to remove the Visual selection */
5631 redraw_curbuf_later(INVERTED);
5632
5633 /* Set '[ mark if something changed. Keep the last end
5634 * position from do_addsub(). */
5635 if (change_cnt > 0)
5636 curbuf->b_op_start = startpos;
5637
5638 if (change_cnt > p_report)
5639 {
5640 if (change_cnt == 1)
5641 MSG(_("1 line changed"));
5642 else
5643 smsg((char_u *)_("%ld lines changed"), change_cnt);
5644 }
5645 }
5646}
5647
5648/*
5649 * Add or subtract 'Prenum1' from a number in a line
5650 * op_type is OP_NR_ADD or OP_NR_SUB
5651 *
5652 * Returns TRUE if some character was changed.
5653 */
5654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005655do_addsub(
5656 int op_type,
5657 pos_T *pos,
5658 int length,
5659 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005660{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 int col;
5662 char_u *buf1;
5663 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005664 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005666 uvarnumber_T n;
5667 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 char_u *ptr;
5669 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 int todel;
5671 int dohex;
5672 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005673 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 int doalp;
5675 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005677 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005678 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005679 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005680 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005681 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005682 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005683 pos_T startpos;
5684 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5687 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005688 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5690
Bram Moolenaard79e5502016-01-10 22:13:02 +01005691 curwin->w_cursor = *pos;
5692 ptr = ml_get(pos->lnum);
5693 col = pos->col;
5694
5695 if (*ptr == NUL)
5696 goto theend;
5697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 /*
5699 * First check if we are on a hexadecimal number, after the "0x".
5700 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005701 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005703 if (dobin)
5704 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005705 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005706 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005707#ifdef FEAT_MBYTE
5708 if (has_mbyte)
5709 col -= (*mb_head_off)(ptr, ptr + col);
5710#endif
5711 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005712
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005713 if (dohex)
5714 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005715 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005716 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005717#ifdef FEAT_MBYTE
5718 if (has_mbyte)
5719 col -= (*mb_head_off)(ptr, ptr + col);
5720#endif
5721 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005722
5723 if ( dobin
5724 && dohex
5725 && ! ((col > 0
5726 && (ptr[col] == 'X'
5727 || ptr[col] == 'x')
5728 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005729#ifdef FEAT_MBYTE
5730 && (!has_mbyte ||
5731 !(*mb_head_off)(ptr, ptr + col - 1))
5732#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005733 && vim_isxdigit(ptr[col + 1]))))
5734 {
5735
5736 /* In case of binary/hexadecimal pattern overlap match, rescan */
5737
Bram Moolenaard79e5502016-01-10 22:13:02 +01005738 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005739
5740 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005741 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005742 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005743#ifdef FEAT_MBYTE
5744 if (has_mbyte)
5745 col -= (*mb_head_off)(ptr, ptr + col);
5746#endif
5747 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005748 }
5749
5750 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005751 && col > 0
5752 && (ptr[col] == 'X'
5753 || ptr[col] == 'x')
5754 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005755#ifdef FEAT_MBYTE
5756 && (!has_mbyte ||
5757 !(*mb_head_off)(ptr, ptr + col - 1))
5758#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005759 && vim_isxdigit(ptr[col + 1])) ||
5760 ( dobin
5761 && col > 0
5762 && (ptr[col] == 'B'
5763 || ptr[col] == 'b')
5764 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005765#ifdef FEAT_MBYTE
5766 && (!has_mbyte ||
5767 !(*mb_head_off)(ptr, ptr + col - 1))
5768#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005769 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005770 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005771 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005772 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005773#ifdef FEAT_MBYTE
5774 if (has_mbyte)
5775 col -= (*mb_head_off)(ptr, ptr + col);
5776#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005777 }
5778 else
5779 {
5780 /*
5781 * Search forward and then backward to find the start of number.
5782 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005783 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005784
5785 while (ptr[col] != NUL
5786 && !vim_isdigit(ptr[col])
5787 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005788 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005789
5790 while (col > 0
5791 && vim_isdigit(ptr[col - 1])
5792 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005793 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005794 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005795#ifdef FEAT_MBYTE
5796 if (has_mbyte)
5797 col -= (*mb_head_off)(ptr, ptr + col);
5798#endif
5799 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005800 }
5801 }
5802
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005803 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005804 {
5805 while (ptr[col] != NUL && length > 0
5806 && !vim_isdigit(ptr[col])
5807 && !(doalp && ASCII_ISALPHA(ptr[col])))
5808 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005809 int mb_len = MB_PTR2LEN(ptr + col);
5810
5811 col += mb_len;
5812 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005813 }
5814
5815 if (length == 0)
5816 goto theend;
5817
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005818 if (col > pos->col && ptr[col - 1] == '-'
5819#ifdef FEAT_MBYTE
5820 && (!has_mbyte ||
5821 !(*mb_head_off)(ptr, ptr + col - 1))
5822#endif
5823 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005824 {
5825 negative = TRUE;
5826 was_positive = FALSE;
5827 }
5828 }
5829
5830 /*
5831 * If a number was found, and saving for undo works, replace the number.
5832 */
5833 firstdigit = ptr[col];
5834 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5835 {
5836 beep_flush();
5837 goto theend;
5838 }
5839
5840 if (doalp && ASCII_ISALPHA(firstdigit))
5841 {
5842 /* decrement or increment alphabetic character */
5843 if (op_type == OP_NR_SUB)
5844 {
5845 if (CharOrd(firstdigit) < Prenum1)
5846 {
5847 if (isupper(firstdigit))
5848 firstdigit = 'A';
5849 else
5850 firstdigit = 'a';
5851 }
5852 else
5853#ifdef EBCDIC
5854 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5855#else
5856 firstdigit -= Prenum1;
5857#endif
5858 }
5859 else
5860 {
5861 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5862 {
5863 if (isupper(firstdigit))
5864 firstdigit = 'Z';
5865 else
5866 firstdigit = 'z';
5867 }
5868 else
5869#ifdef EBCDIC
5870 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5871#else
5872 firstdigit += Prenum1;
5873#endif
5874 }
5875 curwin->w_cursor.col = col;
5876 if (!did_change)
5877 startpos = curwin->w_cursor;
5878 did_change = TRUE;
5879 (void)del_char(FALSE);
5880 ins_char(firstdigit);
5881 endpos = curwin->w_cursor;
5882 curwin->w_cursor.col = col;
5883 }
5884 else
5885 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005886 if (col > 0 && ptr[col - 1] == '-'
5887#ifdef FEAT_MBYTE
5888 && (!has_mbyte ||
5889 !(*mb_head_off)(ptr, ptr + col - 1))
5890#endif
5891 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005892 {
5893 /* negative number */
5894 --col;
5895 negative = TRUE;
5896 }
5897 /* get the number value (unsigned) */
5898 if (visual && VIsual_mode != 'V')
5899 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5900 ? (int)STRLEN(ptr) - col
5901 : length);
5902
5903 vim_str2nr(ptr + col, &pre, &length,
5904 0 + (dobin ? STR2NR_BIN : 0)
5905 + (dooct ? STR2NR_OCT : 0)
5906 + (dohex ? STR2NR_HEX : 0),
5907 NULL, &n, maxlen);
5908
5909 /* ignore leading '-' for hex and octal and bin numbers */
5910 if (pre && negative)
5911 {
5912 ++col;
5913 --length;
5914 negative = FALSE;
5915 }
5916 /* add or subtract */
5917 subtract = FALSE;
5918 if (op_type == OP_NR_SUB)
5919 subtract ^= TRUE;
5920 if (negative)
5921 subtract ^= TRUE;
5922
5923 oldn = n;
5924 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005925 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005926 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005927 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005928 /* handle wraparound for decimal numbers */
5929 if (!pre)
5930 {
5931 if (subtract)
5932 {
5933 if (n > oldn)
5934 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005935 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005936 negative ^= TRUE;
5937 }
5938 }
5939 else
5940 {
5941 /* add */
5942 if (n < oldn)
5943 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005944 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005945 negative ^= TRUE;
5946 }
5947 }
5948 if (n == 0)
5949 negative = FALSE;
5950 }
5951
5952 if (visual && !was_positive && !negative && col > 0)
5953 {
5954 /* need to remove the '-' */
5955 col--;
5956 length++;
5957 }
5958
5959 /*
5960 * Delete the old number.
5961 */
5962 curwin->w_cursor.col = col;
5963 if (!did_change)
5964 startpos = curwin->w_cursor;
5965 did_change = TRUE;
5966 todel = length;
5967 c = gchar_cursor();
5968 /*
5969 * Don't include the '-' in the length, only the length of the
5970 * part after it is kept the same.
5971 */
5972 if (c == '-')
5973 --length;
5974 while (todel-- > 0)
5975 {
5976 if (c < 0x100 && isalpha(c))
5977 {
5978 if (isupper(c))
5979 hexupper = TRUE;
5980 else
5981 hexupper = FALSE;
5982 }
5983 /* del_char() will mark line needing displaying */
5984 (void)del_char(FALSE);
5985 c = gchar_cursor();
5986 }
5987
5988 /*
5989 * Prepare the leading characters in buf1[].
5990 * When there are many leading zeros it could be very long.
5991 * Allocate a bit too much.
5992 */
5993 buf1 = alloc((unsigned)length + NUMBUFLEN);
5994 if (buf1 == NULL)
5995 goto theend;
5996 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005997 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005998 {
5999 *ptr++ = '-';
6000 }
6001 if (pre)
6002 {
6003 *ptr++ = '0';
6004 --length;
6005 }
6006 if (pre == 'b' || pre == 'B' ||
6007 pre == 'x' || pre == 'X')
6008 {
6009 *ptr++ = pre;
6010 --length;
6011 }
6012
6013 /*
6014 * Put the number characters in buf2[].
6015 */
6016 if (pre == 'b' || pre == 'B')
6017 {
6018 int i;
6019 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006020 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006021
6022 /* leading zeros */
6023 for (bit = bits; bit > 0; bit--)
6024 if ((n >> (bit - 1)) & 0x1) break;
6025
6026 for (i = 0; bit > 0; bit--)
6027 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6028
6029 buf2[i] = '\0';
6030 }
6031 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006032 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6033 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006034 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006035 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6036 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006037 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006038 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6039 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006040 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006041 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6042 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006043 length -= (int)STRLEN(buf2);
6044
6045 /*
6046 * Adjust number of zeros to the new number of digits, so the
6047 * total length of the number remains the same.
6048 * Don't do this when
6049 * the result may look like an octal number.
6050 */
6051 if (firstdigit == '0' && !(dooct && pre == 0))
6052 while (length-- > 0)
6053 *ptr++ = '0';
6054 *ptr = NUL;
6055 STRCAT(buf1, buf2);
6056 ins_str(buf1); /* insert the new number */
6057 vim_free(buf1);
6058 endpos = curwin->w_cursor;
6059 if (did_change && curwin->w_cursor.col)
6060 --curwin->w_cursor.col;
6061 }
6062
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006063 if (did_change)
6064 {
6065 /* set the '[ and '] marks */
6066 curbuf->b_op_start = startpos;
6067 curbuf->b_op_end = endpos;
6068 if (curbuf->b_op_end.col > 0)
6069 --curbuf->b_op_end.col;
6070 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006071
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006072theend:
6073 if (visual)
6074 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006075 else if (did_change)
6076 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006077
Bram Moolenaard79e5502016-01-10 22:13:02 +01006078 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079}
6080
6081#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006082
6083static yankreg_T *y_read_regs = NULL;
6084
6085#define REG_PREVIOUS 1
6086#define REG_EXEC 2
6087
6088/*
6089 * Prepare for reading viminfo registers when writing viminfo later.
6090 */
6091 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006092prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006093{
6094 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6095 * (int)sizeof(yankreg_T));
6096}
6097
6098 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006099finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006100{
6101 int i;
6102 int j;
6103
6104 if (y_read_regs != NULL)
6105 {
6106 for (i = 0; i < NUM_REGISTERS; ++i)
6107 if (y_read_regs[i].y_array != NULL)
6108 {
6109 for (j = 0; j < y_read_regs[i].y_size; j++)
6110 vim_free(y_read_regs[i].y_array[j]);
6111 vim_free(y_read_regs[i].y_array);
6112 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006113 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006114 }
6115}
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006118read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 int eof;
6121 int do_it = TRUE;
6122 int size;
6123 int limit;
6124 int i;
6125 int set_prev = FALSE;
6126 char_u *str;
6127 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006128 int new_type = MCHAR; /* init to shut up compiler */
6129 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 /* We only get here (hopefully) if line[0] == '"' */
6132 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006133
6134 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (*str == '"')
6136 {
6137 set_prev = TRUE;
6138 str++;
6139 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (!ASCII_ISALNUM(*str) && *str != '-')
6142 {
6143 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6144 return TRUE; /* too many errors, pretend end-of-file */
6145 do_it = FALSE;
6146 }
6147 get_yank_register(*str++, FALSE);
6148 if (!force && y_current->y_array != NULL)
6149 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006150
6151 if (*str == '@')
6152 {
6153 /* "x@: register x used for @@ */
6154 if (force || execreg_lastc == NUL)
6155 execreg_lastc = str[-1];
6156 }
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 size = 0;
6159 limit = 100; /* Optimized for registers containing <= 100 lines */
6160 if (do_it)
6161 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006162 /*
6163 * Build the new register in array[].
6164 * y_array is kept as-is until done.
6165 * The "do_it" flag is reset when something is wrong, in which case
6166 * array[] needs to be freed.
6167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (set_prev)
6169 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006170 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006171 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006173 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006175 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006177 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 /* get the block width; if it's missing we get a zero, which is OK */
6179 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006180 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182
6183 while (!(eof = viminfo_readline(virp))
6184 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6185 {
6186 if (do_it)
6187 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006188 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006190 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006192
6193 if (new_array == NULL)
6194 {
6195 do_it = FALSE;
6196 break;
6197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006199 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006201 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 }
6204 str = viminfo_readstring(virp, 1, TRUE);
6205 if (str != NULL)
6206 array[size++] = str;
6207 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006208 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 do_it = FALSE;
6210 }
6211 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (do_it)
6214 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006215 /* free y_array[] */
6216 for (i = 0; i < y_current->y_size; i++)
6217 vim_free(y_current->y_array[i]);
6218 vim_free(y_current->y_array);
6219
6220 y_current->y_type = new_type;
6221 y_current->y_width = new_width;
6222 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006223 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (size == 0)
6225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 y_current->y_array = NULL;
6227 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006230 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 y_current->y_array =
6232 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6233 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006234 {
6235 if (y_current->y_array == NULL)
6236 vim_free(array[i]);
6237 else
6238 y_current->y_array[i] = array[i];
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006242 else
6243 {
6244 /* Free array[] if it was filled. */
6245 for (i = 0; i < size; i++)
6246 vim_free(array[i]);
6247 }
6248 vim_free(array);
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 return eof;
6251}
6252
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006253/*
6254 * Accept a new style register line from the viminfo, store it when it's new.
6255 */
6256 void
6257handle_viminfo_register(garray_T *values, int force)
6258{
6259 bval_T *vp = (bval_T *)values->ga_data;
6260 int flags;
6261 int name;
6262 int type;
6263 int linecount;
6264 int width;
6265 time_t timestamp;
6266 yankreg_T *y_ptr;
6267 int i;
6268
6269 /* Check the format:
6270 * |{bartype},{flags},{name},{type},
6271 * {linecount},{width},{timestamp},"line1","line2"
6272 */
6273 if (values->ga_len < 6
6274 || vp[0].bv_type != BVAL_NR
6275 || vp[1].bv_type != BVAL_NR
6276 || vp[2].bv_type != BVAL_NR
6277 || vp[3].bv_type != BVAL_NR
6278 || vp[4].bv_type != BVAL_NR
6279 || vp[5].bv_type != BVAL_NR)
6280 return;
6281 flags = vp[0].bv_nr;
6282 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006283 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006284 return;
6285 type = vp[2].bv_nr;
6286 if (type != MCHAR && type != MLINE && type != MBLOCK)
6287 return;
6288 linecount = vp[3].bv_nr;
6289 if (values->ga_len < 6 + linecount)
6290 return;
6291 width = vp[4].bv_nr;
6292 if (width < 0)
6293 return;
6294
6295 if (y_read_regs != NULL)
6296 /* Reading viminfo for merging and writing. Store the register
6297 * content, don't update the current registers. */
6298 y_ptr = &y_read_regs[name];
6299 else
6300 y_ptr = &y_regs[name];
6301
6302 /* Do not overwrite unless forced or the timestamp is newer. */
6303 timestamp = (time_t)vp[5].bv_nr;
6304 if (y_ptr->y_array != NULL && !force
6305 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6306 return;
6307
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006308 if (y_ptr->y_array != NULL)
6309 for (i = 0; i < y_ptr->y_size; i++)
6310 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006311 vim_free(y_ptr->y_array);
6312
6313 if (y_read_regs == NULL)
6314 {
6315 if (flags & REG_PREVIOUS)
6316 y_previous = y_ptr;
6317 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6318 execreg_lastc = get_register_name(name);
6319 }
6320 y_ptr->y_type = type;
6321 y_ptr->y_width = width;
6322 y_ptr->y_size = linecount;
6323 y_ptr->y_time_set = timestamp;
6324 if (linecount == 0)
6325 y_ptr->y_array = NULL;
6326 else
6327 {
6328 y_ptr->y_array =
6329 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6330 for (i = 0; i < linecount; i++)
6331 {
6332 if (vp[i + 6].bv_allocated)
6333 {
6334 y_ptr->y_array[i] = vp[i + 6].bv_string;
6335 vp[i + 6].bv_string = NULL;
6336 }
6337 else
6338 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6339 }
6340 }
6341}
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006346 int i, j;
6347 char_u *type;
6348 char_u c;
6349 int num_lines;
6350 int max_num_lines;
6351 int max_kbyte;
6352 long len;
6353 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
Bram Moolenaar64404472010-06-26 06:24:45 +02006355 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
6357 /* Get '<' value, use old '"' value if '<' is not found. */
6358 max_num_lines = get_viminfo_parameter('<');
6359 if (max_num_lines < 0)
6360 max_num_lines = get_viminfo_parameter('"');
6361 if (max_num_lines == 0)
6362 return;
6363 max_kbyte = get_viminfo_parameter('s');
6364 if (max_kbyte == 0)
6365 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006366
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 for (i = 0; i < NUM_REGISTERS; i++)
6368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#ifdef FEAT_CLIPBOARD
6370 /* Skip '*'/'+' register, we don't want them back next time */
6371 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6372 continue;
6373#endif
6374#ifdef FEAT_DND
6375 /* Neither do we want the '~' register */
6376 if (i == TILDE_REGISTER)
6377 continue;
6378#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006379 /* When reading viminfo for merging and writing: Use the register from
6380 * viminfo if it's newer. */
6381 if (y_read_regs != NULL
6382 && y_read_regs[i].y_array != NULL
6383 && (y_regs[i].y_array == NULL ||
6384 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6385 y_ptr = &y_read_regs[i];
6386 else if (y_regs[i].y_array == NULL)
6387 continue;
6388 else
6389 y_ptr = &y_regs[i];
6390
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006391 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006392 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006393 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006394 || (num_lines == 1 && y_ptr->y_type == MCHAR
6395 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006396 continue;
6397
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (max_kbyte > 0)
6399 {
6400 /* Skip register if there is more text than the maximum size. */
6401 len = 0;
6402 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006403 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (len > (long)max_kbyte * 1024L)
6405 continue;
6406 }
6407
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006408 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 {
6410 case MLINE:
6411 type = (char_u *)"LINE";
6412 break;
6413 case MCHAR:
6414 type = (char_u *)"CHAR";
6415 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case MBLOCK:
6417 type = (char_u *)"BLOCK";
6418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 default:
6420 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006421 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 emsg(IObuff);
6423 type = (char_u *)"LINE";
6424 break;
6425 }
6426 if (y_previous == &y_regs[i])
6427 fprintf(fp, "\"");
6428 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006429 fprintf(fp, "\"%c", c);
6430 if (c == execreg_lastc)
6431 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006432 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434 /* If max_num_lines < 0, then we save ALL the lines in the register */
6435 if (max_num_lines > 0 && num_lines > max_num_lines)
6436 num_lines = max_num_lines;
6437 for (j = 0; j < num_lines; j++)
6438 {
6439 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006440 viminfo_writestring(fp, y_ptr->y_array[j]);
6441 }
6442
6443 {
6444 int flags = 0;
6445 int remaining;
6446
6447 /* New style with a bar line. Format:
6448 * |{bartype},{flags},{name},{type},
6449 * {linecount},{width},{timestamp},"line1","line2"
6450 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006451 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006452 */
6453 if (y_previous == &y_regs[i])
6454 flags |= REG_PREVIOUS;
6455 if (c == execreg_lastc)
6456 flags |= REG_EXEC;
6457 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6458 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6459 (long)y_ptr->y_time_set);
6460 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6461 remaining = LSIZE - 71;
6462 for (j = 0; j < num_lines; j++)
6463 {
6464 putc(',', fp);
6465 --remaining;
6466 remaining = barline_writestring(fp, y_ptr->y_array[j],
6467 remaining);
6468 }
6469 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
6471 }
6472}
6473#endif /* FEAT_VIMINFO */
6474
6475#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6476/*
6477 * SELECTION / PRIMARY ('*')
6478 *
6479 * Text selection stuff that uses the GUI selection register '*'. When using a
6480 * GUI this may be text from another window, otherwise it is the last text we
6481 * had highlighted with VIsual mode. With mouse support, clicking the middle
6482 * button performs the paste, otherwise you will need to do <"*p>. "
6483 * If not under X, it is synonymous with the clipboard register '+'.
6484 *
6485 * X CLIPBOARD ('+')
6486 *
6487 * Text selection stuff that uses the GUI clipboard register '+'.
6488 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6489 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6490 * otherwise you will need to do <"+p>. "
6491 * If not under X, it is synonymous with the selection register '*'.
6492 */
6493
6494/*
6495 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006496 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 * only exist while the owning application exists, so we write to the
6498 * permanent (while X runs) store CUT_BUFFER0.
6499 * Dump the CLIPBOARD selection if we own it (it's logically the more
6500 * 'permanent' of the two), otherwise the PRIMARY one.
6501 * For now, use a hard-coded sanity limit of 1Mb of data.
6502 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006503#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 Display *dpy;
6508 char_u *str = NULL;
6509 long_u len = 0;
6510 int motion_type = -1;
6511
6512# ifdef FEAT_GUI
6513 if (gui.in_use)
6514 dpy = X_DISPLAY;
6515 else
6516# endif
6517# ifdef FEAT_XCLIPBOARD
6518 dpy = xterm_dpy;
6519# else
6520 return;
6521# endif
6522
6523 /* Get selection to export */
6524 if (clip_plus.owned)
6525 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6526 else if (clip_star.owned)
6527 motion_type = clip_convert_selection(&str, &len, &clip_star);
6528
6529 /* Check it's OK */
6530 if (dpy != NULL && str != NULL && motion_type >= 0
6531 && len < 1024*1024 && len > 0)
6532 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006533#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006534 int ok = TRUE;
6535
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006536 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6537 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6538 * encoding conversion usually doesn't work, so keep the text as-is.
6539 */
6540 if (has_mbyte)
6541 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006542 vimconv_T vc;
6543
6544 vc.vc_type = CONV_NONE;
6545 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6546 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006547 int intlen = len;
6548 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006549
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006550 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006551 conv_str = string_convert(&vc, str, &intlen);
6552 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006553 if (conv_str != NULL)
6554 {
6555 vim_free(str);
6556 str = conv_str;
6557 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006558 else
6559 {
6560 ok = FALSE;
6561 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006562 convert_setup(&vc, NULL, NULL);
6563 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006564 else
6565 {
6566 ok = FALSE;
6567 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006568 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006569
6570 /* Do not store the string if conversion failed. Better to use any
6571 * other selection than garbled text. */
6572 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006573#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006574 {
6575 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6576 XFlush(dpy);
6577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 }
6579
6580 vim_free(str);
6581}
6582#endif
6583
6584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006585clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006587 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 if (cbd == &clip_plus)
6590 y_current = &y_regs[PLUS_REGISTER];
6591 else
6592 y_current = &y_regs[STAR_REGISTER];
6593 free_yank_all();
6594 y_current->y_size = 0;
6595 y_current = y_ptr;
6596}
6597
6598/*
6599 * Get the selected text and put it in the gui selection register '*' or '+'.
6600 */
6601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006604 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 pos_T old_visual;
6607 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 colnr_T old_curswant;
6609 int old_set_curswant;
6610 pos_T old_op_start, old_op_end;
6611 oparg_T oa;
6612 cmdarg_T ca;
6613
6614 if (cbd->owned)
6615 {
6616 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6617 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6618 return;
6619
6620 /* Get the text between clip_star.start & clip_star.end */
6621 old_y_previous = y_previous;
6622 old_y_current = y_current;
6623 old_cursor = curwin->w_cursor;
6624 old_curswant = curwin->w_curswant;
6625 old_set_curswant = curwin->w_set_curswant;
6626 old_op_start = curbuf->b_op_start;
6627 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 old_visual = VIsual;
6629 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 clear_oparg(&oa);
6631 oa.regname = (cbd == &clip_plus ? '+' : '*');
6632 oa.op_type = OP_YANK;
6633 vim_memset(&ca, 0, sizeof(ca));
6634 ca.oap = &oa;
6635 ca.cmdchar = 'y';
6636 ca.count1 = 1;
6637 ca.retval = CA_NO_ADJ_OP_END;
6638 do_pending_operator(&ca, 0, TRUE);
6639 y_previous = old_y_previous;
6640 y_current = old_y_current;
6641 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006642 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 curwin->w_curswant = old_curswant;
6644 curwin->w_set_curswant = old_set_curswant;
6645 curbuf->b_op_start = old_op_start;
6646 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 VIsual = old_visual;
6648 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006650 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
6652 clip_free_selection(cbd);
6653
6654 /* Try to get selected text from another window */
6655 clip_gen_request_selection(cbd);
6656 }
6657}
6658
Bram Moolenaard44347f2011-06-19 01:14:29 +02006659/*
6660 * Convert from the GUI selection string into the '*'/'+' register.
6661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006663clip_yank_selection(
6664 int type,
6665 char_u *str,
6666 long len,
6667 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006669 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 if (cbd == &clip_plus)
6672 y_ptr = &y_regs[PLUS_REGISTER];
6673 else
6674 y_ptr = &y_regs[STAR_REGISTER];
6675
6676 clip_free_selection(cbd);
6677
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006678 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681/*
6682 * Convert the '*'/'+' register into a GUI selection string returned in *str
6683 * with length *len.
6684 * Returns the motion type, or -1 for failure.
6685 */
6686 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006687clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 char_u *p;
6690 int lnum;
6691 int i, j;
6692 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006693 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695 if (cbd == &clip_plus)
6696 y_ptr = &y_regs[PLUS_REGISTER];
6697 else
6698 y_ptr = &y_regs[STAR_REGISTER];
6699
6700#ifdef USE_CRNL
6701 eolsize = 2;
6702#else
6703 eolsize = 1;
6704#endif
6705
6706 *str = NULL;
6707 *len = 0;
6708 if (y_ptr->y_array == NULL)
6709 return -1;
6710
6711 for (i = 0; i < y_ptr->y_size; i++)
6712 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6713
6714 /*
6715 * Don't want newline character at end of last line if we're in MCHAR mode.
6716 */
6717 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6718 *len -= eolsize;
6719
6720 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6721 if (p == NULL)
6722 return -1;
6723 lnum = 0;
6724 for (i = 0, j = 0; i < (int)*len; i++, j++)
6725 {
6726 if (y_ptr->y_array[lnum][j] == '\n')
6727 p[i] = NUL;
6728 else if (y_ptr->y_array[lnum][j] == NUL)
6729 {
6730#ifdef USE_CRNL
6731 p[i++] = '\r';
6732#endif
6733#ifdef USE_CR
6734 p[i] = '\r';
6735#else
6736 p[i] = '\n';
6737#endif
6738 lnum++;
6739 j = -1;
6740 }
6741 else
6742 p[i] = y_ptr->y_array[lnum][j];
6743 }
6744 return y_ptr->y_type;
6745}
6746
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748/*
6749 * If we have written to a clipboard register, send the text to the clipboard.
6750 */
6751 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006752may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6755 {
6756 clip_own_selection(&clip_star);
6757 clip_gen_set_selection(&clip_star);
6758 }
6759 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6760 {
6761 clip_own_selection(&clip_plus);
6762 clip_gen_set_selection(&clip_plus);
6763 }
6764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766#endif /* FEAT_CLIPBOARD || PROTO */
6767
6768
6769#if defined(FEAT_DND) || defined(PROTO)
6770/*
6771 * Replace the contents of the '~' register with str.
6772 */
6773 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006776 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778 curr = y_current;
6779 y_current = &y_regs[TILDE_REGISTER];
6780 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006781 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 y_current = curr;
6783}
6784#endif
6785
6786
6787#if defined(FEAT_EVAL) || defined(PROTO)
6788/*
6789 * Return the type of a register.
6790 * Used for getregtype()
6791 * Returns MAUTO for error.
6792 */
6793 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006794get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 switch (regname)
6797 {
6798 case '%': /* file name */
6799 case '#': /* alternate file name */
6800 case '=': /* expression */
6801 case ':': /* last command line */
6802 case '/': /* last search-pattern */
6803 case '.': /* last inserted text */
6804#ifdef FEAT_SEARCHPATH
6805 case Ctrl_F: /* Filename under cursor */
6806 case Ctrl_P: /* Path under cursor, expand via "path" */
6807#endif
6808 case Ctrl_W: /* word under cursor */
6809 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6810 case '_': /* black hole: always empty */
6811 return MCHAR;
6812 }
6813
6814#ifdef FEAT_CLIPBOARD
6815 regname = may_get_selection(regname);
6816#endif
6817
Bram Moolenaar32b92012014-01-14 12:33:36 +01006818 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006819 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 get_yank_register(regname, FALSE);
6822
6823 if (y_current->y_array != NULL)
6824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (reglen != NULL && y_current->y_type == MBLOCK)
6826 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 return y_current->y_type;
6828 }
6829 return MAUTO;
6830}
6831
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006832static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006833
6834/*
6835 * When "flags" has GREG_LIST return a list with text "s".
6836 * Otherwise just return "s".
6837 */
6838 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006839getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006840{
6841 if (flags & GREG_LIST)
6842 {
6843 list_T *list = list_alloc();
6844
6845 if (list != NULL)
6846 {
6847 if (list_append_string(list, NULL, -1) == FAIL)
6848 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006849 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006850 return NULL;
6851 }
6852 list->lv_first->li_tv.vval.v_string = s;
6853 }
6854 return (char_u *)list;
6855 }
6856 return s;
6857}
6858
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859/*
6860 * Return the contents of a register as a single allocated string.
6861 * Used for "@r" in expressions and for getreg().
6862 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006863 * Flags:
6864 * GREG_NO_EXPR Do not allow expression register
6865 * GREG_EXPR_SRC For the expression register: return expression itself,
6866 * not the result of its evaluation.
6867 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 */
6869 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006870get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
6872 long i;
6873 char_u *retval;
6874 int allocated;
6875 long len;
6876
6877 /* Don't allow using an expression register inside an expression */
6878 if (regname == '=')
6879 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006880 if (flags & GREG_NO_EXPR)
6881 return NULL;
6882 if (flags & GREG_EXPR_SRC)
6883 return getreg_wrap_one_line(get_expr_line_src(), flags);
6884 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
6887 if (regname == '@') /* "@@" is used for unnamed register */
6888 regname = '"';
6889
6890 /* check for valid regname */
6891 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6892 return NULL;
6893
6894#ifdef FEAT_CLIPBOARD
6895 regname = may_get_selection(regname);
6896#endif
6897
6898 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6899 {
6900 if (retval == NULL)
6901 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006902 if (allocated)
6903 return getreg_wrap_one_line(retval, flags);
6904 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 }
6906
6907 get_yank_register(regname, FALSE);
6908 if (y_current->y_array == NULL)
6909 return NULL;
6910
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006911 if (flags & GREG_LIST)
6912 {
6913 list_T *list = list_alloc();
6914 int error = FALSE;
6915
6916 if (list == NULL)
6917 return NULL;
6918 for (i = 0; i < y_current->y_size; ++i)
6919 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6920 error = TRUE;
6921 if (error)
6922 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006923 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006924 return NULL;
6925 }
6926 return (char_u *)list;
6927 }
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 /*
6930 * Compute length of resulting string.
6931 */
6932 len = 0;
6933 for (i = 0; i < y_current->y_size; ++i)
6934 {
6935 len += (long)STRLEN(y_current->y_array[i]);
6936 /*
6937 * Insert a newline between lines and after last line if
6938 * y_type is MLINE.
6939 */
6940 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6941 ++len;
6942 }
6943
6944 retval = lalloc(len + 1, TRUE);
6945
6946 /*
6947 * Copy the lines of the yank register into the string.
6948 */
6949 if (retval != NULL)
6950 {
6951 len = 0;
6952 for (i = 0; i < y_current->y_size; ++i)
6953 {
6954 STRCPY(retval + len, y_current->y_array[i]);
6955 len += (long)STRLEN(retval + len);
6956
6957 /*
6958 * Insert a NL between lines and after the last line if y_type is
6959 * MLINE.
6960 */
6961 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6962 retval[len++] = '\n';
6963 }
6964 retval[len] = NUL;
6965 }
6966
6967 return retval;
6968}
6969
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006970 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006971init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006972 int name,
6973 yankreg_T **old_y_previous,
6974 yankreg_T **old_y_current,
6975 int must_append,
6976 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006977{
6978 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6979 {
6980 emsg_invreg(name);
6981 return FAIL;
6982 }
6983
6984 /* Don't want to change the current (unnamed) register */
6985 *old_y_previous = y_previous;
6986 *old_y_current = y_current;
6987
6988 get_yank_register(name, TRUE);
6989 if (!y_append && !must_append)
6990 free_yank_all();
6991 return OK;
6992}
6993
6994 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006995finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006996 int name,
6997 yankreg_T *old_y_previous,
6998 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006999{
7000# ifdef FEAT_CLIPBOARD
7001 /* Send text of clipboard register to the clipboard. */
7002 may_set_selection();
7003# endif
7004
7005 /* ':let @" = "val"' should change the meaning of the "" register */
7006 if (name != '"')
7007 y_previous = old_y_previous;
7008 y_current = old_y_current;
7009}
7010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011/*
7012 * Store string "str" in register "name".
7013 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7014 * If "must_append" is TRUE, always append to the register. Otherwise append
7015 * if "name" is an uppercase letter.
7016 * Note: "maxlen" and "must_append" don't work for the "/" register.
7017 * Careful: 'str' is modified, you may have to use a copy!
7018 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7019 */
7020 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007021write_reg_contents(
7022 int name,
7023 char_u *str,
7024 int maxlen,
7025 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026{
7027 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7028}
7029
7030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007031write_reg_contents_lst(
7032 int name,
7033 char_u **strings,
7034 int maxlen UNUSED,
7035 int must_append,
7036 int yank_type,
7037 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007038{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007039 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007040
7041 if (name == '/'
7042#ifdef FEAT_EVAL
7043 || name == '='
7044#endif
7045 )
7046 {
7047 char_u *s;
7048
7049 if (strings[0] == NULL)
7050 s = (char_u *)"";
7051 else if (strings[1] != NULL)
7052 {
7053 EMSG(_("E883: search pattern and expression register may not "
7054 "contain two or more lines"));
7055 return;
7056 }
7057 else
7058 s = strings[0];
7059 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7060 return;
7061 }
7062
7063 if (name == '_') /* black hole: nothing to do */
7064 return;
7065
7066 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7067 &yank_type) == FAIL)
7068 return;
7069
7070 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7071
7072 finish_write_reg(name, old_y_previous, old_y_current);
7073}
7074
7075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076write_reg_contents_ex(
7077 int name,
7078 char_u *str,
7079 int maxlen,
7080 int must_append,
7081 int yank_type,
7082 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007084 yankreg_T *old_y_previous, *old_y_current;
7085 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
Bram Moolenaare7566042005-06-17 22:00:15 +00007087 if (maxlen >= 0)
7088 len = maxlen;
7089 else
7090 len = (long)STRLEN(str);
7091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 /* Special case: '/' search pattern */
7093 if (name == '/')
7094 {
7095 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7096 return;
7097 }
7098
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007099 if (name == '#')
7100 {
7101 buf_T *buf;
7102
7103 if (VIM_ISDIGIT(*str))
7104 {
7105 int num = atoi((char *)str);
7106
7107 buf = buflist_findnr(num);
7108 if (buf == NULL)
7109 EMSGN(_(e_nobufnr), (long)num);
7110 }
7111 else
7112 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7113 TRUE, FALSE, FALSE));
7114 if (buf == NULL)
7115 return;
7116 curwin->w_alt_fnum = buf->b_fnum;
7117 return;
7118 }
7119
Bram Moolenaare7566042005-06-17 22:00:15 +00007120#ifdef FEAT_EVAL
7121 if (name == '=')
7122 {
7123 char_u *p, *s;
7124
7125 p = vim_strnsave(str, (int)len);
7126 if (p == NULL)
7127 return;
7128 if (must_append)
7129 {
7130 s = concat_str(get_expr_line_src(), p);
7131 vim_free(p);
7132 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007133 }
7134 set_expr_line(p);
7135 return;
7136 }
7137#endif
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (name == '_') /* black hole: nothing to do */
7140 return;
7141
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007142 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7143 &yank_type) == FAIL)
7144 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007146 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007148 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150#endif /* FEAT_EVAL */
7151
7152#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7153/*
7154 * Put a string into a register. When the register is not empty, the string
7155 * is appended.
7156 */
7157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007158str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007159 yankreg_T *y_ptr, /* pointer to yank register */
7160 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7161 char_u *str, /* string to put in register */
7162 long len, /* length of string */
7163 long blocklen, /* width of Visual block */
7164 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007166 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 int lnum;
7168 long start;
7169 long i;
7170 int extra;
7171 int newlines; /* number of lines added */
7172 int extraline = 0; /* extra line at the end */
7173 int append = FALSE; /* append to last line in register */
7174 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007175 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007179 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 y_ptr->y_size = 0;
7181
Bram Moolenaard44347f2011-06-19 01:14:29 +02007182 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007183 type = ((str_list || (len > 0 && (str[len - 1] == NL
7184 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007185 ? MLINE : MCHAR);
7186 else
7187 type = yank_type;
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 /*
7190 * Count the number of lines within the string
7191 */
7192 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007193 if (str_list)
7194 {
7195 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007200 for (i = 0; i < len; i++)
7201 if (str[i] == '\n')
7202 ++newlines;
7203 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7204 {
7205 extraline = 1;
7206 ++newlines; /* count extra newline at the end */
7207 }
7208 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7209 {
7210 append = TRUE;
7211 --newlines; /* uncount newline when appending first line */
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007215 /* Without any lines make the register empty. */
7216 if (y_ptr->y_size + newlines == 0)
7217 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007218 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007219 return;
7220 }
7221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 /*
7223 * Allocate an array to hold the pointers to the new register lines.
7224 * If the register was not empty, move the existing lines to the new array.
7225 */
7226 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7227 * sizeof(char_u *), TRUE);
7228 if (pp == NULL) /* out of memory */
7229 return;
7230 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7231 pp[lnum] = y_ptr->y_array[lnum];
7232 vim_free(y_ptr->y_array);
7233 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
7236 /*
7237 * Find the end of each line and save it into the array.
7238 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007239 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007241 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7242 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007243 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007244 pp[lnum] = vim_strnsave(*ss, i);
7245 if (i > maxlen)
7246 maxlen = i;
7247 }
7248 }
7249 else
7250 {
7251 for (start = 0; start < len + extraline; start += i + 1)
7252 {
7253 for (i = start; i < len; ++i) /* find the end of the line */
7254 if (str[i] == '\n')
7255 break;
7256 i -= start; /* i is now length of line */
7257 if (i > maxlen)
7258 maxlen = i;
7259 if (append)
7260 {
7261 --lnum;
7262 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7263 }
7264 else
7265 extra = 0;
7266 s = alloc((unsigned)(i + extra + 1));
7267 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007269 if (extra)
7270 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7271 if (append)
7272 vim_free(y_ptr->y_array[lnum]);
7273 if (i)
7274 mch_memmove(s + extra, str + start, (size_t)i);
7275 extra += i;
7276 s[extra] = NUL;
7277 y_ptr->y_array[lnum++] = s;
7278 while (--extra >= 0)
7279 {
7280 if (*s == NUL)
7281 *s = '\n'; /* replace NUL with newline */
7282 ++s;
7283 }
7284 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
7287 y_ptr->y_type = type;
7288 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (type == MBLOCK)
7290 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7291 else
7292 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007293#ifdef FEAT_VIMINFO
7294 y_ptr->y_time_set = vim_time();
7295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7298
7299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007300clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 vim_memset(oap, 0, sizeof(oparg_T));
7303}
7304
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007305static 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 +00007306
7307/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007308 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 *
7310 * "Words" are counted by looking for boundaries between non-space and
7311 * space characters. (it seems to produce results that match 'wc'.)
7312 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 * Return value is byte count; word count for the line is added to "*wc".
7314 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 *
7316 * The function will only examine the first "limit" characters in the
7317 * line, stopping if it encounters an end-of-line (NUL byte). In that
7318 * case, eol_size will be added to the character count to account for
7319 * the size of the EOL character.
7320 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007321 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007322line_count_info(
7323 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007324 varnumber_T *wc,
7325 varnumber_T *cc,
7326 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007327 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007329 varnumber_T i;
7330 varnumber_T words = 0;
7331 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 int is_word = 0;
7333
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007334 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 if (is_word)
7337 {
7338 if (vim_isspace(line[i]))
7339 {
7340 words++;
7341 is_word = 0;
7342 }
7343 }
7344 else if (!vim_isspace(line[i]))
7345 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007346 ++chars;
7347#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007348 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007349#else
7350 ++i;
7351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
7353
7354 if (is_word)
7355 words++;
7356 *wc += words;
7357
7358 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007359 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007362 chars += eol_size;
7363 }
7364 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 return i;
7366}
7367
7368/*
7369 * Give some info about the position of the cursor (for "g CTRL-G").
7370 * In Visual mode, give some info about the selected region. (In this case,
7371 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007372 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 */
7374 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007375cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007378 char_u buf1[50];
7379 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007381 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007382#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007383 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007384#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007385 varnumber_T byte_count_cursor = 0;
7386 varnumber_T char_count = 0;
7387 varnumber_T char_count_cursor = 0;
7388 varnumber_T word_count = 0;
7389 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007390 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007391 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 long line_count_selected = 0;
7393 pos_T min_pos, max_pos;
7394 oparg_T oparg;
7395 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
7397 /*
7398 * Compute the length of the file in characters.
7399 */
7400 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7401 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007402 if (dict == NULL)
7403 {
7404 MSG(_(no_lines_msg));
7405 return;
7406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 }
7408 else
7409 {
7410 if (get_fileformat(curbuf) == EOL_DOS)
7411 eol_size = 2;
7412 else
7413 eol_size = 1;
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (VIsual_active)
7416 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007417 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
7419 min_pos = VIsual;
7420 max_pos = curwin->w_cursor;
7421 }
7422 else
7423 {
7424 min_pos = curwin->w_cursor;
7425 max_pos = VIsual;
7426 }
7427 if (*p_sel == 'e' && max_pos.col > 0)
7428 --max_pos.col;
7429
7430 if (VIsual_mode == Ctrl_V)
7431 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007432#ifdef FEAT_LINEBREAK
7433 char_u * saved_sbr = p_sbr;
7434
7435 /* Make 'sbr' empty for a moment to get the correct size. */
7436 p_sbr = empty_option;
7437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 oparg.is_VIsual = 1;
7439 oparg.block_mode = TRUE;
7440 oparg.op_type = OP_NOP;
7441 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007442 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007443#ifdef FEAT_LINEBREAK
7444 p_sbr = saved_sbr;
7445#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007446 if (curwin->w_curswant == MAXCOL)
7447 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 /* Swap the start, end vcol if needed */
7449 if (oparg.end_vcol < oparg.start_vcol)
7450 {
7451 oparg.end_vcol += oparg.start_vcol;
7452 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7453 oparg.end_vcol -= oparg.start_vcol;
7454 }
7455 }
7456 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7460 {
7461 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007462 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
7464 ui_breakcheck();
7465 if (got_int)
7466 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007467 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 /* Do extra processing for VIsual mode. */
7471 if (VIsual_active
7472 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7473 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007474 char_u *s = NULL;
7475 long len = 0L;
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 switch (VIsual_mode)
7478 {
7479 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007484#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007486#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007487 s = bd.textstart;
7488 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 break;
7490 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007491 s = ml_get(lnum);
7492 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 break;
7494 case 'v':
7495 {
7496 colnr_T start_col = (lnum == min_pos.lnum)
7497 ? min_pos.col : 0;
7498 colnr_T end_col = (lnum == max_pos.lnum)
7499 ? max_pos.col - start_col + 1 : MAXCOL;
7500
Bram Moolenaardef9e822004-12-31 20:58:58 +00007501 s = ml_get(lnum) + start_col;
7502 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 break;
7505 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007506 if (s != NULL)
7507 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007508 byte_count_cursor += line_count_info(s, &word_count_cursor,
7509 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007510 if (lnum == curbuf->b_ml.ml_line_count
7511 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007512 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007513 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007514 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 {
7519 /* In non-visual mode, check for the line the cursor is on */
7520 if (lnum == curwin->w_cursor.lnum)
7521 {
7522 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007523 char_count_cursor += char_count;
7524 byte_count_cursor = byte_count +
7525 line_count_info(ml_get(lnum),
7526 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007527 (varnumber_T)(curwin->w_cursor.col + 1),
7528 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 }
7531 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007532 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007533 &char_count, (varnumber_T)MAXCOL,
7534 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536
7537 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007538 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007539 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaared767a22016-01-03 22:49:16 +01007541 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007543 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007545 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7546 {
7547 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7548 &max_pos.col);
7549 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7550 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7551 }
7552 else
7553 buf1[0] = NUL;
7554
7555 if (char_count_cursor == byte_count_cursor
7556 && char_count == byte_count)
7557 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007558 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007559 buf1, line_count_selected,
7560 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007561 (long long)word_count_cursor,
7562 (long long)word_count,
7563 (long long)byte_count_cursor,
7564 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007565 else
7566 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007567 _("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 +01007568 buf1, line_count_selected,
7569 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007570 (long long)word_count_cursor,
7571 (long long)word_count,
7572 (long long)char_count_cursor,
7573 (long long)char_count,
7574 (long long)byte_count_cursor,
7575 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007578 {
7579 p = ml_get_curline();
7580 validate_virtcol();
7581 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7582 (int)curwin->w_virtcol + 1);
7583 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7584 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
Bram Moolenaared767a22016-01-03 22:49:16 +01007586 if (char_count_cursor == byte_count_cursor
7587 && char_count == byte_count)
7588 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007589 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007590 (char *)buf1, (char *)buf2,
7591 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007593 (long long)word_count_cursor, (long long)word_count,
7594 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007595 else
7596 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007597 _("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 +01007598 (char *)buf1, (char *)buf2,
7599 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007600 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007601 (long long)word_count_cursor, (long long)word_count,
7602 (long long)char_count_cursor, (long long)char_count,
7603 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007604 }
7605 }
7606
Bram Moolenaared767a22016-01-03 22:49:16 +01007607#ifdef FEAT_MBYTE
7608 bom_count = bomb_size();
7609 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007610 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007611 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007612#endif
7613 if (dict == NULL)
7614 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007615 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007616 p = p_shm;
7617 p_shm = (char_u *)"";
7618 msg(IObuff);
7619 p_shm = p;
7620 }
7621 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007622#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007623 if (dict != NULL)
7624 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007625 dict_add_nr_str(dict, "words", word_count, NULL);
7626 dict_add_nr_str(dict, "chars", char_count, NULL);
7627 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007628# ifdef FEAT_MBYTE
7629 + bom_count
7630# endif
7631 , NULL);
7632 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007633 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007634 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007635 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007636 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007637 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}