blob: 8a106ebca2824677670b1830d383051ef5ed2ad2 [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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000716 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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100727 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100773 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200774 "%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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001222 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001280 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001538 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 *argp = last_cmdline;
1540 return TRUE;
1541
1542 case '/': /* last search-pattern */
1543 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 *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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001552 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001764 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001887 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 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 Moolenaarf9e3e092019-01-13 23:38:42 +01002509 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002510 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,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002757#ifdef FEAT_VIRTUALEDIT
2758 curbuf->b_op_start_orig.coladd
2759#else
2760 0
2761#endif
2762 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002763 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002764 pre_textlen -= t - oap->start_vcol;
2765 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002766 }
2767 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002768 && oap->end.col
2769#ifdef FEAT_VIRTUALEDIT
2770 + oap->end.coladd
2771#endif
2772 >= curbuf->b_op_start_orig.col
2773#ifdef FEAT_VIRTUALEDIT
2774 + curbuf->b_op_start_orig.coladd
2775#endif
2776 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002777 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002778 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002779#ifdef FEAT_VIRTUALEDIT
2780 curbuf->b_op_start_orig.coladd
2781#else
2782 0
2783#endif
2784 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002785 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002786 /* reset pre_textlen to the value of OP_INSERT */
2787 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002788 pre_textlen -= t - oap->start_vcol;
2789 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002790 oap->op_type = OP_INSERT;
2791 }
2792 }
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 /*
2795 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002796 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 * Don't do this when "$" used, end-of-line will have changed.
2798 */
2799 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2800 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2801 {
2802 if (oap->op_type == OP_APPEND)
2803 {
2804 pre_textlen += bd2.textlen - bd.textlen;
2805 if (bd2.endspaces)
2806 --bd2.textlen;
2807 }
2808 bd.textcol = bd2.textcol;
2809 bd.textlen = bd2.textlen;
2810 }
2811
2812 /*
2813 * Subsequent calls to ml_get() flush the firstline data - take a
2814 * copy of the required string.
2815 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002816 firstline = ml_get(oap->start.lnum);
2817 len = STRLEN(firstline);
2818 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002820 add += bd.textlen;
2821 if ((size_t)add > len)
2822 firstline += len; // short line, point to the NUL
2823 else
2824 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002825 if (pre_textlen >= 0
2826 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
2828 ins_text = vim_strnsave(firstline, (int)ins_len);
2829 if (ins_text != NULL)
2830 {
2831 /* block handled here */
2832 if (u_save(oap->start.lnum,
2833 (linenr_T)(oap->end.lnum + 1)) == OK)
2834 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2835 &bd);
2836
2837 curwin->w_cursor.col = oap->start.col;
2838 check_cursor();
2839 vim_free(ins_text);
2840 }
2841 }
2842 }
2843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845/*
2846 * op_change - handle a change operation
2847 *
2848 * return TRUE if edit() returns because of a CTRL-O command
2849 */
2850 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 colnr_T l;
2854 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 long offset;
2856 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002857 long ins_len;
2858 long pre_textlen = 0;
2859 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 char_u *firstline;
2861 char_u *ins_text, *newp, *oldp;
2862 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 l = oap->start.col;
2865 if (oap->motion_type == MLINE)
2866 {
2867 l = 0;
2868#ifdef FEAT_SMARTINDENT
2869 if (!p_paste && curbuf->b_p_si
2870# ifdef FEAT_CINDENT
2871 && !curbuf->b_p_cin
2872# endif
2873 )
2874 can_si = TRUE; /* It's like opening a new line, do si */
2875#endif
2876 }
2877
2878 /* First delete the text in the region. In an empty buffer only need to
2879 * save for undo */
2880 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2881 {
2882 if (u_save_cursor() == FAIL)
2883 return FALSE;
2884 }
2885 else if (op_delete(oap) == FAIL)
2886 return FALSE;
2887
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002888 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 && !virtual_op)
2890 inc_cursor();
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 /* check for still on same line (<CR> in inserted text meaningless) */
2893 /* skip blank lines too */
2894 if (oap->block_mode)
2895 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002896#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /* Add spaces before getting the current line length. */
2898 if (virtual_op && (curwin->w_cursor.coladd > 0
2899 || gchar_cursor() == NUL))
2900 coladvance_force(getviscol());
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002901#endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002902 firstline = ml_get(oap->start.lnum);
2903 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002904 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 bd.textcol = curwin->w_cursor.col;
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
2908#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2909 if (oap->motion_type == MLINE)
2910 fix_indent();
2911#endif
2912
2913 retval = edit(NUL, FALSE, (linenr_T)1);
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002916 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002918 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002920 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002922 /* Auto-indenting may have changed the indent. If the cursor was past
2923 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002925 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002927 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002928
2929 pre_textlen += new_indent - pre_indent;
2930 bd.textcol += new_indent - pre_indent;
2931 }
2932
2933 ins_len = (long)STRLEN(firstline) - pre_textlen;
2934 if (ins_len > 0)
2935 {
2936 /* Subsequent calls to ml_get() flush the firstline data - take a
2937 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2939 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002940 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2942 linenr++)
2943 {
2944 block_prep(oap, &bd, linenr, TRUE);
2945 if (!bd.is_short || virtual_op)
2946 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002947#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 pos_T vpos;
2949
2950 /* If the block starts in virtual space, count the
2951 * initial coladd offset as part of "startspaces" */
2952 if (bd.is_short)
2953 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002954 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957 else
2958 vpos.coladd = 0;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 oldp = ml_get(linenr);
2961 newp = alloc_check((unsigned)(STRLEN(oldp)
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002962#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 + vpos.coladd
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 + ins_len + 1));
2966 if (newp == NULL)
2967 continue;
2968 /* copy up to block start */
2969 mch_memmove(newp, oldp, (size_t)bd.textcol);
2970 offset = bd.textcol;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002971#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002972 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 offset += vpos.coladd;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2976 offset += ins_len;
2977 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002978 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 ml_replace(linenr, newp, FALSE);
2980 }
2981 }
2982 check_cursor();
2983
2984 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2985 }
2986 vim_free(ins_text);
2987 }
2988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 return retval;
2991}
2992
2993/*
2994 * set all the yank registers to empty (called from main())
2995 */
2996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002997init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 int i;
3000
3001 for (i = 0; i < NUM_REGISTERS; ++i)
3002 y_regs[i].y_array = NULL;
3003}
3004
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003005#if defined(EXITFREE) || defined(PROTO)
3006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003007clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003008{
3009 int i;
3010
3011 for (i = 0; i < NUM_REGISTERS; ++i)
3012 {
3013 y_current = &y_regs[i];
3014 if (y_current->y_array != NULL)
3015 free_yank_all();
3016 }
3017}
3018#endif
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020/*
3021 * Free "n" lines from the current yank register.
3022 * Called for normal freeing and in case of error.
3023 */
3024 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003025free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if (y_current->y_array != NULL)
3028 {
3029 long i;
3030
3031 for (i = n; --i >= 0; )
3032 {
3033#ifdef AMIGA /* only for very slow machines */
3034 if ((i & 1023) == 1023) /* this may take a while */
3035 {
3036 /*
3037 * This message should never cause a hit-return message.
3038 * Overwrite this message with any next message.
3039 */
3040 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003041 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 --no_wait_return;
3043 msg_didout = FALSE;
3044 msg_col = 0;
3045 }
3046#endif
3047 vim_free(y_current->y_array[i]);
3048 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003049 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050#ifdef AMIGA
3051 if (n >= 1000)
3052 MSG("");
3053#endif
3054 }
3055}
3056
3057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003058free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 free_yank(y_current->y_size);
3061}
3062
3063/*
3064 * Yank the text between "oap->start" and "oap->end" into a yank register.
3065 * If we are to append (uppercase register), we first yank into a new yank
3066 * register and then concatenate the old and the new one (so we keep the old
3067 * one in case of out-of-memory).
3068 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003069 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 */
3071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003072op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003075 yankreg_T *curr; /* copy of y_current */
3076 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 char_u **new_ptr;
3078 linenr_T lnum; /* current line number */
3079 long j;
3080 int yanktype = oap->motion_type;
3081 long yanklines = oap->line_count;
3082 linenr_T yankendlnum = oap->end.lnum;
3083 char_u *p;
3084 char_u *pnew;
3085 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003086#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003087 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 /* check for read-only register */
3091 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3092 {
3093 beep_flush();
3094 return FAIL;
3095 }
3096 if (oap->regname == '_') /* black hole: nothing to do */
3097 return OK;
3098
3099#ifdef FEAT_CLIPBOARD
3100 if (!clip_star.available && oap->regname == '*')
3101 oap->regname = 0;
3102 else if (!clip_plus.available && oap->regname == '+')
3103 oap->regname = 0;
3104#endif
3105
3106 if (!deleting) /* op_delete() already set y_current */
3107 get_yank_register(oap->regname, TRUE);
3108
3109 curr = y_current;
3110 /* append to existing contents */
3111 if (y_append && y_current->y_array != NULL)
3112 y_current = &newreg;
3113 else
3114 free_yank_all(); /* free previously yanked lines */
3115
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003116 /*
3117 * If the cursor was in column 1 before and after the movement, and the
3118 * operator is not inclusive, the yank is always linewise.
3119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if ( oap->motion_type == MCHAR
3121 && oap->start.col == 0
3122 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003124 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 && oap->end.col == 0
3126 && yanklines > 1)
3127 {
3128 yanktype = MLINE;
3129 --yankendlnum;
3130 --yanklines;
3131 }
3132
3133 y_current->y_size = yanklines;
3134 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3137 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 if (y_current->y_array == NULL)
3139 {
3140 y_current = curr;
3141 return FAIL;
3142 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003143#ifdef FEAT_VIMINFO
3144 y_current->y_time_set = vim_time();
3145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 y_idx = 0;
3148 lnum = oap->start.lnum;
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (oap->block_mode)
3151 {
3152 /* Visual block mode */
3153 y_current->y_type = MBLOCK; /* set the yank register type */
3154 y_current->y_width = oap->end_vcol - oap->start_vcol;
3155
3156 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3157 y_current->y_width--;
3158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159
3160 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3161 {
3162 switch (y_current->y_type)
3163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 case MBLOCK:
3165 block_prep(oap, &bd, lnum, FALSE);
3166 if (yank_copy_line(&bd, y_idx) == FAIL)
3167 goto fail;
3168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 case MLINE:
3171 if ((y_current->y_array[y_idx] =
3172 vim_strsave(ml_get(lnum))) == NULL)
3173 goto fail;
3174 break;
3175
3176 case MCHAR:
3177 {
3178 colnr_T startcol = 0, endcol = MAXCOL;
3179#ifdef FEAT_VIRTUALEDIT
3180 int is_oneChar = FALSE;
3181 colnr_T cs, ce;
3182#endif
3183 p = ml_get(lnum);
3184 bd.startspaces = 0;
3185 bd.endspaces = 0;
3186
3187 if (lnum == oap->start.lnum)
3188 {
3189 startcol = oap->start.col;
3190#ifdef FEAT_VIRTUALEDIT
3191 if (virtual_op)
3192 {
3193 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3194 if (ce != cs && oap->start.coladd > 0)
3195 {
3196 /* Part of a tab selected -- but don't
3197 * double-count it. */
3198 bd.startspaces = (ce - cs + 1)
3199 - oap->start.coladd;
3200 startcol++;
3201 }
3202 }
3203#endif
3204 }
3205
3206 if (lnum == oap->end.lnum)
3207 {
3208 endcol = oap->end.col;
3209#ifdef FEAT_VIRTUALEDIT
3210 if (virtual_op)
3211 {
3212 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3213 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3214# ifdef FEAT_MBYTE
3215 /* Don't add space for double-wide
3216 * char; endcol will be on last byte
3217 * of multi-byte char. */
3218 && (*mb_head_off)(p, p + endcol) == 0
3219# endif
3220 ))
3221 {
3222 if (oap->start.lnum == oap->end.lnum
3223 && oap->start.col == oap->end.col)
3224 {
3225 /* Special case: inside a single char */
3226 is_oneChar = TRUE;
3227 bd.startspaces = oap->end.coladd
3228 - oap->start.coladd + oap->inclusive;
3229 endcol = startcol;
3230 }
3231 else
3232 {
3233 bd.endspaces = oap->end.coladd
3234 + oap->inclusive;
3235 endcol -= oap->inclusive;
3236 }
3237 }
3238 }
3239#endif
3240 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003241 if (endcol == MAXCOL)
3242 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (startcol > endcol
3244#ifdef FEAT_VIRTUALEDIT
3245 || is_oneChar
3246#endif
3247 )
3248 bd.textlen = 0;
3249 else
3250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 bd.textlen = endcol - startcol + oap->inclusive;
3252 }
3253 bd.textstart = p + startcol;
3254 if (yank_copy_line(&bd, y_idx) == FAIL)
3255 goto fail;
3256 break;
3257 }
3258 /* NOTREACHED */
3259 }
3260 }
3261
3262 if (curr != y_current) /* append the new block to the old block */
3263 {
3264 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3265 (curr->y_size + y_current->y_size)), TRUE);
3266 if (new_ptr == NULL)
3267 goto fail;
3268 for (j = 0; j < curr->y_size; ++j)
3269 new_ptr[j] = curr->y_array[j];
3270 vim_free(curr->y_array);
3271 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003272#ifdef FEAT_VIMINFO
3273 curr->y_time_set = vim_time();
3274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3277 curr->y_type = MLINE;
3278
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003279 /* Concatenate the last line of the old block with the first line of
3280 * the new block, unless being Vi compatible. */
3281 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
3283 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3284 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3285 if (pnew == NULL)
3286 {
3287 y_idx = y_current->y_size - 1;
3288 goto fail;
3289 }
3290 STRCPY(pnew, curr->y_array[--j]);
3291 STRCAT(pnew, y_current->y_array[0]);
3292 vim_free(curr->y_array[j]);
3293 vim_free(y_current->y_array[0]);
3294 curr->y_array[j++] = pnew;
3295 y_idx = 1;
3296 }
3297 else
3298 y_idx = 0;
3299 while (y_idx < y_current->y_size)
3300 curr->y_array[j++] = y_current->y_array[y_idx++];
3301 curr->y_size = j;
3302 vim_free(y_current->y_array);
3303 y_current = curr;
3304 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003305 if (curwin->w_p_rnu)
3306 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (mess) /* Display message about yank? */
3308 {
3309 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 && yanklines == 1)
3312 yanklines = 0;
3313 /* Some versions of Vi use ">=" here, some don't... */
3314 if (yanklines > p_report)
3315 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003316 char namebuf[100];
3317
3318 if (oap->regname == NUL)
3319 *namebuf = NUL;
3320 else
3321 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003322 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 /* redisplay now, so message is not deleted */
3325 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003326 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003327 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003328 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003329 "block of %ld lines yanked%s", yanklines),
3330 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003334 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003335 "%ld lines yanked%s", yanklines),
3336 yanklines, namebuf);
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 }
3340
3341 /*
3342 * Set "'[" and "']" marks.
3343 */
3344 curbuf->b_op_start = oap->start;
3345 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003346 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003347 {
3348 curbuf->b_op_start.col = 0;
3349 curbuf->b_op_end.col = MAXCOL;
3350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
3352#ifdef FEAT_CLIPBOARD
3353 /*
3354 * If we were yanking to the '*' register, send result to clipboard.
3355 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3356 * to the '*' register.
3357 */
3358 if (clip_star.available
3359 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003360 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003361 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
3363 if (curr != &(y_regs[STAR_REGISTER]))
3364 /* Copy the text from register 0 to the clipboard register. */
3365 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3366
3367 clip_own_selection(&clip_star);
3368 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003369# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003370 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
3373
3374# ifdef FEAT_X11
3375 /*
3376 * If we were yanking to the '+' register, send result to selection.
3377 * Also copy to the '*' register, in case auto-select is off.
3378 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003379 if (clip_plus.available
3380 && (curr == &(y_regs[PLUS_REGISTER])
3381 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003382 && ((clip_unnamed | clip_unnamed_saved) &
3383 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003385 if (curr != &(y_regs[PLUS_REGISTER]))
3386 /* Copy the text from register 0 to the clipboard register. */
3387 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 clip_own_selection(&clip_plus);
3390 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003391 if (!clip_isautosel_star() && !clip_isautosel_plus()
3392 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
3394 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3395 clip_own_selection(&clip_star);
3396 clip_gen_set_selection(&clip_star);
3397 }
3398 }
3399# endif
3400#endif
3401
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003402#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003403 if (!deleting && has_textyankpost())
3404 yank_do_autocmd(oap, y_current);
3405#endif
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return OK;
3408
3409fail: /* free the allocated lines */
3410 free_yank(y_idx + 1);
3411 y_current = curr;
3412 return FAIL;
3413}
3414
3415 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003416yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 char_u *pnew;
3419
3420 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3421 == NULL)
3422 return FAIL;
3423 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003424 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 pnew += bd->startspaces;
3426 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3427 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003428 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 pnew += bd->endspaces;
3430 *pnew = NUL;
3431 return OK;
3432}
3433
3434#ifdef FEAT_CLIPBOARD
3435/*
3436 * Make a copy of the y_current register to register "reg".
3437 */
3438 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003439copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003441 yankreg_T *curr = y_current;
3442 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 y_current = reg;
3445 free_yank_all();
3446 *y_current = *curr;
3447 y_current->y_array = (char_u **)lalloc_clear(
3448 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3449 if (y_current->y_array == NULL)
3450 y_current->y_size = 0;
3451 else
3452 for (j = 0; j < y_current->y_size; ++j)
3453 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3454 {
3455 free_yank(j);
3456 y_current->y_size = 0;
3457 break;
3458 }
3459 y_current = curr;
3460}
3461#endif
3462
3463/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003464 * Put contents of register "regname" into the text.
3465 * Caller must check "regname" to be valid!
3466 * "flags": PUT_FIXINDENT make indent look nice
3467 * PUT_CURSEND leave cursor after end of new text
3468 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
3470 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003471do_put(
3472 int regname,
3473 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3474 long count,
3475 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 char_u *ptr;
3478 char_u *newp, *oldp;
3479 int yanklen;
3480 int totlen = 0; /* init for gcc */
3481 linenr_T lnum;
3482 colnr_T col;
3483 long i; /* index in y_array[] */
3484 int y_type;
3485 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int oldlen;
3487 long y_width = 0;
3488 colnr_T vcol;
3489 int delcount;
3490 int incr = 0;
3491 long j;
3492 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 char_u **y_array = NULL;
3494 long nr_lines = 0;
3495 pos_T new_cursor;
3496 int indent;
3497 int orig_indent = 0; /* init for gcc */
3498 int indent_diff = 0; /* init for gcc */
3499 int first_indent = TRUE;
3500 int lendiff = 0;
3501 pos_T old_pos;
3502 char_u *insert_string = NULL;
3503 int allocated = FALSE;
3504 long cnt;
3505
3506#ifdef FEAT_CLIPBOARD
3507 /* Adjust register name for "unnamed" in 'clipboard'. */
3508 adjust_clip_reg(&regname);
3509 (void)may_get_selection(regname);
3510#endif
3511
3512 if (flags & PUT_FIXINDENT)
3513 orig_indent = get_indent();
3514
3515 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3516 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3517
3518 /*
3519 * Using inserted text works differently, because the register includes
3520 * special characters (newlines, etc.).
3521 */
3522 if (regname == '.')
3523 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003524 if (VIsual_active)
3525 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3527 (count == -1 ? 'O' : 'i')), count, FALSE);
3528 /* Putting the text is done later, so can't really move the cursor to
3529 * the next character. Use "l" to simulate it. */
3530 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3531 stuffcharReadbuff('l');
3532 return;
3533 }
3534
3535 /*
3536 * For special registers '%' (file name), '#' (alternate file name) and
3537 * ':' (last command line), etc. we have to create a fake yank register.
3538 */
3539 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3540 {
3541 if (insert_string == NULL)
3542 return;
3543 }
3544
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003545 /* Autocommands may be executed when saving lines for undo. This might
3546 * make "y_array" invalid, so we start undo now to avoid that. */
3547 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3548 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (insert_string != NULL)
3551 {
3552 y_type = MCHAR;
3553#ifdef FEAT_EVAL
3554 if (regname == '=')
3555 {
3556 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003557 * characters.
3558 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 for (;;)
3560 {
3561 y_size = 0;
3562 ptr = insert_string;
3563 while (ptr != NULL)
3564 {
3565 if (y_array != NULL)
3566 y_array[y_size] = ptr;
3567 ++y_size;
3568 ptr = vim_strchr(ptr, '\n');
3569 if (ptr != NULL)
3570 {
3571 if (y_array != NULL)
3572 *ptr = NUL;
3573 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003574 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (*ptr == NUL)
3576 {
3577 y_type = MLINE;
3578 break;
3579 }
3580 }
3581 }
3582 if (y_array != NULL)
3583 break;
3584 y_array = (char_u **)alloc((unsigned)
3585 (y_size * sizeof(char_u *)));
3586 if (y_array == NULL)
3587 goto end;
3588 }
3589 }
3590 else
3591#endif
3592 {
3593 y_size = 1; /* use fake one-line yank register */
3594 y_array = &insert_string;
3595 }
3596 }
3597 else
3598 {
3599 get_yank_register(regname, FALSE);
3600
3601 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 y_size = y_current->y_size;
3604 y_array = y_current->y_array;
3605 }
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (y_type == MLINE)
3608 {
3609 if (flags & PUT_LINE_SPLIT)
3610 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003611 char_u *p;
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 /* "p" or "P" in Visual mode: split the lines to put the text in
3614 * between. */
3615 if (u_save_cursor() == FAIL)
3616 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003617 p = ml_get_cursor();
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_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (ptr == NULL)
3622 goto end;
3623 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3624 vim_free(ptr);
3625
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003626 oldp = ml_get_curline();
3627 p = oldp + curwin->w_cursor.col;
3628 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003629 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003630 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (ptr == NULL)
3632 goto end;
3633 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3634 ++nr_lines;
3635 dir = FORWARD;
3636 }
3637 if (flags & PUT_LINE_FORWARD)
3638 {
3639 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003640 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 dir = FORWARD;
3642 }
3643 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3644 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3648 y_type = MLINE;
3649
3650 if (y_size == 0 || y_array == NULL)
3651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003652 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 regname == 0 ? (char_u *)"\"" : transchar(regname));
3654 goto end;
3655 }
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (y_type == MBLOCK)
3658 {
3659 lnum = curwin->w_cursor.lnum + y_size + 1;
3660 if (lnum > curbuf->b_ml.ml_line_count)
3661 lnum = curbuf->b_ml.ml_line_count + 1;
3662 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3663 goto end;
3664 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003665 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 lnum = curwin->w_cursor.lnum;
3668#ifdef FEAT_FOLDING
3669 /* Correct line number for closed fold. Don't move the cursor yet,
3670 * u_save() uses it. */
3671 if (dir == BACKWARD)
3672 (void)hasFolding(lnum, &lnum, NULL);
3673 else
3674 (void)hasFolding(lnum, NULL, &lnum);
3675#endif
3676 if (dir == FORWARD)
3677 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003678 /* In an empty buffer the empty line is going to be replaced, include
3679 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003680 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 goto end;
3682#ifdef FEAT_FOLDING
3683 if (dir == FORWARD)
3684 curwin->w_cursor.lnum = lnum - 1;
3685 else
3686 curwin->w_cursor.lnum = lnum;
3687 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3688#endif
3689 }
3690 else if (u_save_cursor() == FAIL)
3691 goto end;
3692
3693 yanklen = (int)STRLEN(y_array[0]);
3694
3695#ifdef FEAT_VIRTUALEDIT
3696 if (ve_flags == VE_ALL && y_type == MCHAR)
3697 {
3698 if (gchar_cursor() == TAB)
3699 {
3700 /* Don't need to insert spaces when "p" on the last position of a
3701 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003702#ifdef FEAT_VARTABS
3703 int viscol = getviscol();
3704 if (dir == FORWARD
3705 ? tabstop_padding(viscol, curbuf->b_p_ts,
3706 curbuf->b_p_vts_array) != 1
3707 : curwin->w_cursor.coladd > 0)
3708 coladvance_force(viscol);
3709#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (dir == FORWARD
3711 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3712 : curwin->w_cursor.coladd > 0)
3713 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 else
3716 curwin->w_cursor.coladd = 0;
3717 }
3718 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3719 coladvance_force(getviscol() + (dir == FORWARD));
3720 }
3721#endif
3722
3723 lnum = curwin->w_cursor.lnum;
3724 col = curwin->w_cursor.col;
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 /*
3727 * Block mode
3728 */
3729 if (y_type == MBLOCK)
3730 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003731 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 colnr_T endcol2 = 0;
3733
3734 if (dir == FORWARD && c != NUL)
3735 {
3736#ifdef FEAT_VIRTUALEDIT
3737 if (ve_flags == VE_ALL)
3738 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3739 else
3740#endif
3741 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3742
3743#ifdef FEAT_MBYTE
3744 if (has_mbyte)
3745 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003746 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 else
3748#endif
3749#ifdef FEAT_VIRTUALEDIT
3750 if (c != TAB || ve_flags != VE_ALL)
3751#endif
3752 ++curwin->w_cursor.col;
3753 ++col;
3754 }
3755 else
3756 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3757
3758#ifdef FEAT_VIRTUALEDIT
3759 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003760 if (ve_flags == VE_ALL
3761 && (curwin->w_cursor.coladd > 0
3762 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
3764 if (dir == FORWARD && c == NUL)
3765 ++col;
3766 if (dir != FORWARD && c != NUL)
3767 ++curwin->w_cursor.col;
3768 if (c == TAB)
3769 {
3770 if (dir == BACKWARD && curwin->w_cursor.col)
3771 curwin->w_cursor.col--;
3772 if (dir == FORWARD && col - 1 == endcol2)
3773 curwin->w_cursor.col++;
3774 }
3775 }
3776 curwin->w_cursor.coladd = 0;
3777#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003778 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 for (i = 0; i < y_size; ++i)
3780 {
3781 int spaces;
3782 char shortline;
3783
3784 bd.startspaces = 0;
3785 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 vcol = 0;
3787 delcount = 0;
3788
3789 /* add a new line */
3790 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3791 {
3792 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3793 (colnr_T)1, FALSE) == FAIL)
3794 break;
3795 ++nr_lines;
3796 }
3797 /* get the old line and advance to the position to insert at */
3798 oldp = ml_get_curline();
3799 oldlen = (int)STRLEN(oldp);
3800 for (ptr = oldp; vcol < col && *ptr; )
3801 {
3802 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003803 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 vcol += incr;
3805 }
3806 bd.textcol = (colnr_T)(ptr - oldp);
3807
3808 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3809
3810 if (vcol < col) /* line too short, padd with spaces */
3811 bd.startspaces = col - vcol;
3812 else if (vcol > col)
3813 {
3814 bd.endspaces = vcol - col;
3815 bd.startspaces = incr - bd.endspaces;
3816 --bd.textcol;
3817 delcount = 1;
3818#ifdef FEAT_MBYTE
3819 if (has_mbyte)
3820 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3821#endif
3822 if (oldp[bd.textcol] != TAB)
3823 {
3824 /* Only a Tab can be split into spaces. Other
3825 * characters will have to be moved to after the
3826 * block, causing misalignment. */
3827 delcount = 0;
3828 bd.endspaces = 0;
3829 }
3830 }
3831
3832 yanklen = (int)STRLEN(y_array[i]);
3833
3834 /* calculate number of spaces required to fill right side of block*/
3835 spaces = y_width + 1;
3836 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003837 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (spaces < 0)
3839 spaces = 0;
3840
3841 /* insert the new text */
3842 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3843 newp = alloc_check((unsigned)totlen + oldlen + 1);
3844 if (newp == NULL)
3845 break;
3846 /* copy part up to cursor to new line */
3847 ptr = newp;
3848 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3849 ptr += bd.textcol;
3850 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003851 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 ptr += bd.startspaces;
3853 /* insert the new text */
3854 for (j = 0; j < count; ++j)
3855 {
3856 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3857 ptr += yanklen;
3858
3859 /* insert block's trailing spaces only if there's text behind */
3860 if ((j < count - 1 || !shortline) && spaces)
3861 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003862 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 ptr += spaces;
3864 }
3865 }
3866 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003867 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 ptr += bd.endspaces;
3869 /* move the text after the cursor to the end of the line. */
3870 mch_memmove(ptr, oldp + bd.textcol + delcount,
3871 (size_t)(oldlen - bd.textcol - delcount + 1));
3872 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3873
3874 ++curwin->w_cursor.lnum;
3875 if (i == 0)
3876 curwin->w_cursor.col += bd.startspaces;
3877 }
3878
3879 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3880
3881 /* Set '[ mark. */
3882 curbuf->b_op_start = curwin->w_cursor;
3883 curbuf->b_op_start.lnum = lnum;
3884
3885 /* adjust '] mark */
3886 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3887 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003888# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (flags & PUT_CURSEND)
3892 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003893 colnr_T len;
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 curwin->w_cursor = curbuf->b_op_end;
3896 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003897
3898 /* in Insert mode we might be after the NUL, correct for that */
3899 len = (colnr_T)STRLEN(ml_get_curline());
3900 if (curwin->w_cursor.col > len)
3901 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 else
3904 curwin->w_cursor.lnum = lnum;
3905 }
3906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 /*
3909 * Character or Line mode
3910 */
3911 if (y_type == MCHAR)
3912 {
3913 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3914 * char */
3915 if (dir == FORWARD && gchar_cursor() != NUL)
3916 {
3917#ifdef FEAT_MBYTE
3918 if (has_mbyte)
3919 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003920 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 /* put it on the next of the multi-byte character. */
3923 col += bytelen;
3924 if (yanklen)
3925 {
3926 curwin->w_cursor.col += bytelen;
3927 curbuf->b_op_end.col += bytelen;
3928 }
3929 }
3930 else
3931#endif
3932 {
3933 ++col;
3934 if (yanklen)
3935 {
3936 ++curwin->w_cursor.col;
3937 ++curbuf->b_op_end.col;
3938 }
3939 }
3940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 curbuf->b_op_start = curwin->w_cursor;
3942 }
3943 /*
3944 * Line mode: BACKWARD is the same as FORWARD on the previous line
3945 */
3946 else if (dir == BACKWARD)
3947 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003948 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 /*
3951 * simple case: insert into current line
3952 */
3953 if (y_type == MCHAR && y_size == 1)
3954 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003955 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003956
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003957 if (VIsual_active)
3958 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003959 end_lnum = curbuf->b_visual.vi_end.lnum;
3960 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3961 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003962 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003963
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003964 do {
3965 totlen = count * yanklen;
3966 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003968 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003969 if (VIsual_active && col > (int)STRLEN(oldp))
3970 {
3971 lnum++;
3972 continue;
3973 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003974 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3975 if (newp == NULL)
3976 goto end; /* alloc() gave an error message */
3977 mch_memmove(newp, oldp, (size_t)col);
3978 ptr = newp + col;
3979 for (i = 0; i < count; ++i)
3980 {
3981 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3982 ptr += yanklen;
3983 }
3984 STRMOVE(ptr, oldp + col);
3985 ml_replace(lnum, newp, FALSE);
3986 /* Place cursor on last putted char. */
3987 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003988 {
3989 /* make sure curwin->w_virtcol is updated */
3990 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003991 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003994 if (VIsual_active)
3995 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003996 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003997
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003998 if (VIsual_active) /* reset lnum to the last visual line */
3999 lnum--;
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 curbuf->b_op_end = curwin->w_cursor;
4002 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4003 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4004 ++curwin->w_cursor.col;
4005 changed_bytes(lnum, col);
4006 }
4007 else
4008 {
4009 /*
4010 * Insert at least one line. When y_type is MCHAR, break the first
4011 * line in two.
4012 */
4013 for (cnt = 1; cnt <= count; ++cnt)
4014 {
4015 i = 0;
4016 if (y_type == MCHAR)
4017 {
4018 /*
4019 * Split the current line in two at the insert position.
4020 * First insert y_array[size - 1] in front of second line.
4021 * Then append y_array[0] to first line.
4022 */
4023 lnum = new_cursor.lnum;
4024 ptr = ml_get(lnum) + col;
4025 totlen = (int)STRLEN(y_array[y_size - 1]);
4026 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4027 if (newp == NULL)
4028 goto error;
4029 STRCPY(newp, y_array[y_size - 1]);
4030 STRCAT(newp, ptr);
4031 /* insert second line */
4032 ml_append(lnum, newp, (colnr_T)0, FALSE);
4033 vim_free(newp);
4034
4035 oldp = ml_get(lnum);
4036 newp = alloc_check((unsigned)(col + yanklen + 1));
4037 if (newp == NULL)
4038 goto error;
4039 /* copy first part of line */
4040 mch_memmove(newp, oldp, (size_t)col);
4041 /* append to first line */
4042 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4043 ml_replace(lnum, newp, FALSE);
4044
4045 curwin->w_cursor.lnum = lnum;
4046 i = 1;
4047 }
4048
4049 for (; i < y_size; ++i)
4050 {
4051 if ((y_type != MCHAR || i < y_size - 1)
4052 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4053 == FAIL)
4054 goto error;
4055 lnum++;
4056 ++nr_lines;
4057 if (flags & PUT_FIXINDENT)
4058 {
4059 old_pos = curwin->w_cursor;
4060 curwin->w_cursor.lnum = lnum;
4061 ptr = ml_get(lnum);
4062 if (cnt == count && i == y_size - 1)
4063 lendiff = (int)STRLEN(ptr);
4064#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4065 if (*ptr == '#' && preprocs_left())
4066 indent = 0; /* Leave # lines at start */
4067 else
4068#endif
4069 if (*ptr == NUL)
4070 indent = 0; /* Ignore empty lines */
4071 else if (first_indent)
4072 {
4073 indent_diff = orig_indent - get_indent();
4074 indent = orig_indent;
4075 first_indent = FALSE;
4076 }
4077 else if ((indent = get_indent() + indent_diff) < 0)
4078 indent = 0;
4079 (void)set_indent(indent, 0);
4080 curwin->w_cursor = old_pos;
4081 /* remember how many chars were removed */
4082 if (cnt == count && i == y_size - 1)
4083 lendiff -= (int)STRLEN(ml_get(lnum));
4084 }
4085 }
4086 }
4087
4088error:
4089 /* Adjust marks. */
4090 if (y_type == MLINE)
4091 {
4092 curbuf->b_op_start.col = 0;
4093 if (dir == FORWARD)
4094 curbuf->b_op_start.lnum++;
4095 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004096 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004097 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004098 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004099 < curbuf->b_ml.ml_line_count
4100#ifdef FEAT_DIFF
4101 || curwin->w_p_diff
4102#endif
4103 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004104 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 (linenr_T)MAXLNUM, nr_lines, 0L);
4106
4107 /* note changed text for displaying and folding */
4108 if (y_type == MCHAR)
4109 changed_lines(curwin->w_cursor.lnum, col,
4110 curwin->w_cursor.lnum + 1, nr_lines);
4111 else
4112 changed_lines(curbuf->b_op_start.lnum, 0,
4113 curbuf->b_op_start.lnum, nr_lines);
4114
4115 /* put '] mark at last inserted character */
4116 curbuf->b_op_end.lnum = lnum;
4117 /* correct length for change in indent */
4118 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4119 if (col > 1)
4120 curbuf->b_op_end.col = col - 1;
4121 else
4122 curbuf->b_op_end.col = 0;
4123
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004124 if (flags & PUT_CURSLINE)
4125 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004126 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004127 curwin->w_cursor.lnum = lnum;
4128 beginline(BL_WHITE | BL_FIX);
4129 }
4130 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132 /* put cursor after inserted text */
4133 if (y_type == MLINE)
4134 {
4135 if (lnum >= curbuf->b_ml.ml_line_count)
4136 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4137 else
4138 curwin->w_cursor.lnum = lnum + 1;
4139 curwin->w_cursor.col = 0;
4140 }
4141 else
4142 {
4143 curwin->w_cursor.lnum = lnum;
4144 curwin->w_cursor.col = col;
4145 }
4146 }
4147 else if (y_type == MLINE)
4148 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004149 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 curwin->w_cursor.col = 0;
4151 if (dir == FORWARD)
4152 ++curwin->w_cursor.lnum;
4153 beginline(BL_WHITE | BL_FIX);
4154 }
4155 else /* put cursor on first inserted character */
4156 curwin->w_cursor = new_cursor;
4157 }
4158 }
4159
4160 msgmore(nr_lines);
4161 curwin->w_set_curswant = TRUE;
4162
4163end:
4164 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004166 if (regname == '=')
4167 vim_free(y_array);
4168
Bram Moolenaar033d8882013-09-25 23:24:57 +02004169 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004170
Bram Moolenaar677ee682005-01-27 14:41:15 +00004171 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004172 adjust_cursor_eol();
4173}
4174
4175/*
4176 * When the cursor is on the NUL past the end of the line and it should not be
4177 * there move it left.
4178 */
4179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004180adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004181{
4182 if (curwin->w_cursor.col > 0
4183 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004184#ifdef FEAT_VIRTUALEDIT
4185 && (ve_flags & VE_ONEMORE) == 0
4186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 && !(restart_edit || (State & INSERT)))
4188 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004189 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004190 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192#ifdef FEAT_VIRTUALEDIT
4193 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004194 {
4195 colnr_T scol, ecol;
4196
4197 /* Coladd is set to the width of the last character. */
4198 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4199 curwin->w_cursor.coladd = ecol - scol + 1;
4200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#endif
4202 }
4203}
4204
4205#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4206/*
4207 * Return TRUE if lines starting with '#' should be left aligned.
4208 */
4209 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004210preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 return
4213# ifdef FEAT_SMARTINDENT
4214# ifdef FEAT_CINDENT
4215 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4216# else
4217 curbuf->b_p_si
4218# endif
4219# endif
4220# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004221 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4222 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223# endif
4224 ;
4225}
4226#endif
4227
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004228/*
4229 * Return the character name of the register with the given number.
4230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004232get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
4234 if (num == -1)
4235 return '"';
4236 else if (num < 10)
4237 return num + '0';
4238 else if (num == DELETION_REGISTER)
4239 return '-';
4240#ifdef FEAT_CLIPBOARD
4241 else if (num == STAR_REGISTER)
4242 return '*';
4243 else if (num == PLUS_REGISTER)
4244 return '+';
4245#endif
4246 else
4247 {
4248#ifdef EBCDIC
4249 int i;
4250
4251 /* EBCDIC is really braindead ... */
4252 i = 'a' + (num - 10);
4253 if (i > 'i')
4254 i += 7;
4255 if (i > 'r')
4256 i += 8;
4257 return i;
4258#else
4259 return num + 'a' - 10;
4260#endif
4261 }
4262}
4263
4264/*
4265 * ":dis" and ":registers": Display the contents of the yank registers.
4266 */
4267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004268ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004270 int i, n;
4271 long j;
4272 char_u *p;
4273 yankreg_T *yb;
4274 int name;
4275 int attr;
4276 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004277#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004278 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004279#else
4280# define clen 1
4281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 if (arg != NULL && *arg == NUL)
4284 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004285 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 /* Highlight title */
4288 MSG_PUTS_TITLE(_("\n--- Registers ---"));
4289 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4290 {
4291 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004292 if (arg != NULL && vim_strchr(arg, name) == NULL
4293#ifdef ONE_CLIPBOARD
4294 /* Star register and plus register contain the same thing. */
4295 && (name != '*' || vim_strchr(arg, '+') == NULL)
4296#endif
4297 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 continue; /* did not ask for this register */
4299
4300#ifdef FEAT_CLIPBOARD
4301 /* Adjust register name for "unnamed" in 'clipboard'.
4302 * When it's a clipboard register, fill it with the current contents
4303 * of the clipboard. */
4304 adjust_clip_reg(&name);
4305 (void)may_get_selection(name);
4306#endif
4307
4308 if (i == -1)
4309 {
4310 if (y_previous != NULL)
4311 yb = y_previous;
4312 else
4313 yb = &(y_regs[0]);
4314 }
4315 else
4316 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004317
4318#ifdef FEAT_EVAL
4319 if (name == MB_TOLOWER(redir_reg)
4320 || (redir_reg == '"' && yb == y_previous))
4321 continue; /* do not list register being written to, the
4322 * pointer can be freed */
4323#endif
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (yb->y_array != NULL)
4326 {
4327 msg_putchar('\n');
4328 msg_putchar('"');
4329 msg_putchar(name);
4330 MSG_PUTS(" ");
4331
4332 n = (int)Columns - 6;
4333 for (j = 0; j < yb->y_size && n > 1; ++j)
4334 {
4335 if (j)
4336 {
4337 MSG_PUTS_ATTR("^J", attr);
4338 n -= 2;
4339 }
4340 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004343 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004344#endif
4345 msg_outtrans_len(p, clen);
4346#ifdef FEAT_MBYTE
4347 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348#endif
4349 }
4350 }
4351 if (n > 1 && yb->y_type == MLINE)
4352 MSG_PUTS_ATTR("^J", attr);
4353 out_flush(); /* show one line at a time */
4354 }
4355 ui_breakcheck();
4356 }
4357
4358 /*
4359 * display last inserted text
4360 */
4361 if ((p = get_last_insert()) != NULL
4362 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4363 {
4364 MSG_PUTS("\n\". ");
4365 dis_msg(p, TRUE);
4366 }
4367
4368 /*
4369 * display last command line
4370 */
4371 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4372 && !got_int)
4373 {
4374 MSG_PUTS("\n\": ");
4375 dis_msg(last_cmdline, FALSE);
4376 }
4377
4378 /*
4379 * display current file name
4380 */
4381 if (curbuf->b_fname != NULL
4382 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4383 {
4384 MSG_PUTS("\n\"% ");
4385 dis_msg(curbuf->b_fname, FALSE);
4386 }
4387
4388 /*
4389 * display alternate file name
4390 */
4391 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4392 {
4393 char_u *fname;
4394 linenr_T dummy;
4395
4396 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4397 {
4398 MSG_PUTS("\n\"# ");
4399 dis_msg(fname, FALSE);
4400 }
4401 }
4402
4403 /*
4404 * display last search pattern
4405 */
4406 if (last_search_pat() != NULL
4407 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4408 {
4409 MSG_PUTS("\n\"/ ");
4410 dis_msg(last_search_pat(), FALSE);
4411 }
4412
4413#ifdef FEAT_EVAL
4414 /*
4415 * display last used expression
4416 */
4417 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4418 && !got_int)
4419 {
4420 MSG_PUTS("\n\"= ");
4421 dis_msg(expr_line, FALSE);
4422 }
4423#endif
4424}
4425
4426/*
4427 * display a string for do_dis()
4428 * truncate at end of screen line
4429 */
4430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431dis_msg(
4432 char_u *p,
4433 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
4435 int n;
4436#ifdef FEAT_MBYTE
4437 int l;
4438#endif
4439
4440 n = (int)Columns - 6;
4441 while (*p != NUL
4442 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4443 && (n -= ptr2cells(p)) >= 0)
4444 {
4445#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004446 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 msg_outtrans_len(p, l);
4449 p += l;
4450 }
4451 else
4452#endif
4453 msg_outtrans_len(p++, 1);
4454 }
4455 ui_breakcheck();
4456}
4457
Bram Moolenaar81340392012-06-06 16:12:59 +02004458#if defined(FEAT_COMMENTS) || defined(PROTO)
4459/*
4460 * If "process" is TRUE and the line begins with a comment leader (possibly
4461 * after some white space), return a pointer to the text after it. Put a boolean
4462 * value indicating whether the line ends with an unclosed comment in
4463 * "is_comment".
4464 * line - line to be processed,
4465 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004466 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004467 * include_space - whether to also skip space following the comment leader,
4468 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004469 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004470 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004471 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004472skip_comment(
4473 char_u *line,
4474 int process,
4475 int include_space,
4476 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004477{
4478 char_u *comment_flags = NULL;
4479 int lead_len;
4480 int leader_offset = get_last_leader_offset(line, &comment_flags);
4481
4482 *is_comment = FALSE;
4483 if (leader_offset != -1)
4484 {
4485 /* Let's check whether the line ends with an unclosed comment.
4486 * If the last comment leader has COM_END in flags, there's no comment.
4487 */
4488 while (*comment_flags)
4489 {
4490 if (*comment_flags == COM_END
4491 || *comment_flags == ':')
4492 break;
4493 ++comment_flags;
4494 }
4495 if (*comment_flags != COM_END)
4496 *is_comment = TRUE;
4497 }
4498
4499 if (process == FALSE)
4500 return line;
4501
4502 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4503
4504 if (lead_len == 0)
4505 return line;
4506
4507 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004508 * - COM_END,
4509 * - colon,
4510 * whichever comes first.
4511 */
4512 while (*comment_flags)
4513 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004514 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004515 || *comment_flags == ':')
4516 {
4517 break;
4518 }
4519 ++comment_flags;
4520 }
4521
4522 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004523 * starting with a closing part of a three-part comment. That's good,
4524 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004525 */
4526 if (*comment_flags == ':' || *comment_flags == NUL)
4527 line += lead_len;
4528
4529 return line;
4530}
4531#endif
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004534 * Join 'count' lines (minimal 2) at cursor position.
4535 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004536 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4537 * leaders should not be removed.
4538 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4539 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004541 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
4543 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004544do_join(
4545 long count,
4546 int insert_space,
4547 int save_undo,
4548 int use_formatoptions UNUSED,
4549 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004551 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004552 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004553 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004555 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004556 int endcurr1 = NUL;
4557 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004558 int currsize = 0; /* size of the current line */
4559 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004561 colnr_T col = 0;
4562 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004563#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004564 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004565 int remove_comments = (use_formatoptions == TRUE)
4566 && has_format_option(FO_REMOVE_COMS);
4567 int prev_was_comment;
4568#endif
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004571 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4572 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004575 /* Allocate an array to store the number of spaces inserted before each
4576 * line. We will use it to pre-compute the length of the new line and the
4577 * proper placement of each original line in the new one. */
4578 spaces = lalloc_clear((long_u)count, TRUE);
4579 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004581#if defined(FEAT_COMMENTS) || defined(PROTO)
4582 if (remove_comments)
4583 {
4584 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4585 if (comments == NULL)
4586 {
4587 vim_free(spaces);
4588 return FAIL;
4589 }
4590 }
4591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004594 * Don't move anything, just compute the final line length
4595 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004597 for (t = 0; t < count; ++t)
4598 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004599 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004600 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004601 {
4602 /* Set the '[ mark. */
4603 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4604 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4605 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004606#if defined(FEAT_COMMENTS) || defined(PROTO)
4607 if (remove_comments)
4608 {
4609 /* We don't want to remove the comment leader if the
4610 * previous line is not a comment. */
4611 if (t > 0 && prev_was_comment)
4612 {
4613
4614 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4615 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004616 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004617 curr = new_curr;
4618 }
4619 else
4620 curr = skip_comment(curr, FALSE, insert_space,
4621 &prev_was_comment);
4622 }
4623#endif
4624
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004625 if (insert_space && t > 0)
4626 {
4627 curr = skipwhite(curr);
4628 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4629#ifdef FEAT_MBYTE
4630 && (!has_format_option(FO_MBYTE_JOIN)
4631 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4632 && (!has_format_option(FO_MBYTE_JOIN2)
4633 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4634#endif
4635 )
4636 {
4637 /* don't add a space if the line is ending in a space */
4638 if (endcurr1 == ' ')
4639 endcurr1 = endcurr2;
4640 else
4641 ++spaces[t];
4642 /* extra space when 'joinspaces' set and line ends in '.' */
4643 if ( p_js
4644 && (endcurr1 == '.'
4645 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4646 && (endcurr1 == '?' || endcurr1 == '!'))))
4647 ++spaces[t];
4648 }
4649 }
4650 currsize = (int)STRLEN(curr);
4651 sumsize += currsize + spaces[t];
4652 endcurr1 = endcurr2 = NUL;
4653 if (insert_space && currsize > 0)
4654 {
4655#ifdef FEAT_MBYTE
4656 if (has_mbyte)
4657 {
4658 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004659 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004660 endcurr1 = (*mb_ptr2char)(cend);
4661 if (cend > curr)
4662 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004663 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004664 endcurr2 = (*mb_ptr2char)(cend);
4665 }
4666 }
4667 else
4668#endif
4669 {
4670 endcurr1 = *(curr + currsize - 1);
4671 if (currsize > 1)
4672 endcurr2 = *(curr + currsize - 2);
4673 }
4674 }
4675 line_breakcheck();
4676 if (got_int)
4677 {
4678 ret = FAIL;
4679 goto theend;
4680 }
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004683 /* store the column position before last line */
4684 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004686 /* allocate the space for the new line */
4687 newp = alloc_check((unsigned)(sumsize + 1));
4688 cend = newp + sumsize;
4689 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004691 /*
4692 * Move affected lines to the new long one.
4693 *
4694 * Move marks from each deleted line to the joined line, adjusting the
4695 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4696 * should not really be a problem.
4697 */
4698 for (t = count - 1; ; --t)
4699 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004700 int spaces_removed;
4701
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004702 cend -= currsize;
4703 mch_memmove(cend, curr, (size_t)currsize);
4704 if (spaces[t] > 0)
4705 {
4706 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004707 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004708 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004709
4710 // If deleting more spaces than adding, the cursor moves no more than
4711 // what is added if it is inside these spaces.
4712 spaces_removed = (curr - curr_start) - spaces[t];
4713
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004714 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004715 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004716 if (t == 0)
4717 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004718 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004719#if defined(FEAT_COMMENTS) || defined(PROTO)
4720 if (remove_comments)
4721 curr += comments[t - 1];
4722#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004723 if (insert_space && t > 1)
4724 curr = skipwhite(curr);
4725 currsize = (int)STRLEN(curr);
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4728
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004729 if (setmark)
4730 {
4731 /* Set the '] mark. */
4732 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4733 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4734 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /* Only report the change in the first line here, del_lines() will report
4737 * the deleted line. */
4738 changed_lines(curwin->w_cursor.lnum, currsize,
4739 curwin->w_cursor.lnum + 1, 0L);
4740
4741 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004742 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 * briefly, and then move it back. After del_lines() the cursor may
4744 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 */
4746 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004748 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 curwin->w_cursor.lnum = t;
4750
4751 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004752 * Set the cursor column:
4753 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004754 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004756 curwin->w_cursor.col =
4757 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760#ifdef FEAT_VIRTUALEDIT
4761 curwin->w_cursor.coladd = 0;
4762#endif
4763 curwin->w_set_curswant = TRUE;
4764
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004765theend:
4766 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004767#if defined(FEAT_COMMENTS) || defined(PROTO)
4768 if (remove_comments)
4769 vim_free(comments);
4770#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004771 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772}
4773
4774#ifdef FEAT_COMMENTS
4775/*
4776 * Return TRUE if the two comment leaders given are the same. "lnum" is
4777 * the first line. White-space is ignored. Note that the whole of
4778 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4779 */
4780 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004781same_leader(
4782 linenr_T lnum,
4783 int leader1_len,
4784 char_u *leader1_flags,
4785 int leader2_len,
4786 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
4788 int idx1 = 0, idx2 = 0;
4789 char_u *p;
4790 char_u *line1;
4791 char_u *line2;
4792
4793 if (leader1_len == 0)
4794 return (leader2_len == 0);
4795
4796 /*
4797 * If first leader has 'f' flag, the lines can be joined only if the
4798 * second line does not have a leader.
4799 * If first leader has 'e' flag, the lines can never be joined.
4800 * If fist leader has 's' flag, the lines can only be joined if there is
4801 * some text after it and the second line has the 'm' flag.
4802 */
4803 if (leader1_flags != NULL)
4804 {
4805 for (p = leader1_flags; *p && *p != ':'; ++p)
4806 {
4807 if (*p == COM_FIRST)
4808 return (leader2_len == 0);
4809 if (*p == COM_END)
4810 return FALSE;
4811 if (*p == COM_START)
4812 {
4813 if (*(ml_get(lnum) + leader1_len) == NUL)
4814 return FALSE;
4815 if (leader2_flags == NULL || leader2_len == 0)
4816 return FALSE;
4817 for (p = leader2_flags; *p && *p != ':'; ++p)
4818 if (*p == COM_MIDDLE)
4819 return TRUE;
4820 return FALSE;
4821 }
4822 }
4823 }
4824
4825 /*
4826 * Get current line and next line, compare the leaders.
4827 * The first line has to be saved, only one line can be locked at a time.
4828 */
4829 line1 = vim_strsave(ml_get(lnum));
4830 if (line1 != NULL)
4831 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004832 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 ;
4834 line2 = ml_get(lnum + 1);
4835 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4836 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004837 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 if (line1[idx1++] != line2[idx2])
4840 break;
4841 }
4842 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004843 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 ++idx1;
4845 }
4846 vim_free(line1);
4847 }
4848 return (idx2 == leader2_len && idx1 == leader1_len);
4849}
4850#endif
4851
4852/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004853 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 */
4855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004856op_format(
4857 oparg_T *oap,
4858 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 long old_line_count = curbuf->b_ml.ml_line_count;
4861
4862 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4863 * can put it back there. */
4864 curwin->w_cursor = oap->cursor_start;
4865
4866 if (u_save((linenr_T)(oap->start.lnum - 1),
4867 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4868 return;
4869 curwin->w_cursor = oap->start;
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (oap->is_VIsual)
4872 /* When there is no change: need to remove the Visual selection */
4873 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /* Set '[ mark at the start of the formatted area */
4876 curbuf->b_op_start = oap->start;
4877
4878 /* For "gw" remember the cursor position and put it back below (adjusted
4879 * for joined and split lines). */
4880 if (keep_cursor)
4881 saved_cursor = oap->cursor_start;
4882
Bram Moolenaar81a82092008-03-12 16:27:00 +00004883 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /*
4886 * Leave the cursor at the first non-blank of the last formatted line.
4887 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4888 * line, so "." will do the next lines.
4889 */
4890 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4891 ++curwin->w_cursor.lnum;
4892 beginline(BL_WHITE | BL_FIX);
4893 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4894 msgmore(old_line_count);
4895
4896 /* put '] mark on the end of the formatted area */
4897 curbuf->b_op_end = curwin->w_cursor;
4898
4899 if (keep_cursor)
4900 {
4901 curwin->w_cursor = saved_cursor;
4902 saved_cursor.lnum = 0;
4903 }
4904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (oap->is_VIsual)
4906 {
4907 win_T *wp;
4908
4909 FOR_ALL_WINDOWS(wp)
4910 {
4911 if (wp->w_old_cursor_lnum != 0)
4912 {
4913 /* When lines have been inserted or deleted, adjust the end of
4914 * the Visual area to be redrawn. */
4915 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4916 wp->w_old_cursor_lnum += old_line_count;
4917 else
4918 wp->w_old_visual_lnum += old_line_count;
4919 }
4920 }
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922}
4923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004924#if defined(FEAT_EVAL) || defined(PROTO)
4925/*
4926 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4927 */
4928 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004929op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004930{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004931 if (oap->is_VIsual)
4932 /* When there is no change: need to remove the Visual selection */
4933 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004934
Bram Moolenaar700303e2010-07-11 17:35:50 +02004935 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4936 /* As documented: when 'formatexpr' returns non-zero fall back to
4937 * internal formatting. */
4938 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004939}
4940
4941 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004942fex_format(
4943 linenr_T lnum,
4944 long count,
4945 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004946{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004947 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4948 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004949 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004950 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004951
4952 /*
4953 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004954 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004955 */
4956 set_vim_var_nr(VV_LNUM, lnum);
4957 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004958 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004959
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004960 /* Make a copy, the option could be changed while calling it. */
4961 fex = vim_strsave(curbuf->b_p_fex);
4962 if (fex == NULL)
4963 return 0;
4964
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004965 /*
4966 * Evaluate the function.
4967 */
4968 if (use_sandbox)
4969 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004970 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004971 if (use_sandbox)
4972 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004973
4974 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004975 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004976
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004977 return r;
4978}
4979#endif
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981/*
4982 * Format "line_count" lines, starting at the cursor position.
4983 * When "line_count" is negative, format until the end of the paragraph.
4984 * Lines after the cursor line are saved for undo, caller must have saved the
4985 * first line.
4986 */
4987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004988format_lines(
4989 linenr_T line_count,
4990 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
4992 int max_len;
4993 int is_not_par; /* current line not part of parag. */
4994 int next_is_not_par; /* next line not part of paragraph */
4995 int is_end_par; /* at end of paragraph */
4996 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4997 int next_is_start_par = FALSE;
4998#ifdef FEAT_COMMENTS
4999 int leader_len = 0; /* leader len of current line */
5000 int next_leader_len; /* leader len of next line */
5001 char_u *leader_flags = NULL; /* flags for leader of current line */
5002 char_u *next_leader_flags; /* flags for leader of next line */
5003 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005004 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#endif
5006 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005007 int second_indent = -1; /* indent for second line (comment
5008 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 int do_second_indent;
5010 int do_number_indent;
5011 int do_trail_white;
5012 int first_par_line = TRUE;
5013 int smd_save;
5014 long count;
5015 int need_set_indent = TRUE; /* set indent of next paragraph */
5016 int force_format = FALSE;
5017 int old_State = State;
5018
5019 /* length of a line to force formatting: 3 * 'tw' */
5020 max_len = comp_textwidth(TRUE) * 3;
5021
5022 /* check for 'q', '2' and '1' in 'formatoptions' */
5023#ifdef FEAT_COMMENTS
5024 do_comments = has_format_option(FO_Q_COMS);
5025#endif
5026 do_second_indent = has_format_option(FO_Q_SECOND);
5027 do_number_indent = has_format_option(FO_Q_NUMBER);
5028 do_trail_white = has_format_option(FO_WHITE_PAR);
5029
5030 /*
5031 * Get info about the previous and current line.
5032 */
5033 if (curwin->w_cursor.lnum > 1)
5034 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5035#ifdef FEAT_COMMENTS
5036 , &leader_len, &leader_flags, do_comments
5037#endif
5038 );
5039 else
5040 is_not_par = TRUE;
5041 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5042#ifdef FEAT_COMMENTS
5043 , &next_leader_len, &next_leader_flags, do_comments
5044#endif
5045 );
5046 is_end_par = (is_not_par || next_is_not_par);
5047 if (!is_end_par && do_trail_white)
5048 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5049
5050 curwin->w_cursor.lnum--;
5051 for (count = line_count; count != 0 && !got_int; --count)
5052 {
5053 /*
5054 * Advance to next paragraph.
5055 */
5056 if (advance)
5057 {
5058 curwin->w_cursor.lnum++;
5059 prev_is_end_par = is_end_par;
5060 is_not_par = next_is_not_par;
5061#ifdef FEAT_COMMENTS
5062 leader_len = next_leader_len;
5063 leader_flags = next_leader_flags;
5064#endif
5065 }
5066
5067 /*
5068 * The last line to be formatted.
5069 */
5070 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5071 {
5072 next_is_not_par = TRUE;
5073#ifdef FEAT_COMMENTS
5074 next_leader_len = 0;
5075 next_leader_flags = NULL;
5076#endif
5077 }
5078 else
5079 {
5080 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5081#ifdef FEAT_COMMENTS
5082 , &next_leader_len, &next_leader_flags, do_comments
5083#endif
5084 );
5085 if (do_number_indent)
5086 next_is_start_par =
5087 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5088 }
5089 advance = TRUE;
5090 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5091 if (!is_end_par && do_trail_white)
5092 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5093
5094 /*
5095 * Skip lines that are not in a paragraph.
5096 */
5097 if (is_not_par)
5098 {
5099 if (line_count < 0)
5100 break;
5101 }
5102 else
5103 {
5104 /*
5105 * For the first line of a paragraph, check indent of second line.
5106 * Don't do this for comments and empty lines.
5107 */
5108 if (first_par_line
5109 && (do_second_indent || do_number_indent)
5110 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005111 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005113 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005114 {
5115#ifdef FEAT_COMMENTS
5116 if (leader_len == 0 && next_leader_len == 0)
5117 {
5118 /* no comment found */
5119#endif
5120 second_indent =
5121 get_indent_lnum(curwin->w_cursor.lnum + 1);
5122#ifdef FEAT_COMMENTS
5123 }
5124 else
5125 {
5126 second_indent = next_leader_len;
5127 do_comments_list = 1;
5128 }
5129#endif
5130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005132 {
5133#ifdef FEAT_COMMENTS
5134 if (leader_len == 0 && next_leader_len == 0)
5135 {
5136 /* no comment found */
5137#endif
5138 second_indent =
5139 get_number_indent(curwin->w_cursor.lnum);
5140#ifdef FEAT_COMMENTS
5141 }
5142 else
5143 {
5144 /* get_number_indent() is now "comment aware"... */
5145 second_indent =
5146 get_number_indent(curwin->w_cursor.lnum);
5147 do_comments_list = 1;
5148 }
5149#endif
5150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152
5153 /*
5154 * When the comment leader changes, it's the end of the paragraph.
5155 */
5156 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5157#ifdef FEAT_COMMENTS
5158 || !same_leader(curwin->w_cursor.lnum,
5159 leader_len, leader_flags,
5160 next_leader_len, next_leader_flags)
5161#endif
5162 )
5163 is_end_par = TRUE;
5164
5165 /*
5166 * If we have got to the end of a paragraph, or the line is
5167 * getting long, format it.
5168 */
5169 if (is_end_par || force_format)
5170 {
5171 if (need_set_indent)
5172 /* replace indent in first line with minimal number of
5173 * tabs and spaces, according to current options */
5174 (void)set_indent(get_indent(), SIN_CHANGED);
5175
5176 /* put cursor on last non-space */
5177 State = NORMAL; /* don't go past end-of-line */
5178 coladvance((colnr_T)MAXCOL);
5179 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5180 dec_cursor();
5181
5182 /* do the formatting, without 'showmode' */
5183 State = INSERT; /* for open_line() */
5184 smd_save = p_smd;
5185 p_smd = FALSE;
5186 insertchar(NUL, INSCHAR_FORMAT
5187#ifdef FEAT_COMMENTS
5188 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005189 + (do_comments && do_comments_list
5190 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005192 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 State = old_State;
5194 p_smd = smd_save;
5195 second_indent = -1;
5196 /* at end of par.: need to set indent of next par. */
5197 need_set_indent = is_end_par;
5198 if (is_end_par)
5199 {
5200 /* When called with a negative line count, break at the
5201 * end of the paragraph. */
5202 if (line_count < 0)
5203 break;
5204 first_par_line = TRUE;
5205 }
5206 force_format = FALSE;
5207 }
5208
5209 /*
5210 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005211 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 */
5213 if (!is_end_par)
5214 {
5215 advance = FALSE;
5216 curwin->w_cursor.lnum++;
5217 curwin->w_cursor.col = 0;
5218 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005222 {
5223 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005225 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005226 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005228 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5229 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005230 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005231
5232 if (indent > 0)
5233 {
5234 (void)del_bytes(indent, FALSE, FALSE);
5235 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005236 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005237 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005240 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 beep_flush();
5243 break;
5244 }
5245 first_par_line = FALSE;
5246 /* If the line is getting long, format it next time */
5247 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5248 force_format = TRUE;
5249 else
5250 force_format = FALSE;
5251 }
5252 }
5253 line_breakcheck();
5254 }
5255}
5256
5257/*
5258 * Return TRUE if line "lnum" ends in a white character.
5259 */
5260 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005261ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
5263 char_u *s = ml_get(lnum);
5264 size_t l;
5265
5266 if (*s == NUL)
5267 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005268 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 * invocation may call function multiple times". */
5270 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005271 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272}
5273
5274/*
5275 * Blank lines, and lines containing only the comment leader, are left
5276 * untouched by the formatting. The function returns TRUE in this
5277 * case. It also returns TRUE when a line starts with the end of a comment
5278 * ('e' in comment flags), so that this line is skipped, and not joined to the
5279 * previous line. A new paragraph starts after a blank line, or when the
5280 * comment leader changes -- webb.
5281 */
5282#ifdef FEAT_COMMENTS
5283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284fmt_check_par(
5285 linenr_T lnum,
5286 int *leader_len,
5287 char_u **leader_flags,
5288 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290 char_u *flags = NULL; /* init for GCC */
5291 char_u *ptr;
5292
5293 ptr = ml_get(lnum);
5294 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005295 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 else
5297 *leader_len = 0;
5298
5299 if (*leader_len > 0)
5300 {
5301 /*
5302 * Search for 'e' flag in comment leader flags.
5303 */
5304 flags = *leader_flags;
5305 while (*flags && *flags != ':' && *flags != COM_END)
5306 ++flags;
5307 }
5308
5309 return (*skipwhite(ptr + *leader_len) == NUL
5310 || (*leader_len > 0 && *flags == COM_END)
5311 || startPS(lnum, NUL, FALSE));
5312}
5313#else
5314 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5318}
5319#endif
5320
5321/*
5322 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5323 * previous line is in the same paragraph. Used for auto-formatting.
5324 */
5325 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005326paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 char_u *p;
5329#ifdef FEAT_COMMENTS
5330 int leader_len = 0; /* leader len of current line */
5331 char_u *leader_flags = NULL; /* flags for leader of current line */
5332 int next_leader_len; /* leader len of next line */
5333 char_u *next_leader_flags; /* flags for leader of next line */
5334 int do_comments; /* format comments */
5335#endif
5336
5337 if (lnum <= 1)
5338 return TRUE; /* start of the file */
5339
5340 p = ml_get(lnum - 1);
5341 if (*p == NUL)
5342 return TRUE; /* after empty line */
5343
5344#ifdef FEAT_COMMENTS
5345 do_comments = has_format_option(FO_Q_COMS);
5346#endif
5347 if (fmt_check_par(lnum - 1
5348#ifdef FEAT_COMMENTS
5349 , &leader_len, &leader_flags, do_comments
5350#endif
5351 ))
5352 return TRUE; /* after non-paragraph line */
5353
5354 if (fmt_check_par(lnum
5355#ifdef FEAT_COMMENTS
5356 , &next_leader_len, &next_leader_flags, do_comments
5357#endif
5358 ))
5359 return TRUE; /* "lnum" is not a paragraph line */
5360
5361 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5362 return TRUE; /* missing trailing space in previous line. */
5363
5364 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5365 return TRUE; /* numbered item starts in "lnum". */
5366
5367#ifdef FEAT_COMMENTS
5368 if (!same_leader(lnum - 1, leader_len, leader_flags,
5369 next_leader_len, next_leader_flags))
5370 return TRUE; /* change of comment leader. */
5371#endif
5372
5373 return FALSE;
5374}
5375
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376/*
5377 * prepare a few things for block mode yank/delete/tilde
5378 *
5379 * for delete:
5380 * - textlen includes the first/last char to be (partly) deleted
5381 * - start/endspaces is the number of columns that are taken by the
5382 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005383 * deleted.
5384 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 * - textlen includes the first/last char to be wholly yanked
5386 * - start/endspaces is the number of columns of the first/last yanked char
5387 * that are to be yanked.
5388 */
5389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005390block_prep(
5391 oparg_T *oap,
5392 struct block_def *bdp,
5393 linenr_T lnum,
5394 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 int incr = 0;
5397 char_u *pend;
5398 char_u *pstart;
5399 char_u *line;
5400 char_u *prev_pstart;
5401 char_u *prev_pend;
5402
5403 bdp->startspaces = 0;
5404 bdp->endspaces = 0;
5405 bdp->textlen = 0;
5406 bdp->start_vcol = 0;
5407 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 bdp->is_short = FALSE;
5409 bdp->is_oneChar = FALSE;
5410 bdp->pre_whitesp = 0;
5411 bdp->pre_whitesp_c = 0;
5412 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 bdp->start_char_vcols = 0;
5414
5415 line = ml_get(lnum);
5416 pstart = line;
5417 prev_pstart = line;
5418 while (bdp->start_vcol < oap->start_vcol && *pstart)
5419 {
5420 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005421 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005423 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 bdp->pre_whitesp += incr;
5426 bdp->pre_whitesp_c++;
5427 }
5428 else
5429 {
5430 bdp->pre_whitesp = 0;
5431 bdp->pre_whitesp_c = 0;
5432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005434 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
5436 bdp->start_char_vcols = incr;
5437 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5438 {
5439 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (!is_del || oap->op_type == OP_APPEND)
5442 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5443 }
5444 else
5445 {
5446 /* notice: this converts partly selected Multibyte characters to
5447 * spaces, too. */
5448 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5449 if (is_del && bdp->startspaces)
5450 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5451 pend = pstart;
5452 bdp->end_vcol = bdp->start_vcol;
5453 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (oap->op_type == OP_INSERT)
5457 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5458 else if (oap->op_type == OP_APPEND)
5459 {
5460 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5461 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5462 }
5463 else
5464 {
5465 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5466 if (is_del && oap->op_type != OP_LSHIFT)
5467 {
5468 /* just putting the sum of those two into
5469 * bdp->startspaces doesn't work for Visual replace,
5470 * so we have to split the tab in two */
5471 bdp->startspaces = bdp->start_char_vcols
5472 - (bdp->start_vcol - oap->start_vcol);
5473 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5474 }
5475 }
5476 }
5477 else
5478 {
5479 prev_pend = pend;
5480 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5481 {
5482 /* Count a tab for what it's worth (if list mode not on) */
5483 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005484 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 bdp->end_vcol += incr;
5486 }
5487 if (bdp->end_vcol <= oap->end_vcol
5488 && (!is_del
5489 || oap->op_type == OP_APPEND
5490 || oap->op_type == OP_REPLACE)) /* line too short */
5491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 /* Alternative: include spaces to fill up the block.
5494 * Disadvantage: can lead to trailing spaces when the line is
5495 * short where the text is put */
5496 /* if (!is_del || oap->op_type == OP_APPEND) */
5497 if (oap->op_type == OP_APPEND || virtual_op)
5498 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005499 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else
5501 bdp->endspaces = 0; /* replace doesn't add characters */
5502 }
5503 else if (bdp->end_vcol > oap->end_vcol)
5504 {
5505 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5506 if (!is_del && bdp->endspaces)
5507 {
5508 bdp->endspaces = incr - bdp->endspaces;
5509 if (pend != pstart)
5510 pend = prev_pend;
5511 }
5512 }
5513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (is_del && bdp->startspaces)
5516 pstart = prev_pstart;
5517 bdp->textlen = (int)(pend - pstart);
5518 }
5519 bdp->textcol = (colnr_T) (pstart - line);
5520 bdp->textstart = pstart;
5521}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005524 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005526 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527op_addsub(
5528 oparg_T *oap,
5529 linenr_T Prenum1, /* Amount of add/subtract */
5530 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005532 pos_T pos;
5533 struct block_def bd;
5534 int change_cnt = 0;
5535 linenr_T amount = Prenum1;
5536
Bram Moolenaar6b731882018-11-16 20:54:47 +01005537 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
5538 // buffer is not completly updated yet. Postpone updating folds until before
5539 // the call to changed_lines().
5540#ifdef FEAT_FOLDING
5541 disable_fold_update++;
5542#endif
5543
Bram Moolenaard79e5502016-01-10 22:13:02 +01005544 if (!VIsual_active)
5545 {
5546 pos = curwin->w_cursor;
5547 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005548 {
5549#ifdef FEAT_FOLDING
5550 disable_fold_update--;
5551#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005552 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005553 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005554 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005555#ifdef FEAT_FOLDING
5556 disable_fold_update--;
5557#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005558 if (change_cnt)
5559 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5560 }
5561 else
5562 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005563 int one_change;
5564 int length;
5565 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005566
5567 if (u_save((linenr_T)(oap->start.lnum - 1),
5568 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005569 {
5570#ifdef FEAT_FOLDING
5571 disable_fold_update--;
5572#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005573 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005574 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005575
5576 pos = oap->start;
5577 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5578 {
5579 if (oap->block_mode) /* Visual block mode */
5580 {
5581 block_prep(oap, &bd, pos.lnum, FALSE);
5582 pos.col = bd.textcol;
5583 length = bd.textlen;
5584 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005585 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005586 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005587 curwin->w_cursor.col = 0;
5588 pos.col = 0;
5589 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5590 }
5591 else /* oap->motion_type == MCHAR */
5592 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005593 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005594 dec(&(oap->end));
5595 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5596 pos.col = 0;
5597 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005598 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005599 pos.col += oap->start.col;
5600 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005601 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005602 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005603 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005604 length = (int)STRLEN(ml_get(oap->end.lnum));
5605 if (oap->end.col >= length)
5606 oap->end.col = length - 1;
5607 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005608 }
5609 }
5610 one_change = do_addsub(oap->op_type, &pos, length, amount);
5611 if (one_change)
5612 {
5613 /* Remember the start position of the first change. */
5614 if (change_cnt == 0)
5615 startpos = curbuf->b_op_start;
5616 ++change_cnt;
5617 }
5618
5619#ifdef FEAT_NETBEANS_INTG
5620 if (netbeans_active() && one_change)
5621 {
5622 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5623
5624 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5625 netbeans_inserted(curbuf, pos.lnum, pos.col,
5626 &ptr[pos.col], length);
5627 }
5628#endif
5629 if (g_cmd && one_change)
5630 amount += Prenum1;
5631 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005632
5633#ifdef FEAT_FOLDING
5634 disable_fold_update--;
5635#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005636 if (change_cnt)
5637 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5638
5639 if (!change_cnt && oap->is_VIsual)
5640 /* No change: need to remove the Visual selection */
5641 redraw_curbuf_later(INVERTED);
5642
5643 /* Set '[ mark if something changed. Keep the last end
5644 * position from do_addsub(). */
5645 if (change_cnt > 0)
5646 curbuf->b_op_start = startpos;
5647
5648 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005649 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005650 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005651 }
5652}
5653
5654/*
5655 * Add or subtract 'Prenum1' from a number in a line
5656 * op_type is OP_NR_ADD or OP_NR_SUB
5657 *
5658 * Returns TRUE if some character was changed.
5659 */
5660 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005661do_addsub(
5662 int op_type,
5663 pos_T *pos,
5664 int length,
5665 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005666{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 int col;
5668 char_u *buf1;
5669 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005670 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005672 uvarnumber_T n;
5673 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 char_u *ptr;
5675 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 int todel;
5677 int dohex;
5678 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005679 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int doalp;
5681 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005683 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005684 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005685 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005686 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005687 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005688 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005689 pos_T startpos;
5690 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5693 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005694 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5696
Bram Moolenaard79e5502016-01-10 22:13:02 +01005697 curwin->w_cursor = *pos;
5698 ptr = ml_get(pos->lnum);
5699 col = pos->col;
5700
5701 if (*ptr == NUL)
5702 goto theend;
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 /*
5705 * First check if we are on a hexadecimal number, after the "0x".
5706 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005707 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005709 if (dobin)
5710 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005711 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005712 --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
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005719 if (dohex)
5720 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005721 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005722 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005723#ifdef FEAT_MBYTE
5724 if (has_mbyte)
5725 col -= (*mb_head_off)(ptr, ptr + col);
5726#endif
5727 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005728
5729 if ( dobin
5730 && dohex
5731 && ! ((col > 0
5732 && (ptr[col] == 'X'
5733 || ptr[col] == 'x')
5734 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005735#ifdef FEAT_MBYTE
5736 && (!has_mbyte ||
5737 !(*mb_head_off)(ptr, ptr + col - 1))
5738#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005739 && vim_isxdigit(ptr[col + 1]))))
5740 {
5741
5742 /* In case of binary/hexadecimal pattern overlap match, rescan */
5743
Bram Moolenaard79e5502016-01-10 22:13:02 +01005744 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005745
5746 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005747 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005748 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005749#ifdef FEAT_MBYTE
5750 if (has_mbyte)
5751 col -= (*mb_head_off)(ptr, ptr + col);
5752#endif
5753 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005754 }
5755
5756 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005757 && col > 0
5758 && (ptr[col] == 'X'
5759 || ptr[col] == 'x')
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_isxdigit(ptr[col + 1])) ||
5766 ( dobin
5767 && col > 0
5768 && (ptr[col] == 'B'
5769 || ptr[col] == 'b')
5770 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005771#ifdef FEAT_MBYTE
5772 && (!has_mbyte ||
5773 !(*mb_head_off)(ptr, ptr + col - 1))
5774#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005775 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005776 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005777 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005778 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005779#ifdef FEAT_MBYTE
5780 if (has_mbyte)
5781 col -= (*mb_head_off)(ptr, ptr + col);
5782#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005783 }
5784 else
5785 {
5786 /*
5787 * Search forward and then backward to find the start of number.
5788 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005789 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005790
5791 while (ptr[col] != NUL
5792 && !vim_isdigit(ptr[col])
5793 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005794 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005795
5796 while (col > 0
5797 && vim_isdigit(ptr[col - 1])
5798 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005799 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005800 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005801#ifdef FEAT_MBYTE
5802 if (has_mbyte)
5803 col -= (*mb_head_off)(ptr, ptr + col);
5804#endif
5805 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005806 }
5807 }
5808
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005809 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005810 {
5811 while (ptr[col] != NUL && length > 0
5812 && !vim_isdigit(ptr[col])
5813 && !(doalp && ASCII_ISALPHA(ptr[col])))
5814 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005815 int mb_len = MB_PTR2LEN(ptr + col);
5816
5817 col += mb_len;
5818 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005819 }
5820
5821 if (length == 0)
5822 goto theend;
5823
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005824 if (col > pos->col && ptr[col - 1] == '-'
5825#ifdef FEAT_MBYTE
5826 && (!has_mbyte ||
5827 !(*mb_head_off)(ptr, ptr + col - 1))
5828#endif
5829 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005830 {
5831 negative = TRUE;
5832 was_positive = FALSE;
5833 }
5834 }
5835
5836 /*
5837 * If a number was found, and saving for undo works, replace the number.
5838 */
5839 firstdigit = ptr[col];
5840 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5841 {
5842 beep_flush();
5843 goto theend;
5844 }
5845
5846 if (doalp && ASCII_ISALPHA(firstdigit))
5847 {
5848 /* decrement or increment alphabetic character */
5849 if (op_type == OP_NR_SUB)
5850 {
5851 if (CharOrd(firstdigit) < Prenum1)
5852 {
5853 if (isupper(firstdigit))
5854 firstdigit = 'A';
5855 else
5856 firstdigit = 'a';
5857 }
5858 else
5859#ifdef EBCDIC
5860 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5861#else
5862 firstdigit -= Prenum1;
5863#endif
5864 }
5865 else
5866 {
5867 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5868 {
5869 if (isupper(firstdigit))
5870 firstdigit = 'Z';
5871 else
5872 firstdigit = 'z';
5873 }
5874 else
5875#ifdef EBCDIC
5876 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5877#else
5878 firstdigit += Prenum1;
5879#endif
5880 }
5881 curwin->w_cursor.col = col;
5882 if (!did_change)
5883 startpos = curwin->w_cursor;
5884 did_change = TRUE;
5885 (void)del_char(FALSE);
5886 ins_char(firstdigit);
5887 endpos = curwin->w_cursor;
5888 curwin->w_cursor.col = col;
5889 }
5890 else
5891 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005892 if (col > 0 && ptr[col - 1] == '-'
5893#ifdef FEAT_MBYTE
5894 && (!has_mbyte ||
5895 !(*mb_head_off)(ptr, ptr + col - 1))
5896#endif
5897 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005898 {
5899 /* negative number */
5900 --col;
5901 negative = TRUE;
5902 }
5903 /* get the number value (unsigned) */
5904 if (visual && VIsual_mode != 'V')
5905 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5906 ? (int)STRLEN(ptr) - col
5907 : length);
5908
5909 vim_str2nr(ptr + col, &pre, &length,
5910 0 + (dobin ? STR2NR_BIN : 0)
5911 + (dooct ? STR2NR_OCT : 0)
5912 + (dohex ? STR2NR_HEX : 0),
5913 NULL, &n, maxlen);
5914
5915 /* ignore leading '-' for hex and octal and bin numbers */
5916 if (pre && negative)
5917 {
5918 ++col;
5919 --length;
5920 negative = FALSE;
5921 }
5922 /* add or subtract */
5923 subtract = FALSE;
5924 if (op_type == OP_NR_SUB)
5925 subtract ^= TRUE;
5926 if (negative)
5927 subtract ^= TRUE;
5928
5929 oldn = n;
5930 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005931 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005932 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005933 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005934 /* handle wraparound for decimal numbers */
5935 if (!pre)
5936 {
5937 if (subtract)
5938 {
5939 if (n > oldn)
5940 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005941 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005942 negative ^= TRUE;
5943 }
5944 }
5945 else
5946 {
5947 /* add */
5948 if (n < oldn)
5949 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005950 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005951 negative ^= TRUE;
5952 }
5953 }
5954 if (n == 0)
5955 negative = FALSE;
5956 }
5957
5958 if (visual && !was_positive && !negative && col > 0)
5959 {
5960 /* need to remove the '-' */
5961 col--;
5962 length++;
5963 }
5964
5965 /*
5966 * Delete the old number.
5967 */
5968 curwin->w_cursor.col = col;
5969 if (!did_change)
5970 startpos = curwin->w_cursor;
5971 did_change = TRUE;
5972 todel = length;
5973 c = gchar_cursor();
5974 /*
5975 * Don't include the '-' in the length, only the length of the
5976 * part after it is kept the same.
5977 */
5978 if (c == '-')
5979 --length;
5980 while (todel-- > 0)
5981 {
5982 if (c < 0x100 && isalpha(c))
5983 {
5984 if (isupper(c))
5985 hexupper = TRUE;
5986 else
5987 hexupper = FALSE;
5988 }
5989 /* del_char() will mark line needing displaying */
5990 (void)del_char(FALSE);
5991 c = gchar_cursor();
5992 }
5993
5994 /*
5995 * Prepare the leading characters in buf1[].
5996 * When there are many leading zeros it could be very long.
5997 * Allocate a bit too much.
5998 */
5999 buf1 = alloc((unsigned)length + NUMBUFLEN);
6000 if (buf1 == NULL)
6001 goto theend;
6002 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02006003 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01006004 {
6005 *ptr++ = '-';
6006 }
6007 if (pre)
6008 {
6009 *ptr++ = '0';
6010 --length;
6011 }
6012 if (pre == 'b' || pre == 'B' ||
6013 pre == 'x' || pre == 'X')
6014 {
6015 *ptr++ = pre;
6016 --length;
6017 }
6018
6019 /*
6020 * Put the number characters in buf2[].
6021 */
6022 if (pre == 'b' || pre == 'B')
6023 {
6024 int i;
6025 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006026 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006027
6028 /* leading zeros */
6029 for (bit = bits; bit > 0; bit--)
6030 if ((n >> (bit - 1)) & 0x1) break;
6031
6032 for (i = 0; bit > 0; bit--)
6033 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6034
6035 buf2[i] = '\0';
6036 }
6037 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006038 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
6039 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006040 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006041 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
6042 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006043 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006044 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
6045 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006046 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006047 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
6048 (long long unsigned)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006049 length -= (int)STRLEN(buf2);
6050
6051 /*
6052 * Adjust number of zeros to the new number of digits, so the
6053 * total length of the number remains the same.
6054 * Don't do this when
6055 * the result may look like an octal number.
6056 */
6057 if (firstdigit == '0' && !(dooct && pre == 0))
6058 while (length-- > 0)
6059 *ptr++ = '0';
6060 *ptr = NUL;
6061 STRCAT(buf1, buf2);
6062 ins_str(buf1); /* insert the new number */
6063 vim_free(buf1);
6064 endpos = curwin->w_cursor;
6065 if (did_change && curwin->w_cursor.col)
6066 --curwin->w_cursor.col;
6067 }
6068
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006069 if (did_change)
6070 {
6071 /* set the '[ and '] marks */
6072 curbuf->b_op_start = startpos;
6073 curbuf->b_op_end = endpos;
6074 if (curbuf->b_op_end.col > 0)
6075 --curbuf->b_op_end.col;
6076 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006077
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006078theend:
6079 if (visual)
6080 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006081 else if (did_change)
6082 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006083
Bram Moolenaard79e5502016-01-10 22:13:02 +01006084 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085}
6086
6087#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006088
6089static yankreg_T *y_read_regs = NULL;
6090
6091#define REG_PREVIOUS 1
6092#define REG_EXEC 2
6093
6094/*
6095 * Prepare for reading viminfo registers when writing viminfo later.
6096 */
6097 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006098prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006099{
6100 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6101 * (int)sizeof(yankreg_T));
6102}
6103
6104 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006105finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006106{
6107 int i;
6108 int j;
6109
6110 if (y_read_regs != NULL)
6111 {
6112 for (i = 0; i < NUM_REGISTERS; ++i)
6113 if (y_read_regs[i].y_array != NULL)
6114 {
6115 for (j = 0; j < y_read_regs[i].y_size; j++)
6116 vim_free(y_read_regs[i].y_array[j]);
6117 vim_free(y_read_regs[i].y_array);
6118 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006119 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006120 }
6121}
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006124read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 int eof;
6127 int do_it = TRUE;
6128 int size;
6129 int limit;
6130 int i;
6131 int set_prev = FALSE;
6132 char_u *str;
6133 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006134 int new_type = MCHAR; /* init to shut up compiler */
6135 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137 /* We only get here (hopefully) if line[0] == '"' */
6138 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006139
6140 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 if (*str == '"')
6142 {
6143 set_prev = TRUE;
6144 str++;
6145 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 if (!ASCII_ISALNUM(*str) && *str != '-')
6148 {
6149 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6150 return TRUE; /* too many errors, pretend end-of-file */
6151 do_it = FALSE;
6152 }
6153 get_yank_register(*str++, FALSE);
6154 if (!force && y_current->y_array != NULL)
6155 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006156
6157 if (*str == '@')
6158 {
6159 /* "x@: register x used for @@ */
6160 if (force || execreg_lastc == NUL)
6161 execreg_lastc = str[-1];
6162 }
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 size = 0;
6165 limit = 100; /* Optimized for registers containing <= 100 lines */
6166 if (do_it)
6167 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006168 /*
6169 * Build the new register in array[].
6170 * y_array is kept as-is until done.
6171 * The "do_it" flag is reset when something is wrong, in which case
6172 * array[] needs to be freed.
6173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (set_prev)
6175 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006176 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006177 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006179 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006181 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006183 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 /* get the block width; if it's missing we get a zero, which is OK */
6185 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006186 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188
6189 while (!(eof = viminfo_readline(virp))
6190 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6191 {
6192 if (do_it)
6193 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006194 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006196 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006198
6199 if (new_array == NULL)
6200 {
6201 do_it = FALSE;
6202 break;
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006205 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006207 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 str = viminfo_readstring(virp, 1, TRUE);
6211 if (str != NULL)
6212 array[size++] = str;
6213 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006214 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 do_it = FALSE;
6216 }
6217 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (do_it)
6220 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006221 /* free y_array[] */
6222 for (i = 0; i < y_current->y_size; i++)
6223 vim_free(y_current->y_array[i]);
6224 vim_free(y_current->y_array);
6225
6226 y_current->y_type = new_type;
6227 y_current->y_width = new_width;
6228 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006229 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 if (size == 0)
6231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 y_current->y_array = NULL;
6233 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006236 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 y_current->y_array =
6238 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6239 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006240 {
6241 if (y_current->y_array == NULL)
6242 vim_free(array[i]);
6243 else
6244 y_current->y_array[i] = array[i];
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006248 else
6249 {
6250 /* Free array[] if it was filled. */
6251 for (i = 0; i < size; i++)
6252 vim_free(array[i]);
6253 }
6254 vim_free(array);
6255
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 return eof;
6257}
6258
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006259/*
6260 * Accept a new style register line from the viminfo, store it when it's new.
6261 */
6262 void
6263handle_viminfo_register(garray_T *values, int force)
6264{
6265 bval_T *vp = (bval_T *)values->ga_data;
6266 int flags;
6267 int name;
6268 int type;
6269 int linecount;
6270 int width;
6271 time_t timestamp;
6272 yankreg_T *y_ptr;
6273 int i;
6274
6275 /* Check the format:
6276 * |{bartype},{flags},{name},{type},
6277 * {linecount},{width},{timestamp},"line1","line2"
6278 */
6279 if (values->ga_len < 6
6280 || vp[0].bv_type != BVAL_NR
6281 || vp[1].bv_type != BVAL_NR
6282 || vp[2].bv_type != BVAL_NR
6283 || vp[3].bv_type != BVAL_NR
6284 || vp[4].bv_type != BVAL_NR
6285 || vp[5].bv_type != BVAL_NR)
6286 return;
6287 flags = vp[0].bv_nr;
6288 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006289 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006290 return;
6291 type = vp[2].bv_nr;
6292 if (type != MCHAR && type != MLINE && type != MBLOCK)
6293 return;
6294 linecount = vp[3].bv_nr;
6295 if (values->ga_len < 6 + linecount)
6296 return;
6297 width = vp[4].bv_nr;
6298 if (width < 0)
6299 return;
6300
6301 if (y_read_regs != NULL)
6302 /* Reading viminfo for merging and writing. Store the register
6303 * content, don't update the current registers. */
6304 y_ptr = &y_read_regs[name];
6305 else
6306 y_ptr = &y_regs[name];
6307
6308 /* Do not overwrite unless forced or the timestamp is newer. */
6309 timestamp = (time_t)vp[5].bv_nr;
6310 if (y_ptr->y_array != NULL && !force
6311 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6312 return;
6313
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006314 if (y_ptr->y_array != NULL)
6315 for (i = 0; i < y_ptr->y_size; i++)
6316 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006317 vim_free(y_ptr->y_array);
6318
6319 if (y_read_regs == NULL)
6320 {
6321 if (flags & REG_PREVIOUS)
6322 y_previous = y_ptr;
6323 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6324 execreg_lastc = get_register_name(name);
6325 }
6326 y_ptr->y_type = type;
6327 y_ptr->y_width = width;
6328 y_ptr->y_size = linecount;
6329 y_ptr->y_time_set = timestamp;
6330 if (linecount == 0)
6331 y_ptr->y_array = NULL;
6332 else
6333 {
6334 y_ptr->y_array =
6335 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6336 for (i = 0; i < linecount; i++)
6337 {
6338 if (vp[i + 6].bv_allocated)
6339 {
6340 y_ptr->y_array[i] = vp[i + 6].bv_string;
6341 vp[i + 6].bv_string = NULL;
6342 }
6343 else
6344 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6345 }
6346 }
6347}
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006350write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006352 int i, j;
6353 char_u *type;
6354 char_u c;
6355 int num_lines;
6356 int max_num_lines;
6357 int max_kbyte;
6358 long len;
6359 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
Bram Moolenaar64404472010-06-26 06:24:45 +02006361 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 /* Get '<' value, use old '"' value if '<' is not found. */
6364 max_num_lines = get_viminfo_parameter('<');
6365 if (max_num_lines < 0)
6366 max_num_lines = get_viminfo_parameter('"');
6367 if (max_num_lines == 0)
6368 return;
6369 max_kbyte = get_viminfo_parameter('s');
6370 if (max_kbyte == 0)
6371 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006372
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 for (i = 0; i < NUM_REGISTERS; i++)
6374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375#ifdef FEAT_CLIPBOARD
6376 /* Skip '*'/'+' register, we don't want them back next time */
6377 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6378 continue;
6379#endif
6380#ifdef FEAT_DND
6381 /* Neither do we want the '~' register */
6382 if (i == TILDE_REGISTER)
6383 continue;
6384#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006385 /* When reading viminfo for merging and writing: Use the register from
6386 * viminfo if it's newer. */
6387 if (y_read_regs != NULL
6388 && y_read_regs[i].y_array != NULL
6389 && (y_regs[i].y_array == NULL ||
6390 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6391 y_ptr = &y_read_regs[i];
6392 else if (y_regs[i].y_array == NULL)
6393 continue;
6394 else
6395 y_ptr = &y_regs[i];
6396
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006397 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006398 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006399 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006400 || (num_lines == 1 && y_ptr->y_type == MCHAR
6401 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006402 continue;
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (max_kbyte > 0)
6405 {
6406 /* Skip register if there is more text than the maximum size. */
6407 len = 0;
6408 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006409 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (len > (long)max_kbyte * 1024L)
6411 continue;
6412 }
6413
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006414 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
6416 case MLINE:
6417 type = (char_u *)"LINE";
6418 break;
6419 case MCHAR:
6420 type = (char_u *)"CHAR";
6421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 case MBLOCK:
6423 type = (char_u *)"BLOCK";
6424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006426 semsg(_("E574: Unknown register type %d"), y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 type = (char_u *)"LINE";
6428 break;
6429 }
6430 if (y_previous == &y_regs[i])
6431 fprintf(fp, "\"");
6432 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006433 fprintf(fp, "\"%c", c);
6434 if (c == execreg_lastc)
6435 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006436 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 /* If max_num_lines < 0, then we save ALL the lines in the register */
6439 if (max_num_lines > 0 && num_lines > max_num_lines)
6440 num_lines = max_num_lines;
6441 for (j = 0; j < num_lines; j++)
6442 {
6443 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006444 viminfo_writestring(fp, y_ptr->y_array[j]);
6445 }
6446
6447 {
6448 int flags = 0;
6449 int remaining;
6450
6451 /* New style with a bar line. Format:
6452 * |{bartype},{flags},{name},{type},
6453 * {linecount},{width},{timestamp},"line1","line2"
6454 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006455 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006456 */
6457 if (y_previous == &y_regs[i])
6458 flags |= REG_PREVIOUS;
6459 if (c == execreg_lastc)
6460 flags |= REG_EXEC;
6461 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6462 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6463 (long)y_ptr->y_time_set);
6464 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6465 remaining = LSIZE - 71;
6466 for (j = 0; j < num_lines; j++)
6467 {
6468 putc(',', fp);
6469 --remaining;
6470 remaining = barline_writestring(fp, y_ptr->y_array[j],
6471 remaining);
6472 }
6473 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 }
6476}
6477#endif /* FEAT_VIMINFO */
6478
6479#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6480/*
6481 * SELECTION / PRIMARY ('*')
6482 *
6483 * Text selection stuff that uses the GUI selection register '*'. When using a
6484 * GUI this may be text from another window, otherwise it is the last text we
6485 * had highlighted with VIsual mode. With mouse support, clicking the middle
6486 * button performs the paste, otherwise you will need to do <"*p>. "
6487 * If not under X, it is synonymous with the clipboard register '+'.
6488 *
6489 * X CLIPBOARD ('+')
6490 *
6491 * Text selection stuff that uses the GUI clipboard register '+'.
6492 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6493 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6494 * otherwise you will need to do <"+p>. "
6495 * If not under X, it is synonymous with the selection register '*'.
6496 */
6497
6498/*
6499 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006500 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 * only exist while the owning application exists, so we write to the
6502 * permanent (while X runs) store CUT_BUFFER0.
6503 * Dump the CLIPBOARD selection if we own it (it's logically the more
6504 * 'permanent' of the two), otherwise the PRIMARY one.
6505 * For now, use a hard-coded sanity limit of 1Mb of data.
6506 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006507#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 Display *dpy;
6512 char_u *str = NULL;
6513 long_u len = 0;
6514 int motion_type = -1;
6515
6516# ifdef FEAT_GUI
6517 if (gui.in_use)
6518 dpy = X_DISPLAY;
6519 else
6520# endif
6521# ifdef FEAT_XCLIPBOARD
6522 dpy = xterm_dpy;
6523# else
6524 return;
6525# endif
6526
6527 /* Get selection to export */
6528 if (clip_plus.owned)
6529 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6530 else if (clip_star.owned)
6531 motion_type = clip_convert_selection(&str, &len, &clip_star);
6532
6533 /* Check it's OK */
6534 if (dpy != NULL && str != NULL && motion_type >= 0
6535 && len < 1024*1024 && len > 0)
6536 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006537#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006538 int ok = TRUE;
6539
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006540 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6541 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6542 * encoding conversion usually doesn't work, so keep the text as-is.
6543 */
6544 if (has_mbyte)
6545 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006546 vimconv_T vc;
6547
6548 vc.vc_type = CONV_NONE;
6549 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6550 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006551 int intlen = len;
6552 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006553
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006554 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006555 conv_str = string_convert(&vc, str, &intlen);
6556 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006557 if (conv_str != NULL)
6558 {
6559 vim_free(str);
6560 str = conv_str;
6561 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006562 else
6563 {
6564 ok = FALSE;
6565 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006566 convert_setup(&vc, NULL, NULL);
6567 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006568 else
6569 {
6570 ok = FALSE;
6571 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006572 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006573
6574 /* Do not store the string if conversion failed. Better to use any
6575 * other selection than garbled text. */
6576 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006577#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006578 {
6579 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6580 XFlush(dpy);
6581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 }
6583
6584 vim_free(str);
6585}
6586#endif
6587
6588 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006589clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006591 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 if (cbd == &clip_plus)
6594 y_current = &y_regs[PLUS_REGISTER];
6595 else
6596 y_current = &y_regs[STAR_REGISTER];
6597 free_yank_all();
6598 y_current->y_size = 0;
6599 y_current = y_ptr;
6600}
6601
6602/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006603 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 */
6605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006608 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 pos_T old_visual;
6611 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 colnr_T old_curswant;
6613 int old_set_curswant;
6614 pos_T old_op_start, old_op_end;
6615 oparg_T oa;
6616 cmdarg_T ca;
6617
6618 if (cbd->owned)
6619 {
6620 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6621 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6622 return;
6623
6624 /* Get the text between clip_star.start & clip_star.end */
6625 old_y_previous = y_previous;
6626 old_y_current = y_current;
6627 old_cursor = curwin->w_cursor;
6628 old_curswant = curwin->w_curswant;
6629 old_set_curswant = curwin->w_set_curswant;
6630 old_op_start = curbuf->b_op_start;
6631 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 old_visual = VIsual;
6633 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 clear_oparg(&oa);
6635 oa.regname = (cbd == &clip_plus ? '+' : '*');
6636 oa.op_type = OP_YANK;
6637 vim_memset(&ca, 0, sizeof(ca));
6638 ca.oap = &oa;
6639 ca.cmdchar = 'y';
6640 ca.count1 = 1;
6641 ca.retval = CA_NO_ADJ_OP_END;
6642 do_pending_operator(&ca, 0, TRUE);
6643 y_previous = old_y_previous;
6644 y_current = old_y_current;
6645 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006646 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 curwin->w_curswant = old_curswant;
6648 curwin->w_set_curswant = old_set_curswant;
6649 curbuf->b_op_start = old_op_start;
6650 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 VIsual = old_visual;
6652 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006654 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 clip_free_selection(cbd);
6657
6658 /* Try to get selected text from another window */
6659 clip_gen_request_selection(cbd);
6660 }
6661}
6662
Bram Moolenaard44347f2011-06-19 01:14:29 +02006663/*
6664 * Convert from the GUI selection string into the '*'/'+' register.
6665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006667clip_yank_selection(
6668 int type,
6669 char_u *str,
6670 long len,
6671 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006673 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 if (cbd == &clip_plus)
6676 y_ptr = &y_regs[PLUS_REGISTER];
6677 else
6678 y_ptr = &y_regs[STAR_REGISTER];
6679
6680 clip_free_selection(cbd);
6681
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006682 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684
6685/*
6686 * Convert the '*'/'+' register into a GUI selection string returned in *str
6687 * with length *len.
6688 * Returns the motion type, or -1 for failure.
6689 */
6690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 char_u *p;
6694 int lnum;
6695 int i, j;
6696 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006697 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 if (cbd == &clip_plus)
6700 y_ptr = &y_regs[PLUS_REGISTER];
6701 else
6702 y_ptr = &y_regs[STAR_REGISTER];
6703
6704#ifdef USE_CRNL
6705 eolsize = 2;
6706#else
6707 eolsize = 1;
6708#endif
6709
6710 *str = NULL;
6711 *len = 0;
6712 if (y_ptr->y_array == NULL)
6713 return -1;
6714
6715 for (i = 0; i < y_ptr->y_size; i++)
6716 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6717
6718 /*
6719 * Don't want newline character at end of last line if we're in MCHAR mode.
6720 */
6721 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6722 *len -= eolsize;
6723
6724 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6725 if (p == NULL)
6726 return -1;
6727 lnum = 0;
6728 for (i = 0, j = 0; i < (int)*len; i++, j++)
6729 {
6730 if (y_ptr->y_array[lnum][j] == '\n')
6731 p[i] = NUL;
6732 else if (y_ptr->y_array[lnum][j] == NUL)
6733 {
6734#ifdef USE_CRNL
6735 p[i++] = '\r';
6736#endif
6737#ifdef USE_CR
6738 p[i] = '\r';
6739#else
6740 p[i] = '\n';
6741#endif
6742 lnum++;
6743 j = -1;
6744 }
6745 else
6746 p[i] = y_ptr->y_array[lnum][j];
6747 }
6748 return y_ptr->y_type;
6749}
6750
6751
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752/*
6753 * If we have written to a clipboard register, send the text to the clipboard.
6754 */
6755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006756may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6759 {
6760 clip_own_selection(&clip_star);
6761 clip_gen_set_selection(&clip_star);
6762 }
6763 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6764 {
6765 clip_own_selection(&clip_plus);
6766 clip_gen_set_selection(&clip_plus);
6767 }
6768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769
6770#endif /* FEAT_CLIPBOARD || PROTO */
6771
6772
6773#if defined(FEAT_DND) || defined(PROTO)
6774/*
6775 * Replace the contents of the '~' register with str.
6776 */
6777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006778dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006780 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
6782 curr = y_current;
6783 y_current = &y_regs[TILDE_REGISTER];
6784 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006785 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 y_current = curr;
6787}
6788#endif
6789
6790
6791#if defined(FEAT_EVAL) || defined(PROTO)
6792/*
6793 * Return the type of a register.
6794 * Used for getregtype()
6795 * Returns MAUTO for error.
6796 */
6797 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006798get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 switch (regname)
6801 {
6802 case '%': /* file name */
6803 case '#': /* alternate file name */
6804 case '=': /* expression */
6805 case ':': /* last command line */
6806 case '/': /* last search-pattern */
6807 case '.': /* last inserted text */
6808#ifdef FEAT_SEARCHPATH
6809 case Ctrl_F: /* Filename under cursor */
6810 case Ctrl_P: /* Path under cursor, expand via "path" */
6811#endif
6812 case Ctrl_W: /* word under cursor */
6813 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6814 case '_': /* black hole: always empty */
6815 return MCHAR;
6816 }
6817
6818#ifdef FEAT_CLIPBOARD
6819 regname = may_get_selection(regname);
6820#endif
6821
Bram Moolenaar32b92012014-01-14 12:33:36 +01006822 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006823 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 get_yank_register(regname, FALSE);
6826
6827 if (y_current->y_array != NULL)
6828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 if (reglen != NULL && y_current->y_type == MBLOCK)
6830 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 return y_current->y_type;
6832 }
6833 return MAUTO;
6834}
6835
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006836/*
6837 * When "flags" has GREG_LIST return a list with text "s".
6838 * Otherwise just return "s".
6839 */
6840 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006841getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006842{
6843 if (flags & GREG_LIST)
6844 {
6845 list_T *list = list_alloc();
6846
6847 if (list != NULL)
6848 {
6849 if (list_append_string(list, NULL, -1) == FAIL)
6850 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006851 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006852 return NULL;
6853 }
6854 list->lv_first->li_tv.vval.v_string = s;
6855 }
6856 return (char_u *)list;
6857 }
6858 return s;
6859}
6860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861/*
6862 * Return the contents of a register as a single allocated string.
6863 * Used for "@r" in expressions and for getreg().
6864 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006865 * Flags:
6866 * GREG_NO_EXPR Do not allow expression register
6867 * GREG_EXPR_SRC For the expression register: return expression itself,
6868 * not the result of its evaluation.
6869 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 */
6871 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006872get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 long i;
6875 char_u *retval;
6876 int allocated;
6877 long len;
6878
6879 /* Don't allow using an expression register inside an expression */
6880 if (regname == '=')
6881 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006882 if (flags & GREG_NO_EXPR)
6883 return NULL;
6884 if (flags & GREG_EXPR_SRC)
6885 return getreg_wrap_one_line(get_expr_line_src(), flags);
6886 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888
6889 if (regname == '@') /* "@@" is used for unnamed register */
6890 regname = '"';
6891
6892 /* check for valid regname */
6893 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6894 return NULL;
6895
6896#ifdef FEAT_CLIPBOARD
6897 regname = may_get_selection(regname);
6898#endif
6899
6900 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6901 {
6902 if (retval == NULL)
6903 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006904 if (allocated)
6905 return getreg_wrap_one_line(retval, flags);
6906 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908
6909 get_yank_register(regname, FALSE);
6910 if (y_current->y_array == NULL)
6911 return NULL;
6912
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006913 if (flags & GREG_LIST)
6914 {
6915 list_T *list = list_alloc();
6916 int error = FALSE;
6917
6918 if (list == NULL)
6919 return NULL;
6920 for (i = 0; i < y_current->y_size; ++i)
6921 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6922 error = TRUE;
6923 if (error)
6924 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006925 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006926 return NULL;
6927 }
6928 return (char_u *)list;
6929 }
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /*
6932 * Compute length of resulting string.
6933 */
6934 len = 0;
6935 for (i = 0; i < y_current->y_size; ++i)
6936 {
6937 len += (long)STRLEN(y_current->y_array[i]);
6938 /*
6939 * Insert a newline between lines and after last line if
6940 * y_type is MLINE.
6941 */
6942 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6943 ++len;
6944 }
6945
6946 retval = lalloc(len + 1, TRUE);
6947
6948 /*
6949 * Copy the lines of the yank register into the string.
6950 */
6951 if (retval != NULL)
6952 {
6953 len = 0;
6954 for (i = 0; i < y_current->y_size; ++i)
6955 {
6956 STRCPY(retval + len, y_current->y_array[i]);
6957 len += (long)STRLEN(retval + len);
6958
6959 /*
6960 * Insert a NL between lines and after the last line if y_type is
6961 * MLINE.
6962 */
6963 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6964 retval[len++] = '\n';
6965 }
6966 retval[len] = NUL;
6967 }
6968
6969 return retval;
6970}
6971
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006972 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006973init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006974 int name,
6975 yankreg_T **old_y_previous,
6976 yankreg_T **old_y_current,
6977 int must_append,
6978 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006979{
6980 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6981 {
6982 emsg_invreg(name);
6983 return FAIL;
6984 }
6985
6986 /* Don't want to change the current (unnamed) register */
6987 *old_y_previous = y_previous;
6988 *old_y_current = y_current;
6989
6990 get_yank_register(name, TRUE);
6991 if (!y_append && !must_append)
6992 free_yank_all();
6993 return OK;
6994}
6995
6996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006997finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006998 int name,
6999 yankreg_T *old_y_previous,
7000 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007001{
7002# ifdef FEAT_CLIPBOARD
7003 /* Send text of clipboard register to the clipboard. */
7004 may_set_selection();
7005# endif
7006
7007 /* ':let @" = "val"' should change the meaning of the "" register */
7008 if (name != '"')
7009 y_previous = old_y_previous;
7010 y_current = old_y_current;
7011}
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013/*
7014 * Store string "str" in register "name".
7015 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7016 * If "must_append" is TRUE, always append to the register. Otherwise append
7017 * if "name" is an uppercase letter.
7018 * Note: "maxlen" and "must_append" don't work for the "/" register.
7019 * Careful: 'str' is modified, you may have to use a copy!
7020 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7021 */
7022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007023write_reg_contents(
7024 int name,
7025 char_u *str,
7026 int maxlen,
7027 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7030}
7031
7032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007033write_reg_contents_lst(
7034 int name,
7035 char_u **strings,
7036 int maxlen UNUSED,
7037 int must_append,
7038 int yank_type,
7039 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007040{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007041 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007042
7043 if (name == '/'
7044#ifdef FEAT_EVAL
7045 || name == '='
7046#endif
7047 )
7048 {
7049 char_u *s;
7050
7051 if (strings[0] == NULL)
7052 s = (char_u *)"";
7053 else if (strings[1] != NULL)
7054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007055 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007056 "contain two or more lines"));
7057 return;
7058 }
7059 else
7060 s = strings[0];
7061 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7062 return;
7063 }
7064
7065 if (name == '_') /* black hole: nothing to do */
7066 return;
7067
7068 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7069 &yank_type) == FAIL)
7070 return;
7071
7072 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7073
7074 finish_write_reg(name, old_y_previous, old_y_current);
7075}
7076
7077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007078write_reg_contents_ex(
7079 int name,
7080 char_u *str,
7081 int maxlen,
7082 int must_append,
7083 int yank_type,
7084 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007086 yankreg_T *old_y_previous, *old_y_current;
7087 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaare7566042005-06-17 22:00:15 +00007089 if (maxlen >= 0)
7090 len = maxlen;
7091 else
7092 len = (long)STRLEN(str);
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 /* Special case: '/' search pattern */
7095 if (name == '/')
7096 {
7097 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7098 return;
7099 }
7100
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007101 if (name == '#')
7102 {
7103 buf_T *buf;
7104
7105 if (VIM_ISDIGIT(*str))
7106 {
7107 int num = atoi((char *)str);
7108
7109 buf = buflist_findnr(num);
7110 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007111 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007112 }
7113 else
7114 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7115 TRUE, FALSE, FALSE));
7116 if (buf == NULL)
7117 return;
7118 curwin->w_alt_fnum = buf->b_fnum;
7119 return;
7120 }
7121
Bram Moolenaare7566042005-06-17 22:00:15 +00007122#ifdef FEAT_EVAL
7123 if (name == '=')
7124 {
7125 char_u *p, *s;
7126
7127 p = vim_strnsave(str, (int)len);
7128 if (p == NULL)
7129 return;
7130 if (must_append)
7131 {
7132 s = concat_str(get_expr_line_src(), p);
7133 vim_free(p);
7134 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007135 }
7136 set_expr_line(p);
7137 return;
7138 }
7139#endif
7140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (name == '_') /* black hole: nothing to do */
7142 return;
7143
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007144 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7145 &yank_type) == FAIL)
7146 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007148 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007150 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151}
7152#endif /* FEAT_EVAL */
7153
7154#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7155/*
7156 * Put a string into a register. When the register is not empty, the string
7157 * is appended.
7158 */
7159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007160str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007161 yankreg_T *y_ptr, /* pointer to yank register */
7162 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7163 char_u *str, /* string to put in register */
7164 long len, /* length of string */
7165 long blocklen, /* width of Visual block */
7166 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007168 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 int lnum;
7170 long start;
7171 long i;
7172 int extra;
7173 int newlines; /* number of lines added */
7174 int extraline = 0; /* extra line at the end */
7175 int append = FALSE; /* append to last line in register */
7176 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007177 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007181 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 y_ptr->y_size = 0;
7183
Bram Moolenaard44347f2011-06-19 01:14:29 +02007184 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007185 type = ((str_list || (len > 0 && (str[len - 1] == NL
7186 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007187 ? MLINE : MCHAR);
7188 else
7189 type = yank_type;
7190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 /*
7192 * Count the number of lines within the string
7193 */
7194 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007195 if (str_list)
7196 {
7197 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007202 for (i = 0; i < len; i++)
7203 if (str[i] == '\n')
7204 ++newlines;
7205 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7206 {
7207 extraline = 1;
7208 ++newlines; /* count extra newline at the end */
7209 }
7210 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7211 {
7212 append = TRUE;
7213 --newlines; /* uncount newline when appending first line */
7214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
7216
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007217 /* Without any lines make the register empty. */
7218 if (y_ptr->y_size + newlines == 0)
7219 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007220 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007221 return;
7222 }
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 /*
7225 * Allocate an array to hold the pointers to the new register lines.
7226 * If the register was not empty, move the existing lines to the new array.
7227 */
7228 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7229 * sizeof(char_u *), TRUE);
7230 if (pp == NULL) /* out of memory */
7231 return;
7232 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7233 pp[lnum] = y_ptr->y_array[lnum];
7234 vim_free(y_ptr->y_array);
7235 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 /*
7239 * Find the end of each line and save it into the array.
7240 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007241 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007243 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7244 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007245 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007246 pp[lnum] = vim_strnsave(*ss, i);
7247 if (i > maxlen)
7248 maxlen = i;
7249 }
7250 }
7251 else
7252 {
7253 for (start = 0; start < len + extraline; start += i + 1)
7254 {
7255 for (i = start; i < len; ++i) /* find the end of the line */
7256 if (str[i] == '\n')
7257 break;
7258 i -= start; /* i is now length of line */
7259 if (i > maxlen)
7260 maxlen = i;
7261 if (append)
7262 {
7263 --lnum;
7264 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7265 }
7266 else
7267 extra = 0;
7268 s = alloc((unsigned)(i + extra + 1));
7269 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007271 if (extra)
7272 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7273 if (append)
7274 vim_free(y_ptr->y_array[lnum]);
7275 if (i)
7276 mch_memmove(s + extra, str + start, (size_t)i);
7277 extra += i;
7278 s[extra] = NUL;
7279 y_ptr->y_array[lnum++] = s;
7280 while (--extra >= 0)
7281 {
7282 if (*s == NUL)
7283 *s = '\n'; /* replace NUL with newline */
7284 ++s;
7285 }
7286 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 }
7289 y_ptr->y_type = type;
7290 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (type == MBLOCK)
7292 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7293 else
7294 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007295#ifdef FEAT_VIMINFO
7296 y_ptr->y_time_set = vim_time();
7297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7300
7301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 vim_memset(oap, 0, sizeof(oparg_T));
7305}
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007308 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 *
7310 * "Words" are counted by looking for boundaries between non-space and
7311 * space characters. (it seems to produce results that match 'wc'.)
7312 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 * Return value is byte count; word count for the line is added to "*wc".
7314 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 *
7316 * The function will only examine the first "limit" characters in the
7317 * line, stopping if it encounters an end-of-line (NUL byte). In that
7318 * case, eol_size will be added to the character count to account for
7319 * the size of the EOL character.
7320 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007321 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007322line_count_info(
7323 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007324 varnumber_T *wc,
7325 varnumber_T *cc,
7326 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007327 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007329 varnumber_T i;
7330 varnumber_T words = 0;
7331 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 int is_word = 0;
7333
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007334 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 if (is_word)
7337 {
7338 if (vim_isspace(line[i]))
7339 {
7340 words++;
7341 is_word = 0;
7342 }
7343 }
7344 else if (!vim_isspace(line[i]))
7345 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007346 ++chars;
7347#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007348 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007349#else
7350 ++i;
7351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
7353
7354 if (is_word)
7355 words++;
7356 *wc += words;
7357
7358 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007359 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007362 chars += eol_size;
7363 }
7364 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 return i;
7366}
7367
7368/*
7369 * Give some info about the position of the cursor (for "g CTRL-G").
7370 * In Visual mode, give some info about the selected region. (In this case,
7371 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007372 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 */
7374 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007375cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
7377 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007378 char_u buf1[50];
7379 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007381 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007382#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007383 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007384#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007385 varnumber_T byte_count_cursor = 0;
7386 varnumber_T char_count = 0;
7387 varnumber_T char_count_cursor = 0;
7388 varnumber_T word_count = 0;
7389 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007390 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007391 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 long line_count_selected = 0;
7393 pos_T min_pos, max_pos;
7394 oparg_T oparg;
7395 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
7397 /*
7398 * Compute the length of the file in characters.
7399 */
7400 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7401 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007402 if (dict == NULL)
7403 {
7404 MSG(_(no_lines_msg));
7405 return;
7406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 }
7408 else
7409 {
7410 if (get_fileformat(curbuf) == EOL_DOS)
7411 eol_size = 2;
7412 else
7413 eol_size = 1;
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (VIsual_active)
7416 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007417 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
7419 min_pos = VIsual;
7420 max_pos = curwin->w_cursor;
7421 }
7422 else
7423 {
7424 min_pos = curwin->w_cursor;
7425 max_pos = VIsual;
7426 }
7427 if (*p_sel == 'e' && max_pos.col > 0)
7428 --max_pos.col;
7429
7430 if (VIsual_mode == Ctrl_V)
7431 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007432#ifdef FEAT_LINEBREAK
7433 char_u * saved_sbr = p_sbr;
7434
7435 /* Make 'sbr' empty for a moment to get the correct size. */
7436 p_sbr = empty_option;
7437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 oparg.is_VIsual = 1;
7439 oparg.block_mode = TRUE;
7440 oparg.op_type = OP_NOP;
7441 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007442 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007443#ifdef FEAT_LINEBREAK
7444 p_sbr = saved_sbr;
7445#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007446 if (curwin->w_curswant == MAXCOL)
7447 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 /* Swap the start, end vcol if needed */
7449 if (oparg.end_vcol < oparg.start_vcol)
7450 {
7451 oparg.end_vcol += oparg.start_vcol;
7452 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7453 oparg.end_vcol -= oparg.start_vcol;
7454 }
7455 }
7456 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7460 {
7461 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007462 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
7464 ui_breakcheck();
7465 if (got_int)
7466 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007467 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 /* Do extra processing for VIsual mode. */
7471 if (VIsual_active
7472 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7473 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007474 char_u *s = NULL;
7475 long len = 0L;
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 switch (VIsual_mode)
7478 {
7479 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007484#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007486#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007487 s = bd.textstart;
7488 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 break;
7490 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007491 s = ml_get(lnum);
7492 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 break;
7494 case 'v':
7495 {
7496 colnr_T start_col = (lnum == min_pos.lnum)
7497 ? min_pos.col : 0;
7498 colnr_T end_col = (lnum == max_pos.lnum)
7499 ? max_pos.col - start_col + 1 : MAXCOL;
7500
Bram Moolenaardef9e822004-12-31 20:58:58 +00007501 s = ml_get(lnum) + start_col;
7502 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 break;
7505 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007506 if (s != NULL)
7507 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007508 byte_count_cursor += line_count_info(s, &word_count_cursor,
7509 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007510 if (lnum == curbuf->b_ml.ml_line_count
7511 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007512 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007513 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007514 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 {
7519 /* In non-visual mode, check for the line the cursor is on */
7520 if (lnum == curwin->w_cursor.lnum)
7521 {
7522 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007523 char_count_cursor += char_count;
7524 byte_count_cursor = byte_count +
7525 line_count_info(ml_get(lnum),
7526 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007527 (varnumber_T)(curwin->w_cursor.col + 1),
7528 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 }
7531 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007532 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007533 &char_count, (varnumber_T)MAXCOL,
7534 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536
7537 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007538 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007539 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaared767a22016-01-03 22:49:16 +01007541 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007543 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007545 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7546 {
7547 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7548 &max_pos.col);
7549 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7550 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7551 }
7552 else
7553 buf1[0] = NUL;
7554
7555 if (char_count_cursor == byte_count_cursor
7556 && char_count == byte_count)
7557 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007558 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007559 buf1, line_count_selected,
7560 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007561 (long long)word_count_cursor,
7562 (long long)word_count,
7563 (long long)byte_count_cursor,
7564 (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007565 else
7566 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007567 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007568 buf1, line_count_selected,
7569 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007570 (long long)word_count_cursor,
7571 (long long)word_count,
7572 (long long)char_count_cursor,
7573 (long long)char_count,
7574 (long long)byte_count_cursor,
7575 (long long)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007578 {
7579 p = ml_get_curline();
7580 validate_virtcol();
7581 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7582 (int)curwin->w_virtcol + 1);
7583 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7584 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
Bram Moolenaared767a22016-01-03 22:49:16 +01007586 if (char_count_cursor == byte_count_cursor
7587 && char_count == byte_count)
7588 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007589 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007590 (char *)buf1, (char *)buf2,
7591 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007593 (long long)word_count_cursor, (long long)word_count,
7594 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007595 else
7596 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007597 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007598 (char *)buf1, (char *)buf2,
7599 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007600 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarea391762018-04-08 13:07:22 +02007601 (long long)word_count_cursor, (long long)word_count,
7602 (long long)char_count_cursor, (long long)char_count,
7603 (long long)byte_count_cursor, (long long)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007604 }
7605 }
7606
Bram Moolenaared767a22016-01-03 22:49:16 +01007607#ifdef FEAT_MBYTE
7608 bom_count = bomb_size();
7609 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007610 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar672afb92018-04-08 16:34:22 +02007611 _("(+%lld for BOM)"), (long long)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007612#endif
7613 if (dict == NULL)
7614 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007615 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007616 p = p_shm;
7617 p_shm = (char_u *)"";
7618 msg(IObuff);
7619 p_shm = p;
7620 }
7621 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007622#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007623 if (dict != NULL)
7624 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007625 dict_add_number(dict, "words", word_count);
7626 dict_add_number(dict, "chars", char_count);
7627 dict_add_number(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007628# ifdef FEAT_MBYTE
7629 + bom_count
7630# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02007631 );
7632 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7633 byte_count_cursor);
7634 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7635 char_count_cursor);
7636 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7637 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}