blob: e3df9b3a6017b8d63cf6cd63e7e18caeb871dc2c [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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 int is_short; /* TRUE if line is too short to fit in block */
86 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
87 int is_oneChar; /* TRUE if block within one character */
88 int pre_whitesp; /* screen cols of ws before block */
89 int pre_whitesp_c; /* chars of ws before block */
90 colnr_T end_char_vcols; /* number of vcols of post-block char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 colnr_T start_char_vcols; /* number of vcols of pre-block char */
92};
93
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010094static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static int stuff_yank(int, char_u *);
96static void put_reedit_in_typebuf(int silent);
97static int put_in_typebuf(char_u *s, int esc, int colon,
98 int silent);
99static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static void free_yank_all(void);
104static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200106static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100107static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
111static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200113static 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 +0000114#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121
Bram Moolenaarf2732452018-06-03 14:47:35 +0200122// Flags for third item in "opchars".
123#define OPF_LINES 1 // operator always works on lines
124#define OPF_CHANGE 2 // operator changes text
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +0200129 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 */
131static char opchars[][3] =
132{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200133 {NUL, NUL, 0}, // OP_NOP
134 {'d', NUL, OPF_CHANGE}, // OP_DELETE
135 {'y', NUL, 0}, // OP_YANK
136 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
137 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
138 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
139 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
140 {'g', '~', OPF_CHANGE}, // OP_TILDE
141 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
142 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
143 {':', NUL, OPF_LINES}, // OP_COLON
144 {'g', 'U', OPF_CHANGE}, // OP_UPPER
145 {'g', 'u', OPF_CHANGE}, // OP_LOWER
146 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
147 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
148 {'g', '?', OPF_CHANGE}, // OP_ROT13
149 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
150 {'I', NUL, OPF_CHANGE}, // OP_INSERT
151 {'A', NUL, OPF_CHANGE}, // OP_APPEND
152 {'z', 'f', OPF_LINES}, // OP_FOLD
153 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
154 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
155 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
156 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
157 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
158 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
159 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
160 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
161 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
162 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163};
164
165/*
166 * Translate a command name into an operator type.
167 * Must only be called with a valid operator name!
168 */
169 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100170get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100178 if (char1 == 'g' && char2 == Ctrl_A) /* add */
179 return OP_NR_ADD;
180 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
181 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (opchars[i][0] == char1 && opchars[i][1] == char2)
185 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100186 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
187 {
188 internal_error("get_op_type()");
189 break;
190 }
191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 return i;
193}
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195/*
196 * Return TRUE if operator "op" always works on whole lines.
197 */
198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100199op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200201 return opchars[op][2] & OPF_LINES;
202}
203
204/*
205 * Return TRUE if operator "op" changes text.
206 */
207 int
208op_is_change(int op)
209{
210 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213/*
214 * Get first operator command character.
215 * Returns 'g' or 'z' if there is another command character.
216 */
217 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100218get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219{
220 return opchars[optype][0];
221}
222
223/*
224 * Get second operator command character.
225 */
226 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100227get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 return opchars[optype][1];
230}
231
232/*
233 * op_shift - handle a shift operation
234 */
235 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100236op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 long i;
239 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
242 if (u_save((linenr_T)(oap->start.lnum - 1),
243 (linenr_T)(oap->end.lnum + 1)) == FAIL)
244 return;
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 if (oap->block_mode)
247 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249 for (i = oap->line_count; --i >= 0; )
250 {
251 first_char = *ml_get_curline();
252 if (first_char == NUL) /* empty line */
253 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 else if (oap->block_mode)
255 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 else
257 /* Move the line right if it doesn't start with '#', 'smartindent'
258 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
259#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
260 if (first_char != '#' || !preprocs_left())
261#endif
262 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000263 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265 ++curwin->w_cursor.lnum;
266 }
267
268 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 if (oap->block_mode)
270 {
271 curwin->w_cursor.lnum = oap->start.lnum;
272 curwin->w_cursor.col = block_col;
273 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100274 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 {
276 curwin->w_cursor.lnum = oap->start.lnum;
277 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
278 }
279 else
280 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
281
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100282#ifdef FEAT_FOLDING
283 /* The cursor line is not in a closed fold */
284 foldOpenCursor();
285#endif
286
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (oap->line_count > p_report)
289 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200290 char *op;
291 char *msg_line_single;
292 char *msg_line_plural;
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200295 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200297 op = "<";
298 msg_line_single = NGETTEXT("%ld line %sed %d time",
299 "%ld line %sed %d times", amount);
300 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
301 "%ld lines %sed %d times", amount);
302 vim_snprintf((char *)IObuff, IOSIZE,
303 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
304 oap->line_count, op, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 msg(IObuff);
306 }
307
308 /*
309 * Set "'[" and "']" marks.
310 */
311 curbuf->b_op_start = oap->start;
312 curbuf->b_op_end.lnum = oap->end.lnum;
313 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
314 if (curbuf->b_op_end.col > 0)
315 --curbuf->b_op_end.col;
316}
317
318/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100319 * Shift the current line one shiftwidth left (if left != 0) or right
320 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 */
322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100323shift_line(
324 int left,
325 int round,
326 int amount,
327 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328{
329 int count;
330 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100331 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
333 count = get_indent(); /* get current indent */
334
335 if (round) /* round off indent */
336 {
337 i = count / p_sw; /* number of p_sw rounded down */
338 j = count % p_sw; /* extra spaces */
339 if (j && left) /* first remove extra spaces */
340 --amount;
341 if (left)
342 {
343 i -= amount;
344 if (i < 0)
345 i = 0;
346 }
347 else
348 i += amount;
349 count = i * p_sw;
350 }
351 else /* original vi indent */
352 {
353 if (left)
354 {
355 count -= p_sw * amount;
356 if (count < 0)
357 count = 0;
358 }
359 else
360 count += p_sw * amount;
361 }
362
363 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000365 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000367 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368}
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370/*
371 * Shift one line of the current block one shiftwidth right or left.
372 * Leaves cursor on first character in block.
373 */
374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100375shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376{
377 int left = (oap->op_type == OP_LSHIFT);
378 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000379 int total;
380 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100382 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200383#ifdef FEAT_VARTABS
384 int *p_vts = curbuf->b_p_vts_array;
385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 int p_ts = (int)curbuf->b_p_ts;
387 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000389 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int i = 0, j = 0;
391 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_RIGHTLEFT
393 int old_p_ri = p_ri;
394
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200395 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
397
398 State = INSERT; /* don't want REPLACE for State */
399 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
400 if (bd.is_short)
401 return;
402
403 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200404 total = (int)((unsigned)amount * (unsigned)p_sw);
405 if ((total / p_sw) != amount)
406 return; /* multiplication overflow */
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 oldp = ml_get_curline();
409
410 if (!left)
411 {
412 /*
413 * 1. Get start vcol
414 * 2. Total ws vcols
415 * 3. Divvy into TABs & spp
416 * 4. Construct new string
417 */
418 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
419 ws_vcol = bd.start_vcol - bd.pre_whitesp;
420 if (bd.startspaces)
421 {
422#ifdef FEAT_MBYTE
423 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100424 {
425 if ((*mb_ptr2len)(bd.textstart) == 1)
426 ++bd.textstart;
427 else
428 {
429 ws_vcol = 0;
430 bd.startspaces = 0;
431 }
432 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000435 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100437 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200439 /* TODO: is passing bd.textstart for start of the line OK? */
440 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
441 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 total += incr;
443 bd.start_vcol += incr;
444 }
445 /* OK, now total=all the VWS reqd, and textstart points at the 1st
446 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200447#ifdef FEAT_VARTABS
448 if (!curbuf->b_p_et)
449 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
450 else
451 j = total;
452#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (!curbuf->b_p_et)
454 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
455 if (i)
456 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
457 else
458 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 /* if we're splitting a TAB, allow for it */
461 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
462 len = (int)STRLEN(bd.textstart) + 1;
463 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
464 if (newp == NULL)
465 return;
466 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
467 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200468 vim_memset(newp + bd.textcol, TAB, (size_t)i);
469 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 /* the end */
471 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
472 }
473 else /* left */
474 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000475 colnr_T destination_col; /* column to which text in block will
476 be shifted */
477 char_u *verbatim_copy_end; /* end of the part of the line which is
478 copied verbatim */
479 colnr_T verbatim_copy_width;/* the (displayed) width of this part
480 of line */
481 unsigned fill; /* nr of spaces that replace a TAB */
482 unsigned new_line_len; /* the length of the line after the
483 block shift */
484 size_t block_space_width;
485 size_t shift_amount;
486 char_u *non_white = bd.textstart;
487 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 /*
490 * Firstly, let's find the first non-whitespace character that is
491 * displayed after the block's start column and the character's column
492 * number. Also, let's calculate the width of all the whitespace
493 * characters that are displayed in the block and precede the searched
494 * non-whitespace character.
495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000497 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
498 * the part of which is displayed at the block's beginning. Let's start
499 * searching from the next character. */
500 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100501 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* The character's column is in "bd.start_vcol". */
504 non_white_col = bd.start_vcol;
505
Bram Moolenaar1c465442017-03-12 20:10:05 +0100506 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200508 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000509 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000512 block_space_width = non_white_col - oap->start_vcol;
513 /* We will shift by "total" or "block_space_width", whichever is less.
514 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000515 shift_amount = (block_space_width < (size_t)total
516 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000517
518 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000519 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520
521 /* Now let's find out how much of the beginning of the line we can
522 * reuse without modification. */
523 verbatim_copy_end = bd.textstart;
524 verbatim_copy_width = bd.start_vcol;
525
526 /* If "bd.startspaces" is set, "bd.textstart" points to the character
527 * preceding the block. We have to subtract its width to obtain its
528 * column number. */
529 if (bd.startspaces)
530 verbatim_copy_width -= bd.start_char_vcols;
531 while (verbatim_copy_width < destination_col)
532 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200533 char_u *line = verbatim_copy_end;
534
535 /* TODO: is passing verbatim_copy_end for start of the line OK? */
536 incr = lbr_chartabsize(line, verbatim_copy_end,
537 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000538 if (verbatim_copy_width + incr > destination_col)
539 break;
540 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100541 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 }
543
544 /* If "destination_col" is different from the width of the initial
545 * part of the line that will be copied, it means we encountered a tab
546 * character, which we will have to partly replace with spaces. */
547 fill = destination_col - verbatim_copy_width;
548
549 /* The replacement line will consist of:
550 * - the beginning of the original line up to "verbatim_copy_end",
551 * - "fill" number of spaces,
552 * - the rest of the line, pointed to by non_white. */
553 new_line_len = (unsigned)(verbatim_copy_end - oldp)
554 + fill
555 + (unsigned)STRLEN(non_white) + 1;
556
557 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (newp == NULL)
559 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000560 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200561 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000562 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564 /* replace the line */
565 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
566 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
567 State = oldstate;
568 curwin->w_cursor.col = oldcol;
569#ifdef FEAT_RIGHTLEFT
570 p_ri = old_p_ri;
571#endif
572}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574/*
575 * Insert string "s" (b_insert ? before : after) block :AKelly
576 * Caller must prepare for undo.
577 */
578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100579block_insert(
580 oparg_T *oap,
581 char_u *s,
582 int b_insert,
583 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
585 int p_ts;
586 int count = 0; /* extra spaces to replace a cut TAB */
587 int spaces = 0; /* non-zero if cutting a TAB */
588 colnr_T offset; /* pointer along new line */
589 unsigned s_len; /* STRLEN(s) */
590 char_u *newp, *oldp; /* new, old lines */
591 linenr_T lnum; /* loop var */
592 int oldstate = State;
593
594 State = INSERT; /* don't want REPLACE for State */
595 s_len = (unsigned)STRLEN(s);
596
597 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
598 {
599 block_prep(oap, bdp, lnum, TRUE);
600 if (bdp->is_short && b_insert)
601 continue; /* OP_INSERT, line ends before block start */
602
603 oldp = ml_get(lnum);
604
605 if (b_insert)
606 {
607 p_ts = bdp->start_char_vcols;
608 spaces = bdp->startspaces;
609 if (spaces != 0)
610 count = p_ts - 1; /* we're cutting a TAB */
611 offset = bdp->textcol;
612 }
613 else /* append */
614 {
615 p_ts = bdp->end_char_vcols;
616 if (!bdp->is_short) /* spaces = padding after block */
617 {
618 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
619 if (spaces != 0)
620 count = p_ts - 1; /* we're cutting a TAB */
621 offset = bdp->textcol + bdp->textlen - (spaces != 0);
622 }
623 else /* spaces = padding to block edge */
624 {
625 /* if $ used, just append to EOL (ie spaces==0) */
626 if (!bdp->is_MAX)
627 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
628 count = spaces;
629 offset = bdp->textcol + bdp->textlen;
630 }
631 }
632
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200633#ifdef FEAT_MBYTE
634 if (has_mbyte && spaces > 0)
635 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100636 int off;
637
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200638 /* Avoid starting halfway a multi-byte character. */
639 if (b_insert)
640 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100641 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200642 }
643 else
644 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100645 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200646 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200647 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100648 spaces -= off;
649 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200650 }
651#endif
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
654 if (newp == NULL)
655 continue;
656
657 /* copy up to shifted part */
658 mch_memmove(newp, oldp, (size_t)(offset));
659 oldp += offset;
660
661 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200662 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 /* copy the new text */
665 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
666 offset += s_len;
667
668 if (spaces && !bdp->is_short)
669 {
670 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200671 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 /* We're splitting a TAB, don't copy it. */
673 oldp++;
674 /* We allowed for that TAB, remember this now */
675 count++;
676 }
677
678 if (spaces > 0)
679 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000680 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 ml_replace(lnum, newp, FALSE);
683
684 if (lnum == oap->end.lnum)
685 {
686 /* Set "']" mark to the end of the block instead of the end of
687 * the insert in the first line. */
688 curbuf->b_op_end.lnum = oap->end.lnum;
689 curbuf->b_op_end.col = offset;
690 }
691 } /* for all lnum */
692
693 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
694
695 State = oldstate;
696}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
699/*
700 * op_reindent - handle reindenting a block of lines.
701 */
702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100703op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
705 long i;
706 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200707 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 linenr_T first_changed = 0;
709 linenr_T last_changed = 0;
710 linenr_T start_lnum = curwin->w_cursor.lnum;
711
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000712 /* Don't even try when 'modifiable' is off. */
713 if (!curbuf->b_p_ma)
714 {
715 EMSG(_(e_modifiable));
716 return;
717 }
718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 for (i = oap->line_count; --i >= 0 && !got_int; )
720 {
721 /* it's a slow thing to do, so give feedback so there's no worry that
722 * the computer's just hung. */
723
724 if (i > 1
725 && (i % 50 == 0 || i == oap->line_count - 1)
726 && oap->line_count > p_report)
727 smsg((char_u *)_("%ld lines to indent... "), i);
728
729 /*
730 * Be vi-compatible: For lisp indenting the first line is not
731 * indented, unless there is only one line.
732 */
733#ifdef FEAT_LISP
734 if (i != oap->line_count - 1 || oap->line_count == 1
735 || how != get_lisp_indent)
736#endif
737 {
738 l = skipwhite(ml_get_curline());
739 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200740 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200742 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200744 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 /* did change the indent, call changed_lines() later */
747 if (first_changed == 0)
748 first_changed = curwin->w_cursor.lnum;
749 last_changed = curwin->w_cursor.lnum;
750 }
751 }
752 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000753 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 }
755
756 /* put cursor on first non-blank of indented line */
757 curwin->w_cursor.lnum = start_lnum;
758 beginline(BL_SOL | BL_FIX);
759
760 /* Mark changed lines so that they will be redrawn. When Visual
761 * highlighting was present, need to continue until the last line. When
762 * there is no change still need to remove the Visual highlighting. */
763 if (last_changed != 0)
764 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 else if (oap->is_VIsual)
768 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 if (oap->line_count > p_report)
771 {
772 i = oap->line_count - (i + 1);
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200773 smsg((char_u *)NGETTEXT("%ld line indented ",
774 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 }
776 /* set '[ and '] marks */
777 curbuf->b_op_start = oap->start;
778 curbuf->b_op_end = oap->end;
779}
780#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
781
782#if defined(FEAT_EVAL) || defined(PROTO)
783/*
784 * Keep the last expression line here, for repeating.
785 */
786static char_u *expr_line = NULL;
787
788/*
789 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
790 * Returns '=' when OK, NUL otherwise.
791 */
792 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 char_u *new_line;
796
797 new_line = getcmdline('=', 0L, 0);
798 if (new_line == NULL)
799 return NUL;
800 if (*new_line == NUL) /* use previous line */
801 vim_free(new_line);
802 else
803 set_expr_line(new_line);
804 return '=';
805}
806
807/*
808 * Set the expression for the '=' register.
809 * Argument must be an allocated string.
810 */
811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100812set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 vim_free(expr_line);
815 expr_line = new_line;
816}
817
818/*
819 * Get the result of the '=' register expression.
820 * Returns a pointer to allocated memory, or NULL for failure.
821 */
822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100823get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 char_u *expr_copy;
826 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000827 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
829 if (expr_line == NULL)
830 return NULL;
831
832 /* Make a copy of the expression, because evaluating it may cause it to be
833 * changed. */
834 expr_copy = vim_strsave(expr_line);
835 if (expr_copy == NULL)
836 return NULL;
837
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000838 /* When we are invoked recursively limit the evaluation to 10 levels.
839 * Then return the string as-is. */
840 if (nested >= 10)
841 return expr_copy;
842
843 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000844 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000845 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 vim_free(expr_copy);
847 return rv;
848}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000849
850/*
851 * Get the '=' register expression itself, without evaluating it.
852 */
853 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100854get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000855{
856 if (expr_line == NULL)
857 return NULL;
858 return vim_strsave(expr_line);
859}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif /* FEAT_EVAL */
861
862/*
863 * Check if 'regname' is a valid name of a yank register.
864 * Note: There is no check for 0 (default register), caller should do this
865 */
866 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100867valid_yank_reg(
868 int regname,
869 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 if ( (regname > 0 && ASCII_ISALNUM(regname))
872 || (!writing && vim_strchr((char_u *)
873#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100874 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100876 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
878 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100879 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 || regname == '"'
881 || regname == '-'
882 || regname == '_'
883#ifdef FEAT_CLIPBOARD
884 || regname == '*'
885 || regname == '+'
886#endif
887#ifdef FEAT_DND
888 || (!writing && regname == '~')
889#endif
890 )
891 return TRUE;
892 return FALSE;
893}
894
895/*
896 * Set y_current and y_append, according to the value of "regname".
897 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000898 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *
900 * If regname is 0 and writing, use register 0
901 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100902 *
903 * Return TRUE when the register should be inserted literally (selection or
904 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100907get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
909 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100910 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912 y_append = FALSE;
913 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
914 {
915 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100916 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918 i = regname;
919 if (VIM_ISDIGIT(i))
920 i -= '0';
921 else if (ASCII_ISLOWER(i))
922 i = CharOrdLow(i) + 10;
923 else if (ASCII_ISUPPER(i))
924 {
925 i = CharOrdUp(i) + 10;
926 y_append = TRUE;
927 }
928 else if (regname == '-')
929 i = DELETION_REGISTER;
930#ifdef FEAT_CLIPBOARD
931 /* When selection is not available, use register 0 instead of '*' */
932 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100935 ret = TRUE;
936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 /* When clipboard is not available, use register 0 instead of '+' */
938 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100941 ret = TRUE;
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944#ifdef FEAT_DND
945 else if (!writing && regname == '~')
946 i = TILDE_REGISTER;
947#endif
948 else /* not 0-9, a-z, A-Z or '-': use register 0 */
949 i = 0;
950 y_current = &(y_regs[i]);
951 if (writing) /* remember the register we write into for do_put() */
952 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100953 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
Bram Moolenaar8299df92004-07-10 09:47:34 +0000956#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957/*
958 * When "regname" is a clipboard register, obtain the selection. If it's not
959 * available return zero, otherwise return "regname".
960 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100962may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 if (regname == '*')
965 {
966 if (!clip_star.available)
967 regname = 0;
968 else
969 clip_get_selection(&clip_star);
970 }
971 else if (regname == '+')
972 {
973 if (!clip_plus.available)
974 regname = 0;
975 else
976 clip_get_selection(&clip_plus);
977 }
978 return regname;
979}
980#endif
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Obtain the contents of a "normal" register. The register is made empty.
984 * The returned pointer has allocated memory, use put_register() later.
985 */
986 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100987get_register(
988 int name,
989 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200991 yankreg_T *reg;
992 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994#ifdef FEAT_CLIPBOARD
995 /* When Visual area changed, may have to update selection. Obtain the
996 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000997 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200999 if (clip_isautosel_star())
1000 clip_update_selection(&clip_star);
1001 may_get_selection(name);
1002 }
1003 if (name == '+' && clip_plus.available)
1004 {
1005 if (clip_isautosel_plus())
1006 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 may_get_selection(name);
1008 }
1009#endif
1010
1011 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001012 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (reg != NULL)
1014 {
1015 *reg = *y_current;
1016 if (copy)
1017 {
1018 /* If we run out of memory some or all of the lines are empty. */
1019 if (reg->y_size == 0)
1020 reg->y_array = NULL;
1021 else
1022 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1023 * reg->y_size));
1024 if (reg->y_array != NULL)
1025 {
1026 for (i = 0; i < reg->y_size; ++i)
1027 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1028 }
1029 }
1030 else
1031 y_current->y_array = NULL;
1032 }
1033 return (void *)reg;
1034}
1035
1036/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001037 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 */
1039 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001040put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 get_yank_register(name, 0);
1043 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001044 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001045 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001047#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /* Send text written to clipboard register to the clipboard. */
1049 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001052
1053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001054free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001055{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001056 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001057
1058 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001059 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001060 free_yank_all();
1061 vim_free(reg);
1062 *y_current = tmp;
1063}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065#if defined(FEAT_MOUSE) || defined(PROTO)
1066/*
1067 * return TRUE if the current yank register has type MLINE
1068 */
1069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1073 return FALSE;
1074 if (regname == '_') /* black hole is always empty */
1075 return FALSE;
1076 get_yank_register(regname, FALSE);
1077 return (y_current->y_type == MLINE);
1078}
1079#endif
1080
1081/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001082 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001084 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
1086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001087do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaard55de222007-05-06 13:38:48 +00001089 char_u *p;
1090 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001091 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001094 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001096 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1098 retval = FAIL;
1099 else
1100 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001101 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 showmode();
1103 regname = c;
1104 retval = OK;
1105 }
1106 }
1107 else /* stop recording */
1108 {
1109 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001110 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1111 * needs to be removed again to put it in a register. exec_reg then
1112 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001114 reg_recording = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 MSG("");
1116 p = get_recorded();
1117 if (p == NULL)
1118 retval = FAIL;
1119 else
1120 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001121 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1122 vim_unescape_csi(p);
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 /*
1125 * We don't want to change the default register here, so save and
1126 * restore the current register name.
1127 */
1128 old_y_previous = y_previous;
1129 old_y_current = y_current;
1130
1131 retval = stuff_yank(regname, p);
1132
1133 y_previous = old_y_previous;
1134 y_current = old_y_current;
1135 }
1136 }
1137 return retval;
1138}
1139
1140/*
1141 * Stuff string "p" into yank register "regname" as a single line (append if
1142 * uppercase). "p" must have been alloced.
1143 *
1144 * return FAIL for failure, OK otherwise
1145 */
1146 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001147stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149 char_u *lp;
1150 char_u **pp;
1151
1152 /* check for read-only register */
1153 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1154 {
1155 vim_free(p);
1156 return FAIL;
1157 }
1158 if (regname == '_') /* black hole: don't do anything */
1159 {
1160 vim_free(p);
1161 return OK;
1162 }
1163 get_yank_register(regname, TRUE);
1164 if (y_append && y_current->y_array != NULL)
1165 {
1166 pp = &(y_current->y_array[y_current->y_size - 1]);
1167 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1168 if (lp == NULL)
1169 {
1170 vim_free(p);
1171 return FAIL;
1172 }
1173 STRCPY(lp, *pp);
1174 STRCAT(lp, p);
1175 vim_free(p);
1176 vim_free(*pp);
1177 *pp = lp;
1178 }
1179 else
1180 {
1181 free_yank_all();
1182 if ((y_current->y_array =
1183 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1184 {
1185 vim_free(p);
1186 return FAIL;
1187 }
1188 y_current->y_array[0] = p;
1189 y_current->y_size = 1;
1190 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001191#ifdef FEAT_VIMINFO
1192 y_current->y_time_set = vim_time();
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 return OK;
1196}
1197
Bram Moolenaar42b94362009-05-26 16:12:37 +00001198static int execreg_lastc = NUL;
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001201 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001203 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 */
1205 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001206do_execreg(
1207 int regname,
1208 int colon, /* insert ':' before each line */
1209 int addcr, /* always add '\n' to end of line */
1210 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 long i;
1213 char_u *p;
1214 int retval = OK;
1215 int remap;
1216
1217 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001218 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001219 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001220 {
1221 EMSG(_("E748: No previously used register"));
1222 return FAIL;
1223 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001224 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* check for valid regname */
1227 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001228 {
1229 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001231 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001232 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234#ifdef FEAT_CLIPBOARD
1235 regname = may_get_selection(regname);
1236#endif
1237
1238 if (regname == '_') /* black hole: don't stuff anything */
1239 return OK;
1240
1241#ifdef FEAT_CMDHIST
1242 if (regname == ':') /* use last command line */
1243 {
1244 if (last_cmdline == NULL)
1245 {
1246 EMSG(_(e_nolastcmd));
1247 return FAIL;
1248 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001249 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001250 /* Escape all control characters with a CTRL-V */
1251 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001252 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001253 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001254 {
1255 /* When in Visual mode "'<,'>" will be prepended to the command.
1256 * Remove it when it's already there. */
1257 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001258 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001259 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001260 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001261 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001262 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264#endif
1265#ifdef FEAT_EVAL
1266 else if (regname == '=')
1267 {
1268 p = get_expr_line();
1269 if (p == NULL)
1270 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 vim_free(p);
1273 }
1274#endif
1275 else if (regname == '.') /* use last inserted text */
1276 {
1277 p = get_last_insert_save();
1278 if (p == NULL)
1279 {
1280 EMSG(_(e_noinstext));
1281 return FAIL;
1282 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(p);
1285 }
1286 else
1287 {
1288 get_yank_register(regname, FALSE);
1289 if (y_current->y_array == NULL)
1290 return FAIL;
1291
1292 /* Disallow remaping for ":@r". */
1293 remap = colon ? REMAP_NONE : REMAP_YES;
1294
1295 /*
1296 * Insert lines into typeahead buffer, from last one to first one.
1297 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001298 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 for (i = y_current->y_size; --i >= 0; )
1300 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001301 char_u *escaped;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /* insert NL between lines and after last line if type is MLINE */
1304 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1305 || addcr)
1306 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 return FAIL;
1309 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001310 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1311 if (escaped == NULL)
1312 return FAIL;
1313 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1314 vim_free(escaped);
1315 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001317 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 == FAIL)
1319 return FAIL;
1320 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001321 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 return retval;
1324}
1325
1326/*
1327 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1328 * used only after other typeahead has been processed.
1329 */
1330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001331put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 char_u buf[3];
1334
1335 if (restart_edit != NUL)
1336 {
1337 if (restart_edit == 'V')
1338 {
1339 buf[0] = 'g';
1340 buf[1] = 'R';
1341 buf[2] = NUL;
1342 }
1343 else
1344 {
1345 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1346 buf[1] = NUL;
1347 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001348 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 restart_edit = NUL;
1350 }
1351}
1352
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353/*
1354 * Insert register contents "s" into the typeahead buffer, so that it will be
1355 * executed again.
1356 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1357 * no remapping.
1358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360put_in_typebuf(
1361 char_u *s,
1362 int esc,
1363 int colon, /* add ':' before the line */
1364 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int retval = OK;
1367
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001368 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001372 {
1373 char_u *p;
1374
1375 if (esc)
1376 p = vim_strsave_escape_csi(s);
1377 else
1378 p = s;
1379 if (p == NULL)
1380 retval = FAIL;
1381 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001382 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1383 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001384 if (esc)
1385 vim_free(p);
1386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001388 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return retval;
1390}
1391
1392/*
1393 * Insert a yank register: copy it into the Read buffer.
1394 * Used by CTRL-R command and middle mouse button in insert mode.
1395 *
1396 * return FAIL for failure, OK otherwise
1397 */
1398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001399insert_reg(
1400 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001401 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 long i;
1404 int retval = OK;
1405 char_u *arg;
1406 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001407 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 /*
1410 * It is possible to get into an endless loop by having CTRL-R a in
1411 * register a and then, in insert mode, doing CTRL-R a.
1412 * If you hit CTRL-C, the loop will be broken here.
1413 */
1414 ui_breakcheck();
1415 if (got_int)
1416 return FAIL;
1417
1418 /* check for valid regname */
1419 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1420 return FAIL;
1421
1422#ifdef FEAT_CLIPBOARD
1423 regname = may_get_selection(regname);
1424#endif
1425
1426 if (regname == '.') /* insert last inserted text */
1427 retval = stuff_inserted(NUL, 1L, TRUE);
1428 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1429 {
1430 if (arg == NULL)
1431 return FAIL;
1432 stuffescaped(arg, literally);
1433 if (allocated)
1434 vim_free(arg);
1435 }
1436 else /* name or number register */
1437 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001438 if (get_yank_register(regname, FALSE))
1439 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (y_current->y_array == NULL)
1441 retval = FAIL;
1442 else
1443 {
1444 for (i = 0; i < y_current->y_size; ++i)
1445 {
1446 stuffescaped(y_current->y_array[i], literally);
1447 /*
1448 * Insert a newline between lines and after last line if
1449 * y_type is MLINE.
1450 */
1451 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1452 stuffcharReadbuff('\n');
1453 }
1454 }
1455 }
1456
1457 return retval;
1458}
1459
1460/*
1461 * Stuff a string into the typeahead buffer, such that edit() will insert it
1462 * literally ("literally" TRUE) or interpret is as typed characters.
1463 */
1464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001465stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 int c;
1468 char_u *start;
1469
1470 while (*arg != NUL)
1471 {
1472 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1473 * stuff K_SPECIAL to get the effect of a special key when "literally"
1474 * is TRUE. */
1475 start = arg;
1476 while ((*arg >= ' '
1477#ifndef EBCDIC
1478 && *arg < DEL /* EBCDIC: chars above space are normal */
1479#endif
1480 )
1481 || (*arg == K_SPECIAL && !literally))
1482 ++arg;
1483 if (arg > start)
1484 stuffReadbuffLen(start, (long)(arg - start));
1485
1486 /* stuff a single special character */
1487 if (*arg != NUL)
1488 {
1489#ifdef FEAT_MBYTE
1490 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001491 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 else
1493#endif
1494 c = *arg++;
1495 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1496 stuffcharReadbuff(Ctrl_V);
1497 stuffcharReadbuff(c);
1498 }
1499 }
1500}
1501
1502/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001503 * If "regname" is a special register, return TRUE and store a pointer to its
1504 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001507get_spec_reg(
1508 int regname,
1509 char_u **argp,
1510 int *allocated, /* return: TRUE when value was allocated */
1511 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
1513 int cnt;
1514
1515 *argp = NULL;
1516 *allocated = FALSE;
1517 switch (regname)
1518 {
1519 case '%': /* file name */
1520 if (errmsg)
1521 check_fname(); /* will give emsg if not set */
1522 *argp = curbuf->b_fname;
1523 return TRUE;
1524
1525 case '#': /* alternate file name */
1526 *argp = getaltfname(errmsg); /* may give emsg if not set */
1527 return TRUE;
1528
1529#ifdef FEAT_EVAL
1530 case '=': /* result of expression */
1531 *argp = get_expr_line();
1532 *allocated = TRUE;
1533 return TRUE;
1534#endif
1535
1536 case ':': /* last command line */
1537 if (last_cmdline == NULL && errmsg)
1538 EMSG(_(e_nolastcmd));
1539 *argp = last_cmdline;
1540 return TRUE;
1541
1542 case '/': /* last search-pattern */
1543 if (last_search_pat() == NULL && errmsg)
1544 EMSG(_(e_noprevre));
1545 *argp = last_search_pat();
1546 return TRUE;
1547
1548 case '.': /* last inserted text */
1549 *argp = get_last_insert_save();
1550 *allocated = TRUE;
1551 if (*argp == NULL && errmsg)
1552 EMSG(_(e_noinstext));
1553 return TRUE;
1554
1555#ifdef FEAT_SEARCHPATH
1556 case Ctrl_F: /* Filename under cursor */
1557 case Ctrl_P: /* Path under cursor, expand via "path" */
1558 if (!errmsg)
1559 return FALSE;
1560 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001561 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 *allocated = TRUE;
1563 return TRUE;
1564#endif
1565
1566 case Ctrl_W: /* word under cursor */
1567 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1568 if (!errmsg)
1569 return FALSE;
1570 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1571 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1572 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1573 *allocated = TRUE;
1574 return TRUE;
1575
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001576 case Ctrl_L: /* Line under cursor */
1577 if (!errmsg)
1578 return FALSE;
1579
1580 *argp = ml_get_buf(curwin->w_buffer,
1581 curwin->w_cursor.lnum, FALSE);
1582 return TRUE;
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 case '_': /* black hole: always empty */
1585 *argp = (char_u *)"";
1586 return TRUE;
1587 }
1588
1589 return FALSE;
1590}
1591
1592/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001593 * Paste a yank register into the command line.
1594 * Only for non-special registers.
1595 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * insert_reg() can't be used here, because special characters from the
1597 * register contents will be interpreted as commands.
1598 *
1599 * return FAIL for failure, OK otherwise
1600 */
1601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602cmdline_paste_reg(
1603 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001604 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001605 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001608 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001610 if (get_yank_register(regname, FALSE))
1611 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (y_current->y_array == NULL)
1613 return FAIL;
1614
1615 for (i = 0; i < y_current->y_size; ++i)
1616 {
1617 cmdline_paste_str(y_current->y_array[i], literally);
1618
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001619 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001620 * Don't do this when "remcr" is TRUE. */
1621 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 cmdline_paste_str((char_u *)"\r", literally);
1623
1624 /* Check for CTRL-C, in case someone tries to paste a few thousand
1625 * lines and gets bored. */
1626 ui_breakcheck();
1627 if (got_int)
1628 return FAIL;
1629 }
1630 return OK;
1631}
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1634/*
1635 * Adjust the register name pointed to with "rp" for the clipboard being
1636 * used always and the clipboard being available.
1637 */
1638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001639adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001641 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1642 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001643 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1644 {
1645 if (clip_unnamed != 0)
1646 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001647 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001648 else
1649 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1650 ? '+' : '*';
1651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (!clip_star.available && *rp == '*')
1653 *rp = 0;
1654 if (!clip_plus.available && *rp == '+')
1655 *rp = 0;
1656}
1657#endif
1658
1659/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001660 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1661 */
1662 void
1663shift_delete_registers()
1664{
1665 int n;
1666
1667 y_current = &y_regs[9];
1668 free_yank_all(); /* free register nine */
1669 for (n = 9; n > 1; --n)
1670 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001671 y_current = &y_regs[1];
1672 if (!y_append)
1673 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001674 y_regs[1].y_array = NULL; /* set register one to empty */
1675}
1676
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001678 static void
1679yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1680{
1681 static int recursive = FALSE;
1682 dict_T *v_event;
1683 list_T *list;
1684 int n;
1685 char_u buf[NUMBUFLEN + 2];
1686 long reglen = 0;
1687
1688 if (recursive)
1689 return;
1690
1691 v_event = get_vim_var_dict(VV_EVENT);
1692
1693 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001694 if (list == NULL)
1695 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001696 for (n = 0; n < reg->y_size; n++)
1697 list_append_string(list, reg->y_array[n], -1);
1698 list->lv_lock = VAR_FIXED;
1699 dict_add_list(v_event, "regcontents", list);
1700
1701 buf[0] = (char_u)oap->regname;
1702 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001703 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001704
1705 buf[0] = get_op_char(oap->op_type);
1706 buf[1] = get_extra_op_char(oap->op_type);
1707 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001708 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001709
1710 buf[0] = NUL;
1711 buf[1] = NUL;
1712 switch (get_reg_type(oap->regname, &reglen))
1713 {
1714 case MLINE: buf[0] = 'V'; break;
1715 case MCHAR: buf[0] = 'v'; break;
1716 case MBLOCK:
1717 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1718 reglen + 1);
1719 break;
1720 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001721 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001722
1723 /* Lock the dictionary and its keys */
1724 dict_set_items_ro(v_event);
1725
1726 recursive = TRUE;
1727 textlock++;
1728 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1729 textlock--;
1730 recursive = FALSE;
1731
1732 /* Empty the dictionary, v:event is still valid */
1733 dict_free_contents(v_event);
1734 hash_init(&v_event->dv_hashtab);
1735}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001736#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001737
Bram Moolenaara1891842017-02-04 21:34:31 +01001738/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001739 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001741 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 */
1743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001744op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int n;
1747 linenr_T lnum;
1748 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 char_u *newp, *oldp;
1750 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1752 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001753 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1756 return OK;
1757
1758 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1759 if (oap->empty)
1760 return u_save_cursor();
1761
1762 if (!curbuf->b_p_ma)
1763 {
1764 EMSG(_(e_modifiable));
1765 return FAIL;
1766 }
1767
1768#ifdef FEAT_CLIPBOARD
1769 adjust_clip_reg(&oap->regname);
1770#endif
1771
1772#ifdef FEAT_MBYTE
1773 if (has_mbyte)
1774 mb_adjust_opend(oap);
1775#endif
1776
Bram Moolenaard04b7502010-07-08 22:27:55 +02001777 /*
1778 * Imitate the strange Vi behaviour: If the delete spans more than one
1779 * line and motion_type == MCHAR and the result is a blank line, make the
1780 * delete linewise. Don't do this for the change command or Visual mode.
1781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001784 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001786 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 && oap->op_type == OP_DELETE)
1788 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001789 ptr = ml_get(oap->end.lnum) + oap->end.col;
1790 if (*ptr != NUL)
1791 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ptr = skipwhite(ptr);
1793 if (*ptr == NUL && inindent(0))
1794 oap->motion_type = MLINE;
1795 }
1796
Bram Moolenaard04b7502010-07-08 22:27:55 +02001797 /*
1798 * Check for trying to delete (e.g. "D") in an empty line.
1799 * Note: For the change operator it is ok.
1800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if ( oap->motion_type == MCHAR
1802 && oap->line_count == 1
1803 && oap->op_type == OP_DELETE
1804 && *ml_get(oap->start.lnum) == NUL)
1805 {
1806 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001807 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 * 'cpoptions' (Vi compatible).
1809 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001810#ifdef FEAT_VIRTUALEDIT
1811 if (virtual_op)
1812 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1813 * marks as if it happened. */
1814 goto setmarks;
1815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1817 beep_flush();
1818 return OK;
1819 }
1820
Bram Moolenaard04b7502010-07-08 22:27:55 +02001821 /*
1822 * Do a yank of whatever we're about to delete.
1823 * If a yank register was specified, put the deleted text into that
1824 * register. For the black hole register '_' don't yank anything.
1825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (oap->regname != '_')
1827 {
1828 if (oap->regname != 0)
1829 {
1830 /* check for read-only register */
1831 if (!valid_yank_reg(oap->regname, TRUE))
1832 {
1833 beep_flush();
1834 return OK;
1835 }
1836 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1837 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1838 did_yank = TRUE;
1839 }
1840
1841 /*
1842 * Put deleted text into register 1 and shift number registers if the
1843 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001844 * Use the register name from before adjust_clip_reg() may have
1845 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001847 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 || oap->line_count > 1 || oap->use_reg_one)
1849 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001850 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (op_yank(oap, TRUE, FALSE) == OK)
1852 did_yank = TRUE;
1853 }
1854
Bram Moolenaar84298db2012-04-20 13:46:08 +02001855 /* Yank into small delete register when no named register specified
1856 * and the delete is within one line. */
1857 if ((
1858#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001859 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1860 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001861#endif
1862 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && oap->line_count == 1)
1864 {
1865 oap->regname = '-';
1866 get_yank_register(oap->regname, TRUE);
1867 if (op_yank(oap, TRUE, FALSE) == OK)
1868 did_yank = TRUE;
1869 oap->regname = 0;
1870 }
1871
1872 /*
1873 * If there's too much stuff to fit in the yank register, then get a
1874 * confirmation before doing the delete. This is crude, but simple.
1875 * And it avoids doing a delete of something we can't put back if we
1876 * want.
1877 */
1878 if (!did_yank)
1879 {
1880 int msg_silent_save = msg_silent;
1881
1882 msg_silent = 0; /* must display the prompt */
1883 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1884 msg_silent = msg_silent_save;
1885 if (n != 'y')
1886 {
1887 EMSG(_(e_abort));
1888 return FAIL;
1889 }
1890 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001891
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001892#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001893 if (did_yank && has_textyankpost())
1894 yank_do_autocmd(oap, y_current);
1895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897
Bram Moolenaard04b7502010-07-08 22:27:55 +02001898 /*
1899 * block mode delete
1900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (oap->block_mode)
1902 {
1903 if (u_save((linenr_T)(oap->start.lnum - 1),
1904 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1905 return FAIL;
1906
1907 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1908 {
1909 block_prep(oap, &bd, lnum, TRUE);
1910 if (bd.textlen == 0) /* nothing to delete */
1911 continue;
1912
1913 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1914 if (lnum == curwin->w_cursor.lnum)
1915 {
1916 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1917# ifdef FEAT_VIRTUALEDIT
1918 curwin->w_cursor.coladd = 0;
1919# endif
1920 }
1921
1922 /* n == number of chars deleted
1923 * If we delete a TAB, it may be replaced by several characters.
1924 * Thus the number of characters may increase!
1925 */
1926 n = bd.textlen - bd.startspaces - bd.endspaces;
1927 oldp = ml_get(lnum);
1928 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1929 if (newp == NULL)
1930 continue;
1931 /* copy up to deleted part */
1932 mch_memmove(newp, oldp, (size_t)bd.textcol);
1933 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001934 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 (size_t)(bd.startspaces + bd.endspaces));
1936 /* copy the part after the deleted part */
1937 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001938 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 /* replace the line */
1940 ml_replace(lnum, newp, FALSE);
1941 }
1942
1943 check_cursor_col();
1944 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1945 oap->end.lnum + 1, 0L);
1946 oap->line_count = 0; /* no lines deleted */
1947 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001948 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 if (oap->op_type == OP_CHANGE)
1951 {
1952 /* Delete the lines except the first one. Temporarily move the
1953 * cursor to the next line. Save the current line number, if the
1954 * last line is deleted it may be changed.
1955 */
1956 if (oap->line_count > 1)
1957 {
1958 lnum = curwin->w_cursor.lnum;
1959 ++curwin->w_cursor.lnum;
1960 del_lines((long)(oap->line_count - 1), TRUE);
1961 curwin->w_cursor.lnum = lnum;
1962 }
1963 if (u_save_cursor() == FAIL)
1964 return FAIL;
1965 if (curbuf->b_p_ai) /* don't delete indent */
1966 {
1967 beginline(BL_WHITE); /* cursor on first non-white */
1968 did_ai = TRUE; /* delete the indent when ESC hit */
1969 ai_col = curwin->w_cursor.col;
1970 }
1971 else
1972 beginline(0); /* cursor in column 0 */
1973 truncate_line(FALSE); /* delete the rest of the line */
1974 /* leave cursor past last char in line */
1975 if (oap->line_count > 1)
1976 u_clearline(); /* "U" command not possible after "2cc" */
1977 }
1978 else
1979 {
1980 del_lines(oap->line_count, TRUE);
1981 beginline(BL_WHITE | BL_FIX);
1982 u_clearline(); /* "U" command not possible after "dd" */
1983 }
1984 }
1985 else
1986 {
1987#ifdef FEAT_VIRTUALEDIT
1988 if (virtual_op)
1989 {
1990 int endcol = 0;
1991
1992 /* For virtualedit: break the tabs that are partly included. */
1993 if (gchar_pos(&oap->start) == '\t')
1994 {
1995 if (u_save_cursor() == FAIL) /* save first line for undo */
1996 return FAIL;
1997 if (oap->line_count == 1)
1998 endcol = getviscol2(oap->end.col, oap->end.coladd);
1999 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2000 oap->start = curwin->w_cursor;
2001 if (oap->line_count == 1)
2002 {
2003 coladvance(endcol);
2004 oap->end.col = curwin->w_cursor.col;
2005 oap->end.coladd = curwin->w_cursor.coladd;
2006 curwin->w_cursor = oap->start;
2007 }
2008 }
2009
2010 /* Break a tab only when it's included in the area. */
2011 if (gchar_pos(&oap->end) == '\t'
2012 && (int)oap->end.coladd < oap->inclusive)
2013 {
2014 /* save last line for undo */
2015 if (u_save((linenr_T)(oap->end.lnum - 1),
2016 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2017 return FAIL;
2018 curwin->w_cursor = oap->end;
2019 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2020 oap->end = curwin->w_cursor;
2021 curwin->w_cursor = oap->start;
2022 }
2023 }
2024#endif
2025
2026 if (oap->line_count == 1) /* delete characters within one line */
2027 {
2028 if (u_save_cursor() == FAIL) /* save line for undo */
2029 return FAIL;
2030
2031 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002032 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 && oap->op_type == OP_CHANGE
2034 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002035 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 display_dollar(oap->end.col - !oap->inclusive);
2037
2038 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2039
2040#ifdef FEAT_VIRTUALEDIT
2041 if (virtual_op)
2042 {
2043 /* fix up things for virtualedit-delete:
2044 * break the tabs which are going to get in our way
2045 */
2046 char_u *curline = ml_get_curline();
2047 int len = (int)STRLEN(curline);
2048
2049 if (oap->end.coladd != 0
2050 && (int)oap->end.col >= len - 1
2051 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2052 n++;
2053 /* Delete at least one char (e.g, when on a control char). */
2054 if (n == 0 && oap->start.coladd != oap->end.coladd)
2055 n = 1;
2056
2057 /* When deleted a char in the line, reset coladd. */
2058 if (gchar_cursor() != NUL)
2059 curwin->w_cursor.coladd = 0;
2060 }
2061#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002062 (void)del_bytes((long)n, !virtual_op,
2063 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 else /* delete characters between lines */
2066 {
2067 pos_T curpos;
2068
2069 /* save deleted and changed lines for undo */
2070 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2071 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2072 return FAIL;
2073
2074 truncate_line(TRUE); /* delete from cursor to end of line */
2075
2076 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2077 ++curwin->w_cursor.lnum;
2078 del_lines((long)(oap->line_count - 2), FALSE);
2079
Bram Moolenaard009e862015-06-09 20:20:03 +02002080 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002081 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002082 curwin->w_cursor.col = 0;
2083 (void)del_bytes((long)n, !virtual_op,
2084 oap->op_type == OP_DELETE && !oap->is_VIsual);
2085 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2086 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 }
2089
2090 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2091
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002092#ifdef FEAT_VIRTUALEDIT
2093setmarks:
2094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (oap->block_mode)
2096 {
2097 curbuf->b_op_end.lnum = oap->end.lnum;
2098 curbuf->b_op_end.col = oap->start.col;
2099 }
2100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 curbuf->b_op_end = oap->start;
2102 curbuf->b_op_start = oap->start;
2103
2104 return OK;
2105}
2106
2107#ifdef FEAT_MBYTE
2108/*
2109 * Adjust end of operating area for ending on a multi-byte character.
2110 * Used for deletion.
2111 */
2112 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002113mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 char_u *p;
2116
2117 if (oap->inclusive)
2118 {
2119 p = ml_get(oap->end.lnum);
2120 oap->end.col += mb_tail_off(p, p + oap->end.col);
2121 }
2122}
2123#endif
2124
Bram Moolenaar630afe82018-06-28 19:26:28 +02002125
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002126#ifdef FEAT_MBYTE
Bram Moolenaar630afe82018-06-28 19:26:28 +02002127/*
2128 * Replace the character under the cursor with "c".
2129 * This takes care of multi-byte characters.
2130 */
2131 static void
2132replace_character(int c)
2133{
2134 int n = State;
2135
2136 State = REPLACE;
2137 ins_char(c);
2138 State = n;
2139 /* Backup to the replaced character. */
2140 dec_cursor();
2141}
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002142#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002143
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144/*
2145 * Replace a whole area with one character.
2146 */
2147 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int n, numc;
2151#ifdef FEAT_MBYTE
2152 int num_chars;
2153#endif
2154 char_u *newp, *oldp;
2155 size_t oldlen;
2156 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002157 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002158 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
2160 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2161 return OK; /* nothing to do */
2162
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002163 if (c == REPLACE_CR_NCHAR)
2164 {
2165 had_ctrl_v_cr = TRUE;
2166 c = CAR;
2167 }
2168 else if (c == REPLACE_NL_NCHAR)
2169 {
2170 had_ctrl_v_cr = TRUE;
2171 c = NL;
2172 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174#ifdef FEAT_MBYTE
2175 if (has_mbyte)
2176 mb_adjust_opend(oap);
2177#endif
2178
2179 if (u_save((linenr_T)(oap->start.lnum - 1),
2180 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2181 return FAIL;
2182
2183 /*
2184 * block mode replace
2185 */
2186 if (oap->block_mode)
2187 {
2188 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2189 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2190 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002191 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2193 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2194 continue; /* nothing to replace */
2195
2196 /* n == number of extra chars required
2197 * If we split a TAB, it may be replaced by several characters.
2198 * Thus the number of characters may increase!
2199 */
2200#ifdef FEAT_VIRTUALEDIT
2201 /* If the range starts in virtual space, count the initial
2202 * coladd offset as part of "startspaces" */
2203 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2204 {
2205 pos_T vpos;
2206
Bram Moolenaara1381de2009-11-03 15:44:21 +00002207 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 getvpos(&vpos, oap->start_vcol);
2209 bd.startspaces += vpos.coladd;
2210 n = bd.startspaces;
2211 }
2212 else
2213#endif
2214 /* allow for pre spaces */
2215 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2216
2217 /* allow for post spp */
2218 n += (bd.endspaces
2219#ifdef FEAT_VIRTUALEDIT
2220 && !bd.is_oneChar
2221#endif
2222 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2223 /* Figure out how many characters to replace. */
2224 numc = oap->end_vcol - oap->start_vcol + 1;
2225 if (bd.is_short && (!virtual_op || bd.is_MAX))
2226 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2227
2228#ifdef FEAT_MBYTE
2229 /* A double-wide character can be replaced only up to half the
2230 * times. */
2231 if ((*mb_char2cells)(c) > 1)
2232 {
2233 if ((numc & 1) && !bd.is_short)
2234 {
2235 ++bd.endspaces;
2236 ++n;
2237 }
2238 numc = numc / 2;
2239 }
2240
2241 /* Compute bytes needed, move character count to num_chars. */
2242 num_chars = numc;
2243 numc *= (*mb_char2len)(c);
2244#endif
2245 /* oldlen includes textlen, so don't double count */
2246 n += numc - bd.textlen;
2247
2248 oldp = ml_get_curline();
2249 oldlen = STRLEN(oldp);
2250 newp = alloc_check((unsigned)oldlen + 1 + n);
2251 if (newp == NULL)
2252 continue;
2253 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2254 /* copy up to deleted part */
2255 mch_memmove(newp, oldp, (size_t)bd.textcol);
2256 oldp += bd.textcol + bd.textlen;
2257 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002258 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002260 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2261 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002262 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002264#ifdef FEAT_MBYTE
2265 if (has_mbyte)
2266 {
2267 n = (int)STRLEN(newp);
2268 while (--num_chars >= 0)
2269 n += (*mb_char2bytes)(c, newp + n);
2270 }
2271 else
2272#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002273 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002274 if (!bd.is_short)
2275 {
2276 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002277 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002278 /* copy the part after the changed part */
2279 STRMOVE(newp + STRLEN(newp), oldp);
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002284 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002285 after_p = alloc_check(
2286 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002287 if (after_p != NULL)
2288 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290 /* replace the line */
2291 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002292 if (after_p != NULL)
2293 {
2294 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2295 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2296 oap->end.lnum++;
2297 vim_free(after_p);
2298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300 }
2301 else
2302 {
2303 /*
2304 * MCHAR and MLINE motion replace.
2305 */
2306 if (oap->motion_type == MLINE)
2307 {
2308 oap->start.col = 0;
2309 curwin->w_cursor.col = 0;
2310 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2311 if (oap->end.col)
2312 --oap->end.col;
2313 }
2314 else if (!oap->inclusive)
2315 dec(&(oap->end));
2316
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002317 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 n = gchar_cursor();
2320 if (n != NUL)
2321 {
2322#ifdef FEAT_MBYTE
2323 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2324 {
2325 /* This is slow, but it handles replacing a single-byte
2326 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002327 if (curwin->w_cursor.lnum == oap->end.lnum)
2328 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002329 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
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 Moolenaar630afe82018-06-28 19:26:28 +02002351 PBYTE(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 Moolenaar870ba5f2019-01-11 14:37:20 +01002370# ifdef FEAT_MBYTE
Bram Moolenaar630afe82018-06-28 19:26:28 +02002371 if ((*mb_char2len)(c) > 1)
2372 replace_character(c);
2373 else
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002374# endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002375 PBYTE(curwin->w_cursor, c);
2376 if (inc(&curwin->w_cursor) == -1)
2377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379 }
2380#endif
2381
2382 /* Advance to next character, stop at the end of the file. */
2383 if (inc_cursor() == -1)
2384 break;
2385 }
2386 }
2387
2388 curwin->w_cursor = oap->start;
2389 check_cursor();
2390 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2391
2392 /* Set "'[" and "']" marks. */
2393 curbuf->b_op_start = oap->start;
2394 curbuf->b_op_end = oap->end;
2395
2396 return OK;
2397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002399static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401/*
2402 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2403 */
2404 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002409 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (u_save((linenr_T)(oap->start.lnum - 1),
2412 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2413 return;
2414
2415 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (oap->block_mode) /* Visual block mode */
2417 {
2418 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2419 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002420 int one_change;
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 block_prep(oap, &bd, pos.lnum, FALSE);
2423 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002424 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2425 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002426
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002427#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002428 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2431
Bram Moolenaar009b2592004-10-24 19:18:58 +00002432 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2433 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002435 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 if (did_change)
2440 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2441 }
2442 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 if (oap->motion_type == MLINE)
2445 {
2446 oap->start.col = 0;
2447 pos.col = 0;
2448 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2449 if (oap->end.col)
2450 --oap->end.col;
2451 }
2452 else if (!oap->inclusive)
2453 dec(&(oap->end));
2454
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002455 if (pos.lnum == oap->end.lnum)
2456 did_change = swapchars(oap->op_type, &pos,
2457 oap->end.col - pos.col + 1);
2458 else
2459 for (;;)
2460 {
2461 did_change |= swapchars(oap->op_type, &pos,
2462 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2463 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002464 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002465 break;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (did_change)
2468 {
2469 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2470 0L);
2471#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002472 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 char_u *ptr;
2475 int count;
2476
2477 pos = oap->start;
2478 while (pos.lnum < oap->end.lnum)
2479 {
2480 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002481 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002482 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002484 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 pos.col = 0;
2486 pos.lnum++;
2487 }
2488 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2489 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002490 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002492 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494#endif
2495 }
2496 }
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (!did_change && oap->is_VIsual)
2499 /* No change: need to remove the Visual selection */
2500 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /*
2503 * Set '[ and '] marks.
2504 */
2505 curbuf->b_op_start = oap->start;
2506 curbuf->b_op_end = oap->end;
2507
2508 if (oap->line_count > p_report)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002509 smsg((char_u *)NGETTEXT("%ld line changed", "%ld lines changed",
2510 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511}
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 Moolenaar630afe82018-06-28 19:26:28 +02002612 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return TRUE;
2614 }
2615 return FALSE;
2616}
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618/*
2619 * op_insert - Insert and append operators for Visual mode.
2620 */
2621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002622op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623{
2624 long ins_len, pre_textlen = 0;
2625 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002626 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 struct block_def bd;
2628 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002629 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631 /* edit() changes this - record it for OP_APPEND */
2632 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2633
2634 /* vis block is still marked. Get rid of it now. */
2635 curwin->w_cursor.lnum = oap->start.lnum;
2636 update_screen(INVERTED);
2637
2638 if (oap->block_mode)
2639 {
2640#ifdef FEAT_VIRTUALEDIT
2641 /* When 'virtualedit' is used, need to insert the extra spaces before
2642 * doing block_prep(). When only "block" is used, virtual edit is
2643 * already disabled, but still need it when calling
2644 * coladvance_force(). */
2645 if (curwin->w_cursor.coladd > 0)
2646 {
2647 int old_ve_flags = ve_flags;
2648
2649 ve_flags = VE_ALL;
2650 if (u_save_cursor() == FAIL)
2651 return;
2652 coladvance_force(oap->op_type == OP_APPEND
2653 ? oap->end_vcol + 1 : getviscol());
2654 if (oap->op_type == OP_APPEND)
2655 --curwin->w_cursor.col;
2656 ve_flags = old_ve_flags;
2657 }
2658#endif
2659 /* Get the info about the block before entering the text */
2660 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002661 /* Get indent information */
2662 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (oap->op_type == OP_APPEND)
2666 firstline += bd.textlen;
2667 pre_textlen = (long)STRLEN(firstline);
2668 }
2669
2670 if (oap->op_type == OP_APPEND)
2671 {
2672 if (oap->block_mode
2673#ifdef FEAT_VIRTUALEDIT
2674 && curwin->w_cursor.coladd == 0
2675#endif
2676 )
2677 {
2678 /* Move the cursor to the character right of the block. */
2679 curwin->w_set_curswant = TRUE;
2680 while (*ml_get_cursor() != NUL
2681 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2682 ++curwin->w_cursor.col;
2683 if (bd.is_short && !bd.is_MAX)
2684 {
2685 /* First line was too short, make it longer and adjust the
2686 * values in "bd". */
2687 if (u_save_cursor() == FAIL)
2688 return;
2689 for (i = 0; i < bd.endspaces; ++i)
2690 ins_char(' ');
2691 bd.textlen += bd.endspaces;
2692 }
2693 }
2694 else
2695 {
2696 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002697 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002700 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 && oap->start_vcol != oap->end_vcol)
2702 inc_cursor();
2703 }
2704 }
2705
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002706 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002707 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002709 /* When a tab was inserted, and the characters in front of the tab
2710 * have been converted to a tab as well, the column of the cursor
2711 * might have actually been reduced, so need to adjust here. */
2712 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002713 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002714 oap->start = curbuf->b_op_start_orig;
2715
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002716 /* If user has moved off this line, we don't know what to do, so do
2717 * nothing.
2718 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2719 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return;
2721
2722 if (oap->block_mode)
2723 {
2724 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002725 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002726 size_t len;
2727 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002729 /* If indent kicked in, the firstline might have changed
2730 * but only do that, if the indent actually increased. */
2731 ind_post = (colnr_T)getwhitecols_curline();
2732 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2733 {
2734 bd.textcol += ind_post - ind_pre;
2735 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002736 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002737 }
2738
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002739 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002740 * to adjust the block for that. But only do it, if the difference
2741 * does not come from indent kicking in. */
2742 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2743 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002744 {
2745 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002746 && oap->start.col
2747#ifdef FEAT_VIRTUALEDIT
2748 + oap->start.coladd
2749#endif
2750 != curbuf->b_op_start_orig.col
2751#ifdef FEAT_VIRTUALEDIT
2752 + curbuf->b_op_start_orig.coladd
2753#endif
2754 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002755 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002756 int t = getviscol2(curbuf->b_op_start_orig.col,
2757 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002758 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002759 pre_textlen -= t - oap->start_vcol;
2760 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002761 }
2762 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002763 && oap->end.col
2764#ifdef FEAT_VIRTUALEDIT
2765 + oap->end.coladd
2766#endif
2767 >= curbuf->b_op_start_orig.col
2768#ifdef FEAT_VIRTUALEDIT
2769 + curbuf->b_op_start_orig.coladd
2770#endif
2771 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002772 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002773 int t = getviscol2(curbuf->b_op_start_orig.col,
2774 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002775 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002776 /* reset pre_textlen to the value of OP_INSERT */
2777 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002778 pre_textlen -= t - oap->start_vcol;
2779 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002780 oap->op_type = OP_INSERT;
2781 }
2782 }
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /*
2785 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002786 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 * Don't do this when "$" used, end-of-line will have changed.
2788 */
2789 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2790 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2791 {
2792 if (oap->op_type == OP_APPEND)
2793 {
2794 pre_textlen += bd2.textlen - bd.textlen;
2795 if (bd2.endspaces)
2796 --bd2.textlen;
2797 }
2798 bd.textcol = bd2.textcol;
2799 bd.textlen = bd2.textlen;
2800 }
2801
2802 /*
2803 * Subsequent calls to ml_get() flush the firstline data - take a
2804 * copy of the required string.
2805 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002806 firstline = ml_get(oap->start.lnum);
2807 len = STRLEN(firstline);
2808 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002810 add += bd.textlen;
2811 if ((size_t)add > len)
2812 firstline += len; // short line, point to the NUL
2813 else
2814 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002815 if (pre_textlen >= 0
2816 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
2818 ins_text = vim_strnsave(firstline, (int)ins_len);
2819 if (ins_text != NULL)
2820 {
2821 /* block handled here */
2822 if (u_save(oap->start.lnum,
2823 (linenr_T)(oap->end.lnum + 1)) == OK)
2824 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2825 &bd);
2826
2827 curwin->w_cursor.col = oap->start.col;
2828 check_cursor();
2829 vim_free(ins_text);
2830 }
2831 }
2832 }
2833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835/*
2836 * op_change - handle a change operation
2837 *
2838 * return TRUE if edit() returns because of a CTRL-O command
2839 */
2840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002841op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 colnr_T l;
2844 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 long offset;
2846 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002847 long ins_len;
2848 long pre_textlen = 0;
2849 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 char_u *firstline;
2851 char_u *ins_text, *newp, *oldp;
2852 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 l = oap->start.col;
2855 if (oap->motion_type == MLINE)
2856 {
2857 l = 0;
2858#ifdef FEAT_SMARTINDENT
2859 if (!p_paste && curbuf->b_p_si
2860# ifdef FEAT_CINDENT
2861 && !curbuf->b_p_cin
2862# endif
2863 )
2864 can_si = TRUE; /* It's like opening a new line, do si */
2865#endif
2866 }
2867
2868 /* First delete the text in the region. In an empty buffer only need to
2869 * save for undo */
2870 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2871 {
2872 if (u_save_cursor() == FAIL)
2873 return FALSE;
2874 }
2875 else if (op_delete(oap) == FAIL)
2876 return FALSE;
2877
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002878 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 && !virtual_op)
2880 inc_cursor();
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 /* check for still on same line (<CR> in inserted text meaningless) */
2883 /* skip blank lines too */
2884 if (oap->block_mode)
2885 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002886#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 /* Add spaces before getting the current line length. */
2888 if (virtual_op && (curwin->w_cursor.coladd > 0
2889 || gchar_cursor() == NUL))
2890 coladvance_force(getviscol());
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002891#endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002892 firstline = ml_get(oap->start.lnum);
2893 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002894 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 bd.textcol = curwin->w_cursor.col;
2896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2899 if (oap->motion_type == MLINE)
2900 fix_indent();
2901#endif
2902
2903 retval = edit(NUL, FALSE, (linenr_T)1);
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002906 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002908 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002910 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002912 /* Auto-indenting may have changed the indent. If the cursor was past
2913 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002915 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002917 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002918
2919 pre_textlen += new_indent - pre_indent;
2920 bd.textcol += new_indent - pre_indent;
2921 }
2922
2923 ins_len = (long)STRLEN(firstline) - pre_textlen;
2924 if (ins_len > 0)
2925 {
2926 /* Subsequent calls to ml_get() flush the firstline data - take a
2927 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2929 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002930 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2932 linenr++)
2933 {
2934 block_prep(oap, &bd, linenr, TRUE);
2935 if (!bd.is_short || virtual_op)
2936 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002937#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 pos_T vpos;
2939
2940 /* If the block starts in virtual space, count the
2941 * initial coladd offset as part of "startspaces" */
2942 if (bd.is_short)
2943 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002944 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 else
2948 vpos.coladd = 0;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 oldp = ml_get(linenr);
2951 newp = alloc_check((unsigned)(STRLEN(oldp)
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002952#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 + vpos.coladd
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 + ins_len + 1));
2956 if (newp == NULL)
2957 continue;
2958 /* copy up to block start */
2959 mch_memmove(newp, oldp, (size_t)bd.textcol);
2960 offset = bd.textcol;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002961#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002962 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 offset += vpos.coladd;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2966 offset += ins_len;
2967 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002968 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 ml_replace(linenr, newp, FALSE);
2970 }
2971 }
2972 check_cursor();
2973
2974 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2975 }
2976 vim_free(ins_text);
2977 }
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 return retval;
2981}
2982
2983/*
2984 * set all the yank registers to empty (called from main())
2985 */
2986 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002987init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 int i;
2990
2991 for (i = 0; i < NUM_REGISTERS; ++i)
2992 y_regs[i].y_array = NULL;
2993}
2994
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002995#if defined(EXITFREE) || defined(PROTO)
2996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002997clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002998{
2999 int i;
3000
3001 for (i = 0; i < NUM_REGISTERS; ++i)
3002 {
3003 y_current = &y_regs[i];
3004 if (y_current->y_array != NULL)
3005 free_yank_all();
3006 }
3007}
3008#endif
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010/*
3011 * Free "n" lines from the current yank register.
3012 * Called for normal freeing and in case of error.
3013 */
3014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003015free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 if (y_current->y_array != NULL)
3018 {
3019 long i;
3020
3021 for (i = n; --i >= 0; )
3022 {
3023#ifdef AMIGA /* only for very slow machines */
3024 if ((i & 1023) == 1023) /* this may take a while */
3025 {
3026 /*
3027 * This message should never cause a hit-return message.
3028 * Overwrite this message with any next message.
3029 */
3030 ++no_wait_return;
3031 smsg((char_u *)_("freeing %ld lines"), i + 1);
3032 --no_wait_return;
3033 msg_didout = FALSE;
3034 msg_col = 0;
3035 }
3036#endif
3037 vim_free(y_current->y_array[i]);
3038 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003039 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#ifdef AMIGA
3041 if (n >= 1000)
3042 MSG("");
3043#endif
3044 }
3045}
3046
3047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003048free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 free_yank(y_current->y_size);
3051}
3052
3053/*
3054 * Yank the text between "oap->start" and "oap->end" into a yank register.
3055 * If we are to append (uppercase register), we first yank into a new yank
3056 * register and then concatenate the old and the new one (so we keep the old
3057 * one in case of out-of-memory).
3058 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003059 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
3061 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003065 yankreg_T *curr; /* copy of y_current */
3066 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 char_u **new_ptr;
3068 linenr_T lnum; /* current line number */
3069 long j;
3070 int yanktype = oap->motion_type;
3071 long yanklines = oap->line_count;
3072 linenr_T yankendlnum = oap->end.lnum;
3073 char_u *p;
3074 char_u *pnew;
3075 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003076#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003077 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 /* check for read-only register */
3081 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3082 {
3083 beep_flush();
3084 return FAIL;
3085 }
3086 if (oap->regname == '_') /* black hole: nothing to do */
3087 return OK;
3088
3089#ifdef FEAT_CLIPBOARD
3090 if (!clip_star.available && oap->regname == '*')
3091 oap->regname = 0;
3092 else if (!clip_plus.available && oap->regname == '+')
3093 oap->regname = 0;
3094#endif
3095
3096 if (!deleting) /* op_delete() already set y_current */
3097 get_yank_register(oap->regname, TRUE);
3098
3099 curr = y_current;
3100 /* append to existing contents */
3101 if (y_append && y_current->y_array != NULL)
3102 y_current = &newreg;
3103 else
3104 free_yank_all(); /* free previously yanked lines */
3105
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003106 /*
3107 * If the cursor was in column 1 before and after the movement, and the
3108 * operator is not inclusive, the yank is always linewise.
3109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if ( oap->motion_type == MCHAR
3111 && oap->start.col == 0
3112 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003114 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 && oap->end.col == 0
3116 && yanklines > 1)
3117 {
3118 yanktype = MLINE;
3119 --yankendlnum;
3120 --yanklines;
3121 }
3122
3123 y_current->y_size = yanklines;
3124 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3127 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (y_current->y_array == NULL)
3129 {
3130 y_current = curr;
3131 return FAIL;
3132 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003133#ifdef FEAT_VIMINFO
3134 y_current->y_time_set = vim_time();
3135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 y_idx = 0;
3138 lnum = oap->start.lnum;
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (oap->block_mode)
3141 {
3142 /* Visual block mode */
3143 y_current->y_type = MBLOCK; /* set the yank register type */
3144 y_current->y_width = oap->end_vcol - oap->start_vcol;
3145
3146 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3147 y_current->y_width--;
3148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3151 {
3152 switch (y_current->y_type)
3153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 case MBLOCK:
3155 block_prep(oap, &bd, lnum, FALSE);
3156 if (yank_copy_line(&bd, y_idx) == FAIL)
3157 goto fail;
3158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 case MLINE:
3161 if ((y_current->y_array[y_idx] =
3162 vim_strsave(ml_get(lnum))) == NULL)
3163 goto fail;
3164 break;
3165
3166 case MCHAR:
3167 {
3168 colnr_T startcol = 0, endcol = MAXCOL;
3169#ifdef FEAT_VIRTUALEDIT
3170 int is_oneChar = FALSE;
3171 colnr_T cs, ce;
3172#endif
3173 p = ml_get(lnum);
3174 bd.startspaces = 0;
3175 bd.endspaces = 0;
3176
3177 if (lnum == oap->start.lnum)
3178 {
3179 startcol = oap->start.col;
3180#ifdef FEAT_VIRTUALEDIT
3181 if (virtual_op)
3182 {
3183 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3184 if (ce != cs && oap->start.coladd > 0)
3185 {
3186 /* Part of a tab selected -- but don't
3187 * double-count it. */
3188 bd.startspaces = (ce - cs + 1)
3189 - oap->start.coladd;
3190 startcol++;
3191 }
3192 }
3193#endif
3194 }
3195
3196 if (lnum == oap->end.lnum)
3197 {
3198 endcol = oap->end.col;
3199#ifdef FEAT_VIRTUALEDIT
3200 if (virtual_op)
3201 {
3202 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3203 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3204# ifdef FEAT_MBYTE
3205 /* Don't add space for double-wide
3206 * char; endcol will be on last byte
3207 * of multi-byte char. */
3208 && (*mb_head_off)(p, p + endcol) == 0
3209# endif
3210 ))
3211 {
3212 if (oap->start.lnum == oap->end.lnum
3213 && oap->start.col == oap->end.col)
3214 {
3215 /* Special case: inside a single char */
3216 is_oneChar = TRUE;
3217 bd.startspaces = oap->end.coladd
3218 - oap->start.coladd + oap->inclusive;
3219 endcol = startcol;
3220 }
3221 else
3222 {
3223 bd.endspaces = oap->end.coladd
3224 + oap->inclusive;
3225 endcol -= oap->inclusive;
3226 }
3227 }
3228 }
3229#endif
3230 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003231 if (endcol == MAXCOL)
3232 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (startcol > endcol
3234#ifdef FEAT_VIRTUALEDIT
3235 || is_oneChar
3236#endif
3237 )
3238 bd.textlen = 0;
3239 else
3240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 bd.textlen = endcol - startcol + oap->inclusive;
3242 }
3243 bd.textstart = p + startcol;
3244 if (yank_copy_line(&bd, y_idx) == FAIL)
3245 goto fail;
3246 break;
3247 }
3248 /* NOTREACHED */
3249 }
3250 }
3251
3252 if (curr != y_current) /* append the new block to the old block */
3253 {
3254 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3255 (curr->y_size + y_current->y_size)), TRUE);
3256 if (new_ptr == NULL)
3257 goto fail;
3258 for (j = 0; j < curr->y_size; ++j)
3259 new_ptr[j] = curr->y_array[j];
3260 vim_free(curr->y_array);
3261 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003262#ifdef FEAT_VIMINFO
3263 curr->y_time_set = vim_time();
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3267 curr->y_type = MLINE;
3268
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003269 /* Concatenate the last line of the old block with the first line of
3270 * the new block, unless being Vi compatible. */
3271 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3274 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3275 if (pnew == NULL)
3276 {
3277 y_idx = y_current->y_size - 1;
3278 goto fail;
3279 }
3280 STRCPY(pnew, curr->y_array[--j]);
3281 STRCAT(pnew, y_current->y_array[0]);
3282 vim_free(curr->y_array[j]);
3283 vim_free(y_current->y_array[0]);
3284 curr->y_array[j++] = pnew;
3285 y_idx = 1;
3286 }
3287 else
3288 y_idx = 0;
3289 while (y_idx < y_current->y_size)
3290 curr->y_array[j++] = y_current->y_array[y_idx++];
3291 curr->y_size = j;
3292 vim_free(y_current->y_array);
3293 y_current = curr;
3294 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003295 if (curwin->w_p_rnu)
3296 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (mess) /* Display message about yank? */
3298 {
3299 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 && yanklines == 1)
3302 yanklines = 0;
3303 /* Some versions of Vi use ">=" here, some don't... */
3304 if (yanklines > p_report)
3305 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003306 char namebuf[100];
3307
3308 if (oap->regname == NUL)
3309 *namebuf = NUL;
3310 else
3311 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003312 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 /* redisplay now, so message is not deleted */
3315 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003316 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003317 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003318 smsg((char_u *)NGETTEXT("block of %ld line yanked%s",
3319 "block of %ld lines yanked%s", yanklines),
3320 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003323 {
3324 smsg((char_u *)NGETTEXT("%ld line yanked%s",
3325 "%ld lines yanked%s", yanklines),
3326 yanklines, namebuf);
3327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 }
3330
3331 /*
3332 * Set "'[" and "']" marks.
3333 */
3334 curbuf->b_op_start = oap->start;
3335 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003336 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003337 {
3338 curbuf->b_op_start.col = 0;
3339 curbuf->b_op_end.col = MAXCOL;
3340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
3342#ifdef FEAT_CLIPBOARD
3343 /*
3344 * If we were yanking to the '*' register, send result to clipboard.
3345 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3346 * to the '*' register.
3347 */
3348 if (clip_star.available
3349 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003350 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003351 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
3353 if (curr != &(y_regs[STAR_REGISTER]))
3354 /* Copy the text from register 0 to the clipboard register. */
3355 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3356
3357 clip_own_selection(&clip_star);
3358 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003359# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003360 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363
3364# ifdef FEAT_X11
3365 /*
3366 * If we were yanking to the '+' register, send result to selection.
3367 * Also copy to the '*' register, in case auto-select is off.
3368 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003369 if (clip_plus.available
3370 && (curr == &(y_regs[PLUS_REGISTER])
3371 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003372 && ((clip_unnamed | clip_unnamed_saved) &
3373 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003375 if (curr != &(y_regs[PLUS_REGISTER]))
3376 /* Copy the text from register 0 to the clipboard register. */
3377 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 clip_own_selection(&clip_plus);
3380 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003381 if (!clip_isautosel_star() && !clip_isautosel_plus()
3382 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3385 clip_own_selection(&clip_star);
3386 clip_gen_set_selection(&clip_star);
3387 }
3388 }
3389# endif
3390#endif
3391
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003392#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003393 if (!deleting && has_textyankpost())
3394 yank_do_autocmd(oap, y_current);
3395#endif
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 return OK;
3398
3399fail: /* free the allocated lines */
3400 free_yank(y_idx + 1);
3401 y_current = curr;
3402 return FAIL;
3403}
3404
3405 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003406yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 char_u *pnew;
3409
3410 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3411 == NULL)
3412 return FAIL;
3413 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003414 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 pnew += bd->startspaces;
3416 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3417 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003418 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 pnew += bd->endspaces;
3420 *pnew = NUL;
3421 return OK;
3422}
3423
3424#ifdef FEAT_CLIPBOARD
3425/*
3426 * Make a copy of the y_current register to register "reg".
3427 */
3428 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003429copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003431 yankreg_T *curr = y_current;
3432 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 y_current = reg;
3435 free_yank_all();
3436 *y_current = *curr;
3437 y_current->y_array = (char_u **)lalloc_clear(
3438 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3439 if (y_current->y_array == NULL)
3440 y_current->y_size = 0;
3441 else
3442 for (j = 0; j < y_current->y_size; ++j)
3443 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3444 {
3445 free_yank(j);
3446 y_current->y_size = 0;
3447 break;
3448 }
3449 y_current = curr;
3450}
3451#endif
3452
3453/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003454 * Put contents of register "regname" into the text.
3455 * Caller must check "regname" to be valid!
3456 * "flags": PUT_FIXINDENT make indent look nice
3457 * PUT_CURSEND leave cursor after end of new text
3458 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003461do_put(
3462 int regname,
3463 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3464 long count,
3465 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 char_u *ptr;
3468 char_u *newp, *oldp;
3469 int yanklen;
3470 int totlen = 0; /* init for gcc */
3471 linenr_T lnum;
3472 colnr_T col;
3473 long i; /* index in y_array[] */
3474 int y_type;
3475 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int oldlen;
3477 long y_width = 0;
3478 colnr_T vcol;
3479 int delcount;
3480 int incr = 0;
3481 long j;
3482 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 char_u **y_array = NULL;
3484 long nr_lines = 0;
3485 pos_T new_cursor;
3486 int indent;
3487 int orig_indent = 0; /* init for gcc */
3488 int indent_diff = 0; /* init for gcc */
3489 int first_indent = TRUE;
3490 int lendiff = 0;
3491 pos_T old_pos;
3492 char_u *insert_string = NULL;
3493 int allocated = FALSE;
3494 long cnt;
3495
3496#ifdef FEAT_CLIPBOARD
3497 /* Adjust register name for "unnamed" in 'clipboard'. */
3498 adjust_clip_reg(&regname);
3499 (void)may_get_selection(regname);
3500#endif
3501
3502 if (flags & PUT_FIXINDENT)
3503 orig_indent = get_indent();
3504
3505 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3506 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3507
3508 /*
3509 * Using inserted text works differently, because the register includes
3510 * special characters (newlines, etc.).
3511 */
3512 if (regname == '.')
3513 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003514 if (VIsual_active)
3515 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3517 (count == -1 ? 'O' : 'i')), count, FALSE);
3518 /* Putting the text is done later, so can't really move the cursor to
3519 * the next character. Use "l" to simulate it. */
3520 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3521 stuffcharReadbuff('l');
3522 return;
3523 }
3524
3525 /*
3526 * For special registers '%' (file name), '#' (alternate file name) and
3527 * ':' (last command line), etc. we have to create a fake yank register.
3528 */
3529 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3530 {
3531 if (insert_string == NULL)
3532 return;
3533 }
3534
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003535 /* Autocommands may be executed when saving lines for undo. This might
3536 * make "y_array" invalid, so we start undo now to avoid that. */
3537 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3538 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (insert_string != NULL)
3541 {
3542 y_type = MCHAR;
3543#ifdef FEAT_EVAL
3544 if (regname == '=')
3545 {
3546 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003547 * characters.
3548 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 for (;;)
3550 {
3551 y_size = 0;
3552 ptr = insert_string;
3553 while (ptr != NULL)
3554 {
3555 if (y_array != NULL)
3556 y_array[y_size] = ptr;
3557 ++y_size;
3558 ptr = vim_strchr(ptr, '\n');
3559 if (ptr != NULL)
3560 {
3561 if (y_array != NULL)
3562 *ptr = NUL;
3563 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003564 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (*ptr == NUL)
3566 {
3567 y_type = MLINE;
3568 break;
3569 }
3570 }
3571 }
3572 if (y_array != NULL)
3573 break;
3574 y_array = (char_u **)alloc((unsigned)
3575 (y_size * sizeof(char_u *)));
3576 if (y_array == NULL)
3577 goto end;
3578 }
3579 }
3580 else
3581#endif
3582 {
3583 y_size = 1; /* use fake one-line yank register */
3584 y_array = &insert_string;
3585 }
3586 }
3587 else
3588 {
3589 get_yank_register(regname, FALSE);
3590
3591 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 y_size = y_current->y_size;
3594 y_array = y_current->y_array;
3595 }
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (y_type == MLINE)
3598 {
3599 if (flags & PUT_LINE_SPLIT)
3600 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003601 char_u *p;
3602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 /* "p" or "P" in Visual mode: split the lines to put the text in
3604 * between. */
3605 if (u_save_cursor() == FAIL)
3606 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003607 p = ml_get_cursor();
3608 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003609 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003610 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (ptr == NULL)
3612 goto end;
3613 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3614 vim_free(ptr);
3615
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003616 oldp = ml_get_curline();
3617 p = oldp + curwin->w_cursor.col;
3618 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003619 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003620 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (ptr == NULL)
3622 goto end;
3623 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3624 ++nr_lines;
3625 dir = FORWARD;
3626 }
3627 if (flags & PUT_LINE_FORWARD)
3628 {
3629 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003630 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 dir = FORWARD;
3632 }
3633 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3634 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3638 y_type = MLINE;
3639
3640 if (y_size == 0 || y_array == NULL)
3641 {
3642 EMSG2(_("E353: Nothing in register %s"),
3643 regname == 0 ? (char_u *)"\"" : transchar(regname));
3644 goto end;
3645 }
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (y_type == MBLOCK)
3648 {
3649 lnum = curwin->w_cursor.lnum + y_size + 1;
3650 if (lnum > curbuf->b_ml.ml_line_count)
3651 lnum = curbuf->b_ml.ml_line_count + 1;
3652 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3653 goto end;
3654 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003655 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 lnum = curwin->w_cursor.lnum;
3658#ifdef FEAT_FOLDING
3659 /* Correct line number for closed fold. Don't move the cursor yet,
3660 * u_save() uses it. */
3661 if (dir == BACKWARD)
3662 (void)hasFolding(lnum, &lnum, NULL);
3663 else
3664 (void)hasFolding(lnum, NULL, &lnum);
3665#endif
3666 if (dir == FORWARD)
3667 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003668 /* In an empty buffer the empty line is going to be replaced, include
3669 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003670 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 goto end;
3672#ifdef FEAT_FOLDING
3673 if (dir == FORWARD)
3674 curwin->w_cursor.lnum = lnum - 1;
3675 else
3676 curwin->w_cursor.lnum = lnum;
3677 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3678#endif
3679 }
3680 else if (u_save_cursor() == FAIL)
3681 goto end;
3682
3683 yanklen = (int)STRLEN(y_array[0]);
3684
3685#ifdef FEAT_VIRTUALEDIT
3686 if (ve_flags == VE_ALL && y_type == MCHAR)
3687 {
3688 if (gchar_cursor() == TAB)
3689 {
3690 /* Don't need to insert spaces when "p" on the last position of a
3691 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003692#ifdef FEAT_VARTABS
3693 int viscol = getviscol();
3694 if (dir == FORWARD
3695 ? tabstop_padding(viscol, curbuf->b_p_ts,
3696 curbuf->b_p_vts_array) != 1
3697 : curwin->w_cursor.coladd > 0)
3698 coladvance_force(viscol);
3699#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (dir == FORWARD
3701 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3702 : curwin->w_cursor.coladd > 0)
3703 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
3706 curwin->w_cursor.coladd = 0;
3707 }
3708 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3709 coladvance_force(getviscol() + (dir == FORWARD));
3710 }
3711#endif
3712
3713 lnum = curwin->w_cursor.lnum;
3714 col = curwin->w_cursor.col;
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 /*
3717 * Block mode
3718 */
3719 if (y_type == MBLOCK)
3720 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003721 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 colnr_T endcol2 = 0;
3723
3724 if (dir == FORWARD && c != NUL)
3725 {
3726#ifdef FEAT_VIRTUALEDIT
3727 if (ve_flags == VE_ALL)
3728 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3729 else
3730#endif
3731 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3732
3733#ifdef FEAT_MBYTE
3734 if (has_mbyte)
3735 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003736 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else
3738#endif
3739#ifdef FEAT_VIRTUALEDIT
3740 if (c != TAB || ve_flags != VE_ALL)
3741#endif
3742 ++curwin->w_cursor.col;
3743 ++col;
3744 }
3745 else
3746 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3747
3748#ifdef FEAT_VIRTUALEDIT
3749 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003750 if (ve_flags == VE_ALL
3751 && (curwin->w_cursor.coladd > 0
3752 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 if (dir == FORWARD && c == NUL)
3755 ++col;
3756 if (dir != FORWARD && c != NUL)
3757 ++curwin->w_cursor.col;
3758 if (c == TAB)
3759 {
3760 if (dir == BACKWARD && curwin->w_cursor.col)
3761 curwin->w_cursor.col--;
3762 if (dir == FORWARD && col - 1 == endcol2)
3763 curwin->w_cursor.col++;
3764 }
3765 }
3766 curwin->w_cursor.coladd = 0;
3767#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003768 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 for (i = 0; i < y_size; ++i)
3770 {
3771 int spaces;
3772 char shortline;
3773
3774 bd.startspaces = 0;
3775 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 vcol = 0;
3777 delcount = 0;
3778
3779 /* add a new line */
3780 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3781 {
3782 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3783 (colnr_T)1, FALSE) == FAIL)
3784 break;
3785 ++nr_lines;
3786 }
3787 /* get the old line and advance to the position to insert at */
3788 oldp = ml_get_curline();
3789 oldlen = (int)STRLEN(oldp);
3790 for (ptr = oldp; vcol < col && *ptr; )
3791 {
3792 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003793 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 vcol += incr;
3795 }
3796 bd.textcol = (colnr_T)(ptr - oldp);
3797
3798 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3799
3800 if (vcol < col) /* line too short, padd with spaces */
3801 bd.startspaces = col - vcol;
3802 else if (vcol > col)
3803 {
3804 bd.endspaces = vcol - col;
3805 bd.startspaces = incr - bd.endspaces;
3806 --bd.textcol;
3807 delcount = 1;
3808#ifdef FEAT_MBYTE
3809 if (has_mbyte)
3810 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3811#endif
3812 if (oldp[bd.textcol] != TAB)
3813 {
3814 /* Only a Tab can be split into spaces. Other
3815 * characters will have to be moved to after the
3816 * block, causing misalignment. */
3817 delcount = 0;
3818 bd.endspaces = 0;
3819 }
3820 }
3821
3822 yanklen = (int)STRLEN(y_array[i]);
3823
3824 /* calculate number of spaces required to fill right side of block*/
3825 spaces = y_width + 1;
3826 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003827 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (spaces < 0)
3829 spaces = 0;
3830
3831 /* insert the new text */
3832 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3833 newp = alloc_check((unsigned)totlen + oldlen + 1);
3834 if (newp == NULL)
3835 break;
3836 /* copy part up to cursor to new line */
3837 ptr = newp;
3838 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3839 ptr += bd.textcol;
3840 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003841 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 ptr += bd.startspaces;
3843 /* insert the new text */
3844 for (j = 0; j < count; ++j)
3845 {
3846 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3847 ptr += yanklen;
3848
3849 /* insert block's trailing spaces only if there's text behind */
3850 if ((j < count - 1 || !shortline) && spaces)
3851 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003852 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 ptr += spaces;
3854 }
3855 }
3856 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003857 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 ptr += bd.endspaces;
3859 /* move the text after the cursor to the end of the line. */
3860 mch_memmove(ptr, oldp + bd.textcol + delcount,
3861 (size_t)(oldlen - bd.textcol - delcount + 1));
3862 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3863
3864 ++curwin->w_cursor.lnum;
3865 if (i == 0)
3866 curwin->w_cursor.col += bd.startspaces;
3867 }
3868
3869 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3870
3871 /* Set '[ mark. */
3872 curbuf->b_op_start = curwin->w_cursor;
3873 curbuf->b_op_start.lnum = lnum;
3874
3875 /* adjust '] mark */
3876 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3877 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003878# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (flags & PUT_CURSEND)
3882 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003883 colnr_T len;
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 curwin->w_cursor = curbuf->b_op_end;
3886 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003887
3888 /* in Insert mode we might be after the NUL, correct for that */
3889 len = (colnr_T)STRLEN(ml_get_curline());
3890 if (curwin->w_cursor.col > len)
3891 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 else
3894 curwin->w_cursor.lnum = lnum;
3895 }
3896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 /*
3899 * Character or Line mode
3900 */
3901 if (y_type == MCHAR)
3902 {
3903 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3904 * char */
3905 if (dir == FORWARD && gchar_cursor() != NUL)
3906 {
3907#ifdef FEAT_MBYTE
3908 if (has_mbyte)
3909 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003910 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 /* put it on the next of the multi-byte character. */
3913 col += bytelen;
3914 if (yanklen)
3915 {
3916 curwin->w_cursor.col += bytelen;
3917 curbuf->b_op_end.col += bytelen;
3918 }
3919 }
3920 else
3921#endif
3922 {
3923 ++col;
3924 if (yanklen)
3925 {
3926 ++curwin->w_cursor.col;
3927 ++curbuf->b_op_end.col;
3928 }
3929 }
3930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 curbuf->b_op_start = curwin->w_cursor;
3932 }
3933 /*
3934 * Line mode: BACKWARD is the same as FORWARD on the previous line
3935 */
3936 else if (dir == BACKWARD)
3937 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003938 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 /*
3941 * simple case: insert into current line
3942 */
3943 if (y_type == MCHAR && y_size == 1)
3944 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003945 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003946
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003947 if (VIsual_active)
3948 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003949 end_lnum = curbuf->b_visual.vi_end.lnum;
3950 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3951 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003952 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003953
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003954 do {
3955 totlen = count * yanklen;
3956 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003958 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003959 if (VIsual_active && col > (int)STRLEN(oldp))
3960 {
3961 lnum++;
3962 continue;
3963 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003964 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3965 if (newp == NULL)
3966 goto end; /* alloc() gave an error message */
3967 mch_memmove(newp, oldp, (size_t)col);
3968 ptr = newp + col;
3969 for (i = 0; i < count; ++i)
3970 {
3971 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3972 ptr += yanklen;
3973 }
3974 STRMOVE(ptr, oldp + col);
3975 ml_replace(lnum, newp, FALSE);
3976 /* Place cursor on last putted char. */
3977 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003978 {
3979 /* make sure curwin->w_virtcol is updated */
3980 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003981 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003984 if (VIsual_active)
3985 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003986 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003987
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003988 if (VIsual_active) /* reset lnum to the last visual line */
3989 lnum--;
3990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 curbuf->b_op_end = curwin->w_cursor;
3992 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3993 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3994 ++curwin->w_cursor.col;
3995 changed_bytes(lnum, col);
3996 }
3997 else
3998 {
3999 /*
4000 * Insert at least one line. When y_type is MCHAR, break the first
4001 * line in two.
4002 */
4003 for (cnt = 1; cnt <= count; ++cnt)
4004 {
4005 i = 0;
4006 if (y_type == MCHAR)
4007 {
4008 /*
4009 * Split the current line in two at the insert position.
4010 * First insert y_array[size - 1] in front of second line.
4011 * Then append y_array[0] to first line.
4012 */
4013 lnum = new_cursor.lnum;
4014 ptr = ml_get(lnum) + col;
4015 totlen = (int)STRLEN(y_array[y_size - 1]);
4016 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4017 if (newp == NULL)
4018 goto error;
4019 STRCPY(newp, y_array[y_size - 1]);
4020 STRCAT(newp, ptr);
4021 /* insert second line */
4022 ml_append(lnum, newp, (colnr_T)0, FALSE);
4023 vim_free(newp);
4024
4025 oldp = ml_get(lnum);
4026 newp = alloc_check((unsigned)(col + yanklen + 1));
4027 if (newp == NULL)
4028 goto error;
4029 /* copy first part of line */
4030 mch_memmove(newp, oldp, (size_t)col);
4031 /* append to first line */
4032 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4033 ml_replace(lnum, newp, FALSE);
4034
4035 curwin->w_cursor.lnum = lnum;
4036 i = 1;
4037 }
4038
4039 for (; i < y_size; ++i)
4040 {
4041 if ((y_type != MCHAR || i < y_size - 1)
4042 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4043 == FAIL)
4044 goto error;
4045 lnum++;
4046 ++nr_lines;
4047 if (flags & PUT_FIXINDENT)
4048 {
4049 old_pos = curwin->w_cursor;
4050 curwin->w_cursor.lnum = lnum;
4051 ptr = ml_get(lnum);
4052 if (cnt == count && i == y_size - 1)
4053 lendiff = (int)STRLEN(ptr);
4054#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4055 if (*ptr == '#' && preprocs_left())
4056 indent = 0; /* Leave # lines at start */
4057 else
4058#endif
4059 if (*ptr == NUL)
4060 indent = 0; /* Ignore empty lines */
4061 else if (first_indent)
4062 {
4063 indent_diff = orig_indent - get_indent();
4064 indent = orig_indent;
4065 first_indent = FALSE;
4066 }
4067 else if ((indent = get_indent() + indent_diff) < 0)
4068 indent = 0;
4069 (void)set_indent(indent, 0);
4070 curwin->w_cursor = old_pos;
4071 /* remember how many chars were removed */
4072 if (cnt == count && i == y_size - 1)
4073 lendiff -= (int)STRLEN(ml_get(lnum));
4074 }
4075 }
4076 }
4077
4078error:
4079 /* Adjust marks. */
4080 if (y_type == MLINE)
4081 {
4082 curbuf->b_op_start.col = 0;
4083 if (dir == FORWARD)
4084 curbuf->b_op_start.lnum++;
4085 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004086 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004087 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004088 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004089 < curbuf->b_ml.ml_line_count
4090#ifdef FEAT_DIFF
4091 || curwin->w_p_diff
4092#endif
4093 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004094 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 (linenr_T)MAXLNUM, nr_lines, 0L);
4096
4097 /* note changed text for displaying and folding */
4098 if (y_type == MCHAR)
4099 changed_lines(curwin->w_cursor.lnum, col,
4100 curwin->w_cursor.lnum + 1, nr_lines);
4101 else
4102 changed_lines(curbuf->b_op_start.lnum, 0,
4103 curbuf->b_op_start.lnum, nr_lines);
4104
4105 /* put '] mark at last inserted character */
4106 curbuf->b_op_end.lnum = lnum;
4107 /* correct length for change in indent */
4108 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4109 if (col > 1)
4110 curbuf->b_op_end.col = col - 1;
4111 else
4112 curbuf->b_op_end.col = 0;
4113
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004114 if (flags & PUT_CURSLINE)
4115 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004116 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004117 curwin->w_cursor.lnum = lnum;
4118 beginline(BL_WHITE | BL_FIX);
4119 }
4120 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 /* put cursor after inserted text */
4123 if (y_type == MLINE)
4124 {
4125 if (lnum >= curbuf->b_ml.ml_line_count)
4126 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4127 else
4128 curwin->w_cursor.lnum = lnum + 1;
4129 curwin->w_cursor.col = 0;
4130 }
4131 else
4132 {
4133 curwin->w_cursor.lnum = lnum;
4134 curwin->w_cursor.col = col;
4135 }
4136 }
4137 else if (y_type == MLINE)
4138 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004139 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 curwin->w_cursor.col = 0;
4141 if (dir == FORWARD)
4142 ++curwin->w_cursor.lnum;
4143 beginline(BL_WHITE | BL_FIX);
4144 }
4145 else /* put cursor on first inserted character */
4146 curwin->w_cursor = new_cursor;
4147 }
4148 }
4149
4150 msgmore(nr_lines);
4151 curwin->w_set_curswant = TRUE;
4152
4153end:
4154 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004156 if (regname == '=')
4157 vim_free(y_array);
4158
Bram Moolenaar033d8882013-09-25 23:24:57 +02004159 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004160
Bram Moolenaar677ee682005-01-27 14:41:15 +00004161 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004162 adjust_cursor_eol();
4163}
4164
4165/*
4166 * When the cursor is on the NUL past the end of the line and it should not be
4167 * there move it left.
4168 */
4169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004170adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004171{
4172 if (curwin->w_cursor.col > 0
4173 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004174#ifdef FEAT_VIRTUALEDIT
4175 && (ve_flags & VE_ONEMORE) == 0
4176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 && !(restart_edit || (State & INSERT)))
4178 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004179 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004180 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_VIRTUALEDIT
4183 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004184 {
4185 colnr_T scol, ecol;
4186
4187 /* Coladd is set to the width of the last character. */
4188 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4189 curwin->w_cursor.coladd = ecol - scol + 1;
4190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191#endif
4192 }
4193}
4194
4195#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4196/*
4197 * Return TRUE if lines starting with '#' should be left aligned.
4198 */
4199 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004200preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
4202 return
4203# ifdef FEAT_SMARTINDENT
4204# ifdef FEAT_CINDENT
4205 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4206# else
4207 curbuf->b_p_si
4208# endif
4209# endif
4210# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004211 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4212 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213# endif
4214 ;
4215}
4216#endif
4217
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004218/*
4219 * Return the character name of the register with the given number.
4220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004222get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 if (num == -1)
4225 return '"';
4226 else if (num < 10)
4227 return num + '0';
4228 else if (num == DELETION_REGISTER)
4229 return '-';
4230#ifdef FEAT_CLIPBOARD
4231 else if (num == STAR_REGISTER)
4232 return '*';
4233 else if (num == PLUS_REGISTER)
4234 return '+';
4235#endif
4236 else
4237 {
4238#ifdef EBCDIC
4239 int i;
4240
4241 /* EBCDIC is really braindead ... */
4242 i = 'a' + (num - 10);
4243 if (i > 'i')
4244 i += 7;
4245 if (i > 'r')
4246 i += 8;
4247 return i;
4248#else
4249 return num + 'a' - 10;
4250#endif
4251 }
4252}
4253
4254/*
4255 * ":dis" and ":registers": Display the contents of the yank registers.
4256 */
4257 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004258ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004260 int i, n;
4261 long j;
4262 char_u *p;
4263 yankreg_T *yb;
4264 int name;
4265 int attr;
4266 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004267#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004268 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004269#else
4270# define clen 1
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 if (arg != NULL && *arg == NUL)
4274 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004275 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /* Highlight title */
4278 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4279 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4280 {
4281 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004282 if (arg != NULL && vim_strchr(arg, name) == NULL
4283#ifdef ONE_CLIPBOARD
4284 /* Star register and plus register contain the same thing. */
4285 && (name != '*' || vim_strchr(arg, '+') == NULL)
4286#endif
4287 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 continue; /* did not ask for this register */
4289
4290#ifdef FEAT_CLIPBOARD
4291 /* Adjust register name for "unnamed" in 'clipboard'.
4292 * When it's a clipboard register, fill it with the current contents
4293 * of the clipboard. */
4294 adjust_clip_reg(&name);
4295 (void)may_get_selection(name);
4296#endif
4297
4298 if (i == -1)
4299 {
4300 if (y_previous != NULL)
4301 yb = y_previous;
4302 else
4303 yb = &(y_regs[0]);
4304 }
4305 else
4306 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004307
4308#ifdef FEAT_EVAL
4309 if (name == MB_TOLOWER(redir_reg)
4310 || (redir_reg == '"' && yb == y_previous))
4311 continue; /* do not list register being written to, the
4312 * pointer can be freed */
4313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 if (yb->y_array != NULL)
4316 {
4317 msg_putchar('\n');
4318 msg_putchar('"');
4319 msg_putchar(name);
4320 MSG_PUTS(" ");
4321
4322 n = (int)Columns - 6;
4323 for (j = 0; j < yb->y_size && n > 1; ++j)
4324 {
4325 if (j)
4326 {
4327 MSG_PUTS_ATTR("^J", attr);
4328 n -= 2;
4329 }
4330 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004333 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004334#endif
4335 msg_outtrans_len(p, clen);
4336#ifdef FEAT_MBYTE
4337 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#endif
4339 }
4340 }
4341 if (n > 1 && yb->y_type == MLINE)
4342 MSG_PUTS_ATTR("^J", attr);
4343 out_flush(); /* show one line at a time */
4344 }
4345 ui_breakcheck();
4346 }
4347
4348 /*
4349 * display last inserted text
4350 */
4351 if ((p = get_last_insert()) != NULL
4352 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4353 {
4354 MSG_PUTS("\n\". ");
4355 dis_msg(p, TRUE);
4356 }
4357
4358 /*
4359 * display last command line
4360 */
4361 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4362 && !got_int)
4363 {
4364 MSG_PUTS("\n\": ");
4365 dis_msg(last_cmdline, FALSE);
4366 }
4367
4368 /*
4369 * display current file name
4370 */
4371 if (curbuf->b_fname != NULL
4372 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4373 {
4374 MSG_PUTS("\n\"% ");
4375 dis_msg(curbuf->b_fname, FALSE);
4376 }
4377
4378 /*
4379 * display alternate file name
4380 */
4381 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4382 {
4383 char_u *fname;
4384 linenr_T dummy;
4385
4386 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4387 {
4388 MSG_PUTS("\n\"# ");
4389 dis_msg(fname, FALSE);
4390 }
4391 }
4392
4393 /*
4394 * display last search pattern
4395 */
4396 if (last_search_pat() != NULL
4397 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4398 {
4399 MSG_PUTS("\n\"/ ");
4400 dis_msg(last_search_pat(), FALSE);
4401 }
4402
4403#ifdef FEAT_EVAL
4404 /*
4405 * display last used expression
4406 */
4407 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4408 && !got_int)
4409 {
4410 MSG_PUTS("\n\"= ");
4411 dis_msg(expr_line, FALSE);
4412 }
4413#endif
4414}
4415
4416/*
4417 * display a string for do_dis()
4418 * truncate at end of screen line
4419 */
4420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421dis_msg(
4422 char_u *p,
4423 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int n;
4426#ifdef FEAT_MBYTE
4427 int l;
4428#endif
4429
4430 n = (int)Columns - 6;
4431 while (*p != NUL
4432 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4433 && (n -= ptr2cells(p)) >= 0)
4434 {
4435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004436 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 msg_outtrans_len(p, l);
4439 p += l;
4440 }
4441 else
4442#endif
4443 msg_outtrans_len(p++, 1);
4444 }
4445 ui_breakcheck();
4446}
4447
Bram Moolenaar81340392012-06-06 16:12:59 +02004448#if defined(FEAT_COMMENTS) || defined(PROTO)
4449/*
4450 * If "process" is TRUE and the line begins with a comment leader (possibly
4451 * after some white space), return a pointer to the text after it. Put a boolean
4452 * value indicating whether the line ends with an unclosed comment in
4453 * "is_comment".
4454 * line - line to be processed,
4455 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004456 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004457 * include_space - whether to also skip space following the comment leader,
4458 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004459 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004460 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004461 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004462skip_comment(
4463 char_u *line,
4464 int process,
4465 int include_space,
4466 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004467{
4468 char_u *comment_flags = NULL;
4469 int lead_len;
4470 int leader_offset = get_last_leader_offset(line, &comment_flags);
4471
4472 *is_comment = FALSE;
4473 if (leader_offset != -1)
4474 {
4475 /* Let's check whether the line ends with an unclosed comment.
4476 * If the last comment leader has COM_END in flags, there's no comment.
4477 */
4478 while (*comment_flags)
4479 {
4480 if (*comment_flags == COM_END
4481 || *comment_flags == ':')
4482 break;
4483 ++comment_flags;
4484 }
4485 if (*comment_flags != COM_END)
4486 *is_comment = TRUE;
4487 }
4488
4489 if (process == FALSE)
4490 return line;
4491
4492 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4493
4494 if (lead_len == 0)
4495 return line;
4496
4497 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004498 * - COM_END,
4499 * - colon,
4500 * whichever comes first.
4501 */
4502 while (*comment_flags)
4503 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004504 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004505 || *comment_flags == ':')
4506 {
4507 break;
4508 }
4509 ++comment_flags;
4510 }
4511
4512 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004513 * starting with a closing part of a three-part comment. That's good,
4514 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004515 */
4516 if (*comment_flags == ':' || *comment_flags == NUL)
4517 line += lead_len;
4518
4519 return line;
4520}
4521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004524 * Join 'count' lines (minimal 2) at cursor position.
4525 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004526 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4527 * leaders should not be removed.
4528 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4529 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004531 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 */
4533 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004534do_join(
4535 long count,
4536 int insert_space,
4537 int save_undo,
4538 int use_formatoptions UNUSED,
4539 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004541 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004542 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004543 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004545 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004546 int endcurr1 = NUL;
4547 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004548 int currsize = 0; /* size of the current line */
4549 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004551 colnr_T col = 0;
4552 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004553#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004554 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004555 int remove_comments = (use_formatoptions == TRUE)
4556 && has_format_option(FO_REMOVE_COMS);
4557 int prev_was_comment;
4558#endif
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004561 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4562 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004565 /* Allocate an array to store the number of spaces inserted before each
4566 * line. We will use it to pre-compute the length of the new line and the
4567 * proper placement of each original line in the new one. */
4568 spaces = lalloc_clear((long_u)count, TRUE);
4569 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004571#if defined(FEAT_COMMENTS) || defined(PROTO)
4572 if (remove_comments)
4573 {
4574 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4575 if (comments == NULL)
4576 {
4577 vim_free(spaces);
4578 return FAIL;
4579 }
4580 }
4581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004584 * Don't move anything, just compute the final line length
4585 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004587 for (t = 0; t < count; ++t)
4588 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004589 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004590 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004591 {
4592 /* Set the '[ mark. */
4593 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4594 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4595 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004596#if defined(FEAT_COMMENTS) || defined(PROTO)
4597 if (remove_comments)
4598 {
4599 /* We don't want to remove the comment leader if the
4600 * previous line is not a comment. */
4601 if (t > 0 && prev_was_comment)
4602 {
4603
4604 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4605 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004606 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004607 curr = new_curr;
4608 }
4609 else
4610 curr = skip_comment(curr, FALSE, insert_space,
4611 &prev_was_comment);
4612 }
4613#endif
4614
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004615 if (insert_space && t > 0)
4616 {
4617 curr = skipwhite(curr);
4618 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4619#ifdef FEAT_MBYTE
4620 && (!has_format_option(FO_MBYTE_JOIN)
4621 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4622 && (!has_format_option(FO_MBYTE_JOIN2)
4623 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4624#endif
4625 )
4626 {
4627 /* don't add a space if the line is ending in a space */
4628 if (endcurr1 == ' ')
4629 endcurr1 = endcurr2;
4630 else
4631 ++spaces[t];
4632 /* extra space when 'joinspaces' set and line ends in '.' */
4633 if ( p_js
4634 && (endcurr1 == '.'
4635 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4636 && (endcurr1 == '?' || endcurr1 == '!'))))
4637 ++spaces[t];
4638 }
4639 }
4640 currsize = (int)STRLEN(curr);
4641 sumsize += currsize + spaces[t];
4642 endcurr1 = endcurr2 = NUL;
4643 if (insert_space && currsize > 0)
4644 {
4645#ifdef FEAT_MBYTE
4646 if (has_mbyte)
4647 {
4648 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004649 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004650 endcurr1 = (*mb_ptr2char)(cend);
4651 if (cend > curr)
4652 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004653 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004654 endcurr2 = (*mb_ptr2char)(cend);
4655 }
4656 }
4657 else
4658#endif
4659 {
4660 endcurr1 = *(curr + currsize - 1);
4661 if (currsize > 1)
4662 endcurr2 = *(curr + currsize - 2);
4663 }
4664 }
4665 line_breakcheck();
4666 if (got_int)
4667 {
4668 ret = FAIL;
4669 goto theend;
4670 }
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004673 /* store the column position before last line */
4674 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004676 /* allocate the space for the new line */
4677 newp = alloc_check((unsigned)(sumsize + 1));
4678 cend = newp + sumsize;
4679 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004681 /*
4682 * Move affected lines to the new long one.
4683 *
4684 * Move marks from each deleted line to the joined line, adjusting the
4685 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4686 * should not really be a problem.
4687 */
4688 for (t = count - 1; ; --t)
4689 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004690 int spaces_removed;
4691
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004692 cend -= currsize;
4693 mch_memmove(cend, curr, (size_t)currsize);
4694 if (spaces[t] > 0)
4695 {
4696 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004697 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004698 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004699
4700 // If deleting more spaces than adding, the cursor moves no more than
4701 // what is added if it is inside these spaces.
4702 spaces_removed = (curr - curr_start) - spaces[t];
4703
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004704 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004705 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004706 if (t == 0)
4707 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004708 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004709#if defined(FEAT_COMMENTS) || defined(PROTO)
4710 if (remove_comments)
4711 curr += comments[t - 1];
4712#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004713 if (insert_space && t > 1)
4714 curr = skipwhite(curr);
4715 currsize = (int)STRLEN(curr);
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4718
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004719 if (setmark)
4720 {
4721 /* Set the '] mark. */
4722 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4723 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4724 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /* Only report the change in the first line here, del_lines() will report
4727 * the deleted line. */
4728 changed_lines(curwin->w_cursor.lnum, currsize,
4729 curwin->w_cursor.lnum + 1, 0L);
4730
4731 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004732 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 * briefly, and then move it back. After del_lines() the cursor may
4734 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
4736 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004738 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 curwin->w_cursor.lnum = t;
4740
4741 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004742 * Set the cursor column:
4743 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004744 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004746 curwin->w_cursor.col =
4747 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750#ifdef FEAT_VIRTUALEDIT
4751 curwin->w_cursor.coladd = 0;
4752#endif
4753 curwin->w_set_curswant = TRUE;
4754
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004755theend:
4756 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004757#if defined(FEAT_COMMENTS) || defined(PROTO)
4758 if (remove_comments)
4759 vim_free(comments);
4760#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004761 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
4763
4764#ifdef FEAT_COMMENTS
4765/*
4766 * Return TRUE if the two comment leaders given are the same. "lnum" is
4767 * the first line. White-space is ignored. Note that the whole of
4768 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4769 */
4770 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004771same_leader(
4772 linenr_T lnum,
4773 int leader1_len,
4774 char_u *leader1_flags,
4775 int leader2_len,
4776 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 int idx1 = 0, idx2 = 0;
4779 char_u *p;
4780 char_u *line1;
4781 char_u *line2;
4782
4783 if (leader1_len == 0)
4784 return (leader2_len == 0);
4785
4786 /*
4787 * If first leader has 'f' flag, the lines can be joined only if the
4788 * second line does not have a leader.
4789 * If first leader has 'e' flag, the lines can never be joined.
4790 * If fist leader has 's' flag, the lines can only be joined if there is
4791 * some text after it and the second line has the 'm' flag.
4792 */
4793 if (leader1_flags != NULL)
4794 {
4795 for (p = leader1_flags; *p && *p != ':'; ++p)
4796 {
4797 if (*p == COM_FIRST)
4798 return (leader2_len == 0);
4799 if (*p == COM_END)
4800 return FALSE;
4801 if (*p == COM_START)
4802 {
4803 if (*(ml_get(lnum) + leader1_len) == NUL)
4804 return FALSE;
4805 if (leader2_flags == NULL || leader2_len == 0)
4806 return FALSE;
4807 for (p = leader2_flags; *p && *p != ':'; ++p)
4808 if (*p == COM_MIDDLE)
4809 return TRUE;
4810 return FALSE;
4811 }
4812 }
4813 }
4814
4815 /*
4816 * Get current line and next line, compare the leaders.
4817 * The first line has to be saved, only one line can be locked at a time.
4818 */
4819 line1 = vim_strsave(ml_get(lnum));
4820 if (line1 != NULL)
4821 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004822 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 ;
4824 line2 = ml_get(lnum + 1);
4825 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4826 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004827 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 if (line1[idx1++] != line2[idx2])
4830 break;
4831 }
4832 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004833 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 ++idx1;
4835 }
4836 vim_free(line1);
4837 }
4838 return (idx2 == leader2_len && idx1 == leader1_len);
4839}
4840#endif
4841
4842/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004843 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 */
4845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004846op_format(
4847 oparg_T *oap,
4848 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 long old_line_count = curbuf->b_ml.ml_line_count;
4851
4852 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4853 * can put it back there. */
4854 curwin->w_cursor = oap->cursor_start;
4855
4856 if (u_save((linenr_T)(oap->start.lnum - 1),
4857 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4858 return;
4859 curwin->w_cursor = oap->start;
4860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (oap->is_VIsual)
4862 /* When there is no change: need to remove the Visual selection */
4863 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
4865 /* Set '[ mark at the start of the formatted area */
4866 curbuf->b_op_start = oap->start;
4867
4868 /* For "gw" remember the cursor position and put it back below (adjusted
4869 * for joined and split lines). */
4870 if (keep_cursor)
4871 saved_cursor = oap->cursor_start;
4872
Bram Moolenaar81a82092008-03-12 16:27:00 +00004873 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Leave the cursor at the first non-blank of the last formatted line.
4877 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4878 * line, so "." will do the next lines.
4879 */
4880 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4881 ++curwin->w_cursor.lnum;
4882 beginline(BL_WHITE | BL_FIX);
4883 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4884 msgmore(old_line_count);
4885
4886 /* put '] mark on the end of the formatted area */
4887 curbuf->b_op_end = curwin->w_cursor;
4888
4889 if (keep_cursor)
4890 {
4891 curwin->w_cursor = saved_cursor;
4892 saved_cursor.lnum = 0;
4893 }
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (oap->is_VIsual)
4896 {
4897 win_T *wp;
4898
4899 FOR_ALL_WINDOWS(wp)
4900 {
4901 if (wp->w_old_cursor_lnum != 0)
4902 {
4903 /* When lines have been inserted or deleted, adjust the end of
4904 * the Visual area to be redrawn. */
4905 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4906 wp->w_old_cursor_lnum += old_line_count;
4907 else
4908 wp->w_old_visual_lnum += old_line_count;
4909 }
4910 }
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912}
4913
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004914#if defined(FEAT_EVAL) || defined(PROTO)
4915/*
4916 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4917 */
4918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004919op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004920{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004921 if (oap->is_VIsual)
4922 /* When there is no change: need to remove the Visual selection */
4923 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004924
Bram Moolenaar700303e2010-07-11 17:35:50 +02004925 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4926 /* As documented: when 'formatexpr' returns non-zero fall back to
4927 * internal formatting. */
4928 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004929}
4930
4931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004932fex_format(
4933 linenr_T lnum,
4934 long count,
4935 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004936{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004937 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4938 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004939 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004940 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004941
4942 /*
4943 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004944 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004945 */
4946 set_vim_var_nr(VV_LNUM, lnum);
4947 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004948 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004949
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004950 /* Make a copy, the option could be changed while calling it. */
4951 fex = vim_strsave(curbuf->b_p_fex);
4952 if (fex == NULL)
4953 return 0;
4954
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004955 /*
4956 * Evaluate the function.
4957 */
4958 if (use_sandbox)
4959 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004960 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004961 if (use_sandbox)
4962 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004963
4964 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004965 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004966
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004967 return r;
4968}
4969#endif
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971/*
4972 * Format "line_count" lines, starting at the cursor position.
4973 * When "line_count" is negative, format until the end of the paragraph.
4974 * Lines after the cursor line are saved for undo, caller must have saved the
4975 * first line.
4976 */
4977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004978format_lines(
4979 linenr_T line_count,
4980 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
4982 int max_len;
4983 int is_not_par; /* current line not part of parag. */
4984 int next_is_not_par; /* next line not part of paragraph */
4985 int is_end_par; /* at end of paragraph */
4986 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4987 int next_is_start_par = FALSE;
4988#ifdef FEAT_COMMENTS
4989 int leader_len = 0; /* leader len of current line */
4990 int next_leader_len; /* leader len of next line */
4991 char_u *leader_flags = NULL; /* flags for leader of current line */
4992 char_u *next_leader_flags; /* flags for leader of next line */
4993 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004994 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#endif
4996 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004997 int second_indent = -1; /* indent for second line (comment
4998 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 int do_second_indent;
5000 int do_number_indent;
5001 int do_trail_white;
5002 int first_par_line = TRUE;
5003 int smd_save;
5004 long count;
5005 int need_set_indent = TRUE; /* set indent of next paragraph */
5006 int force_format = FALSE;
5007 int old_State = State;
5008
5009 /* length of a line to force formatting: 3 * 'tw' */
5010 max_len = comp_textwidth(TRUE) * 3;
5011
5012 /* check for 'q', '2' and '1' in 'formatoptions' */
5013#ifdef FEAT_COMMENTS
5014 do_comments = has_format_option(FO_Q_COMS);
5015#endif
5016 do_second_indent = has_format_option(FO_Q_SECOND);
5017 do_number_indent = has_format_option(FO_Q_NUMBER);
5018 do_trail_white = has_format_option(FO_WHITE_PAR);
5019
5020 /*
5021 * Get info about the previous and current line.
5022 */
5023 if (curwin->w_cursor.lnum > 1)
5024 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5025#ifdef FEAT_COMMENTS
5026 , &leader_len, &leader_flags, do_comments
5027#endif
5028 );
5029 else
5030 is_not_par = TRUE;
5031 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5032#ifdef FEAT_COMMENTS
5033 , &next_leader_len, &next_leader_flags, do_comments
5034#endif
5035 );
5036 is_end_par = (is_not_par || next_is_not_par);
5037 if (!is_end_par && do_trail_white)
5038 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5039
5040 curwin->w_cursor.lnum--;
5041 for (count = line_count; count != 0 && !got_int; --count)
5042 {
5043 /*
5044 * Advance to next paragraph.
5045 */
5046 if (advance)
5047 {
5048 curwin->w_cursor.lnum++;
5049 prev_is_end_par = is_end_par;
5050 is_not_par = next_is_not_par;
5051#ifdef FEAT_COMMENTS
5052 leader_len = next_leader_len;
5053 leader_flags = next_leader_flags;
5054#endif
5055 }
5056
5057 /*
5058 * The last line to be formatted.
5059 */
5060 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5061 {
5062 next_is_not_par = TRUE;
5063#ifdef FEAT_COMMENTS
5064 next_leader_len = 0;
5065 next_leader_flags = NULL;
5066#endif
5067 }
5068 else
5069 {
5070 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5071#ifdef FEAT_COMMENTS
5072 , &next_leader_len, &next_leader_flags, do_comments
5073#endif
5074 );
5075 if (do_number_indent)
5076 next_is_start_par =
5077 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5078 }
5079 advance = TRUE;
5080 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5081 if (!is_end_par && do_trail_white)
5082 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5083
5084 /*
5085 * Skip lines that are not in a paragraph.
5086 */
5087 if (is_not_par)
5088 {
5089 if (line_count < 0)
5090 break;
5091 }
5092 else
5093 {
5094 /*
5095 * For the first line of a paragraph, check indent of second line.
5096 * Don't do this for comments and empty lines.
5097 */
5098 if (first_par_line
5099 && (do_second_indent || do_number_indent)
5100 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005101 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005103 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005104 {
5105#ifdef FEAT_COMMENTS
5106 if (leader_len == 0 && next_leader_len == 0)
5107 {
5108 /* no comment found */
5109#endif
5110 second_indent =
5111 get_indent_lnum(curwin->w_cursor.lnum + 1);
5112#ifdef FEAT_COMMENTS
5113 }
5114 else
5115 {
5116 second_indent = next_leader_len;
5117 do_comments_list = 1;
5118 }
5119#endif
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005122 {
5123#ifdef FEAT_COMMENTS
5124 if (leader_len == 0 && next_leader_len == 0)
5125 {
5126 /* no comment found */
5127#endif
5128 second_indent =
5129 get_number_indent(curwin->w_cursor.lnum);
5130#ifdef FEAT_COMMENTS
5131 }
5132 else
5133 {
5134 /* get_number_indent() is now "comment aware"... */
5135 second_indent =
5136 get_number_indent(curwin->w_cursor.lnum);
5137 do_comments_list = 1;
5138 }
5139#endif
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 /*
5144 * When the comment leader changes, it's the end of the paragraph.
5145 */
5146 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5147#ifdef FEAT_COMMENTS
5148 || !same_leader(curwin->w_cursor.lnum,
5149 leader_len, leader_flags,
5150 next_leader_len, next_leader_flags)
5151#endif
5152 )
5153 is_end_par = TRUE;
5154
5155 /*
5156 * If we have got to the end of a paragraph, or the line is
5157 * getting long, format it.
5158 */
5159 if (is_end_par || force_format)
5160 {
5161 if (need_set_indent)
5162 /* replace indent in first line with minimal number of
5163 * tabs and spaces, according to current options */
5164 (void)set_indent(get_indent(), SIN_CHANGED);
5165
5166 /* put cursor on last non-space */
5167 State = NORMAL; /* don't go past end-of-line */
5168 coladvance((colnr_T)MAXCOL);
5169 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5170 dec_cursor();
5171
5172 /* do the formatting, without 'showmode' */
5173 State = INSERT; /* for open_line() */
5174 smd_save = p_smd;
5175 p_smd = FALSE;
5176 insertchar(NUL, INSCHAR_FORMAT
5177#ifdef FEAT_COMMENTS
5178 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005179 + (do_comments && do_comments_list
5180 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005182 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 State = old_State;
5184 p_smd = smd_save;
5185 second_indent = -1;
5186 /* at end of par.: need to set indent of next par. */
5187 need_set_indent = is_end_par;
5188 if (is_end_par)
5189 {
5190 /* When called with a negative line count, break at the
5191 * end of the paragraph. */
5192 if (line_count < 0)
5193 break;
5194 first_par_line = TRUE;
5195 }
5196 force_format = FALSE;
5197 }
5198
5199 /*
5200 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005201 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
5203 if (!is_end_par)
5204 {
5205 advance = FALSE;
5206 curwin->w_cursor.lnum++;
5207 curwin->w_cursor.col = 0;
5208 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005212 {
5213 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005215 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005216 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005218 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5219 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005220 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005221
5222 if (indent > 0)
5223 {
5224 (void)del_bytes(indent, FALSE, FALSE);
5225 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005226 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005227 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005230 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 beep_flush();
5233 break;
5234 }
5235 first_par_line = FALSE;
5236 /* If the line is getting long, format it next time */
5237 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5238 force_format = TRUE;
5239 else
5240 force_format = FALSE;
5241 }
5242 }
5243 line_breakcheck();
5244 }
5245}
5246
5247/*
5248 * Return TRUE if line "lnum" ends in a white character.
5249 */
5250 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 char_u *s = ml_get(lnum);
5254 size_t l;
5255
5256 if (*s == NUL)
5257 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005258 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 * invocation may call function multiple times". */
5260 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005261 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262}
5263
5264/*
5265 * Blank lines, and lines containing only the comment leader, are left
5266 * untouched by the formatting. The function returns TRUE in this
5267 * case. It also returns TRUE when a line starts with the end of a comment
5268 * ('e' in comment flags), so that this line is skipped, and not joined to the
5269 * previous line. A new paragraph starts after a blank line, or when the
5270 * comment leader changes -- webb.
5271 */
5272#ifdef FEAT_COMMENTS
5273 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005274fmt_check_par(
5275 linenr_T lnum,
5276 int *leader_len,
5277 char_u **leader_flags,
5278 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 char_u *flags = NULL; /* init for GCC */
5281 char_u *ptr;
5282
5283 ptr = ml_get(lnum);
5284 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005285 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 else
5287 *leader_len = 0;
5288
5289 if (*leader_len > 0)
5290 {
5291 /*
5292 * Search for 'e' flag in comment leader flags.
5293 */
5294 flags = *leader_flags;
5295 while (*flags && *flags != ':' && *flags != COM_END)
5296 ++flags;
5297 }
5298
5299 return (*skipwhite(ptr + *leader_len) == NUL
5300 || (*leader_len > 0 && *flags == COM_END)
5301 || startPS(lnum, NUL, FALSE));
5302}
5303#else
5304 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5308}
5309#endif
5310
5311/*
5312 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5313 * previous line is in the same paragraph. Used for auto-formatting.
5314 */
5315 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 char_u *p;
5319#ifdef FEAT_COMMENTS
5320 int leader_len = 0; /* leader len of current line */
5321 char_u *leader_flags = NULL; /* flags for leader of current line */
5322 int next_leader_len; /* leader len of next line */
5323 char_u *next_leader_flags; /* flags for leader of next line */
5324 int do_comments; /* format comments */
5325#endif
5326
5327 if (lnum <= 1)
5328 return TRUE; /* start of the file */
5329
5330 p = ml_get(lnum - 1);
5331 if (*p == NUL)
5332 return TRUE; /* after empty line */
5333
5334#ifdef FEAT_COMMENTS
5335 do_comments = has_format_option(FO_Q_COMS);
5336#endif
5337 if (fmt_check_par(lnum - 1
5338#ifdef FEAT_COMMENTS
5339 , &leader_len, &leader_flags, do_comments
5340#endif
5341 ))
5342 return TRUE; /* after non-paragraph line */
5343
5344 if (fmt_check_par(lnum
5345#ifdef FEAT_COMMENTS
5346 , &next_leader_len, &next_leader_flags, do_comments
5347#endif
5348 ))
5349 return TRUE; /* "lnum" is not a paragraph line */
5350
5351 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5352 return TRUE; /* missing trailing space in previous line. */
5353
5354 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5355 return TRUE; /* numbered item starts in "lnum". */
5356
5357#ifdef FEAT_COMMENTS
5358 if (!same_leader(lnum - 1, leader_len, leader_flags,
5359 next_leader_len, next_leader_flags))
5360 return TRUE; /* change of comment leader. */
5361#endif
5362
5363 return FALSE;
5364}
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366/*
5367 * prepare a few things for block mode yank/delete/tilde
5368 *
5369 * for delete:
5370 * - textlen includes the first/last char to be (partly) deleted
5371 * - start/endspaces is the number of columns that are taken by the
5372 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005373 * deleted.
5374 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 * - textlen includes the first/last char to be wholly yanked
5376 * - start/endspaces is the number of columns of the first/last yanked char
5377 * that are to be yanked.
5378 */
5379 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005380block_prep(
5381 oparg_T *oap,
5382 struct block_def *bdp,
5383 linenr_T lnum,
5384 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 int incr = 0;
5387 char_u *pend;
5388 char_u *pstart;
5389 char_u *line;
5390 char_u *prev_pstart;
5391 char_u *prev_pend;
5392
5393 bdp->startspaces = 0;
5394 bdp->endspaces = 0;
5395 bdp->textlen = 0;
5396 bdp->start_vcol = 0;
5397 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 bdp->is_short = FALSE;
5399 bdp->is_oneChar = FALSE;
5400 bdp->pre_whitesp = 0;
5401 bdp->pre_whitesp_c = 0;
5402 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 bdp->start_char_vcols = 0;
5404
5405 line = ml_get(lnum);
5406 pstart = line;
5407 prev_pstart = line;
5408 while (bdp->start_vcol < oap->start_vcol && *pstart)
5409 {
5410 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005411 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005413 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 bdp->pre_whitesp += incr;
5416 bdp->pre_whitesp_c++;
5417 }
5418 else
5419 {
5420 bdp->pre_whitesp = 0;
5421 bdp->pre_whitesp_c = 0;
5422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005424 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
5426 bdp->start_char_vcols = incr;
5427 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5428 {
5429 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (!is_del || oap->op_type == OP_APPEND)
5432 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5433 }
5434 else
5435 {
5436 /* notice: this converts partly selected Multibyte characters to
5437 * spaces, too. */
5438 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5439 if (is_del && bdp->startspaces)
5440 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5441 pend = pstart;
5442 bdp->end_vcol = bdp->start_vcol;
5443 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 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 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 /* Alternative: include spaces to fill up the block.
5484 * Disadvantage: can lead to trailing spaces when the line is
5485 * short where the text is put */
5486 /* if (!is_del || oap->op_type == OP_APPEND) */
5487 if (oap->op_type == OP_APPEND || virtual_op)
5488 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005489 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 else
5491 bdp->endspaces = 0; /* replace doesn't add characters */
5492 }
5493 else if (bdp->end_vcol > oap->end_vcol)
5494 {
5495 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5496 if (!is_del && bdp->endspaces)
5497 {
5498 bdp->endspaces = incr - bdp->endspaces;
5499 if (pend != pstart)
5500 pend = prev_pend;
5501 }
5502 }
5503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (is_del && bdp->startspaces)
5506 pstart = prev_pstart;
5507 bdp->textlen = (int)(pend - pstart);
5508 }
5509 bdp->textcol = (colnr_T) (pstart - line);
5510 bdp->textstart = pstart;
5511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005514 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517op_addsub(
5518 oparg_T *oap,
5519 linenr_T Prenum1, /* Amount of add/subtract */
5520 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005522 pos_T pos;
5523 struct block_def bd;
5524 int change_cnt = 0;
5525 linenr_T amount = Prenum1;
5526
Bram Moolenaar6b731882018-11-16 20:54:47 +01005527 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
5528 // buffer is not completly updated yet. Postpone updating folds until before
5529 // the call to changed_lines().
5530#ifdef FEAT_FOLDING
5531 disable_fold_update++;
5532#endif
5533
Bram Moolenaard79e5502016-01-10 22:13:02 +01005534 if (!VIsual_active)
5535 {
5536 pos = curwin->w_cursor;
5537 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005538 {
5539#ifdef FEAT_FOLDING
5540 disable_fold_update--;
5541#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005542 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005543 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005544 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005545#ifdef FEAT_FOLDING
5546 disable_fold_update--;
5547#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005548 if (change_cnt)
5549 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5550 }
5551 else
5552 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005553 int one_change;
5554 int length;
5555 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005556
5557 if (u_save((linenr_T)(oap->start.lnum - 1),
5558 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005559 {
5560#ifdef FEAT_FOLDING
5561 disable_fold_update--;
5562#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005563 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005564 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005565
5566 pos = oap->start;
5567 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5568 {
5569 if (oap->block_mode) /* Visual block mode */
5570 {
5571 block_prep(oap, &bd, pos.lnum, FALSE);
5572 pos.col = bd.textcol;
5573 length = bd.textlen;
5574 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005575 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005576 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005577 curwin->w_cursor.col = 0;
5578 pos.col = 0;
5579 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5580 }
5581 else /* oap->motion_type == MCHAR */
5582 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005583 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005584 dec(&(oap->end));
5585 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5586 pos.col = 0;
5587 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005588 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005589 pos.col += oap->start.col;
5590 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005591 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005592 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005593 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005594 length = (int)STRLEN(ml_get(oap->end.lnum));
5595 if (oap->end.col >= length)
5596 oap->end.col = length - 1;
5597 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005598 }
5599 }
5600 one_change = do_addsub(oap->op_type, &pos, length, amount);
5601 if (one_change)
5602 {
5603 /* Remember the start position of the first change. */
5604 if (change_cnt == 0)
5605 startpos = curbuf->b_op_start;
5606 ++change_cnt;
5607 }
5608
5609#ifdef FEAT_NETBEANS_INTG
5610 if (netbeans_active() && one_change)
5611 {
5612 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5613
5614 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5615 netbeans_inserted(curbuf, pos.lnum, pos.col,
5616 &ptr[pos.col], length);
5617 }
5618#endif
5619 if (g_cmd && one_change)
5620 amount += Prenum1;
5621 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005622
5623#ifdef FEAT_FOLDING
5624 disable_fold_update--;
5625#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005626 if (change_cnt)
5627 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5628
5629 if (!change_cnt && oap->is_VIsual)
5630 /* No change: need to remove the Visual selection */
5631 redraw_curbuf_later(INVERTED);
5632
5633 /* Set '[ mark if something changed. Keep the last end
5634 * position from do_addsub(). */
5635 if (change_cnt > 0)
5636 curbuf->b_op_start = startpos;
5637
5638 if (change_cnt > p_report)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005639 smsg((char_u *)NGETTEXT("%ld line changed", "%ld lines changed",
5640 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005641 }
5642}
5643
5644/*
5645 * Add or subtract 'Prenum1' from a number in a line
5646 * op_type is OP_NR_ADD or OP_NR_SUB
5647 *
5648 * Returns TRUE if some character was changed.
5649 */
5650 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005651do_addsub(
5652 int op_type,
5653 pos_T *pos,
5654 int length,
5655 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005656{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 int col;
5658 char_u *buf1;
5659 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005660 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005662 uvarnumber_T n;
5663 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 char_u *ptr;
5665 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 int todel;
5667 int dohex;
5668 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005669 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 int doalp;
5671 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005673 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005674 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005675 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005676 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005677 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005678 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005679 pos_T startpos;
5680 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5683 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005684 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5686
Bram Moolenaard79e5502016-01-10 22:13:02 +01005687 curwin->w_cursor = *pos;
5688 ptr = ml_get(pos->lnum);
5689 col = pos->col;
5690
5691 if (*ptr == NUL)
5692 goto theend;
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /*
5695 * First check if we are on a hexadecimal number, after the "0x".
5696 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005697 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005699 if (dobin)
5700 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005701 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005702 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005703#ifdef FEAT_MBYTE
5704 if (has_mbyte)
5705 col -= (*mb_head_off)(ptr, ptr + col);
5706#endif
5707 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005708
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005709 if (dohex)
5710 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005711 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005712 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005713#ifdef FEAT_MBYTE
5714 if (has_mbyte)
5715 col -= (*mb_head_off)(ptr, ptr + col);
5716#endif
5717 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005718
5719 if ( dobin
5720 && dohex
5721 && ! ((col > 0
5722 && (ptr[col] == 'X'
5723 || ptr[col] == 'x')
5724 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005725#ifdef FEAT_MBYTE
5726 && (!has_mbyte ||
5727 !(*mb_head_off)(ptr, ptr + col - 1))
5728#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005729 && vim_isxdigit(ptr[col + 1]))))
5730 {
5731
5732 /* In case of binary/hexadecimal pattern overlap match, rescan */
5733
Bram Moolenaard79e5502016-01-10 22:13:02 +01005734 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005735
5736 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005737 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005738 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005739#ifdef FEAT_MBYTE
5740 if (has_mbyte)
5741 col -= (*mb_head_off)(ptr, ptr + col);
5742#endif
5743 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005744 }
5745
5746 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005747 && col > 0
5748 && (ptr[col] == 'X'
5749 || ptr[col] == 'x')
5750 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005751#ifdef FEAT_MBYTE
5752 && (!has_mbyte ||
5753 !(*mb_head_off)(ptr, ptr + col - 1))
5754#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005755 && vim_isxdigit(ptr[col + 1])) ||
5756 ( dobin
5757 && col > 0
5758 && (ptr[col] == 'B'
5759 || ptr[col] == 'b')
5760 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005761#ifdef FEAT_MBYTE
5762 && (!has_mbyte ||
5763 !(*mb_head_off)(ptr, ptr + col - 1))
5764#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005765 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005766 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005767 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005768 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005769#ifdef FEAT_MBYTE
5770 if (has_mbyte)
5771 col -= (*mb_head_off)(ptr, ptr + col);
5772#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005773 }
5774 else
5775 {
5776 /*
5777 * Search forward and then backward to find the start of number.
5778 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005779 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005780
5781 while (ptr[col] != NUL
5782 && !vim_isdigit(ptr[col])
5783 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005784 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005785
5786 while (col > 0
5787 && vim_isdigit(ptr[col - 1])
5788 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005789 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005790 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005791#ifdef FEAT_MBYTE
5792 if (has_mbyte)
5793 col -= (*mb_head_off)(ptr, ptr + col);
5794#endif
5795 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005796 }
5797 }
5798
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005799 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005800 {
5801 while (ptr[col] != NUL && length > 0
5802 && !vim_isdigit(ptr[col])
5803 && !(doalp && ASCII_ISALPHA(ptr[col])))
5804 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005805 int mb_len = MB_PTR2LEN(ptr + col);
5806
5807 col += mb_len;
5808 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005809 }
5810
5811 if (length == 0)
5812 goto theend;
5813
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005814 if (col > pos->col && ptr[col - 1] == '-'
5815#ifdef FEAT_MBYTE
5816 && (!has_mbyte ||
5817 !(*mb_head_off)(ptr, ptr + col - 1))
5818#endif
5819 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005820 {
5821 negative = TRUE;
5822 was_positive = FALSE;
5823 }
5824 }
5825
5826 /*
5827 * If a number was found, and saving for undo works, replace the number.
5828 */
5829 firstdigit = ptr[col];
5830 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5831 {
5832 beep_flush();
5833 goto theend;
5834 }
5835
5836 if (doalp && ASCII_ISALPHA(firstdigit))
5837 {
5838 /* decrement or increment alphabetic character */
5839 if (op_type == OP_NR_SUB)
5840 {
5841 if (CharOrd(firstdigit) < Prenum1)
5842 {
5843 if (isupper(firstdigit))
5844 firstdigit = 'A';
5845 else
5846 firstdigit = 'a';
5847 }
5848 else
5849#ifdef EBCDIC
5850 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5851#else
5852 firstdigit -= Prenum1;
5853#endif
5854 }
5855 else
5856 {
5857 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5858 {
5859 if (isupper(firstdigit))
5860 firstdigit = 'Z';
5861 else
5862 firstdigit = 'z';
5863 }
5864 else
5865#ifdef EBCDIC
5866 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5867#else
5868 firstdigit += Prenum1;
5869#endif
5870 }
5871 curwin->w_cursor.col = col;
5872 if (!did_change)
5873 startpos = curwin->w_cursor;
5874 did_change = TRUE;
5875 (void)del_char(FALSE);
5876 ins_char(firstdigit);
5877 endpos = curwin->w_cursor;
5878 curwin->w_cursor.col = col;
5879 }
5880 else
5881 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005882 if (col > 0 && ptr[col - 1] == '-'
5883#ifdef FEAT_MBYTE
5884 && (!has_mbyte ||
5885 !(*mb_head_off)(ptr, ptr + col - 1))
5886#endif
5887 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005888 {
5889 /* negative number */
5890 --col;
5891 negative = TRUE;
5892 }
5893 /* get the number value (unsigned) */
5894 if (visual && VIsual_mode != 'V')
5895 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5896 ? (int)STRLEN(ptr) - col
5897 : length);
5898
5899 vim_str2nr(ptr + col, &pre, &length,
5900 0 + (dobin ? STR2NR_BIN : 0)
5901 + (dooct ? STR2NR_OCT : 0)
5902 + (dohex ? STR2NR_HEX : 0),
5903 NULL, &n, maxlen);
5904
5905 /* ignore leading '-' for hex and octal and bin numbers */
5906 if (pre && negative)
5907 {
5908 ++col;
5909 --length;
5910 negative = FALSE;
5911 }
5912 /* add or subtract */
5913 subtract = FALSE;
5914 if (op_type == OP_NR_SUB)
5915 subtract ^= TRUE;
5916 if (negative)
5917 subtract ^= TRUE;
5918
5919 oldn = n;
5920 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005921 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005922 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005923 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005924 /* handle wraparound for decimal numbers */
5925 if (!pre)
5926 {
5927 if (subtract)
5928 {
5929 if (n > oldn)
5930 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005931 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005932 negative ^= TRUE;
5933 }
5934 }
5935 else
5936 {
5937 /* add */
5938 if (n < oldn)
5939 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005940 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005941 negative ^= TRUE;
5942 }
5943 }
5944 if (n == 0)
5945 negative = FALSE;
5946 }
5947
5948 if (visual && !was_positive && !negative && col > 0)
5949 {
5950 /* need to remove the '-' */
5951 col--;
5952 length++;
5953 }
5954
5955 /*
5956 * Delete the old number.
5957 */
5958 curwin->w_cursor.col = col;
5959 if (!did_change)
5960 startpos = curwin->w_cursor;
5961 did_change = TRUE;
5962 todel = length;
5963 c = gchar_cursor();
5964 /*
5965 * Don't include the '-' in the length, only the length of the
5966 * part after it is kept the same.
5967 */
5968 if (c == '-')
5969 --length;
5970 while (todel-- > 0)
5971 {
5972 if (c < 0x100 && isalpha(c))
5973 {
5974 if (isupper(c))
5975 hexupper = TRUE;
5976 else
5977 hexupper = FALSE;
5978 }
5979 /* del_char() will mark line needing displaying */
5980 (void)del_char(FALSE);
5981 c = gchar_cursor();
5982 }
5983
5984 /*
5985 * Prepare the leading characters in buf1[].
5986 * When there are many leading zeros it could be very long.
5987 * Allocate a bit too much.
5988 */
5989 buf1 = alloc((unsigned)length + NUMBUFLEN);
5990 if (buf1 == NULL)
5991 goto theend;
5992 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005993 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005994 {
5995 *ptr++ = '-';
5996 }
5997 if (pre)
5998 {
5999 *ptr++ = '0';
6000 --length;
6001 }
6002 if (pre == 'b' || pre == 'B' ||
6003 pre == 'x' || pre == 'X')
6004 {
6005 *ptr++ = pre;
6006 --length;
6007 }
6008
6009 /*
6010 * Put the number characters in buf2[].
6011 */
6012 if (pre == 'b' || pre == 'B')
6013 {
6014 int i;
6015 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006016 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006017
6018 /* leading zeros */
6019 for (bit = bits; bit > 0; bit--)
6020 if ((n >> (bit - 1)) & 0x1) break;
6021
6022 for (i = 0; bit > 0; bit--)
6023 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6024
6025 buf2[i] = '\0';
6026 }
6027 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006028 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6029 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006030 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006031 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6032 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006033 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006034 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6035 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006036 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006037 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6038 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006039 length -= (int)STRLEN(buf2);
6040
6041 /*
6042 * Adjust number of zeros to the new number of digits, so the
6043 * total length of the number remains the same.
6044 * Don't do this when
6045 * the result may look like an octal number.
6046 */
6047 if (firstdigit == '0' && !(dooct && pre == 0))
6048 while (length-- > 0)
6049 *ptr++ = '0';
6050 *ptr = NUL;
6051 STRCAT(buf1, buf2);
6052 ins_str(buf1); /* insert the new number */
6053 vim_free(buf1);
6054 endpos = curwin->w_cursor;
6055 if (did_change && curwin->w_cursor.col)
6056 --curwin->w_cursor.col;
6057 }
6058
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006059 if (did_change)
6060 {
6061 /* set the '[ and '] marks */
6062 curbuf->b_op_start = startpos;
6063 curbuf->b_op_end = endpos;
6064 if (curbuf->b_op_end.col > 0)
6065 --curbuf->b_op_end.col;
6066 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006067
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006068theend:
6069 if (visual)
6070 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006071 else if (did_change)
6072 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006073
Bram Moolenaard79e5502016-01-10 22:13:02 +01006074 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075}
6076
6077#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006078
6079static yankreg_T *y_read_regs = NULL;
6080
6081#define REG_PREVIOUS 1
6082#define REG_EXEC 2
6083
6084/*
6085 * Prepare for reading viminfo registers when writing viminfo later.
6086 */
6087 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006088prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006089{
6090 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6091 * (int)sizeof(yankreg_T));
6092}
6093
6094 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006095finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006096{
6097 int i;
6098 int j;
6099
6100 if (y_read_regs != NULL)
6101 {
6102 for (i = 0; i < NUM_REGISTERS; ++i)
6103 if (y_read_regs[i].y_array != NULL)
6104 {
6105 for (j = 0; j < y_read_regs[i].y_size; j++)
6106 vim_free(y_read_regs[i].y_array[j]);
6107 vim_free(y_read_regs[i].y_array);
6108 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006109 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006110 }
6111}
6112
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006114read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 int eof;
6117 int do_it = TRUE;
6118 int size;
6119 int limit;
6120 int i;
6121 int set_prev = FALSE;
6122 char_u *str;
6123 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006124 int new_type = MCHAR; /* init to shut up compiler */
6125 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 /* We only get here (hopefully) if line[0] == '"' */
6128 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006129
6130 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (*str == '"')
6132 {
6133 set_prev = TRUE;
6134 str++;
6135 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if (!ASCII_ISALNUM(*str) && *str != '-')
6138 {
6139 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6140 return TRUE; /* too many errors, pretend end-of-file */
6141 do_it = FALSE;
6142 }
6143 get_yank_register(*str++, FALSE);
6144 if (!force && y_current->y_array != NULL)
6145 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006146
6147 if (*str == '@')
6148 {
6149 /* "x@: register x used for @@ */
6150 if (force || execreg_lastc == NUL)
6151 execreg_lastc = str[-1];
6152 }
6153
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 size = 0;
6155 limit = 100; /* Optimized for registers containing <= 100 lines */
6156 if (do_it)
6157 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006158 /*
6159 * Build the new register in array[].
6160 * y_array is kept as-is until done.
6161 * The "do_it" flag is reset when something is wrong, in which case
6162 * array[] needs to be freed.
6163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (set_prev)
6165 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006166 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006167 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006169 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006171 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006173 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 /* get the block width; if it's missing we get a zero, which is OK */
6175 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006176 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178
6179 while (!(eof = viminfo_readline(virp))
6180 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6181 {
6182 if (do_it)
6183 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006184 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006186 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006188
6189 if (new_array == NULL)
6190 {
6191 do_it = FALSE;
6192 break;
6193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006195 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006197 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
6200 str = viminfo_readstring(virp, 1, TRUE);
6201 if (str != NULL)
6202 array[size++] = str;
6203 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006204 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 do_it = FALSE;
6206 }
6207 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (do_it)
6210 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006211 /* free y_array[] */
6212 for (i = 0; i < y_current->y_size; i++)
6213 vim_free(y_current->y_array[i]);
6214 vim_free(y_current->y_array);
6215
6216 y_current->y_type = new_type;
6217 y_current->y_width = new_width;
6218 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006219 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (size == 0)
6221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 y_current->y_array = NULL;
6223 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006224 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006226 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 y_current->y_array =
6228 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6229 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006230 {
6231 if (y_current->y_array == NULL)
6232 vim_free(array[i]);
6233 else
6234 y_current->y_array[i] = array[i];
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006238 else
6239 {
6240 /* Free array[] if it was filled. */
6241 for (i = 0; i < size; i++)
6242 vim_free(array[i]);
6243 }
6244 vim_free(array);
6245
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 return eof;
6247}
6248
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006249/*
6250 * Accept a new style register line from the viminfo, store it when it's new.
6251 */
6252 void
6253handle_viminfo_register(garray_T *values, int force)
6254{
6255 bval_T *vp = (bval_T *)values->ga_data;
6256 int flags;
6257 int name;
6258 int type;
6259 int linecount;
6260 int width;
6261 time_t timestamp;
6262 yankreg_T *y_ptr;
6263 int i;
6264
6265 /* Check the format:
6266 * |{bartype},{flags},{name},{type},
6267 * {linecount},{width},{timestamp},"line1","line2"
6268 */
6269 if (values->ga_len < 6
6270 || vp[0].bv_type != BVAL_NR
6271 || vp[1].bv_type != BVAL_NR
6272 || vp[2].bv_type != BVAL_NR
6273 || vp[3].bv_type != BVAL_NR
6274 || vp[4].bv_type != BVAL_NR
6275 || vp[5].bv_type != BVAL_NR)
6276 return;
6277 flags = vp[0].bv_nr;
6278 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006279 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006280 return;
6281 type = vp[2].bv_nr;
6282 if (type != MCHAR && type != MLINE && type != MBLOCK)
6283 return;
6284 linecount = vp[3].bv_nr;
6285 if (values->ga_len < 6 + linecount)
6286 return;
6287 width = vp[4].bv_nr;
6288 if (width < 0)
6289 return;
6290
6291 if (y_read_regs != NULL)
6292 /* Reading viminfo for merging and writing. Store the register
6293 * content, don't update the current registers. */
6294 y_ptr = &y_read_regs[name];
6295 else
6296 y_ptr = &y_regs[name];
6297
6298 /* Do not overwrite unless forced or the timestamp is newer. */
6299 timestamp = (time_t)vp[5].bv_nr;
6300 if (y_ptr->y_array != NULL && !force
6301 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6302 return;
6303
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006304 if (y_ptr->y_array != NULL)
6305 for (i = 0; i < y_ptr->y_size; i++)
6306 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006307 vim_free(y_ptr->y_array);
6308
6309 if (y_read_regs == NULL)
6310 {
6311 if (flags & REG_PREVIOUS)
6312 y_previous = y_ptr;
6313 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6314 execreg_lastc = get_register_name(name);
6315 }
6316 y_ptr->y_type = type;
6317 y_ptr->y_width = width;
6318 y_ptr->y_size = linecount;
6319 y_ptr->y_time_set = timestamp;
6320 if (linecount == 0)
6321 y_ptr->y_array = NULL;
6322 else
6323 {
6324 y_ptr->y_array =
6325 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6326 for (i = 0; i < linecount; i++)
6327 {
6328 if (vp[i + 6].bv_allocated)
6329 {
6330 y_ptr->y_array[i] = vp[i + 6].bv_string;
6331 vp[i + 6].bv_string = NULL;
6332 }
6333 else
6334 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6335 }
6336 }
6337}
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006342 int i, j;
6343 char_u *type;
6344 char_u c;
6345 int num_lines;
6346 int max_num_lines;
6347 int max_kbyte;
6348 long len;
6349 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
Bram Moolenaar64404472010-06-26 06:24:45 +02006351 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353 /* Get '<' value, use old '"' value if '<' is not found. */
6354 max_num_lines = get_viminfo_parameter('<');
6355 if (max_num_lines < 0)
6356 max_num_lines = get_viminfo_parameter('"');
6357 if (max_num_lines == 0)
6358 return;
6359 max_kbyte = get_viminfo_parameter('s');
6360 if (max_kbyte == 0)
6361 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 for (i = 0; i < NUM_REGISTERS; i++)
6364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365#ifdef FEAT_CLIPBOARD
6366 /* Skip '*'/'+' register, we don't want them back next time */
6367 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6368 continue;
6369#endif
6370#ifdef FEAT_DND
6371 /* Neither do we want the '~' register */
6372 if (i == TILDE_REGISTER)
6373 continue;
6374#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006375 /* When reading viminfo for merging and writing: Use the register from
6376 * viminfo if it's newer. */
6377 if (y_read_regs != NULL
6378 && y_read_regs[i].y_array != NULL
6379 && (y_regs[i].y_array == NULL ||
6380 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6381 y_ptr = &y_read_regs[i];
6382 else if (y_regs[i].y_array == NULL)
6383 continue;
6384 else
6385 y_ptr = &y_regs[i];
6386
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006387 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006388 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006389 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006390 || (num_lines == 1 && y_ptr->y_type == MCHAR
6391 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006392 continue;
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (max_kbyte > 0)
6395 {
6396 /* Skip register if there is more text than the maximum size. */
6397 len = 0;
6398 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006399 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (len > (long)max_kbyte * 1024L)
6401 continue;
6402 }
6403
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006404 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 case MLINE:
6407 type = (char_u *)"LINE";
6408 break;
6409 case MCHAR:
6410 type = (char_u *)"CHAR";
6411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 case MBLOCK:
6413 type = (char_u *)"BLOCK";
6414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 default:
6416 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006417 y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 emsg(IObuff);
6419 type = (char_u *)"LINE";
6420 break;
6421 }
6422 if (y_previous == &y_regs[i])
6423 fprintf(fp, "\"");
6424 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006425 fprintf(fp, "\"%c", c);
6426 if (c == execreg_lastc)
6427 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006428 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
6430 /* If max_num_lines < 0, then we save ALL the lines in the register */
6431 if (max_num_lines > 0 && num_lines > max_num_lines)
6432 num_lines = max_num_lines;
6433 for (j = 0; j < num_lines; j++)
6434 {
6435 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006436 viminfo_writestring(fp, y_ptr->y_array[j]);
6437 }
6438
6439 {
6440 int flags = 0;
6441 int remaining;
6442
6443 /* New style with a bar line. Format:
6444 * |{bartype},{flags},{name},{type},
6445 * {linecount},{width},{timestamp},"line1","line2"
6446 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006447 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006448 */
6449 if (y_previous == &y_regs[i])
6450 flags |= REG_PREVIOUS;
6451 if (c == execreg_lastc)
6452 flags |= REG_EXEC;
6453 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6454 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6455 (long)y_ptr->y_time_set);
6456 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6457 remaining = LSIZE - 71;
6458 for (j = 0; j < num_lines; j++)
6459 {
6460 putc(',', fp);
6461 --remaining;
6462 remaining = barline_writestring(fp, y_ptr->y_array[j],
6463 remaining);
6464 }
6465 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467 }
6468}
6469#endif /* FEAT_VIMINFO */
6470
6471#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6472/*
6473 * SELECTION / PRIMARY ('*')
6474 *
6475 * Text selection stuff that uses the GUI selection register '*'. When using a
6476 * GUI this may be text from another window, otherwise it is the last text we
6477 * had highlighted with VIsual mode. With mouse support, clicking the middle
6478 * button performs the paste, otherwise you will need to do <"*p>. "
6479 * If not under X, it is synonymous with the clipboard register '+'.
6480 *
6481 * X CLIPBOARD ('+')
6482 *
6483 * Text selection stuff that uses the GUI clipboard register '+'.
6484 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6485 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6486 * otherwise you will need to do <"+p>. "
6487 * If not under X, it is synonymous with the selection register '*'.
6488 */
6489
6490/*
6491 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006492 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 * only exist while the owning application exists, so we write to the
6494 * permanent (while X runs) store CUT_BUFFER0.
6495 * Dump the CLIPBOARD selection if we own it (it's logically the more
6496 * 'permanent' of the two), otherwise the PRIMARY one.
6497 * For now, use a hard-coded sanity limit of 1Mb of data.
6498 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006499#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006501x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 Display *dpy;
6504 char_u *str = NULL;
6505 long_u len = 0;
6506 int motion_type = -1;
6507
6508# ifdef FEAT_GUI
6509 if (gui.in_use)
6510 dpy = X_DISPLAY;
6511 else
6512# endif
6513# ifdef FEAT_XCLIPBOARD
6514 dpy = xterm_dpy;
6515# else
6516 return;
6517# endif
6518
6519 /* Get selection to export */
6520 if (clip_plus.owned)
6521 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6522 else if (clip_star.owned)
6523 motion_type = clip_convert_selection(&str, &len, &clip_star);
6524
6525 /* Check it's OK */
6526 if (dpy != NULL && str != NULL && motion_type >= 0
6527 && len < 1024*1024 && len > 0)
6528 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006529#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006530 int ok = TRUE;
6531
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006532 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6533 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6534 * encoding conversion usually doesn't work, so keep the text as-is.
6535 */
6536 if (has_mbyte)
6537 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006538 vimconv_T vc;
6539
6540 vc.vc_type = CONV_NONE;
6541 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6542 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006543 int intlen = len;
6544 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006545
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006546 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006547 conv_str = string_convert(&vc, str, &intlen);
6548 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006549 if (conv_str != NULL)
6550 {
6551 vim_free(str);
6552 str = conv_str;
6553 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006554 else
6555 {
6556 ok = FALSE;
6557 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006558 convert_setup(&vc, NULL, NULL);
6559 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006560 else
6561 {
6562 ok = FALSE;
6563 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006564 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006565
6566 /* Do not store the string if conversion failed. Better to use any
6567 * other selection than garbled text. */
6568 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006569#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006570 {
6571 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6572 XFlush(dpy);
6573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575
6576 vim_free(str);
6577}
6578#endif
6579
6580 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006581clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006583 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 if (cbd == &clip_plus)
6586 y_current = &y_regs[PLUS_REGISTER];
6587 else
6588 y_current = &y_regs[STAR_REGISTER];
6589 free_yank_all();
6590 y_current->y_size = 0;
6591 y_current = y_ptr;
6592}
6593
6594/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006595 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 */
6597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006598clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006600 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 pos_T old_visual;
6603 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 colnr_T old_curswant;
6605 int old_set_curswant;
6606 pos_T old_op_start, old_op_end;
6607 oparg_T oa;
6608 cmdarg_T ca;
6609
6610 if (cbd->owned)
6611 {
6612 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6613 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6614 return;
6615
6616 /* Get the text between clip_star.start & clip_star.end */
6617 old_y_previous = y_previous;
6618 old_y_current = y_current;
6619 old_cursor = curwin->w_cursor;
6620 old_curswant = curwin->w_curswant;
6621 old_set_curswant = curwin->w_set_curswant;
6622 old_op_start = curbuf->b_op_start;
6623 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 old_visual = VIsual;
6625 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 clear_oparg(&oa);
6627 oa.regname = (cbd == &clip_plus ? '+' : '*');
6628 oa.op_type = OP_YANK;
6629 vim_memset(&ca, 0, sizeof(ca));
6630 ca.oap = &oa;
6631 ca.cmdchar = 'y';
6632 ca.count1 = 1;
6633 ca.retval = CA_NO_ADJ_OP_END;
6634 do_pending_operator(&ca, 0, TRUE);
6635 y_previous = old_y_previous;
6636 y_current = old_y_current;
6637 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006638 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 curwin->w_curswant = old_curswant;
6640 curwin->w_set_curswant = old_set_curswant;
6641 curbuf->b_op_start = old_op_start;
6642 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 VIsual = old_visual;
6644 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006646 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
6648 clip_free_selection(cbd);
6649
6650 /* Try to get selected text from another window */
6651 clip_gen_request_selection(cbd);
6652 }
6653}
6654
Bram Moolenaard44347f2011-06-19 01:14:29 +02006655/*
6656 * Convert from the GUI selection string into the '*'/'+' register.
6657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006659clip_yank_selection(
6660 int type,
6661 char_u *str,
6662 long len,
6663 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006665 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667 if (cbd == &clip_plus)
6668 y_ptr = &y_regs[PLUS_REGISTER];
6669 else
6670 y_ptr = &y_regs[STAR_REGISTER];
6671
6672 clip_free_selection(cbd);
6673
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006674 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675}
6676
6677/*
6678 * Convert the '*'/'+' register into a GUI selection string returned in *str
6679 * with length *len.
6680 * Returns the motion type, or -1 for failure.
6681 */
6682 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006683clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 char_u *p;
6686 int lnum;
6687 int i, j;
6688 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006689 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (cbd == &clip_plus)
6692 y_ptr = &y_regs[PLUS_REGISTER];
6693 else
6694 y_ptr = &y_regs[STAR_REGISTER];
6695
6696#ifdef USE_CRNL
6697 eolsize = 2;
6698#else
6699 eolsize = 1;
6700#endif
6701
6702 *str = NULL;
6703 *len = 0;
6704 if (y_ptr->y_array == NULL)
6705 return -1;
6706
6707 for (i = 0; i < y_ptr->y_size; i++)
6708 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6709
6710 /*
6711 * Don't want newline character at end of last line if we're in MCHAR mode.
6712 */
6713 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6714 *len -= eolsize;
6715
6716 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6717 if (p == NULL)
6718 return -1;
6719 lnum = 0;
6720 for (i = 0, j = 0; i < (int)*len; i++, j++)
6721 {
6722 if (y_ptr->y_array[lnum][j] == '\n')
6723 p[i] = NUL;
6724 else if (y_ptr->y_array[lnum][j] == NUL)
6725 {
6726#ifdef USE_CRNL
6727 p[i++] = '\r';
6728#endif
6729#ifdef USE_CR
6730 p[i] = '\r';
6731#else
6732 p[i] = '\n';
6733#endif
6734 lnum++;
6735 j = -1;
6736 }
6737 else
6738 p[i] = y_ptr->y_array[lnum][j];
6739 }
6740 return y_ptr->y_type;
6741}
6742
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744/*
6745 * If we have written to a clipboard register, send the text to the clipboard.
6746 */
6747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6751 {
6752 clip_own_selection(&clip_star);
6753 clip_gen_set_selection(&clip_star);
6754 }
6755 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6756 {
6757 clip_own_selection(&clip_plus);
6758 clip_gen_set_selection(&clip_plus);
6759 }
6760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
6762#endif /* FEAT_CLIPBOARD || PROTO */
6763
6764
6765#if defined(FEAT_DND) || defined(PROTO)
6766/*
6767 * Replace the contents of the '~' register with str.
6768 */
6769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006770dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006772 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 curr = y_current;
6775 y_current = &y_regs[TILDE_REGISTER];
6776 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006777 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 y_current = curr;
6779}
6780#endif
6781
6782
6783#if defined(FEAT_EVAL) || defined(PROTO)
6784/*
6785 * Return the type of a register.
6786 * Used for getregtype()
6787 * Returns MAUTO for error.
6788 */
6789 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006790get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 switch (regname)
6793 {
6794 case '%': /* file name */
6795 case '#': /* alternate file name */
6796 case '=': /* expression */
6797 case ':': /* last command line */
6798 case '/': /* last search-pattern */
6799 case '.': /* last inserted text */
6800#ifdef FEAT_SEARCHPATH
6801 case Ctrl_F: /* Filename under cursor */
6802 case Ctrl_P: /* Path under cursor, expand via "path" */
6803#endif
6804 case Ctrl_W: /* word under cursor */
6805 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6806 case '_': /* black hole: always empty */
6807 return MCHAR;
6808 }
6809
6810#ifdef FEAT_CLIPBOARD
6811 regname = may_get_selection(regname);
6812#endif
6813
Bram Moolenaar32b92012014-01-14 12:33:36 +01006814 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006815 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 get_yank_register(regname, FALSE);
6818
6819 if (y_current->y_array != NULL)
6820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (reglen != NULL && y_current->y_type == MBLOCK)
6822 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 return y_current->y_type;
6824 }
6825 return MAUTO;
6826}
6827
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006828/*
6829 * When "flags" has GREG_LIST return a list with text "s".
6830 * Otherwise just return "s".
6831 */
6832 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006834{
6835 if (flags & GREG_LIST)
6836 {
6837 list_T *list = list_alloc();
6838
6839 if (list != NULL)
6840 {
6841 if (list_append_string(list, NULL, -1) == FAIL)
6842 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006843 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006844 return NULL;
6845 }
6846 list->lv_first->li_tv.vval.v_string = s;
6847 }
6848 return (char_u *)list;
6849 }
6850 return s;
6851}
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853/*
6854 * Return the contents of a register as a single allocated string.
6855 * Used for "@r" in expressions and for getreg().
6856 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006857 * Flags:
6858 * GREG_NO_EXPR Do not allow expression register
6859 * GREG_EXPR_SRC For the expression register: return expression itself,
6860 * not the result of its evaluation.
6861 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 */
6863 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 long i;
6867 char_u *retval;
6868 int allocated;
6869 long len;
6870
6871 /* Don't allow using an expression register inside an expression */
6872 if (regname == '=')
6873 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006874 if (flags & GREG_NO_EXPR)
6875 return NULL;
6876 if (flags & GREG_EXPR_SRC)
6877 return getreg_wrap_one_line(get_expr_line_src(), flags);
6878 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880
6881 if (regname == '@') /* "@@" is used for unnamed register */
6882 regname = '"';
6883
6884 /* check for valid regname */
6885 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6886 return NULL;
6887
6888#ifdef FEAT_CLIPBOARD
6889 regname = may_get_selection(regname);
6890#endif
6891
6892 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6893 {
6894 if (retval == NULL)
6895 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006896 if (allocated)
6897 return getreg_wrap_one_line(retval, flags);
6898 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 get_yank_register(regname, FALSE);
6902 if (y_current->y_array == NULL)
6903 return NULL;
6904
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006905 if (flags & GREG_LIST)
6906 {
6907 list_T *list = list_alloc();
6908 int error = FALSE;
6909
6910 if (list == NULL)
6911 return NULL;
6912 for (i = 0; i < y_current->y_size; ++i)
6913 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6914 error = TRUE;
6915 if (error)
6916 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006917 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006918 return NULL;
6919 }
6920 return (char_u *)list;
6921 }
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /*
6924 * Compute length of resulting string.
6925 */
6926 len = 0;
6927 for (i = 0; i < y_current->y_size; ++i)
6928 {
6929 len += (long)STRLEN(y_current->y_array[i]);
6930 /*
6931 * Insert a newline between lines and after last line if
6932 * y_type is MLINE.
6933 */
6934 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6935 ++len;
6936 }
6937
6938 retval = lalloc(len + 1, TRUE);
6939
6940 /*
6941 * Copy the lines of the yank register into the string.
6942 */
6943 if (retval != NULL)
6944 {
6945 len = 0;
6946 for (i = 0; i < y_current->y_size; ++i)
6947 {
6948 STRCPY(retval + len, y_current->y_array[i]);
6949 len += (long)STRLEN(retval + len);
6950
6951 /*
6952 * Insert a NL between lines and after the last line if y_type is
6953 * MLINE.
6954 */
6955 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6956 retval[len++] = '\n';
6957 }
6958 retval[len] = NUL;
6959 }
6960
6961 return retval;
6962}
6963
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006964 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006965init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006966 int name,
6967 yankreg_T **old_y_previous,
6968 yankreg_T **old_y_current,
6969 int must_append,
6970 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006971{
6972 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6973 {
6974 emsg_invreg(name);
6975 return FAIL;
6976 }
6977
6978 /* Don't want to change the current (unnamed) register */
6979 *old_y_previous = y_previous;
6980 *old_y_current = y_current;
6981
6982 get_yank_register(name, TRUE);
6983 if (!y_append && !must_append)
6984 free_yank_all();
6985 return OK;
6986}
6987
6988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006989finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006990 int name,
6991 yankreg_T *old_y_previous,
6992 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006993{
6994# ifdef FEAT_CLIPBOARD
6995 /* Send text of clipboard register to the clipboard. */
6996 may_set_selection();
6997# endif
6998
6999 /* ':let @" = "val"' should change the meaning of the "" register */
7000 if (name != '"')
7001 y_previous = old_y_previous;
7002 y_current = old_y_current;
7003}
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005/*
7006 * Store string "str" in register "name".
7007 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7008 * If "must_append" is TRUE, always append to the register. Otherwise append
7009 * if "name" is an uppercase letter.
7010 * Note: "maxlen" and "must_append" don't work for the "/" register.
7011 * Careful: 'str' is modified, you may have to use a copy!
7012 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7013 */
7014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007015write_reg_contents(
7016 int name,
7017 char_u *str,
7018 int maxlen,
7019 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7022}
7023
7024 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007025write_reg_contents_lst(
7026 int name,
7027 char_u **strings,
7028 int maxlen UNUSED,
7029 int must_append,
7030 int yank_type,
7031 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007032{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007033 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007034
7035 if (name == '/'
7036#ifdef FEAT_EVAL
7037 || name == '='
7038#endif
7039 )
7040 {
7041 char_u *s;
7042
7043 if (strings[0] == NULL)
7044 s = (char_u *)"";
7045 else if (strings[1] != NULL)
7046 {
7047 EMSG(_("E883: search pattern and expression register may not "
7048 "contain two or more lines"));
7049 return;
7050 }
7051 else
7052 s = strings[0];
7053 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7054 return;
7055 }
7056
7057 if (name == '_') /* black hole: nothing to do */
7058 return;
7059
7060 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7061 &yank_type) == FAIL)
7062 return;
7063
7064 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7065
7066 finish_write_reg(name, old_y_previous, old_y_current);
7067}
7068
7069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007070write_reg_contents_ex(
7071 int name,
7072 char_u *str,
7073 int maxlen,
7074 int must_append,
7075 int yank_type,
7076 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007078 yankreg_T *old_y_previous, *old_y_current;
7079 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
Bram Moolenaare7566042005-06-17 22:00:15 +00007081 if (maxlen >= 0)
7082 len = maxlen;
7083 else
7084 len = (long)STRLEN(str);
7085
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 /* Special case: '/' search pattern */
7087 if (name == '/')
7088 {
7089 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7090 return;
7091 }
7092
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007093 if (name == '#')
7094 {
7095 buf_T *buf;
7096
7097 if (VIM_ISDIGIT(*str))
7098 {
7099 int num = atoi((char *)str);
7100
7101 buf = buflist_findnr(num);
7102 if (buf == NULL)
7103 EMSGN(_(e_nobufnr), (long)num);
7104 }
7105 else
7106 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7107 TRUE, FALSE, FALSE));
7108 if (buf == NULL)
7109 return;
7110 curwin->w_alt_fnum = buf->b_fnum;
7111 return;
7112 }
7113
Bram Moolenaare7566042005-06-17 22:00:15 +00007114#ifdef FEAT_EVAL
7115 if (name == '=')
7116 {
7117 char_u *p, *s;
7118
7119 p = vim_strnsave(str, (int)len);
7120 if (p == NULL)
7121 return;
7122 if (must_append)
7123 {
7124 s = concat_str(get_expr_line_src(), p);
7125 vim_free(p);
7126 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007127 }
7128 set_expr_line(p);
7129 return;
7130 }
7131#endif
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (name == '_') /* black hole: nothing to do */
7134 return;
7135
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007136 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7137 &yank_type) == FAIL)
7138 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007140 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007142 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143}
7144#endif /* FEAT_EVAL */
7145
7146#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7147/*
7148 * Put a string into a register. When the register is not empty, the string
7149 * is appended.
7150 */
7151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007152str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007153 yankreg_T *y_ptr, /* pointer to yank register */
7154 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7155 char_u *str, /* string to put in register */
7156 long len, /* length of string */
7157 long blocklen, /* width of Visual block */
7158 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007160 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 int lnum;
7162 long start;
7163 long i;
7164 int extra;
7165 int newlines; /* number of lines added */
7166 int extraline = 0; /* extra line at the end */
7167 int append = FALSE; /* append to last line in register */
7168 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007169 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007173 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 y_ptr->y_size = 0;
7175
Bram Moolenaard44347f2011-06-19 01:14:29 +02007176 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007177 type = ((str_list || (len > 0 && (str[len - 1] == NL
7178 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007179 ? MLINE : MCHAR);
7180 else
7181 type = yank_type;
7182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 /*
7184 * Count the number of lines within the string
7185 */
7186 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007187 if (str_list)
7188 {
7189 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007192 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007194 for (i = 0; i < len; i++)
7195 if (str[i] == '\n')
7196 ++newlines;
7197 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7198 {
7199 extraline = 1;
7200 ++newlines; /* count extra newline at the end */
7201 }
7202 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7203 {
7204 append = TRUE;
7205 --newlines; /* uncount newline when appending first line */
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007209 /* Without any lines make the register empty. */
7210 if (y_ptr->y_size + newlines == 0)
7211 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007212 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007213 return;
7214 }
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 /*
7217 * Allocate an array to hold the pointers to the new register lines.
7218 * If the register was not empty, move the existing lines to the new array.
7219 */
7220 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7221 * sizeof(char_u *), TRUE);
7222 if (pp == NULL) /* out of memory */
7223 return;
7224 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7225 pp[lnum] = y_ptr->y_array[lnum];
7226 vim_free(y_ptr->y_array);
7227 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 /*
7231 * Find the end of each line and save it into the array.
7232 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007233 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007235 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7236 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007237 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007238 pp[lnum] = vim_strnsave(*ss, i);
7239 if (i > maxlen)
7240 maxlen = i;
7241 }
7242 }
7243 else
7244 {
7245 for (start = 0; start < len + extraline; start += i + 1)
7246 {
7247 for (i = start; i < len; ++i) /* find the end of the line */
7248 if (str[i] == '\n')
7249 break;
7250 i -= start; /* i is now length of line */
7251 if (i > maxlen)
7252 maxlen = i;
7253 if (append)
7254 {
7255 --lnum;
7256 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7257 }
7258 else
7259 extra = 0;
7260 s = alloc((unsigned)(i + extra + 1));
7261 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007263 if (extra)
7264 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7265 if (append)
7266 vim_free(y_ptr->y_array[lnum]);
7267 if (i)
7268 mch_memmove(s + extra, str + start, (size_t)i);
7269 extra += i;
7270 s[extra] = NUL;
7271 y_ptr->y_array[lnum++] = s;
7272 while (--extra >= 0)
7273 {
7274 if (*s == NUL)
7275 *s = '\n'; /* replace NUL with newline */
7276 ++s;
7277 }
7278 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 y_ptr->y_type = type;
7282 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 if (type == MBLOCK)
7284 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7285 else
7286 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007287#ifdef FEAT_VIMINFO
7288 y_ptr->y_time_set = vim_time();
7289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
7291#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7292
7293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007294clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 vim_memset(oap, 0, sizeof(oparg_T));
7297}
7298
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007300 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 *
7302 * "Words" are counted by looking for boundaries between non-space and
7303 * space characters. (it seems to produce results that match 'wc'.)
7304 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007305 * Return value is byte count; word count for the line is added to "*wc".
7306 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 *
7308 * The function will only examine the first "limit" characters in the
7309 * line, stopping if it encounters an end-of-line (NUL byte). In that
7310 * case, eol_size will be added to the character count to account for
7311 * the size of the EOL character.
7312 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007313 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007314line_count_info(
7315 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007316 varnumber_T *wc,
7317 varnumber_T *cc,
7318 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007319 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007321 varnumber_T i;
7322 varnumber_T words = 0;
7323 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int is_word = 0;
7325
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007326 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 if (is_word)
7329 {
7330 if (vim_isspace(line[i]))
7331 {
7332 words++;
7333 is_word = 0;
7334 }
7335 }
7336 else if (!vim_isspace(line[i]))
7337 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007338 ++chars;
7339#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007340 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007341#else
7342 ++i;
7343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345
7346 if (is_word)
7347 words++;
7348 *wc += words;
7349
7350 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007351 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007354 chars += eol_size;
7355 }
7356 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 return i;
7358}
7359
7360/*
7361 * Give some info about the position of the cursor (for "g CTRL-G").
7362 * In Visual mode, give some info about the selected region. (In this case,
7363 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007364 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 */
7366 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007367cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007370 char_u buf1[50];
7371 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007373 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007374#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007375 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007376#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007377 varnumber_T byte_count_cursor = 0;
7378 varnumber_T char_count = 0;
7379 varnumber_T char_count_cursor = 0;
7380 varnumber_T word_count = 0;
7381 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007382 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007383 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 long line_count_selected = 0;
7385 pos_T min_pos, max_pos;
7386 oparg_T oparg;
7387 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
7389 /*
7390 * Compute the length of the file in characters.
7391 */
7392 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7393 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007394 if (dict == NULL)
7395 {
7396 MSG(_(no_lines_msg));
7397 return;
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400 else
7401 {
7402 if (get_fileformat(curbuf) == EOL_DOS)
7403 eol_size = 2;
7404 else
7405 eol_size = 1;
7406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (VIsual_active)
7408 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007409 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
7411 min_pos = VIsual;
7412 max_pos = curwin->w_cursor;
7413 }
7414 else
7415 {
7416 min_pos = curwin->w_cursor;
7417 max_pos = VIsual;
7418 }
7419 if (*p_sel == 'e' && max_pos.col > 0)
7420 --max_pos.col;
7421
7422 if (VIsual_mode == Ctrl_V)
7423 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007424#ifdef FEAT_LINEBREAK
7425 char_u * saved_sbr = p_sbr;
7426
7427 /* Make 'sbr' empty for a moment to get the correct size. */
7428 p_sbr = empty_option;
7429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 oparg.is_VIsual = 1;
7431 oparg.block_mode = TRUE;
7432 oparg.op_type = OP_NOP;
7433 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007434 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007435#ifdef FEAT_LINEBREAK
7436 p_sbr = saved_sbr;
7437#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007438 if (curwin->w_curswant == MAXCOL)
7439 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 /* Swap the start, end vcol if needed */
7441 if (oparg.end_vcol < oparg.start_vcol)
7442 {
7443 oparg.end_vcol += oparg.start_vcol;
7444 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7445 oparg.end_vcol -= oparg.start_vcol;
7446 }
7447 }
7448 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7452 {
7453 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007454 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 {
7456 ui_breakcheck();
7457 if (got_int)
7458 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007459 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 /* Do extra processing for VIsual mode. */
7463 if (VIsual_active
7464 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7465 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007466 char_u *s = NULL;
7467 long len = 0L;
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 switch (VIsual_mode)
7470 {
7471 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007472#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007476#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007478#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007479 s = bd.textstart;
7480 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 break;
7482 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007483 s = ml_get(lnum);
7484 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 break;
7486 case 'v':
7487 {
7488 colnr_T start_col = (lnum == min_pos.lnum)
7489 ? min_pos.col : 0;
7490 colnr_T end_col = (lnum == max_pos.lnum)
7491 ? max_pos.col - start_col + 1 : MAXCOL;
7492
Bram Moolenaardef9e822004-12-31 20:58:58 +00007493 s = ml_get(lnum) + start_col;
7494 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
7496 break;
7497 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007498 if (s != NULL)
7499 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007500 byte_count_cursor += line_count_info(s, &word_count_cursor,
7501 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007502 if (lnum == curbuf->b_ml.ml_line_count
7503 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007504 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007505 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007506 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 /* In non-visual mode, check for the line the cursor is on */
7512 if (lnum == curwin->w_cursor.lnum)
7513 {
7514 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007515 char_count_cursor += char_count;
7516 byte_count_cursor = byte_count +
7517 line_count_info(ml_get(lnum),
7518 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007519 (varnumber_T)(curwin->w_cursor.col + 1),
7520 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 }
7523 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007524 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007525 &char_count, (varnumber_T)MAXCOL,
7526 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 }
7528
7529 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007530 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007531 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
Bram Moolenaared767a22016-01-03 22:49:16 +01007533 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007535 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007537 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7538 {
7539 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7540 &max_pos.col);
7541 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7542 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7543 }
7544 else
7545 buf1[0] = NUL;
7546
7547 if (char_count_cursor == byte_count_cursor
7548 && char_count == byte_count)
7549 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007550 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007551 buf1, line_count_selected,
7552 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007553 (long long)word_count_cursor,
7554 (long long)word_count,
7555 (long long)byte_count_cursor,
7556 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007557 else
7558 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007559 _("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 +01007560 buf1, line_count_selected,
7561 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007562 (long long)word_count_cursor,
7563 (long long)word_count,
7564 (long long)char_count_cursor,
7565 (long long)char_count,
7566 (long long)byte_count_cursor,
7567 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007570 {
7571 p = ml_get_curline();
7572 validate_virtcol();
7573 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7574 (int)curwin->w_virtcol + 1);
7575 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7576 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577
Bram Moolenaared767a22016-01-03 22:49:16 +01007578 if (char_count_cursor == byte_count_cursor
7579 && char_count == byte_count)
7580 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007581 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007582 (char *)buf1, (char *)buf2,
7583 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007585 (long long)word_count_cursor, (long long)word_count,
7586 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007587 else
7588 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007589 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007590 (char *)buf1, (char *)buf2,
7591 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007592 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007593 (long long)word_count_cursor, (long long)word_count,
7594 (long long)char_count_cursor, (long long)char_count,
7595 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007596 }
7597 }
7598
Bram Moolenaared767a22016-01-03 22:49:16 +01007599#ifdef FEAT_MBYTE
7600 bom_count = bomb_size();
7601 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007602 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007603 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007604#endif
7605 if (dict == NULL)
7606 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007607 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007608 p = p_shm;
7609 p_shm = (char_u *)"";
7610 msg(IObuff);
7611 p_shm = p;
7612 }
7613 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007614#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007615 if (dict != NULL)
7616 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007617 dict_add_number(dict, "words", word_count);
7618 dict_add_number(dict, "chars", char_count);
7619 dict_add_number(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007620# ifdef FEAT_MBYTE
7621 + bom_count
7622# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02007623 );
7624 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7625 byte_count_cursor);
7626 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7627 char_count_cursor);
7628 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7629 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632}