blob: 65fc75e20b722bd081444542502bd7b259d4f75d [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 Moolenaar071d4272004-06-13 20:20:40 +0000401 int p_ts = (int)curbuf->b_p_ts;
402 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000404 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 int i = 0, j = 0;
406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#ifdef FEAT_RIGHTLEFT
408 int old_p_ri = p_ri;
409
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200410 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#endif
412
413 State = INSERT; /* don't want REPLACE for State */
414 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
415 if (bd.is_short)
416 return;
417
418 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200419 total = (int)((unsigned)amount * (unsigned)p_sw);
420 if ((total / p_sw) != amount)
421 return; /* multiplication overflow */
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 oldp = ml_get_curline();
424
425 if (!left)
426 {
427 /*
428 * 1. Get start vcol
429 * 2. Total ws vcols
430 * 3. Divvy into TABs & spp
431 * 4. Construct new string
432 */
433 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
434 ws_vcol = bd.start_vcol - bd.pre_whitesp;
435 if (bd.startspaces)
436 {
437#ifdef FEAT_MBYTE
438 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100439 {
440 if ((*mb_ptr2len)(bd.textstart) == 1)
441 ++bd.textstart;
442 else
443 {
444 ws_vcol = 0;
445 bd.startspaces = 0;
446 }
447 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000450 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100452 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200454 /* TODO: is passing bd.textstart for start of the line OK? */
455 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
456 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 total += incr;
458 bd.start_vcol += incr;
459 }
460 /* OK, now total=all the VWS reqd, and textstart points at the 1st
461 * non-ws char in the block. */
462 if (!curbuf->b_p_et)
463 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
464 if (i)
465 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
466 else
467 j = total;
468 /* if we're splitting a TAB, allow for it */
469 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
470 len = (int)STRLEN(bd.textstart) + 1;
471 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
472 if (newp == NULL)
473 return;
474 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
475 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200476 vim_memset(newp + bd.textcol, TAB, (size_t)i);
477 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 /* the end */
479 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
480 }
481 else /* left */
482 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000483 colnr_T destination_col; /* column to which text in block will
484 be shifted */
485 char_u *verbatim_copy_end; /* end of the part of the line which is
486 copied verbatim */
487 colnr_T verbatim_copy_width;/* the (displayed) width of this part
488 of line */
489 unsigned fill; /* nr of spaces that replace a TAB */
490 unsigned new_line_len; /* the length of the line after the
491 block shift */
492 size_t block_space_width;
493 size_t shift_amount;
494 char_u *non_white = bd.textstart;
495 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497 /*
498 * Firstly, let's find the first non-whitespace character that is
499 * displayed after the block's start column and the character's column
500 * number. Also, let's calculate the width of all the whitespace
501 * characters that are displayed in the block and precede the searched
502 * non-whitespace character.
503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000505 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
506 * the part of which is displayed at the block's beginning. Let's start
507 * searching from the next character. */
508 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100509 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000510
511 /* The character's column is in "bd.start_vcol". */
512 non_white_col = bd.start_vcol;
513
Bram Moolenaar1c465442017-03-12 20:10:05 +0100514 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000517 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520 block_space_width = non_white_col - oap->start_vcol;
521 /* We will shift by "total" or "block_space_width", whichever is less.
522 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000523 shift_amount = (block_space_width < (size_t)total
524 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000525
526 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000527 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000528
529 /* Now let's find out how much of the beginning of the line we can
530 * reuse without modification. */
531 verbatim_copy_end = bd.textstart;
532 verbatim_copy_width = bd.start_vcol;
533
534 /* If "bd.startspaces" is set, "bd.textstart" points to the character
535 * preceding the block. We have to subtract its width to obtain its
536 * column number. */
537 if (bd.startspaces)
538 verbatim_copy_width -= bd.start_char_vcols;
539 while (verbatim_copy_width < destination_col)
540 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200541 char_u *line = verbatim_copy_end;
542
543 /* TODO: is passing verbatim_copy_end for start of the line OK? */
544 incr = lbr_chartabsize(line, verbatim_copy_end,
545 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000546 if (verbatim_copy_width + incr > destination_col)
547 break;
548 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100549 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000550 }
551
552 /* If "destination_col" is different from the width of the initial
553 * part of the line that will be copied, it means we encountered a tab
554 * character, which we will have to partly replace with spaces. */
555 fill = destination_col - verbatim_copy_width;
556
557 /* The replacement line will consist of:
558 * - the beginning of the original line up to "verbatim_copy_end",
559 * - "fill" number of spaces,
560 * - the rest of the line, pointed to by non_white. */
561 new_line_len = (unsigned)(verbatim_copy_end - oldp)
562 + fill
563 + (unsigned)STRLEN(non_white) + 1;
564
565 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (newp == NULL)
567 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000568 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200569 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000570 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
572 /* replace the line */
573 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
574 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
575 State = oldstate;
576 curwin->w_cursor.col = oldcol;
577#ifdef FEAT_RIGHTLEFT
578 p_ri = old_p_ri;
579#endif
580}
581#endif
582
583#ifdef FEAT_VISUALEXTRA
584/*
585 * Insert string "s" (b_insert ? before : after) block :AKelly
586 * Caller must prepare for undo.
587 */
588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100589block_insert(
590 oparg_T *oap,
591 char_u *s,
592 int b_insert,
593 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 int p_ts;
596 int count = 0; /* extra spaces to replace a cut TAB */
597 int spaces = 0; /* non-zero if cutting a TAB */
598 colnr_T offset; /* pointer along new line */
599 unsigned s_len; /* STRLEN(s) */
600 char_u *newp, *oldp; /* new, old lines */
601 linenr_T lnum; /* loop var */
602 int oldstate = State;
603
604 State = INSERT; /* don't want REPLACE for State */
605 s_len = (unsigned)STRLEN(s);
606
607 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
608 {
609 block_prep(oap, bdp, lnum, TRUE);
610 if (bdp->is_short && b_insert)
611 continue; /* OP_INSERT, line ends before block start */
612
613 oldp = ml_get(lnum);
614
615 if (b_insert)
616 {
617 p_ts = bdp->start_char_vcols;
618 spaces = bdp->startspaces;
619 if (spaces != 0)
620 count = p_ts - 1; /* we're cutting a TAB */
621 offset = bdp->textcol;
622 }
623 else /* append */
624 {
625 p_ts = bdp->end_char_vcols;
626 if (!bdp->is_short) /* spaces = padding after block */
627 {
628 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
629 if (spaces != 0)
630 count = p_ts - 1; /* we're cutting a TAB */
631 offset = bdp->textcol + bdp->textlen - (spaces != 0);
632 }
633 else /* spaces = padding to block edge */
634 {
635 /* if $ used, just append to EOL (ie spaces==0) */
636 if (!bdp->is_MAX)
637 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
638 count = spaces;
639 offset = bdp->textcol + bdp->textlen;
640 }
641 }
642
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200643#ifdef FEAT_MBYTE
644 if (has_mbyte && spaces > 0)
645 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100646 int off;
647
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200648 /* Avoid starting halfway a multi-byte character. */
649 if (b_insert)
650 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100651 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200652 }
653 else
654 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100655 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200656 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200657 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100658 spaces -= off;
659 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200660 }
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
664 if (newp == NULL)
665 continue;
666
667 /* copy up to shifted part */
668 mch_memmove(newp, oldp, (size_t)(offset));
669 oldp += offset;
670
671 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200672 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 /* copy the new text */
675 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
676 offset += s_len;
677
678 if (spaces && !bdp->is_short)
679 {
680 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200681 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 /* We're splitting a TAB, don't copy it. */
683 oldp++;
684 /* We allowed for that TAB, remember this now */
685 count++;
686 }
687
688 if (spaces > 0)
689 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000690 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 ml_replace(lnum, newp, FALSE);
693
694 if (lnum == oap->end.lnum)
695 {
696 /* Set "']" mark to the end of the block instead of the end of
697 * the insert in the first line. */
698 curbuf->b_op_end.lnum = oap->end.lnum;
699 curbuf->b_op_end.col = offset;
700 }
701 } /* for all lnum */
702
703 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
704
705 State = oldstate;
706}
707#endif
708
709#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
710/*
711 * op_reindent - handle reindenting a block of lines.
712 */
713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100714op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 long i;
717 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200718 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 linenr_T first_changed = 0;
720 linenr_T last_changed = 0;
721 linenr_T start_lnum = curwin->w_cursor.lnum;
722
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000723 /* Don't even try when 'modifiable' is off. */
724 if (!curbuf->b_p_ma)
725 {
726 EMSG(_(e_modifiable));
727 return;
728 }
729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 for (i = oap->line_count; --i >= 0 && !got_int; )
731 {
732 /* it's a slow thing to do, so give feedback so there's no worry that
733 * the computer's just hung. */
734
735 if (i > 1
736 && (i % 50 == 0 || i == oap->line_count - 1)
737 && oap->line_count > p_report)
738 smsg((char_u *)_("%ld lines to indent... "), i);
739
740 /*
741 * Be vi-compatible: For lisp indenting the first line is not
742 * indented, unless there is only one line.
743 */
744#ifdef FEAT_LISP
745 if (i != oap->line_count - 1 || oap->line_count == 1
746 || how != get_lisp_indent)
747#endif
748 {
749 l = skipwhite(ml_get_curline());
750 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200751 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200753 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200755 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
757 /* did change the indent, call changed_lines() later */
758 if (first_changed == 0)
759 first_changed = curwin->w_cursor.lnum;
760 last_changed = curwin->w_cursor.lnum;
761 }
762 }
763 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000764 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766
767 /* put cursor on first non-blank of indented line */
768 curwin->w_cursor.lnum = start_lnum;
769 beginline(BL_SOL | BL_FIX);
770
771 /* Mark changed lines so that they will be redrawn. When Visual
772 * highlighting was present, need to continue until the last line. When
773 * there is no change still need to remove the Visual highlighting. */
774 if (last_changed != 0)
775 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 else if (oap->is_VIsual)
779 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 if (oap->line_count > p_report)
782 {
783 i = oap->line_count - (i + 1);
784 if (i == 1)
785 MSG(_("1 line indented "));
786 else
787 smsg((char_u *)_("%ld lines indented "), i);
788 }
789 /* set '[ and '] marks */
790 curbuf->b_op_start = oap->start;
791 curbuf->b_op_end = oap->end;
792}
793#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
794
795#if defined(FEAT_EVAL) || defined(PROTO)
796/*
797 * Keep the last expression line here, for repeating.
798 */
799static char_u *expr_line = NULL;
800
801/*
802 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
803 * Returns '=' when OK, NUL otherwise.
804 */
805 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100806get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 char_u *new_line;
809
810 new_line = getcmdline('=', 0L, 0);
811 if (new_line == NULL)
812 return NUL;
813 if (*new_line == NUL) /* use previous line */
814 vim_free(new_line);
815 else
816 set_expr_line(new_line);
817 return '=';
818}
819
820/*
821 * Set the expression for the '=' register.
822 * Argument must be an allocated string.
823 */
824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100825set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
827 vim_free(expr_line);
828 expr_line = new_line;
829}
830
831/*
832 * Get the result of the '=' register expression.
833 * Returns a pointer to allocated memory, or NULL for failure.
834 */
835 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100836get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 char_u *expr_copy;
839 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000840 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 if (expr_line == NULL)
843 return NULL;
844
845 /* Make a copy of the expression, because evaluating it may cause it to be
846 * changed. */
847 expr_copy = vim_strsave(expr_line);
848 if (expr_copy == NULL)
849 return NULL;
850
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000851 /* When we are invoked recursively limit the evaluation to 10 levels.
852 * Then return the string as-is. */
853 if (nested >= 10)
854 return expr_copy;
855
856 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000857 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000858 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 vim_free(expr_copy);
860 return rv;
861}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000862
863/*
864 * Get the '=' register expression itself, without evaluating it.
865 */
866 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000868{
869 if (expr_line == NULL)
870 return NULL;
871 return vim_strsave(expr_line);
872}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif /* FEAT_EVAL */
874
875/*
876 * Check if 'regname' is a valid name of a yank register.
877 * Note: There is no check for 0 (default register), caller should do this
878 */
879 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100880valid_yank_reg(
881 int regname,
882 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 if ( (regname > 0 && ASCII_ISALNUM(regname))
885 || (!writing && vim_strchr((char_u *)
886#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100887 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100889 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890#endif
891 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100892 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 || regname == '"'
894 || regname == '-'
895 || regname == '_'
896#ifdef FEAT_CLIPBOARD
897 || regname == '*'
898 || regname == '+'
899#endif
900#ifdef FEAT_DND
901 || (!writing && regname == '~')
902#endif
903 )
904 return TRUE;
905 return FALSE;
906}
907
908/*
909 * Set y_current and y_append, according to the value of "regname".
910 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000911 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 *
913 * If regname is 0 and writing, use register 0
914 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100915 *
916 * Return TRUE when the register should be inserted literally (selection or
917 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100920get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100923 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
925 y_append = FALSE;
926 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
927 {
928 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100929 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
931 i = regname;
932 if (VIM_ISDIGIT(i))
933 i -= '0';
934 else if (ASCII_ISLOWER(i))
935 i = CharOrdLow(i) + 10;
936 else if (ASCII_ISUPPER(i))
937 {
938 i = CharOrdUp(i) + 10;
939 y_append = TRUE;
940 }
941 else if (regname == '-')
942 i = DELETION_REGISTER;
943#ifdef FEAT_CLIPBOARD
944 /* When selection is not available, use register 0 instead of '*' */
945 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100948 ret = TRUE;
949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 /* When clipboard is not available, use register 0 instead of '+' */
951 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100954 ret = TRUE;
955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#endif
957#ifdef FEAT_DND
958 else if (!writing && regname == '~')
959 i = TILDE_REGISTER;
960#endif
961 else /* not 0-9, a-z, A-Z or '-': use register 0 */
962 i = 0;
963 y_current = &(y_regs[i]);
964 if (writing) /* remember the register we write into for do_put() */
965 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100966 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
Bram Moolenaar8299df92004-07-10 09:47:34 +0000969#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970/*
971 * When "regname" is a clipboard register, obtain the selection. If it's not
972 * available return zero, otherwise return "regname".
973 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 if (regname == '*')
978 {
979 if (!clip_star.available)
980 regname = 0;
981 else
982 clip_get_selection(&clip_star);
983 }
984 else if (regname == '+')
985 {
986 if (!clip_plus.available)
987 regname = 0;
988 else
989 clip_get_selection(&clip_plus);
990 }
991 return regname;
992}
993#endif
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Obtain the contents of a "normal" register. The register is made empty.
997 * The returned pointer has allocated memory, use put_register() later.
998 */
999 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001000get_register(
1001 int name,
1002 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001004 yankreg_T *reg;
1005 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007#ifdef FEAT_CLIPBOARD
1008 /* When Visual area changed, may have to update selection. Obtain the
1009 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +00001010 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001012 if (clip_isautosel_star())
1013 clip_update_selection(&clip_star);
1014 may_get_selection(name);
1015 }
1016 if (name == '+' && clip_plus.available)
1017 {
1018 if (clip_isautosel_plus())
1019 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 may_get_selection(name);
1021 }
1022#endif
1023
1024 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001025 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (reg != NULL)
1027 {
1028 *reg = *y_current;
1029 if (copy)
1030 {
1031 /* If we run out of memory some or all of the lines are empty. */
1032 if (reg->y_size == 0)
1033 reg->y_array = NULL;
1034 else
1035 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1036 * reg->y_size));
1037 if (reg->y_array != NULL)
1038 {
1039 for (i = 0; i < reg->y_size; ++i)
1040 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1041 }
1042 }
1043 else
1044 y_current->y_array = NULL;
1045 }
1046 return (void *)reg;
1047}
1048
1049/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001050 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
1052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001053put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 get_yank_register(name, 0);
1056 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001057 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001058 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001060#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 /* Send text written to clipboard register to the clipboard. */
1062 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001065
1066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001067free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001068{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001069 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001070
1071 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001072 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001073 free_yank_all();
1074 vim_free(reg);
1075 *y_current = tmp;
1076}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078#if defined(FEAT_MOUSE) || defined(PROTO)
1079/*
1080 * return TRUE if the current yank register has type MLINE
1081 */
1082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001083yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
1085 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1086 return FALSE;
1087 if (regname == '_') /* black hole is always empty */
1088 return FALSE;
1089 get_yank_register(regname, FALSE);
1090 return (y_current->y_type == MLINE);
1091}
1092#endif
1093
1094/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001095 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001097 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
1099 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001100do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
Bram Moolenaard55de222007-05-06 13:38:48 +00001102 char_u *p;
1103 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001104 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001105 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001107 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001109 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1111 retval = FAIL;
1112 else
1113 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001114 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 showmode();
1116 regname = c;
1117 retval = OK;
1118 }
1119 }
1120 else /* stop recording */
1121 {
1122 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001123 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1124 * needs to be removed again to put it in a register. exec_reg then
1125 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001127 reg_recording = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 MSG("");
1129 p = get_recorded();
1130 if (p == NULL)
1131 retval = FAIL;
1132 else
1133 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001134 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1135 vim_unescape_csi(p);
1136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /*
1138 * We don't want to change the default register here, so save and
1139 * restore the current register name.
1140 */
1141 old_y_previous = y_previous;
1142 old_y_current = y_current;
1143
1144 retval = stuff_yank(regname, p);
1145
1146 y_previous = old_y_previous;
1147 y_current = old_y_current;
1148 }
1149 }
1150 return retval;
1151}
1152
1153/*
1154 * Stuff string "p" into yank register "regname" as a single line (append if
1155 * uppercase). "p" must have been alloced.
1156 *
1157 * return FAIL for failure, OK otherwise
1158 */
1159 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001160stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 char_u *lp;
1163 char_u **pp;
1164
1165 /* check for read-only register */
1166 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1167 {
1168 vim_free(p);
1169 return FAIL;
1170 }
1171 if (regname == '_') /* black hole: don't do anything */
1172 {
1173 vim_free(p);
1174 return OK;
1175 }
1176 get_yank_register(regname, TRUE);
1177 if (y_append && y_current->y_array != NULL)
1178 {
1179 pp = &(y_current->y_array[y_current->y_size - 1]);
1180 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1181 if (lp == NULL)
1182 {
1183 vim_free(p);
1184 return FAIL;
1185 }
1186 STRCPY(lp, *pp);
1187 STRCAT(lp, p);
1188 vim_free(p);
1189 vim_free(*pp);
1190 *pp = lp;
1191 }
1192 else
1193 {
1194 free_yank_all();
1195 if ((y_current->y_array =
1196 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1197 {
1198 vim_free(p);
1199 return FAIL;
1200 }
1201 y_current->y_array[0] = p;
1202 y_current->y_size = 1;
1203 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001204#ifdef FEAT_VIMINFO
1205 y_current->y_time_set = vim_time();
1206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 return OK;
1209}
1210
Bram Moolenaar42b94362009-05-26 16:12:37 +00001211static int execreg_lastc = NUL;
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001214 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001216 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001219do_execreg(
1220 int regname,
1221 int colon, /* insert ':' before each line */
1222 int addcr, /* always add '\n' to end of line */
1223 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 long i;
1226 char_u *p;
1227 int retval = OK;
1228 int remap;
1229
1230 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001231 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001232 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001233 {
1234 EMSG(_("E748: No previously used register"));
1235 return FAIL;
1236 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001237 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 /* check for valid regname */
1240 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001241 {
1242 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001244 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001245 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247#ifdef FEAT_CLIPBOARD
1248 regname = may_get_selection(regname);
1249#endif
1250
1251 if (regname == '_') /* black hole: don't stuff anything */
1252 return OK;
1253
1254#ifdef FEAT_CMDHIST
1255 if (regname == ':') /* use last command line */
1256 {
1257 if (last_cmdline == NULL)
1258 {
1259 EMSG(_(e_nolastcmd));
1260 return FAIL;
1261 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001262 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001263 /* Escape all control characters with a CTRL-V */
1264 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001265 (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 +00001266 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001267 {
1268 /* When in Visual mode "'<,'>" will be prepended to the command.
1269 * Remove it when it's already there. */
1270 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001272 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001273 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001274 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001275 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277#endif
1278#ifdef FEAT_EVAL
1279 else if (regname == '=')
1280 {
1281 p = get_expr_line();
1282 if (p == NULL)
1283 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001284 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(p);
1286 }
1287#endif
1288 else if (regname == '.') /* use last inserted text */
1289 {
1290 p = get_last_insert_save();
1291 if (p == NULL)
1292 {
1293 EMSG(_(e_noinstext));
1294 return FAIL;
1295 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001296 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 vim_free(p);
1298 }
1299 else
1300 {
1301 get_yank_register(regname, FALSE);
1302 if (y_current->y_array == NULL)
1303 return FAIL;
1304
1305 /* Disallow remaping for ":@r". */
1306 remap = colon ? REMAP_NONE : REMAP_YES;
1307
1308 /*
1309 * Insert lines into typeahead buffer, from last one to first one.
1310 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001311 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 for (i = y_current->y_size; --i >= 0; )
1313 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001314 char_u *escaped;
1315
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 /* insert NL between lines and after last line if type is MLINE */
1317 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1318 || addcr)
1319 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001320 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 return FAIL;
1322 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001323 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1324 if (escaped == NULL)
1325 return FAIL;
1326 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1327 vim_free(escaped);
1328 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001330 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 == FAIL)
1332 return FAIL;
1333 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001334 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 return retval;
1337}
1338
1339/*
1340 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1341 * used only after other typeahead has been processed.
1342 */
1343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001344put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
1346 char_u buf[3];
1347
1348 if (restart_edit != NUL)
1349 {
1350 if (restart_edit == 'V')
1351 {
1352 buf[0] = 'g';
1353 buf[1] = 'R';
1354 buf[2] = NUL;
1355 }
1356 else
1357 {
1358 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1359 buf[1] = NUL;
1360 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001361 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 restart_edit = NUL;
1363 }
1364}
1365
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001366/*
1367 * Insert register contents "s" into the typeahead buffer, so that it will be
1368 * executed again.
1369 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1370 * no remapping.
1371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001373put_in_typebuf(
1374 char_u *s,
1375 int esc,
1376 int colon, /* add ':' before the line */
1377 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
1379 int retval = OK;
1380
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001381 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001383 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001385 {
1386 char_u *p;
1387
1388 if (esc)
1389 p = vim_strsave_escape_csi(s);
1390 else
1391 p = s;
1392 if (p == NULL)
1393 retval = FAIL;
1394 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001395 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1396 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001397 if (esc)
1398 vim_free(p);
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001401 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return retval;
1403}
1404
1405/*
1406 * Insert a yank register: copy it into the Read buffer.
1407 * Used by CTRL-R command and middle mouse button in insert mode.
1408 *
1409 * return FAIL for failure, OK otherwise
1410 */
1411 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412insert_reg(
1413 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001414 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 long i;
1417 int retval = OK;
1418 char_u *arg;
1419 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001420 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 /*
1423 * It is possible to get into an endless loop by having CTRL-R a in
1424 * register a and then, in insert mode, doing CTRL-R a.
1425 * If you hit CTRL-C, the loop will be broken here.
1426 */
1427 ui_breakcheck();
1428 if (got_int)
1429 return FAIL;
1430
1431 /* check for valid regname */
1432 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1433 return FAIL;
1434
1435#ifdef FEAT_CLIPBOARD
1436 regname = may_get_selection(regname);
1437#endif
1438
1439 if (regname == '.') /* insert last inserted text */
1440 retval = stuff_inserted(NUL, 1L, TRUE);
1441 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1442 {
1443 if (arg == NULL)
1444 return FAIL;
1445 stuffescaped(arg, literally);
1446 if (allocated)
1447 vim_free(arg);
1448 }
1449 else /* name or number register */
1450 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001451 if (get_yank_register(regname, FALSE))
1452 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (y_current->y_array == NULL)
1454 retval = FAIL;
1455 else
1456 {
1457 for (i = 0; i < y_current->y_size; ++i)
1458 {
1459 stuffescaped(y_current->y_array[i], literally);
1460 /*
1461 * Insert a newline between lines and after last line if
1462 * y_type is MLINE.
1463 */
1464 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1465 stuffcharReadbuff('\n');
1466 }
1467 }
1468 }
1469
1470 return retval;
1471}
1472
1473/*
1474 * Stuff a string into the typeahead buffer, such that edit() will insert it
1475 * literally ("literally" TRUE) or interpret is as typed characters.
1476 */
1477 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001478stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
1480 int c;
1481 char_u *start;
1482
1483 while (*arg != NUL)
1484 {
1485 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1486 * stuff K_SPECIAL to get the effect of a special key when "literally"
1487 * is TRUE. */
1488 start = arg;
1489 while ((*arg >= ' '
1490#ifndef EBCDIC
1491 && *arg < DEL /* EBCDIC: chars above space are normal */
1492#endif
1493 )
1494 || (*arg == K_SPECIAL && !literally))
1495 ++arg;
1496 if (arg > start)
1497 stuffReadbuffLen(start, (long)(arg - start));
1498
1499 /* stuff a single special character */
1500 if (*arg != NUL)
1501 {
1502#ifdef FEAT_MBYTE
1503 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001504 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 else
1506#endif
1507 c = *arg++;
1508 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1509 stuffcharReadbuff(Ctrl_V);
1510 stuffcharReadbuff(c);
1511 }
1512 }
1513}
1514
1515/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001516 * If "regname" is a special register, return TRUE and store a pointer to its
1517 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001519 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001520get_spec_reg(
1521 int regname,
1522 char_u **argp,
1523 int *allocated, /* return: TRUE when value was allocated */
1524 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 int cnt;
1527
1528 *argp = NULL;
1529 *allocated = FALSE;
1530 switch (regname)
1531 {
1532 case '%': /* file name */
1533 if (errmsg)
1534 check_fname(); /* will give emsg if not set */
1535 *argp = curbuf->b_fname;
1536 return TRUE;
1537
1538 case '#': /* alternate file name */
1539 *argp = getaltfname(errmsg); /* may give emsg if not set */
1540 return TRUE;
1541
1542#ifdef FEAT_EVAL
1543 case '=': /* result of expression */
1544 *argp = get_expr_line();
1545 *allocated = TRUE;
1546 return TRUE;
1547#endif
1548
1549 case ':': /* last command line */
1550 if (last_cmdline == NULL && errmsg)
1551 EMSG(_(e_nolastcmd));
1552 *argp = last_cmdline;
1553 return TRUE;
1554
1555 case '/': /* last search-pattern */
1556 if (last_search_pat() == NULL && errmsg)
1557 EMSG(_(e_noprevre));
1558 *argp = last_search_pat();
1559 return TRUE;
1560
1561 case '.': /* last inserted text */
1562 *argp = get_last_insert_save();
1563 *allocated = TRUE;
1564 if (*argp == NULL && errmsg)
1565 EMSG(_(e_noinstext));
1566 return TRUE;
1567
1568#ifdef FEAT_SEARCHPATH
1569 case Ctrl_F: /* Filename under cursor */
1570 case Ctrl_P: /* Path under cursor, expand via "path" */
1571 if (!errmsg)
1572 return FALSE;
1573 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001574 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 *allocated = TRUE;
1576 return TRUE;
1577#endif
1578
1579 case Ctrl_W: /* word under cursor */
1580 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1581 if (!errmsg)
1582 return FALSE;
1583 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1584 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1585 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1586 *allocated = TRUE;
1587 return TRUE;
1588
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001589 case Ctrl_L: /* Line under cursor */
1590 if (!errmsg)
1591 return FALSE;
1592
1593 *argp = ml_get_buf(curwin->w_buffer,
1594 curwin->w_cursor.lnum, FALSE);
1595 return TRUE;
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 case '_': /* black hole: always empty */
1598 *argp = (char_u *)"";
1599 return TRUE;
1600 }
1601
1602 return FALSE;
1603}
1604
1605/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001606 * Paste a yank register into the command line.
1607 * Only for non-special registers.
1608 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 * insert_reg() can't be used here, because special characters from the
1610 * register contents will be interpreted as commands.
1611 *
1612 * return FAIL for failure, OK otherwise
1613 */
1614 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001615cmdline_paste_reg(
1616 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001617 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001618 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001621 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001623 if (get_yank_register(regname, FALSE))
1624 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (y_current->y_array == NULL)
1626 return FAIL;
1627
1628 for (i = 0; i < y_current->y_size; ++i)
1629 {
1630 cmdline_paste_str(y_current->y_array[i], literally);
1631
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001632 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001633 * Don't do this when "remcr" is TRUE. */
1634 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 cmdline_paste_str((char_u *)"\r", literally);
1636
1637 /* Check for CTRL-C, in case someone tries to paste a few thousand
1638 * lines and gets bored. */
1639 ui_breakcheck();
1640 if (got_int)
1641 return FAIL;
1642 }
1643 return OK;
1644}
1645
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1647/*
1648 * Adjust the register name pointed to with "rp" for the clipboard being
1649 * used always and the clipboard being available.
1650 */
1651 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001652adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001654 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1655 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001656 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1657 {
1658 if (clip_unnamed != 0)
1659 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001660 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001661 else
1662 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1663 ? '+' : '*';
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (!clip_star.available && *rp == '*')
1666 *rp = 0;
1667 if (!clip_plus.available && *rp == '+')
1668 *rp = 0;
1669}
1670#endif
1671
1672/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001673 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1674 */
1675 void
1676shift_delete_registers()
1677{
1678 int n;
1679
1680 y_current = &y_regs[9];
1681 free_yank_all(); /* free register nine */
1682 for (n = 9; n > 1; --n)
1683 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001684 y_current = &y_regs[1];
1685 if (!y_append)
1686 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001687 y_regs[1].y_array = NULL; /* set register one to empty */
1688}
1689
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001690#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001691 static void
1692yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1693{
1694 static int recursive = FALSE;
1695 dict_T *v_event;
1696 list_T *list;
1697 int n;
1698 char_u buf[NUMBUFLEN + 2];
1699 long reglen = 0;
1700
1701 if (recursive)
1702 return;
1703
1704 v_event = get_vim_var_dict(VV_EVENT);
1705
1706 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001707 if (list == NULL)
1708 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001709 for (n = 0; n < reg->y_size; n++)
1710 list_append_string(list, reg->y_array[n], -1);
1711 list->lv_lock = VAR_FIXED;
1712 dict_add_list(v_event, "regcontents", list);
1713
1714 buf[0] = (char_u)oap->regname;
1715 buf[1] = NUL;
1716 dict_add_nr_str(v_event, "regname", 0, buf);
1717
1718 buf[0] = get_op_char(oap->op_type);
1719 buf[1] = get_extra_op_char(oap->op_type);
1720 buf[2] = NUL;
1721 dict_add_nr_str(v_event, "operator", 0, buf);
1722
1723 buf[0] = NUL;
1724 buf[1] = NUL;
1725 switch (get_reg_type(oap->regname, &reglen))
1726 {
1727 case MLINE: buf[0] = 'V'; break;
1728 case MCHAR: buf[0] = 'v'; break;
1729 case MBLOCK:
1730 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1731 reglen + 1);
1732 break;
1733 }
1734 dict_add_nr_str(v_event, "regtype", 0, buf);
1735
1736 /* Lock the dictionary and its keys */
1737 dict_set_items_ro(v_event);
1738
1739 recursive = TRUE;
1740 textlock++;
1741 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1742 textlock--;
1743 recursive = FALSE;
1744
1745 /* Empty the dictionary, v:event is still valid */
1746 dict_free_contents(v_event);
1747 hash_init(&v_event->dv_hashtab);
1748}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001749#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001750
Bram Moolenaara1891842017-02-04 21:34:31 +01001751/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001752 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001754 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 */
1756 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001757op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 int n;
1760 linenr_T lnum;
1761 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 char_u *newp, *oldp;
1763 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1765 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001766 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1769 return OK;
1770
1771 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1772 if (oap->empty)
1773 return u_save_cursor();
1774
1775 if (!curbuf->b_p_ma)
1776 {
1777 EMSG(_(e_modifiable));
1778 return FAIL;
1779 }
1780
1781#ifdef FEAT_CLIPBOARD
1782 adjust_clip_reg(&oap->regname);
1783#endif
1784
1785#ifdef FEAT_MBYTE
1786 if (has_mbyte)
1787 mb_adjust_opend(oap);
1788#endif
1789
Bram Moolenaard04b7502010-07-08 22:27:55 +02001790 /*
1791 * Imitate the strange Vi behaviour: If the delete spans more than one
1792 * line and motion_type == MCHAR and the result is a blank line, make the
1793 * delete linewise. Don't do this for the change command or Visual mode.
1794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001797 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001799 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 && oap->op_type == OP_DELETE)
1801 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001802 ptr = ml_get(oap->end.lnum) + oap->end.col;
1803 if (*ptr != NUL)
1804 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 ptr = skipwhite(ptr);
1806 if (*ptr == NUL && inindent(0))
1807 oap->motion_type = MLINE;
1808 }
1809
Bram Moolenaard04b7502010-07-08 22:27:55 +02001810 /*
1811 * Check for trying to delete (e.g. "D") in an empty line.
1812 * Note: For the change operator it is ok.
1813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if ( oap->motion_type == MCHAR
1815 && oap->line_count == 1
1816 && oap->op_type == OP_DELETE
1817 && *ml_get(oap->start.lnum) == NUL)
1818 {
1819 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001820 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 * 'cpoptions' (Vi compatible).
1822 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001823#ifdef FEAT_VIRTUALEDIT
1824 if (virtual_op)
1825 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1826 * marks as if it happened. */
1827 goto setmarks;
1828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1830 beep_flush();
1831 return OK;
1832 }
1833
Bram Moolenaard04b7502010-07-08 22:27:55 +02001834 /*
1835 * Do a yank of whatever we're about to delete.
1836 * If a yank register was specified, put the deleted text into that
1837 * register. For the black hole register '_' don't yank anything.
1838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (oap->regname != '_')
1840 {
1841 if (oap->regname != 0)
1842 {
1843 /* check for read-only register */
1844 if (!valid_yank_reg(oap->regname, TRUE))
1845 {
1846 beep_flush();
1847 return OK;
1848 }
1849 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1850 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1851 did_yank = TRUE;
1852 }
1853
1854 /*
1855 * Put deleted text into register 1 and shift number registers if the
1856 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001857 * Use the register name from before adjust_clip_reg() may have
1858 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001860 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 || oap->line_count > 1 || oap->use_reg_one)
1862 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001863 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (op_yank(oap, TRUE, FALSE) == OK)
1865 did_yank = TRUE;
1866 }
1867
Bram Moolenaar84298db2012-04-20 13:46:08 +02001868 /* Yank into small delete register when no named register specified
1869 * and the delete is within one line. */
1870 if ((
1871#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001872 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1873 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001874#endif
1875 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 && oap->line_count == 1)
1877 {
1878 oap->regname = '-';
1879 get_yank_register(oap->regname, TRUE);
1880 if (op_yank(oap, TRUE, FALSE) == OK)
1881 did_yank = TRUE;
1882 oap->regname = 0;
1883 }
1884
1885 /*
1886 * If there's too much stuff to fit in the yank register, then get a
1887 * confirmation before doing the delete. This is crude, but simple.
1888 * And it avoids doing a delete of something we can't put back if we
1889 * want.
1890 */
1891 if (!did_yank)
1892 {
1893 int msg_silent_save = msg_silent;
1894
1895 msg_silent = 0; /* must display the prompt */
1896 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1897 msg_silent = msg_silent_save;
1898 if (n != 'y')
1899 {
1900 EMSG(_(e_abort));
1901 return FAIL;
1902 }
1903 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001904
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001905#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001906 if (did_yank && has_textyankpost())
1907 yank_do_autocmd(oap, y_current);
1908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910
Bram Moolenaard04b7502010-07-08 22:27:55 +02001911 /*
1912 * block mode delete
1913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (oap->block_mode)
1915 {
1916 if (u_save((linenr_T)(oap->start.lnum - 1),
1917 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1918 return FAIL;
1919
1920 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1921 {
1922 block_prep(oap, &bd, lnum, TRUE);
1923 if (bd.textlen == 0) /* nothing to delete */
1924 continue;
1925
1926 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1927 if (lnum == curwin->w_cursor.lnum)
1928 {
1929 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1930# ifdef FEAT_VIRTUALEDIT
1931 curwin->w_cursor.coladd = 0;
1932# endif
1933 }
1934
1935 /* n == number of chars deleted
1936 * If we delete a TAB, it may be replaced by several characters.
1937 * Thus the number of characters may increase!
1938 */
1939 n = bd.textlen - bd.startspaces - bd.endspaces;
1940 oldp = ml_get(lnum);
1941 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1942 if (newp == NULL)
1943 continue;
1944 /* copy up to deleted part */
1945 mch_memmove(newp, oldp, (size_t)bd.textcol);
1946 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001947 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 (size_t)(bd.startspaces + bd.endspaces));
1949 /* copy the part after the deleted part */
1950 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001951 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 /* replace the line */
1953 ml_replace(lnum, newp, FALSE);
1954 }
1955
1956 check_cursor_col();
1957 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1958 oap->end.lnum + 1, 0L);
1959 oap->line_count = 0; /* no lines deleted */
1960 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001961 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 if (oap->op_type == OP_CHANGE)
1964 {
1965 /* Delete the lines except the first one. Temporarily move the
1966 * cursor to the next line. Save the current line number, if the
1967 * last line is deleted it may be changed.
1968 */
1969 if (oap->line_count > 1)
1970 {
1971 lnum = curwin->w_cursor.lnum;
1972 ++curwin->w_cursor.lnum;
1973 del_lines((long)(oap->line_count - 1), TRUE);
1974 curwin->w_cursor.lnum = lnum;
1975 }
1976 if (u_save_cursor() == FAIL)
1977 return FAIL;
1978 if (curbuf->b_p_ai) /* don't delete indent */
1979 {
1980 beginline(BL_WHITE); /* cursor on first non-white */
1981 did_ai = TRUE; /* delete the indent when ESC hit */
1982 ai_col = curwin->w_cursor.col;
1983 }
1984 else
1985 beginline(0); /* cursor in column 0 */
1986 truncate_line(FALSE); /* delete the rest of the line */
1987 /* leave cursor past last char in line */
1988 if (oap->line_count > 1)
1989 u_clearline(); /* "U" command not possible after "2cc" */
1990 }
1991 else
1992 {
1993 del_lines(oap->line_count, TRUE);
1994 beginline(BL_WHITE | BL_FIX);
1995 u_clearline(); /* "U" command not possible after "dd" */
1996 }
1997 }
1998 else
1999 {
2000#ifdef FEAT_VIRTUALEDIT
2001 if (virtual_op)
2002 {
2003 int endcol = 0;
2004
2005 /* For virtualedit: break the tabs that are partly included. */
2006 if (gchar_pos(&oap->start) == '\t')
2007 {
2008 if (u_save_cursor() == FAIL) /* save first line for undo */
2009 return FAIL;
2010 if (oap->line_count == 1)
2011 endcol = getviscol2(oap->end.col, oap->end.coladd);
2012 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2013 oap->start = curwin->w_cursor;
2014 if (oap->line_count == 1)
2015 {
2016 coladvance(endcol);
2017 oap->end.col = curwin->w_cursor.col;
2018 oap->end.coladd = curwin->w_cursor.coladd;
2019 curwin->w_cursor = oap->start;
2020 }
2021 }
2022
2023 /* Break a tab only when it's included in the area. */
2024 if (gchar_pos(&oap->end) == '\t'
2025 && (int)oap->end.coladd < oap->inclusive)
2026 {
2027 /* save last line for undo */
2028 if (u_save((linenr_T)(oap->end.lnum - 1),
2029 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2030 return FAIL;
2031 curwin->w_cursor = oap->end;
2032 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2033 oap->end = curwin->w_cursor;
2034 curwin->w_cursor = oap->start;
2035 }
2036 }
2037#endif
2038
2039 if (oap->line_count == 1) /* delete characters within one line */
2040 {
2041 if (u_save_cursor() == FAIL) /* save line for undo */
2042 return FAIL;
2043
2044 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002045 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 && oap->op_type == OP_CHANGE
2047 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002048 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 display_dollar(oap->end.col - !oap->inclusive);
2050
2051 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2052
2053#ifdef FEAT_VIRTUALEDIT
2054 if (virtual_op)
2055 {
2056 /* fix up things for virtualedit-delete:
2057 * break the tabs which are going to get in our way
2058 */
2059 char_u *curline = ml_get_curline();
2060 int len = (int)STRLEN(curline);
2061
2062 if (oap->end.coladd != 0
2063 && (int)oap->end.col >= len - 1
2064 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2065 n++;
2066 /* Delete at least one char (e.g, when on a control char). */
2067 if (n == 0 && oap->start.coladd != oap->end.coladd)
2068 n = 1;
2069
2070 /* When deleted a char in the line, reset coladd. */
2071 if (gchar_cursor() != NUL)
2072 curwin->w_cursor.coladd = 0;
2073 }
2074#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002075 (void)del_bytes((long)n, !virtual_op,
2076 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078 else /* delete characters between lines */
2079 {
2080 pos_T curpos;
2081
2082 /* save deleted and changed lines for undo */
2083 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2084 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2085 return FAIL;
2086
2087 truncate_line(TRUE); /* delete from cursor to end of line */
2088
2089 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2090 ++curwin->w_cursor.lnum;
2091 del_lines((long)(oap->line_count - 2), FALSE);
2092
Bram Moolenaard009e862015-06-09 20:20:03 +02002093 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002094 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002095 curwin->w_cursor.col = 0;
2096 (void)del_bytes((long)n, !virtual_op,
2097 oap->op_type == OP_DELETE && !oap->is_VIsual);
2098 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2099 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 }
2102
2103 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2104
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002105#ifdef FEAT_VIRTUALEDIT
2106setmarks:
2107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (oap->block_mode)
2109 {
2110 curbuf->b_op_end.lnum = oap->end.lnum;
2111 curbuf->b_op_end.col = oap->start.col;
2112 }
2113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 curbuf->b_op_end = oap->start;
2115 curbuf->b_op_start = oap->start;
2116
2117 return OK;
2118}
2119
2120#ifdef FEAT_MBYTE
2121/*
2122 * Adjust end of operating area for ending on a multi-byte character.
2123 * Used for deletion.
2124 */
2125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002126mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 char_u *p;
2129
2130 if (oap->inclusive)
2131 {
2132 p = ml_get(oap->end.lnum);
2133 oap->end.col += mb_tail_off(p, p + oap->end.col);
2134 }
2135}
2136#endif
2137
2138#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2139/*
2140 * Replace a whole area with one character.
2141 */
2142 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002143op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144{
2145 int n, numc;
2146#ifdef FEAT_MBYTE
2147 int num_chars;
2148#endif
2149 char_u *newp, *oldp;
2150 size_t oldlen;
2151 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002152 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002153 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2156 return OK; /* nothing to do */
2157
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002158 if (c == REPLACE_CR_NCHAR)
2159 {
2160 had_ctrl_v_cr = TRUE;
2161 c = CAR;
2162 }
2163 else if (c == REPLACE_NL_NCHAR)
2164 {
2165 had_ctrl_v_cr = TRUE;
2166 c = NL;
2167 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_MBYTE
2170 if (has_mbyte)
2171 mb_adjust_opend(oap);
2172#endif
2173
2174 if (u_save((linenr_T)(oap->start.lnum - 1),
2175 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2176 return FAIL;
2177
2178 /*
2179 * block mode replace
2180 */
2181 if (oap->block_mode)
2182 {
2183 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2184 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2185 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002186 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2188 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2189 continue; /* nothing to replace */
2190
2191 /* n == number of extra chars required
2192 * If we split a TAB, it may be replaced by several characters.
2193 * Thus the number of characters may increase!
2194 */
2195#ifdef FEAT_VIRTUALEDIT
2196 /* If the range starts in virtual space, count the initial
2197 * coladd offset as part of "startspaces" */
2198 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2199 {
2200 pos_T vpos;
2201
Bram Moolenaara1381de2009-11-03 15:44:21 +00002202 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 getvpos(&vpos, oap->start_vcol);
2204 bd.startspaces += vpos.coladd;
2205 n = bd.startspaces;
2206 }
2207 else
2208#endif
2209 /* allow for pre spaces */
2210 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2211
2212 /* allow for post spp */
2213 n += (bd.endspaces
2214#ifdef FEAT_VIRTUALEDIT
2215 && !bd.is_oneChar
2216#endif
2217 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2218 /* Figure out how many characters to replace. */
2219 numc = oap->end_vcol - oap->start_vcol + 1;
2220 if (bd.is_short && (!virtual_op || bd.is_MAX))
2221 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2222
2223#ifdef FEAT_MBYTE
2224 /* A double-wide character can be replaced only up to half the
2225 * times. */
2226 if ((*mb_char2cells)(c) > 1)
2227 {
2228 if ((numc & 1) && !bd.is_short)
2229 {
2230 ++bd.endspaces;
2231 ++n;
2232 }
2233 numc = numc / 2;
2234 }
2235
2236 /* Compute bytes needed, move character count to num_chars. */
2237 num_chars = numc;
2238 numc *= (*mb_char2len)(c);
2239#endif
2240 /* oldlen includes textlen, so don't double count */
2241 n += numc - bd.textlen;
2242
2243 oldp = ml_get_curline();
2244 oldlen = STRLEN(oldp);
2245 newp = alloc_check((unsigned)oldlen + 1 + n);
2246 if (newp == NULL)
2247 continue;
2248 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2249 /* copy up to deleted part */
2250 mch_memmove(newp, oldp, (size_t)bd.textcol);
2251 oldp += bd.textcol + bd.textlen;
2252 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002253 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002255 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2256 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002257 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002259#ifdef FEAT_MBYTE
2260 if (has_mbyte)
2261 {
2262 n = (int)STRLEN(newp);
2263 while (--num_chars >= 0)
2264 n += (*mb_char2bytes)(c, newp + n);
2265 }
2266 else
2267#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002268 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002269 if (!bd.is_short)
2270 {
2271 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002272 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002273 /* copy the part after the changed part */
2274 STRMOVE(newp + STRLEN(newp), oldp);
2275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002279 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002280 after_p = alloc_check(
2281 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002282 if (after_p != NULL)
2283 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285 /* replace the line */
2286 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002287 if (after_p != NULL)
2288 {
2289 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2290 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2291 oap->end.lnum++;
2292 vim_free(after_p);
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295 }
2296 else
2297 {
2298 /*
2299 * MCHAR and MLINE motion replace.
2300 */
2301 if (oap->motion_type == MLINE)
2302 {
2303 oap->start.col = 0;
2304 curwin->w_cursor.col = 0;
2305 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2306 if (oap->end.col)
2307 --oap->end.col;
2308 }
2309 else if (!oap->inclusive)
2310 dec(&(oap->end));
2311
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002312 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 n = gchar_cursor();
2315 if (n != NUL)
2316 {
2317#ifdef FEAT_MBYTE
2318 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2319 {
2320 /* This is slow, but it handles replacing a single-byte
2321 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002322 if (curwin->w_cursor.lnum == oap->end.lnum)
2323 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 n = State;
2325 State = REPLACE;
2326 ins_char(c);
2327 State = n;
2328 /* Backup to the replaced character. */
2329 dec_cursor();
2330 }
2331 else
2332#endif
2333 {
2334#ifdef FEAT_VIRTUALEDIT
2335 if (n == TAB)
2336 {
2337 int end_vcol = 0;
2338
2339 if (curwin->w_cursor.lnum == oap->end.lnum)
2340 {
2341 /* oap->end has to be recalculated when
2342 * the tab breaks */
2343 end_vcol = getviscol2(oap->end.col,
2344 oap->end.coladd);
2345 }
2346 coladvance_force(getviscol());
2347 if (curwin->w_cursor.lnum == oap->end.lnum)
2348 getvpos(&oap->end, end_vcol);
2349 }
2350#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002351 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 }
2354#ifdef FEAT_VIRTUALEDIT
2355 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2356 {
2357 int virtcols = oap->end.coladd;
2358
2359 if (curwin->w_cursor.lnum == oap->start.lnum
2360 && oap->start.col == oap->end.col && oap->start.coladd)
2361 virtcols -= oap->start.coladd;
2362
2363 /* oap->end has been trimmed so it's effectively inclusive;
2364 * as a result an extra +1 must be counted so we don't
2365 * trample the NUL byte. */
2366 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2367 curwin->w_cursor.col -= (virtcols + 1);
2368 for (; virtcols >= 0; virtcols--)
2369 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002370 PCHAR(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (inc(&curwin->w_cursor) == -1)
2372 break;
2373 }
2374 }
2375#endif
2376
2377 /* Advance to next character, stop at the end of the file. */
2378 if (inc_cursor() == -1)
2379 break;
2380 }
2381 }
2382
2383 curwin->w_cursor = oap->start;
2384 check_cursor();
2385 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2386
2387 /* Set "'[" and "']" marks. */
2388 curbuf->b_op_start = oap->start;
2389 curbuf->b_op_end = oap->end;
2390
2391 return OK;
2392}
2393#endif
2394
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002395static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397/*
2398 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2399 */
2400 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002401op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002405 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 if (u_save((linenr_T)(oap->start.lnum - 1),
2408 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2409 return;
2410
2411 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (oap->block_mode) /* Visual block mode */
2413 {
2414 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2415 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002416 int one_change;
2417
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 block_prep(oap, &bd, pos.lnum, FALSE);
2419 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002420 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2421 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002422
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002423#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002424 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
2426 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2427
Bram Moolenaar009b2592004-10-24 19:18:58 +00002428 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2429 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002431 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 if (did_change)
2436 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2437 }
2438 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 if (oap->motion_type == MLINE)
2441 {
2442 oap->start.col = 0;
2443 pos.col = 0;
2444 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2445 if (oap->end.col)
2446 --oap->end.col;
2447 }
2448 else if (!oap->inclusive)
2449 dec(&(oap->end));
2450
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002451 if (pos.lnum == oap->end.lnum)
2452 did_change = swapchars(oap->op_type, &pos,
2453 oap->end.col - pos.col + 1);
2454 else
2455 for (;;)
2456 {
2457 did_change |= swapchars(oap->op_type, &pos,
2458 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2459 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002460 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002461 break;
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (did_change)
2464 {
2465 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2466 0L);
2467#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002468 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 char_u *ptr;
2471 int count;
2472
2473 pos = oap->start;
2474 while (pos.lnum < oap->end.lnum)
2475 {
2476 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002477 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002478 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002480 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 pos.col = 0;
2482 pos.lnum++;
2483 }
2484 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2485 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002486 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002488 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490#endif
2491 }
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 if (!did_change && oap->is_VIsual)
2495 /* No change: need to remove the Visual selection */
2496 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 /*
2499 * Set '[ and '] marks.
2500 */
2501 curbuf->b_op_start = oap->start;
2502 curbuf->b_op_end = oap->end;
2503
2504 if (oap->line_count > p_report)
2505 {
2506 if (oap->line_count == 1)
2507 MSG(_("1 line changed"));
2508 else
2509 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2510 }
2511}
2512
2513/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002514 * Invoke swapchar() on "length" bytes at position "pos".
2515 * "pos" is advanced to just after the changed characters.
2516 * "length" is rounded up to include the whole last multi-byte character.
2517 * Also works correctly when the number of bytes changes.
2518 * Returns TRUE if some character was changed.
2519 */
2520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002522{
2523 int todo;
2524 int did_change = 0;
2525
2526 for (todo = length; todo > 0; --todo)
2527 {
2528# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002529 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002530 {
2531 int len = (*mb_ptr2len)(ml_get_pos(pos));
2532
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002533 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002534 if (len > 0)
2535 todo -= len - 1;
2536 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002537# endif
2538 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002539 if (inc(pos) == -1) /* at end of file */
2540 break;
2541 }
2542 return did_change;
2543}
2544
2545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * If op_type == OP_UPPER: make uppercase,
2547 * if op_type == OP_LOWER: make lowercase,
2548 * if op_type == OP_ROT13: do rot13 encoding,
2549 * else swap case of character at 'pos'
2550 * returns TRUE when something actually changed.
2551 */
2552 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002553swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int c;
2556 int nc;
2557
2558 c = gchar_pos(pos);
2559
2560 /* Only do rot13 encoding for ASCII characters. */
2561 if (c >= 0x80 && op_type == OP_ROT13)
2562 return FALSE;
2563
2564#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002565 if (op_type == OP_UPPER && c == 0xdf
2566 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002567 {
2568 pos_T sp = curwin->w_cursor;
2569
2570 /* Special handling of German sharp s: change to "SS". */
2571 curwin->w_cursor = *pos;
2572 del_char(FALSE);
2573 ins_char('S');
2574 ins_char('S');
2575 curwin->w_cursor = sp;
2576 inc(pos);
2577 }
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2580 return FALSE;
2581#endif
2582 nc = c;
2583 if (MB_ISLOWER(c))
2584 {
2585 if (op_type == OP_ROT13)
2586 nc = ROT13(c, 'a');
2587 else if (op_type != OP_LOWER)
2588 nc = MB_TOUPPER(c);
2589 }
2590 else if (MB_ISUPPER(c))
2591 {
2592 if (op_type == OP_ROT13)
2593 nc = ROT13(c, 'A');
2594 else if (op_type != OP_UPPER)
2595 nc = MB_TOLOWER(c);
2596 }
2597 if (nc != c)
2598 {
2599#ifdef FEAT_MBYTE
2600 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2601 {
2602 pos_T sp = curwin->w_cursor;
2603
2604 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002605 /* don't use del_char(), it also removes composing chars */
2606 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 ins_char(nc);
2608 curwin->w_cursor = sp;
2609 }
2610 else
2611#endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002612 PCHAR(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return TRUE;
2614 }
2615 return FALSE;
2616}
2617
2618#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2619/*
2620 * op_insert - Insert and append operators for Visual mode.
2621 */
2622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 long ins_len, pre_textlen = 0;
2626 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002627 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 struct block_def bd;
2629 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002630 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632 /* edit() changes this - record it for OP_APPEND */
2633 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2634
2635 /* vis block is still marked. Get rid of it now. */
2636 curwin->w_cursor.lnum = oap->start.lnum;
2637 update_screen(INVERTED);
2638
2639 if (oap->block_mode)
2640 {
2641#ifdef FEAT_VIRTUALEDIT
2642 /* When 'virtualedit' is used, need to insert the extra spaces before
2643 * doing block_prep(). When only "block" is used, virtual edit is
2644 * already disabled, but still need it when calling
2645 * coladvance_force(). */
2646 if (curwin->w_cursor.coladd > 0)
2647 {
2648 int old_ve_flags = ve_flags;
2649
2650 ve_flags = VE_ALL;
2651 if (u_save_cursor() == FAIL)
2652 return;
2653 coladvance_force(oap->op_type == OP_APPEND
2654 ? oap->end_vcol + 1 : getviscol());
2655 if (oap->op_type == OP_APPEND)
2656 --curwin->w_cursor.col;
2657 ve_flags = old_ve_flags;
2658 }
2659#endif
2660 /* Get the info about the block before entering the text */
2661 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002662 /* Get indent information */
2663 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002665
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (oap->op_type == OP_APPEND)
2667 firstline += bd.textlen;
2668 pre_textlen = (long)STRLEN(firstline);
2669 }
2670
2671 if (oap->op_type == OP_APPEND)
2672 {
2673 if (oap->block_mode
2674#ifdef FEAT_VIRTUALEDIT
2675 && curwin->w_cursor.coladd == 0
2676#endif
2677 )
2678 {
2679 /* Move the cursor to the character right of the block. */
2680 curwin->w_set_curswant = TRUE;
2681 while (*ml_get_cursor() != NUL
2682 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2683 ++curwin->w_cursor.col;
2684 if (bd.is_short && !bd.is_MAX)
2685 {
2686 /* First line was too short, make it longer and adjust the
2687 * values in "bd". */
2688 if (u_save_cursor() == FAIL)
2689 return;
2690 for (i = 0; i < bd.endspaces; ++i)
2691 ins_char(' ');
2692 bd.textlen += bd.endspaces;
2693 }
2694 }
2695 else
2696 {
2697 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002698 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002701 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 && oap->start_vcol != oap->end_vcol)
2703 inc_cursor();
2704 }
2705 }
2706
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002707 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002708 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002710 /* When a tab was inserted, and the characters in front of the tab
2711 * have been converted to a tab as well, the column of the cursor
2712 * might have actually been reduced, so need to adjust here. */
2713 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002714 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002715 oap->start = curbuf->b_op_start_orig;
2716
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002717 /* If user has moved off this line, we don't know what to do, so do
2718 * nothing.
2719 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2720 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return;
2722
2723 if (oap->block_mode)
2724 {
2725 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002726 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002727 size_t len;
2728 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002730 /* If indent kicked in, the firstline might have changed
2731 * but only do that, if the indent actually increased. */
2732 ind_post = (colnr_T)getwhitecols_curline();
2733 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2734 {
2735 bd.textcol += ind_post - ind_pre;
2736 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002737 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002738 }
2739
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002740 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002741 * to adjust the block for that. But only do it, if the difference
2742 * does not come from indent kicking in. */
2743 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2744 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002745 {
2746 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002747 && oap->start.col
2748#ifdef FEAT_VIRTUALEDIT
2749 + oap->start.coladd
2750#endif
2751 != curbuf->b_op_start_orig.col
2752#ifdef FEAT_VIRTUALEDIT
2753 + curbuf->b_op_start_orig.coladd
2754#endif
2755 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002756 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002757 int t = getviscol2(curbuf->b_op_start_orig.col,
2758 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002759 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002760 pre_textlen -= t - oap->start_vcol;
2761 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002762 }
2763 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002764 && oap->end.col
2765#ifdef FEAT_VIRTUALEDIT
2766 + oap->end.coladd
2767#endif
2768 >= curbuf->b_op_start_orig.col
2769#ifdef FEAT_VIRTUALEDIT
2770 + curbuf->b_op_start_orig.coladd
2771#endif
2772 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002773 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002774 int t = getviscol2(curbuf->b_op_start_orig.col,
2775 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002776 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002777 /* reset pre_textlen to the value of OP_INSERT */
2778 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002779 pre_textlen -= t - oap->start_vcol;
2780 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002781 oap->op_type = OP_INSERT;
2782 }
2783 }
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 /*
2786 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002787 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * Don't do this when "$" used, end-of-line will have changed.
2789 */
2790 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2791 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2792 {
2793 if (oap->op_type == OP_APPEND)
2794 {
2795 pre_textlen += bd2.textlen - bd.textlen;
2796 if (bd2.endspaces)
2797 --bd2.textlen;
2798 }
2799 bd.textcol = bd2.textcol;
2800 bd.textlen = bd2.textlen;
2801 }
2802
2803 /*
2804 * Subsequent calls to ml_get() flush the firstline data - take a
2805 * copy of the required string.
2806 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002807 firstline = ml_get(oap->start.lnum);
2808 len = STRLEN(firstline);
2809 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002811 add += bd.textlen;
2812 if ((size_t)add > len)
2813 firstline += len; // short line, point to the NUL
2814 else
2815 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002816 if (pre_textlen >= 0
2817 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
2819 ins_text = vim_strnsave(firstline, (int)ins_len);
2820 if (ins_text != NULL)
2821 {
2822 /* block handled here */
2823 if (u_save(oap->start.lnum,
2824 (linenr_T)(oap->end.lnum + 1)) == OK)
2825 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2826 &bd);
2827
2828 curwin->w_cursor.col = oap->start.col;
2829 check_cursor();
2830 vim_free(ins_text);
2831 }
2832 }
2833 }
2834}
2835#endif
2836
2837/*
2838 * op_change - handle a change operation
2839 *
2840 * return TRUE if edit() returns because of a CTRL-O command
2841 */
2842 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002843op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 colnr_T l;
2846 int retval;
2847#ifdef FEAT_VISUALEXTRA
2848 long offset;
2849 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002850 long ins_len;
2851 long pre_textlen = 0;
2852 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 char_u *firstline;
2854 char_u *ins_text, *newp, *oldp;
2855 struct block_def bd;
2856#endif
2857
2858 l = oap->start.col;
2859 if (oap->motion_type == MLINE)
2860 {
2861 l = 0;
2862#ifdef FEAT_SMARTINDENT
2863 if (!p_paste && curbuf->b_p_si
2864# ifdef FEAT_CINDENT
2865 && !curbuf->b_p_cin
2866# endif
2867 )
2868 can_si = TRUE; /* It's like opening a new line, do si */
2869#endif
2870 }
2871
2872 /* First delete the text in the region. In an empty buffer only need to
2873 * save for undo */
2874 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2875 {
2876 if (u_save_cursor() == FAIL)
2877 return FALSE;
2878 }
2879 else if (op_delete(oap) == FAIL)
2880 return FALSE;
2881
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002882 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 && !virtual_op)
2884 inc_cursor();
2885
2886#ifdef FEAT_VISUALEXTRA
2887 /* check for still on same line (<CR> in inserted text meaningless) */
2888 /* skip blank lines too */
2889 if (oap->block_mode)
2890 {
2891# ifdef FEAT_VIRTUALEDIT
2892 /* Add spaces before getting the current line length. */
2893 if (virtual_op && (curwin->w_cursor.coladd > 0
2894 || gchar_cursor() == NUL))
2895 coladvance_force(getviscol());
2896# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002897 firstline = ml_get(oap->start.lnum);
2898 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002899 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 bd.textcol = curwin->w_cursor.col;
2901 }
2902#endif
2903
2904#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2905 if (oap->motion_type == MLINE)
2906 fix_indent();
2907#endif
2908
2909 retval = edit(NUL, FALSE, (linenr_T)1);
2910
2911#ifdef FEAT_VISUALEXTRA
2912 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002913 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002915 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002917 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002919 /* Auto-indenting may have changed the indent. If the cursor was past
2920 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002922 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002924 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002925
2926 pre_textlen += new_indent - pre_indent;
2927 bd.textcol += new_indent - pre_indent;
2928 }
2929
2930 ins_len = (long)STRLEN(firstline) - pre_textlen;
2931 if (ins_len > 0)
2932 {
2933 /* Subsequent calls to ml_get() flush the firstline data - take a
2934 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2936 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002937 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2939 linenr++)
2940 {
2941 block_prep(oap, &bd, linenr, TRUE);
2942 if (!bd.is_short || virtual_op)
2943 {
2944# ifdef FEAT_VIRTUALEDIT
2945 pos_T vpos;
2946
2947 /* If the block starts in virtual space, count the
2948 * initial coladd offset as part of "startspaces" */
2949 if (bd.is_short)
2950 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002951 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
2954 else
2955 vpos.coladd = 0;
2956# endif
2957 oldp = ml_get(linenr);
2958 newp = alloc_check((unsigned)(STRLEN(oldp)
2959# ifdef FEAT_VIRTUALEDIT
2960 + vpos.coladd
2961# endif
2962 + ins_len + 1));
2963 if (newp == NULL)
2964 continue;
2965 /* copy up to block start */
2966 mch_memmove(newp, oldp, (size_t)bd.textcol);
2967 offset = bd.textcol;
2968# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002969 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 offset += vpos.coladd;
2971# endif
2972 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2973 offset += ins_len;
2974 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002975 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 ml_replace(linenr, newp, FALSE);
2977 }
2978 }
2979 check_cursor();
2980
2981 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2982 }
2983 vim_free(ins_text);
2984 }
2985 }
2986#endif
2987
2988 return retval;
2989}
2990
2991/*
2992 * set all the yank registers to empty (called from main())
2993 */
2994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002995init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 int i;
2998
2999 for (i = 0; i < NUM_REGISTERS; ++i)
3000 y_regs[i].y_array = NULL;
3001}
3002
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003003#if defined(EXITFREE) || defined(PROTO)
3004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003005clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003006{
3007 int i;
3008
3009 for (i = 0; i < NUM_REGISTERS; ++i)
3010 {
3011 y_current = &y_regs[i];
3012 if (y_current->y_array != NULL)
3013 free_yank_all();
3014 }
3015}
3016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Free "n" lines from the current yank register.
3020 * Called for normal freeing and in case of error.
3021 */
3022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 if (y_current->y_array != NULL)
3026 {
3027 long i;
3028
3029 for (i = n; --i >= 0; )
3030 {
3031#ifdef AMIGA /* only for very slow machines */
3032 if ((i & 1023) == 1023) /* this may take a while */
3033 {
3034 /*
3035 * This message should never cause a hit-return message.
3036 * Overwrite this message with any next message.
3037 */
3038 ++no_wait_return;
3039 smsg((char_u *)_("freeing %ld lines"), i + 1);
3040 --no_wait_return;
3041 msg_didout = FALSE;
3042 msg_col = 0;
3043 }
3044#endif
3045 vim_free(y_current->y_array[i]);
3046 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003047 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef AMIGA
3049 if (n >= 1000)
3050 MSG("");
3051#endif
3052 }
3053}
3054
3055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003056free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 free_yank(y_current->y_size);
3059}
3060
3061/*
3062 * Yank the text between "oap->start" and "oap->end" into a yank register.
3063 * If we are to append (uppercase register), we first yank into a new yank
3064 * register and then concatenate the old and the new one (so we keep the old
3065 * one in case of out-of-memory).
3066 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003067 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 */
3069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003073 yankreg_T *curr; /* copy of y_current */
3074 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 char_u **new_ptr;
3076 linenr_T lnum; /* current line number */
3077 long j;
3078 int yanktype = oap->motion_type;
3079 long yanklines = oap->line_count;
3080 linenr_T yankendlnum = oap->end.lnum;
3081 char_u *p;
3082 char_u *pnew;
3083 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003084#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003085 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 /* check for read-only register */
3089 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3090 {
3091 beep_flush();
3092 return FAIL;
3093 }
3094 if (oap->regname == '_') /* black hole: nothing to do */
3095 return OK;
3096
3097#ifdef FEAT_CLIPBOARD
3098 if (!clip_star.available && oap->regname == '*')
3099 oap->regname = 0;
3100 else if (!clip_plus.available && oap->regname == '+')
3101 oap->regname = 0;
3102#endif
3103
3104 if (!deleting) /* op_delete() already set y_current */
3105 get_yank_register(oap->regname, TRUE);
3106
3107 curr = y_current;
3108 /* append to existing contents */
3109 if (y_append && y_current->y_array != NULL)
3110 y_current = &newreg;
3111 else
3112 free_yank_all(); /* free previously yanked lines */
3113
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003114 /*
3115 * If the cursor was in column 1 before and after the movement, and the
3116 * operator is not inclusive, the yank is always linewise.
3117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if ( oap->motion_type == MCHAR
3119 && oap->start.col == 0
3120 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003122 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 && oap->end.col == 0
3124 && yanklines > 1)
3125 {
3126 yanktype = MLINE;
3127 --yankendlnum;
3128 --yanklines;
3129 }
3130
3131 y_current->y_size = yanklines;
3132 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3135 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (y_current->y_array == NULL)
3137 {
3138 y_current = curr;
3139 return FAIL;
3140 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003141#ifdef FEAT_VIMINFO
3142 y_current->y_time_set = vim_time();
3143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 y_idx = 0;
3146 lnum = oap->start.lnum;
3147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 if (oap->block_mode)
3149 {
3150 /* Visual block mode */
3151 y_current->y_type = MBLOCK; /* set the yank register type */
3152 y_current->y_width = oap->end_vcol - oap->start_vcol;
3153
3154 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3155 y_current->y_width--;
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3159 {
3160 switch (y_current->y_type)
3161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 case MBLOCK:
3163 block_prep(oap, &bd, lnum, FALSE);
3164 if (yank_copy_line(&bd, y_idx) == FAIL)
3165 goto fail;
3166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 case MLINE:
3169 if ((y_current->y_array[y_idx] =
3170 vim_strsave(ml_get(lnum))) == NULL)
3171 goto fail;
3172 break;
3173
3174 case MCHAR:
3175 {
3176 colnr_T startcol = 0, endcol = MAXCOL;
3177#ifdef FEAT_VIRTUALEDIT
3178 int is_oneChar = FALSE;
3179 colnr_T cs, ce;
3180#endif
3181 p = ml_get(lnum);
3182 bd.startspaces = 0;
3183 bd.endspaces = 0;
3184
3185 if (lnum == oap->start.lnum)
3186 {
3187 startcol = oap->start.col;
3188#ifdef FEAT_VIRTUALEDIT
3189 if (virtual_op)
3190 {
3191 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3192 if (ce != cs && oap->start.coladd > 0)
3193 {
3194 /* Part of a tab selected -- but don't
3195 * double-count it. */
3196 bd.startspaces = (ce - cs + 1)
3197 - oap->start.coladd;
3198 startcol++;
3199 }
3200 }
3201#endif
3202 }
3203
3204 if (lnum == oap->end.lnum)
3205 {
3206 endcol = oap->end.col;
3207#ifdef FEAT_VIRTUALEDIT
3208 if (virtual_op)
3209 {
3210 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3211 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3212# ifdef FEAT_MBYTE
3213 /* Don't add space for double-wide
3214 * char; endcol will be on last byte
3215 * of multi-byte char. */
3216 && (*mb_head_off)(p, p + endcol) == 0
3217# endif
3218 ))
3219 {
3220 if (oap->start.lnum == oap->end.lnum
3221 && oap->start.col == oap->end.col)
3222 {
3223 /* Special case: inside a single char */
3224 is_oneChar = TRUE;
3225 bd.startspaces = oap->end.coladd
3226 - oap->start.coladd + oap->inclusive;
3227 endcol = startcol;
3228 }
3229 else
3230 {
3231 bd.endspaces = oap->end.coladd
3232 + oap->inclusive;
3233 endcol -= oap->inclusive;
3234 }
3235 }
3236 }
3237#endif
3238 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003239 if (endcol == MAXCOL)
3240 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (startcol > endcol
3242#ifdef FEAT_VIRTUALEDIT
3243 || is_oneChar
3244#endif
3245 )
3246 bd.textlen = 0;
3247 else
3248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 bd.textlen = endcol - startcol + oap->inclusive;
3250 }
3251 bd.textstart = p + startcol;
3252 if (yank_copy_line(&bd, y_idx) == FAIL)
3253 goto fail;
3254 break;
3255 }
3256 /* NOTREACHED */
3257 }
3258 }
3259
3260 if (curr != y_current) /* append the new block to the old block */
3261 {
3262 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3263 (curr->y_size + y_current->y_size)), TRUE);
3264 if (new_ptr == NULL)
3265 goto fail;
3266 for (j = 0; j < curr->y_size; ++j)
3267 new_ptr[j] = curr->y_array[j];
3268 vim_free(curr->y_array);
3269 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003270#ifdef FEAT_VIMINFO
3271 curr->y_time_set = vim_time();
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3275 curr->y_type = MLINE;
3276
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003277 /* Concatenate the last line of the old block with the first line of
3278 * the new block, unless being Vi compatible. */
3279 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3282 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3283 if (pnew == NULL)
3284 {
3285 y_idx = y_current->y_size - 1;
3286 goto fail;
3287 }
3288 STRCPY(pnew, curr->y_array[--j]);
3289 STRCAT(pnew, y_current->y_array[0]);
3290 vim_free(curr->y_array[j]);
3291 vim_free(y_current->y_array[0]);
3292 curr->y_array[j++] = pnew;
3293 y_idx = 1;
3294 }
3295 else
3296 y_idx = 0;
3297 while (y_idx < y_current->y_size)
3298 curr->y_array[j++] = y_current->y_array[y_idx++];
3299 curr->y_size = j;
3300 vim_free(y_current->y_array);
3301 y_current = curr;
3302 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003303 if (curwin->w_p_rnu)
3304 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (mess) /* Display message about yank? */
3306 {
3307 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 && yanklines == 1)
3310 yanklines = 0;
3311 /* Some versions of Vi use ">=" here, some don't... */
3312 if (yanklines > p_report)
3313 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003314 char namebuf[100];
3315
3316 if (oap->regname == NUL)
3317 *namebuf = NUL;
3318 else
3319 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003320 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 /* redisplay now, so message is not deleted */
3323 update_topline_redraw();
3324 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003325 {
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003326 if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003327 smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003328 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003329 smsg((char_u *)_("1 line yanked%s"), namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003330 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003331 else if (oap->block_mode)
Bram Moolenaare45deb72017-07-16 17:56:16 +02003332 smsg((char_u *)_("block of %ld lines yanked%s"),
3333 yanklines, namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
Bram Moolenaare45deb72017-07-16 17:56:16 +02003335 smsg((char_u *)_("%ld lines yanked%s"), yanklines,
3336 namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
3339
3340 /*
3341 * Set "'[" and "']" marks.
3342 */
3343 curbuf->b_op_start = oap->start;
3344 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003345 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003346 {
3347 curbuf->b_op_start.col = 0;
3348 curbuf->b_op_end.col = MAXCOL;
3349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351#ifdef FEAT_CLIPBOARD
3352 /*
3353 * If we were yanking to the '*' register, send result to clipboard.
3354 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3355 * to the '*' register.
3356 */
3357 if (clip_star.available
3358 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003359 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003360 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 {
3362 if (curr != &(y_regs[STAR_REGISTER]))
3363 /* Copy the text from register 0 to the clipboard register. */
3364 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3365
3366 clip_own_selection(&clip_star);
3367 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003368# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003369 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
3372
3373# ifdef FEAT_X11
3374 /*
3375 * If we were yanking to the '+' register, send result to selection.
3376 * Also copy to the '*' register, in case auto-select is off.
3377 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003378 if (clip_plus.available
3379 && (curr == &(y_regs[PLUS_REGISTER])
3380 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003381 && ((clip_unnamed | clip_unnamed_saved) &
3382 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003384 if (curr != &(y_regs[PLUS_REGISTER]))
3385 /* Copy the text from register 0 to the clipboard register. */
3386 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 clip_own_selection(&clip_plus);
3389 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003390 if (!clip_isautosel_star() && !clip_isautosel_plus()
3391 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 {
3393 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3394 clip_own_selection(&clip_star);
3395 clip_gen_set_selection(&clip_star);
3396 }
3397 }
3398# endif
3399#endif
3400
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003401#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003402 if (!deleting && has_textyankpost())
3403 yank_do_autocmd(oap, y_current);
3404#endif
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 return OK;
3407
3408fail: /* free the allocated lines */
3409 free_yank(y_idx + 1);
3410 y_current = curr;
3411 return FAIL;
3412}
3413
3414 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003415yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 char_u *pnew;
3418
3419 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3420 == NULL)
3421 return FAIL;
3422 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003423 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 pnew += bd->startspaces;
3425 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3426 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003427 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 pnew += bd->endspaces;
3429 *pnew = NUL;
3430 return OK;
3431}
3432
3433#ifdef FEAT_CLIPBOARD
3434/*
3435 * Make a copy of the y_current register to register "reg".
3436 */
3437 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003438copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003440 yankreg_T *curr = y_current;
3441 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 y_current = reg;
3444 free_yank_all();
3445 *y_current = *curr;
3446 y_current->y_array = (char_u **)lalloc_clear(
3447 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3448 if (y_current->y_array == NULL)
3449 y_current->y_size = 0;
3450 else
3451 for (j = 0; j < y_current->y_size; ++j)
3452 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3453 {
3454 free_yank(j);
3455 y_current->y_size = 0;
3456 break;
3457 }
3458 y_current = curr;
3459}
3460#endif
3461
3462/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003463 * Put contents of register "regname" into the text.
3464 * Caller must check "regname" to be valid!
3465 * "flags": PUT_FIXINDENT make indent look nice
3466 * PUT_CURSEND leave cursor after end of new text
3467 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
3469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003470do_put(
3471 int regname,
3472 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3473 long count,
3474 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
3476 char_u *ptr;
3477 char_u *newp, *oldp;
3478 int yanklen;
3479 int totlen = 0; /* init for gcc */
3480 linenr_T lnum;
3481 colnr_T col;
3482 long i; /* index in y_array[] */
3483 int y_type;
3484 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int oldlen;
3486 long y_width = 0;
3487 colnr_T vcol;
3488 int delcount;
3489 int incr = 0;
3490 long j;
3491 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 char_u **y_array = NULL;
3493 long nr_lines = 0;
3494 pos_T new_cursor;
3495 int indent;
3496 int orig_indent = 0; /* init for gcc */
3497 int indent_diff = 0; /* init for gcc */
3498 int first_indent = TRUE;
3499 int lendiff = 0;
3500 pos_T old_pos;
3501 char_u *insert_string = NULL;
3502 int allocated = FALSE;
3503 long cnt;
3504
3505#ifdef FEAT_CLIPBOARD
3506 /* Adjust register name for "unnamed" in 'clipboard'. */
3507 adjust_clip_reg(&regname);
3508 (void)may_get_selection(regname);
3509#endif
3510
3511 if (flags & PUT_FIXINDENT)
3512 orig_indent = get_indent();
3513
3514 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3515 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3516
3517 /*
3518 * Using inserted text works differently, because the register includes
3519 * special characters (newlines, etc.).
3520 */
3521 if (regname == '.')
3522 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003523 if (VIsual_active)
3524 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3526 (count == -1 ? 'O' : 'i')), count, FALSE);
3527 /* Putting the text is done later, so can't really move the cursor to
3528 * the next character. Use "l" to simulate it. */
3529 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3530 stuffcharReadbuff('l');
3531 return;
3532 }
3533
3534 /*
3535 * For special registers '%' (file name), '#' (alternate file name) and
3536 * ':' (last command line), etc. we have to create a fake yank register.
3537 */
3538 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3539 {
3540 if (insert_string == NULL)
3541 return;
3542 }
3543
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003544 /* Autocommands may be executed when saving lines for undo, which may make
3545 * y_array invalid. Start undo now to avoid that. */
3546 u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (insert_string != NULL)
3549 {
3550 y_type = MCHAR;
3551#ifdef FEAT_EVAL
3552 if (regname == '=')
3553 {
3554 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003555 * characters.
3556 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 for (;;)
3558 {
3559 y_size = 0;
3560 ptr = insert_string;
3561 while (ptr != NULL)
3562 {
3563 if (y_array != NULL)
3564 y_array[y_size] = ptr;
3565 ++y_size;
3566 ptr = vim_strchr(ptr, '\n');
3567 if (ptr != NULL)
3568 {
3569 if (y_array != NULL)
3570 *ptr = NUL;
3571 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003572 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (*ptr == NUL)
3574 {
3575 y_type = MLINE;
3576 break;
3577 }
3578 }
3579 }
3580 if (y_array != NULL)
3581 break;
3582 y_array = (char_u **)alloc((unsigned)
3583 (y_size * sizeof(char_u *)));
3584 if (y_array == NULL)
3585 goto end;
3586 }
3587 }
3588 else
3589#endif
3590 {
3591 y_size = 1; /* use fake one-line yank register */
3592 y_array = &insert_string;
3593 }
3594 }
3595 else
3596 {
3597 get_yank_register(regname, FALSE);
3598
3599 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 y_size = y_current->y_size;
3602 y_array = y_current->y_array;
3603 }
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (y_type == MLINE)
3606 {
3607 if (flags & PUT_LINE_SPLIT)
3608 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003609 char_u *p;
3610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 /* "p" or "P" in Visual mode: split the lines to put the text in
3612 * between. */
3613 if (u_save_cursor() == FAIL)
3614 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003615 p = ml_get_cursor();
3616 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003617 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003618 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (ptr == NULL)
3620 goto end;
3621 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3622 vim_free(ptr);
3623
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003624 oldp = ml_get_curline();
3625 p = oldp + curwin->w_cursor.col;
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_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (ptr == NULL)
3630 goto end;
3631 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3632 ++nr_lines;
3633 dir = FORWARD;
3634 }
3635 if (flags & PUT_LINE_FORWARD)
3636 {
3637 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003638 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 dir = FORWARD;
3640 }
3641 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3642 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3646 y_type = MLINE;
3647
3648 if (y_size == 0 || y_array == NULL)
3649 {
3650 EMSG2(_("E353: Nothing in register %s"),
3651 regname == 0 ? (char_u *)"\"" : transchar(regname));
3652 goto end;
3653 }
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (y_type == MBLOCK)
3656 {
3657 lnum = curwin->w_cursor.lnum + y_size + 1;
3658 if (lnum > curbuf->b_ml.ml_line_count)
3659 lnum = curbuf->b_ml.ml_line_count + 1;
3660 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3661 goto end;
3662 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003663 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 lnum = curwin->w_cursor.lnum;
3666#ifdef FEAT_FOLDING
3667 /* Correct line number for closed fold. Don't move the cursor yet,
3668 * u_save() uses it. */
3669 if (dir == BACKWARD)
3670 (void)hasFolding(lnum, &lnum, NULL);
3671 else
3672 (void)hasFolding(lnum, NULL, &lnum);
3673#endif
3674 if (dir == FORWARD)
3675 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003676 /* In an empty buffer the empty line is going to be replaced, include
3677 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003678 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 goto end;
3680#ifdef FEAT_FOLDING
3681 if (dir == FORWARD)
3682 curwin->w_cursor.lnum = lnum - 1;
3683 else
3684 curwin->w_cursor.lnum = lnum;
3685 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3686#endif
3687 }
3688 else if (u_save_cursor() == FAIL)
3689 goto end;
3690
3691 yanklen = (int)STRLEN(y_array[0]);
3692
3693#ifdef FEAT_VIRTUALEDIT
3694 if (ve_flags == VE_ALL && y_type == MCHAR)
3695 {
3696 if (gchar_cursor() == TAB)
3697 {
3698 /* Don't need to insert spaces when "p" on the last position of a
3699 * tab or "P" on the first position. */
3700 if (dir == FORWARD
3701 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3702 : curwin->w_cursor.coladd > 0)
3703 coladvance_force(getviscol());
3704 else
3705 curwin->w_cursor.coladd = 0;
3706 }
3707 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3708 coladvance_force(getviscol() + (dir == FORWARD));
3709 }
3710#endif
3711
3712 lnum = curwin->w_cursor.lnum;
3713 col = curwin->w_cursor.col;
3714
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 /*
3716 * Block mode
3717 */
3718 if (y_type == MBLOCK)
3719 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003720 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 colnr_T endcol2 = 0;
3722
3723 if (dir == FORWARD && c != NUL)
3724 {
3725#ifdef FEAT_VIRTUALEDIT
3726 if (ve_flags == VE_ALL)
3727 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3728 else
3729#endif
3730 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3731
3732#ifdef FEAT_MBYTE
3733 if (has_mbyte)
3734 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003735 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 else
3737#endif
3738#ifdef FEAT_VIRTUALEDIT
3739 if (c != TAB || ve_flags != VE_ALL)
3740#endif
3741 ++curwin->w_cursor.col;
3742 ++col;
3743 }
3744 else
3745 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3746
3747#ifdef FEAT_VIRTUALEDIT
3748 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003749 if (ve_flags == VE_ALL
3750 && (curwin->w_cursor.coladd > 0
3751 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 if (dir == FORWARD && c == NUL)
3754 ++col;
3755 if (dir != FORWARD && c != NUL)
3756 ++curwin->w_cursor.col;
3757 if (c == TAB)
3758 {
3759 if (dir == BACKWARD && curwin->w_cursor.col)
3760 curwin->w_cursor.col--;
3761 if (dir == FORWARD && col - 1 == endcol2)
3762 curwin->w_cursor.col++;
3763 }
3764 }
3765 curwin->w_cursor.coladd = 0;
3766#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003767 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 for (i = 0; i < y_size; ++i)
3769 {
3770 int spaces;
3771 char shortline;
3772
3773 bd.startspaces = 0;
3774 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 vcol = 0;
3776 delcount = 0;
3777
3778 /* add a new line */
3779 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3780 {
3781 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3782 (colnr_T)1, FALSE) == FAIL)
3783 break;
3784 ++nr_lines;
3785 }
3786 /* get the old line and advance to the position to insert at */
3787 oldp = ml_get_curline();
3788 oldlen = (int)STRLEN(oldp);
3789 for (ptr = oldp; vcol < col && *ptr; )
3790 {
3791 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003792 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 vcol += incr;
3794 }
3795 bd.textcol = (colnr_T)(ptr - oldp);
3796
3797 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3798
3799 if (vcol < col) /* line too short, padd with spaces */
3800 bd.startspaces = col - vcol;
3801 else if (vcol > col)
3802 {
3803 bd.endspaces = vcol - col;
3804 bd.startspaces = incr - bd.endspaces;
3805 --bd.textcol;
3806 delcount = 1;
3807#ifdef FEAT_MBYTE
3808 if (has_mbyte)
3809 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3810#endif
3811 if (oldp[bd.textcol] != TAB)
3812 {
3813 /* Only a Tab can be split into spaces. Other
3814 * characters will have to be moved to after the
3815 * block, causing misalignment. */
3816 delcount = 0;
3817 bd.endspaces = 0;
3818 }
3819 }
3820
3821 yanklen = (int)STRLEN(y_array[i]);
3822
3823 /* calculate number of spaces required to fill right side of block*/
3824 spaces = y_width + 1;
3825 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003826 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (spaces < 0)
3828 spaces = 0;
3829
3830 /* insert the new text */
3831 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3832 newp = alloc_check((unsigned)totlen + oldlen + 1);
3833 if (newp == NULL)
3834 break;
3835 /* copy part up to cursor to new line */
3836 ptr = newp;
3837 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3838 ptr += bd.textcol;
3839 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003840 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 ptr += bd.startspaces;
3842 /* insert the new text */
3843 for (j = 0; j < count; ++j)
3844 {
3845 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3846 ptr += yanklen;
3847
3848 /* insert block's trailing spaces only if there's text behind */
3849 if ((j < count - 1 || !shortline) && spaces)
3850 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003851 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 ptr += spaces;
3853 }
3854 }
3855 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003856 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 ptr += bd.endspaces;
3858 /* move the text after the cursor to the end of the line. */
3859 mch_memmove(ptr, oldp + bd.textcol + delcount,
3860 (size_t)(oldlen - bd.textcol - delcount + 1));
3861 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3862
3863 ++curwin->w_cursor.lnum;
3864 if (i == 0)
3865 curwin->w_cursor.col += bd.startspaces;
3866 }
3867
3868 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3869
3870 /* Set '[ mark. */
3871 curbuf->b_op_start = curwin->w_cursor;
3872 curbuf->b_op_start.lnum = lnum;
3873
3874 /* adjust '] mark */
3875 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3876 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003877# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (flags & PUT_CURSEND)
3881 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003882 colnr_T len;
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 curwin->w_cursor = curbuf->b_op_end;
3885 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003886
3887 /* in Insert mode we might be after the NUL, correct for that */
3888 len = (colnr_T)STRLEN(ml_get_curline());
3889 if (curwin->w_cursor.col > len)
3890 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892 else
3893 curwin->w_cursor.lnum = lnum;
3894 }
3895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
3897 /*
3898 * Character or Line mode
3899 */
3900 if (y_type == MCHAR)
3901 {
3902 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3903 * char */
3904 if (dir == FORWARD && gchar_cursor() != NUL)
3905 {
3906#ifdef FEAT_MBYTE
3907 if (has_mbyte)
3908 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003909 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 /* put it on the next of the multi-byte character. */
3912 col += bytelen;
3913 if (yanklen)
3914 {
3915 curwin->w_cursor.col += bytelen;
3916 curbuf->b_op_end.col += bytelen;
3917 }
3918 }
3919 else
3920#endif
3921 {
3922 ++col;
3923 if (yanklen)
3924 {
3925 ++curwin->w_cursor.col;
3926 ++curbuf->b_op_end.col;
3927 }
3928 }
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 curbuf->b_op_start = curwin->w_cursor;
3931 }
3932 /*
3933 * Line mode: BACKWARD is the same as FORWARD on the previous line
3934 */
3935 else if (dir == BACKWARD)
3936 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003937 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 /*
3940 * simple case: insert into current line
3941 */
3942 if (y_type == MCHAR && y_size == 1)
3943 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003944 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003945
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003946 if (VIsual_active)
3947 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003948 end_lnum = curbuf->b_visual.vi_end.lnum;
3949 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3950 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003951 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003952
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003953 do {
3954 totlen = count * yanklen;
3955 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003957 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003958 if (VIsual_active && col > (int)STRLEN(oldp))
3959 {
3960 lnum++;
3961 continue;
3962 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003963 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3964 if (newp == NULL)
3965 goto end; /* alloc() gave an error message */
3966 mch_memmove(newp, oldp, (size_t)col);
3967 ptr = newp + col;
3968 for (i = 0; i < count; ++i)
3969 {
3970 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3971 ptr += yanklen;
3972 }
3973 STRMOVE(ptr, oldp + col);
3974 ml_replace(lnum, newp, FALSE);
3975 /* Place cursor on last putted char. */
3976 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003977 {
3978 /* make sure curwin->w_virtcol is updated */
3979 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003980 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003983 if (VIsual_active)
3984 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003985 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003986
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003987 if (VIsual_active) /* reset lnum to the last visual line */
3988 lnum--;
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 curbuf->b_op_end = curwin->w_cursor;
3991 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3992 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3993 ++curwin->w_cursor.col;
3994 changed_bytes(lnum, col);
3995 }
3996 else
3997 {
3998 /*
3999 * Insert at least one line. When y_type is MCHAR, break the first
4000 * line in two.
4001 */
4002 for (cnt = 1; cnt <= count; ++cnt)
4003 {
4004 i = 0;
4005 if (y_type == MCHAR)
4006 {
4007 /*
4008 * Split the current line in two at the insert position.
4009 * First insert y_array[size - 1] in front of second line.
4010 * Then append y_array[0] to first line.
4011 */
4012 lnum = new_cursor.lnum;
4013 ptr = ml_get(lnum) + col;
4014 totlen = (int)STRLEN(y_array[y_size - 1]);
4015 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4016 if (newp == NULL)
4017 goto error;
4018 STRCPY(newp, y_array[y_size - 1]);
4019 STRCAT(newp, ptr);
4020 /* insert second line */
4021 ml_append(lnum, newp, (colnr_T)0, FALSE);
4022 vim_free(newp);
4023
4024 oldp = ml_get(lnum);
4025 newp = alloc_check((unsigned)(col + yanklen + 1));
4026 if (newp == NULL)
4027 goto error;
4028 /* copy first part of line */
4029 mch_memmove(newp, oldp, (size_t)col);
4030 /* append to first line */
4031 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4032 ml_replace(lnum, newp, FALSE);
4033
4034 curwin->w_cursor.lnum = lnum;
4035 i = 1;
4036 }
4037
4038 for (; i < y_size; ++i)
4039 {
4040 if ((y_type != MCHAR || i < y_size - 1)
4041 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4042 == FAIL)
4043 goto error;
4044 lnum++;
4045 ++nr_lines;
4046 if (flags & PUT_FIXINDENT)
4047 {
4048 old_pos = curwin->w_cursor;
4049 curwin->w_cursor.lnum = lnum;
4050 ptr = ml_get(lnum);
4051 if (cnt == count && i == y_size - 1)
4052 lendiff = (int)STRLEN(ptr);
4053#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4054 if (*ptr == '#' && preprocs_left())
4055 indent = 0; /* Leave # lines at start */
4056 else
4057#endif
4058 if (*ptr == NUL)
4059 indent = 0; /* Ignore empty lines */
4060 else if (first_indent)
4061 {
4062 indent_diff = orig_indent - get_indent();
4063 indent = orig_indent;
4064 first_indent = FALSE;
4065 }
4066 else if ((indent = get_indent() + indent_diff) < 0)
4067 indent = 0;
4068 (void)set_indent(indent, 0);
4069 curwin->w_cursor = old_pos;
4070 /* remember how many chars were removed */
4071 if (cnt == count && i == y_size - 1)
4072 lendiff -= (int)STRLEN(ml_get(lnum));
4073 }
4074 }
4075 }
4076
4077error:
4078 /* Adjust marks. */
4079 if (y_type == MLINE)
4080 {
4081 curbuf->b_op_start.col = 0;
4082 if (dir == FORWARD)
4083 curbuf->b_op_start.lnum++;
4084 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004085 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004086 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004087 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004088 < curbuf->b_ml.ml_line_count
4089#ifdef FEAT_DIFF
4090 || curwin->w_p_diff
4091#endif
4092 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004093 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 (linenr_T)MAXLNUM, nr_lines, 0L);
4095
4096 /* note changed text for displaying and folding */
4097 if (y_type == MCHAR)
4098 changed_lines(curwin->w_cursor.lnum, col,
4099 curwin->w_cursor.lnum + 1, nr_lines);
4100 else
4101 changed_lines(curbuf->b_op_start.lnum, 0,
4102 curbuf->b_op_start.lnum, nr_lines);
4103
4104 /* put '] mark at last inserted character */
4105 curbuf->b_op_end.lnum = lnum;
4106 /* correct length for change in indent */
4107 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4108 if (col > 1)
4109 curbuf->b_op_end.col = col - 1;
4110 else
4111 curbuf->b_op_end.col = 0;
4112
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004113 if (flags & PUT_CURSLINE)
4114 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004115 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004116 curwin->w_cursor.lnum = lnum;
4117 beginline(BL_WHITE | BL_FIX);
4118 }
4119 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 /* put cursor after inserted text */
4122 if (y_type == MLINE)
4123 {
4124 if (lnum >= curbuf->b_ml.ml_line_count)
4125 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4126 else
4127 curwin->w_cursor.lnum = lnum + 1;
4128 curwin->w_cursor.col = 0;
4129 }
4130 else
4131 {
4132 curwin->w_cursor.lnum = lnum;
4133 curwin->w_cursor.col = col;
4134 }
4135 }
4136 else if (y_type == MLINE)
4137 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004138 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 curwin->w_cursor.col = 0;
4140 if (dir == FORWARD)
4141 ++curwin->w_cursor.lnum;
4142 beginline(BL_WHITE | BL_FIX);
4143 }
4144 else /* put cursor on first inserted character */
4145 curwin->w_cursor = new_cursor;
4146 }
4147 }
4148
4149 msgmore(nr_lines);
4150 curwin->w_set_curswant = TRUE;
4151
4152end:
4153 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004155 if (regname == '=')
4156 vim_free(y_array);
4157
Bram Moolenaar033d8882013-09-25 23:24:57 +02004158 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004159
Bram Moolenaar677ee682005-01-27 14:41:15 +00004160 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004161 adjust_cursor_eol();
4162}
4163
4164/*
4165 * When the cursor is on the NUL past the end of the line and it should not be
4166 * there move it left.
4167 */
4168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004169adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004170{
4171 if (curwin->w_cursor.col > 0
4172 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004173#ifdef FEAT_VIRTUALEDIT
4174 && (ve_flags & VE_ONEMORE) == 0
4175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 && !(restart_edit || (State & INSERT)))
4177 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004178 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004179 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#ifdef FEAT_VIRTUALEDIT
4182 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004183 {
4184 colnr_T scol, ecol;
4185
4186 /* Coladd is set to the width of the last character. */
4187 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4188 curwin->w_cursor.coladd = ecol - scol + 1;
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#endif
4191 }
4192}
4193
4194#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4195/*
4196 * Return TRUE if lines starting with '#' should be left aligned.
4197 */
4198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004199preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
4201 return
4202# ifdef FEAT_SMARTINDENT
4203# ifdef FEAT_CINDENT
4204 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4205# else
4206 curbuf->b_p_si
4207# endif
4208# endif
4209# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004210 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4211 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212# endif
4213 ;
4214}
4215#endif
4216
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004217/*
4218 * Return the character name of the register with the given number.
4219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004221get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
4223 if (num == -1)
4224 return '"';
4225 else if (num < 10)
4226 return num + '0';
4227 else if (num == DELETION_REGISTER)
4228 return '-';
4229#ifdef FEAT_CLIPBOARD
4230 else if (num == STAR_REGISTER)
4231 return '*';
4232 else if (num == PLUS_REGISTER)
4233 return '+';
4234#endif
4235 else
4236 {
4237#ifdef EBCDIC
4238 int i;
4239
4240 /* EBCDIC is really braindead ... */
4241 i = 'a' + (num - 10);
4242 if (i > 'i')
4243 i += 7;
4244 if (i > 'r')
4245 i += 8;
4246 return i;
4247#else
4248 return num + 'a' - 10;
4249#endif
4250 }
4251}
4252
4253/*
4254 * ":dis" and ":registers": Display the contents of the yank registers.
4255 */
4256 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004257ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004259 int i, n;
4260 long j;
4261 char_u *p;
4262 yankreg_T *yb;
4263 int name;
4264 int attr;
4265 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004266#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004267 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004268#else
4269# define clen 1
4270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 if (arg != NULL && *arg == NUL)
4273 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004274 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /* Highlight title */
4277 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4278 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4279 {
4280 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004281 if (arg != NULL && vim_strchr(arg, name) == NULL
4282#ifdef ONE_CLIPBOARD
4283 /* Star register and plus register contain the same thing. */
4284 && (name != '*' || vim_strchr(arg, '+') == NULL)
4285#endif
4286 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 continue; /* did not ask for this register */
4288
4289#ifdef FEAT_CLIPBOARD
4290 /* Adjust register name for "unnamed" in 'clipboard'.
4291 * When it's a clipboard register, fill it with the current contents
4292 * of the clipboard. */
4293 adjust_clip_reg(&name);
4294 (void)may_get_selection(name);
4295#endif
4296
4297 if (i == -1)
4298 {
4299 if (y_previous != NULL)
4300 yb = y_previous;
4301 else
4302 yb = &(y_regs[0]);
4303 }
4304 else
4305 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004306
4307#ifdef FEAT_EVAL
4308 if (name == MB_TOLOWER(redir_reg)
4309 || (redir_reg == '"' && yb == y_previous))
4310 continue; /* do not list register being written to, the
4311 * pointer can be freed */
4312#endif
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (yb->y_array != NULL)
4315 {
4316 msg_putchar('\n');
4317 msg_putchar('"');
4318 msg_putchar(name);
4319 MSG_PUTS(" ");
4320
4321 n = (int)Columns - 6;
4322 for (j = 0; j < yb->y_size && n > 1; ++j)
4323 {
4324 if (j)
4325 {
4326 MSG_PUTS_ATTR("^J", attr);
4327 n -= 2;
4328 }
4329 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004332 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004333#endif
4334 msg_outtrans_len(p, clen);
4335#ifdef FEAT_MBYTE
4336 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#endif
4338 }
4339 }
4340 if (n > 1 && yb->y_type == MLINE)
4341 MSG_PUTS_ATTR("^J", attr);
4342 out_flush(); /* show one line at a time */
4343 }
4344 ui_breakcheck();
4345 }
4346
4347 /*
4348 * display last inserted text
4349 */
4350 if ((p = get_last_insert()) != NULL
4351 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4352 {
4353 MSG_PUTS("\n\". ");
4354 dis_msg(p, TRUE);
4355 }
4356
4357 /*
4358 * display last command line
4359 */
4360 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4361 && !got_int)
4362 {
4363 MSG_PUTS("\n\": ");
4364 dis_msg(last_cmdline, FALSE);
4365 }
4366
4367 /*
4368 * display current file name
4369 */
4370 if (curbuf->b_fname != NULL
4371 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4372 {
4373 MSG_PUTS("\n\"% ");
4374 dis_msg(curbuf->b_fname, FALSE);
4375 }
4376
4377 /*
4378 * display alternate file name
4379 */
4380 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4381 {
4382 char_u *fname;
4383 linenr_T dummy;
4384
4385 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4386 {
4387 MSG_PUTS("\n\"# ");
4388 dis_msg(fname, FALSE);
4389 }
4390 }
4391
4392 /*
4393 * display last search pattern
4394 */
4395 if (last_search_pat() != NULL
4396 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4397 {
4398 MSG_PUTS("\n\"/ ");
4399 dis_msg(last_search_pat(), FALSE);
4400 }
4401
4402#ifdef FEAT_EVAL
4403 /*
4404 * display last used expression
4405 */
4406 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4407 && !got_int)
4408 {
4409 MSG_PUTS("\n\"= ");
4410 dis_msg(expr_line, FALSE);
4411 }
4412#endif
4413}
4414
4415/*
4416 * display a string for do_dis()
4417 * truncate at end of screen line
4418 */
4419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420dis_msg(
4421 char_u *p,
4422 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 int n;
4425#ifdef FEAT_MBYTE
4426 int l;
4427#endif
4428
4429 n = (int)Columns - 6;
4430 while (*p != NUL
4431 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4432 && (n -= ptr2cells(p)) >= 0)
4433 {
4434#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004435 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 msg_outtrans_len(p, l);
4438 p += l;
4439 }
4440 else
4441#endif
4442 msg_outtrans_len(p++, 1);
4443 }
4444 ui_breakcheck();
4445}
4446
Bram Moolenaar81340392012-06-06 16:12:59 +02004447#if defined(FEAT_COMMENTS) || defined(PROTO)
4448/*
4449 * If "process" is TRUE and the line begins with a comment leader (possibly
4450 * after some white space), return a pointer to the text after it. Put a boolean
4451 * value indicating whether the line ends with an unclosed comment in
4452 * "is_comment".
4453 * line - line to be processed,
4454 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004455 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004456 * include_space - whether to also skip space following the comment leader,
4457 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004458 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004459 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004460 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004461skip_comment(
4462 char_u *line,
4463 int process,
4464 int include_space,
4465 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004466{
4467 char_u *comment_flags = NULL;
4468 int lead_len;
4469 int leader_offset = get_last_leader_offset(line, &comment_flags);
4470
4471 *is_comment = FALSE;
4472 if (leader_offset != -1)
4473 {
4474 /* Let's check whether the line ends with an unclosed comment.
4475 * If the last comment leader has COM_END in flags, there's no comment.
4476 */
4477 while (*comment_flags)
4478 {
4479 if (*comment_flags == COM_END
4480 || *comment_flags == ':')
4481 break;
4482 ++comment_flags;
4483 }
4484 if (*comment_flags != COM_END)
4485 *is_comment = TRUE;
4486 }
4487
4488 if (process == FALSE)
4489 return line;
4490
4491 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4492
4493 if (lead_len == 0)
4494 return line;
4495
4496 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004497 * - COM_END,
4498 * - colon,
4499 * whichever comes first.
4500 */
4501 while (*comment_flags)
4502 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004503 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004504 || *comment_flags == ':')
4505 {
4506 break;
4507 }
4508 ++comment_flags;
4509 }
4510
4511 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004512 * starting with a closing part of a three-part comment. That's good,
4513 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004514 */
4515 if (*comment_flags == ':' || *comment_flags == NUL)
4516 line += lead_len;
4517
4518 return line;
4519}
4520#endif
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004523 * Join 'count' lines (minimal 2) at cursor position.
4524 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004525 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4526 * leaders should not be removed.
4527 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4528 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004530 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
4532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004533do_join(
4534 long count,
4535 int insert_space,
4536 int save_undo,
4537 int use_formatoptions UNUSED,
4538 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004540 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004541 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004542 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004544 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004545 int endcurr1 = NUL;
4546 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004547 int currsize = 0; /* size of the current line */
4548 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004550 colnr_T col = 0;
4551 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004552#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004553 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004554 int remove_comments = (use_formatoptions == TRUE)
4555 && has_format_option(FO_REMOVE_COMS);
4556 int prev_was_comment;
4557#endif
4558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004560 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4561 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004564 /* Allocate an array to store the number of spaces inserted before each
4565 * line. We will use it to pre-compute the length of the new line and the
4566 * proper placement of each original line in the new one. */
4567 spaces = lalloc_clear((long_u)count, TRUE);
4568 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004570#if defined(FEAT_COMMENTS) || defined(PROTO)
4571 if (remove_comments)
4572 {
4573 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4574 if (comments == NULL)
4575 {
4576 vim_free(spaces);
4577 return FAIL;
4578 }
4579 }
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004583 * Don't move anything, just compute the final line length
4584 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004586 for (t = 0; t < count; ++t)
4587 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004588 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004589 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004590 {
4591 /* Set the '[ mark. */
4592 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4593 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4594 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004595#if defined(FEAT_COMMENTS) || defined(PROTO)
4596 if (remove_comments)
4597 {
4598 /* We don't want to remove the comment leader if the
4599 * previous line is not a comment. */
4600 if (t > 0 && prev_was_comment)
4601 {
4602
4603 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4604 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004605 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004606 curr = new_curr;
4607 }
4608 else
4609 curr = skip_comment(curr, FALSE, insert_space,
4610 &prev_was_comment);
4611 }
4612#endif
4613
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004614 if (insert_space && t > 0)
4615 {
4616 curr = skipwhite(curr);
4617 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4618#ifdef FEAT_MBYTE
4619 && (!has_format_option(FO_MBYTE_JOIN)
4620 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4621 && (!has_format_option(FO_MBYTE_JOIN2)
4622 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4623#endif
4624 )
4625 {
4626 /* don't add a space if the line is ending in a space */
4627 if (endcurr1 == ' ')
4628 endcurr1 = endcurr2;
4629 else
4630 ++spaces[t];
4631 /* extra space when 'joinspaces' set and line ends in '.' */
4632 if ( p_js
4633 && (endcurr1 == '.'
4634 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4635 && (endcurr1 == '?' || endcurr1 == '!'))))
4636 ++spaces[t];
4637 }
4638 }
4639 currsize = (int)STRLEN(curr);
4640 sumsize += currsize + spaces[t];
4641 endcurr1 = endcurr2 = NUL;
4642 if (insert_space && currsize > 0)
4643 {
4644#ifdef FEAT_MBYTE
4645 if (has_mbyte)
4646 {
4647 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004648 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004649 endcurr1 = (*mb_ptr2char)(cend);
4650 if (cend > curr)
4651 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004652 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004653 endcurr2 = (*mb_ptr2char)(cend);
4654 }
4655 }
4656 else
4657#endif
4658 {
4659 endcurr1 = *(curr + currsize - 1);
4660 if (currsize > 1)
4661 endcurr2 = *(curr + currsize - 2);
4662 }
4663 }
4664 line_breakcheck();
4665 if (got_int)
4666 {
4667 ret = FAIL;
4668 goto theend;
4669 }
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004672 /* store the column position before last line */
4673 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004675 /* allocate the space for the new line */
4676 newp = alloc_check((unsigned)(sumsize + 1));
4677 cend = newp + sumsize;
4678 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004680 /*
4681 * Move affected lines to the new long one.
4682 *
4683 * Move marks from each deleted line to the joined line, adjusting the
4684 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4685 * should not really be a problem.
4686 */
4687 for (t = count - 1; ; --t)
4688 {
4689 cend -= currsize;
4690 mch_memmove(cend, curr, (size_t)currsize);
4691 if (spaces[t] > 0)
4692 {
4693 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004694 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004695 }
4696 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004697 (long)(cend - newp + spaces[t] - (curr - curr_start)));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004698 if (t == 0)
4699 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004700 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004701#if defined(FEAT_COMMENTS) || defined(PROTO)
4702 if (remove_comments)
4703 curr += comments[t - 1];
4704#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004705 if (insert_space && t > 1)
4706 curr = skipwhite(curr);
4707 currsize = (int)STRLEN(curr);
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4710
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004711 if (setmark)
4712 {
4713 /* Set the '] mark. */
4714 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4715 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4716 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 /* Only report the change in the first line here, del_lines() will report
4719 * the deleted line. */
4720 changed_lines(curwin->w_cursor.lnum, currsize,
4721 curwin->w_cursor.lnum + 1, 0L);
4722
4723 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004724 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 * briefly, and then move it back. After del_lines() the cursor may
4726 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 */
4728 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004730 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 curwin->w_cursor.lnum = t;
4732
4733 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004734 * Set the cursor column:
4735 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004736 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004738 curwin->w_cursor.col =
4739 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#ifdef FEAT_VIRTUALEDIT
4743 curwin->w_cursor.coladd = 0;
4744#endif
4745 curwin->w_set_curswant = TRUE;
4746
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004747theend:
4748 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004749#if defined(FEAT_COMMENTS) || defined(PROTO)
4750 if (remove_comments)
4751 vim_free(comments);
4752#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004753 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754}
4755
4756#ifdef FEAT_COMMENTS
4757/*
4758 * Return TRUE if the two comment leaders given are the same. "lnum" is
4759 * the first line. White-space is ignored. Note that the whole of
4760 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4761 */
4762 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004763same_leader(
4764 linenr_T lnum,
4765 int leader1_len,
4766 char_u *leader1_flags,
4767 int leader2_len,
4768 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 int idx1 = 0, idx2 = 0;
4771 char_u *p;
4772 char_u *line1;
4773 char_u *line2;
4774
4775 if (leader1_len == 0)
4776 return (leader2_len == 0);
4777
4778 /*
4779 * If first leader has 'f' flag, the lines can be joined only if the
4780 * second line does not have a leader.
4781 * If first leader has 'e' flag, the lines can never be joined.
4782 * If fist leader has 's' flag, the lines can only be joined if there is
4783 * some text after it and the second line has the 'm' flag.
4784 */
4785 if (leader1_flags != NULL)
4786 {
4787 for (p = leader1_flags; *p && *p != ':'; ++p)
4788 {
4789 if (*p == COM_FIRST)
4790 return (leader2_len == 0);
4791 if (*p == COM_END)
4792 return FALSE;
4793 if (*p == COM_START)
4794 {
4795 if (*(ml_get(lnum) + leader1_len) == NUL)
4796 return FALSE;
4797 if (leader2_flags == NULL || leader2_len == 0)
4798 return FALSE;
4799 for (p = leader2_flags; *p && *p != ':'; ++p)
4800 if (*p == COM_MIDDLE)
4801 return TRUE;
4802 return FALSE;
4803 }
4804 }
4805 }
4806
4807 /*
4808 * Get current line and next line, compare the leaders.
4809 * The first line has to be saved, only one line can be locked at a time.
4810 */
4811 line1 = vim_strsave(ml_get(lnum));
4812 if (line1 != NULL)
4813 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004814 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 ;
4816 line2 = ml_get(lnum + 1);
4817 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4818 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004819 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (line1[idx1++] != line2[idx2])
4822 break;
4823 }
4824 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004825 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 ++idx1;
4827 }
4828 vim_free(line1);
4829 }
4830 return (idx2 == leader2_len && idx1 == leader1_len);
4831}
4832#endif
4833
4834/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004835 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
4837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838op_format(
4839 oparg_T *oap,
4840 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
4842 long old_line_count = curbuf->b_ml.ml_line_count;
4843
4844 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4845 * can put it back there. */
4846 curwin->w_cursor = oap->cursor_start;
4847
4848 if (u_save((linenr_T)(oap->start.lnum - 1),
4849 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4850 return;
4851 curwin->w_cursor = oap->start;
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (oap->is_VIsual)
4854 /* When there is no change: need to remove the Visual selection */
4855 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857 /* Set '[ mark at the start of the formatted area */
4858 curbuf->b_op_start = oap->start;
4859
4860 /* For "gw" remember the cursor position and put it back below (adjusted
4861 * for joined and split lines). */
4862 if (keep_cursor)
4863 saved_cursor = oap->cursor_start;
4864
Bram Moolenaar81a82092008-03-12 16:27:00 +00004865 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 /*
4868 * Leave the cursor at the first non-blank of the last formatted line.
4869 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4870 * line, so "." will do the next lines.
4871 */
4872 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4873 ++curwin->w_cursor.lnum;
4874 beginline(BL_WHITE | BL_FIX);
4875 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4876 msgmore(old_line_count);
4877
4878 /* put '] mark on the end of the formatted area */
4879 curbuf->b_op_end = curwin->w_cursor;
4880
4881 if (keep_cursor)
4882 {
4883 curwin->w_cursor = saved_cursor;
4884 saved_cursor.lnum = 0;
4885 }
4886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (oap->is_VIsual)
4888 {
4889 win_T *wp;
4890
4891 FOR_ALL_WINDOWS(wp)
4892 {
4893 if (wp->w_old_cursor_lnum != 0)
4894 {
4895 /* When lines have been inserted or deleted, adjust the end of
4896 * the Visual area to be redrawn. */
4897 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4898 wp->w_old_cursor_lnum += old_line_count;
4899 else
4900 wp->w_old_visual_lnum += old_line_count;
4901 }
4902 }
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904}
4905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004906#if defined(FEAT_EVAL) || defined(PROTO)
4907/*
4908 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4909 */
4910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004912{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004913 if (oap->is_VIsual)
4914 /* When there is no change: need to remove the Visual selection */
4915 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004916
Bram Moolenaar700303e2010-07-11 17:35:50 +02004917 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4918 /* As documented: when 'formatexpr' returns non-zero fall back to
4919 * internal formatting. */
4920 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004921}
4922
4923 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004924fex_format(
4925 linenr_T lnum,
4926 long count,
4927 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004928{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004929 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4930 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004931 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004932 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004933
4934 /*
4935 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004936 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004937 */
4938 set_vim_var_nr(VV_LNUM, lnum);
4939 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004940 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004941
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004942 /* Make a copy, the option could be changed while calling it. */
4943 fex = vim_strsave(curbuf->b_p_fex);
4944 if (fex == NULL)
4945 return 0;
4946
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004947 /*
4948 * Evaluate the function.
4949 */
4950 if (use_sandbox)
4951 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004952 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004953 if (use_sandbox)
4954 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004955
4956 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004957 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004958
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004959 return r;
4960}
4961#endif
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * Format "line_count" lines, starting at the cursor position.
4965 * When "line_count" is negative, format until the end of the paragraph.
4966 * Lines after the cursor line are saved for undo, caller must have saved the
4967 * first line.
4968 */
4969 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004970format_lines(
4971 linenr_T line_count,
4972 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
4974 int max_len;
4975 int is_not_par; /* current line not part of parag. */
4976 int next_is_not_par; /* next line not part of paragraph */
4977 int is_end_par; /* at end of paragraph */
4978 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4979 int next_is_start_par = FALSE;
4980#ifdef FEAT_COMMENTS
4981 int leader_len = 0; /* leader len of current line */
4982 int next_leader_len; /* leader len of next line */
4983 char_u *leader_flags = NULL; /* flags for leader of current line */
4984 char_u *next_leader_flags; /* flags for leader of next line */
4985 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004986 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#endif
4988 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004989 int second_indent = -1; /* indent for second line (comment
4990 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int do_second_indent;
4992 int do_number_indent;
4993 int do_trail_white;
4994 int first_par_line = TRUE;
4995 int smd_save;
4996 long count;
4997 int need_set_indent = TRUE; /* set indent of next paragraph */
4998 int force_format = FALSE;
4999 int old_State = State;
5000
5001 /* length of a line to force formatting: 3 * 'tw' */
5002 max_len = comp_textwidth(TRUE) * 3;
5003
5004 /* check for 'q', '2' and '1' in 'formatoptions' */
5005#ifdef FEAT_COMMENTS
5006 do_comments = has_format_option(FO_Q_COMS);
5007#endif
5008 do_second_indent = has_format_option(FO_Q_SECOND);
5009 do_number_indent = has_format_option(FO_Q_NUMBER);
5010 do_trail_white = has_format_option(FO_WHITE_PAR);
5011
5012 /*
5013 * Get info about the previous and current line.
5014 */
5015 if (curwin->w_cursor.lnum > 1)
5016 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5017#ifdef FEAT_COMMENTS
5018 , &leader_len, &leader_flags, do_comments
5019#endif
5020 );
5021 else
5022 is_not_par = TRUE;
5023 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5024#ifdef FEAT_COMMENTS
5025 , &next_leader_len, &next_leader_flags, do_comments
5026#endif
5027 );
5028 is_end_par = (is_not_par || next_is_not_par);
5029 if (!is_end_par && do_trail_white)
5030 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5031
5032 curwin->w_cursor.lnum--;
5033 for (count = line_count; count != 0 && !got_int; --count)
5034 {
5035 /*
5036 * Advance to next paragraph.
5037 */
5038 if (advance)
5039 {
5040 curwin->w_cursor.lnum++;
5041 prev_is_end_par = is_end_par;
5042 is_not_par = next_is_not_par;
5043#ifdef FEAT_COMMENTS
5044 leader_len = next_leader_len;
5045 leader_flags = next_leader_flags;
5046#endif
5047 }
5048
5049 /*
5050 * The last line to be formatted.
5051 */
5052 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5053 {
5054 next_is_not_par = TRUE;
5055#ifdef FEAT_COMMENTS
5056 next_leader_len = 0;
5057 next_leader_flags = NULL;
5058#endif
5059 }
5060 else
5061 {
5062 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5063#ifdef FEAT_COMMENTS
5064 , &next_leader_len, &next_leader_flags, do_comments
5065#endif
5066 );
5067 if (do_number_indent)
5068 next_is_start_par =
5069 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5070 }
5071 advance = TRUE;
5072 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5073 if (!is_end_par && do_trail_white)
5074 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5075
5076 /*
5077 * Skip lines that are not in a paragraph.
5078 */
5079 if (is_not_par)
5080 {
5081 if (line_count < 0)
5082 break;
5083 }
5084 else
5085 {
5086 /*
5087 * For the first line of a paragraph, check indent of second line.
5088 * Don't do this for comments and empty lines.
5089 */
5090 if (first_par_line
5091 && (do_second_indent || do_number_indent)
5092 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005093 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005095 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005096 {
5097#ifdef FEAT_COMMENTS
5098 if (leader_len == 0 && next_leader_len == 0)
5099 {
5100 /* no comment found */
5101#endif
5102 second_indent =
5103 get_indent_lnum(curwin->w_cursor.lnum + 1);
5104#ifdef FEAT_COMMENTS
5105 }
5106 else
5107 {
5108 second_indent = next_leader_len;
5109 do_comments_list = 1;
5110 }
5111#endif
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005114 {
5115#ifdef FEAT_COMMENTS
5116 if (leader_len == 0 && next_leader_len == 0)
5117 {
5118 /* no comment found */
5119#endif
5120 second_indent =
5121 get_number_indent(curwin->w_cursor.lnum);
5122#ifdef FEAT_COMMENTS
5123 }
5124 else
5125 {
5126 /* get_number_indent() is now "comment aware"... */
5127 second_indent =
5128 get_number_indent(curwin->w_cursor.lnum);
5129 do_comments_list = 1;
5130 }
5131#endif
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134
5135 /*
5136 * When the comment leader changes, it's the end of the paragraph.
5137 */
5138 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5139#ifdef FEAT_COMMENTS
5140 || !same_leader(curwin->w_cursor.lnum,
5141 leader_len, leader_flags,
5142 next_leader_len, next_leader_flags)
5143#endif
5144 )
5145 is_end_par = TRUE;
5146
5147 /*
5148 * If we have got to the end of a paragraph, or the line is
5149 * getting long, format it.
5150 */
5151 if (is_end_par || force_format)
5152 {
5153 if (need_set_indent)
5154 /* replace indent in first line with minimal number of
5155 * tabs and spaces, according to current options */
5156 (void)set_indent(get_indent(), SIN_CHANGED);
5157
5158 /* put cursor on last non-space */
5159 State = NORMAL; /* don't go past end-of-line */
5160 coladvance((colnr_T)MAXCOL);
5161 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5162 dec_cursor();
5163
5164 /* do the formatting, without 'showmode' */
5165 State = INSERT; /* for open_line() */
5166 smd_save = p_smd;
5167 p_smd = FALSE;
5168 insertchar(NUL, INSCHAR_FORMAT
5169#ifdef FEAT_COMMENTS
5170 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005171 + (do_comments && do_comments_list
5172 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005174 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 State = old_State;
5176 p_smd = smd_save;
5177 second_indent = -1;
5178 /* at end of par.: need to set indent of next par. */
5179 need_set_indent = is_end_par;
5180 if (is_end_par)
5181 {
5182 /* When called with a negative line count, break at the
5183 * end of the paragraph. */
5184 if (line_count < 0)
5185 break;
5186 first_par_line = TRUE;
5187 }
5188 force_format = FALSE;
5189 }
5190
5191 /*
5192 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005193 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 */
5195 if (!is_end_par)
5196 {
5197 advance = FALSE;
5198 curwin->w_cursor.lnum++;
5199 curwin->w_cursor.col = 0;
5200 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005204 {
5205 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
5207 (long)-next_leader_len);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005208 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005210 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5211 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005212 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005213
5214 if (indent > 0)
5215 {
5216 (void)del_bytes(indent, FALSE, FALSE);
5217 mark_col_adjust(curwin->w_cursor.lnum,
5218 (colnr_T)0, 0L, (long)-indent);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005219 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005222 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 beep_flush();
5225 break;
5226 }
5227 first_par_line = FALSE;
5228 /* If the line is getting long, format it next time */
5229 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5230 force_format = TRUE;
5231 else
5232 force_format = FALSE;
5233 }
5234 }
5235 line_breakcheck();
5236 }
5237}
5238
5239/*
5240 * Return TRUE if line "lnum" ends in a white character.
5241 */
5242 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005243ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
5245 char_u *s = ml_get(lnum);
5246 size_t l;
5247
5248 if (*s == NUL)
5249 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005250 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 * invocation may call function multiple times". */
5252 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005253 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254}
5255
5256/*
5257 * Blank lines, and lines containing only the comment leader, are left
5258 * untouched by the formatting. The function returns TRUE in this
5259 * case. It also returns TRUE when a line starts with the end of a comment
5260 * ('e' in comment flags), so that this line is skipped, and not joined to the
5261 * previous line. A new paragraph starts after a blank line, or when the
5262 * comment leader changes -- webb.
5263 */
5264#ifdef FEAT_COMMENTS
5265 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005266fmt_check_par(
5267 linenr_T lnum,
5268 int *leader_len,
5269 char_u **leader_flags,
5270 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 char_u *flags = NULL; /* init for GCC */
5273 char_u *ptr;
5274
5275 ptr = ml_get(lnum);
5276 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005277 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 else
5279 *leader_len = 0;
5280
5281 if (*leader_len > 0)
5282 {
5283 /*
5284 * Search for 'e' flag in comment leader flags.
5285 */
5286 flags = *leader_flags;
5287 while (*flags && *flags != ':' && *flags != COM_END)
5288 ++flags;
5289 }
5290
5291 return (*skipwhite(ptr + *leader_len) == NUL
5292 || (*leader_len > 0 && *flags == COM_END)
5293 || startPS(lnum, NUL, FALSE));
5294}
5295#else
5296 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005297fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
5299 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5300}
5301#endif
5302
5303/*
5304 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5305 * previous line is in the same paragraph. Used for auto-formatting.
5306 */
5307 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310 char_u *p;
5311#ifdef FEAT_COMMENTS
5312 int leader_len = 0; /* leader len of current line */
5313 char_u *leader_flags = NULL; /* flags for leader of current line */
5314 int next_leader_len; /* leader len of next line */
5315 char_u *next_leader_flags; /* flags for leader of next line */
5316 int do_comments; /* format comments */
5317#endif
5318
5319 if (lnum <= 1)
5320 return TRUE; /* start of the file */
5321
5322 p = ml_get(lnum - 1);
5323 if (*p == NUL)
5324 return TRUE; /* after empty line */
5325
5326#ifdef FEAT_COMMENTS
5327 do_comments = has_format_option(FO_Q_COMS);
5328#endif
5329 if (fmt_check_par(lnum - 1
5330#ifdef FEAT_COMMENTS
5331 , &leader_len, &leader_flags, do_comments
5332#endif
5333 ))
5334 return TRUE; /* after non-paragraph line */
5335
5336 if (fmt_check_par(lnum
5337#ifdef FEAT_COMMENTS
5338 , &next_leader_len, &next_leader_flags, do_comments
5339#endif
5340 ))
5341 return TRUE; /* "lnum" is not a paragraph line */
5342
5343 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5344 return TRUE; /* missing trailing space in previous line. */
5345
5346 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5347 return TRUE; /* numbered item starts in "lnum". */
5348
5349#ifdef FEAT_COMMENTS
5350 if (!same_leader(lnum - 1, leader_len, leader_flags,
5351 next_leader_len, next_leader_flags))
5352 return TRUE; /* change of comment leader. */
5353#endif
5354
5355 return FALSE;
5356}
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358/*
5359 * prepare a few things for block mode yank/delete/tilde
5360 *
5361 * for delete:
5362 * - textlen includes the first/last char to be (partly) deleted
5363 * - start/endspaces is the number of columns that are taken by the
5364 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005365 * deleted.
5366 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 * - textlen includes the first/last char to be wholly yanked
5368 * - start/endspaces is the number of columns of the first/last yanked char
5369 * that are to be yanked.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372block_prep(
5373 oparg_T *oap,
5374 struct block_def *bdp,
5375 linenr_T lnum,
5376 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 int incr = 0;
5379 char_u *pend;
5380 char_u *pstart;
5381 char_u *line;
5382 char_u *prev_pstart;
5383 char_u *prev_pend;
5384
5385 bdp->startspaces = 0;
5386 bdp->endspaces = 0;
5387 bdp->textlen = 0;
5388 bdp->start_vcol = 0;
5389 bdp->end_vcol = 0;
5390#ifdef FEAT_VISUALEXTRA
5391 bdp->is_short = FALSE;
5392 bdp->is_oneChar = FALSE;
5393 bdp->pre_whitesp = 0;
5394 bdp->pre_whitesp_c = 0;
5395 bdp->end_char_vcols = 0;
5396#endif
5397 bdp->start_char_vcols = 0;
5398
5399 line = ml_get(lnum);
5400 pstart = line;
5401 prev_pstart = line;
5402 while (bdp->start_vcol < oap->start_vcol && *pstart)
5403 {
5404 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005405 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 bdp->start_vcol += incr;
5407#ifdef FEAT_VISUALEXTRA
Bram Moolenaar1c465442017-03-12 20:10:05 +01005408 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 bdp->pre_whitesp += incr;
5411 bdp->pre_whitesp_c++;
5412 }
5413 else
5414 {
5415 bdp->pre_whitesp = 0;
5416 bdp->pre_whitesp_c = 0;
5417 }
5418#endif
5419 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005420 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422 bdp->start_char_vcols = incr;
5423 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5424 {
5425 bdp->end_vcol = bdp->start_vcol;
5426#ifdef FEAT_VISUALEXTRA
5427 bdp->is_short = TRUE;
5428#endif
5429 if (!is_del || oap->op_type == OP_APPEND)
5430 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5431 }
5432 else
5433 {
5434 /* notice: this converts partly selected Multibyte characters to
5435 * spaces, too. */
5436 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5437 if (is_del && bdp->startspaces)
5438 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5439 pend = pstart;
5440 bdp->end_vcol = bdp->start_vcol;
5441 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5442 {
5443#ifdef FEAT_VISUALEXTRA
5444 bdp->is_oneChar = TRUE;
5445#endif
5446 if (oap->op_type == OP_INSERT)
5447 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5448 else if (oap->op_type == OP_APPEND)
5449 {
5450 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5451 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5452 }
5453 else
5454 {
5455 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5456 if (is_del && oap->op_type != OP_LSHIFT)
5457 {
5458 /* just putting the sum of those two into
5459 * bdp->startspaces doesn't work for Visual replace,
5460 * so we have to split the tab in two */
5461 bdp->startspaces = bdp->start_char_vcols
5462 - (bdp->start_vcol - oap->start_vcol);
5463 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5464 }
5465 }
5466 }
5467 else
5468 {
5469 prev_pend = pend;
5470 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5471 {
5472 /* Count a tab for what it's worth (if list mode not on) */
5473 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005474 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 bdp->end_vcol += incr;
5476 }
5477 if (bdp->end_vcol <= oap->end_vcol
5478 && (!is_del
5479 || oap->op_type == OP_APPEND
5480 || oap->op_type == OP_REPLACE)) /* line too short */
5481 {
5482#ifdef FEAT_VISUALEXTRA
5483 bdp->is_short = TRUE;
5484#endif
5485 /* Alternative: include spaces to fill up the block.
5486 * Disadvantage: can lead to trailing spaces when the line is
5487 * short where the text is put */
5488 /* if (!is_del || oap->op_type == OP_APPEND) */
5489 if (oap->op_type == OP_APPEND || virtual_op)
5490 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005491 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 else
5493 bdp->endspaces = 0; /* replace doesn't add characters */
5494 }
5495 else if (bdp->end_vcol > oap->end_vcol)
5496 {
5497 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5498 if (!is_del && bdp->endspaces)
5499 {
5500 bdp->endspaces = incr - bdp->endspaces;
5501 if (pend != pstart)
5502 pend = prev_pend;
5503 }
5504 }
5505 }
5506#ifdef FEAT_VISUALEXTRA
5507 bdp->end_char_vcols = incr;
5508#endif
5509 if (is_del && bdp->startspaces)
5510 pstart = prev_pstart;
5511 bdp->textlen = (int)(pend - pstart);
5512 }
5513 bdp->textcol = (colnr_T) (pstart - line);
5514 bdp->textstart = pstart;
5515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005518 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005521op_addsub(
5522 oparg_T *oap,
5523 linenr_T Prenum1, /* Amount of add/subtract */
5524 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005526 pos_T pos;
5527 struct block_def bd;
5528 int change_cnt = 0;
5529 linenr_T amount = Prenum1;
5530
5531 if (!VIsual_active)
5532 {
5533 pos = curwin->w_cursor;
5534 if (u_save_cursor() == FAIL)
5535 return;
5536 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
5537 if (change_cnt)
5538 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5539 }
5540 else
5541 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005542 int one_change;
5543 int length;
5544 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005545
5546 if (u_save((linenr_T)(oap->start.lnum - 1),
5547 (linenr_T)(oap->end.lnum + 1)) == FAIL)
5548 return;
5549
5550 pos = oap->start;
5551 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5552 {
5553 if (oap->block_mode) /* Visual block mode */
5554 {
5555 block_prep(oap, &bd, pos.lnum, FALSE);
5556 pos.col = bd.textcol;
5557 length = bd.textlen;
5558 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005559 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005560 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005561 curwin->w_cursor.col = 0;
5562 pos.col = 0;
5563 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5564 }
5565 else /* oap->motion_type == MCHAR */
5566 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005567 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005568 dec(&(oap->end));
5569 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5570 pos.col = 0;
5571 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005572 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005573 pos.col += oap->start.col;
5574 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005575 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005576 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005577 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005578 length = (int)STRLEN(ml_get(oap->end.lnum));
5579 if (oap->end.col >= length)
5580 oap->end.col = length - 1;
5581 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005582 }
5583 }
5584 one_change = do_addsub(oap->op_type, &pos, length, amount);
5585 if (one_change)
5586 {
5587 /* Remember the start position of the first change. */
5588 if (change_cnt == 0)
5589 startpos = curbuf->b_op_start;
5590 ++change_cnt;
5591 }
5592
5593#ifdef FEAT_NETBEANS_INTG
5594 if (netbeans_active() && one_change)
5595 {
5596 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5597
5598 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5599 netbeans_inserted(curbuf, pos.lnum, pos.col,
5600 &ptr[pos.col], length);
5601 }
5602#endif
5603 if (g_cmd && one_change)
5604 amount += Prenum1;
5605 }
5606 if (change_cnt)
5607 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5608
5609 if (!change_cnt && oap->is_VIsual)
5610 /* No change: need to remove the Visual selection */
5611 redraw_curbuf_later(INVERTED);
5612
5613 /* Set '[ mark if something changed. Keep the last end
5614 * position from do_addsub(). */
5615 if (change_cnt > 0)
5616 curbuf->b_op_start = startpos;
5617
5618 if (change_cnt > p_report)
5619 {
5620 if (change_cnt == 1)
5621 MSG(_("1 line changed"));
5622 else
5623 smsg((char_u *)_("%ld lines changed"), change_cnt);
5624 }
5625 }
5626}
5627
5628/*
5629 * Add or subtract 'Prenum1' from a number in a line
5630 * op_type is OP_NR_ADD or OP_NR_SUB
5631 *
5632 * Returns TRUE if some character was changed.
5633 */
5634 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005635do_addsub(
5636 int op_type,
5637 pos_T *pos,
5638 int length,
5639 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 int col;
5642 char_u *buf1;
5643 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005644 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005646 uvarnumber_T n;
5647 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 char_u *ptr;
5649 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 int todel;
5651 int dohex;
5652 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005653 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 int doalp;
5655 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005657 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005658 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005659 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005660 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005661 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005662 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005663 pos_T startpos;
5664 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5667 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005668 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5670
Bram Moolenaard79e5502016-01-10 22:13:02 +01005671 curwin->w_cursor = *pos;
5672 ptr = ml_get(pos->lnum);
5673 col = pos->col;
5674
5675 if (*ptr == NUL)
5676 goto theend;
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 /*
5679 * First check if we are on a hexadecimal number, after the "0x".
5680 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005681 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005683 if (dobin)
5684 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005685 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005686 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005687#ifdef FEAT_MBYTE
5688 if (has_mbyte)
5689 col -= (*mb_head_off)(ptr, ptr + col);
5690#endif
5691 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005692
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005693 if (dohex)
5694 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005695 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005696 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005697#ifdef FEAT_MBYTE
5698 if (has_mbyte)
5699 col -= (*mb_head_off)(ptr, ptr + col);
5700#endif
5701 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005702
5703 if ( dobin
5704 && dohex
5705 && ! ((col > 0
5706 && (ptr[col] == 'X'
5707 || ptr[col] == 'x')
5708 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005709#ifdef FEAT_MBYTE
5710 && (!has_mbyte ||
5711 !(*mb_head_off)(ptr, ptr + col - 1))
5712#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005713 && vim_isxdigit(ptr[col + 1]))))
5714 {
5715
5716 /* In case of binary/hexadecimal pattern overlap match, rescan */
5717
Bram Moolenaard79e5502016-01-10 22:13:02 +01005718 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005719
5720 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005721 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005722 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005723#ifdef FEAT_MBYTE
5724 if (has_mbyte)
5725 col -= (*mb_head_off)(ptr, ptr + col);
5726#endif
5727 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005728 }
5729
5730 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005731 && col > 0
5732 && (ptr[col] == 'X'
5733 || ptr[col] == 'x')
5734 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005735#ifdef FEAT_MBYTE
5736 && (!has_mbyte ||
5737 !(*mb_head_off)(ptr, ptr + col - 1))
5738#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005739 && vim_isxdigit(ptr[col + 1])) ||
5740 ( dobin
5741 && col > 0
5742 && (ptr[col] == 'B'
5743 || ptr[col] == 'b')
5744 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005745#ifdef FEAT_MBYTE
5746 && (!has_mbyte ||
5747 !(*mb_head_off)(ptr, ptr + col - 1))
5748#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005749 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005750 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005751 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005752 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005753#ifdef FEAT_MBYTE
5754 if (has_mbyte)
5755 col -= (*mb_head_off)(ptr, ptr + col);
5756#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005757 }
5758 else
5759 {
5760 /*
5761 * Search forward and then backward to find the start of number.
5762 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005763 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005764
5765 while (ptr[col] != NUL
5766 && !vim_isdigit(ptr[col])
5767 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005768 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005769
5770 while (col > 0
5771 && vim_isdigit(ptr[col - 1])
5772 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005773 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005774 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005775#ifdef FEAT_MBYTE
5776 if (has_mbyte)
5777 col -= (*mb_head_off)(ptr, ptr + col);
5778#endif
5779 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005780 }
5781 }
5782
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005783 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005784 {
5785 while (ptr[col] != NUL && length > 0
5786 && !vim_isdigit(ptr[col])
5787 && !(doalp && ASCII_ISALPHA(ptr[col])))
5788 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005789 int mb_len = MB_PTR2LEN(ptr + col);
5790
5791 col += mb_len;
5792 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005793 }
5794
5795 if (length == 0)
5796 goto theend;
5797
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005798 if (col > pos->col && ptr[col - 1] == '-'
5799#ifdef FEAT_MBYTE
5800 && (!has_mbyte ||
5801 !(*mb_head_off)(ptr, ptr + col - 1))
5802#endif
5803 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005804 {
5805 negative = TRUE;
5806 was_positive = FALSE;
5807 }
5808 }
5809
5810 /*
5811 * If a number was found, and saving for undo works, replace the number.
5812 */
5813 firstdigit = ptr[col];
5814 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5815 {
5816 beep_flush();
5817 goto theend;
5818 }
5819
5820 if (doalp && ASCII_ISALPHA(firstdigit))
5821 {
5822 /* decrement or increment alphabetic character */
5823 if (op_type == OP_NR_SUB)
5824 {
5825 if (CharOrd(firstdigit) < Prenum1)
5826 {
5827 if (isupper(firstdigit))
5828 firstdigit = 'A';
5829 else
5830 firstdigit = 'a';
5831 }
5832 else
5833#ifdef EBCDIC
5834 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5835#else
5836 firstdigit -= Prenum1;
5837#endif
5838 }
5839 else
5840 {
5841 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5842 {
5843 if (isupper(firstdigit))
5844 firstdigit = 'Z';
5845 else
5846 firstdigit = 'z';
5847 }
5848 else
5849#ifdef EBCDIC
5850 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5851#else
5852 firstdigit += Prenum1;
5853#endif
5854 }
5855 curwin->w_cursor.col = col;
5856 if (!did_change)
5857 startpos = curwin->w_cursor;
5858 did_change = TRUE;
5859 (void)del_char(FALSE);
5860 ins_char(firstdigit);
5861 endpos = curwin->w_cursor;
5862 curwin->w_cursor.col = col;
5863 }
5864 else
5865 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005866 if (col > 0 && ptr[col - 1] == '-'
5867#ifdef FEAT_MBYTE
5868 && (!has_mbyte ||
5869 !(*mb_head_off)(ptr, ptr + col - 1))
5870#endif
5871 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005872 {
5873 /* negative number */
5874 --col;
5875 negative = TRUE;
5876 }
5877 /* get the number value (unsigned) */
5878 if (visual && VIsual_mode != 'V')
5879 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5880 ? (int)STRLEN(ptr) - col
5881 : length);
5882
5883 vim_str2nr(ptr + col, &pre, &length,
5884 0 + (dobin ? STR2NR_BIN : 0)
5885 + (dooct ? STR2NR_OCT : 0)
5886 + (dohex ? STR2NR_HEX : 0),
5887 NULL, &n, maxlen);
5888
5889 /* ignore leading '-' for hex and octal and bin numbers */
5890 if (pre && negative)
5891 {
5892 ++col;
5893 --length;
5894 negative = FALSE;
5895 }
5896 /* add or subtract */
5897 subtract = FALSE;
5898 if (op_type == OP_NR_SUB)
5899 subtract ^= TRUE;
5900 if (negative)
5901 subtract ^= TRUE;
5902
5903 oldn = n;
5904 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005905 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005906 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005907 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005908 /* handle wraparound for decimal numbers */
5909 if (!pre)
5910 {
5911 if (subtract)
5912 {
5913 if (n > oldn)
5914 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005915 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 negative ^= TRUE;
5917 }
5918 }
5919 else
5920 {
5921 /* add */
5922 if (n < oldn)
5923 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005924 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005925 negative ^= TRUE;
5926 }
5927 }
5928 if (n == 0)
5929 negative = FALSE;
5930 }
5931
5932 if (visual && !was_positive && !negative && col > 0)
5933 {
5934 /* need to remove the '-' */
5935 col--;
5936 length++;
5937 }
5938
5939 /*
5940 * Delete the old number.
5941 */
5942 curwin->w_cursor.col = col;
5943 if (!did_change)
5944 startpos = curwin->w_cursor;
5945 did_change = TRUE;
5946 todel = length;
5947 c = gchar_cursor();
5948 /*
5949 * Don't include the '-' in the length, only the length of the
5950 * part after it is kept the same.
5951 */
5952 if (c == '-')
5953 --length;
5954 while (todel-- > 0)
5955 {
5956 if (c < 0x100 && isalpha(c))
5957 {
5958 if (isupper(c))
5959 hexupper = TRUE;
5960 else
5961 hexupper = FALSE;
5962 }
5963 /* del_char() will mark line needing displaying */
5964 (void)del_char(FALSE);
5965 c = gchar_cursor();
5966 }
5967
5968 /*
5969 * Prepare the leading characters in buf1[].
5970 * When there are many leading zeros it could be very long.
5971 * Allocate a bit too much.
5972 */
5973 buf1 = alloc((unsigned)length + NUMBUFLEN);
5974 if (buf1 == NULL)
5975 goto theend;
5976 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005977 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005978 {
5979 *ptr++ = '-';
5980 }
5981 if (pre)
5982 {
5983 *ptr++ = '0';
5984 --length;
5985 }
5986 if (pre == 'b' || pre == 'B' ||
5987 pre == 'x' || pre == 'X')
5988 {
5989 *ptr++ = pre;
5990 --length;
5991 }
5992
5993 /*
5994 * Put the number characters in buf2[].
5995 */
5996 if (pre == 'b' || pre == 'B')
5997 {
5998 int i;
5999 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006000 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006001
6002 /* leading zeros */
6003 for (bit = bits; bit > 0; bit--)
6004 if ((n >> (bit - 1)) & 0x1) break;
6005
6006 for (i = 0; bit > 0; bit--)
6007 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6008
6009 buf2[i] = '\0';
6010 }
6011 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006012 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6013 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006014 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006015 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6016 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006017 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006018 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6019 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006020 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006021 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6022 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006023 length -= (int)STRLEN(buf2);
6024
6025 /*
6026 * Adjust number of zeros to the new number of digits, so the
6027 * total length of the number remains the same.
6028 * Don't do this when
6029 * the result may look like an octal number.
6030 */
6031 if (firstdigit == '0' && !(dooct && pre == 0))
6032 while (length-- > 0)
6033 *ptr++ = '0';
6034 *ptr = NUL;
6035 STRCAT(buf1, buf2);
6036 ins_str(buf1); /* insert the new number */
6037 vim_free(buf1);
6038 endpos = curwin->w_cursor;
6039 if (did_change && curwin->w_cursor.col)
6040 --curwin->w_cursor.col;
6041 }
6042
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006043 if (did_change)
6044 {
6045 /* set the '[ and '] marks */
6046 curbuf->b_op_start = startpos;
6047 curbuf->b_op_end = endpos;
6048 if (curbuf->b_op_end.col > 0)
6049 --curbuf->b_op_end.col;
6050 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006051
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006052theend:
6053 if (visual)
6054 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006055 else if (did_change)
6056 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006057
Bram Moolenaard79e5502016-01-10 22:13:02 +01006058 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059}
6060
6061#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006062
6063static yankreg_T *y_read_regs = NULL;
6064
6065#define REG_PREVIOUS 1
6066#define REG_EXEC 2
6067
6068/*
6069 * Prepare for reading viminfo registers when writing viminfo later.
6070 */
6071 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006072prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006073{
6074 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6075 * (int)sizeof(yankreg_T));
6076}
6077
6078 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006079finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006080{
6081 int i;
6082 int j;
6083
6084 if (y_read_regs != NULL)
6085 {
6086 for (i = 0; i < NUM_REGISTERS; ++i)
6087 if (y_read_regs[i].y_array != NULL)
6088 {
6089 for (j = 0; j < y_read_regs[i].y_size; j++)
6090 vim_free(y_read_regs[i].y_array[j]);
6091 vim_free(y_read_regs[i].y_array);
6092 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006093 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006094 }
6095}
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006098read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 int eof;
6101 int do_it = TRUE;
6102 int size;
6103 int limit;
6104 int i;
6105 int set_prev = FALSE;
6106 char_u *str;
6107 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006108 int new_type = MCHAR; /* init to shut up compiler */
6109 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110
6111 /* We only get here (hopefully) if line[0] == '"' */
6112 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006113
6114 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (*str == '"')
6116 {
6117 set_prev = TRUE;
6118 str++;
6119 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 if (!ASCII_ISALNUM(*str) && *str != '-')
6122 {
6123 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6124 return TRUE; /* too many errors, pretend end-of-file */
6125 do_it = FALSE;
6126 }
6127 get_yank_register(*str++, FALSE);
6128 if (!force && y_current->y_array != NULL)
6129 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006130
6131 if (*str == '@')
6132 {
6133 /* "x@: register x used for @@ */
6134 if (force || execreg_lastc == NUL)
6135 execreg_lastc = str[-1];
6136 }
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 size = 0;
6139 limit = 100; /* Optimized for registers containing <= 100 lines */
6140 if (do_it)
6141 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006142 /*
6143 * Build the new register in array[].
6144 * y_array is kept as-is until done.
6145 * The "do_it" flag is reset when something is wrong, in which case
6146 * array[] needs to be freed.
6147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (set_prev)
6149 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006150 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006151 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006153 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006155 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006157 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 /* get the block width; if it's missing we get a zero, which is OK */
6159 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006160 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
6162
6163 while (!(eof = viminfo_readline(virp))
6164 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6165 {
6166 if (do_it)
6167 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006168 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006170 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006172
6173 if (new_array == NULL)
6174 {
6175 do_it = FALSE;
6176 break;
6177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006179 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006181 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 str = viminfo_readstring(virp, 1, TRUE);
6185 if (str != NULL)
6186 array[size++] = str;
6187 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006188 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 do_it = FALSE;
6190 }
6191 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (do_it)
6194 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006195 /* free y_array[] */
6196 for (i = 0; i < y_current->y_size; i++)
6197 vim_free(y_current->y_array[i]);
6198 vim_free(y_current->y_array);
6199
6200 y_current->y_type = new_type;
6201 y_current->y_width = new_width;
6202 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006203 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 if (size == 0)
6205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 y_current->y_array = NULL;
6207 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006210 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 y_current->y_array =
6212 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6213 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006214 {
6215 if (y_current->y_array == NULL)
6216 vim_free(array[i]);
6217 else
6218 y_current->y_array[i] = array[i];
6219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006222 else
6223 {
6224 /* Free array[] if it was filled. */
6225 for (i = 0; i < size; i++)
6226 vim_free(array[i]);
6227 }
6228 vim_free(array);
6229
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return eof;
6231}
6232
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006233/*
6234 * Accept a new style register line from the viminfo, store it when it's new.
6235 */
6236 void
6237handle_viminfo_register(garray_T *values, int force)
6238{
6239 bval_T *vp = (bval_T *)values->ga_data;
6240 int flags;
6241 int name;
6242 int type;
6243 int linecount;
6244 int width;
6245 time_t timestamp;
6246 yankreg_T *y_ptr;
6247 int i;
6248
6249 /* Check the format:
6250 * |{bartype},{flags},{name},{type},
6251 * {linecount},{width},{timestamp},"line1","line2"
6252 */
6253 if (values->ga_len < 6
6254 || vp[0].bv_type != BVAL_NR
6255 || vp[1].bv_type != BVAL_NR
6256 || vp[2].bv_type != BVAL_NR
6257 || vp[3].bv_type != BVAL_NR
6258 || vp[4].bv_type != BVAL_NR
6259 || vp[5].bv_type != BVAL_NR)
6260 return;
6261 flags = vp[0].bv_nr;
6262 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006263 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006264 return;
6265 type = vp[2].bv_nr;
6266 if (type != MCHAR && type != MLINE && type != MBLOCK)
6267 return;
6268 linecount = vp[3].bv_nr;
6269 if (values->ga_len < 6 + linecount)
6270 return;
6271 width = vp[4].bv_nr;
6272 if (width < 0)
6273 return;
6274
6275 if (y_read_regs != NULL)
6276 /* Reading viminfo for merging and writing. Store the register
6277 * content, don't update the current registers. */
6278 y_ptr = &y_read_regs[name];
6279 else
6280 y_ptr = &y_regs[name];
6281
6282 /* Do not overwrite unless forced or the timestamp is newer. */
6283 timestamp = (time_t)vp[5].bv_nr;
6284 if (y_ptr->y_array != NULL && !force
6285 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6286 return;
6287
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006288 if (y_ptr->y_array != NULL)
6289 for (i = 0; i < y_ptr->y_size; i++)
6290 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006291 vim_free(y_ptr->y_array);
6292
6293 if (y_read_regs == NULL)
6294 {
6295 if (flags & REG_PREVIOUS)
6296 y_previous = y_ptr;
6297 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6298 execreg_lastc = get_register_name(name);
6299 }
6300 y_ptr->y_type = type;
6301 y_ptr->y_width = width;
6302 y_ptr->y_size = linecount;
6303 y_ptr->y_time_set = timestamp;
6304 if (linecount == 0)
6305 y_ptr->y_array = NULL;
6306 else
6307 {
6308 y_ptr->y_array =
6309 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6310 for (i = 0; i < linecount; i++)
6311 {
6312 if (vp[i + 6].bv_allocated)
6313 {
6314 y_ptr->y_array[i] = vp[i + 6].bv_string;
6315 vp[i + 6].bv_string = NULL;
6316 }
6317 else
6318 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6319 }
6320 }
6321}
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006326 int i, j;
6327 char_u *type;
6328 char_u c;
6329 int num_lines;
6330 int max_num_lines;
6331 int max_kbyte;
6332 long len;
6333 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaar64404472010-06-26 06:24:45 +02006335 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 /* Get '<' value, use old '"' value if '<' is not found. */
6338 max_num_lines = get_viminfo_parameter('<');
6339 if (max_num_lines < 0)
6340 max_num_lines = get_viminfo_parameter('"');
6341 if (max_num_lines == 0)
6342 return;
6343 max_kbyte = get_viminfo_parameter('s');
6344 if (max_kbyte == 0)
6345 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 for (i = 0; i < NUM_REGISTERS; i++)
6348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349#ifdef FEAT_CLIPBOARD
6350 /* Skip '*'/'+' register, we don't want them back next time */
6351 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6352 continue;
6353#endif
6354#ifdef FEAT_DND
6355 /* Neither do we want the '~' register */
6356 if (i == TILDE_REGISTER)
6357 continue;
6358#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006359 /* When reading viminfo for merging and writing: Use the register from
6360 * viminfo if it's newer. */
6361 if (y_read_regs != NULL
6362 && y_read_regs[i].y_array != NULL
6363 && (y_regs[i].y_array == NULL ||
6364 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6365 y_ptr = &y_read_regs[i];
6366 else if (y_regs[i].y_array == NULL)
6367 continue;
6368 else
6369 y_ptr = &y_regs[i];
6370
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006371 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006372 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006373 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006374 || (num_lines == 1 && y_ptr->y_type == MCHAR
6375 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006376 continue;
6377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 if (max_kbyte > 0)
6379 {
6380 /* Skip register if there is more text than the maximum size. */
6381 len = 0;
6382 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006383 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (len > (long)max_kbyte * 1024L)
6385 continue;
6386 }
6387
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006388 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
6390 case MLINE:
6391 type = (char_u *)"LINE";
6392 break;
6393 case MCHAR:
6394 type = (char_u *)"CHAR";
6395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 case MBLOCK:
6397 type = (char_u *)"BLOCK";
6398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 default:
6400 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006401 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 emsg(IObuff);
6403 type = (char_u *)"LINE";
6404 break;
6405 }
6406 if (y_previous == &y_regs[i])
6407 fprintf(fp, "\"");
6408 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006409 fprintf(fp, "\"%c", c);
6410 if (c == execreg_lastc)
6411 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006412 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 /* If max_num_lines < 0, then we save ALL the lines in the register */
6415 if (max_num_lines > 0 && num_lines > max_num_lines)
6416 num_lines = max_num_lines;
6417 for (j = 0; j < num_lines; j++)
6418 {
6419 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006420 viminfo_writestring(fp, y_ptr->y_array[j]);
6421 }
6422
6423 {
6424 int flags = 0;
6425 int remaining;
6426
6427 /* New style with a bar line. Format:
6428 * |{bartype},{flags},{name},{type},
6429 * {linecount},{width},{timestamp},"line1","line2"
6430 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006431 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006432 */
6433 if (y_previous == &y_regs[i])
6434 flags |= REG_PREVIOUS;
6435 if (c == execreg_lastc)
6436 flags |= REG_EXEC;
6437 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6438 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6439 (long)y_ptr->y_time_set);
6440 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6441 remaining = LSIZE - 71;
6442 for (j = 0; j < num_lines; j++)
6443 {
6444 putc(',', fp);
6445 --remaining;
6446 remaining = barline_writestring(fp, y_ptr->y_array[j],
6447 remaining);
6448 }
6449 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 }
6451 }
6452}
6453#endif /* FEAT_VIMINFO */
6454
6455#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6456/*
6457 * SELECTION / PRIMARY ('*')
6458 *
6459 * Text selection stuff that uses the GUI selection register '*'. When using a
6460 * GUI this may be text from another window, otherwise it is the last text we
6461 * had highlighted with VIsual mode. With mouse support, clicking the middle
6462 * button performs the paste, otherwise you will need to do <"*p>. "
6463 * If not under X, it is synonymous with the clipboard register '+'.
6464 *
6465 * X CLIPBOARD ('+')
6466 *
6467 * Text selection stuff that uses the GUI clipboard register '+'.
6468 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6469 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6470 * otherwise you will need to do <"+p>. "
6471 * If not under X, it is synonymous with the selection register '*'.
6472 */
6473
6474/*
6475 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006476 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 * only exist while the owning application exists, so we write to the
6478 * permanent (while X runs) store CUT_BUFFER0.
6479 * Dump the CLIPBOARD selection if we own it (it's logically the more
6480 * 'permanent' of the two), otherwise the PRIMARY one.
6481 * For now, use a hard-coded sanity limit of 1Mb of data.
6482 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006483#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006485x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 Display *dpy;
6488 char_u *str = NULL;
6489 long_u len = 0;
6490 int motion_type = -1;
6491
6492# ifdef FEAT_GUI
6493 if (gui.in_use)
6494 dpy = X_DISPLAY;
6495 else
6496# endif
6497# ifdef FEAT_XCLIPBOARD
6498 dpy = xterm_dpy;
6499# else
6500 return;
6501# endif
6502
6503 /* Get selection to export */
6504 if (clip_plus.owned)
6505 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6506 else if (clip_star.owned)
6507 motion_type = clip_convert_selection(&str, &len, &clip_star);
6508
6509 /* Check it's OK */
6510 if (dpy != NULL && str != NULL && motion_type >= 0
6511 && len < 1024*1024 && len > 0)
6512 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006513#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006514 int ok = TRUE;
6515
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006516 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6517 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6518 * encoding conversion usually doesn't work, so keep the text as-is.
6519 */
6520 if (has_mbyte)
6521 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006522 vimconv_T vc;
6523
6524 vc.vc_type = CONV_NONE;
6525 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6526 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006527 int intlen = len;
6528 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006529
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006530 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006531 conv_str = string_convert(&vc, str, &intlen);
6532 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006533 if (conv_str != NULL)
6534 {
6535 vim_free(str);
6536 str = conv_str;
6537 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006538 else
6539 {
6540 ok = FALSE;
6541 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006542 convert_setup(&vc, NULL, NULL);
6543 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006544 else
6545 {
6546 ok = FALSE;
6547 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006548 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006549
6550 /* Do not store the string if conversion failed. Better to use any
6551 * other selection than garbled text. */
6552 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006553#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006554 {
6555 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6556 XFlush(dpy);
6557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
6560 vim_free(str);
6561}
6562#endif
6563
6564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006565clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006567 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 if (cbd == &clip_plus)
6570 y_current = &y_regs[PLUS_REGISTER];
6571 else
6572 y_current = &y_regs[STAR_REGISTER];
6573 free_yank_all();
6574 y_current->y_size = 0;
6575 y_current = y_ptr;
6576}
6577
6578/*
6579 * Get the selected text and put it in the gui selection register '*' or '+'.
6580 */
6581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006584 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 pos_T old_visual;
6587 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 colnr_T old_curswant;
6589 int old_set_curswant;
6590 pos_T old_op_start, old_op_end;
6591 oparg_T oa;
6592 cmdarg_T ca;
6593
6594 if (cbd->owned)
6595 {
6596 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6597 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6598 return;
6599
6600 /* Get the text between clip_star.start & clip_star.end */
6601 old_y_previous = y_previous;
6602 old_y_current = y_current;
6603 old_cursor = curwin->w_cursor;
6604 old_curswant = curwin->w_curswant;
6605 old_set_curswant = curwin->w_set_curswant;
6606 old_op_start = curbuf->b_op_start;
6607 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 old_visual = VIsual;
6609 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 clear_oparg(&oa);
6611 oa.regname = (cbd == &clip_plus ? '+' : '*');
6612 oa.op_type = OP_YANK;
6613 vim_memset(&ca, 0, sizeof(ca));
6614 ca.oap = &oa;
6615 ca.cmdchar = 'y';
6616 ca.count1 = 1;
6617 ca.retval = CA_NO_ADJ_OP_END;
6618 do_pending_operator(&ca, 0, TRUE);
6619 y_previous = old_y_previous;
6620 y_current = old_y_current;
6621 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006622 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 curwin->w_curswant = old_curswant;
6624 curwin->w_set_curswant = old_set_curswant;
6625 curbuf->b_op_start = old_op_start;
6626 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 VIsual = old_visual;
6628 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006630 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 {
6632 clip_free_selection(cbd);
6633
6634 /* Try to get selected text from another window */
6635 clip_gen_request_selection(cbd);
6636 }
6637}
6638
Bram Moolenaard44347f2011-06-19 01:14:29 +02006639/*
6640 * Convert from the GUI selection string into the '*'/'+' register.
6641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006643clip_yank_selection(
6644 int type,
6645 char_u *str,
6646 long len,
6647 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006649 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 if (cbd == &clip_plus)
6652 y_ptr = &y_regs[PLUS_REGISTER];
6653 else
6654 y_ptr = &y_regs[STAR_REGISTER];
6655
6656 clip_free_selection(cbd);
6657
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006658 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659}
6660
6661/*
6662 * Convert the '*'/'+' register into a GUI selection string returned in *str
6663 * with length *len.
6664 * Returns the motion type, or -1 for failure.
6665 */
6666 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006667clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 char_u *p;
6670 int lnum;
6671 int i, j;
6672 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006673 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 if (cbd == &clip_plus)
6676 y_ptr = &y_regs[PLUS_REGISTER];
6677 else
6678 y_ptr = &y_regs[STAR_REGISTER];
6679
6680#ifdef USE_CRNL
6681 eolsize = 2;
6682#else
6683 eolsize = 1;
6684#endif
6685
6686 *str = NULL;
6687 *len = 0;
6688 if (y_ptr->y_array == NULL)
6689 return -1;
6690
6691 for (i = 0; i < y_ptr->y_size; i++)
6692 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6693
6694 /*
6695 * Don't want newline character at end of last line if we're in MCHAR mode.
6696 */
6697 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6698 *len -= eolsize;
6699
6700 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6701 if (p == NULL)
6702 return -1;
6703 lnum = 0;
6704 for (i = 0, j = 0; i < (int)*len; i++, j++)
6705 {
6706 if (y_ptr->y_array[lnum][j] == '\n')
6707 p[i] = NUL;
6708 else if (y_ptr->y_array[lnum][j] == NUL)
6709 {
6710#ifdef USE_CRNL
6711 p[i++] = '\r';
6712#endif
6713#ifdef USE_CR
6714 p[i] = '\r';
6715#else
6716 p[i] = '\n';
6717#endif
6718 lnum++;
6719 j = -1;
6720 }
6721 else
6722 p[i] = y_ptr->y_array[lnum][j];
6723 }
6724 return y_ptr->y_type;
6725}
6726
6727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728/*
6729 * If we have written to a clipboard register, send the text to the clipboard.
6730 */
6731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6735 {
6736 clip_own_selection(&clip_star);
6737 clip_gen_set_selection(&clip_star);
6738 }
6739 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6740 {
6741 clip_own_selection(&clip_plus);
6742 clip_gen_set_selection(&clip_plus);
6743 }
6744}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
6746#endif /* FEAT_CLIPBOARD || PROTO */
6747
6748
6749#if defined(FEAT_DND) || defined(PROTO)
6750/*
6751 * Replace the contents of the '~' register with str.
6752 */
6753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006754dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006756 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 curr = y_current;
6759 y_current = &y_regs[TILDE_REGISTER];
6760 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006761 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 y_current = curr;
6763}
6764#endif
6765
6766
6767#if defined(FEAT_EVAL) || defined(PROTO)
6768/*
6769 * Return the type of a register.
6770 * Used for getregtype()
6771 * Returns MAUTO for error.
6772 */
6773 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 switch (regname)
6777 {
6778 case '%': /* file name */
6779 case '#': /* alternate file name */
6780 case '=': /* expression */
6781 case ':': /* last command line */
6782 case '/': /* last search-pattern */
6783 case '.': /* last inserted text */
6784#ifdef FEAT_SEARCHPATH
6785 case Ctrl_F: /* Filename under cursor */
6786 case Ctrl_P: /* Path under cursor, expand via "path" */
6787#endif
6788 case Ctrl_W: /* word under cursor */
6789 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6790 case '_': /* black hole: always empty */
6791 return MCHAR;
6792 }
6793
6794#ifdef FEAT_CLIPBOARD
6795 regname = may_get_selection(regname);
6796#endif
6797
Bram Moolenaar32b92012014-01-14 12:33:36 +01006798 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006799 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 get_yank_register(regname, FALSE);
6802
6803 if (y_current->y_array != NULL)
6804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (reglen != NULL && y_current->y_type == MBLOCK)
6806 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return y_current->y_type;
6808 }
6809 return MAUTO;
6810}
6811
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006812static char_u *getreg_wrap_one_line(char_u *s, int flags);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006813
6814/*
6815 * When "flags" has GREG_LIST return a list with text "s".
6816 * Otherwise just return "s".
6817 */
6818 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006819getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006820{
6821 if (flags & GREG_LIST)
6822 {
6823 list_T *list = list_alloc();
6824
6825 if (list != NULL)
6826 {
6827 if (list_append_string(list, NULL, -1) == FAIL)
6828 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006829 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006830 return NULL;
6831 }
6832 list->lv_first->li_tv.vval.v_string = s;
6833 }
6834 return (char_u *)list;
6835 }
6836 return s;
6837}
6838
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839/*
6840 * Return the contents of a register as a single allocated string.
6841 * Used for "@r" in expressions and for getreg().
6842 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006843 * Flags:
6844 * GREG_NO_EXPR Do not allow expression register
6845 * GREG_EXPR_SRC For the expression register: return expression itself,
6846 * not the result of its evaluation.
6847 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 */
6849 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 long i;
6853 char_u *retval;
6854 int allocated;
6855 long len;
6856
6857 /* Don't allow using an expression register inside an expression */
6858 if (regname == '=')
6859 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006860 if (flags & GREG_NO_EXPR)
6861 return NULL;
6862 if (flags & GREG_EXPR_SRC)
6863 return getreg_wrap_one_line(get_expr_line_src(), flags);
6864 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866
6867 if (regname == '@') /* "@@" is used for unnamed register */
6868 regname = '"';
6869
6870 /* check for valid regname */
6871 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6872 return NULL;
6873
6874#ifdef FEAT_CLIPBOARD
6875 regname = may_get_selection(regname);
6876#endif
6877
6878 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6879 {
6880 if (retval == NULL)
6881 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006882 if (allocated)
6883 return getreg_wrap_one_line(retval, flags);
6884 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886
6887 get_yank_register(regname, FALSE);
6888 if (y_current->y_array == NULL)
6889 return NULL;
6890
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006891 if (flags & GREG_LIST)
6892 {
6893 list_T *list = list_alloc();
6894 int error = FALSE;
6895
6896 if (list == NULL)
6897 return NULL;
6898 for (i = 0; i < y_current->y_size; ++i)
6899 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6900 error = TRUE;
6901 if (error)
6902 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006903 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006904 return NULL;
6905 }
6906 return (char_u *)list;
6907 }
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 /*
6910 * Compute length of resulting string.
6911 */
6912 len = 0;
6913 for (i = 0; i < y_current->y_size; ++i)
6914 {
6915 len += (long)STRLEN(y_current->y_array[i]);
6916 /*
6917 * Insert a newline between lines and after last line if
6918 * y_type is MLINE.
6919 */
6920 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6921 ++len;
6922 }
6923
6924 retval = lalloc(len + 1, TRUE);
6925
6926 /*
6927 * Copy the lines of the yank register into the string.
6928 */
6929 if (retval != NULL)
6930 {
6931 len = 0;
6932 for (i = 0; i < y_current->y_size; ++i)
6933 {
6934 STRCPY(retval + len, y_current->y_array[i]);
6935 len += (long)STRLEN(retval + len);
6936
6937 /*
6938 * Insert a NL between lines and after the last line if y_type is
6939 * MLINE.
6940 */
6941 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6942 retval[len++] = '\n';
6943 }
6944 retval[len] = NUL;
6945 }
6946
6947 return retval;
6948}
6949
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006950 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006952 int name,
6953 yankreg_T **old_y_previous,
6954 yankreg_T **old_y_current,
6955 int must_append,
6956 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006957{
6958 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6959 {
6960 emsg_invreg(name);
6961 return FAIL;
6962 }
6963
6964 /* Don't want to change the current (unnamed) register */
6965 *old_y_previous = y_previous;
6966 *old_y_current = y_current;
6967
6968 get_yank_register(name, TRUE);
6969 if (!y_append && !must_append)
6970 free_yank_all();
6971 return OK;
6972}
6973
6974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006975finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006976 int name,
6977 yankreg_T *old_y_previous,
6978 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006979{
6980# ifdef FEAT_CLIPBOARD
6981 /* Send text of clipboard register to the clipboard. */
6982 may_set_selection();
6983# endif
6984
6985 /* ':let @" = "val"' should change the meaning of the "" register */
6986 if (name != '"')
6987 y_previous = old_y_previous;
6988 y_current = old_y_current;
6989}
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991/*
6992 * Store string "str" in register "name".
6993 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6994 * If "must_append" is TRUE, always append to the register. Otherwise append
6995 * if "name" is an uppercase letter.
6996 * Note: "maxlen" and "must_append" don't work for the "/" register.
6997 * Careful: 'str' is modified, you may have to use a copy!
6998 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6999 */
7000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007001write_reg_contents(
7002 int name,
7003 char_u *str,
7004 int maxlen,
7005 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7008}
7009
7010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007011write_reg_contents_lst(
7012 int name,
7013 char_u **strings,
7014 int maxlen UNUSED,
7015 int must_append,
7016 int yank_type,
7017 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007018{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007019 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007020
7021 if (name == '/'
7022#ifdef FEAT_EVAL
7023 || name == '='
7024#endif
7025 )
7026 {
7027 char_u *s;
7028
7029 if (strings[0] == NULL)
7030 s = (char_u *)"";
7031 else if (strings[1] != NULL)
7032 {
7033 EMSG(_("E883: search pattern and expression register may not "
7034 "contain two or more lines"));
7035 return;
7036 }
7037 else
7038 s = strings[0];
7039 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7040 return;
7041 }
7042
7043 if (name == '_') /* black hole: nothing to do */
7044 return;
7045
7046 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7047 &yank_type) == FAIL)
7048 return;
7049
7050 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7051
7052 finish_write_reg(name, old_y_previous, old_y_current);
7053}
7054
7055 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007056write_reg_contents_ex(
7057 int name,
7058 char_u *str,
7059 int maxlen,
7060 int must_append,
7061 int yank_type,
7062 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007064 yankreg_T *old_y_previous, *old_y_current;
7065 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
Bram Moolenaare7566042005-06-17 22:00:15 +00007067 if (maxlen >= 0)
7068 len = maxlen;
7069 else
7070 len = (long)STRLEN(str);
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 /* Special case: '/' search pattern */
7073 if (name == '/')
7074 {
7075 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7076 return;
7077 }
7078
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007079 if (name == '#')
7080 {
7081 buf_T *buf;
7082
7083 if (VIM_ISDIGIT(*str))
7084 {
7085 int num = atoi((char *)str);
7086
7087 buf = buflist_findnr(num);
7088 if (buf == NULL)
7089 EMSGN(_(e_nobufnr), (long)num);
7090 }
7091 else
7092 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7093 TRUE, FALSE, FALSE));
7094 if (buf == NULL)
7095 return;
7096 curwin->w_alt_fnum = buf->b_fnum;
7097 return;
7098 }
7099
Bram Moolenaare7566042005-06-17 22:00:15 +00007100#ifdef FEAT_EVAL
7101 if (name == '=')
7102 {
7103 char_u *p, *s;
7104
7105 p = vim_strnsave(str, (int)len);
7106 if (p == NULL)
7107 return;
7108 if (must_append)
7109 {
7110 s = concat_str(get_expr_line_src(), p);
7111 vim_free(p);
7112 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007113 }
7114 set_expr_line(p);
7115 return;
7116 }
7117#endif
7118
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 if (name == '_') /* black hole: nothing to do */
7120 return;
7121
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007122 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7123 &yank_type) == FAIL)
7124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007126 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007128 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129}
7130#endif /* FEAT_EVAL */
7131
7132#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7133/*
7134 * Put a string into a register. When the register is not empty, the string
7135 * is appended.
7136 */
7137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007139 yankreg_T *y_ptr, /* pointer to yank register */
7140 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7141 char_u *str, /* string to put in register */
7142 long len, /* length of string */
7143 long blocklen, /* width of Visual block */
7144 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007146 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 int lnum;
7148 long start;
7149 long i;
7150 int extra;
7151 int newlines; /* number of lines added */
7152 int extraline = 0; /* extra line at the end */
7153 int append = FALSE; /* append to last line in register */
7154 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007155 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007159 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 y_ptr->y_size = 0;
7161
Bram Moolenaard44347f2011-06-19 01:14:29 +02007162 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007163 type = ((str_list || (len > 0 && (str[len - 1] == NL
7164 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007165 ? MLINE : MCHAR);
7166 else
7167 type = yank_type;
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 /*
7170 * Count the number of lines within the string
7171 */
7172 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007173 if (str_list)
7174 {
7175 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007178 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007180 for (i = 0; i < len; i++)
7181 if (str[i] == '\n')
7182 ++newlines;
7183 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7184 {
7185 extraline = 1;
7186 ++newlines; /* count extra newline at the end */
7187 }
7188 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7189 {
7190 append = TRUE;
7191 --newlines; /* uncount newline when appending first line */
7192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007195 /* Without any lines make the register empty. */
7196 if (y_ptr->y_size + newlines == 0)
7197 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007198 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007199 return;
7200 }
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /*
7203 * Allocate an array to hold the pointers to the new register lines.
7204 * If the register was not empty, move the existing lines to the new array.
7205 */
7206 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7207 * sizeof(char_u *), TRUE);
7208 if (pp == NULL) /* out of memory */
7209 return;
7210 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7211 pp[lnum] = y_ptr->y_array[lnum];
7212 vim_free(y_ptr->y_array);
7213 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 /*
7217 * Find the end of each line and save it into the array.
7218 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007219 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007221 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7222 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007223 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007224 pp[lnum] = vim_strnsave(*ss, i);
7225 if (i > maxlen)
7226 maxlen = i;
7227 }
7228 }
7229 else
7230 {
7231 for (start = 0; start < len + extraline; start += i + 1)
7232 {
7233 for (i = start; i < len; ++i) /* find the end of the line */
7234 if (str[i] == '\n')
7235 break;
7236 i -= start; /* i is now length of line */
7237 if (i > maxlen)
7238 maxlen = i;
7239 if (append)
7240 {
7241 --lnum;
7242 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7243 }
7244 else
7245 extra = 0;
7246 s = alloc((unsigned)(i + extra + 1));
7247 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007249 if (extra)
7250 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7251 if (append)
7252 vim_free(y_ptr->y_array[lnum]);
7253 if (i)
7254 mch_memmove(s + extra, str + start, (size_t)i);
7255 extra += i;
7256 s[extra] = NUL;
7257 y_ptr->y_array[lnum++] = s;
7258 while (--extra >= 0)
7259 {
7260 if (*s == NUL)
7261 *s = '\n'; /* replace NUL with newline */
7262 ++s;
7263 }
7264 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 }
7267 y_ptr->y_type = type;
7268 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (type == MBLOCK)
7270 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7271 else
7272 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007273#ifdef FEAT_VIMINFO
7274 y_ptr->y_time_set = vim_time();
7275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276}
7277#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7278
7279 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007280clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281{
7282 vim_memset(oap, 0, sizeof(oparg_T));
7283}
7284
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007285static 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 +00007286
7287/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007288 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 *
7290 * "Words" are counted by looking for boundaries between non-space and
7291 * space characters. (it seems to produce results that match 'wc'.)
7292 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007293 * Return value is byte count; word count for the line is added to "*wc".
7294 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 *
7296 * The function will only examine the first "limit" characters in the
7297 * line, stopping if it encounters an end-of-line (NUL byte). In that
7298 * case, eol_size will be added to the character count to account for
7299 * the size of the EOL character.
7300 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007301 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302line_count_info(
7303 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007304 varnumber_T *wc,
7305 varnumber_T *cc,
7306 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007307 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007309 varnumber_T i;
7310 varnumber_T words = 0;
7311 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 int is_word = 0;
7313
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007314 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
7316 if (is_word)
7317 {
7318 if (vim_isspace(line[i]))
7319 {
7320 words++;
7321 is_word = 0;
7322 }
7323 }
7324 else if (!vim_isspace(line[i]))
7325 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007326 ++chars;
7327#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007328 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007329#else
7330 ++i;
7331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 }
7333
7334 if (is_word)
7335 words++;
7336 *wc += words;
7337
7338 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007339 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007342 chars += eol_size;
7343 }
7344 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 return i;
7346}
7347
7348/*
7349 * Give some info about the position of the cursor (for "g CTRL-G").
7350 * In Visual mode, give some info about the selected region. (In this case,
7351 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007352 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 */
7354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007355cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007358 char_u buf1[50];
7359 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007361 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007362#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007363 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007364#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007365 varnumber_T byte_count_cursor = 0;
7366 varnumber_T char_count = 0;
7367 varnumber_T char_count_cursor = 0;
7368 varnumber_T word_count = 0;
7369 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007370 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007371 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 long line_count_selected = 0;
7373 pos_T min_pos, max_pos;
7374 oparg_T oparg;
7375 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
7377 /*
7378 * Compute the length of the file in characters.
7379 */
7380 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7381 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007382 if (dict == NULL)
7383 {
7384 MSG(_(no_lines_msg));
7385 return;
7386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
7388 else
7389 {
7390 if (get_fileformat(curbuf) == EOL_DOS)
7391 eol_size = 2;
7392 else
7393 eol_size = 1;
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 if (VIsual_active)
7396 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007397 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 min_pos = VIsual;
7400 max_pos = curwin->w_cursor;
7401 }
7402 else
7403 {
7404 min_pos = curwin->w_cursor;
7405 max_pos = VIsual;
7406 }
7407 if (*p_sel == 'e' && max_pos.col > 0)
7408 --max_pos.col;
7409
7410 if (VIsual_mode == Ctrl_V)
7411 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007412#ifdef FEAT_LINEBREAK
7413 char_u * saved_sbr = p_sbr;
7414
7415 /* Make 'sbr' empty for a moment to get the correct size. */
7416 p_sbr = empty_option;
7417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 oparg.is_VIsual = 1;
7419 oparg.block_mode = TRUE;
7420 oparg.op_type = OP_NOP;
7421 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007422 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007423#ifdef FEAT_LINEBREAK
7424 p_sbr = saved_sbr;
7425#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007426 if (curwin->w_curswant == MAXCOL)
7427 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 /* Swap the start, end vcol if needed */
7429 if (oparg.end_vcol < oparg.start_vcol)
7430 {
7431 oparg.end_vcol += oparg.start_vcol;
7432 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7433 oparg.end_vcol -= oparg.start_vcol;
7434 }
7435 }
7436 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
7439 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7440 {
7441 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007442 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
7444 ui_breakcheck();
7445 if (got_int)
7446 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007447 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 /* Do extra processing for VIsual mode. */
7451 if (VIsual_active
7452 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7453 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007454 char_u *s = NULL;
7455 long len = 0L;
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 switch (VIsual_mode)
7458 {
7459 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007460#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007464#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007466#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007467 s = bd.textstart;
7468 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 break;
7470 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007471 s = ml_get(lnum);
7472 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 break;
7474 case 'v':
7475 {
7476 colnr_T start_col = (lnum == min_pos.lnum)
7477 ? min_pos.col : 0;
7478 colnr_T end_col = (lnum == max_pos.lnum)
7479 ? max_pos.col - start_col + 1 : MAXCOL;
7480
Bram Moolenaardef9e822004-12-31 20:58:58 +00007481 s = ml_get(lnum) + start_col;
7482 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 break;
7485 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007486 if (s != NULL)
7487 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007488 byte_count_cursor += line_count_info(s, &word_count_cursor,
7489 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007490 if (lnum == curbuf->b_ml.ml_line_count
7491 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007492 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007493 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007494 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 }
7497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
7499 /* In non-visual mode, check for the line the cursor is on */
7500 if (lnum == curwin->w_cursor.lnum)
7501 {
7502 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007503 char_count_cursor += char_count;
7504 byte_count_cursor = byte_count +
7505 line_count_info(ml_get(lnum),
7506 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007507 (varnumber_T)(curwin->w_cursor.col + 1),
7508 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
7511 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007512 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007513 &char_count, (varnumber_T)MAXCOL,
7514 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516
7517 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007518 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007519 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
Bram Moolenaared767a22016-01-03 22:49:16 +01007521 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007523 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007525 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7526 {
7527 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7528 &max_pos.col);
7529 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7530 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7531 }
7532 else
7533 buf1[0] = NUL;
7534
7535 if (char_count_cursor == byte_count_cursor
7536 && char_count == byte_count)
7537 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007538 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007539 buf1, line_count_selected,
7540 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007541 (long long)word_count_cursor,
7542 (long long)word_count,
7543 (long long)byte_count_cursor,
7544 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007545 else
7546 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007547 _("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 +01007548 buf1, line_count_selected,
7549 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007550 (long long)word_count_cursor,
7551 (long long)word_count,
7552 (long long)char_count_cursor,
7553 (long long)char_count,
7554 (long long)byte_count_cursor,
7555 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007558 {
7559 p = ml_get_curline();
7560 validate_virtcol();
7561 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7562 (int)curwin->w_virtcol + 1);
7563 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7564 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
Bram Moolenaared767a22016-01-03 22:49:16 +01007566 if (char_count_cursor == byte_count_cursor
7567 && char_count == byte_count)
7568 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007569 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007570 (char *)buf1, (char *)buf2,
7571 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007573 (long long)word_count_cursor, (long long)word_count,
7574 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007575 else
7576 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007577 _("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 +01007578 (char *)buf1, (char *)buf2,
7579 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007580 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007581 (long long)word_count_cursor, (long long)word_count,
7582 (long long)char_count_cursor, (long long)char_count,
7583 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007584 }
7585 }
7586
Bram Moolenaared767a22016-01-03 22:49:16 +01007587#ifdef FEAT_MBYTE
7588 bom_count = bomb_size();
7589 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007590 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007591 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007592#endif
7593 if (dict == NULL)
7594 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007595 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007596 p = p_shm;
7597 p_shm = (char_u *)"";
7598 msg(IObuff);
7599 p_shm = p;
7600 }
7601 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007602#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007603 if (dict != NULL)
7604 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007605 dict_add_nr_str(dict, "words", word_count, NULL);
7606 dict_add_nr_str(dict, "chars", char_count, NULL);
7607 dict_add_nr_str(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007608# ifdef FEAT_MBYTE
7609 + bom_count
7610# endif
7611 , NULL);
7612 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007613 byte_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007614 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007615 char_count_cursor, NULL);
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007616 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007617 word_count_cursor, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620}