blob: 2dbc74889d8696f86a38a4bf53e8382e93396884 [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 Moolenaar27356ad2012-12-12 16:11:36 +01003554 /* Autocommands may be executed when saving lines for undo, which may make
3555 * y_array invalid. Start undo now to avoid that. */
3556 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (insert_string != NULL)
3559 {
3560 y_type = MCHAR;
3561#ifdef FEAT_EVAL
3562 if (regname == '=')
3563 {
3564 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003565 * characters.
3566 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 for (;;)
3568 {
3569 y_size = 0;
3570 ptr = insert_string;
3571 while (ptr != NULL)
3572 {
3573 if (y_array != NULL)
3574 y_array[y_size] = ptr;
3575 ++y_size;
3576 ptr = vim_strchr(ptr, '\n');
3577 if (ptr != NULL)
3578 {
3579 if (y_array != NULL)
3580 *ptr = NUL;
3581 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003582 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (*ptr == NUL)
3584 {
3585 y_type = MLINE;
3586 break;
3587 }
3588 }
3589 }
3590 if (y_array != NULL)
3591 break;
3592 y_array = (char_u **)alloc((unsigned)
3593 (y_size * sizeof(char_u *)));
3594 if (y_array == NULL)
3595 goto end;
3596 }
3597 }
3598 else
3599#endif
3600 {
3601 y_size = 1; /* use fake one-line yank register */
3602 y_array = &insert_string;
3603 }
3604 }
3605 else
3606 {
3607 get_yank_register(regname, FALSE);
3608
3609 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 y_size = y_current->y_size;
3612 y_array = y_current->y_array;
3613 }
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (y_type == MLINE)
3616 {
3617 if (flags & PUT_LINE_SPLIT)
3618 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003619 char_u *p;
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 /* "p" or "P" in Visual mode: split the lines to put the text in
3622 * between. */
3623 if (u_save_cursor() == FAIL)
3624 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003625 p = ml_get_cursor();
3626 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003627 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003628 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (ptr == NULL)
3630 goto end;
3631 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3632 vim_free(ptr);
3633
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003634 oldp = ml_get_curline();
3635 p = oldp + curwin->w_cursor.col;
3636 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003637 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003638 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (ptr == NULL)
3640 goto end;
3641 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3642 ++nr_lines;
3643 dir = FORWARD;
3644 }
3645 if (flags & PUT_LINE_FORWARD)
3646 {
3647 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003648 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 dir = FORWARD;
3650 }
3651 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3652 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3656 y_type = MLINE;
3657
3658 if (y_size == 0 || y_array == NULL)
3659 {
3660 EMSG2(_("E353: Nothing in register %s"),
3661 regname == 0 ? (char_u *)"\"" : transchar(regname));
3662 goto end;
3663 }
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if (y_type == MBLOCK)
3666 {
3667 lnum = curwin->w_cursor.lnum + y_size + 1;
3668 if (lnum > curbuf->b_ml.ml_line_count)
3669 lnum = curbuf->b_ml.ml_line_count + 1;
3670 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3671 goto end;
3672 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003673 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 lnum = curwin->w_cursor.lnum;
3676#ifdef FEAT_FOLDING
3677 /* Correct line number for closed fold. Don't move the cursor yet,
3678 * u_save() uses it. */
3679 if (dir == BACKWARD)
3680 (void)hasFolding(lnum, &lnum, NULL);
3681 else
3682 (void)hasFolding(lnum, NULL, &lnum);
3683#endif
3684 if (dir == FORWARD)
3685 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003686 /* In an empty buffer the empty line is going to be replaced, include
3687 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003688 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 goto end;
3690#ifdef FEAT_FOLDING
3691 if (dir == FORWARD)
3692 curwin->w_cursor.lnum = lnum - 1;
3693 else
3694 curwin->w_cursor.lnum = lnum;
3695 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3696#endif
3697 }
3698 else if (u_save_cursor() == FAIL)
3699 goto end;
3700
3701 yanklen = (int)STRLEN(y_array[0]);
3702
3703#ifdef FEAT_VIRTUALEDIT
3704 if (ve_flags == VE_ALL && y_type == MCHAR)
3705 {
3706 if (gchar_cursor() == TAB)
3707 {
3708 /* Don't need to insert spaces when "p" on the last position of a
3709 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003710#ifdef FEAT_VARTABS
3711 int viscol = getviscol();
3712 if (dir == FORWARD
3713 ? tabstop_padding(viscol, curbuf->b_p_ts,
3714 curbuf->b_p_vts_array) != 1
3715 : curwin->w_cursor.coladd > 0)
3716 coladvance_force(viscol);
3717#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (dir == FORWARD
3719 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3720 : curwin->w_cursor.coladd > 0)
3721 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else
3724 curwin->w_cursor.coladd = 0;
3725 }
3726 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3727 coladvance_force(getviscol() + (dir == FORWARD));
3728 }
3729#endif
3730
3731 lnum = curwin->w_cursor.lnum;
3732 col = curwin->w_cursor.col;
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 /*
3735 * Block mode
3736 */
3737 if (y_type == MBLOCK)
3738 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003739 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 colnr_T endcol2 = 0;
3741
3742 if (dir == FORWARD && c != NUL)
3743 {
3744#ifdef FEAT_VIRTUALEDIT
3745 if (ve_flags == VE_ALL)
3746 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3747 else
3748#endif
3749 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3750
3751#ifdef FEAT_MBYTE
3752 if (has_mbyte)
3753 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003754 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 else
3756#endif
3757#ifdef FEAT_VIRTUALEDIT
3758 if (c != TAB || ve_flags != VE_ALL)
3759#endif
3760 ++curwin->w_cursor.col;
3761 ++col;
3762 }
3763 else
3764 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3765
3766#ifdef FEAT_VIRTUALEDIT
3767 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003768 if (ve_flags == VE_ALL
3769 && (curwin->w_cursor.coladd > 0
3770 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
3772 if (dir == FORWARD && c == NUL)
3773 ++col;
3774 if (dir != FORWARD && c != NUL)
3775 ++curwin->w_cursor.col;
3776 if (c == TAB)
3777 {
3778 if (dir == BACKWARD && curwin->w_cursor.col)
3779 curwin->w_cursor.col--;
3780 if (dir == FORWARD && col - 1 == endcol2)
3781 curwin->w_cursor.col++;
3782 }
3783 }
3784 curwin->w_cursor.coladd = 0;
3785#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003786 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 for (i = 0; i < y_size; ++i)
3788 {
3789 int spaces;
3790 char shortline;
3791
3792 bd.startspaces = 0;
3793 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 vcol = 0;
3795 delcount = 0;
3796
3797 /* add a new line */
3798 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3799 {
3800 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3801 (colnr_T)1, FALSE) == FAIL)
3802 break;
3803 ++nr_lines;
3804 }
3805 /* get the old line and advance to the position to insert at */
3806 oldp = ml_get_curline();
3807 oldlen = (int)STRLEN(oldp);
3808 for (ptr = oldp; vcol < col && *ptr; )
3809 {
3810 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003811 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 vcol += incr;
3813 }
3814 bd.textcol = (colnr_T)(ptr - oldp);
3815
3816 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3817
3818 if (vcol < col) /* line too short, padd with spaces */
3819 bd.startspaces = col - vcol;
3820 else if (vcol > col)
3821 {
3822 bd.endspaces = vcol - col;
3823 bd.startspaces = incr - bd.endspaces;
3824 --bd.textcol;
3825 delcount = 1;
3826#ifdef FEAT_MBYTE
3827 if (has_mbyte)
3828 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3829#endif
3830 if (oldp[bd.textcol] != TAB)
3831 {
3832 /* Only a Tab can be split into spaces. Other
3833 * characters will have to be moved to after the
3834 * block, causing misalignment. */
3835 delcount = 0;
3836 bd.endspaces = 0;
3837 }
3838 }
3839
3840 yanklen = (int)STRLEN(y_array[i]);
3841
3842 /* calculate number of spaces required to fill right side of block*/
3843 spaces = y_width + 1;
3844 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003845 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (spaces < 0)
3847 spaces = 0;
3848
3849 /* insert the new text */
3850 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3851 newp = alloc_check((unsigned)totlen + oldlen + 1);
3852 if (newp == NULL)
3853 break;
3854 /* copy part up to cursor to new line */
3855 ptr = newp;
3856 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3857 ptr += bd.textcol;
3858 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003859 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 ptr += bd.startspaces;
3861 /* insert the new text */
3862 for (j = 0; j < count; ++j)
3863 {
3864 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3865 ptr += yanklen;
3866
3867 /* insert block's trailing spaces only if there's text behind */
3868 if ((j < count - 1 || !shortline) && spaces)
3869 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003870 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 ptr += spaces;
3872 }
3873 }
3874 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003875 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 ptr += bd.endspaces;
3877 /* move the text after the cursor to the end of the line. */
3878 mch_memmove(ptr, oldp + bd.textcol + delcount,
3879 (size_t)(oldlen - bd.textcol - delcount + 1));
3880 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3881
3882 ++curwin->w_cursor.lnum;
3883 if (i == 0)
3884 curwin->w_cursor.col += bd.startspaces;
3885 }
3886
3887 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3888
3889 /* Set '[ mark. */
3890 curbuf->b_op_start = curwin->w_cursor;
3891 curbuf->b_op_start.lnum = lnum;
3892
3893 /* adjust '] mark */
3894 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3895 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003896# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (flags & PUT_CURSEND)
3900 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003901 colnr_T len;
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 curwin->w_cursor = curbuf->b_op_end;
3904 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003905
3906 /* in Insert mode we might be after the NUL, correct for that */
3907 len = (colnr_T)STRLEN(ml_get_curline());
3908 if (curwin->w_cursor.col > len)
3909 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 else
3912 curwin->w_cursor.lnum = lnum;
3913 }
3914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
3916 /*
3917 * Character or Line mode
3918 */
3919 if (y_type == MCHAR)
3920 {
3921 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3922 * char */
3923 if (dir == FORWARD && gchar_cursor() != NUL)
3924 {
3925#ifdef FEAT_MBYTE
3926 if (has_mbyte)
3927 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003928 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /* put it on the next of the multi-byte character. */
3931 col += bytelen;
3932 if (yanklen)
3933 {
3934 curwin->w_cursor.col += bytelen;
3935 curbuf->b_op_end.col += bytelen;
3936 }
3937 }
3938 else
3939#endif
3940 {
3941 ++col;
3942 if (yanklen)
3943 {
3944 ++curwin->w_cursor.col;
3945 ++curbuf->b_op_end.col;
3946 }
3947 }
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 curbuf->b_op_start = curwin->w_cursor;
3950 }
3951 /*
3952 * Line mode: BACKWARD is the same as FORWARD on the previous line
3953 */
3954 else if (dir == BACKWARD)
3955 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003956 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
3958 /*
3959 * simple case: insert into current line
3960 */
3961 if (y_type == MCHAR && y_size == 1)
3962 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003963 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003964
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003965 if (VIsual_active)
3966 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003967 end_lnum = curbuf->b_visual.vi_end.lnum;
3968 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3969 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003970 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003971
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003972 do {
3973 totlen = count * yanklen;
3974 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003976 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003977 if (VIsual_active && col > (int)STRLEN(oldp))
3978 {
3979 lnum++;
3980 continue;
3981 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003982 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3983 if (newp == NULL)
3984 goto end; /* alloc() gave an error message */
3985 mch_memmove(newp, oldp, (size_t)col);
3986 ptr = newp + col;
3987 for (i = 0; i < count; ++i)
3988 {
3989 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3990 ptr += yanklen;
3991 }
3992 STRMOVE(ptr, oldp + col);
3993 ml_replace(lnum, newp, FALSE);
3994 /* Place cursor on last putted char. */
3995 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003996 {
3997 /* make sure curwin->w_virtcol is updated */
3998 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003999 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01004000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004002 if (VIsual_active)
4003 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01004004 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004005
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01004006 if (VIsual_active) /* reset lnum to the last visual line */
4007 lnum--;
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 curbuf->b_op_end = curwin->w_cursor;
4010 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4011 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4012 ++curwin->w_cursor.col;
4013 changed_bytes(lnum, col);
4014 }
4015 else
4016 {
4017 /*
4018 * Insert at least one line. When y_type is MCHAR, break the first
4019 * line in two.
4020 */
4021 for (cnt = 1; cnt <= count; ++cnt)
4022 {
4023 i = 0;
4024 if (y_type == MCHAR)
4025 {
4026 /*
4027 * Split the current line in two at the insert position.
4028 * First insert y_array[size - 1] in front of second line.
4029 * Then append y_array[0] to first line.
4030 */
4031 lnum = new_cursor.lnum;
4032 ptr = ml_get(lnum) + col;
4033 totlen = (int)STRLEN(y_array[y_size - 1]);
4034 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4035 if (newp == NULL)
4036 goto error;
4037 STRCPY(newp, y_array[y_size - 1]);
4038 STRCAT(newp, ptr);
4039 /* insert second line */
4040 ml_append(lnum, newp, (colnr_T)0, FALSE);
4041 vim_free(newp);
4042
4043 oldp = ml_get(lnum);
4044 newp = alloc_check((unsigned)(col + yanklen + 1));
4045 if (newp == NULL)
4046 goto error;
4047 /* copy first part of line */
4048 mch_memmove(newp, oldp, (size_t)col);
4049 /* append to first line */
4050 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4051 ml_replace(lnum, newp, FALSE);
4052
4053 curwin->w_cursor.lnum = lnum;
4054 i = 1;
4055 }
4056
4057 for (; i < y_size; ++i)
4058 {
4059 if ((y_type != MCHAR || i < y_size - 1)
4060 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4061 == FAIL)
4062 goto error;
4063 lnum++;
4064 ++nr_lines;
4065 if (flags & PUT_FIXINDENT)
4066 {
4067 old_pos = curwin->w_cursor;
4068 curwin->w_cursor.lnum = lnum;
4069 ptr = ml_get(lnum);
4070 if (cnt == count && i == y_size - 1)
4071 lendiff = (int)STRLEN(ptr);
4072#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4073 if (*ptr == '#' && preprocs_left())
4074 indent = 0; /* Leave # lines at start */
4075 else
4076#endif
4077 if (*ptr == NUL)
4078 indent = 0; /* Ignore empty lines */
4079 else if (first_indent)
4080 {
4081 indent_diff = orig_indent - get_indent();
4082 indent = orig_indent;
4083 first_indent = FALSE;
4084 }
4085 else if ((indent = get_indent() + indent_diff) < 0)
4086 indent = 0;
4087 (void)set_indent(indent, 0);
4088 curwin->w_cursor = old_pos;
4089 /* remember how many chars were removed */
4090 if (cnt == count && i == y_size - 1)
4091 lendiff -= (int)STRLEN(ml_get(lnum));
4092 }
4093 }
4094 }
4095
4096error:
4097 /* Adjust marks. */
4098 if (y_type == MLINE)
4099 {
4100 curbuf->b_op_start.col = 0;
4101 if (dir == FORWARD)
4102 curbuf->b_op_start.lnum++;
4103 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004104 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004105 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004106 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004107 < curbuf->b_ml.ml_line_count
4108#ifdef FEAT_DIFF
4109 || curwin->w_p_diff
4110#endif
4111 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004112 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 (linenr_T)MAXLNUM, nr_lines, 0L);
4114
4115 /* note changed text for displaying and folding */
4116 if (y_type == MCHAR)
4117 changed_lines(curwin->w_cursor.lnum, col,
4118 curwin->w_cursor.lnum + 1, nr_lines);
4119 else
4120 changed_lines(curbuf->b_op_start.lnum, 0,
4121 curbuf->b_op_start.lnum, nr_lines);
4122
4123 /* put '] mark at last inserted character */
4124 curbuf->b_op_end.lnum = lnum;
4125 /* correct length for change in indent */
4126 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4127 if (col > 1)
4128 curbuf->b_op_end.col = col - 1;
4129 else
4130 curbuf->b_op_end.col = 0;
4131
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004132 if (flags & PUT_CURSLINE)
4133 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004134 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004135 curwin->w_cursor.lnum = lnum;
4136 beginline(BL_WHITE | BL_FIX);
4137 }
4138 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
4140 /* put cursor after inserted text */
4141 if (y_type == MLINE)
4142 {
4143 if (lnum >= curbuf->b_ml.ml_line_count)
4144 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4145 else
4146 curwin->w_cursor.lnum = lnum + 1;
4147 curwin->w_cursor.col = 0;
4148 }
4149 else
4150 {
4151 curwin->w_cursor.lnum = lnum;
4152 curwin->w_cursor.col = col;
4153 }
4154 }
4155 else if (y_type == MLINE)
4156 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004157 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 curwin->w_cursor.col = 0;
4159 if (dir == FORWARD)
4160 ++curwin->w_cursor.lnum;
4161 beginline(BL_WHITE | BL_FIX);
4162 }
4163 else /* put cursor on first inserted character */
4164 curwin->w_cursor = new_cursor;
4165 }
4166 }
4167
4168 msgmore(nr_lines);
4169 curwin->w_set_curswant = TRUE;
4170
4171end:
4172 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004174 if (regname == '=')
4175 vim_free(y_array);
4176
Bram Moolenaar033d8882013-09-25 23:24:57 +02004177 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004178
Bram Moolenaar677ee682005-01-27 14:41:15 +00004179 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004180 adjust_cursor_eol();
4181}
4182
4183/*
4184 * When the cursor is on the NUL past the end of the line and it should not be
4185 * there move it left.
4186 */
4187 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004188adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004189{
4190 if (curwin->w_cursor.col > 0
4191 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004192#ifdef FEAT_VIRTUALEDIT
4193 && (ve_flags & VE_ONEMORE) == 0
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 && !(restart_edit || (State & INSERT)))
4196 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004197 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004198 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200#ifdef FEAT_VIRTUALEDIT
4201 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004202 {
4203 colnr_T scol, ecol;
4204
4205 /* Coladd is set to the width of the last character. */
4206 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4207 curwin->w_cursor.coladd = ecol - scol + 1;
4208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#endif
4210 }
4211}
4212
4213#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4214/*
4215 * Return TRUE if lines starting with '#' should be left aligned.
4216 */
4217 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004218preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 return
4221# ifdef FEAT_SMARTINDENT
4222# ifdef FEAT_CINDENT
4223 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4224# else
4225 curbuf->b_p_si
4226# endif
4227# endif
4228# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004229 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4230 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231# endif
4232 ;
4233}
4234#endif
4235
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004236/*
4237 * Return the character name of the register with the given number.
4238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004240get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 if (num == -1)
4243 return '"';
4244 else if (num < 10)
4245 return num + '0';
4246 else if (num == DELETION_REGISTER)
4247 return '-';
4248#ifdef FEAT_CLIPBOARD
4249 else if (num == STAR_REGISTER)
4250 return '*';
4251 else if (num == PLUS_REGISTER)
4252 return '+';
4253#endif
4254 else
4255 {
4256#ifdef EBCDIC
4257 int i;
4258
4259 /* EBCDIC is really braindead ... */
4260 i = 'a' + (num - 10);
4261 if (i > 'i')
4262 i += 7;
4263 if (i > 'r')
4264 i += 8;
4265 return i;
4266#else
4267 return num + 'a' - 10;
4268#endif
4269 }
4270}
4271
4272/*
4273 * ":dis" and ":registers": Display the contents of the yank registers.
4274 */
4275 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004276ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004278 int i, n;
4279 long j;
4280 char_u *p;
4281 yankreg_T *yb;
4282 int name;
4283 int attr;
4284 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004285#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004286 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004287#else
4288# define clen 1
4289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 if (arg != NULL && *arg == NUL)
4292 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004293 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /* Highlight title */
4296 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4297 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4298 {
4299 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004300 if (arg != NULL && vim_strchr(arg, name) == NULL
4301#ifdef ONE_CLIPBOARD
4302 /* Star register and plus register contain the same thing. */
4303 && (name != '*' || vim_strchr(arg, '+') == NULL)
4304#endif
4305 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 continue; /* did not ask for this register */
4307
4308#ifdef FEAT_CLIPBOARD
4309 /* Adjust register name for "unnamed" in 'clipboard'.
4310 * When it's a clipboard register, fill it with the current contents
4311 * of the clipboard. */
4312 adjust_clip_reg(&name);
4313 (void)may_get_selection(name);
4314#endif
4315
4316 if (i == -1)
4317 {
4318 if (y_previous != NULL)
4319 yb = y_previous;
4320 else
4321 yb = &(y_regs[0]);
4322 }
4323 else
4324 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004325
4326#ifdef FEAT_EVAL
4327 if (name == MB_TOLOWER(redir_reg)
4328 || (redir_reg == '"' && yb == y_previous))
4329 continue; /* do not list register being written to, the
4330 * pointer can be freed */
4331#endif
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 if (yb->y_array != NULL)
4334 {
4335 msg_putchar('\n');
4336 msg_putchar('"');
4337 msg_putchar(name);
4338 MSG_PUTS(" ");
4339
4340 n = (int)Columns - 6;
4341 for (j = 0; j < yb->y_size && n > 1; ++j)
4342 {
4343 if (j)
4344 {
4345 MSG_PUTS_ATTR("^J", attr);
4346 n -= 2;
4347 }
4348 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004351 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004352#endif
4353 msg_outtrans_len(p, clen);
4354#ifdef FEAT_MBYTE
4355 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#endif
4357 }
4358 }
4359 if (n > 1 && yb->y_type == MLINE)
4360 MSG_PUTS_ATTR("^J", attr);
4361 out_flush(); /* show one line at a time */
4362 }
4363 ui_breakcheck();
4364 }
4365
4366 /*
4367 * display last inserted text
4368 */
4369 if ((p = get_last_insert()) != NULL
4370 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4371 {
4372 MSG_PUTS("\n\". ");
4373 dis_msg(p, TRUE);
4374 }
4375
4376 /*
4377 * display last command line
4378 */
4379 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4380 && !got_int)
4381 {
4382 MSG_PUTS("\n\": ");
4383 dis_msg(last_cmdline, FALSE);
4384 }
4385
4386 /*
4387 * display current file name
4388 */
4389 if (curbuf->b_fname != NULL
4390 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4391 {
4392 MSG_PUTS("\n\"% ");
4393 dis_msg(curbuf->b_fname, FALSE);
4394 }
4395
4396 /*
4397 * display alternate file name
4398 */
4399 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4400 {
4401 char_u *fname;
4402 linenr_T dummy;
4403
4404 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4405 {
4406 MSG_PUTS("\n\"# ");
4407 dis_msg(fname, FALSE);
4408 }
4409 }
4410
4411 /*
4412 * display last search pattern
4413 */
4414 if (last_search_pat() != NULL
4415 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4416 {
4417 MSG_PUTS("\n\"/ ");
4418 dis_msg(last_search_pat(), FALSE);
4419 }
4420
4421#ifdef FEAT_EVAL
4422 /*
4423 * display last used expression
4424 */
4425 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4426 && !got_int)
4427 {
4428 MSG_PUTS("\n\"= ");
4429 dis_msg(expr_line, FALSE);
4430 }
4431#endif
4432}
4433
4434/*
4435 * display a string for do_dis()
4436 * truncate at end of screen line
4437 */
4438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004439dis_msg(
4440 char_u *p,
4441 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443 int n;
4444#ifdef FEAT_MBYTE
4445 int l;
4446#endif
4447
4448 n = (int)Columns - 6;
4449 while (*p != NUL
4450 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4451 && (n -= ptr2cells(p)) >= 0)
4452 {
4453#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004454 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
4456 msg_outtrans_len(p, l);
4457 p += l;
4458 }
4459 else
4460#endif
4461 msg_outtrans_len(p++, 1);
4462 }
4463 ui_breakcheck();
4464}
4465
Bram Moolenaar81340392012-06-06 16:12:59 +02004466#if defined(FEAT_COMMENTS) || defined(PROTO)
4467/*
4468 * If "process" is TRUE and the line begins with a comment leader (possibly
4469 * after some white space), return a pointer to the text after it. Put a boolean
4470 * value indicating whether the line ends with an unclosed comment in
4471 * "is_comment".
4472 * line - line to be processed,
4473 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004474 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004475 * include_space - whether to also skip space following the comment leader,
4476 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004477 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004478 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004479 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004480skip_comment(
4481 char_u *line,
4482 int process,
4483 int include_space,
4484 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004485{
4486 char_u *comment_flags = NULL;
4487 int lead_len;
4488 int leader_offset = get_last_leader_offset(line, &comment_flags);
4489
4490 *is_comment = FALSE;
4491 if (leader_offset != -1)
4492 {
4493 /* Let's check whether the line ends with an unclosed comment.
4494 * If the last comment leader has COM_END in flags, there's no comment.
4495 */
4496 while (*comment_flags)
4497 {
4498 if (*comment_flags == COM_END
4499 || *comment_flags == ':')
4500 break;
4501 ++comment_flags;
4502 }
4503 if (*comment_flags != COM_END)
4504 *is_comment = TRUE;
4505 }
4506
4507 if (process == FALSE)
4508 return line;
4509
4510 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4511
4512 if (lead_len == 0)
4513 return line;
4514
4515 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004516 * - COM_END,
4517 * - colon,
4518 * whichever comes first.
4519 */
4520 while (*comment_flags)
4521 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004522 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004523 || *comment_flags == ':')
4524 {
4525 break;
4526 }
4527 ++comment_flags;
4528 }
4529
4530 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004531 * starting with a closing part of a three-part comment. That's good,
4532 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004533 */
4534 if (*comment_flags == ':' || *comment_flags == NUL)
4535 line += lead_len;
4536
4537 return line;
4538}
4539#endif
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004542 * Join 'count' lines (minimal 2) at cursor position.
4543 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004544 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4545 * leaders should not be removed.
4546 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4547 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004549 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 */
4551 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004552do_join(
4553 long count,
4554 int insert_space,
4555 int save_undo,
4556 int use_formatoptions UNUSED,
4557 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004559 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004560 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004561 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004563 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004564 int endcurr1 = NUL;
4565 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004566 int currsize = 0; /* size of the current line */
4567 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004569 colnr_T col = 0;
4570 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004571#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004572 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004573 int remove_comments = (use_formatoptions == TRUE)
4574 && has_format_option(FO_REMOVE_COMS);
4575 int prev_was_comment;
4576#endif
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004579 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4580 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004583 /* Allocate an array to store the number of spaces inserted before each
4584 * line. We will use it to pre-compute the length of the new line and the
4585 * proper placement of each original line in the new one. */
4586 spaces = lalloc_clear((long_u)count, TRUE);
4587 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004589#if defined(FEAT_COMMENTS) || defined(PROTO)
4590 if (remove_comments)
4591 {
4592 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4593 if (comments == NULL)
4594 {
4595 vim_free(spaces);
4596 return FAIL;
4597 }
4598 }
4599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004602 * Don't move anything, just compute the final line length
4603 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004605 for (t = 0; t < count; ++t)
4606 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004607 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004608 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004609 {
4610 /* Set the '[ mark. */
4611 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4612 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4613 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004614#if defined(FEAT_COMMENTS) || defined(PROTO)
4615 if (remove_comments)
4616 {
4617 /* We don't want to remove the comment leader if the
4618 * previous line is not a comment. */
4619 if (t > 0 && prev_was_comment)
4620 {
4621
4622 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4623 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004624 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004625 curr = new_curr;
4626 }
4627 else
4628 curr = skip_comment(curr, FALSE, insert_space,
4629 &prev_was_comment);
4630 }
4631#endif
4632
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004633 if (insert_space && t > 0)
4634 {
4635 curr = skipwhite(curr);
4636 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4637#ifdef FEAT_MBYTE
4638 && (!has_format_option(FO_MBYTE_JOIN)
4639 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4640 && (!has_format_option(FO_MBYTE_JOIN2)
4641 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4642#endif
4643 )
4644 {
4645 /* don't add a space if the line is ending in a space */
4646 if (endcurr1 == ' ')
4647 endcurr1 = endcurr2;
4648 else
4649 ++spaces[t];
4650 /* extra space when 'joinspaces' set and line ends in '.' */
4651 if ( p_js
4652 && (endcurr1 == '.'
4653 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4654 && (endcurr1 == '?' || endcurr1 == '!'))))
4655 ++spaces[t];
4656 }
4657 }
4658 currsize = (int)STRLEN(curr);
4659 sumsize += currsize + spaces[t];
4660 endcurr1 = endcurr2 = NUL;
4661 if (insert_space && currsize > 0)
4662 {
4663#ifdef FEAT_MBYTE
4664 if (has_mbyte)
4665 {
4666 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004667 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004668 endcurr1 = (*mb_ptr2char)(cend);
4669 if (cend > curr)
4670 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004671 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004672 endcurr2 = (*mb_ptr2char)(cend);
4673 }
4674 }
4675 else
4676#endif
4677 {
4678 endcurr1 = *(curr + currsize - 1);
4679 if (currsize > 1)
4680 endcurr2 = *(curr + currsize - 2);
4681 }
4682 }
4683 line_breakcheck();
4684 if (got_int)
4685 {
4686 ret = FAIL;
4687 goto theend;
4688 }
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004691 /* store the column position before last line */
4692 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004694 /* allocate the space for the new line */
4695 newp = alloc_check((unsigned)(sumsize + 1));
4696 cend = newp + sumsize;
4697 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004699 /*
4700 * Move affected lines to the new long one.
4701 *
4702 * Move marks from each deleted line to the joined line, adjusting the
4703 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4704 * should not really be a problem.
4705 */
4706 for (t = count - 1; ; --t)
4707 {
4708 cend -= currsize;
4709 mch_memmove(cend, curr, (size_t)currsize);
4710 if (spaces[t] > 0)
4711 {
4712 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004713 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004714 }
4715 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004716 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004717 if (t == 0)
4718 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004719 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004720#if defined(FEAT_COMMENTS) || defined(PROTO)
4721 if (remove_comments)
4722 curr += comments[t - 1];
4723#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004724 if (insert_space && t > 1)
4725 curr = skipwhite(curr);
4726 currsize = (int)STRLEN(curr);
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4729
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004730 if (setmark)
4731 {
4732 /* Set the '] mark. */
4733 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4734 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4735 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /* Only report the change in the first line here, del_lines() will report
4738 * the deleted line. */
4739 changed_lines(curwin->w_cursor.lnum, currsize,
4740 curwin->w_cursor.lnum + 1, 0L);
4741
4742 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004743 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * briefly, and then move it back. After del_lines() the cursor may
4745 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 */
4747 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004749 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 curwin->w_cursor.lnum = t;
4751
4752 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004753 * Set the cursor column:
4754 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004755 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004757 curwin->w_cursor.col =
4758 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#ifdef FEAT_VIRTUALEDIT
4762 curwin->w_cursor.coladd = 0;
4763#endif
4764 curwin->w_set_curswant = TRUE;
4765
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004766theend:
4767 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004768#if defined(FEAT_COMMENTS) || defined(PROTO)
4769 if (remove_comments)
4770 vim_free(comments);
4771#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004772 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773}
4774
4775#ifdef FEAT_COMMENTS
4776/*
4777 * Return TRUE if the two comment leaders given are the same. "lnum" is
4778 * the first line. White-space is ignored. Note that the whole of
4779 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4780 */
4781 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004782same_leader(
4783 linenr_T lnum,
4784 int leader1_len,
4785 char_u *leader1_flags,
4786 int leader2_len,
4787 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 int idx1 = 0, idx2 = 0;
4790 char_u *p;
4791 char_u *line1;
4792 char_u *line2;
4793
4794 if (leader1_len == 0)
4795 return (leader2_len == 0);
4796
4797 /*
4798 * If first leader has 'f' flag, the lines can be joined only if the
4799 * second line does not have a leader.
4800 * If first leader has 'e' flag, the lines can never be joined.
4801 * If fist leader has 's' flag, the lines can only be joined if there is
4802 * some text after it and the second line has the 'm' flag.
4803 */
4804 if (leader1_flags != NULL)
4805 {
4806 for (p = leader1_flags; *p && *p != ':'; ++p)
4807 {
4808 if (*p == COM_FIRST)
4809 return (leader2_len == 0);
4810 if (*p == COM_END)
4811 return FALSE;
4812 if (*p == COM_START)
4813 {
4814 if (*(ml_get(lnum) + leader1_len) == NUL)
4815 return FALSE;
4816 if (leader2_flags == NULL || leader2_len == 0)
4817 return FALSE;
4818 for (p = leader2_flags; *p && *p != ':'; ++p)
4819 if (*p == COM_MIDDLE)
4820 return TRUE;
4821 return FALSE;
4822 }
4823 }
4824 }
4825
4826 /*
4827 * Get current line and next line, compare the leaders.
4828 * The first line has to be saved, only one line can be locked at a time.
4829 */
4830 line1 = vim_strsave(ml_get(lnum));
4831 if (line1 != NULL)
4832 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004833 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 ;
4835 line2 = ml_get(lnum + 1);
4836 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4837 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004838 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 if (line1[idx1++] != line2[idx2])
4841 break;
4842 }
4843 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004844 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 ++idx1;
4846 }
4847 vim_free(line1);
4848 }
4849 return (idx2 == leader2_len && idx1 == leader1_len);
4850}
4851#endif
4852
4853/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004854 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 */
4856 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004857op_format(
4858 oparg_T *oap,
4859 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
4861 long old_line_count = curbuf->b_ml.ml_line_count;
4862
4863 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4864 * can put it back there. */
4865 curwin->w_cursor = oap->cursor_start;
4866
4867 if (u_save((linenr_T)(oap->start.lnum - 1),
4868 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4869 return;
4870 curwin->w_cursor = oap->start;
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (oap->is_VIsual)
4873 /* When there is no change: need to remove the Visual selection */
4874 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
4876 /* Set '[ mark at the start of the formatted area */
4877 curbuf->b_op_start = oap->start;
4878
4879 /* For "gw" remember the cursor position and put it back below (adjusted
4880 * for joined and split lines). */
4881 if (keep_cursor)
4882 saved_cursor = oap->cursor_start;
4883
Bram Moolenaar81a82092008-03-12 16:27:00 +00004884 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /*
4887 * Leave the cursor at the first non-blank of the last formatted line.
4888 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4889 * line, so "." will do the next lines.
4890 */
4891 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4892 ++curwin->w_cursor.lnum;
4893 beginline(BL_WHITE | BL_FIX);
4894 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4895 msgmore(old_line_count);
4896
4897 /* put '] mark on the end of the formatted area */
4898 curbuf->b_op_end = curwin->w_cursor;
4899
4900 if (keep_cursor)
4901 {
4902 curwin->w_cursor = saved_cursor;
4903 saved_cursor.lnum = 0;
4904 }
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (oap->is_VIsual)
4907 {
4908 win_T *wp;
4909
4910 FOR_ALL_WINDOWS(wp)
4911 {
4912 if (wp->w_old_cursor_lnum != 0)
4913 {
4914 /* When lines have been inserted or deleted, adjust the end of
4915 * the Visual area to be redrawn. */
4916 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4917 wp->w_old_cursor_lnum += old_line_count;
4918 else
4919 wp->w_old_visual_lnum += old_line_count;
4920 }
4921 }
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004925#if defined(FEAT_EVAL) || defined(PROTO)
4926/*
4927 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4928 */
4929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004930op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004931{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004932 if (oap->is_VIsual)
4933 /* When there is no change: need to remove the Visual selection */
4934 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004935
Bram Moolenaar700303e2010-07-11 17:35:50 +02004936 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4937 /* As documented: when 'formatexpr' returns non-zero fall back to
4938 * internal formatting. */
4939 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004940}
4941
4942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004943fex_format(
4944 linenr_T lnum,
4945 long count,
4946 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004947{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004948 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4949 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004950 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004951 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004952
4953 /*
4954 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004955 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004956 */
4957 set_vim_var_nr(VV_LNUM, lnum);
4958 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004959 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004960
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004961 /* Make a copy, the option could be changed while calling it. */
4962 fex = vim_strsave(curbuf->b_p_fex);
4963 if (fex == NULL)
4964 return 0;
4965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004966 /*
4967 * Evaluate the function.
4968 */
4969 if (use_sandbox)
4970 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004971 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004972 if (use_sandbox)
4973 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004974
4975 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004976 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004977
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004978 return r;
4979}
4980#endif
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982/*
4983 * Format "line_count" lines, starting at the cursor position.
4984 * When "line_count" is negative, format until the end of the paragraph.
4985 * Lines after the cursor line are saved for undo, caller must have saved the
4986 * first line.
4987 */
4988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989format_lines(
4990 linenr_T line_count,
4991 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
4993 int max_len;
4994 int is_not_par; /* current line not part of parag. */
4995 int next_is_not_par; /* next line not part of paragraph */
4996 int is_end_par; /* at end of paragraph */
4997 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4998 int next_is_start_par = FALSE;
4999#ifdef FEAT_COMMENTS
5000 int leader_len = 0; /* leader len of current line */
5001 int next_leader_len; /* leader len of next line */
5002 char_u *leader_flags = NULL; /* flags for leader of current line */
5003 char_u *next_leader_flags; /* flags for leader of next line */
5004 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005005 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
5007 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005008 int second_indent = -1; /* indent for second line (comment
5009 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 int do_second_indent;
5011 int do_number_indent;
5012 int do_trail_white;
5013 int first_par_line = TRUE;
5014 int smd_save;
5015 long count;
5016 int need_set_indent = TRUE; /* set indent of next paragraph */
5017 int force_format = FALSE;
5018 int old_State = State;
5019
5020 /* length of a line to force formatting: 3 * 'tw' */
5021 max_len = comp_textwidth(TRUE) * 3;
5022
5023 /* check for 'q', '2' and '1' in 'formatoptions' */
5024#ifdef FEAT_COMMENTS
5025 do_comments = has_format_option(FO_Q_COMS);
5026#endif
5027 do_second_indent = has_format_option(FO_Q_SECOND);
5028 do_number_indent = has_format_option(FO_Q_NUMBER);
5029 do_trail_white = has_format_option(FO_WHITE_PAR);
5030
5031 /*
5032 * Get info about the previous and current line.
5033 */
5034 if (curwin->w_cursor.lnum > 1)
5035 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5036#ifdef FEAT_COMMENTS
5037 , &leader_len, &leader_flags, do_comments
5038#endif
5039 );
5040 else
5041 is_not_par = TRUE;
5042 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5043#ifdef FEAT_COMMENTS
5044 , &next_leader_len, &next_leader_flags, do_comments
5045#endif
5046 );
5047 is_end_par = (is_not_par || next_is_not_par);
5048 if (!is_end_par && do_trail_white)
5049 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5050
5051 curwin->w_cursor.lnum--;
5052 for (count = line_count; count != 0 && !got_int; --count)
5053 {
5054 /*
5055 * Advance to next paragraph.
5056 */
5057 if (advance)
5058 {
5059 curwin->w_cursor.lnum++;
5060 prev_is_end_par = is_end_par;
5061 is_not_par = next_is_not_par;
5062#ifdef FEAT_COMMENTS
5063 leader_len = next_leader_len;
5064 leader_flags = next_leader_flags;
5065#endif
5066 }
5067
5068 /*
5069 * The last line to be formatted.
5070 */
5071 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5072 {
5073 next_is_not_par = TRUE;
5074#ifdef FEAT_COMMENTS
5075 next_leader_len = 0;
5076 next_leader_flags = NULL;
5077#endif
5078 }
5079 else
5080 {
5081 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5082#ifdef FEAT_COMMENTS
5083 , &next_leader_len, &next_leader_flags, do_comments
5084#endif
5085 );
5086 if (do_number_indent)
5087 next_is_start_par =
5088 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5089 }
5090 advance = TRUE;
5091 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5092 if (!is_end_par && do_trail_white)
5093 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5094
5095 /*
5096 * Skip lines that are not in a paragraph.
5097 */
5098 if (is_not_par)
5099 {
5100 if (line_count < 0)
5101 break;
5102 }
5103 else
5104 {
5105 /*
5106 * For the first line of a paragraph, check indent of second line.
5107 * Don't do this for comments and empty lines.
5108 */
5109 if (first_par_line
5110 && (do_second_indent || do_number_indent)
5111 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005112 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005114 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005115 {
5116#ifdef FEAT_COMMENTS
5117 if (leader_len == 0 && next_leader_len == 0)
5118 {
5119 /* no comment found */
5120#endif
5121 second_indent =
5122 get_indent_lnum(curwin->w_cursor.lnum + 1);
5123#ifdef FEAT_COMMENTS
5124 }
5125 else
5126 {
5127 second_indent = next_leader_len;
5128 do_comments_list = 1;
5129 }
5130#endif
5131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005133 {
5134#ifdef FEAT_COMMENTS
5135 if (leader_len == 0 && next_leader_len == 0)
5136 {
5137 /* no comment found */
5138#endif
5139 second_indent =
5140 get_number_indent(curwin->w_cursor.lnum);
5141#ifdef FEAT_COMMENTS
5142 }
5143 else
5144 {
5145 /* get_number_indent() is now "comment aware"... */
5146 second_indent =
5147 get_number_indent(curwin->w_cursor.lnum);
5148 do_comments_list = 1;
5149 }
5150#endif
5151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153
5154 /*
5155 * When the comment leader changes, it's the end of the paragraph.
5156 */
5157 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5158#ifdef FEAT_COMMENTS
5159 || !same_leader(curwin->w_cursor.lnum,
5160 leader_len, leader_flags,
5161 next_leader_len, next_leader_flags)
5162#endif
5163 )
5164 is_end_par = TRUE;
5165
5166 /*
5167 * If we have got to the end of a paragraph, or the line is
5168 * getting long, format it.
5169 */
5170 if (is_end_par || force_format)
5171 {
5172 if (need_set_indent)
5173 /* replace indent in first line with minimal number of
5174 * tabs and spaces, according to current options */
5175 (void)set_indent(get_indent(), SIN_CHANGED);
5176
5177 /* put cursor on last non-space */
5178 State = NORMAL; /* don't go past end-of-line */
5179 coladvance((colnr_T)MAXCOL);
5180 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5181 dec_cursor();
5182
5183 /* do the formatting, without 'showmode' */
5184 State = INSERT; /* for open_line() */
5185 smd_save = p_smd;
5186 p_smd = FALSE;
5187 insertchar(NUL, INSCHAR_FORMAT
5188#ifdef FEAT_COMMENTS
5189 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005190 + (do_comments && do_comments_list
5191 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005193 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 State = old_State;
5195 p_smd = smd_save;
5196 second_indent = -1;
5197 /* at end of par.: need to set indent of next par. */
5198 need_set_indent = is_end_par;
5199 if (is_end_par)
5200 {
5201 /* When called with a negative line count, break at the
5202 * end of the paragraph. */
5203 if (line_count < 0)
5204 break;
5205 first_par_line = TRUE;
5206 }
5207 force_format = FALSE;
5208 }
5209
5210 /*
5211 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005212 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 */
5214 if (!is_end_par)
5215 {
5216 advance = FALSE;
5217 curwin->w_cursor.lnum++;
5218 curwin->w_cursor.col = 0;
5219 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005223 {
5224 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5226 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005227 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005229 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5230 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005231 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005232
5233 if (indent > 0)
5234 {
5235 (void)del_bytes(indent, FALSE, FALSE);
5236 mark_col_adjust(curwin->w_cursor.lnum,
5237 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005238 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005241 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
5243 beep_flush();
5244 break;
5245 }
5246 first_par_line = FALSE;
5247 /* If the line is getting long, format it next time */
5248 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5249 force_format = TRUE;
5250 else
5251 force_format = FALSE;
5252 }
5253 }
5254 line_breakcheck();
5255 }
5256}
5257
5258/*
5259 * Return TRUE if line "lnum" ends in a white character.
5260 */
5261 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 char_u *s = ml_get(lnum);
5265 size_t l;
5266
5267 if (*s == NUL)
5268 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005269 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 * invocation may call function multiple times". */
5271 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005272 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
5275/*
5276 * Blank lines, and lines containing only the comment leader, are left
5277 * untouched by the formatting. The function returns TRUE in this
5278 * case. It also returns TRUE when a line starts with the end of a comment
5279 * ('e' in comment flags), so that this line is skipped, and not joined to the
5280 * previous line. A new paragraph starts after a blank line, or when the
5281 * comment leader changes -- webb.
5282 */
5283#ifdef FEAT_COMMENTS
5284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005285fmt_check_par(
5286 linenr_T lnum,
5287 int *leader_len,
5288 char_u **leader_flags,
5289 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
5291 char_u *flags = NULL; /* init for GCC */
5292 char_u *ptr;
5293
5294 ptr = ml_get(lnum);
5295 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005296 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 else
5298 *leader_len = 0;
5299
5300 if (*leader_len > 0)
5301 {
5302 /*
5303 * Search for 'e' flag in comment leader flags.
5304 */
5305 flags = *leader_flags;
5306 while (*flags && *flags != ':' && *flags != COM_END)
5307 ++flags;
5308 }
5309
5310 return (*skipwhite(ptr + *leader_len) == NUL
5311 || (*leader_len > 0 && *flags == COM_END)
5312 || startPS(lnum, NUL, FALSE));
5313}
5314#else
5315 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5319}
5320#endif
5321
5322/*
5323 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5324 * previous line is in the same paragraph. Used for auto-formatting.
5325 */
5326 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005327paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 char_u *p;
5330#ifdef FEAT_COMMENTS
5331 int leader_len = 0; /* leader len of current line */
5332 char_u *leader_flags = NULL; /* flags for leader of current line */
5333 int next_leader_len; /* leader len of next line */
5334 char_u *next_leader_flags; /* flags for leader of next line */
5335 int do_comments; /* format comments */
5336#endif
5337
5338 if (lnum <= 1)
5339 return TRUE; /* start of the file */
5340
5341 p = ml_get(lnum - 1);
5342 if (*p == NUL)
5343 return TRUE; /* after empty line */
5344
5345#ifdef FEAT_COMMENTS
5346 do_comments = has_format_option(FO_Q_COMS);
5347#endif
5348 if (fmt_check_par(lnum - 1
5349#ifdef FEAT_COMMENTS
5350 , &leader_len, &leader_flags, do_comments
5351#endif
5352 ))
5353 return TRUE; /* after non-paragraph line */
5354
5355 if (fmt_check_par(lnum
5356#ifdef FEAT_COMMENTS
5357 , &next_leader_len, &next_leader_flags, do_comments
5358#endif
5359 ))
5360 return TRUE; /* "lnum" is not a paragraph line */
5361
5362 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5363 return TRUE; /* missing trailing space in previous line. */
5364
5365 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5366 return TRUE; /* numbered item starts in "lnum". */
5367
5368#ifdef FEAT_COMMENTS
5369 if (!same_leader(lnum - 1, leader_len, leader_flags,
5370 next_leader_len, next_leader_flags))
5371 return TRUE; /* change of comment leader. */
5372#endif
5373
5374 return FALSE;
5375}
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377/*
5378 * prepare a few things for block mode yank/delete/tilde
5379 *
5380 * for delete:
5381 * - textlen includes the first/last char to be (partly) deleted
5382 * - start/endspaces is the number of columns that are taken by the
5383 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005384 * deleted.
5385 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 * - textlen includes the first/last char to be wholly yanked
5387 * - start/endspaces is the number of columns of the first/last yanked char
5388 * that are to be yanked.
5389 */
5390 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005391block_prep(
5392 oparg_T *oap,
5393 struct block_def *bdp,
5394 linenr_T lnum,
5395 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397 int incr = 0;
5398 char_u *pend;
5399 char_u *pstart;
5400 char_u *line;
5401 char_u *prev_pstart;
5402 char_u *prev_pend;
5403
5404 bdp->startspaces = 0;
5405 bdp->endspaces = 0;
5406 bdp->textlen = 0;
5407 bdp->start_vcol = 0;
5408 bdp->end_vcol = 0;
5409#ifdef FEAT_VISUALEXTRA
5410 bdp->is_short = FALSE;
5411 bdp->is_oneChar = FALSE;
5412 bdp->pre_whitesp = 0;
5413 bdp->pre_whitesp_c = 0;
5414 bdp->end_char_vcols = 0;
5415#endif
5416 bdp->start_char_vcols = 0;
5417
5418 line = ml_get(lnum);
5419 pstart = line;
5420 prev_pstart = line;
5421 while (bdp->start_vcol < oap->start_vcol && *pstart)
5422 {
5423 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005424 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 bdp->start_vcol += incr;
5426#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005427 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 bdp->pre_whitesp += incr;
5430 bdp->pre_whitesp_c++;
5431 }
5432 else
5433 {
5434 bdp->pre_whitesp = 0;
5435 bdp->pre_whitesp_c = 0;
5436 }
5437#endif
5438 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005439 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441 bdp->start_char_vcols = incr;
5442 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5443 {
5444 bdp->end_vcol = bdp->start_vcol;
5445#ifdef FEAT_VISUALEXTRA
5446 bdp->is_short = TRUE;
5447#endif
5448 if (!is_del || oap->op_type == OP_APPEND)
5449 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5450 }
5451 else
5452 {
5453 /* notice: this converts partly selected Multibyte characters to
5454 * spaces, too. */
5455 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5456 if (is_del && bdp->startspaces)
5457 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5458 pend = pstart;
5459 bdp->end_vcol = bdp->start_vcol;
5460 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5461 {
5462#ifdef FEAT_VISUALEXTRA
5463 bdp->is_oneChar = TRUE;
5464#endif
5465 if (oap->op_type == OP_INSERT)
5466 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5467 else if (oap->op_type == OP_APPEND)
5468 {
5469 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5470 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5471 }
5472 else
5473 {
5474 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5475 if (is_del && oap->op_type != OP_LSHIFT)
5476 {
5477 /* just putting the sum of those two into
5478 * bdp->startspaces doesn't work for Visual replace,
5479 * so we have to split the tab in two */
5480 bdp->startspaces = bdp->start_char_vcols
5481 - (bdp->start_vcol - oap->start_vcol);
5482 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5483 }
5484 }
5485 }
5486 else
5487 {
5488 prev_pend = pend;
5489 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5490 {
5491 /* Count a tab for what it's worth (if list mode not on) */
5492 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005493 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 bdp->end_vcol += incr;
5495 }
5496 if (bdp->end_vcol <= oap->end_vcol
5497 && (!is_del
5498 || oap->op_type == OP_APPEND
5499 || oap->op_type == OP_REPLACE)) /* line too short */
5500 {
5501#ifdef FEAT_VISUALEXTRA
5502 bdp->is_short = TRUE;
5503#endif
5504 /* Alternative: include spaces to fill up the block.
5505 * Disadvantage: can lead to trailing spaces when the line is
5506 * short where the text is put */
5507 /* if (!is_del || oap->op_type == OP_APPEND) */
5508 if (oap->op_type == OP_APPEND || virtual_op)
5509 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005510 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 else
5512 bdp->endspaces = 0; /* replace doesn't add characters */
5513 }
5514 else if (bdp->end_vcol > oap->end_vcol)
5515 {
5516 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5517 if (!is_del && bdp->endspaces)
5518 {
5519 bdp->endspaces = incr - bdp->endspaces;
5520 if (pend != pstart)
5521 pend = prev_pend;
5522 }
5523 }
5524 }
5525#ifdef FEAT_VISUALEXTRA
5526 bdp->end_char_vcols = incr;
5527#endif
5528 if (is_del && bdp->startspaces)
5529 pstart = prev_pstart;
5530 bdp->textlen = (int)(pend - pstart);
5531 }
5532 bdp->textcol = (colnr_T) (pstart - line);
5533 bdp->textstart = pstart;
5534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005537 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540op_addsub(
5541 oparg_T *oap,
5542 linenr_T Prenum1, /* Amount of add/subtract */
5543 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005545 pos_T pos;
5546 struct block_def bd;
5547 int change_cnt = 0;
5548 linenr_T amount = Prenum1;
5549
5550 if (!VIsual_active)
5551 {
5552 pos = curwin->w_cursor;
5553 if (u_save_cursor() == FAIL)
5554 return;
5555 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5556 if (change_cnt)
5557 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5558 }
5559 else
5560 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005561 int one_change;
5562 int length;
5563 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005564
5565 if (u_save((linenr_T)(oap->start.lnum - 1),
5566 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5567 return;
5568
5569 pos = oap->start;
5570 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5571 {
5572 if (oap->block_mode) /* Visual block mode */
5573 {
5574 block_prep(oap, &bd, pos.lnum, FALSE);
5575 pos.col = bd.textcol;
5576 length = bd.textlen;
5577 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005578 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005579 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005580 curwin->w_cursor.col = 0;
5581 pos.col = 0;
5582 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5583 }
5584 else /* oap->motion_type == MCHAR */
5585 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005586 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005587 dec(&(oap->end));
5588 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5589 pos.col = 0;
5590 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005591 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005592 pos.col += oap->start.col;
5593 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005594 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005595 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005596 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005597 length = (int)STRLEN(ml_get(oap->end.lnum));
5598 if (oap->end.col >= length)
5599 oap->end.col = length - 1;
5600 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005601 }
5602 }
5603 one_change = do_addsub(oap->op_type, &pos, length, amount);
5604 if (one_change)
5605 {
5606 /* Remember the start position of the first change. */
5607 if (change_cnt == 0)
5608 startpos = curbuf->b_op_start;
5609 ++change_cnt;
5610 }
5611
5612#ifdef FEAT_NETBEANS_INTG
5613 if (netbeans_active() && one_change)
5614 {
5615 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5616
5617 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5618 netbeans_inserted(curbuf, pos.lnum, pos.col,
5619 &ptr[pos.col], length);
5620 }
5621#endif
5622 if (g_cmd && one_change)
5623 amount += Prenum1;
5624 }
5625 if (change_cnt)
5626 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5627
5628 if (!change_cnt && oap->is_VIsual)
5629 /* No change: need to remove the Visual selection */
5630 redraw_curbuf_later(INVERTED);
5631
5632 /* Set '[ mark if something changed. Keep the last end
5633 * position from do_addsub(). */
5634 if (change_cnt > 0)
5635 curbuf->b_op_start = startpos;
5636
5637 if (change_cnt > p_report)
5638 {
5639 if (change_cnt == 1)
5640 MSG(_("1 line changed"));
5641 else
5642 smsg((char_u *)_("%ld lines changed"), change_cnt);
5643 }
5644 }
5645}
5646
5647/*
5648 * Add or subtract 'Prenum1' from a number in a line
5649 * op_type is OP_NR_ADD or OP_NR_SUB
5650 *
5651 * Returns TRUE if some character was changed.
5652 */
5653 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005654do_addsub(
5655 int op_type,
5656 pos_T *pos,
5657 int length,
5658 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005659{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 int col;
5661 char_u *buf1;
5662 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005663 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005665 uvarnumber_T n;
5666 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 char_u *ptr;
5668 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 int todel;
5670 int dohex;
5671 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005672 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 int doalp;
5674 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005676 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005677 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005678 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005679 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005680 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005681 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005682 pos_T startpos;
5683 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
5685 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5686 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005687 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5689
Bram Moolenaard79e5502016-01-10 22:13:02 +01005690 curwin->w_cursor = *pos;
5691 ptr = ml_get(pos->lnum);
5692 col = pos->col;
5693
5694 if (*ptr == NUL)
5695 goto theend;
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 /*
5698 * First check if we are on a hexadecimal number, after the "0x".
5699 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005700 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005702 if (dobin)
5703 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005704 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005705 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005706#ifdef FEAT_MBYTE
5707 if (has_mbyte)
5708 col -= (*mb_head_off)(ptr, ptr + col);
5709#endif
5710 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005711
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005712 if (dohex)
5713 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005714 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005715 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005716#ifdef FEAT_MBYTE
5717 if (has_mbyte)
5718 col -= (*mb_head_off)(ptr, ptr + col);
5719#endif
5720 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005721
5722 if ( dobin
5723 && dohex
5724 && ! ((col > 0
5725 && (ptr[col] == 'X'
5726 || ptr[col] == 'x')
5727 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005728#ifdef FEAT_MBYTE
5729 && (!has_mbyte ||
5730 !(*mb_head_off)(ptr, ptr + col - 1))
5731#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005732 && vim_isxdigit(ptr[col + 1]))))
5733 {
5734
5735 /* In case of binary/hexadecimal pattern overlap match, rescan */
5736
Bram Moolenaard79e5502016-01-10 22:13:02 +01005737 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005738
5739 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005740 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005741 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005742#ifdef FEAT_MBYTE
5743 if (has_mbyte)
5744 col -= (*mb_head_off)(ptr, ptr + col);
5745#endif
5746 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005747 }
5748
5749 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005750 && col > 0
5751 && (ptr[col] == 'X'
5752 || ptr[col] == 'x')
5753 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005754#ifdef FEAT_MBYTE
5755 && (!has_mbyte ||
5756 !(*mb_head_off)(ptr, ptr + col - 1))
5757#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005758 && vim_isxdigit(ptr[col + 1])) ||
5759 ( dobin
5760 && col > 0
5761 && (ptr[col] == 'B'
5762 || ptr[col] == 'b')
5763 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005764#ifdef FEAT_MBYTE
5765 && (!has_mbyte ||
5766 !(*mb_head_off)(ptr, ptr + col - 1))
5767#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005768 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005769 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005770 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005771 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005772#ifdef FEAT_MBYTE
5773 if (has_mbyte)
5774 col -= (*mb_head_off)(ptr, ptr + col);
5775#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005776 }
5777 else
5778 {
5779 /*
5780 * Search forward and then backward to find the start of number.
5781 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005782 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005783
5784 while (ptr[col] != NUL
5785 && !vim_isdigit(ptr[col])
5786 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005787 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005788
5789 while (col > 0
5790 && vim_isdigit(ptr[col - 1])
5791 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005792 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005793 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005794#ifdef FEAT_MBYTE
5795 if (has_mbyte)
5796 col -= (*mb_head_off)(ptr, ptr + col);
5797#endif
5798 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005799 }
5800 }
5801
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005802 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005803 {
5804 while (ptr[col] != NUL && length > 0
5805 && !vim_isdigit(ptr[col])
5806 && !(doalp && ASCII_ISALPHA(ptr[col])))
5807 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005808 int mb_len = MB_PTR2LEN(ptr + col);
5809
5810 col += mb_len;
5811 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005812 }
5813
5814 if (length == 0)
5815 goto theend;
5816
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005817 if (col > pos->col && ptr[col - 1] == '-'
5818#ifdef FEAT_MBYTE
5819 && (!has_mbyte ||
5820 !(*mb_head_off)(ptr, ptr + col - 1))
5821#endif
5822 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005823 {
5824 negative = TRUE;
5825 was_positive = FALSE;
5826 }
5827 }
5828
5829 /*
5830 * If a number was found, and saving for undo works, replace the number.
5831 */
5832 firstdigit = ptr[col];
5833 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5834 {
5835 beep_flush();
5836 goto theend;
5837 }
5838
5839 if (doalp && ASCII_ISALPHA(firstdigit))
5840 {
5841 /* decrement or increment alphabetic character */
5842 if (op_type == OP_NR_SUB)
5843 {
5844 if (CharOrd(firstdigit) < Prenum1)
5845 {
5846 if (isupper(firstdigit))
5847 firstdigit = 'A';
5848 else
5849 firstdigit = 'a';
5850 }
5851 else
5852#ifdef EBCDIC
5853 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5854#else
5855 firstdigit -= Prenum1;
5856#endif
5857 }
5858 else
5859 {
5860 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5861 {
5862 if (isupper(firstdigit))
5863 firstdigit = 'Z';
5864 else
5865 firstdigit = 'z';
5866 }
5867 else
5868#ifdef EBCDIC
5869 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5870#else
5871 firstdigit += Prenum1;
5872#endif
5873 }
5874 curwin->w_cursor.col = col;
5875 if (!did_change)
5876 startpos = curwin->w_cursor;
5877 did_change = TRUE;
5878 (void)del_char(FALSE);
5879 ins_char(firstdigit);
5880 endpos = curwin->w_cursor;
5881 curwin->w_cursor.col = col;
5882 }
5883 else
5884 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005885 if (col > 0 && ptr[col - 1] == '-'
5886#ifdef FEAT_MBYTE
5887 && (!has_mbyte ||
5888 !(*mb_head_off)(ptr, ptr + col - 1))
5889#endif
5890 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005891 {
5892 /* negative number */
5893 --col;
5894 negative = TRUE;
5895 }
5896 /* get the number value (unsigned) */
5897 if (visual && VIsual_mode != 'V')
5898 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5899 ? (int)STRLEN(ptr) - col
5900 : length);
5901
5902 vim_str2nr(ptr + col, &pre, &length,
5903 0 + (dobin ? STR2NR_BIN : 0)
5904 + (dooct ? STR2NR_OCT : 0)
5905 + (dohex ? STR2NR_HEX : 0),
5906 NULL, &n, maxlen);
5907
5908 /* ignore leading '-' for hex and octal and bin numbers */
5909 if (pre && negative)
5910 {
5911 ++col;
5912 --length;
5913 negative = FALSE;
5914 }
5915 /* add or subtract */
5916 subtract = FALSE;
5917 if (op_type == OP_NR_SUB)
5918 subtract ^= TRUE;
5919 if (negative)
5920 subtract ^= TRUE;
5921
5922 oldn = n;
5923 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005924 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005925 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005926 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005927 /* handle wraparound for decimal numbers */
5928 if (!pre)
5929 {
5930 if (subtract)
5931 {
5932 if (n > oldn)
5933 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005934 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005935 negative ^= TRUE;
5936 }
5937 }
5938 else
5939 {
5940 /* add */
5941 if (n < oldn)
5942 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005943 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005944 negative ^= TRUE;
5945 }
5946 }
5947 if (n == 0)
5948 negative = FALSE;
5949 }
5950
5951 if (visual && !was_positive && !negative && col > 0)
5952 {
5953 /* need to remove the '-' */
5954 col--;
5955 length++;
5956 }
5957
5958 /*
5959 * Delete the old number.
5960 */
5961 curwin->w_cursor.col = col;
5962 if (!did_change)
5963 startpos = curwin->w_cursor;
5964 did_change = TRUE;
5965 todel = length;
5966 c = gchar_cursor();
5967 /*
5968 * Don't include the '-' in the length, only the length of the
5969 * part after it is kept the same.
5970 */
5971 if (c == '-')
5972 --length;
5973 while (todel-- > 0)
5974 {
5975 if (c < 0x100 && isalpha(c))
5976 {
5977 if (isupper(c))
5978 hexupper = TRUE;
5979 else
5980 hexupper = FALSE;
5981 }
5982 /* del_char() will mark line needing displaying */
5983 (void)del_char(FALSE);
5984 c = gchar_cursor();
5985 }
5986
5987 /*
5988 * Prepare the leading characters in buf1[].
5989 * When there are many leading zeros it could be very long.
5990 * Allocate a bit too much.
5991 */
5992 buf1 = alloc((unsigned)length + NUMBUFLEN);
5993 if (buf1 == NULL)
5994 goto theend;
5995 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005996 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005997 {
5998 *ptr++ = '-';
5999 }
6000 if (pre)
6001 {
6002 *ptr++ = '0';
6003 --length;
6004 }
6005 if (pre == 'b' || pre == 'B' ||
6006 pre == 'x' || pre == 'X')
6007 {
6008 *ptr++ = pre;
6009 --length;
6010 }
6011
6012 /*
6013 * Put the number characters in buf2[].
6014 */
6015 if (pre == 'b' || pre == 'B')
6016 {
6017 int i;
6018 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006019 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006020
6021 /* leading zeros */
6022 for (bit = bits; bit > 0; bit--)
6023 if ((n >> (bit - 1)) & 0x1) break;
6024
6025 for (i = 0; bit > 0; bit--)
6026 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6027
6028 buf2[i] = '\0';
6029 }
6030 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006031 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6032 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006033 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006034 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6035 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006036 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006037 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6038 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006039 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006040 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6041 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006042 length -= (int)STRLEN(buf2);
6043
6044 /*
6045 * Adjust number of zeros to the new number of digits, so the
6046 * total length of the number remains the same.
6047 * Don't do this when
6048 * the result may look like an octal number.
6049 */
6050 if (firstdigit == '0' && !(dooct && pre == 0))
6051 while (length-- > 0)
6052 *ptr++ = '0';
6053 *ptr = NUL;
6054 STRCAT(buf1, buf2);
6055 ins_str(buf1); /* insert the new number */
6056 vim_free(buf1);
6057 endpos = curwin->w_cursor;
6058 if (did_change && curwin->w_cursor.col)
6059 --curwin->w_cursor.col;
6060 }
6061
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006062 if (did_change)
6063 {
6064 /* set the '[ and '] marks */
6065 curbuf->b_op_start = startpos;
6066 curbuf->b_op_end = endpos;
6067 if (curbuf->b_op_end.col > 0)
6068 --curbuf->b_op_end.col;
6069 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006070
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006071theend:
6072 if (visual)
6073 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006074 else if (did_change)
6075 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006076
Bram Moolenaard79e5502016-01-10 22:13:02 +01006077 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078}
6079
6080#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006081
6082static yankreg_T *y_read_regs = NULL;
6083
6084#define REG_PREVIOUS 1
6085#define REG_EXEC 2
6086
6087/*
6088 * Prepare for reading viminfo registers when writing viminfo later.
6089 */
6090 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006091prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006092{
6093 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6094 * (int)sizeof(yankreg_T));
6095}
6096
6097 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006098finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006099{
6100 int i;
6101 int j;
6102
6103 if (y_read_regs != NULL)
6104 {
6105 for (i = 0; i < NUM_REGISTERS; ++i)
6106 if (y_read_regs[i].y_array != NULL)
6107 {
6108 for (j = 0; j < y_read_regs[i].y_size; j++)
6109 vim_free(y_read_regs[i].y_array[j]);
6110 vim_free(y_read_regs[i].y_array);
6111 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006112 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006113 }
6114}
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 int eof;
6120 int do_it = TRUE;
6121 int size;
6122 int limit;
6123 int i;
6124 int set_prev = FALSE;
6125 char_u *str;
6126 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006127 int new_type = MCHAR; /* init to shut up compiler */
6128 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 /* We only get here (hopefully) if line[0] == '"' */
6131 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006132
6133 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (*str == '"')
6135 {
6136 set_prev = TRUE;
6137 str++;
6138 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006139
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (!ASCII_ISALNUM(*str) && *str != '-')
6141 {
6142 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6143 return TRUE; /* too many errors, pretend end-of-file */
6144 do_it = FALSE;
6145 }
6146 get_yank_register(*str++, FALSE);
6147 if (!force && y_current->y_array != NULL)
6148 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006149
6150 if (*str == '@')
6151 {
6152 /* "x@: register x used for @@ */
6153 if (force || execreg_lastc == NUL)
6154 execreg_lastc = str[-1];
6155 }
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 size = 0;
6158 limit = 100; /* Optimized for registers containing <= 100 lines */
6159 if (do_it)
6160 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006161 /*
6162 * Build the new register in array[].
6163 * y_array is kept as-is until done.
6164 * The "do_it" flag is reset when something is wrong, in which case
6165 * array[] needs to be freed.
6166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (set_prev)
6168 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006169 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006170 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006172 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006174 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006176 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 /* get the block width; if it's missing we get a zero, which is OK */
6178 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006179 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 }
6181
6182 while (!(eof = viminfo_readline(virp))
6183 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6184 {
6185 if (do_it)
6186 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006187 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006189 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006191
6192 if (new_array == NULL)
6193 {
6194 do_it = FALSE;
6195 break;
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006198 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006200 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203 str = viminfo_readstring(virp, 1, TRUE);
6204 if (str != NULL)
6205 array[size++] = str;
6206 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006207 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 do_it = FALSE;
6209 }
6210 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 if (do_it)
6213 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006214 /* free y_array[] */
6215 for (i = 0; i < y_current->y_size; i++)
6216 vim_free(y_current->y_array[i]);
6217 vim_free(y_current->y_array);
6218
6219 y_current->y_type = new_type;
6220 y_current->y_width = new_width;
6221 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006222 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (size == 0)
6224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 y_current->y_array = NULL;
6226 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006229 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 y_current->y_array =
6231 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6232 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006233 {
6234 if (y_current->y_array == NULL)
6235 vim_free(array[i]);
6236 else
6237 y_current->y_array[i] = array[i];
6238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006241 else
6242 {
6243 /* Free array[] if it was filled. */
6244 for (i = 0; i < size; i++)
6245 vim_free(array[i]);
6246 }
6247 vim_free(array);
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 return eof;
6250}
6251
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006252/*
6253 * Accept a new style register line from the viminfo, store it when it's new.
6254 */
6255 void
6256handle_viminfo_register(garray_T *values, int force)
6257{
6258 bval_T *vp = (bval_T *)values->ga_data;
6259 int flags;
6260 int name;
6261 int type;
6262 int linecount;
6263 int width;
6264 time_t timestamp;
6265 yankreg_T *y_ptr;
6266 int i;
6267
6268 /* Check the format:
6269 * |{bartype},{flags},{name},{type},
6270 * {linecount},{width},{timestamp},"line1","line2"
6271 */
6272 if (values->ga_len < 6
6273 || vp[0].bv_type != BVAL_NR
6274 || vp[1].bv_type != BVAL_NR
6275 || vp[2].bv_type != BVAL_NR
6276 || vp[3].bv_type != BVAL_NR
6277 || vp[4].bv_type != BVAL_NR
6278 || vp[5].bv_type != BVAL_NR)
6279 return;
6280 flags = vp[0].bv_nr;
6281 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006282 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006283 return;
6284 type = vp[2].bv_nr;
6285 if (type != MCHAR && type != MLINE && type != MBLOCK)
6286 return;
6287 linecount = vp[3].bv_nr;
6288 if (values->ga_len < 6 + linecount)
6289 return;
6290 width = vp[4].bv_nr;
6291 if (width < 0)
6292 return;
6293
6294 if (y_read_regs != NULL)
6295 /* Reading viminfo for merging and writing. Store the register
6296 * content, don't update the current registers. */
6297 y_ptr = &y_read_regs[name];
6298 else
6299 y_ptr = &y_regs[name];
6300
6301 /* Do not overwrite unless forced or the timestamp is newer. */
6302 timestamp = (time_t)vp[5].bv_nr;
6303 if (y_ptr->y_array != NULL && !force
6304 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6305 return;
6306
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006307 if (y_ptr->y_array != NULL)
6308 for (i = 0; i < y_ptr->y_size; i++)
6309 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006310 vim_free(y_ptr->y_array);
6311
6312 if (y_read_regs == NULL)
6313 {
6314 if (flags & REG_PREVIOUS)
6315 y_previous = y_ptr;
6316 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6317 execreg_lastc = get_register_name(name);
6318 }
6319 y_ptr->y_type = type;
6320 y_ptr->y_width = width;
6321 y_ptr->y_size = linecount;
6322 y_ptr->y_time_set = timestamp;
6323 if (linecount == 0)
6324 y_ptr->y_array = NULL;
6325 else
6326 {
6327 y_ptr->y_array =
6328 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6329 for (i = 0; i < linecount; i++)
6330 {
6331 if (vp[i + 6].bv_allocated)
6332 {
6333 y_ptr->y_array[i] = vp[i + 6].bv_string;
6334 vp[i + 6].bv_string = NULL;
6335 }
6336 else
6337 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6338 }
6339 }
6340}
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006345 int i, j;
6346 char_u *type;
6347 char_u c;
6348 int num_lines;
6349 int max_num_lines;
6350 int max_kbyte;
6351 long len;
6352 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaar64404472010-06-26 06:24:45 +02006354 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 /* Get '<' value, use old '"' value if '<' is not found. */
6357 max_num_lines = get_viminfo_parameter('<');
6358 if (max_num_lines < 0)
6359 max_num_lines = get_viminfo_parameter('"');
6360 if (max_num_lines == 0)
6361 return;
6362 max_kbyte = get_viminfo_parameter('s');
6363 if (max_kbyte == 0)
6364 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006365
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 for (i = 0; i < NUM_REGISTERS; i++)
6367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368#ifdef FEAT_CLIPBOARD
6369 /* Skip '*'/'+' register, we don't want them back next time */
6370 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6371 continue;
6372#endif
6373#ifdef FEAT_DND
6374 /* Neither do we want the '~' register */
6375 if (i == TILDE_REGISTER)
6376 continue;
6377#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006378 /* When reading viminfo for merging and writing: Use the register from
6379 * viminfo if it's newer. */
6380 if (y_read_regs != NULL
6381 && y_read_regs[i].y_array != NULL
6382 && (y_regs[i].y_array == NULL ||
6383 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6384 y_ptr = &y_read_regs[i];
6385 else if (y_regs[i].y_array == NULL)
6386 continue;
6387 else
6388 y_ptr = &y_regs[i];
6389
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006390 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006391 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006392 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006393 || (num_lines == 1 && y_ptr->y_type == MCHAR
6394 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006395 continue;
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (max_kbyte > 0)
6398 {
6399 /* Skip register if there is more text than the maximum size. */
6400 len = 0;
6401 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006402 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (len > (long)max_kbyte * 1024L)
6404 continue;
6405 }
6406
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006407 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
6409 case MLINE:
6410 type = (char_u *)"LINE";
6411 break;
6412 case MCHAR:
6413 type = (char_u *)"CHAR";
6414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 case MBLOCK:
6416 type = (char_u *)"BLOCK";
6417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 default:
6419 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006420 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 emsg(IObuff);
6422 type = (char_u *)"LINE";
6423 break;
6424 }
6425 if (y_previous == &y_regs[i])
6426 fprintf(fp, "\"");
6427 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006428 fprintf(fp, "\"%c", c);
6429 if (c == execreg_lastc)
6430 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006431 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 /* If max_num_lines < 0, then we save ALL the lines in the register */
6434 if (max_num_lines > 0 && num_lines > max_num_lines)
6435 num_lines = max_num_lines;
6436 for (j = 0; j < num_lines; j++)
6437 {
6438 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006439 viminfo_writestring(fp, y_ptr->y_array[j]);
6440 }
6441
6442 {
6443 int flags = 0;
6444 int remaining;
6445
6446 /* New style with a bar line. Format:
6447 * |{bartype},{flags},{name},{type},
6448 * {linecount},{width},{timestamp},"line1","line2"
6449 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006450 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006451 */
6452 if (y_previous == &y_regs[i])
6453 flags |= REG_PREVIOUS;
6454 if (c == execreg_lastc)
6455 flags |= REG_EXEC;
6456 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6457 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6458 (long)y_ptr->y_time_set);
6459 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6460 remaining = LSIZE - 71;
6461 for (j = 0; j < num_lines; j++)
6462 {
6463 putc(',', fp);
6464 --remaining;
6465 remaining = barline_writestring(fp, y_ptr->y_array[j],
6466 remaining);
6467 }
6468 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 }
6471}
6472#endif /* FEAT_VIMINFO */
6473
6474#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6475/*
6476 * SELECTION / PRIMARY ('*')
6477 *
6478 * Text selection stuff that uses the GUI selection register '*'. When using a
6479 * GUI this may be text from another window, otherwise it is the last text we
6480 * had highlighted with VIsual mode. With mouse support, clicking the middle
6481 * button performs the paste, otherwise you will need to do <"*p>. "
6482 * If not under X, it is synonymous with the clipboard register '+'.
6483 *
6484 * X CLIPBOARD ('+')
6485 *
6486 * Text selection stuff that uses the GUI clipboard register '+'.
6487 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6488 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6489 * otherwise you will need to do <"+p>. "
6490 * If not under X, it is synonymous with the selection register '*'.
6491 */
6492
6493/*
6494 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006495 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 * only exist while the owning application exists, so we write to the
6497 * permanent (while X runs) store CUT_BUFFER0.
6498 * Dump the CLIPBOARD selection if we own it (it's logically the more
6499 * 'permanent' of the two), otherwise the PRIMARY one.
6500 * For now, use a hard-coded sanity limit of 1Mb of data.
6501 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006502#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 Display *dpy;
6507 char_u *str = NULL;
6508 long_u len = 0;
6509 int motion_type = -1;
6510
6511# ifdef FEAT_GUI
6512 if (gui.in_use)
6513 dpy = X_DISPLAY;
6514 else
6515# endif
6516# ifdef FEAT_XCLIPBOARD
6517 dpy = xterm_dpy;
6518# else
6519 return;
6520# endif
6521
6522 /* Get selection to export */
6523 if (clip_plus.owned)
6524 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6525 else if (clip_star.owned)
6526 motion_type = clip_convert_selection(&str, &len, &clip_star);
6527
6528 /* Check it's OK */
6529 if (dpy != NULL && str != NULL && motion_type >= 0
6530 && len < 1024*1024 && len > 0)
6531 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006532#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006533 int ok = TRUE;
6534
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006535 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6536 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6537 * encoding conversion usually doesn't work, so keep the text as-is.
6538 */
6539 if (has_mbyte)
6540 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006541 vimconv_T vc;
6542
6543 vc.vc_type = CONV_NONE;
6544 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6545 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006546 int intlen = len;
6547 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006548
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006549 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006550 conv_str = string_convert(&vc, str, &intlen);
6551 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006552 if (conv_str != NULL)
6553 {
6554 vim_free(str);
6555 str = conv_str;
6556 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006557 else
6558 {
6559 ok = FALSE;
6560 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006561 convert_setup(&vc, NULL, NULL);
6562 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006563 else
6564 {
6565 ok = FALSE;
6566 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006567 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006568
6569 /* Do not store the string if conversion failed. Better to use any
6570 * other selection than garbled text. */
6571 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006572#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006573 {
6574 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6575 XFlush(dpy);
6576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 }
6578
6579 vim_free(str);
6580}
6581#endif
6582
6583 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006586 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
6588 if (cbd == &clip_plus)
6589 y_current = &y_regs[PLUS_REGISTER];
6590 else
6591 y_current = &y_regs[STAR_REGISTER];
6592 free_yank_all();
6593 y_current->y_size = 0;
6594 y_current = y_ptr;
6595}
6596
6597/*
6598 * Get the selected text and put it in the gui selection register '*' or '+'.
6599 */
6600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006603 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 pos_T old_visual;
6606 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 colnr_T old_curswant;
6608 int old_set_curswant;
6609 pos_T old_op_start, old_op_end;
6610 oparg_T oa;
6611 cmdarg_T ca;
6612
6613 if (cbd->owned)
6614 {
6615 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6616 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6617 return;
6618
6619 /* Get the text between clip_star.start & clip_star.end */
6620 old_y_previous = y_previous;
6621 old_y_current = y_current;
6622 old_cursor = curwin->w_cursor;
6623 old_curswant = curwin->w_curswant;
6624 old_set_curswant = curwin->w_set_curswant;
6625 old_op_start = curbuf->b_op_start;
6626 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 old_visual = VIsual;
6628 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 clear_oparg(&oa);
6630 oa.regname = (cbd == &clip_plus ? '+' : '*');
6631 oa.op_type = OP_YANK;
6632 vim_memset(&ca, 0, sizeof(ca));
6633 ca.oap = &oa;
6634 ca.cmdchar = 'y';
6635 ca.count1 = 1;
6636 ca.retval = CA_NO_ADJ_OP_END;
6637 do_pending_operator(&ca, 0, TRUE);
6638 y_previous = old_y_previous;
6639 y_current = old_y_current;
6640 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006641 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 curwin->w_curswant = old_curswant;
6643 curwin->w_set_curswant = old_set_curswant;
6644 curbuf->b_op_start = old_op_start;
6645 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 VIsual = old_visual;
6647 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006649 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
6651 clip_free_selection(cbd);
6652
6653 /* Try to get selected text from another window */
6654 clip_gen_request_selection(cbd);
6655 }
6656}
6657
Bram Moolenaard44347f2011-06-19 01:14:29 +02006658/*
6659 * Convert from the GUI selection string into the '*'/'+' register.
6660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006662clip_yank_selection(
6663 int type,
6664 char_u *str,
6665 long len,
6666 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006668 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669
6670 if (cbd == &clip_plus)
6671 y_ptr = &y_regs[PLUS_REGISTER];
6672 else
6673 y_ptr = &y_regs[STAR_REGISTER];
6674
6675 clip_free_selection(cbd);
6676
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006677 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
6680/*
6681 * Convert the '*'/'+' register into a GUI selection string returned in *str
6682 * with length *len.
6683 * Returns the motion type, or -1 for failure.
6684 */
6685 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006686clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 char_u *p;
6689 int lnum;
6690 int i, j;
6691 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006692 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694 if (cbd == &clip_plus)
6695 y_ptr = &y_regs[PLUS_REGISTER];
6696 else
6697 y_ptr = &y_regs[STAR_REGISTER];
6698
6699#ifdef USE_CRNL
6700 eolsize = 2;
6701#else
6702 eolsize = 1;
6703#endif
6704
6705 *str = NULL;
6706 *len = 0;
6707 if (y_ptr->y_array == NULL)
6708 return -1;
6709
6710 for (i = 0; i < y_ptr->y_size; i++)
6711 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6712
6713 /*
6714 * Don't want newline character at end of last line if we're in MCHAR mode.
6715 */
6716 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6717 *len -= eolsize;
6718
6719 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6720 if (p == NULL)
6721 return -1;
6722 lnum = 0;
6723 for (i = 0, j = 0; i < (int)*len; i++, j++)
6724 {
6725 if (y_ptr->y_array[lnum][j] == '\n')
6726 p[i] = NUL;
6727 else if (y_ptr->y_array[lnum][j] == NUL)
6728 {
6729#ifdef USE_CRNL
6730 p[i++] = '\r';
6731#endif
6732#ifdef USE_CR
6733 p[i] = '\r';
6734#else
6735 p[i] = '\n';
6736#endif
6737 lnum++;
6738 j = -1;
6739 }
6740 else
6741 p[i] = y_ptr->y_array[lnum][j];
6742 }
6743 return y_ptr->y_type;
6744}
6745
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747/*
6748 * If we have written to a clipboard register, send the text to the clipboard.
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6754 {
6755 clip_own_selection(&clip_star);
6756 clip_gen_set_selection(&clip_star);
6757 }
6758 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6759 {
6760 clip_own_selection(&clip_plus);
6761 clip_gen_set_selection(&clip_plus);
6762 }
6763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
6765#endif /* FEAT_CLIPBOARD || PROTO */
6766
6767
6768#if defined(FEAT_DND) || defined(PROTO)
6769/*
6770 * Replace the contents of the '~' register with str.
6771 */
6772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006775 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777 curr = y_current;
6778 y_current = &y_regs[TILDE_REGISTER];
6779 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006780 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 y_current = curr;
6782}
6783#endif
6784
6785
6786#if defined(FEAT_EVAL) || defined(PROTO)
6787/*
6788 * Return the type of a register.
6789 * Used for getregtype()
6790 * Returns MAUTO for error.
6791 */
6792 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 switch (regname)
6796 {
6797 case '%': /* file name */
6798 case '#': /* alternate file name */
6799 case '=': /* expression */
6800 case ':': /* last command line */
6801 case '/': /* last search-pattern */
6802 case '.': /* last inserted text */
6803#ifdef FEAT_SEARCHPATH
6804 case Ctrl_F: /* Filename under cursor */
6805 case Ctrl_P: /* Path under cursor, expand via "path" */
6806#endif
6807 case Ctrl_W: /* word under cursor */
6808 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6809 case '_': /* black hole: always empty */
6810 return MCHAR;
6811 }
6812
6813#ifdef FEAT_CLIPBOARD
6814 regname = may_get_selection(regname);
6815#endif
6816
Bram Moolenaar32b92012014-01-14 12:33:36 +01006817 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006818 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 get_yank_register(regname, FALSE);
6821
6822 if (y_current->y_array != NULL)
6823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (reglen != NULL && y_current->y_type == MBLOCK)
6825 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 return y_current->y_type;
6827 }
6828 return MAUTO;
6829}
6830
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006831static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006832
6833/*
6834 * When "flags" has GREG_LIST return a list with text "s".
6835 * Otherwise just return "s".
6836 */
6837 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006838getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006839{
6840 if (flags & GREG_LIST)
6841 {
6842 list_T *list = list_alloc();
6843
6844 if (list != NULL)
6845 {
6846 if (list_append_string(list, NULL, -1) == FAIL)
6847 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006848 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006849 return NULL;
6850 }
6851 list->lv_first->li_tv.vval.v_string = s;
6852 }
6853 return (char_u *)list;
6854 }
6855 return s;
6856}
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * Return the contents of a register as a single allocated string.
6860 * Used for "@r" in expressions and for getreg().
6861 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006862 * Flags:
6863 * GREG_NO_EXPR Do not allow expression register
6864 * GREG_EXPR_SRC For the expression register: return expression itself,
6865 * not the result of its evaluation.
6866 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 */
6868 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 long i;
6872 char_u *retval;
6873 int allocated;
6874 long len;
6875
6876 /* Don't allow using an expression register inside an expression */
6877 if (regname == '=')
6878 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006879 if (flags & GREG_NO_EXPR)
6880 return NULL;
6881 if (flags & GREG_EXPR_SRC)
6882 return getreg_wrap_one_line(get_expr_line_src(), flags);
6883 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 }
6885
6886 if (regname == '@') /* "@@" is used for unnamed register */
6887 regname = '"';
6888
6889 /* check for valid regname */
6890 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6891 return NULL;
6892
6893#ifdef FEAT_CLIPBOARD
6894 regname = may_get_selection(regname);
6895#endif
6896
6897 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6898 {
6899 if (retval == NULL)
6900 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006901 if (allocated)
6902 return getreg_wrap_one_line(retval, flags);
6903 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
6906 get_yank_register(regname, FALSE);
6907 if (y_current->y_array == NULL)
6908 return NULL;
6909
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006910 if (flags & GREG_LIST)
6911 {
6912 list_T *list = list_alloc();
6913 int error = FALSE;
6914
6915 if (list == NULL)
6916 return NULL;
6917 for (i = 0; i < y_current->y_size; ++i)
6918 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6919 error = TRUE;
6920 if (error)
6921 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006922 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006923 return NULL;
6924 }
6925 return (char_u *)list;
6926 }
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /*
6929 * Compute length of resulting string.
6930 */
6931 len = 0;
6932 for (i = 0; i < y_current->y_size; ++i)
6933 {
6934 len += (long)STRLEN(y_current->y_array[i]);
6935 /*
6936 * Insert a newline between lines and after last line if
6937 * y_type is MLINE.
6938 */
6939 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6940 ++len;
6941 }
6942
6943 retval = lalloc(len + 1, TRUE);
6944
6945 /*
6946 * Copy the lines of the yank register into the string.
6947 */
6948 if (retval != NULL)
6949 {
6950 len = 0;
6951 for (i = 0; i < y_current->y_size; ++i)
6952 {
6953 STRCPY(retval + len, y_current->y_array[i]);
6954 len += (long)STRLEN(retval + len);
6955
6956 /*
6957 * Insert a NL between lines and after the last line if y_type is
6958 * MLINE.
6959 */
6960 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6961 retval[len++] = '\n';
6962 }
6963 retval[len] = NUL;
6964 }
6965
6966 return retval;
6967}
6968
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006969 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006970init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006971 int name,
6972 yankreg_T **old_y_previous,
6973 yankreg_T **old_y_current,
6974 int must_append,
6975 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006976{
6977 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6978 {
6979 emsg_invreg(name);
6980 return FAIL;
6981 }
6982
6983 /* Don't want to change the current (unnamed) register */
6984 *old_y_previous = y_previous;
6985 *old_y_current = y_current;
6986
6987 get_yank_register(name, TRUE);
6988 if (!y_append && !must_append)
6989 free_yank_all();
6990 return OK;
6991}
6992
6993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006994finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006995 int name,
6996 yankreg_T *old_y_previous,
6997 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006998{
6999# ifdef FEAT_CLIPBOARD
7000 /* Send text of clipboard register to the clipboard. */
7001 may_set_selection();
7002# endif
7003
7004 /* ':let @" = "val"' should change the meaning of the "" register */
7005 if (name != '"')
7006 y_previous = old_y_previous;
7007 y_current = old_y_current;
7008}
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010/*
7011 * Store string "str" in register "name".
7012 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7013 * If "must_append" is TRUE, always append to the register. Otherwise append
7014 * if "name" is an uppercase letter.
7015 * Note: "maxlen" and "must_append" don't work for the "/" register.
7016 * Careful: 'str' is modified, you may have to use a copy!
7017 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7018 */
7019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007020write_reg_contents(
7021 int name,
7022 char_u *str,
7023 int maxlen,
7024 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7027}
7028
7029 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007030write_reg_contents_lst(
7031 int name,
7032 char_u **strings,
7033 int maxlen UNUSED,
7034 int must_append,
7035 int yank_type,
7036 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007037{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007038 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007039
7040 if (name == '/'
7041#ifdef FEAT_EVAL
7042 || name == '='
7043#endif
7044 )
7045 {
7046 char_u *s;
7047
7048 if (strings[0] == NULL)
7049 s = (char_u *)"";
7050 else if (strings[1] != NULL)
7051 {
7052 EMSG(_("E883: search pattern and expression register may not "
7053 "contain two or more lines"));
7054 return;
7055 }
7056 else
7057 s = strings[0];
7058 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7059 return;
7060 }
7061
7062 if (name == '_') /* black hole: nothing to do */
7063 return;
7064
7065 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7066 &yank_type) == FAIL)
7067 return;
7068
7069 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7070
7071 finish_write_reg(name, old_y_previous, old_y_current);
7072}
7073
7074 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007075write_reg_contents_ex(
7076 int name,
7077 char_u *str,
7078 int maxlen,
7079 int must_append,
7080 int yank_type,
7081 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007083 yankreg_T *old_y_previous, *old_y_current;
7084 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaare7566042005-06-17 22:00:15 +00007086 if (maxlen >= 0)
7087 len = maxlen;
7088 else
7089 len = (long)STRLEN(str);
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 /* Special case: '/' search pattern */
7092 if (name == '/')
7093 {
7094 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7095 return;
7096 }
7097
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007098 if (name == '#')
7099 {
7100 buf_T *buf;
7101
7102 if (VIM_ISDIGIT(*str))
7103 {
7104 int num = atoi((char *)str);
7105
7106 buf = buflist_findnr(num);
7107 if (buf == NULL)
7108 EMSGN(_(e_nobufnr), (long)num);
7109 }
7110 else
7111 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7112 TRUE, FALSE, FALSE));
7113 if (buf == NULL)
7114 return;
7115 curwin->w_alt_fnum = buf->b_fnum;
7116 return;
7117 }
7118
Bram Moolenaare7566042005-06-17 22:00:15 +00007119#ifdef FEAT_EVAL
7120 if (name == '=')
7121 {
7122 char_u *p, *s;
7123
7124 p = vim_strnsave(str, (int)len);
7125 if (p == NULL)
7126 return;
7127 if (must_append)
7128 {
7129 s = concat_str(get_expr_line_src(), p);
7130 vim_free(p);
7131 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007132 }
7133 set_expr_line(p);
7134 return;
7135 }
7136#endif
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (name == '_') /* black hole: nothing to do */
7139 return;
7140
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007141 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7142 &yank_type) == FAIL)
7143 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007145 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007147 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149#endif /* FEAT_EVAL */
7150
7151#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7152/*
7153 * Put a string into a register. When the register is not empty, the string
7154 * is appended.
7155 */
7156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007157str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007158 yankreg_T *y_ptr, /* pointer to yank register */
7159 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7160 char_u *str, /* string to put in register */
7161 long len, /* length of string */
7162 long blocklen, /* width of Visual block */
7163 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007165 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 int lnum;
7167 long start;
7168 long i;
7169 int extra;
7170 int newlines; /* number of lines added */
7171 int extraline = 0; /* extra line at the end */
7172 int append = FALSE; /* append to last line in register */
7173 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007174 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007178 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 y_ptr->y_size = 0;
7180
Bram Moolenaard44347f2011-06-19 01:14:29 +02007181 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007182 type = ((str_list || (len > 0 && (str[len - 1] == NL
7183 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007184 ? MLINE : MCHAR);
7185 else
7186 type = yank_type;
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 /*
7189 * Count the number of lines within the string
7190 */
7191 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007192 if (str_list)
7193 {
7194 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007199 for (i = 0; i < len; i++)
7200 if (str[i] == '\n')
7201 ++newlines;
7202 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7203 {
7204 extraline = 1;
7205 ++newlines; /* count extra newline at the end */
7206 }
7207 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7208 {
7209 append = TRUE;
7210 --newlines; /* uncount newline when appending first line */
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007214 /* Without any lines make the register empty. */
7215 if (y_ptr->y_size + newlines == 0)
7216 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007217 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007218 return;
7219 }
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /*
7222 * Allocate an array to hold the pointers to the new register lines.
7223 * If the register was not empty, move the existing lines to the new array.
7224 */
7225 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7226 * sizeof(char_u *), TRUE);
7227 if (pp == NULL) /* out of memory */
7228 return;
7229 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7230 pp[lnum] = y_ptr->y_array[lnum];
7231 vim_free(y_ptr->y_array);
7232 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 /*
7236 * Find the end of each line and save it into the array.
7237 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007238 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007240 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7241 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007242 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007243 pp[lnum] = vim_strnsave(*ss, i);
7244 if (i > maxlen)
7245 maxlen = i;
7246 }
7247 }
7248 else
7249 {
7250 for (start = 0; start < len + extraline; start += i + 1)
7251 {
7252 for (i = start; i < len; ++i) /* find the end of the line */
7253 if (str[i] == '\n')
7254 break;
7255 i -= start; /* i is now length of line */
7256 if (i > maxlen)
7257 maxlen = i;
7258 if (append)
7259 {
7260 --lnum;
7261 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7262 }
7263 else
7264 extra = 0;
7265 s = alloc((unsigned)(i + extra + 1));
7266 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007268 if (extra)
7269 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7270 if (append)
7271 vim_free(y_ptr->y_array[lnum]);
7272 if (i)
7273 mch_memmove(s + extra, str + start, (size_t)i);
7274 extra += i;
7275 s[extra] = NUL;
7276 y_ptr->y_array[lnum++] = s;
7277 while (--extra >= 0)
7278 {
7279 if (*s == NUL)
7280 *s = '\n'; /* replace NUL with newline */
7281 ++s;
7282 }
7283 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 y_ptr->y_type = type;
7287 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (type == MBLOCK)
7289 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7290 else
7291 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007292#ifdef FEAT_VIMINFO
7293 y_ptr->y_time_set = vim_time();
7294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295}
7296#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7297
7298 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007299clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 vim_memset(oap, 0, sizeof(oparg_T));
7302}
7303
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007304static 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 +00007305
7306/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007307 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 *
7309 * "Words" are counted by looking for boundaries between non-space and
7310 * space characters. (it seems to produce results that match 'wc'.)
7311 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007312 * Return value is byte count; word count for the line is added to "*wc".
7313 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 *
7315 * The function will only examine the first "limit" characters in the
7316 * line, stopping if it encounters an end-of-line (NUL byte). In that
7317 * case, eol_size will be added to the character count to account for
7318 * the size of the EOL character.
7319 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007320 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007321line_count_info(
7322 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007323 varnumber_T *wc,
7324 varnumber_T *cc,
7325 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007326 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007328 varnumber_T i;
7329 varnumber_T words = 0;
7330 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 int is_word = 0;
7332
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007333 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 {
7335 if (is_word)
7336 {
7337 if (vim_isspace(line[i]))
7338 {
7339 words++;
7340 is_word = 0;
7341 }
7342 }
7343 else if (!vim_isspace(line[i]))
7344 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007345 ++chars;
7346#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007347 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007348#else
7349 ++i;
7350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352
7353 if (is_word)
7354 words++;
7355 *wc += words;
7356
7357 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007358 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007361 chars += eol_size;
7362 }
7363 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 return i;
7365}
7366
7367/*
7368 * Give some info about the position of the cursor (for "g CTRL-G").
7369 * In Visual mode, give some info about the selected region. (In this case,
7370 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007371 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 */
7373 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007374cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007377 char_u buf1[50];
7378 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007380 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007381#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007382 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007383#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007384 varnumber_T byte_count_cursor = 0;
7385 varnumber_T char_count = 0;
7386 varnumber_T char_count_cursor = 0;
7387 varnumber_T word_count = 0;
7388 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007389 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007390 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 long line_count_selected = 0;
7392 pos_T min_pos, max_pos;
7393 oparg_T oparg;
7394 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 /*
7397 * Compute the length of the file in characters.
7398 */
7399 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7400 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007401 if (dict == NULL)
7402 {
7403 MSG(_(no_lines_msg));
7404 return;
7405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407 else
7408 {
7409 if (get_fileformat(curbuf) == EOL_DOS)
7410 eol_size = 2;
7411 else
7412 eol_size = 1;
7413
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (VIsual_active)
7415 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007416 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 min_pos = VIsual;
7419 max_pos = curwin->w_cursor;
7420 }
7421 else
7422 {
7423 min_pos = curwin->w_cursor;
7424 max_pos = VIsual;
7425 }
7426 if (*p_sel == 'e' && max_pos.col > 0)
7427 --max_pos.col;
7428
7429 if (VIsual_mode == Ctrl_V)
7430 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007431#ifdef FEAT_LINEBREAK
7432 char_u * saved_sbr = p_sbr;
7433
7434 /* Make 'sbr' empty for a moment to get the correct size. */
7435 p_sbr = empty_option;
7436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 oparg.is_VIsual = 1;
7438 oparg.block_mode = TRUE;
7439 oparg.op_type = OP_NOP;
7440 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007441 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007442#ifdef FEAT_LINEBREAK
7443 p_sbr = saved_sbr;
7444#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007445 if (curwin->w_curswant == MAXCOL)
7446 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /* Swap the start, end vcol if needed */
7448 if (oparg.end_vcol < oparg.start_vcol)
7449 {
7450 oparg.end_vcol += oparg.start_vcol;
7451 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7452 oparg.end_vcol -= oparg.start_vcol;
7453 }
7454 }
7455 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7459 {
7460 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007461 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 {
7463 ui_breakcheck();
7464 if (got_int)
7465 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007466 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 /* Do extra processing for VIsual mode. */
7470 if (VIsual_active
7471 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7472 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007473 char_u *s = NULL;
7474 long len = 0L;
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 switch (VIsual_mode)
7477 {
7478 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007483#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007485#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007486 s = bd.textstart;
7487 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 break;
7489 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007490 s = ml_get(lnum);
7491 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 break;
7493 case 'v':
7494 {
7495 colnr_T start_col = (lnum == min_pos.lnum)
7496 ? min_pos.col : 0;
7497 colnr_T end_col = (lnum == max_pos.lnum)
7498 ? max_pos.col - start_col + 1 : MAXCOL;
7499
Bram Moolenaardef9e822004-12-31 20:58:58 +00007500 s = ml_get(lnum) + start_col;
7501 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 break;
7504 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007505 if (s != NULL)
7506 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007507 byte_count_cursor += line_count_info(s, &word_count_cursor,
7508 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007509 if (lnum == curbuf->b_ml.ml_line_count
7510 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007511 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007512 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007513 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 {
7518 /* In non-visual mode, check for the line the cursor is on */
7519 if (lnum == curwin->w_cursor.lnum)
7520 {
7521 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007522 char_count_cursor += char_count;
7523 byte_count_cursor = byte_count +
7524 line_count_info(ml_get(lnum),
7525 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007526 (varnumber_T)(curwin->w_cursor.col + 1),
7527 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 }
7530 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007531 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007532 &char_count, (varnumber_T)MAXCOL,
7533 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
7535
7536 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007537 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007538 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
Bram Moolenaared767a22016-01-03 22:49:16 +01007540 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007542 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007544 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7545 {
7546 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7547 &max_pos.col);
7548 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7549 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7550 }
7551 else
7552 buf1[0] = NUL;
7553
7554 if (char_count_cursor == byte_count_cursor
7555 && char_count == byte_count)
7556 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007557 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007558 buf1, line_count_selected,
7559 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007560 (long long)word_count_cursor,
7561 (long long)word_count,
7562 (long long)byte_count_cursor,
7563 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007564 else
7565 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007566 _("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 +01007567 buf1, line_count_selected,
7568 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007569 (long long)word_count_cursor,
7570 (long long)word_count,
7571 (long long)char_count_cursor,
7572 (long long)char_count,
7573 (long long)byte_count_cursor,
7574 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
7576 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007577 {
7578 p = ml_get_curline();
7579 validate_virtcol();
7580 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7581 (int)curwin->w_virtcol + 1);
7582 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7583 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
Bram Moolenaared767a22016-01-03 22:49:16 +01007585 if (char_count_cursor == byte_count_cursor
7586 && char_count == byte_count)
7587 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007588 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007589 (char *)buf1, (char *)buf2,
7590 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007592 (long long)word_count_cursor, (long long)word_count,
7593 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007594 else
7595 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007596 _("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 +01007597 (char *)buf1, (char *)buf2,
7598 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007599 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007600 (long long)word_count_cursor, (long long)word_count,
7601 (long long)char_count_cursor, (long long)char_count,
7602 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007603 }
7604 }
7605
Bram Moolenaared767a22016-01-03 22:49:16 +01007606#ifdef FEAT_MBYTE
7607 bom_count = bomb_size();
7608 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007609 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007610 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007611#endif
7612 if (dict == NULL)
7613 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007614 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007615 p = p_shm;
7616 p_shm = (char_u *)"";
7617 msg(IObuff);
7618 p_shm = p;
7619 }
7620 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007621#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007622 if (dict != NULL)
7623 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007624 dict_add_nr_str(dict, "words", word_count, NULL);
7625 dict_add_nr_str(dict, "chars", char_count, NULL);
7626 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007627# ifdef FEAT_MBYTE
7628 + bom_count
7629# endif
7630 , NULL);
7631 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007632 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007633 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007634 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007635 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007636 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639}