blob: e93119049a2a90b91f2da3d8e57ab487b10086f4 [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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100100static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void free_yank_all(void);
102static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200104static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100105static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100107static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100108static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
109static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200111static 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 +0000112#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100113static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#endif
119
Bram Moolenaarf2732452018-06-03 14:47:35 +0200120// Flags for third item in "opchars".
121#define OPF_LINES 1 // operator always works on lines
122#define OPF_CHANGE 2 // operator changes text
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124/*
125 * The names of operators.
126 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +0200127 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
129static char opchars[][3] =
130{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200131 {NUL, NUL, 0}, // OP_NOP
132 {'d', NUL, OPF_CHANGE}, // OP_DELETE
133 {'y', NUL, 0}, // OP_YANK
134 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
135 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
136 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
137 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
138 {'g', '~', OPF_CHANGE}, // OP_TILDE
139 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
140 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
141 {':', NUL, OPF_LINES}, // OP_COLON
142 {'g', 'U', OPF_CHANGE}, // OP_UPPER
143 {'g', 'u', OPF_CHANGE}, // OP_LOWER
144 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
145 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
146 {'g', '?', OPF_CHANGE}, // OP_ROT13
147 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
148 {'I', NUL, OPF_CHANGE}, // OP_INSERT
149 {'A', NUL, OPF_CHANGE}, // OP_APPEND
150 {'z', 'f', OPF_LINES}, // OP_FOLD
151 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
152 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
153 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
154 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
155 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
156 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
157 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
158 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
159 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
160 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161};
162
163/*
164 * Translate a command name into an operator type.
165 * Must only be called with a valid operator name!
166 */
167 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100168get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
170 int i;
171
172 if (char1 == 'r') /* ignore second character */
173 return OP_REPLACE;
174 if (char1 == '~') /* when tilde is an operator */
175 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100176 if (char1 == 'g' && char2 == Ctrl_A) /* add */
177 return OP_NR_ADD;
178 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
179 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (opchars[i][0] == char1 && opchars[i][1] == char2)
183 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100184 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
185 {
186 internal_error("get_op_type()");
187 break;
188 }
189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 return i;
191}
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193/*
194 * Return TRUE if operator "op" always works on whole lines.
195 */
196 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100197op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200199 return opchars[op][2] & OPF_LINES;
200}
201
Bram Moolenaar113e1072019-01-20 15:30:40 +0100202#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200203/*
204 * Return TRUE if operator "op" changes text.
205 */
206 int
207op_is_change(int op)
208{
209 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100211#endif
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 Moolenaar32526b32019-01-19 17:43:09 +0100305 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
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 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100423 {
424 if ((*mb_ptr2len)(bd.textstart) == 1)
425 ++bd.textstart;
426 else
427 {
428 ws_vcol = 0;
429 bd.startspaces = 0;
430 }
431 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000432 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000433 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100435 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200437 /* TODO: is passing bd.textstart for start of the line OK? */
438 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
439 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 total += incr;
441 bd.start_vcol += incr;
442 }
443 /* OK, now total=all the VWS reqd, and textstart points at the 1st
444 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200445#ifdef FEAT_VARTABS
446 if (!curbuf->b_p_et)
447 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
448 else
449 j = total;
450#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (!curbuf->b_p_et)
452 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
453 if (i)
454 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
455 else
456 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 /* if we're splitting a TAB, allow for it */
459 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
460 len = (int)STRLEN(bd.textstart) + 1;
461 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
462 if (newp == NULL)
463 return;
464 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
465 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200466 vim_memset(newp + bd.textcol, TAB, (size_t)i);
467 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 /* the end */
469 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
470 }
471 else /* left */
472 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000473 colnr_T destination_col; /* column to which text in block will
474 be shifted */
475 char_u *verbatim_copy_end; /* end of the part of the line which is
476 copied verbatim */
477 colnr_T verbatim_copy_width;/* the (displayed) width of this part
478 of line */
479 unsigned fill; /* nr of spaces that replace a TAB */
480 unsigned new_line_len; /* the length of the line after the
481 block shift */
482 size_t block_space_width;
483 size_t shift_amount;
484 char_u *non_white = bd.textstart;
485 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000487 /*
488 * Firstly, let's find the first non-whitespace character that is
489 * displayed after the block's start column and the character's column
490 * number. Also, let's calculate the width of all the whitespace
491 * characters that are displayed in the block and precede the searched
492 * non-whitespace character.
493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000495 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
496 * the part of which is displayed at the block's beginning. Let's start
497 * searching from the next character. */
498 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100499 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000500
501 /* The character's column is in "bd.start_vcol". */
502 non_white_col = bd.start_vcol;
503
Bram Moolenaar1c465442017-03-12 20:10:05 +0100504 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000505 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000507 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 }
509
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000510 block_space_width = non_white_col - oap->start_vcol;
511 /* We will shift by "total" or "block_space_width", whichever is less.
512 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000513 shift_amount = (block_space_width < (size_t)total
514 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000515
516 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000517 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000518
519 /* Now let's find out how much of the beginning of the line we can
520 * reuse without modification. */
521 verbatim_copy_end = bd.textstart;
522 verbatim_copy_width = bd.start_vcol;
523
524 /* If "bd.startspaces" is set, "bd.textstart" points to the character
525 * preceding the block. We have to subtract its width to obtain its
526 * column number. */
527 if (bd.startspaces)
528 verbatim_copy_width -= bd.start_char_vcols;
529 while (verbatim_copy_width < destination_col)
530 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200531 char_u *line = verbatim_copy_end;
532
533 /* TODO: is passing verbatim_copy_end for start of the line OK? */
534 incr = lbr_chartabsize(line, verbatim_copy_end,
535 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000536 if (verbatim_copy_width + incr > destination_col)
537 break;
538 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100539 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000540 }
541
542 /* If "destination_col" is different from the width of the initial
543 * part of the line that will be copied, it means we encountered a tab
544 * character, which we will have to partly replace with spaces. */
545 fill = destination_col - verbatim_copy_width;
546
547 /* The replacement line will consist of:
548 * - the beginning of the original line up to "verbatim_copy_end",
549 * - "fill" number of spaces,
550 * - the rest of the line, pointed to by non_white. */
551 new_line_len = (unsigned)(verbatim_copy_end - oldp)
552 + fill
553 + (unsigned)STRLEN(non_white) + 1;
554
555 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if (newp == NULL)
557 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000558 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200559 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000560 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
562 /* replace the line */
563 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
564 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
565 State = oldstate;
566 curwin->w_cursor.col = oldcol;
567#ifdef FEAT_RIGHTLEFT
568 p_ri = old_p_ri;
569#endif
570}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572/*
573 * Insert string "s" (b_insert ? before : after) block :AKelly
574 * Caller must prepare for undo.
575 */
576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100577block_insert(
578 oparg_T *oap,
579 char_u *s,
580 int b_insert,
581 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 int p_ts;
584 int count = 0; /* extra spaces to replace a cut TAB */
585 int spaces = 0; /* non-zero if cutting a TAB */
586 colnr_T offset; /* pointer along new line */
587 unsigned s_len; /* STRLEN(s) */
588 char_u *newp, *oldp; /* new, old lines */
589 linenr_T lnum; /* loop var */
590 int oldstate = State;
591
592 State = INSERT; /* don't want REPLACE for State */
593 s_len = (unsigned)STRLEN(s);
594
595 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
596 {
597 block_prep(oap, bdp, lnum, TRUE);
598 if (bdp->is_short && b_insert)
599 continue; /* OP_INSERT, line ends before block start */
600
601 oldp = ml_get(lnum);
602
603 if (b_insert)
604 {
605 p_ts = bdp->start_char_vcols;
606 spaces = bdp->startspaces;
607 if (spaces != 0)
608 count = p_ts - 1; /* we're cutting a TAB */
609 offset = bdp->textcol;
610 }
611 else /* append */
612 {
613 p_ts = bdp->end_char_vcols;
614 if (!bdp->is_short) /* spaces = padding after block */
615 {
616 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
617 if (spaces != 0)
618 count = p_ts - 1; /* we're cutting a TAB */
619 offset = bdp->textcol + bdp->textlen - (spaces != 0);
620 }
621 else /* spaces = padding to block edge */
622 {
623 /* if $ used, just append to EOL (ie spaces==0) */
624 if (!bdp->is_MAX)
625 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
626 count = spaces;
627 offset = bdp->textcol + bdp->textlen;
628 }
629 }
630
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 if (has_mbyte && spaces > 0)
632 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100633 int off;
634
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635 /* Avoid starting halfway a multi-byte character. */
636 if (b_insert)
637 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100638 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200639 }
640 else
641 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100642 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200643 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200644 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100645 spaces -= off;
646 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200647 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
650 if (newp == NULL)
651 continue;
652
653 /* copy up to shifted part */
654 mch_memmove(newp, oldp, (size_t)(offset));
655 oldp += offset;
656
657 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200658 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660 /* copy the new text */
661 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
662 offset += s_len;
663
664 if (spaces && !bdp->is_short)
665 {
666 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200667 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /* We're splitting a TAB, don't copy it. */
669 oldp++;
670 /* We allowed for that TAB, remember this now */
671 count++;
672 }
673
674 if (spaces > 0)
675 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000676 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 ml_replace(lnum, newp, FALSE);
679
680 if (lnum == oap->end.lnum)
681 {
682 /* Set "']" mark to the end of the block instead of the end of
683 * the insert in the first line. */
684 curbuf->b_op_end.lnum = oap->end.lnum;
685 curbuf->b_op_end.col = offset;
686 }
687 } /* for all lnum */
688
689 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
690
691 State = oldstate;
692}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
695/*
696 * op_reindent - handle reindenting a block of lines.
697 */
698 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100699op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
701 long i;
702 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200703 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 linenr_T first_changed = 0;
705 linenr_T last_changed = 0;
706 linenr_T start_lnum = curwin->w_cursor.lnum;
707
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000708 /* Don't even try when 'modifiable' is off. */
709 if (!curbuf->b_p_ma)
710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100711 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000712 return;
713 }
714
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 for (i = oap->line_count; --i >= 0 && !got_int; )
716 {
717 /* it's a slow thing to do, so give feedback so there's no worry that
718 * the computer's just hung. */
719
720 if (i > 1
721 && (i % 50 == 0 || i == oap->line_count - 1)
722 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 /*
726 * Be vi-compatible: For lisp indenting the first line is not
727 * indented, unless there is only one line.
728 */
729#ifdef FEAT_LISP
730 if (i != oap->line_count - 1 || oap->line_count == 1
731 || how != get_lisp_indent)
732#endif
733 {
734 l = skipwhite(ml_get_curline());
735 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200736 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200738 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200740 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
742 /* did change the indent, call changed_lines() later */
743 if (first_changed == 0)
744 first_changed = curwin->w_cursor.lnum;
745 last_changed = curwin->w_cursor.lnum;
746 }
747 }
748 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000749 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751
752 /* put cursor on first non-blank of indented line */
753 curwin->w_cursor.lnum = start_lnum;
754 beginline(BL_SOL | BL_FIX);
755
756 /* Mark changed lines so that they will be redrawn. When Visual
757 * highlighting was present, need to continue until the last line. When
758 * there is no change still need to remove the Visual highlighting. */
759 if (last_changed != 0)
760 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else if (oap->is_VIsual)
764 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 if (oap->line_count > p_report)
767 {
768 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100769 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200770 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 /* set '[ and '] marks */
773 curbuf->b_op_start = oap->start;
774 curbuf->b_op_end = oap->end;
775}
776#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
777
778#if defined(FEAT_EVAL) || defined(PROTO)
779/*
780 * Keep the last expression line here, for repeating.
781 */
782static char_u *expr_line = NULL;
783
784/*
785 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
786 * Returns '=' when OK, NUL otherwise.
787 */
788 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100789get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 char_u *new_line;
792
793 new_line = getcmdline('=', 0L, 0);
794 if (new_line == NULL)
795 return NUL;
796 if (*new_line == NUL) /* use previous line */
797 vim_free(new_line);
798 else
799 set_expr_line(new_line);
800 return '=';
801}
802
803/*
804 * Set the expression for the '=' register.
805 * Argument must be an allocated string.
806 */
807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100808set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809{
810 vim_free(expr_line);
811 expr_line = new_line;
812}
813
814/*
815 * Get the result of the '=' register expression.
816 * Returns a pointer to allocated memory, or NULL for failure.
817 */
818 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100819get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 char_u *expr_copy;
822 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000823 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 if (expr_line == NULL)
826 return NULL;
827
828 /* Make a copy of the expression, because evaluating it may cause it to be
829 * changed. */
830 expr_copy = vim_strsave(expr_line);
831 if (expr_copy == NULL)
832 return NULL;
833
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000834 /* When we are invoked recursively limit the evaluation to 10 levels.
835 * Then return the string as-is. */
836 if (nested >= 10)
837 return expr_copy;
838
839 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000840 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000841 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 vim_free(expr_copy);
843 return rv;
844}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000845
846/*
847 * Get the '=' register expression itself, without evaluating it.
848 */
849 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100850get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000851{
852 if (expr_line == NULL)
853 return NULL;
854 return vim_strsave(expr_line);
855}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif /* FEAT_EVAL */
857
858/*
859 * Check if 'regname' is a valid name of a yank register.
860 * Note: There is no check for 0 (default register), caller should do this
861 */
862 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100863valid_yank_reg(
864 int regname,
865 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867 if ( (regname > 0 && ASCII_ISALNUM(regname))
868 || (!writing && vim_strchr((char_u *)
869#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100870 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100872 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#endif
874 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100875 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 || regname == '"'
877 || regname == '-'
878 || regname == '_'
879#ifdef FEAT_CLIPBOARD
880 || regname == '*'
881 || regname == '+'
882#endif
883#ifdef FEAT_DND
884 || (!writing && regname == '~')
885#endif
886 )
887 return TRUE;
888 return FALSE;
889}
890
891/*
892 * Set y_current and y_append, according to the value of "regname".
893 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000894 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 *
896 * If regname is 0 and writing, use register 0
897 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100898 *
899 * Return TRUE when the register should be inserted literally (selection or
900 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100902 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100903get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100906 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 y_append = FALSE;
909 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
910 {
911 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100912 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 i = regname;
915 if (VIM_ISDIGIT(i))
916 i -= '0';
917 else if (ASCII_ISLOWER(i))
918 i = CharOrdLow(i) + 10;
919 else if (ASCII_ISUPPER(i))
920 {
921 i = CharOrdUp(i) + 10;
922 y_append = TRUE;
923 }
924 else if (regname == '-')
925 i = DELETION_REGISTER;
926#ifdef FEAT_CLIPBOARD
927 /* When selection is not available, use register 0 instead of '*' */
928 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100931 ret = TRUE;
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 /* When clipboard is not available, use register 0 instead of '+' */
934 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100937 ret = TRUE;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940#ifdef FEAT_DND
941 else if (!writing && regname == '~')
942 i = TILDE_REGISTER;
943#endif
944 else /* not 0-9, a-z, A-Z or '-': use register 0 */
945 i = 0;
946 y_current = &(y_regs[i]);
947 if (writing) /* remember the register we write into for do_put() */
948 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100949 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
Bram Moolenaar8299df92004-07-10 09:47:34 +0000952#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953/*
954 * When "regname" is a clipboard register, obtain the selection. If it's not
955 * available return zero, otherwise return "regname".
956 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100958may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959{
960 if (regname == '*')
961 {
962 if (!clip_star.available)
963 regname = 0;
964 else
965 clip_get_selection(&clip_star);
966 }
967 else if (regname == '+')
968 {
969 if (!clip_plus.available)
970 regname = 0;
971 else
972 clip_get_selection(&clip_plus);
973 }
974 return regname;
975}
976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978/*
979 * Obtain the contents of a "normal" register. The register is made empty.
980 * The returned pointer has allocated memory, use put_register() later.
981 */
982 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100983get_register(
984 int name,
985 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200987 yankreg_T *reg;
988 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990#ifdef FEAT_CLIPBOARD
991 /* When Visual area changed, may have to update selection. Obtain the
992 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000993 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200995 if (clip_isautosel_star())
996 clip_update_selection(&clip_star);
997 may_get_selection(name);
998 }
999 if (name == '+' && clip_plus.available)
1000 {
1001 if (clip_isautosel_plus())
1002 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 may_get_selection(name);
1004 }
1005#endif
1006
1007 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001008 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (reg != NULL)
1010 {
1011 *reg = *y_current;
1012 if (copy)
1013 {
1014 /* If we run out of memory some or all of the lines are empty. */
1015 if (reg->y_size == 0)
1016 reg->y_array = NULL;
1017 else
1018 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1019 * reg->y_size));
1020 if (reg->y_array != NULL)
1021 {
1022 for (i = 0; i < reg->y_size; ++i)
1023 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1024 }
1025 }
1026 else
1027 y_current->y_array = NULL;
1028 }
1029 return (void *)reg;
1030}
1031
1032/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001033 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 */
1035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001036put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 get_yank_register(name, 0);
1039 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001040 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001041 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001043#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 /* Send text written to clipboard register to the clipboard. */
1045 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001048
Bram Moolenaar113e1072019-01-20 15:30:40 +01001049#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1050 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001051 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001053{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001054 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001055
1056 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001057 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001058 free_yank_all();
1059 vim_free(reg);
1060 *y_current = tmp;
1061}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064#if defined(FEAT_MOUSE) || defined(PROTO)
1065/*
1066 * return TRUE if the current yank register has type MLINE
1067 */
1068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
1071 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1072 return FALSE;
1073 if (regname == '_') /* black hole is always empty */
1074 return FALSE;
1075 get_yank_register(regname, FALSE);
1076 return (y_current->y_type == MLINE);
1077}
1078#endif
1079
1080/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001081 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001083 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 */
1085 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001086do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaard55de222007-05-06 13:38:48 +00001088 char_u *p;
1089 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001090 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001091 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001093 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001095 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1097 retval = FAIL;
1098 else
1099 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001100 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 showmode();
1102 regname = c;
1103 retval = OK;
1104 }
1105 }
1106 else /* stop recording */
1107 {
1108 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001109 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1110 * needs to be removed again to put it in a register. exec_reg then
1111 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001113 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001114 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 p = get_recorded();
1116 if (p == NULL)
1117 retval = FAIL;
1118 else
1119 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001120 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1121 vim_unescape_csi(p);
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 /*
1124 * We don't want to change the default register here, so save and
1125 * restore the current register name.
1126 */
1127 old_y_previous = y_previous;
1128 old_y_current = y_current;
1129
1130 retval = stuff_yank(regname, p);
1131
1132 y_previous = old_y_previous;
1133 y_current = old_y_current;
1134 }
1135 }
1136 return retval;
1137}
1138
1139/*
1140 * Stuff string "p" into yank register "regname" as a single line (append if
1141 * uppercase). "p" must have been alloced.
1142 *
1143 * return FAIL for failure, OK otherwise
1144 */
1145 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001146stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
1148 char_u *lp;
1149 char_u **pp;
1150
1151 /* check for read-only register */
1152 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1153 {
1154 vim_free(p);
1155 return FAIL;
1156 }
1157 if (regname == '_') /* black hole: don't do anything */
1158 {
1159 vim_free(p);
1160 return OK;
1161 }
1162 get_yank_register(regname, TRUE);
1163 if (y_append && y_current->y_array != NULL)
1164 {
1165 pp = &(y_current->y_array[y_current->y_size - 1]);
1166 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1167 if (lp == NULL)
1168 {
1169 vim_free(p);
1170 return FAIL;
1171 }
1172 STRCPY(lp, *pp);
1173 STRCAT(lp, p);
1174 vim_free(p);
1175 vim_free(*pp);
1176 *pp = lp;
1177 }
1178 else
1179 {
1180 free_yank_all();
1181 if ((y_current->y_array =
1182 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1183 {
1184 vim_free(p);
1185 return FAIL;
1186 }
1187 y_current->y_array[0] = p;
1188 y_current->y_size = 1;
1189 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001190#ifdef FEAT_VIMINFO
1191 y_current->y_time_set = vim_time();
1192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 return OK;
1195}
1196
Bram Moolenaar42b94362009-05-26 16:12:37 +00001197static int execreg_lastc = NUL;
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001200 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001202 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001205do_execreg(
1206 int regname,
1207 int colon, /* insert ':' before each line */
1208 int addcr, /* always add '\n' to end of line */
1209 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 long i;
1212 char_u *p;
1213 int retval = OK;
1214 int remap;
1215
1216 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001217 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001218 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001221 return FAIL;
1222 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001223 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 /* check for valid regname */
1226 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001227 {
1228 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001230 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001231 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233#ifdef FEAT_CLIPBOARD
1234 regname = may_get_selection(regname);
1235#endif
1236
1237 if (regname == '_') /* black hole: don't stuff anything */
1238 return OK;
1239
1240#ifdef FEAT_CMDHIST
1241 if (regname == ':') /* use last command line */
1242 {
1243 if (last_cmdline == NULL)
1244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001245 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 return FAIL;
1247 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001248 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001249 /* Escape all control characters with a CTRL-V */
1250 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001251 (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 +00001252 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001253 {
1254 /* When in Visual mode "'<,'>" will be prepended to the command.
1255 * Remove it when it's already there. */
1256 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001257 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001258 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001259 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001260 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001261 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263#endif
1264#ifdef FEAT_EVAL
1265 else if (regname == '=')
1266 {
1267 p = get_expr_line();
1268 if (p == NULL)
1269 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001270 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 vim_free(p);
1272 }
1273#endif
1274 else if (regname == '.') /* use last inserted text */
1275 {
1276 p = get_last_insert_save();
1277 if (p == NULL)
1278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001279 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 return FAIL;
1281 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 vim_free(p);
1284 }
1285 else
1286 {
1287 get_yank_register(regname, FALSE);
1288 if (y_current->y_array == NULL)
1289 return FAIL;
1290
1291 /* Disallow remaping for ":@r". */
1292 remap = colon ? REMAP_NONE : REMAP_YES;
1293
1294 /*
1295 * Insert lines into typeahead buffer, from last one to first one.
1296 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001297 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 for (i = y_current->y_size; --i >= 0; )
1299 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001300 char_u *escaped;
1301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 /* insert NL between lines and after last line if type is MLINE */
1303 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1304 || addcr)
1305 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001306 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 return FAIL;
1308 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001309 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1310 if (escaped == NULL)
1311 return FAIL;
1312 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1313 vim_free(escaped);
1314 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001316 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 == FAIL)
1318 return FAIL;
1319 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001320 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 return retval;
1323}
1324
1325/*
1326 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1327 * used only after other typeahead has been processed.
1328 */
1329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001330put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 char_u buf[3];
1333
1334 if (restart_edit != NUL)
1335 {
1336 if (restart_edit == 'V')
1337 {
1338 buf[0] = 'g';
1339 buf[1] = 'R';
1340 buf[2] = NUL;
1341 }
1342 else
1343 {
1344 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1345 buf[1] = NUL;
1346 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001347 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 restart_edit = NUL;
1349 }
1350}
1351
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001352/*
1353 * Insert register contents "s" into the typeahead buffer, so that it will be
1354 * executed again.
1355 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1356 * no remapping.
1357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001359put_in_typebuf(
1360 char_u *s,
1361 int esc,
1362 int colon, /* add ':' before the line */
1363 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 int retval = OK;
1366
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001367 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001369 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001371 {
1372 char_u *p;
1373
1374 if (esc)
1375 p = vim_strsave_escape_csi(s);
1376 else
1377 p = s;
1378 if (p == NULL)
1379 retval = FAIL;
1380 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001381 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1382 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001383 if (esc)
1384 vim_free(p);
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001387 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 return retval;
1389}
1390
1391/*
1392 * Insert a yank register: copy it into the Read buffer.
1393 * Used by CTRL-R command and middle mouse button in insert mode.
1394 *
1395 * return FAIL for failure, OK otherwise
1396 */
1397 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001398insert_reg(
1399 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001400 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 long i;
1403 int retval = OK;
1404 char_u *arg;
1405 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001406 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 /*
1409 * It is possible to get into an endless loop by having CTRL-R a in
1410 * register a and then, in insert mode, doing CTRL-R a.
1411 * If you hit CTRL-C, the loop will be broken here.
1412 */
1413 ui_breakcheck();
1414 if (got_int)
1415 return FAIL;
1416
1417 /* check for valid regname */
1418 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1419 return FAIL;
1420
1421#ifdef FEAT_CLIPBOARD
1422 regname = may_get_selection(regname);
1423#endif
1424
1425 if (regname == '.') /* insert last inserted text */
1426 retval = stuff_inserted(NUL, 1L, TRUE);
1427 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1428 {
1429 if (arg == NULL)
1430 return FAIL;
1431 stuffescaped(arg, literally);
1432 if (allocated)
1433 vim_free(arg);
1434 }
1435 else /* name or number register */
1436 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001437 if (get_yank_register(regname, FALSE))
1438 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (y_current->y_array == NULL)
1440 retval = FAIL;
1441 else
1442 {
1443 for (i = 0; i < y_current->y_size; ++i)
1444 {
1445 stuffescaped(y_current->y_array[i], literally);
1446 /*
1447 * Insert a newline between lines and after last line if
1448 * y_type is MLINE.
1449 */
1450 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1451 stuffcharReadbuff('\n');
1452 }
1453 }
1454 }
1455
1456 return retval;
1457}
1458
1459/*
1460 * Stuff a string into the typeahead buffer, such that edit() will insert it
1461 * literally ("literally" TRUE) or interpret is as typed characters.
1462 */
1463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001464stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 int c;
1467 char_u *start;
1468
1469 while (*arg != NUL)
1470 {
1471 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1472 * stuff K_SPECIAL to get the effect of a special key when "literally"
1473 * is TRUE. */
1474 start = arg;
1475 while ((*arg >= ' '
1476#ifndef EBCDIC
1477 && *arg < DEL /* EBCDIC: chars above space are normal */
1478#endif
1479 )
1480 || (*arg == K_SPECIAL && !literally))
1481 ++arg;
1482 if (arg > start)
1483 stuffReadbuffLen(start, (long)(arg - start));
1484
1485 /* stuff a single special character */
1486 if (*arg != NUL)
1487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001489 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 c = *arg++;
1492 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1493 stuffcharReadbuff(Ctrl_V);
1494 stuffcharReadbuff(c);
1495 }
1496 }
1497}
1498
1499/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001500 * If "regname" is a special register, return TRUE and store a pointer to its
1501 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504get_spec_reg(
1505 int regname,
1506 char_u **argp,
1507 int *allocated, /* return: TRUE when value was allocated */
1508 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int cnt;
1511
1512 *argp = NULL;
1513 *allocated = FALSE;
1514 switch (regname)
1515 {
1516 case '%': /* file name */
1517 if (errmsg)
1518 check_fname(); /* will give emsg if not set */
1519 *argp = curbuf->b_fname;
1520 return TRUE;
1521
1522 case '#': /* alternate file name */
1523 *argp = getaltfname(errmsg); /* may give emsg if not set */
1524 return TRUE;
1525
1526#ifdef FEAT_EVAL
1527 case '=': /* result of expression */
1528 *argp = get_expr_line();
1529 *allocated = TRUE;
1530 return TRUE;
1531#endif
1532
1533 case ':': /* last command line */
1534 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001535 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 *argp = last_cmdline;
1537 return TRUE;
1538
1539 case '/': /* last search-pattern */
1540 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001541 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 *argp = last_search_pat();
1543 return TRUE;
1544
1545 case '.': /* last inserted text */
1546 *argp = get_last_insert_save();
1547 *allocated = TRUE;
1548 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 return TRUE;
1551
1552#ifdef FEAT_SEARCHPATH
1553 case Ctrl_F: /* Filename under cursor */
1554 case Ctrl_P: /* Path under cursor, expand via "path" */
1555 if (!errmsg)
1556 return FALSE;
1557 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001558 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 *allocated = TRUE;
1560 return TRUE;
1561#endif
1562
1563 case Ctrl_W: /* word under cursor */
1564 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1565 if (!errmsg)
1566 return FALSE;
1567 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1568 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1569 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1570 *allocated = TRUE;
1571 return TRUE;
1572
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001573 case Ctrl_L: /* Line under cursor */
1574 if (!errmsg)
1575 return FALSE;
1576
1577 *argp = ml_get_buf(curwin->w_buffer,
1578 curwin->w_cursor.lnum, FALSE);
1579 return TRUE;
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 case '_': /* black hole: always empty */
1582 *argp = (char_u *)"";
1583 return TRUE;
1584 }
1585
1586 return FALSE;
1587}
1588
1589/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001590 * Paste a yank register into the command line.
1591 * Only for non-special registers.
1592 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 * insert_reg() can't be used here, because special characters from the
1594 * register contents will be interpreted as commands.
1595 *
1596 * return FAIL for failure, OK otherwise
1597 */
1598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001599cmdline_paste_reg(
1600 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001601 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001605 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001607 if (get_yank_register(regname, FALSE))
1608 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (y_current->y_array == NULL)
1610 return FAIL;
1611
1612 for (i = 0; i < y_current->y_size; ++i)
1613 {
1614 cmdline_paste_str(y_current->y_array[i], literally);
1615
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001616 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001617 * Don't do this when "remcr" is TRUE. */
1618 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 cmdline_paste_str((char_u *)"\r", literally);
1620
1621 /* Check for CTRL-C, in case someone tries to paste a few thousand
1622 * lines and gets bored. */
1623 ui_breakcheck();
1624 if (got_int)
1625 return FAIL;
1626 }
1627 return OK;
1628}
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1631/*
1632 * Adjust the register name pointed to with "rp" for the clipboard being
1633 * used always and the clipboard being available.
1634 */
1635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001636adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001638 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1639 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001640 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1641 {
1642 if (clip_unnamed != 0)
1643 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001644 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001645 else
1646 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1647 ? '+' : '*';
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (!clip_star.available && *rp == '*')
1650 *rp = 0;
1651 if (!clip_plus.available && *rp == '+')
1652 *rp = 0;
1653}
1654#endif
1655
1656/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001657 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1658 */
1659 void
1660shift_delete_registers()
1661{
1662 int n;
1663
1664 y_current = &y_regs[9];
1665 free_yank_all(); /* free register nine */
1666 for (n = 9; n > 1; --n)
1667 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001668 y_current = &y_regs[1];
1669 if (!y_append)
1670 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001671 y_regs[1].y_array = NULL; /* set register one to empty */
1672}
1673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001675 static void
1676yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1677{
1678 static int recursive = FALSE;
1679 dict_T *v_event;
1680 list_T *list;
1681 int n;
1682 char_u buf[NUMBUFLEN + 2];
1683 long reglen = 0;
1684
1685 if (recursive)
1686 return;
1687
1688 v_event = get_vim_var_dict(VV_EVENT);
1689
1690 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001691 if (list == NULL)
1692 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001693 for (n = 0; n < reg->y_size; n++)
1694 list_append_string(list, reg->y_array[n], -1);
1695 list->lv_lock = VAR_FIXED;
1696 dict_add_list(v_event, "regcontents", list);
1697
1698 buf[0] = (char_u)oap->regname;
1699 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001700 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001701
1702 buf[0] = get_op_char(oap->op_type);
1703 buf[1] = get_extra_op_char(oap->op_type);
1704 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001705 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001706
1707 buf[0] = NUL;
1708 buf[1] = NUL;
1709 switch (get_reg_type(oap->regname, &reglen))
1710 {
1711 case MLINE: buf[0] = 'V'; break;
1712 case MCHAR: buf[0] = 'v'; break;
1713 case MBLOCK:
1714 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1715 reglen + 1);
1716 break;
1717 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001718 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001719
1720 /* Lock the dictionary and its keys */
1721 dict_set_items_ro(v_event);
1722
1723 recursive = TRUE;
1724 textlock++;
1725 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1726 textlock--;
1727 recursive = FALSE;
1728
1729 /* Empty the dictionary, v:event is still valid */
1730 dict_free_contents(v_event);
1731 hash_init(&v_event->dv_hashtab);
1732}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001733#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001734
Bram Moolenaara1891842017-02-04 21:34:31 +01001735/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001736 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001738 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 */
1740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001741op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 int n;
1744 linenr_T lnum;
1745 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 char_u *newp, *oldp;
1747 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1749 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001750 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1753 return OK;
1754
1755 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1756 if (oap->empty)
1757 return u_save_cursor();
1758
1759 if (!curbuf->b_p_ma)
1760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 return FAIL;
1763 }
1764
1765#ifdef FEAT_CLIPBOARD
1766 adjust_clip_reg(&oap->regname);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (has_mbyte)
1770 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaard04b7502010-07-08 22:27:55 +02001772 /*
1773 * Imitate the strange Vi behaviour: If the delete spans more than one
1774 * line and motion_type == MCHAR and the result is a blank line, make the
1775 * delete linewise. Don't do this for the change command or Visual mode.
1776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001779 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001781 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 && oap->op_type == OP_DELETE)
1783 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001784 ptr = ml_get(oap->end.lnum) + oap->end.col;
1785 if (*ptr != NUL)
1786 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 ptr = skipwhite(ptr);
1788 if (*ptr == NUL && inindent(0))
1789 oap->motion_type = MLINE;
1790 }
1791
Bram Moolenaard04b7502010-07-08 22:27:55 +02001792 /*
1793 * Check for trying to delete (e.g. "D") in an empty line.
1794 * Note: For the change operator it is ok.
1795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if ( oap->motion_type == MCHAR
1797 && oap->line_count == 1
1798 && oap->op_type == OP_DELETE
1799 && *ml_get(oap->start.lnum) == NUL)
1800 {
1801 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001802 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * 'cpoptions' (Vi compatible).
1804 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001805#ifdef FEAT_VIRTUALEDIT
1806 if (virtual_op)
1807 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1808 * marks as if it happened. */
1809 goto setmarks;
1810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1812 beep_flush();
1813 return OK;
1814 }
1815
Bram Moolenaard04b7502010-07-08 22:27:55 +02001816 /*
1817 * Do a yank of whatever we're about to delete.
1818 * If a yank register was specified, put the deleted text into that
1819 * register. For the black hole register '_' don't yank anything.
1820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (oap->regname != '_')
1822 {
1823 if (oap->regname != 0)
1824 {
1825 /* check for read-only register */
1826 if (!valid_yank_reg(oap->regname, TRUE))
1827 {
1828 beep_flush();
1829 return OK;
1830 }
1831 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1832 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1833 did_yank = TRUE;
1834 }
1835
1836 /*
1837 * Put deleted text into register 1 and shift number registers if the
1838 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001839 * Use the register name from before adjust_clip_reg() may have
1840 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001842 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 || oap->line_count > 1 || oap->use_reg_one)
1844 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001845 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (op_yank(oap, TRUE, FALSE) == OK)
1847 did_yank = TRUE;
1848 }
1849
Bram Moolenaar84298db2012-04-20 13:46:08 +02001850 /* Yank into small delete register when no named register specified
1851 * and the delete is within one line. */
1852 if ((
1853#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001854 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1855 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001856#endif
1857 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 && oap->line_count == 1)
1859 {
1860 oap->regname = '-';
1861 get_yank_register(oap->regname, TRUE);
1862 if (op_yank(oap, TRUE, FALSE) == OK)
1863 did_yank = TRUE;
1864 oap->regname = 0;
1865 }
1866
1867 /*
1868 * If there's too much stuff to fit in the yank register, then get a
1869 * confirmation before doing the delete. This is crude, but simple.
1870 * And it avoids doing a delete of something we can't put back if we
1871 * want.
1872 */
1873 if (!did_yank)
1874 {
1875 int msg_silent_save = msg_silent;
1876
1877 msg_silent = 0; /* must display the prompt */
1878 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1879 msg_silent = msg_silent_save;
1880 if (n != 'y')
1881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 return FAIL;
1884 }
1885 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001886
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001887#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001888 if (did_yank && has_textyankpost())
1889 yank_do_autocmd(oap, y_current);
1890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892
Bram Moolenaard04b7502010-07-08 22:27:55 +02001893 /*
1894 * block mode delete
1895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (oap->block_mode)
1897 {
1898 if (u_save((linenr_T)(oap->start.lnum - 1),
1899 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1900 return FAIL;
1901
1902 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1903 {
1904 block_prep(oap, &bd, lnum, TRUE);
1905 if (bd.textlen == 0) /* nothing to delete */
1906 continue;
1907
1908 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1909 if (lnum == curwin->w_cursor.lnum)
1910 {
1911 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1912# ifdef FEAT_VIRTUALEDIT
1913 curwin->w_cursor.coladd = 0;
1914# endif
1915 }
1916
1917 /* n == number of chars deleted
1918 * If we delete a TAB, it may be replaced by several characters.
1919 * Thus the number of characters may increase!
1920 */
1921 n = bd.textlen - bd.startspaces - bd.endspaces;
1922 oldp = ml_get(lnum);
1923 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1924 if (newp == NULL)
1925 continue;
1926 /* copy up to deleted part */
1927 mch_memmove(newp, oldp, (size_t)bd.textcol);
1928 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001929 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 (size_t)(bd.startspaces + bd.endspaces));
1931 /* copy the part after the deleted part */
1932 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001933 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 /* replace the line */
1935 ml_replace(lnum, newp, FALSE);
1936 }
1937
1938 check_cursor_col();
1939 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1940 oap->end.lnum + 1, 0L);
1941 oap->line_count = 0; /* no lines deleted */
1942 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001943 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 if (oap->op_type == OP_CHANGE)
1946 {
1947 /* Delete the lines except the first one. Temporarily move the
1948 * cursor to the next line. Save the current line number, if the
1949 * last line is deleted it may be changed.
1950 */
1951 if (oap->line_count > 1)
1952 {
1953 lnum = curwin->w_cursor.lnum;
1954 ++curwin->w_cursor.lnum;
1955 del_lines((long)(oap->line_count - 1), TRUE);
1956 curwin->w_cursor.lnum = lnum;
1957 }
1958 if (u_save_cursor() == FAIL)
1959 return FAIL;
1960 if (curbuf->b_p_ai) /* don't delete indent */
1961 {
1962 beginline(BL_WHITE); /* cursor on first non-white */
1963 did_ai = TRUE; /* delete the indent when ESC hit */
1964 ai_col = curwin->w_cursor.col;
1965 }
1966 else
1967 beginline(0); /* cursor in column 0 */
1968 truncate_line(FALSE); /* delete the rest of the line */
1969 /* leave cursor past last char in line */
1970 if (oap->line_count > 1)
1971 u_clearline(); /* "U" command not possible after "2cc" */
1972 }
1973 else
1974 {
1975 del_lines(oap->line_count, TRUE);
1976 beginline(BL_WHITE | BL_FIX);
1977 u_clearline(); /* "U" command not possible after "dd" */
1978 }
1979 }
1980 else
1981 {
1982#ifdef FEAT_VIRTUALEDIT
1983 if (virtual_op)
1984 {
1985 int endcol = 0;
1986
1987 /* For virtualedit: break the tabs that are partly included. */
1988 if (gchar_pos(&oap->start) == '\t')
1989 {
1990 if (u_save_cursor() == FAIL) /* save first line for undo */
1991 return FAIL;
1992 if (oap->line_count == 1)
1993 endcol = getviscol2(oap->end.col, oap->end.coladd);
1994 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1995 oap->start = curwin->w_cursor;
1996 if (oap->line_count == 1)
1997 {
1998 coladvance(endcol);
1999 oap->end.col = curwin->w_cursor.col;
2000 oap->end.coladd = curwin->w_cursor.coladd;
2001 curwin->w_cursor = oap->start;
2002 }
2003 }
2004
2005 /* Break a tab only when it's included in the area. */
2006 if (gchar_pos(&oap->end) == '\t'
2007 && (int)oap->end.coladd < oap->inclusive)
2008 {
2009 /* save last line for undo */
2010 if (u_save((linenr_T)(oap->end.lnum - 1),
2011 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2012 return FAIL;
2013 curwin->w_cursor = oap->end;
2014 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2015 oap->end = curwin->w_cursor;
2016 curwin->w_cursor = oap->start;
2017 }
2018 }
2019#endif
2020
2021 if (oap->line_count == 1) /* delete characters within one line */
2022 {
2023 if (u_save_cursor() == FAIL) /* save line for undo */
2024 return FAIL;
2025
2026 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002027 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 && oap->op_type == OP_CHANGE
2029 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002030 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 display_dollar(oap->end.col - !oap->inclusive);
2032
2033 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2034
2035#ifdef FEAT_VIRTUALEDIT
2036 if (virtual_op)
2037 {
2038 /* fix up things for virtualedit-delete:
2039 * break the tabs which are going to get in our way
2040 */
2041 char_u *curline = ml_get_curline();
2042 int len = (int)STRLEN(curline);
2043
2044 if (oap->end.coladd != 0
2045 && (int)oap->end.col >= len - 1
2046 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2047 n++;
2048 /* Delete at least one char (e.g, when on a control char). */
2049 if (n == 0 && oap->start.coladd != oap->end.coladd)
2050 n = 1;
2051
2052 /* When deleted a char in the line, reset coladd. */
2053 if (gchar_cursor() != NUL)
2054 curwin->w_cursor.coladd = 0;
2055 }
2056#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002057 (void)del_bytes((long)n, !virtual_op,
2058 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060 else /* delete characters between lines */
2061 {
2062 pos_T curpos;
2063
2064 /* save deleted and changed lines for undo */
2065 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2066 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2067 return FAIL;
2068
2069 truncate_line(TRUE); /* delete from cursor to end of line */
2070
2071 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2072 ++curwin->w_cursor.lnum;
2073 del_lines((long)(oap->line_count - 2), FALSE);
2074
Bram Moolenaard009e862015-06-09 20:20:03 +02002075 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002076 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002077 curwin->w_cursor.col = 0;
2078 (void)del_bytes((long)n, !virtual_op,
2079 oap->op_type == OP_DELETE && !oap->is_VIsual);
2080 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2081 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 }
2084
2085 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2086
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002087#ifdef FEAT_VIRTUALEDIT
2088setmarks:
2089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (oap->block_mode)
2091 {
2092 curbuf->b_op_end.lnum = oap->end.lnum;
2093 curbuf->b_op_end.col = oap->start.col;
2094 }
2095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 curbuf->b_op_end = oap->start;
2097 curbuf->b_op_start = oap->start;
2098
2099 return OK;
2100}
2101
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102/*
2103 * Adjust end of operating area for ending on a multi-byte character.
2104 * Used for deletion.
2105 */
2106 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002107mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 char_u *p;
2110
2111 if (oap->inclusive)
2112 {
2113 p = ml_get(oap->end.lnum);
2114 oap->end.col += mb_tail_off(p, p + oap->end.col);
2115 }
2116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar630afe82018-06-28 19:26:28 +02002118/*
2119 * Replace the character under the cursor with "c".
2120 * This takes care of multi-byte characters.
2121 */
2122 static void
2123replace_character(int c)
2124{
2125 int n = State;
2126
2127 State = REPLACE;
2128 ins_char(c);
2129 State = n;
2130 /* Backup to the replaced character. */
2131 dec_cursor();
2132}
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134/*
2135 * Replace a whole area with one character.
2136 */
2137 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002138op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 char_u *newp, *oldp;
2143 size_t oldlen;
2144 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002145 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002146 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2149 return OK; /* nothing to do */
2150
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002151 if (c == REPLACE_CR_NCHAR)
2152 {
2153 had_ctrl_v_cr = TRUE;
2154 c = CAR;
2155 }
2156 else if (c == REPLACE_NL_NCHAR)
2157 {
2158 had_ctrl_v_cr = TRUE;
2159 c = NL;
2160 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (has_mbyte)
2163 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 if (u_save((linenr_T)(oap->start.lnum - 1),
2166 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2167 return FAIL;
2168
2169 /*
2170 * block mode replace
2171 */
2172 if (oap->block_mode)
2173 {
2174 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2175 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2176 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002177 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2179 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2180 continue; /* nothing to replace */
2181
2182 /* n == number of extra chars required
2183 * If we split a TAB, it may be replaced by several characters.
2184 * Thus the number of characters may increase!
2185 */
2186#ifdef FEAT_VIRTUALEDIT
2187 /* If the range starts in virtual space, count the initial
2188 * coladd offset as part of "startspaces" */
2189 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2190 {
2191 pos_T vpos;
2192
Bram Moolenaara1381de2009-11-03 15:44:21 +00002193 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 getvpos(&vpos, oap->start_vcol);
2195 bd.startspaces += vpos.coladd;
2196 n = bd.startspaces;
2197 }
2198 else
2199#endif
2200 /* allow for pre spaces */
2201 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2202
2203 /* allow for post spp */
2204 n += (bd.endspaces
2205#ifdef FEAT_VIRTUALEDIT
2206 && !bd.is_oneChar
2207#endif
2208 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2209 /* Figure out how many characters to replace. */
2210 numc = oap->end_vcol - oap->start_vcol + 1;
2211 if (bd.is_short && (!virtual_op || bd.is_MAX))
2212 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 /* A double-wide character can be replaced only up to half the
2215 * times. */
2216 if ((*mb_char2cells)(c) > 1)
2217 {
2218 if ((numc & 1) && !bd.is_short)
2219 {
2220 ++bd.endspaces;
2221 ++n;
2222 }
2223 numc = numc / 2;
2224 }
2225
2226 /* Compute bytes needed, move character count to num_chars. */
2227 num_chars = numc;
2228 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 /* oldlen includes textlen, so don't double count */
2230 n += numc - bd.textlen;
2231
2232 oldp = ml_get_curline();
2233 oldlen = STRLEN(oldp);
2234 newp = alloc_check((unsigned)oldlen + 1 + n);
2235 if (newp == NULL)
2236 continue;
2237 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2238 /* copy up to deleted part */
2239 mch_memmove(newp, oldp, (size_t)bd.textcol);
2240 oldp += bd.textcol + bd.textlen;
2241 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002242 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002244 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2245 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002246 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002248 if (has_mbyte)
2249 {
2250 n = (int)STRLEN(newp);
2251 while (--num_chars >= 0)
2252 n += (*mb_char2bytes)(c, newp + n);
2253 }
2254 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002255 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002256 if (!bd.is_short)
2257 {
2258 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002259 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002260 /* copy the part after the changed part */
2261 STRMOVE(newp + STRLEN(newp), oldp);
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002266 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002267 after_p = alloc_check(
2268 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002269 if (after_p != NULL)
2270 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 /* replace the line */
2273 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002274 if (after_p != NULL)
2275 {
2276 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2277 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2278 oap->end.lnum++;
2279 vim_free(after_p);
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 }
2283 else
2284 {
2285 /*
2286 * MCHAR and MLINE motion replace.
2287 */
2288 if (oap->motion_type == MLINE)
2289 {
2290 oap->start.col = 0;
2291 curwin->w_cursor.col = 0;
2292 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2293 if (oap->end.col)
2294 --oap->end.col;
2295 }
2296 else if (!oap->inclusive)
2297 dec(&(oap->end));
2298
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002299 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 n = gchar_cursor();
2302 if (n != NUL)
2303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2305 {
2306 /* This is slow, but it handles replacing a single-byte
2307 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002308 if (curwin->w_cursor.lnum == oap->end.lnum)
2309 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002310 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 }
2312 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314#ifdef FEAT_VIRTUALEDIT
2315 if (n == TAB)
2316 {
2317 int end_vcol = 0;
2318
2319 if (curwin->w_cursor.lnum == oap->end.lnum)
2320 {
2321 /* oap->end has to be recalculated when
2322 * the tab breaks */
2323 end_vcol = getviscol2(oap->end.col,
2324 oap->end.coladd);
2325 }
2326 coladvance_force(getviscol());
2327 if (curwin->w_cursor.lnum == oap->end.lnum)
2328 getvpos(&oap->end, end_vcol);
2329 }
2330#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002331 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 }
2334#ifdef FEAT_VIRTUALEDIT
2335 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2336 {
2337 int virtcols = oap->end.coladd;
2338
2339 if (curwin->w_cursor.lnum == oap->start.lnum
2340 && oap->start.col == oap->end.col && oap->start.coladd)
2341 virtcols -= oap->start.coladd;
2342
2343 /* oap->end has been trimmed so it's effectively inclusive;
2344 * as a result an extra +1 must be counted so we don't
2345 * trample the NUL byte. */
2346 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2347 curwin->w_cursor.col -= (virtcols + 1);
2348 for (; virtcols >= 0; virtcols--)
2349 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002350 if ((*mb_char2len)(c) > 1)
2351 replace_character(c);
2352 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002353 PBYTE(curwin->w_cursor, c);
2354 if (inc(&curwin->w_cursor) == -1)
2355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 }
2358#endif
2359
2360 /* Advance to next character, stop at the end of the file. */
2361 if (inc_cursor() == -1)
2362 break;
2363 }
2364 }
2365
2366 curwin->w_cursor = oap->start;
2367 check_cursor();
2368 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2369
2370 /* Set "'[" and "']" marks. */
2371 curbuf->b_op_start = oap->start;
2372 curbuf->b_op_end = oap->end;
2373
2374 return OK;
2375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002377static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379/*
2380 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2381 */
2382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002383op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002387 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389 if (u_save((linenr_T)(oap->start.lnum - 1),
2390 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2391 return;
2392
2393 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (oap->block_mode) /* Visual block mode */
2395 {
2396 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2397 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002398 int one_change;
2399
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 block_prep(oap, &bd, pos.lnum, FALSE);
2401 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002402 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2403 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002404
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002405#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002406 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2409
Bram Moolenaar009b2592004-10-24 19:18:58 +00002410 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2411 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002413 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 if (did_change)
2418 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2419 }
2420 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 if (oap->motion_type == MLINE)
2423 {
2424 oap->start.col = 0;
2425 pos.col = 0;
2426 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2427 if (oap->end.col)
2428 --oap->end.col;
2429 }
2430 else if (!oap->inclusive)
2431 dec(&(oap->end));
2432
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002433 if (pos.lnum == oap->end.lnum)
2434 did_change = swapchars(oap->op_type, &pos,
2435 oap->end.col - pos.col + 1);
2436 else
2437 for (;;)
2438 {
2439 did_change |= swapchars(oap->op_type, &pos,
2440 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2441 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002442 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002443 break;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (did_change)
2446 {
2447 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2448 0L);
2449#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002450 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
2452 char_u *ptr;
2453 int count;
2454
2455 pos = oap->start;
2456 while (pos.lnum < oap->end.lnum)
2457 {
2458 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002459 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002460 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002462 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 pos.col = 0;
2464 pos.lnum++;
2465 }
2466 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2467 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002468 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002470 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472#endif
2473 }
2474 }
2475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (!did_change && oap->is_VIsual)
2477 /* No change: need to remove the Visual selection */
2478 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 /*
2481 * Set '[ and '] marks.
2482 */
2483 curbuf->b_op_start = oap->start;
2484 curbuf->b_op_end = oap->end;
2485
2486 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002487 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002488 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
2490
2491/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002492 * Invoke swapchar() on "length" bytes at position "pos".
2493 * "pos" is advanced to just after the changed characters.
2494 * "length" is rounded up to include the whole last multi-byte character.
2495 * Also works correctly when the number of bytes changes.
2496 * Returns TRUE if some character was changed.
2497 */
2498 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002499swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002500{
2501 int todo;
2502 int did_change = 0;
2503
2504 for (todo = length; todo > 0; --todo)
2505 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002506 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002507 {
2508 int len = (*mb_ptr2len)(ml_get_pos(pos));
2509
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002510 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002511 if (len > 0)
2512 todo -= len - 1;
2513 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002514 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002515 if (inc(pos) == -1) /* at end of file */
2516 break;
2517 }
2518 return did_change;
2519}
2520
2521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * If op_type == OP_UPPER: make uppercase,
2523 * if op_type == OP_LOWER: make lowercase,
2524 * if op_type == OP_ROT13: do rot13 encoding,
2525 * else swap case of character at 'pos'
2526 * returns TRUE when something actually changed.
2527 */
2528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002529swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 int c;
2532 int nc;
2533
2534 c = gchar_pos(pos);
2535
2536 /* Only do rot13 encoding for ASCII characters. */
2537 if (c >= 0x80 && op_type == OP_ROT13)
2538 return FALSE;
2539
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002540 if (op_type == OP_UPPER && c == 0xdf
2541 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002542 {
2543 pos_T sp = curwin->w_cursor;
2544
2545 /* Special handling of German sharp s: change to "SS". */
2546 curwin->w_cursor = *pos;
2547 del_char(FALSE);
2548 ins_char('S');
2549 ins_char('S');
2550 curwin->w_cursor = sp;
2551 inc(pos);
2552 }
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2555 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 nc = c;
2557 if (MB_ISLOWER(c))
2558 {
2559 if (op_type == OP_ROT13)
2560 nc = ROT13(c, 'a');
2561 else if (op_type != OP_LOWER)
2562 nc = MB_TOUPPER(c);
2563 }
2564 else if (MB_ISUPPER(c))
2565 {
2566 if (op_type == OP_ROT13)
2567 nc = ROT13(c, 'A');
2568 else if (op_type != OP_UPPER)
2569 nc = MB_TOLOWER(c);
2570 }
2571 if (nc != c)
2572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2574 {
2575 pos_T sp = curwin->w_cursor;
2576
2577 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002578 /* don't use del_char(), it also removes composing chars */
2579 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 ins_char(nc);
2581 curwin->w_cursor = sp;
2582 }
2583 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002584 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return TRUE;
2586 }
2587 return FALSE;
2588}
2589
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590/*
2591 * op_insert - Insert and append operators for Visual mode.
2592 */
2593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002594op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 long ins_len, pre_textlen = 0;
2597 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002598 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 struct block_def bd;
2600 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002601 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 /* edit() changes this - record it for OP_APPEND */
2604 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2605
2606 /* vis block is still marked. Get rid of it now. */
2607 curwin->w_cursor.lnum = oap->start.lnum;
2608 update_screen(INVERTED);
2609
2610 if (oap->block_mode)
2611 {
2612#ifdef FEAT_VIRTUALEDIT
2613 /* When 'virtualedit' is used, need to insert the extra spaces before
2614 * doing block_prep(). When only "block" is used, virtual edit is
2615 * already disabled, but still need it when calling
2616 * coladvance_force(). */
2617 if (curwin->w_cursor.coladd > 0)
2618 {
2619 int old_ve_flags = ve_flags;
2620
2621 ve_flags = VE_ALL;
2622 if (u_save_cursor() == FAIL)
2623 return;
2624 coladvance_force(oap->op_type == OP_APPEND
2625 ? oap->end_vcol + 1 : getviscol());
2626 if (oap->op_type == OP_APPEND)
2627 --curwin->w_cursor.col;
2628 ve_flags = old_ve_flags;
2629 }
2630#endif
2631 /* Get the info about the block before entering the text */
2632 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002633 /* Get indent information */
2634 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 if (oap->op_type == OP_APPEND)
2638 firstline += bd.textlen;
2639 pre_textlen = (long)STRLEN(firstline);
2640 }
2641
2642 if (oap->op_type == OP_APPEND)
2643 {
2644 if (oap->block_mode
2645#ifdef FEAT_VIRTUALEDIT
2646 && curwin->w_cursor.coladd == 0
2647#endif
2648 )
2649 {
2650 /* Move the cursor to the character right of the block. */
2651 curwin->w_set_curswant = TRUE;
2652 while (*ml_get_cursor() != NUL
2653 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2654 ++curwin->w_cursor.col;
2655 if (bd.is_short && !bd.is_MAX)
2656 {
2657 /* First line was too short, make it longer and adjust the
2658 * values in "bd". */
2659 if (u_save_cursor() == FAIL)
2660 return;
2661 for (i = 0; i < bd.endspaces; ++i)
2662 ins_char(' ');
2663 bd.textlen += bd.endspaces;
2664 }
2665 }
2666 else
2667 {
2668 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002669 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002672 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 && oap->start_vcol != oap->end_vcol)
2674 inc_cursor();
2675 }
2676 }
2677
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002678 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002679 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002681 /* When a tab was inserted, and the characters in front of the tab
2682 * have been converted to a tab as well, the column of the cursor
2683 * might have actually been reduced, so need to adjust here. */
2684 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002685 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002686 oap->start = curbuf->b_op_start_orig;
2687
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002688 /* If user has moved off this line, we don't know what to do, so do
2689 * nothing.
2690 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2691 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return;
2693
2694 if (oap->block_mode)
2695 {
2696 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002697 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002698 size_t len;
2699 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002701 /* If indent kicked in, the firstline might have changed
2702 * but only do that, if the indent actually increased. */
2703 ind_post = (colnr_T)getwhitecols_curline();
2704 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2705 {
2706 bd.textcol += ind_post - ind_pre;
2707 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002708 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002709 }
2710
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002711 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002712 * to adjust the block for that. But only do it, if the difference
2713 * does not come from indent kicking in. */
2714 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2715 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002716 {
2717 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002718 && oap->start.col
2719#ifdef FEAT_VIRTUALEDIT
2720 + oap->start.coladd
2721#endif
2722 != curbuf->b_op_start_orig.col
2723#ifdef FEAT_VIRTUALEDIT
2724 + curbuf->b_op_start_orig.coladd
2725#endif
2726 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002727 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002728 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002729#ifdef FEAT_VIRTUALEDIT
2730 curbuf->b_op_start_orig.coladd
2731#else
2732 0
2733#endif
2734 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002735 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002736 pre_textlen -= t - oap->start_vcol;
2737 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002738 }
2739 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002740 && oap->end.col
2741#ifdef FEAT_VIRTUALEDIT
2742 + oap->end.coladd
2743#endif
2744 >= curbuf->b_op_start_orig.col
2745#ifdef FEAT_VIRTUALEDIT
2746 + curbuf->b_op_start_orig.coladd
2747#endif
2748 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002749 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002750 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002751#ifdef FEAT_VIRTUALEDIT
2752 curbuf->b_op_start_orig.coladd
2753#else
2754 0
2755#endif
2756 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002757 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002758 /* reset pre_textlen to the value of OP_INSERT */
2759 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002760 pre_textlen -= t - oap->start_vcol;
2761 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002762 oap->op_type = OP_INSERT;
2763 }
2764 }
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 /*
2767 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002768 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * Don't do this when "$" used, end-of-line will have changed.
2770 */
2771 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2772 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2773 {
2774 if (oap->op_type == OP_APPEND)
2775 {
2776 pre_textlen += bd2.textlen - bd.textlen;
2777 if (bd2.endspaces)
2778 --bd2.textlen;
2779 }
2780 bd.textcol = bd2.textcol;
2781 bd.textlen = bd2.textlen;
2782 }
2783
2784 /*
2785 * Subsequent calls to ml_get() flush the firstline data - take a
2786 * copy of the required string.
2787 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002788 firstline = ml_get(oap->start.lnum);
2789 len = STRLEN(firstline);
2790 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002792 add += bd.textlen;
2793 if ((size_t)add > len)
2794 firstline += len; // short line, point to the NUL
2795 else
2796 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002797 if (pre_textlen >= 0
2798 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
2800 ins_text = vim_strnsave(firstline, (int)ins_len);
2801 if (ins_text != NULL)
2802 {
2803 /* block handled here */
2804 if (u_save(oap->start.lnum,
2805 (linenr_T)(oap->end.lnum + 1)) == OK)
2806 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2807 &bd);
2808
2809 curwin->w_cursor.col = oap->start.col;
2810 check_cursor();
2811 vim_free(ins_text);
2812 }
2813 }
2814 }
2815}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817/*
2818 * op_change - handle a change operation
2819 *
2820 * return TRUE if edit() returns because of a CTRL-O command
2821 */
2822 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002823op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 colnr_T l;
2826 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 long offset;
2828 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002829 long ins_len;
2830 long pre_textlen = 0;
2831 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 char_u *firstline;
2833 char_u *ins_text, *newp, *oldp;
2834 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836 l = oap->start.col;
2837 if (oap->motion_type == MLINE)
2838 {
2839 l = 0;
2840#ifdef FEAT_SMARTINDENT
2841 if (!p_paste && curbuf->b_p_si
2842# ifdef FEAT_CINDENT
2843 && !curbuf->b_p_cin
2844# endif
2845 )
2846 can_si = TRUE; /* It's like opening a new line, do si */
2847#endif
2848 }
2849
2850 /* First delete the text in the region. In an empty buffer only need to
2851 * save for undo */
2852 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2853 {
2854 if (u_save_cursor() == FAIL)
2855 return FALSE;
2856 }
2857 else if (op_delete(oap) == FAIL)
2858 return FALSE;
2859
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002860 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 && !virtual_op)
2862 inc_cursor();
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* check for still on same line (<CR> in inserted text meaningless) */
2865 /* skip blank lines too */
2866 if (oap->block_mode)
2867 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002868#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 /* Add spaces before getting the current line length. */
2870 if (virtual_op && (curwin->w_cursor.coladd > 0
2871 || gchar_cursor() == NUL))
2872 coladvance_force(getviscol());
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002873#endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002874 firstline = ml_get(oap->start.lnum);
2875 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002876 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 bd.textcol = curwin->w_cursor.col;
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2881 if (oap->motion_type == MLINE)
2882 fix_indent();
2883#endif
2884
2885 retval = edit(NUL, FALSE, (linenr_T)1);
2886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002888 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002890 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002892 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002894 /* Auto-indenting may have changed the indent. If the cursor was past
2895 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002897 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002899 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002900
2901 pre_textlen += new_indent - pre_indent;
2902 bd.textcol += new_indent - pre_indent;
2903 }
2904
2905 ins_len = (long)STRLEN(firstline) - pre_textlen;
2906 if (ins_len > 0)
2907 {
2908 /* Subsequent calls to ml_get() flush the firstline data - take a
2909 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2911 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002912 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2914 linenr++)
2915 {
2916 block_prep(oap, &bd, linenr, TRUE);
2917 if (!bd.is_short || virtual_op)
2918 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002919#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 pos_T vpos;
2921
2922 /* If the block starts in virtual space, count the
2923 * initial coladd offset as part of "startspaces" */
2924 if (bd.is_short)
2925 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002926 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929 else
2930 vpos.coladd = 0;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 oldp = ml_get(linenr);
2933 newp = alloc_check((unsigned)(STRLEN(oldp)
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002934#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 + vpos.coladd
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 + ins_len + 1));
2938 if (newp == NULL)
2939 continue;
2940 /* copy up to block start */
2941 mch_memmove(newp, oldp, (size_t)bd.textcol);
2942 offset = bd.textcol;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002943#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002944 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 offset += vpos.coladd;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2948 offset += ins_len;
2949 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002950 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 ml_replace(linenr, newp, FALSE);
2952 }
2953 }
2954 check_cursor();
2955
2956 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2957 }
2958 vim_free(ins_text);
2959 }
2960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 return retval;
2963}
2964
2965/*
2966 * set all the yank registers to empty (called from main())
2967 */
2968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002969init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 int i;
2972
2973 for (i = 0; i < NUM_REGISTERS; ++i)
2974 y_regs[i].y_array = NULL;
2975}
2976
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002977#if defined(EXITFREE) || defined(PROTO)
2978 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002979clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002980{
2981 int i;
2982
2983 for (i = 0; i < NUM_REGISTERS; ++i)
2984 {
2985 y_current = &y_regs[i];
2986 if (y_current->y_array != NULL)
2987 free_yank_all();
2988 }
2989}
2990#endif
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
2993 * Free "n" lines from the current yank register.
2994 * Called for normal freeing and in case of error.
2995 */
2996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002997free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 if (y_current->y_array != NULL)
3000 {
3001 long i;
3002
3003 for (i = n; --i >= 0; )
3004 {
3005#ifdef AMIGA /* only for very slow machines */
3006 if ((i & 1023) == 1023) /* this may take a while */
3007 {
3008 /*
3009 * This message should never cause a hit-return message.
3010 * Overwrite this message with any next message.
3011 */
3012 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 --no_wait_return;
3015 msg_didout = FALSE;
3016 msg_col = 0;
3017 }
3018#endif
3019 vim_free(y_current->y_array[i]);
3020 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003021 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022#ifdef AMIGA
3023 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003024 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#endif
3026 }
3027}
3028
3029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003030free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 free_yank(y_current->y_size);
3033}
3034
3035/*
3036 * Yank the text between "oap->start" and "oap->end" into a yank register.
3037 * If we are to append (uppercase register), we first yank into a new yank
3038 * register and then concatenate the old and the new one (so we keep the old
3039 * one in case of out-of-memory).
3040 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003041 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 */
3043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003044op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003047 yankreg_T *curr; /* copy of y_current */
3048 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 char_u **new_ptr;
3050 linenr_T lnum; /* current line number */
3051 long j;
3052 int yanktype = oap->motion_type;
3053 long yanklines = oap->line_count;
3054 linenr_T yankendlnum = oap->end.lnum;
3055 char_u *p;
3056 char_u *pnew;
3057 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003058#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003059 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /* check for read-only register */
3063 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3064 {
3065 beep_flush();
3066 return FAIL;
3067 }
3068 if (oap->regname == '_') /* black hole: nothing to do */
3069 return OK;
3070
3071#ifdef FEAT_CLIPBOARD
3072 if (!clip_star.available && oap->regname == '*')
3073 oap->regname = 0;
3074 else if (!clip_plus.available && oap->regname == '+')
3075 oap->regname = 0;
3076#endif
3077
3078 if (!deleting) /* op_delete() already set y_current */
3079 get_yank_register(oap->regname, TRUE);
3080
3081 curr = y_current;
3082 /* append to existing contents */
3083 if (y_append && y_current->y_array != NULL)
3084 y_current = &newreg;
3085 else
3086 free_yank_all(); /* free previously yanked lines */
3087
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003088 /*
3089 * If the cursor was in column 1 before and after the movement, and the
3090 * operator is not inclusive, the yank is always linewise.
3091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if ( oap->motion_type == MCHAR
3093 && oap->start.col == 0
3094 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003096 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 && oap->end.col == 0
3098 && yanklines > 1)
3099 {
3100 yanktype = MLINE;
3101 --yankendlnum;
3102 --yanklines;
3103 }
3104
3105 y_current->y_size = yanklines;
3106 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3109 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if (y_current->y_array == NULL)
3111 {
3112 y_current = curr;
3113 return FAIL;
3114 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003115#ifdef FEAT_VIMINFO
3116 y_current->y_time_set = vim_time();
3117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 y_idx = 0;
3120 lnum = oap->start.lnum;
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (oap->block_mode)
3123 {
3124 /* Visual block mode */
3125 y_current->y_type = MBLOCK; /* set the yank register type */
3126 y_current->y_width = oap->end_vcol - oap->start_vcol;
3127
3128 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3129 y_current->y_width--;
3130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3133 {
3134 switch (y_current->y_type)
3135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 case MBLOCK:
3137 block_prep(oap, &bd, lnum, FALSE);
3138 if (yank_copy_line(&bd, y_idx) == FAIL)
3139 goto fail;
3140 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 case MLINE:
3143 if ((y_current->y_array[y_idx] =
3144 vim_strsave(ml_get(lnum))) == NULL)
3145 goto fail;
3146 break;
3147
3148 case MCHAR:
3149 {
3150 colnr_T startcol = 0, endcol = MAXCOL;
3151#ifdef FEAT_VIRTUALEDIT
3152 int is_oneChar = FALSE;
3153 colnr_T cs, ce;
3154#endif
3155 p = ml_get(lnum);
3156 bd.startspaces = 0;
3157 bd.endspaces = 0;
3158
3159 if (lnum == oap->start.lnum)
3160 {
3161 startcol = oap->start.col;
3162#ifdef FEAT_VIRTUALEDIT
3163 if (virtual_op)
3164 {
3165 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3166 if (ce != cs && oap->start.coladd > 0)
3167 {
3168 /* Part of a tab selected -- but don't
3169 * double-count it. */
3170 bd.startspaces = (ce - cs + 1)
3171 - oap->start.coladd;
3172 startcol++;
3173 }
3174 }
3175#endif
3176 }
3177
3178 if (lnum == oap->end.lnum)
3179 {
3180 endcol = oap->end.col;
3181#ifdef FEAT_VIRTUALEDIT
3182 if (virtual_op)
3183 {
3184 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3185 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 /* Don't add space for double-wide
3187 * char; endcol will be on last byte
3188 * of multi-byte char. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003189 && (*mb_head_off)(p, p + endcol) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 if (oap->start.lnum == oap->end.lnum
3192 && oap->start.col == oap->end.col)
3193 {
3194 /* Special case: inside a single char */
3195 is_oneChar = TRUE;
3196 bd.startspaces = oap->end.coladd
3197 - oap->start.coladd + oap->inclusive;
3198 endcol = startcol;
3199 }
3200 else
3201 {
3202 bd.endspaces = oap->end.coladd
3203 + oap->inclusive;
3204 endcol -= oap->inclusive;
3205 }
3206 }
3207 }
3208#endif
3209 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003210 if (endcol == MAXCOL)
3211 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (startcol > endcol
3213#ifdef FEAT_VIRTUALEDIT
3214 || is_oneChar
3215#endif
3216 )
3217 bd.textlen = 0;
3218 else
3219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 bd.textlen = endcol - startcol + oap->inclusive;
3221 }
3222 bd.textstart = p + startcol;
3223 if (yank_copy_line(&bd, y_idx) == FAIL)
3224 goto fail;
3225 break;
3226 }
3227 /* NOTREACHED */
3228 }
3229 }
3230
3231 if (curr != y_current) /* append the new block to the old block */
3232 {
3233 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3234 (curr->y_size + y_current->y_size)), TRUE);
3235 if (new_ptr == NULL)
3236 goto fail;
3237 for (j = 0; j < curr->y_size; ++j)
3238 new_ptr[j] = curr->y_array[j];
3239 vim_free(curr->y_array);
3240 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003241#ifdef FEAT_VIMINFO
3242 curr->y_time_set = vim_time();
3243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3246 curr->y_type = MLINE;
3247
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003248 /* Concatenate the last line of the old block with the first line of
3249 * the new block, unless being Vi compatible. */
3250 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
3252 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3253 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3254 if (pnew == NULL)
3255 {
3256 y_idx = y_current->y_size - 1;
3257 goto fail;
3258 }
3259 STRCPY(pnew, curr->y_array[--j]);
3260 STRCAT(pnew, y_current->y_array[0]);
3261 vim_free(curr->y_array[j]);
3262 vim_free(y_current->y_array[0]);
3263 curr->y_array[j++] = pnew;
3264 y_idx = 1;
3265 }
3266 else
3267 y_idx = 0;
3268 while (y_idx < y_current->y_size)
3269 curr->y_array[j++] = y_current->y_array[y_idx++];
3270 curr->y_size = j;
3271 vim_free(y_current->y_array);
3272 y_current = curr;
3273 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003274 if (curwin->w_p_rnu)
3275 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 if (mess) /* Display message about yank? */
3277 {
3278 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 && yanklines == 1)
3281 yanklines = 0;
3282 /* Some versions of Vi use ">=" here, some don't... */
3283 if (yanklines > p_report)
3284 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003285 char namebuf[100];
3286
3287 if (oap->regname == NUL)
3288 *namebuf = NUL;
3289 else
3290 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003291 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 /* redisplay now, so message is not deleted */
3294 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003295 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003298 "block of %ld lines yanked%s", yanklines),
3299 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003304 "%ld lines yanked%s", yanklines),
3305 yanklines, namebuf);
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 }
3309
3310 /*
3311 * Set "'[" and "']" marks.
3312 */
3313 curbuf->b_op_start = oap->start;
3314 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003315 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003316 {
3317 curbuf->b_op_start.col = 0;
3318 curbuf->b_op_end.col = MAXCOL;
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321#ifdef FEAT_CLIPBOARD
3322 /*
3323 * If we were yanking to the '*' register, send result to clipboard.
3324 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3325 * to the '*' register.
3326 */
3327 if (clip_star.available
3328 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003329 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003330 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 if (curr != &(y_regs[STAR_REGISTER]))
3333 /* Copy the text from register 0 to the clipboard register. */
3334 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3335
3336 clip_own_selection(&clip_star);
3337 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003338# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003339 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342
3343# ifdef FEAT_X11
3344 /*
3345 * If we were yanking to the '+' register, send result to selection.
3346 * Also copy to the '*' register, in case auto-select is off.
3347 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003348 if (clip_plus.available
3349 && (curr == &(y_regs[PLUS_REGISTER])
3350 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003351 && ((clip_unnamed | clip_unnamed_saved) &
3352 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003354 if (curr != &(y_regs[PLUS_REGISTER]))
3355 /* Copy the text from register 0 to the clipboard register. */
3356 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 clip_own_selection(&clip_plus);
3359 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003360 if (!clip_isautosel_star() && !clip_isautosel_plus()
3361 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
3363 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3364 clip_own_selection(&clip_star);
3365 clip_gen_set_selection(&clip_star);
3366 }
3367 }
3368# endif
3369#endif
3370
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003371#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003372 if (!deleting && has_textyankpost())
3373 yank_do_autocmd(oap, y_current);
3374#endif
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 return OK;
3377
3378fail: /* free the allocated lines */
3379 free_yank(y_idx + 1);
3380 y_current = curr;
3381 return FAIL;
3382}
3383
3384 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003385yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
3387 char_u *pnew;
3388
3389 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3390 == NULL)
3391 return FAIL;
3392 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003393 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 pnew += bd->startspaces;
3395 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3396 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003397 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 pnew += bd->endspaces;
3399 *pnew = NUL;
3400 return OK;
3401}
3402
3403#ifdef FEAT_CLIPBOARD
3404/*
3405 * Make a copy of the y_current register to register "reg".
3406 */
3407 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003408copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003410 yankreg_T *curr = y_current;
3411 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 y_current = reg;
3414 free_yank_all();
3415 *y_current = *curr;
3416 y_current->y_array = (char_u **)lalloc_clear(
3417 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3418 if (y_current->y_array == NULL)
3419 y_current->y_size = 0;
3420 else
3421 for (j = 0; j < y_current->y_size; ++j)
3422 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3423 {
3424 free_yank(j);
3425 y_current->y_size = 0;
3426 break;
3427 }
3428 y_current = curr;
3429}
3430#endif
3431
3432/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003433 * Put contents of register "regname" into the text.
3434 * Caller must check "regname" to be valid!
3435 * "flags": PUT_FIXINDENT make indent look nice
3436 * PUT_CURSEND leave cursor after end of new text
3437 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
3439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003440do_put(
3441 int regname,
3442 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3443 long count,
3444 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 char_u *ptr;
3447 char_u *newp, *oldp;
3448 int yanklen;
3449 int totlen = 0; /* init for gcc */
3450 linenr_T lnum;
3451 colnr_T col;
3452 long i; /* index in y_array[] */
3453 int y_type;
3454 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int oldlen;
3456 long y_width = 0;
3457 colnr_T vcol;
3458 int delcount;
3459 int incr = 0;
3460 long j;
3461 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 char_u **y_array = NULL;
3463 long nr_lines = 0;
3464 pos_T new_cursor;
3465 int indent;
3466 int orig_indent = 0; /* init for gcc */
3467 int indent_diff = 0; /* init for gcc */
3468 int first_indent = TRUE;
3469 int lendiff = 0;
3470 pos_T old_pos;
3471 char_u *insert_string = NULL;
3472 int allocated = FALSE;
3473 long cnt;
3474
3475#ifdef FEAT_CLIPBOARD
3476 /* Adjust register name for "unnamed" in 'clipboard'. */
3477 adjust_clip_reg(&regname);
3478 (void)may_get_selection(regname);
3479#endif
3480
3481 if (flags & PUT_FIXINDENT)
3482 orig_indent = get_indent();
3483
3484 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3485 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3486
3487 /*
3488 * Using inserted text works differently, because the register includes
3489 * special characters (newlines, etc.).
3490 */
3491 if (regname == '.')
3492 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003493 if (VIsual_active)
3494 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3496 (count == -1 ? 'O' : 'i')), count, FALSE);
3497 /* Putting the text is done later, so can't really move the cursor to
3498 * the next character. Use "l" to simulate it. */
3499 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3500 stuffcharReadbuff('l');
3501 return;
3502 }
3503
3504 /*
3505 * For special registers '%' (file name), '#' (alternate file name) and
3506 * ':' (last command line), etc. we have to create a fake yank register.
3507 */
3508 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3509 {
3510 if (insert_string == NULL)
3511 return;
3512 }
3513
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003514 /* Autocommands may be executed when saving lines for undo. This might
3515 * make "y_array" invalid, so we start undo now to avoid that. */
3516 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3517 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (insert_string != NULL)
3520 {
3521 y_type = MCHAR;
3522#ifdef FEAT_EVAL
3523 if (regname == '=')
3524 {
3525 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003526 * characters.
3527 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 for (;;)
3529 {
3530 y_size = 0;
3531 ptr = insert_string;
3532 while (ptr != NULL)
3533 {
3534 if (y_array != NULL)
3535 y_array[y_size] = ptr;
3536 ++y_size;
3537 ptr = vim_strchr(ptr, '\n');
3538 if (ptr != NULL)
3539 {
3540 if (y_array != NULL)
3541 *ptr = NUL;
3542 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003543 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (*ptr == NUL)
3545 {
3546 y_type = MLINE;
3547 break;
3548 }
3549 }
3550 }
3551 if (y_array != NULL)
3552 break;
3553 y_array = (char_u **)alloc((unsigned)
3554 (y_size * sizeof(char_u *)));
3555 if (y_array == NULL)
3556 goto end;
3557 }
3558 }
3559 else
3560#endif
3561 {
3562 y_size = 1; /* use fake one-line yank register */
3563 y_array = &insert_string;
3564 }
3565 }
3566 else
3567 {
3568 get_yank_register(regname, FALSE);
3569
3570 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 y_size = y_current->y_size;
3573 y_array = y_current->y_array;
3574 }
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (y_type == MLINE)
3577 {
3578 if (flags & PUT_LINE_SPLIT)
3579 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003580 char_u *p;
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /* "p" or "P" in Visual mode: split the lines to put the text in
3583 * between. */
3584 if (u_save_cursor() == FAIL)
3585 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003586 p = ml_get_cursor();
3587 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003588 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003589 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (ptr == NULL)
3591 goto end;
3592 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3593 vim_free(ptr);
3594
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003595 oldp = ml_get_curline();
3596 p = oldp + curwin->w_cursor.col;
3597 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003598 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003599 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (ptr == NULL)
3601 goto end;
3602 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3603 ++nr_lines;
3604 dir = FORWARD;
3605 }
3606 if (flags & PUT_LINE_FORWARD)
3607 {
3608 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003609 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 dir = FORWARD;
3611 }
3612 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3613 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3617 y_type = MLINE;
3618
3619 if (y_size == 0 || y_array == NULL)
3620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003621 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 regname == 0 ? (char_u *)"\"" : transchar(regname));
3623 goto end;
3624 }
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (y_type == MBLOCK)
3627 {
3628 lnum = curwin->w_cursor.lnum + y_size + 1;
3629 if (lnum > curbuf->b_ml.ml_line_count)
3630 lnum = curbuf->b_ml.ml_line_count + 1;
3631 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3632 goto end;
3633 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003634 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
3636 lnum = curwin->w_cursor.lnum;
3637#ifdef FEAT_FOLDING
3638 /* Correct line number for closed fold. Don't move the cursor yet,
3639 * u_save() uses it. */
3640 if (dir == BACKWARD)
3641 (void)hasFolding(lnum, &lnum, NULL);
3642 else
3643 (void)hasFolding(lnum, NULL, &lnum);
3644#endif
3645 if (dir == FORWARD)
3646 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003647 /* In an empty buffer the empty line is going to be replaced, include
3648 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003649 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 goto end;
3651#ifdef FEAT_FOLDING
3652 if (dir == FORWARD)
3653 curwin->w_cursor.lnum = lnum - 1;
3654 else
3655 curwin->w_cursor.lnum = lnum;
3656 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3657#endif
3658 }
3659 else if (u_save_cursor() == FAIL)
3660 goto end;
3661
3662 yanklen = (int)STRLEN(y_array[0]);
3663
3664#ifdef FEAT_VIRTUALEDIT
3665 if (ve_flags == VE_ALL && y_type == MCHAR)
3666 {
3667 if (gchar_cursor() == TAB)
3668 {
3669 /* Don't need to insert spaces when "p" on the last position of a
3670 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003671#ifdef FEAT_VARTABS
3672 int viscol = getviscol();
3673 if (dir == FORWARD
3674 ? tabstop_padding(viscol, curbuf->b_p_ts,
3675 curbuf->b_p_vts_array) != 1
3676 : curwin->w_cursor.coladd > 0)
3677 coladvance_force(viscol);
3678#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (dir == FORWARD
3680 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3681 : curwin->w_cursor.coladd > 0)
3682 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
3685 curwin->w_cursor.coladd = 0;
3686 }
3687 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3688 coladvance_force(getviscol() + (dir == FORWARD));
3689 }
3690#endif
3691
3692 lnum = curwin->w_cursor.lnum;
3693 col = curwin->w_cursor.col;
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 /*
3696 * Block mode
3697 */
3698 if (y_type == MBLOCK)
3699 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003700 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 colnr_T endcol2 = 0;
3702
3703 if (dir == FORWARD && c != NUL)
3704 {
3705#ifdef FEAT_VIRTUALEDIT
3706 if (ve_flags == VE_ALL)
3707 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3708 else
3709#endif
3710 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (has_mbyte)
3713 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003714 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#ifdef FEAT_VIRTUALEDIT
3717 if (c != TAB || ve_flags != VE_ALL)
3718#endif
3719 ++curwin->w_cursor.col;
3720 ++col;
3721 }
3722 else
3723 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3724
3725#ifdef FEAT_VIRTUALEDIT
3726 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003727 if (ve_flags == VE_ALL
3728 && (curwin->w_cursor.coladd > 0
3729 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
3731 if (dir == FORWARD && c == NUL)
3732 ++col;
3733 if (dir != FORWARD && c != NUL)
3734 ++curwin->w_cursor.col;
3735 if (c == TAB)
3736 {
3737 if (dir == BACKWARD && curwin->w_cursor.col)
3738 curwin->w_cursor.col--;
3739 if (dir == FORWARD && col - 1 == endcol2)
3740 curwin->w_cursor.col++;
3741 }
3742 }
3743 curwin->w_cursor.coladd = 0;
3744#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003745 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 for (i = 0; i < y_size; ++i)
3747 {
3748 int spaces;
3749 char shortline;
3750
3751 bd.startspaces = 0;
3752 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 vcol = 0;
3754 delcount = 0;
3755
3756 /* add a new line */
3757 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3758 {
3759 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3760 (colnr_T)1, FALSE) == FAIL)
3761 break;
3762 ++nr_lines;
3763 }
3764 /* get the old line and advance to the position to insert at */
3765 oldp = ml_get_curline();
3766 oldlen = (int)STRLEN(oldp);
3767 for (ptr = oldp; vcol < col && *ptr; )
3768 {
3769 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 vcol += incr;
3772 }
3773 bd.textcol = (colnr_T)(ptr - oldp);
3774
3775 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3776
3777 if (vcol < col) /* line too short, padd with spaces */
3778 bd.startspaces = col - vcol;
3779 else if (vcol > col)
3780 {
3781 bd.endspaces = vcol - col;
3782 bd.startspaces = incr - bd.endspaces;
3783 --bd.textcol;
3784 delcount = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 if (has_mbyte)
3786 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (oldp[bd.textcol] != TAB)
3788 {
3789 /* Only a Tab can be split into spaces. Other
3790 * characters will have to be moved to after the
3791 * block, causing misalignment. */
3792 delcount = 0;
3793 bd.endspaces = 0;
3794 }
3795 }
3796
3797 yanklen = (int)STRLEN(y_array[i]);
3798
3799 /* calculate number of spaces required to fill right side of block*/
3800 spaces = y_width + 1;
3801 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003802 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (spaces < 0)
3804 spaces = 0;
3805
3806 /* insert the new text */
3807 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3808 newp = alloc_check((unsigned)totlen + oldlen + 1);
3809 if (newp == NULL)
3810 break;
3811 /* copy part up to cursor to new line */
3812 ptr = newp;
3813 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3814 ptr += bd.textcol;
3815 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003816 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 ptr += bd.startspaces;
3818 /* insert the new text */
3819 for (j = 0; j < count; ++j)
3820 {
3821 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3822 ptr += yanklen;
3823
3824 /* insert block's trailing spaces only if there's text behind */
3825 if ((j < count - 1 || !shortline) && spaces)
3826 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003827 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 ptr += spaces;
3829 }
3830 }
3831 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003832 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 ptr += bd.endspaces;
3834 /* move the text after the cursor to the end of the line. */
3835 mch_memmove(ptr, oldp + bd.textcol + delcount,
3836 (size_t)(oldlen - bd.textcol - delcount + 1));
3837 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3838
3839 ++curwin->w_cursor.lnum;
3840 if (i == 0)
3841 curwin->w_cursor.col += bd.startspaces;
3842 }
3843
3844 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3845
3846 /* Set '[ mark. */
3847 curbuf->b_op_start = curwin->w_cursor;
3848 curbuf->b_op_start.lnum = lnum;
3849
3850 /* adjust '] mark */
3851 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3852 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003853# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (flags & PUT_CURSEND)
3857 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003858 colnr_T len;
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 curwin->w_cursor = curbuf->b_op_end;
3861 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003862
3863 /* in Insert mode we might be after the NUL, correct for that */
3864 len = (colnr_T)STRLEN(ml_get_curline());
3865 if (curwin->w_cursor.col > len)
3866 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 else
3869 curwin->w_cursor.lnum = lnum;
3870 }
3871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 {
3873 /*
3874 * Character or Line mode
3875 */
3876 if (y_type == MCHAR)
3877 {
3878 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3879 * char */
3880 if (dir == FORWARD && gchar_cursor() != NUL)
3881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (has_mbyte)
3883 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003884 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886 /* put it on the next of the multi-byte character. */
3887 col += bytelen;
3888 if (yanklen)
3889 {
3890 curwin->w_cursor.col += bytelen;
3891 curbuf->b_op_end.col += bytelen;
3892 }
3893 }
3894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 ++col;
3897 if (yanklen)
3898 {
3899 ++curwin->w_cursor.col;
3900 ++curbuf->b_op_end.col;
3901 }
3902 }
3903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 curbuf->b_op_start = curwin->w_cursor;
3905 }
3906 /*
3907 * Line mode: BACKWARD is the same as FORWARD on the previous line
3908 */
3909 else if (dir == BACKWARD)
3910 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003911 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912
3913 /*
3914 * simple case: insert into current line
3915 */
3916 if (y_type == MCHAR && y_size == 1)
3917 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003918 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003919
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003920 if (VIsual_active)
3921 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003922 end_lnum = curbuf->b_visual.vi_end.lnum;
3923 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3924 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003925 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003926
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003927 do {
3928 totlen = count * yanklen;
3929 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003931 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003932 if (VIsual_active && col > (int)STRLEN(oldp))
3933 {
3934 lnum++;
3935 continue;
3936 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003937 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3938 if (newp == NULL)
3939 goto end; /* alloc() gave an error message */
3940 mch_memmove(newp, oldp, (size_t)col);
3941 ptr = newp + col;
3942 for (i = 0; i < count; ++i)
3943 {
3944 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3945 ptr += yanklen;
3946 }
3947 STRMOVE(ptr, oldp + col);
3948 ml_replace(lnum, newp, FALSE);
3949 /* Place cursor on last putted char. */
3950 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003951 {
3952 /* make sure curwin->w_virtcol is updated */
3953 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003954 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003957 if (VIsual_active)
3958 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003959 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003960
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003961 if (VIsual_active) /* reset lnum to the last visual line */
3962 lnum--;
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 curbuf->b_op_end = curwin->w_cursor;
3965 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3966 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3967 ++curwin->w_cursor.col;
3968 changed_bytes(lnum, col);
3969 }
3970 else
3971 {
3972 /*
3973 * Insert at least one line. When y_type is MCHAR, break the first
3974 * line in two.
3975 */
3976 for (cnt = 1; cnt <= count; ++cnt)
3977 {
3978 i = 0;
3979 if (y_type == MCHAR)
3980 {
3981 /*
3982 * Split the current line in two at the insert position.
3983 * First insert y_array[size - 1] in front of second line.
3984 * Then append y_array[0] to first line.
3985 */
3986 lnum = new_cursor.lnum;
3987 ptr = ml_get(lnum) + col;
3988 totlen = (int)STRLEN(y_array[y_size - 1]);
3989 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3990 if (newp == NULL)
3991 goto error;
3992 STRCPY(newp, y_array[y_size - 1]);
3993 STRCAT(newp, ptr);
3994 /* insert second line */
3995 ml_append(lnum, newp, (colnr_T)0, FALSE);
3996 vim_free(newp);
3997
3998 oldp = ml_get(lnum);
3999 newp = alloc_check((unsigned)(col + yanklen + 1));
4000 if (newp == NULL)
4001 goto error;
4002 /* copy first part of line */
4003 mch_memmove(newp, oldp, (size_t)col);
4004 /* append to first line */
4005 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4006 ml_replace(lnum, newp, FALSE);
4007
4008 curwin->w_cursor.lnum = lnum;
4009 i = 1;
4010 }
4011
4012 for (; i < y_size; ++i)
4013 {
4014 if ((y_type != MCHAR || i < y_size - 1)
4015 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4016 == FAIL)
4017 goto error;
4018 lnum++;
4019 ++nr_lines;
4020 if (flags & PUT_FIXINDENT)
4021 {
4022 old_pos = curwin->w_cursor;
4023 curwin->w_cursor.lnum = lnum;
4024 ptr = ml_get(lnum);
4025 if (cnt == count && i == y_size - 1)
4026 lendiff = (int)STRLEN(ptr);
4027#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4028 if (*ptr == '#' && preprocs_left())
4029 indent = 0; /* Leave # lines at start */
4030 else
4031#endif
4032 if (*ptr == NUL)
4033 indent = 0; /* Ignore empty lines */
4034 else if (first_indent)
4035 {
4036 indent_diff = orig_indent - get_indent();
4037 indent = orig_indent;
4038 first_indent = FALSE;
4039 }
4040 else if ((indent = get_indent() + indent_diff) < 0)
4041 indent = 0;
4042 (void)set_indent(indent, 0);
4043 curwin->w_cursor = old_pos;
4044 /* remember how many chars were removed */
4045 if (cnt == count && i == y_size - 1)
4046 lendiff -= (int)STRLEN(ml_get(lnum));
4047 }
4048 }
4049 }
4050
4051error:
4052 /* Adjust marks. */
4053 if (y_type == MLINE)
4054 {
4055 curbuf->b_op_start.col = 0;
4056 if (dir == FORWARD)
4057 curbuf->b_op_start.lnum++;
4058 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004059 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004060 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004061 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004062 < curbuf->b_ml.ml_line_count
4063#ifdef FEAT_DIFF
4064 || curwin->w_p_diff
4065#endif
4066 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004067 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 (linenr_T)MAXLNUM, nr_lines, 0L);
4069
4070 /* note changed text for displaying and folding */
4071 if (y_type == MCHAR)
4072 changed_lines(curwin->w_cursor.lnum, col,
4073 curwin->w_cursor.lnum + 1, nr_lines);
4074 else
4075 changed_lines(curbuf->b_op_start.lnum, 0,
4076 curbuf->b_op_start.lnum, nr_lines);
4077
4078 /* put '] mark at last inserted character */
4079 curbuf->b_op_end.lnum = lnum;
4080 /* correct length for change in indent */
4081 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4082 if (col > 1)
4083 curbuf->b_op_end.col = col - 1;
4084 else
4085 curbuf->b_op_end.col = 0;
4086
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004087 if (flags & PUT_CURSLINE)
4088 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004089 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004090 curwin->w_cursor.lnum = lnum;
4091 beginline(BL_WHITE | BL_FIX);
4092 }
4093 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 /* put cursor after inserted text */
4096 if (y_type == MLINE)
4097 {
4098 if (lnum >= curbuf->b_ml.ml_line_count)
4099 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4100 else
4101 curwin->w_cursor.lnum = lnum + 1;
4102 curwin->w_cursor.col = 0;
4103 }
4104 else
4105 {
4106 curwin->w_cursor.lnum = lnum;
4107 curwin->w_cursor.col = col;
4108 }
4109 }
4110 else if (y_type == MLINE)
4111 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004112 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 curwin->w_cursor.col = 0;
4114 if (dir == FORWARD)
4115 ++curwin->w_cursor.lnum;
4116 beginline(BL_WHITE | BL_FIX);
4117 }
4118 else /* put cursor on first inserted character */
4119 curwin->w_cursor = new_cursor;
4120 }
4121 }
4122
4123 msgmore(nr_lines);
4124 curwin->w_set_curswant = TRUE;
4125
4126end:
4127 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004129 if (regname == '=')
4130 vim_free(y_array);
4131
Bram Moolenaar033d8882013-09-25 23:24:57 +02004132 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004133
Bram Moolenaar677ee682005-01-27 14:41:15 +00004134 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004135 adjust_cursor_eol();
4136}
4137
4138/*
4139 * When the cursor is on the NUL past the end of the line and it should not be
4140 * there move it left.
4141 */
4142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004143adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004144{
4145 if (curwin->w_cursor.col > 0
4146 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004147#ifdef FEAT_VIRTUALEDIT
4148 && (ve_flags & VE_ONEMORE) == 0
4149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 && !(restart_edit || (State & INSERT)))
4151 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004152 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004153 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#ifdef FEAT_VIRTUALEDIT
4156 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004157 {
4158 colnr_T scol, ecol;
4159
4160 /* Coladd is set to the width of the last character. */
4161 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4162 curwin->w_cursor.coladd = ecol - scol + 1;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#endif
4165 }
4166}
4167
4168#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4169/*
4170 * Return TRUE if lines starting with '#' should be left aligned.
4171 */
4172 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004173preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 return
4176# ifdef FEAT_SMARTINDENT
4177# ifdef FEAT_CINDENT
4178 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4179# else
4180 curbuf->b_p_si
4181# endif
4182# endif
4183# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004184 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4185 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186# endif
4187 ;
4188}
4189#endif
4190
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004191/*
4192 * Return the character name of the register with the given number.
4193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004195get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 if (num == -1)
4198 return '"';
4199 else if (num < 10)
4200 return num + '0';
4201 else if (num == DELETION_REGISTER)
4202 return '-';
4203#ifdef FEAT_CLIPBOARD
4204 else if (num == STAR_REGISTER)
4205 return '*';
4206 else if (num == PLUS_REGISTER)
4207 return '+';
4208#endif
4209 else
4210 {
4211#ifdef EBCDIC
4212 int i;
4213
4214 /* EBCDIC is really braindead ... */
4215 i = 'a' + (num - 10);
4216 if (i > 'i')
4217 i += 7;
4218 if (i > 'r')
4219 i += 8;
4220 return i;
4221#else
4222 return num + 'a' - 10;
4223#endif
4224 }
4225}
4226
4227/*
4228 * ":dis" and ":registers": Display the contents of the yank registers.
4229 */
4230 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004231ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004233 int i, n;
4234 long j;
4235 char_u *p;
4236 yankreg_T *yb;
4237 int name;
4238 int attr;
4239 char_u *arg = eap->arg;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004240 int clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 if (arg != NULL && *arg == NUL)
4243 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004244 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004247 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4249 {
4250 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004251 if (arg != NULL && vim_strchr(arg, name) == NULL
4252#ifdef ONE_CLIPBOARD
4253 /* Star register and plus register contain the same thing. */
4254 && (name != '*' || vim_strchr(arg, '+') == NULL)
4255#endif
4256 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 continue; /* did not ask for this register */
4258
4259#ifdef FEAT_CLIPBOARD
4260 /* Adjust register name for "unnamed" in 'clipboard'.
4261 * When it's a clipboard register, fill it with the current contents
4262 * of the clipboard. */
4263 adjust_clip_reg(&name);
4264 (void)may_get_selection(name);
4265#endif
4266
4267 if (i == -1)
4268 {
4269 if (y_previous != NULL)
4270 yb = y_previous;
4271 else
4272 yb = &(y_regs[0]);
4273 }
4274 else
4275 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004276
4277#ifdef FEAT_EVAL
4278 if (name == MB_TOLOWER(redir_reg)
4279 || (redir_reg == '"' && yb == y_previous))
4280 continue; /* do not list register being written to, the
4281 * pointer can be freed */
4282#endif
4283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (yb->y_array != NULL)
4285 {
4286 msg_putchar('\n');
4287 msg_putchar('"');
4288 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004289 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 n = (int)Columns - 6;
4292 for (j = 0; j < yb->y_size && n > 1; ++j)
4293 {
4294 if (j)
4295 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 n -= 2;
4298 }
4299 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4300 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004301 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004302 msg_outtrans_len(p, clen);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004303 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004307 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 out_flush(); /* show one line at a time */
4309 }
4310 ui_breakcheck();
4311 }
4312
4313 /*
4314 * display last inserted text
4315 */
4316 if ((p = get_last_insert()) != NULL
4317 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4318 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004319 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 dis_msg(p, TRUE);
4321 }
4322
4323 /*
4324 * display last command line
4325 */
4326 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4327 && !got_int)
4328 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004329 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 dis_msg(last_cmdline, FALSE);
4331 }
4332
4333 /*
4334 * display current file name
4335 */
4336 if (curbuf->b_fname != NULL
4337 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4338 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004339 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 dis_msg(curbuf->b_fname, FALSE);
4341 }
4342
4343 /*
4344 * display alternate file name
4345 */
4346 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4347 {
4348 char_u *fname;
4349 linenr_T dummy;
4350
4351 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4352 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004353 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 dis_msg(fname, FALSE);
4355 }
4356 }
4357
4358 /*
4359 * display last search pattern
4360 */
4361 if (last_search_pat() != NULL
4362 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4363 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004364 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 dis_msg(last_search_pat(), FALSE);
4366 }
4367
4368#ifdef FEAT_EVAL
4369 /*
4370 * display last used expression
4371 */
4372 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4373 && !got_int)
4374 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004375 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 dis_msg(expr_line, FALSE);
4377 }
4378#endif
4379}
4380
4381/*
4382 * display a string for do_dis()
4383 * truncate at end of screen line
4384 */
4385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004386dis_msg(
4387 char_u *p,
4388 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 n = (int)Columns - 6;
4394 while (*p != NUL
4395 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4396 && (n -= ptr2cells(p)) >= 0)
4397 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004398 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 msg_outtrans_len(p, l);
4401 p += l;
4402 }
4403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 msg_outtrans_len(p++, 1);
4405 }
4406 ui_breakcheck();
4407}
4408
Bram Moolenaar81340392012-06-06 16:12:59 +02004409#if defined(FEAT_COMMENTS) || defined(PROTO)
4410/*
4411 * If "process" is TRUE and the line begins with a comment leader (possibly
4412 * after some white space), return a pointer to the text after it. Put a boolean
4413 * value indicating whether the line ends with an unclosed comment in
4414 * "is_comment".
4415 * line - line to be processed,
4416 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004417 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004418 * include_space - whether to also skip space following the comment leader,
4419 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004420 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004421 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004422 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004423skip_comment(
4424 char_u *line,
4425 int process,
4426 int include_space,
4427 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004428{
4429 char_u *comment_flags = NULL;
4430 int lead_len;
4431 int leader_offset = get_last_leader_offset(line, &comment_flags);
4432
4433 *is_comment = FALSE;
4434 if (leader_offset != -1)
4435 {
4436 /* Let's check whether the line ends with an unclosed comment.
4437 * If the last comment leader has COM_END in flags, there's no comment.
4438 */
4439 while (*comment_flags)
4440 {
4441 if (*comment_flags == COM_END
4442 || *comment_flags == ':')
4443 break;
4444 ++comment_flags;
4445 }
4446 if (*comment_flags != COM_END)
4447 *is_comment = TRUE;
4448 }
4449
4450 if (process == FALSE)
4451 return line;
4452
4453 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4454
4455 if (lead_len == 0)
4456 return line;
4457
4458 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004459 * - COM_END,
4460 * - colon,
4461 * whichever comes first.
4462 */
4463 while (*comment_flags)
4464 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004465 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004466 || *comment_flags == ':')
4467 {
4468 break;
4469 }
4470 ++comment_flags;
4471 }
4472
4473 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004474 * starting with a closing part of a three-part comment. That's good,
4475 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004476 */
4477 if (*comment_flags == ':' || *comment_flags == NUL)
4478 line += lead_len;
4479
4480 return line;
4481}
4482#endif
4483
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004485 * Join 'count' lines (minimal 2) at cursor position.
4486 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004487 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4488 * leaders should not be removed.
4489 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4490 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004492 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 */
4494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004495do_join(
4496 long count,
4497 int insert_space,
4498 int save_undo,
4499 int use_formatoptions UNUSED,
4500 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004503 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004504 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004506 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004507 int endcurr1 = NUL;
4508 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004509 int currsize = 0; /* size of the current line */
4510 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004512 colnr_T col = 0;
4513 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004514#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004515 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004516 int remove_comments = (use_formatoptions == TRUE)
4517 && has_format_option(FO_REMOVE_COMS);
4518 int prev_was_comment;
4519#endif
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004522 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4523 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004526 /* Allocate an array to store the number of spaces inserted before each
4527 * line. We will use it to pre-compute the length of the new line and the
4528 * proper placement of each original line in the new one. */
4529 spaces = lalloc_clear((long_u)count, TRUE);
4530 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004532#if defined(FEAT_COMMENTS) || defined(PROTO)
4533 if (remove_comments)
4534 {
4535 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4536 if (comments == NULL)
4537 {
4538 vim_free(spaces);
4539 return FAIL;
4540 }
4541 }
4542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
4544 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004545 * Don't move anything, just compute the final line length
4546 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004548 for (t = 0; t < count; ++t)
4549 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004550 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004551 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004552 {
4553 /* Set the '[ mark. */
4554 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4555 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4556 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004557#if defined(FEAT_COMMENTS) || defined(PROTO)
4558 if (remove_comments)
4559 {
4560 /* We don't want to remove the comment leader if the
4561 * previous line is not a comment. */
4562 if (t > 0 && prev_was_comment)
4563 {
4564
4565 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4566 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004567 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004568 curr = new_curr;
4569 }
4570 else
4571 curr = skip_comment(curr, FALSE, insert_space,
4572 &prev_was_comment);
4573 }
4574#endif
4575
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004576 if (insert_space && t > 0)
4577 {
4578 curr = skipwhite(curr);
4579 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004580 && (!has_format_option(FO_MBYTE_JOIN)
4581 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4582 && (!has_format_option(FO_MBYTE_JOIN2)
4583 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004584 )
4585 {
4586 /* don't add a space if the line is ending in a space */
4587 if (endcurr1 == ' ')
4588 endcurr1 = endcurr2;
4589 else
4590 ++spaces[t];
4591 /* extra space when 'joinspaces' set and line ends in '.' */
4592 if ( p_js
4593 && (endcurr1 == '.'
4594 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4595 && (endcurr1 == '?' || endcurr1 == '!'))))
4596 ++spaces[t];
4597 }
4598 }
4599 currsize = (int)STRLEN(curr);
4600 sumsize += currsize + spaces[t];
4601 endcurr1 = endcurr2 = NUL;
4602 if (insert_space && currsize > 0)
4603 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004604 if (has_mbyte)
4605 {
4606 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004607 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004608 endcurr1 = (*mb_ptr2char)(cend);
4609 if (cend > curr)
4610 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004611 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004612 endcurr2 = (*mb_ptr2char)(cend);
4613 }
4614 }
4615 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004616 {
4617 endcurr1 = *(curr + currsize - 1);
4618 if (currsize > 1)
4619 endcurr2 = *(curr + currsize - 2);
4620 }
4621 }
4622 line_breakcheck();
4623 if (got_int)
4624 {
4625 ret = FAIL;
4626 goto theend;
4627 }
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004630 /* store the column position before last line */
4631 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004633 /* allocate the space for the new line */
4634 newp = alloc_check((unsigned)(sumsize + 1));
4635 cend = newp + sumsize;
4636 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004638 /*
4639 * Move affected lines to the new long one.
4640 *
4641 * Move marks from each deleted line to the joined line, adjusting the
4642 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4643 * should not really be a problem.
4644 */
4645 for (t = count - 1; ; --t)
4646 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004647 int spaces_removed;
4648
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004649 cend -= currsize;
4650 mch_memmove(cend, curr, (size_t)currsize);
4651 if (spaces[t] > 0)
4652 {
4653 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004654 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004655 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004656
4657 // If deleting more spaces than adding, the cursor moves no more than
4658 // what is added if it is inside these spaces.
4659 spaces_removed = (curr - curr_start) - spaces[t];
4660
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004661 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004662 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004663 if (t == 0)
4664 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004665 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004666#if defined(FEAT_COMMENTS) || defined(PROTO)
4667 if (remove_comments)
4668 curr += comments[t - 1];
4669#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004670 if (insert_space && t > 1)
4671 curr = skipwhite(curr);
4672 currsize = (int)STRLEN(curr);
4673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4675
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004676 if (setmark)
4677 {
4678 /* Set the '] mark. */
4679 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4680 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4681 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 /* Only report the change in the first line here, del_lines() will report
4684 * the deleted line. */
4685 changed_lines(curwin->w_cursor.lnum, currsize,
4686 curwin->w_cursor.lnum + 1, 0L);
4687
4688 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004689 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 * briefly, and then move it back. After del_lines() the cursor may
4691 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 */
4693 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004695 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 curwin->w_cursor.lnum = t;
4697
4698 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004699 * Set the cursor column:
4700 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004701 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004703 curwin->w_cursor.col =
4704 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707#ifdef FEAT_VIRTUALEDIT
4708 curwin->w_cursor.coladd = 0;
4709#endif
4710 curwin->w_set_curswant = TRUE;
4711
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004712theend:
4713 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004714#if defined(FEAT_COMMENTS) || defined(PROTO)
4715 if (remove_comments)
4716 vim_free(comments);
4717#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004718 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719}
4720
4721#ifdef FEAT_COMMENTS
4722/*
4723 * Return TRUE if the two comment leaders given are the same. "lnum" is
4724 * the first line. White-space is ignored. Note that the whole of
4725 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4726 */
4727 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004728same_leader(
4729 linenr_T lnum,
4730 int leader1_len,
4731 char_u *leader1_flags,
4732 int leader2_len,
4733 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 int idx1 = 0, idx2 = 0;
4736 char_u *p;
4737 char_u *line1;
4738 char_u *line2;
4739
4740 if (leader1_len == 0)
4741 return (leader2_len == 0);
4742
4743 /*
4744 * If first leader has 'f' flag, the lines can be joined only if the
4745 * second line does not have a leader.
4746 * If first leader has 'e' flag, the lines can never be joined.
4747 * If fist leader has 's' flag, the lines can only be joined if there is
4748 * some text after it and the second line has the 'm' flag.
4749 */
4750 if (leader1_flags != NULL)
4751 {
4752 for (p = leader1_flags; *p && *p != ':'; ++p)
4753 {
4754 if (*p == COM_FIRST)
4755 return (leader2_len == 0);
4756 if (*p == COM_END)
4757 return FALSE;
4758 if (*p == COM_START)
4759 {
4760 if (*(ml_get(lnum) + leader1_len) == NUL)
4761 return FALSE;
4762 if (leader2_flags == NULL || leader2_len == 0)
4763 return FALSE;
4764 for (p = leader2_flags; *p && *p != ':'; ++p)
4765 if (*p == COM_MIDDLE)
4766 return TRUE;
4767 return FALSE;
4768 }
4769 }
4770 }
4771
4772 /*
4773 * Get current line and next line, compare the leaders.
4774 * The first line has to be saved, only one line can be locked at a time.
4775 */
4776 line1 = vim_strsave(ml_get(lnum));
4777 if (line1 != NULL)
4778 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004779 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 ;
4781 line2 = ml_get(lnum + 1);
4782 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4783 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004784 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 if (line1[idx1++] != line2[idx2])
4787 break;
4788 }
4789 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004790 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 ++idx1;
4792 }
4793 vim_free(line1);
4794 }
4795 return (idx2 == leader2_len && idx1 == leader1_len);
4796}
4797#endif
4798
4799/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004800 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
4802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004803op_format(
4804 oparg_T *oap,
4805 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 long old_line_count = curbuf->b_ml.ml_line_count;
4808
4809 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4810 * can put it back there. */
4811 curwin->w_cursor = oap->cursor_start;
4812
4813 if (u_save((linenr_T)(oap->start.lnum - 1),
4814 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4815 return;
4816 curwin->w_cursor = oap->start;
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (oap->is_VIsual)
4819 /* When there is no change: need to remove the Visual selection */
4820 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822 /* Set '[ mark at the start of the formatted area */
4823 curbuf->b_op_start = oap->start;
4824
4825 /* For "gw" remember the cursor position and put it back below (adjusted
4826 * for joined and split lines). */
4827 if (keep_cursor)
4828 saved_cursor = oap->cursor_start;
4829
Bram Moolenaar81a82092008-03-12 16:27:00 +00004830 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 /*
4833 * Leave the cursor at the first non-blank of the last formatted line.
4834 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4835 * line, so "." will do the next lines.
4836 */
4837 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4838 ++curwin->w_cursor.lnum;
4839 beginline(BL_WHITE | BL_FIX);
4840 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4841 msgmore(old_line_count);
4842
4843 /* put '] mark on the end of the formatted area */
4844 curbuf->b_op_end = curwin->w_cursor;
4845
4846 if (keep_cursor)
4847 {
4848 curwin->w_cursor = saved_cursor;
4849 saved_cursor.lnum = 0;
4850 }
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (oap->is_VIsual)
4853 {
4854 win_T *wp;
4855
4856 FOR_ALL_WINDOWS(wp)
4857 {
4858 if (wp->w_old_cursor_lnum != 0)
4859 {
4860 /* When lines have been inserted or deleted, adjust the end of
4861 * the Visual area to be redrawn. */
4862 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4863 wp->w_old_cursor_lnum += old_line_count;
4864 else
4865 wp->w_old_visual_lnum += old_line_count;
4866 }
4867 }
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869}
4870
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004871#if defined(FEAT_EVAL) || defined(PROTO)
4872/*
4873 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4874 */
4875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004876op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004877{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004878 if (oap->is_VIsual)
4879 /* When there is no change: need to remove the Visual selection */
4880 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004881
Bram Moolenaar700303e2010-07-11 17:35:50 +02004882 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4883 /* As documented: when 'formatexpr' returns non-zero fall back to
4884 * internal formatting. */
4885 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004886}
4887
4888 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004889fex_format(
4890 linenr_T lnum,
4891 long count,
4892 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004893{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004894 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4895 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004896 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004897 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004898
4899 /*
4900 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004901 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004902 */
4903 set_vim_var_nr(VV_LNUM, lnum);
4904 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004905 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004906
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004907 /* Make a copy, the option could be changed while calling it. */
4908 fex = vim_strsave(curbuf->b_p_fex);
4909 if (fex == NULL)
4910 return 0;
4911
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004912 /*
4913 * Evaluate the function.
4914 */
4915 if (use_sandbox)
4916 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004917 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004918 if (use_sandbox)
4919 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004920
4921 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004922 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004924 return r;
4925}
4926#endif
4927
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928/*
4929 * Format "line_count" lines, starting at the cursor position.
4930 * When "line_count" is negative, format until the end of the paragraph.
4931 * Lines after the cursor line are saved for undo, caller must have saved the
4932 * first line.
4933 */
4934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935format_lines(
4936 linenr_T line_count,
4937 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 int max_len;
4940 int is_not_par; /* current line not part of parag. */
4941 int next_is_not_par; /* next line not part of paragraph */
4942 int is_end_par; /* at end of paragraph */
4943 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4944 int next_is_start_par = FALSE;
4945#ifdef FEAT_COMMENTS
4946 int leader_len = 0; /* leader len of current line */
4947 int next_leader_len; /* leader len of next line */
4948 char_u *leader_flags = NULL; /* flags for leader of current line */
4949 char_u *next_leader_flags; /* flags for leader of next line */
4950 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004951 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#endif
4953 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004954 int second_indent = -1; /* indent for second line (comment
4955 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 int do_second_indent;
4957 int do_number_indent;
4958 int do_trail_white;
4959 int first_par_line = TRUE;
4960 int smd_save;
4961 long count;
4962 int need_set_indent = TRUE; /* set indent of next paragraph */
4963 int force_format = FALSE;
4964 int old_State = State;
4965
4966 /* length of a line to force formatting: 3 * 'tw' */
4967 max_len = comp_textwidth(TRUE) * 3;
4968
4969 /* check for 'q', '2' and '1' in 'formatoptions' */
4970#ifdef FEAT_COMMENTS
4971 do_comments = has_format_option(FO_Q_COMS);
4972#endif
4973 do_second_indent = has_format_option(FO_Q_SECOND);
4974 do_number_indent = has_format_option(FO_Q_NUMBER);
4975 do_trail_white = has_format_option(FO_WHITE_PAR);
4976
4977 /*
4978 * Get info about the previous and current line.
4979 */
4980 if (curwin->w_cursor.lnum > 1)
4981 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4982#ifdef FEAT_COMMENTS
4983 , &leader_len, &leader_flags, do_comments
4984#endif
4985 );
4986 else
4987 is_not_par = TRUE;
4988 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4989#ifdef FEAT_COMMENTS
4990 , &next_leader_len, &next_leader_flags, do_comments
4991#endif
4992 );
4993 is_end_par = (is_not_par || next_is_not_par);
4994 if (!is_end_par && do_trail_white)
4995 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4996
4997 curwin->w_cursor.lnum--;
4998 for (count = line_count; count != 0 && !got_int; --count)
4999 {
5000 /*
5001 * Advance to next paragraph.
5002 */
5003 if (advance)
5004 {
5005 curwin->w_cursor.lnum++;
5006 prev_is_end_par = is_end_par;
5007 is_not_par = next_is_not_par;
5008#ifdef FEAT_COMMENTS
5009 leader_len = next_leader_len;
5010 leader_flags = next_leader_flags;
5011#endif
5012 }
5013
5014 /*
5015 * The last line to be formatted.
5016 */
5017 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5018 {
5019 next_is_not_par = TRUE;
5020#ifdef FEAT_COMMENTS
5021 next_leader_len = 0;
5022 next_leader_flags = NULL;
5023#endif
5024 }
5025 else
5026 {
5027 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5028#ifdef FEAT_COMMENTS
5029 , &next_leader_len, &next_leader_flags, do_comments
5030#endif
5031 );
5032 if (do_number_indent)
5033 next_is_start_par =
5034 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5035 }
5036 advance = TRUE;
5037 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5038 if (!is_end_par && do_trail_white)
5039 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5040
5041 /*
5042 * Skip lines that are not in a paragraph.
5043 */
5044 if (is_not_par)
5045 {
5046 if (line_count < 0)
5047 break;
5048 }
5049 else
5050 {
5051 /*
5052 * For the first line of a paragraph, check indent of second line.
5053 * Don't do this for comments and empty lines.
5054 */
5055 if (first_par_line
5056 && (do_second_indent || do_number_indent)
5057 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005058 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005060 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005061 {
5062#ifdef FEAT_COMMENTS
5063 if (leader_len == 0 && next_leader_len == 0)
5064 {
5065 /* no comment found */
5066#endif
5067 second_indent =
5068 get_indent_lnum(curwin->w_cursor.lnum + 1);
5069#ifdef FEAT_COMMENTS
5070 }
5071 else
5072 {
5073 second_indent = next_leader_len;
5074 do_comments_list = 1;
5075 }
5076#endif
5077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005079 {
5080#ifdef FEAT_COMMENTS
5081 if (leader_len == 0 && next_leader_len == 0)
5082 {
5083 /* no comment found */
5084#endif
5085 second_indent =
5086 get_number_indent(curwin->w_cursor.lnum);
5087#ifdef FEAT_COMMENTS
5088 }
5089 else
5090 {
5091 /* get_number_indent() is now "comment aware"... */
5092 second_indent =
5093 get_number_indent(curwin->w_cursor.lnum);
5094 do_comments_list = 1;
5095 }
5096#endif
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099
5100 /*
5101 * When the comment leader changes, it's the end of the paragraph.
5102 */
5103 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5104#ifdef FEAT_COMMENTS
5105 || !same_leader(curwin->w_cursor.lnum,
5106 leader_len, leader_flags,
5107 next_leader_len, next_leader_flags)
5108#endif
5109 )
5110 is_end_par = TRUE;
5111
5112 /*
5113 * If we have got to the end of a paragraph, or the line is
5114 * getting long, format it.
5115 */
5116 if (is_end_par || force_format)
5117 {
5118 if (need_set_indent)
5119 /* replace indent in first line with minimal number of
5120 * tabs and spaces, according to current options */
5121 (void)set_indent(get_indent(), SIN_CHANGED);
5122
5123 /* put cursor on last non-space */
5124 State = NORMAL; /* don't go past end-of-line */
5125 coladvance((colnr_T)MAXCOL);
5126 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5127 dec_cursor();
5128
5129 /* do the formatting, without 'showmode' */
5130 State = INSERT; /* for open_line() */
5131 smd_save = p_smd;
5132 p_smd = FALSE;
5133 insertchar(NUL, INSCHAR_FORMAT
5134#ifdef FEAT_COMMENTS
5135 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005136 + (do_comments && do_comments_list
5137 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005139 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 State = old_State;
5141 p_smd = smd_save;
5142 second_indent = -1;
5143 /* at end of par.: need to set indent of next par. */
5144 need_set_indent = is_end_par;
5145 if (is_end_par)
5146 {
5147 /* When called with a negative line count, break at the
5148 * end of the paragraph. */
5149 if (line_count < 0)
5150 break;
5151 first_par_line = TRUE;
5152 }
5153 force_format = FALSE;
5154 }
5155
5156 /*
5157 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005158 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 */
5160 if (!is_end_par)
5161 {
5162 advance = FALSE;
5163 curwin->w_cursor.lnum++;
5164 curwin->w_cursor.col = 0;
5165 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005169 {
5170 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005172 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005173 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005175 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5176 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005177 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005178
5179 if (indent > 0)
5180 {
5181 (void)del_bytes(indent, FALSE, FALSE);
5182 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005183 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005184 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005187 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 beep_flush();
5190 break;
5191 }
5192 first_par_line = FALSE;
5193 /* If the line is getting long, format it next time */
5194 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5195 force_format = TRUE;
5196 else
5197 force_format = FALSE;
5198 }
5199 }
5200 line_breakcheck();
5201 }
5202}
5203
5204/*
5205 * Return TRUE if line "lnum" ends in a white character.
5206 */
5207 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005208ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 char_u *s = ml_get(lnum);
5211 size_t l;
5212
5213 if (*s == NUL)
5214 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005215 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * invocation may call function multiple times". */
5217 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005218 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
5220
5221/*
5222 * Blank lines, and lines containing only the comment leader, are left
5223 * untouched by the formatting. The function returns TRUE in this
5224 * case. It also returns TRUE when a line starts with the end of a comment
5225 * ('e' in comment flags), so that this line is skipped, and not joined to the
5226 * previous line. A new paragraph starts after a blank line, or when the
5227 * comment leader changes -- webb.
5228 */
5229#ifdef FEAT_COMMENTS
5230 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005231fmt_check_par(
5232 linenr_T lnum,
5233 int *leader_len,
5234 char_u **leader_flags,
5235 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 char_u *flags = NULL; /* init for GCC */
5238 char_u *ptr;
5239
5240 ptr = ml_get(lnum);
5241 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005242 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
5244 *leader_len = 0;
5245
5246 if (*leader_len > 0)
5247 {
5248 /*
5249 * Search for 'e' flag in comment leader flags.
5250 */
5251 flags = *leader_flags;
5252 while (*flags && *flags != ':' && *flags != COM_END)
5253 ++flags;
5254 }
5255
5256 return (*skipwhite(ptr + *leader_len) == NUL
5257 || (*leader_len > 0 && *flags == COM_END)
5258 || startPS(lnum, NUL, FALSE));
5259}
5260#else
5261 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5265}
5266#endif
5267
5268/*
5269 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5270 * previous line is in the same paragraph. Used for auto-formatting.
5271 */
5272 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *p;
5276#ifdef FEAT_COMMENTS
5277 int leader_len = 0; /* leader len of current line */
5278 char_u *leader_flags = NULL; /* flags for leader of current line */
5279 int next_leader_len; /* leader len of next line */
5280 char_u *next_leader_flags; /* flags for leader of next line */
5281 int do_comments; /* format comments */
5282#endif
5283
5284 if (lnum <= 1)
5285 return TRUE; /* start of the file */
5286
5287 p = ml_get(lnum - 1);
5288 if (*p == NUL)
5289 return TRUE; /* after empty line */
5290
5291#ifdef FEAT_COMMENTS
5292 do_comments = has_format_option(FO_Q_COMS);
5293#endif
5294 if (fmt_check_par(lnum - 1
5295#ifdef FEAT_COMMENTS
5296 , &leader_len, &leader_flags, do_comments
5297#endif
5298 ))
5299 return TRUE; /* after non-paragraph line */
5300
5301 if (fmt_check_par(lnum
5302#ifdef FEAT_COMMENTS
5303 , &next_leader_len, &next_leader_flags, do_comments
5304#endif
5305 ))
5306 return TRUE; /* "lnum" is not a paragraph line */
5307
5308 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5309 return TRUE; /* missing trailing space in previous line. */
5310
5311 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5312 return TRUE; /* numbered item starts in "lnum". */
5313
5314#ifdef FEAT_COMMENTS
5315 if (!same_leader(lnum - 1, leader_len, leader_flags,
5316 next_leader_len, next_leader_flags))
5317 return TRUE; /* change of comment leader. */
5318#endif
5319
5320 return FALSE;
5321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
5324 * prepare a few things for block mode yank/delete/tilde
5325 *
5326 * for delete:
5327 * - textlen includes the first/last char to be (partly) deleted
5328 * - start/endspaces is the number of columns that are taken by the
5329 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005330 * deleted.
5331 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 * - textlen includes the first/last char to be wholly yanked
5333 * - start/endspaces is the number of columns of the first/last yanked char
5334 * that are to be yanked.
5335 */
5336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005337block_prep(
5338 oparg_T *oap,
5339 struct block_def *bdp,
5340 linenr_T lnum,
5341 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 int incr = 0;
5344 char_u *pend;
5345 char_u *pstart;
5346 char_u *line;
5347 char_u *prev_pstart;
5348 char_u *prev_pend;
5349
5350 bdp->startspaces = 0;
5351 bdp->endspaces = 0;
5352 bdp->textlen = 0;
5353 bdp->start_vcol = 0;
5354 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 bdp->is_short = FALSE;
5356 bdp->is_oneChar = FALSE;
5357 bdp->pre_whitesp = 0;
5358 bdp->pre_whitesp_c = 0;
5359 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 bdp->start_char_vcols = 0;
5361
5362 line = ml_get(lnum);
5363 pstart = line;
5364 prev_pstart = line;
5365 while (bdp->start_vcol < oap->start_vcol && *pstart)
5366 {
5367 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005368 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005370 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 bdp->pre_whitesp += incr;
5373 bdp->pre_whitesp_c++;
5374 }
5375 else
5376 {
5377 bdp->pre_whitesp = 0;
5378 bdp->pre_whitesp_c = 0;
5379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005381 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 bdp->start_char_vcols = incr;
5384 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5385 {
5386 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 if (!is_del || oap->op_type == OP_APPEND)
5389 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5390 }
5391 else
5392 {
5393 /* notice: this converts partly selected Multibyte characters to
5394 * spaces, too. */
5395 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5396 if (is_del && bdp->startspaces)
5397 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5398 pend = pstart;
5399 bdp->end_vcol = bdp->start_vcol;
5400 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 if (oap->op_type == OP_INSERT)
5404 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5405 else if (oap->op_type == OP_APPEND)
5406 {
5407 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5408 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5409 }
5410 else
5411 {
5412 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5413 if (is_del && oap->op_type != OP_LSHIFT)
5414 {
5415 /* just putting the sum of those two into
5416 * bdp->startspaces doesn't work for Visual replace,
5417 * so we have to split the tab in two */
5418 bdp->startspaces = bdp->start_char_vcols
5419 - (bdp->start_vcol - oap->start_vcol);
5420 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5421 }
5422 }
5423 }
5424 else
5425 {
5426 prev_pend = pend;
5427 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5428 {
5429 /* Count a tab for what it's worth (if list mode not on) */
5430 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005431 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 bdp->end_vcol += incr;
5433 }
5434 if (bdp->end_vcol <= oap->end_vcol
5435 && (!is_del
5436 || oap->op_type == OP_APPEND
5437 || oap->op_type == OP_REPLACE)) /* line too short */
5438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 /* Alternative: include spaces to fill up the block.
5441 * Disadvantage: can lead to trailing spaces when the line is
5442 * short where the text is put */
5443 /* if (!is_del || oap->op_type == OP_APPEND) */
5444 if (oap->op_type == OP_APPEND || virtual_op)
5445 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005446 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 else
5448 bdp->endspaces = 0; /* replace doesn't add characters */
5449 }
5450 else if (bdp->end_vcol > oap->end_vcol)
5451 {
5452 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5453 if (!is_del && bdp->endspaces)
5454 {
5455 bdp->endspaces = incr - bdp->endspaces;
5456 if (pend != pstart)
5457 pend = prev_pend;
5458 }
5459 }
5460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (is_del && bdp->startspaces)
5463 pstart = prev_pstart;
5464 bdp->textlen = (int)(pend - pstart);
5465 }
5466 bdp->textcol = (colnr_T) (pstart - line);
5467 bdp->textstart = pstart;
5468}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005471 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474op_addsub(
5475 oparg_T *oap,
5476 linenr_T Prenum1, /* Amount of add/subtract */
5477 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005479 pos_T pos;
5480 struct block_def bd;
5481 int change_cnt = 0;
5482 linenr_T amount = Prenum1;
5483
Bram Moolenaar6b731882018-11-16 20:54:47 +01005484 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
5485 // buffer is not completly updated yet. Postpone updating folds until before
5486 // the call to changed_lines().
5487#ifdef FEAT_FOLDING
5488 disable_fold_update++;
5489#endif
5490
Bram Moolenaard79e5502016-01-10 22:13:02 +01005491 if (!VIsual_active)
5492 {
5493 pos = curwin->w_cursor;
5494 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005495 {
5496#ifdef FEAT_FOLDING
5497 disable_fold_update--;
5498#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005499 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005500 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005501 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005502#ifdef FEAT_FOLDING
5503 disable_fold_update--;
5504#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005505 if (change_cnt)
5506 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5507 }
5508 else
5509 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005510 int one_change;
5511 int length;
5512 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005513
5514 if (u_save((linenr_T)(oap->start.lnum - 1),
5515 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005516 {
5517#ifdef FEAT_FOLDING
5518 disable_fold_update--;
5519#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005520 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005521 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005522
5523 pos = oap->start;
5524 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5525 {
5526 if (oap->block_mode) /* Visual block mode */
5527 {
5528 block_prep(oap, &bd, pos.lnum, FALSE);
5529 pos.col = bd.textcol;
5530 length = bd.textlen;
5531 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005532 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005533 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005534 curwin->w_cursor.col = 0;
5535 pos.col = 0;
5536 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5537 }
5538 else /* oap->motion_type == MCHAR */
5539 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005540 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005541 dec(&(oap->end));
5542 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5543 pos.col = 0;
5544 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005545 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005546 pos.col += oap->start.col;
5547 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005548 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005549 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005550 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005551 length = (int)STRLEN(ml_get(oap->end.lnum));
5552 if (oap->end.col >= length)
5553 oap->end.col = length - 1;
5554 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005555 }
5556 }
5557 one_change = do_addsub(oap->op_type, &pos, length, amount);
5558 if (one_change)
5559 {
5560 /* Remember the start position of the first change. */
5561 if (change_cnt == 0)
5562 startpos = curbuf->b_op_start;
5563 ++change_cnt;
5564 }
5565
5566#ifdef FEAT_NETBEANS_INTG
5567 if (netbeans_active() && one_change)
5568 {
5569 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5570
5571 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5572 netbeans_inserted(curbuf, pos.lnum, pos.col,
5573 &ptr[pos.col], length);
5574 }
5575#endif
5576 if (g_cmd && one_change)
5577 amount += Prenum1;
5578 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005579
5580#ifdef FEAT_FOLDING
5581 disable_fold_update--;
5582#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005583 if (change_cnt)
5584 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5585
5586 if (!change_cnt && oap->is_VIsual)
5587 /* No change: need to remove the Visual selection */
5588 redraw_curbuf_later(INVERTED);
5589
5590 /* Set '[ mark if something changed. Keep the last end
5591 * position from do_addsub(). */
5592 if (change_cnt > 0)
5593 curbuf->b_op_start = startpos;
5594
5595 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005596 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005597 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005598 }
5599}
5600
5601/*
5602 * Add or subtract 'Prenum1' from a number in a line
5603 * op_type is OP_NR_ADD or OP_NR_SUB
5604 *
5605 * Returns TRUE if some character was changed.
5606 */
5607 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005608do_addsub(
5609 int op_type,
5610 pos_T *pos,
5611 int length,
5612 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005613{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 int col;
5615 char_u *buf1;
5616 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005617 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005619 uvarnumber_T n;
5620 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 char_u *ptr;
5622 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 int todel;
5624 int dohex;
5625 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005626 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 int doalp;
5628 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005630 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005631 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005632 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005633 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005634 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005635 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005636 pos_T startpos;
5637 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5640 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005641 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5643
Bram Moolenaard79e5502016-01-10 22:13:02 +01005644 curwin->w_cursor = *pos;
5645 ptr = ml_get(pos->lnum);
5646 col = pos->col;
5647
5648 if (*ptr == NUL)
5649 goto theend;
5650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 /*
5652 * First check if we are on a hexadecimal number, after the "0x".
5653 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005654 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005656 if (dobin)
5657 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005658 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005659 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005660 if (has_mbyte)
5661 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005662 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005663
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005664 if (dohex)
5665 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005666 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005667 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005668 if (has_mbyte)
5669 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005670 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005671
5672 if ( dobin
5673 && dohex
5674 && ! ((col > 0
5675 && (ptr[col] == 'X'
5676 || ptr[col] == 'x')
5677 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005678 && (!has_mbyte ||
5679 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005680 && vim_isxdigit(ptr[col + 1]))))
5681 {
5682
5683 /* In case of binary/hexadecimal pattern overlap match, rescan */
5684
Bram Moolenaard79e5502016-01-10 22:13:02 +01005685 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005686
5687 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005688 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005689 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005690 if (has_mbyte)
5691 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005692 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005693 }
5694
5695 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005696 && col > 0
5697 && (ptr[col] == 'X'
5698 || ptr[col] == 'x')
5699 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005700 && (!has_mbyte ||
5701 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005702 && vim_isxdigit(ptr[col + 1])) ||
5703 ( dobin
5704 && col > 0
5705 && (ptr[col] == 'B'
5706 || ptr[col] == 'b')
5707 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005708 && (!has_mbyte ||
5709 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005710 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005711 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005712 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005713 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005714 if (has_mbyte)
5715 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005716 }
5717 else
5718 {
5719 /*
5720 * Search forward and then backward to find the start of number.
5721 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005722 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005723
5724 while (ptr[col] != NUL
5725 && !vim_isdigit(ptr[col])
5726 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005727 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005728
5729 while (col > 0
5730 && vim_isdigit(ptr[col - 1])
5731 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005732 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005733 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005734 if (has_mbyte)
5735 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005736 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005737 }
5738 }
5739
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005740 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005741 {
5742 while (ptr[col] != NUL && length > 0
5743 && !vim_isdigit(ptr[col])
5744 && !(doalp && ASCII_ISALPHA(ptr[col])))
5745 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005746 int mb_len = MB_PTR2LEN(ptr + col);
5747
5748 col += mb_len;
5749 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005750 }
5751
5752 if (length == 0)
5753 goto theend;
5754
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005755 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005756 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005757 {
5758 negative = TRUE;
5759 was_positive = FALSE;
5760 }
5761 }
5762
5763 /*
5764 * If a number was found, and saving for undo works, replace the number.
5765 */
5766 firstdigit = ptr[col];
5767 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5768 {
5769 beep_flush();
5770 goto theend;
5771 }
5772
5773 if (doalp && ASCII_ISALPHA(firstdigit))
5774 {
5775 /* decrement or increment alphabetic character */
5776 if (op_type == OP_NR_SUB)
5777 {
5778 if (CharOrd(firstdigit) < Prenum1)
5779 {
5780 if (isupper(firstdigit))
5781 firstdigit = 'A';
5782 else
5783 firstdigit = 'a';
5784 }
5785 else
5786#ifdef EBCDIC
5787 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5788#else
5789 firstdigit -= Prenum1;
5790#endif
5791 }
5792 else
5793 {
5794 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5795 {
5796 if (isupper(firstdigit))
5797 firstdigit = 'Z';
5798 else
5799 firstdigit = 'z';
5800 }
5801 else
5802#ifdef EBCDIC
5803 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5804#else
5805 firstdigit += Prenum1;
5806#endif
5807 }
5808 curwin->w_cursor.col = col;
5809 if (!did_change)
5810 startpos = curwin->w_cursor;
5811 did_change = TRUE;
5812 (void)del_char(FALSE);
5813 ins_char(firstdigit);
5814 endpos = curwin->w_cursor;
5815 curwin->w_cursor.col = col;
5816 }
5817 else
5818 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005819 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005820 && (!has_mbyte ||
5821 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005822 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005823 {
5824 /* negative number */
5825 --col;
5826 negative = TRUE;
5827 }
5828 /* get the number value (unsigned) */
5829 if (visual && VIsual_mode != 'V')
5830 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5831 ? (int)STRLEN(ptr) - col
5832 : length);
5833
5834 vim_str2nr(ptr + col, &pre, &length,
5835 0 + (dobin ? STR2NR_BIN : 0)
5836 + (dooct ? STR2NR_OCT : 0)
5837 + (dohex ? STR2NR_HEX : 0),
5838 NULL, &n, maxlen);
5839
5840 /* ignore leading '-' for hex and octal and bin numbers */
5841 if (pre && negative)
5842 {
5843 ++col;
5844 --length;
5845 negative = FALSE;
5846 }
5847 /* add or subtract */
5848 subtract = FALSE;
5849 if (op_type == OP_NR_SUB)
5850 subtract ^= TRUE;
5851 if (negative)
5852 subtract ^= TRUE;
5853
5854 oldn = n;
5855 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005856 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005857 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005858 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005859 /* handle wraparound for decimal numbers */
5860 if (!pre)
5861 {
5862 if (subtract)
5863 {
5864 if (n > oldn)
5865 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005866 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005867 negative ^= TRUE;
5868 }
5869 }
5870 else
5871 {
5872 /* add */
5873 if (n < oldn)
5874 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005875 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005876 negative ^= TRUE;
5877 }
5878 }
5879 if (n == 0)
5880 negative = FALSE;
5881 }
5882
5883 if (visual && !was_positive && !negative && col > 0)
5884 {
5885 /* need to remove the '-' */
5886 col--;
5887 length++;
5888 }
5889
5890 /*
5891 * Delete the old number.
5892 */
5893 curwin->w_cursor.col = col;
5894 if (!did_change)
5895 startpos = curwin->w_cursor;
5896 did_change = TRUE;
5897 todel = length;
5898 c = gchar_cursor();
5899 /*
5900 * Don't include the '-' in the length, only the length of the
5901 * part after it is kept the same.
5902 */
5903 if (c == '-')
5904 --length;
5905 while (todel-- > 0)
5906 {
5907 if (c < 0x100 && isalpha(c))
5908 {
5909 if (isupper(c))
5910 hexupper = TRUE;
5911 else
5912 hexupper = FALSE;
5913 }
5914 /* del_char() will mark line needing displaying */
5915 (void)del_char(FALSE);
5916 c = gchar_cursor();
5917 }
5918
5919 /*
5920 * Prepare the leading characters in buf1[].
5921 * When there are many leading zeros it could be very long.
5922 * Allocate a bit too much.
5923 */
5924 buf1 = alloc((unsigned)length + NUMBUFLEN);
5925 if (buf1 == NULL)
5926 goto theend;
5927 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005928 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005929 {
5930 *ptr++ = '-';
5931 }
5932 if (pre)
5933 {
5934 *ptr++ = '0';
5935 --length;
5936 }
5937 if (pre == 'b' || pre == 'B' ||
5938 pre == 'x' || pre == 'X')
5939 {
5940 *ptr++ = pre;
5941 --length;
5942 }
5943
5944 /*
5945 * Put the number characters in buf2[].
5946 */
5947 if (pre == 'b' || pre == 'B')
5948 {
5949 int i;
5950 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005951 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005952
5953 /* leading zeros */
5954 for (bit = bits; bit > 0; bit--)
5955 if ((n >> (bit - 1)) & 0x1) break;
5956
5957 for (i = 0; bit > 0; bit--)
5958 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5959
5960 buf2[i] = '\0';
5961 }
5962 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005963 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005964 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005965 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02005966 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005967 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005968 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02005969 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005970 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005971 else
Bram Moolenaarea391762018-04-08 13:07:22 +02005972 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005973 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005974 length -= (int)STRLEN(buf2);
5975
5976 /*
5977 * Adjust number of zeros to the new number of digits, so the
5978 * total length of the number remains the same.
5979 * Don't do this when
5980 * the result may look like an octal number.
5981 */
5982 if (firstdigit == '0' && !(dooct && pre == 0))
5983 while (length-- > 0)
5984 *ptr++ = '0';
5985 *ptr = NUL;
5986 STRCAT(buf1, buf2);
5987 ins_str(buf1); /* insert the new number */
5988 vim_free(buf1);
5989 endpos = curwin->w_cursor;
5990 if (did_change && curwin->w_cursor.col)
5991 --curwin->w_cursor.col;
5992 }
5993
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005994 if (did_change)
5995 {
5996 /* set the '[ and '] marks */
5997 curbuf->b_op_start = startpos;
5998 curbuf->b_op_end = endpos;
5999 if (curbuf->b_op_end.col > 0)
6000 --curbuf->b_op_end.col;
6001 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006002
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006003theend:
6004 if (visual)
6005 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006006 else if (did_change)
6007 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006008
Bram Moolenaard79e5502016-01-10 22:13:02 +01006009 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010}
6011
6012#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006013
6014static yankreg_T *y_read_regs = NULL;
6015
6016#define REG_PREVIOUS 1
6017#define REG_EXEC 2
6018
6019/*
6020 * Prepare for reading viminfo registers when writing viminfo later.
6021 */
6022 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006023prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006024{
6025 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6026 * (int)sizeof(yankreg_T));
6027}
6028
6029 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006030finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006031{
6032 int i;
6033 int j;
6034
6035 if (y_read_regs != NULL)
6036 {
6037 for (i = 0; i < NUM_REGISTERS; ++i)
6038 if (y_read_regs[i].y_array != NULL)
6039 {
6040 for (j = 0; j < y_read_regs[i].y_size; j++)
6041 vim_free(y_read_regs[i].y_array[j]);
6042 vim_free(y_read_regs[i].y_array);
6043 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006044 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006045 }
6046}
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006049read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int eof;
6052 int do_it = TRUE;
6053 int size;
6054 int limit;
6055 int i;
6056 int set_prev = FALSE;
6057 char_u *str;
6058 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006059 int new_type = MCHAR; /* init to shut up compiler */
6060 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062 /* We only get here (hopefully) if line[0] == '"' */
6063 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006064
6065 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (*str == '"')
6067 {
6068 set_prev = TRUE;
6069 str++;
6070 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 if (!ASCII_ISALNUM(*str) && *str != '-')
6073 {
6074 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6075 return TRUE; /* too many errors, pretend end-of-file */
6076 do_it = FALSE;
6077 }
6078 get_yank_register(*str++, FALSE);
6079 if (!force && y_current->y_array != NULL)
6080 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006081
6082 if (*str == '@')
6083 {
6084 /* "x@: register x used for @@ */
6085 if (force || execreg_lastc == NUL)
6086 execreg_lastc = str[-1];
6087 }
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 size = 0;
6090 limit = 100; /* Optimized for registers containing <= 100 lines */
6091 if (do_it)
6092 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006093 /*
6094 * Build the new register in array[].
6095 * y_array is kept as-is until done.
6096 * The "do_it" flag is reset when something is wrong, in which case
6097 * array[] needs to be freed.
6098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (set_prev)
6100 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006101 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006102 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006104 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006106 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006108 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 /* get the block width; if it's missing we get a zero, which is OK */
6110 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006111 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 }
6113
6114 while (!(eof = viminfo_readline(virp))
6115 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6116 {
6117 if (do_it)
6118 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006119 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006121 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006123
6124 if (new_array == NULL)
6125 {
6126 do_it = FALSE;
6127 break;
6128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006130 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006132 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135 str = viminfo_readstring(virp, 1, TRUE);
6136 if (str != NULL)
6137 array[size++] = str;
6138 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006139 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 do_it = FALSE;
6141 }
6142 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006143
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (do_it)
6145 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006146 /* free y_array[] */
6147 for (i = 0; i < y_current->y_size; i++)
6148 vim_free(y_current->y_array[i]);
6149 vim_free(y_current->y_array);
6150
6151 y_current->y_type = new_type;
6152 y_current->y_width = new_width;
6153 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006154 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (size == 0)
6156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 y_current->y_array = NULL;
6158 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006161 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 y_current->y_array =
6163 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6164 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006165 {
6166 if (y_current->y_array == NULL)
6167 vim_free(array[i]);
6168 else
6169 y_current->y_array[i] = array[i];
6170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006173 else
6174 {
6175 /* Free array[] if it was filled. */
6176 for (i = 0; i < size; i++)
6177 vim_free(array[i]);
6178 }
6179 vim_free(array);
6180
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 return eof;
6182}
6183
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006184/*
6185 * Accept a new style register line from the viminfo, store it when it's new.
6186 */
6187 void
6188handle_viminfo_register(garray_T *values, int force)
6189{
6190 bval_T *vp = (bval_T *)values->ga_data;
6191 int flags;
6192 int name;
6193 int type;
6194 int linecount;
6195 int width;
6196 time_t timestamp;
6197 yankreg_T *y_ptr;
6198 int i;
6199
6200 /* Check the format:
6201 * |{bartype},{flags},{name},{type},
6202 * {linecount},{width},{timestamp},"line1","line2"
6203 */
6204 if (values->ga_len < 6
6205 || vp[0].bv_type != BVAL_NR
6206 || vp[1].bv_type != BVAL_NR
6207 || vp[2].bv_type != BVAL_NR
6208 || vp[3].bv_type != BVAL_NR
6209 || vp[4].bv_type != BVAL_NR
6210 || vp[5].bv_type != BVAL_NR)
6211 return;
6212 flags = vp[0].bv_nr;
6213 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006214 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006215 return;
6216 type = vp[2].bv_nr;
6217 if (type != MCHAR && type != MLINE && type != MBLOCK)
6218 return;
6219 linecount = vp[3].bv_nr;
6220 if (values->ga_len < 6 + linecount)
6221 return;
6222 width = vp[4].bv_nr;
6223 if (width < 0)
6224 return;
6225
6226 if (y_read_regs != NULL)
6227 /* Reading viminfo for merging and writing. Store the register
6228 * content, don't update the current registers. */
6229 y_ptr = &y_read_regs[name];
6230 else
6231 y_ptr = &y_regs[name];
6232
6233 /* Do not overwrite unless forced or the timestamp is newer. */
6234 timestamp = (time_t)vp[5].bv_nr;
6235 if (y_ptr->y_array != NULL && !force
6236 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6237 return;
6238
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006239 if (y_ptr->y_array != NULL)
6240 for (i = 0; i < y_ptr->y_size; i++)
6241 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006242 vim_free(y_ptr->y_array);
6243
6244 if (y_read_regs == NULL)
6245 {
6246 if (flags & REG_PREVIOUS)
6247 y_previous = y_ptr;
6248 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6249 execreg_lastc = get_register_name(name);
6250 }
6251 y_ptr->y_type = type;
6252 y_ptr->y_width = width;
6253 y_ptr->y_size = linecount;
6254 y_ptr->y_time_set = timestamp;
6255 if (linecount == 0)
6256 y_ptr->y_array = NULL;
6257 else
6258 {
6259 y_ptr->y_array =
6260 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6261 for (i = 0; i < linecount; i++)
6262 {
6263 if (vp[i + 6].bv_allocated)
6264 {
6265 y_ptr->y_array[i] = vp[i + 6].bv_string;
6266 vp[i + 6].bv_string = NULL;
6267 }
6268 else
6269 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6270 }
6271 }
6272}
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006275write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006277 int i, j;
6278 char_u *type;
6279 char_u c;
6280 int num_lines;
6281 int max_num_lines;
6282 int max_kbyte;
6283 long len;
6284 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285
Bram Moolenaar64404472010-06-26 06:24:45 +02006286 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
6288 /* Get '<' value, use old '"' value if '<' is not found. */
6289 max_num_lines = get_viminfo_parameter('<');
6290 if (max_num_lines < 0)
6291 max_num_lines = get_viminfo_parameter('"');
6292 if (max_num_lines == 0)
6293 return;
6294 max_kbyte = get_viminfo_parameter('s');
6295 if (max_kbyte == 0)
6296 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 for (i = 0; i < NUM_REGISTERS; i++)
6299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300#ifdef FEAT_CLIPBOARD
6301 /* Skip '*'/'+' register, we don't want them back next time */
6302 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6303 continue;
6304#endif
6305#ifdef FEAT_DND
6306 /* Neither do we want the '~' register */
6307 if (i == TILDE_REGISTER)
6308 continue;
6309#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006310 /* When reading viminfo for merging and writing: Use the register from
6311 * viminfo if it's newer. */
6312 if (y_read_regs != NULL
6313 && y_read_regs[i].y_array != NULL
6314 && (y_regs[i].y_array == NULL ||
6315 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6316 y_ptr = &y_read_regs[i];
6317 else if (y_regs[i].y_array == NULL)
6318 continue;
6319 else
6320 y_ptr = &y_regs[i];
6321
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006322 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006323 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006324 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006325 || (num_lines == 1 && y_ptr->y_type == MCHAR
6326 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006327 continue;
6328
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (max_kbyte > 0)
6330 {
6331 /* Skip register if there is more text than the maximum size. */
6332 len = 0;
6333 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006334 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (len > (long)max_kbyte * 1024L)
6336 continue;
6337 }
6338
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006339 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 case MLINE:
6342 type = (char_u *)"LINE";
6343 break;
6344 case MCHAR:
6345 type = (char_u *)"CHAR";
6346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 case MBLOCK:
6348 type = (char_u *)"BLOCK";
6349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006351 semsg(_("E574: Unknown register type %d"), y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 type = (char_u *)"LINE";
6353 break;
6354 }
6355 if (y_previous == &y_regs[i])
6356 fprintf(fp, "\"");
6357 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006358 fprintf(fp, "\"%c", c);
6359 if (c == execreg_lastc)
6360 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006361 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 /* If max_num_lines < 0, then we save ALL the lines in the register */
6364 if (max_num_lines > 0 && num_lines > max_num_lines)
6365 num_lines = max_num_lines;
6366 for (j = 0; j < num_lines; j++)
6367 {
6368 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006369 viminfo_writestring(fp, y_ptr->y_array[j]);
6370 }
6371
6372 {
6373 int flags = 0;
6374 int remaining;
6375
6376 /* New style with a bar line. Format:
6377 * |{bartype},{flags},{name},{type},
6378 * {linecount},{width},{timestamp},"line1","line2"
6379 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006380 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006381 */
6382 if (y_previous == &y_regs[i])
6383 flags |= REG_PREVIOUS;
6384 if (c == execreg_lastc)
6385 flags |= REG_EXEC;
6386 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6387 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6388 (long)y_ptr->y_time_set);
6389 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6390 remaining = LSIZE - 71;
6391 for (j = 0; j < num_lines; j++)
6392 {
6393 putc(',', fp);
6394 --remaining;
6395 remaining = barline_writestring(fp, y_ptr->y_array[j],
6396 remaining);
6397 }
6398 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400 }
6401}
6402#endif /* FEAT_VIMINFO */
6403
6404#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6405/*
6406 * SELECTION / PRIMARY ('*')
6407 *
6408 * Text selection stuff that uses the GUI selection register '*'. When using a
6409 * GUI this may be text from another window, otherwise it is the last text we
6410 * had highlighted with VIsual mode. With mouse support, clicking the middle
6411 * button performs the paste, otherwise you will need to do <"*p>. "
6412 * If not under X, it is synonymous with the clipboard register '+'.
6413 *
6414 * X CLIPBOARD ('+')
6415 *
6416 * Text selection stuff that uses the GUI clipboard register '+'.
6417 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6418 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6419 * otherwise you will need to do <"+p>. "
6420 * If not under X, it is synonymous with the selection register '*'.
6421 */
6422
6423/*
6424 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006425 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 * only exist while the owning application exists, so we write to the
6427 * permanent (while X runs) store CUT_BUFFER0.
6428 * Dump the CLIPBOARD selection if we own it (it's logically the more
6429 * 'permanent' of the two), otherwise the PRIMARY one.
6430 * For now, use a hard-coded sanity limit of 1Mb of data.
6431 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006432#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006434x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 Display *dpy;
6437 char_u *str = NULL;
6438 long_u len = 0;
6439 int motion_type = -1;
6440
6441# ifdef FEAT_GUI
6442 if (gui.in_use)
6443 dpy = X_DISPLAY;
6444 else
6445# endif
6446# ifdef FEAT_XCLIPBOARD
6447 dpy = xterm_dpy;
6448# else
6449 return;
6450# endif
6451
6452 /* Get selection to export */
6453 if (clip_plus.owned)
6454 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6455 else if (clip_star.owned)
6456 motion_type = clip_convert_selection(&str, &len, &clip_star);
6457
6458 /* Check it's OK */
6459 if (dpy != NULL && str != NULL && motion_type >= 0
6460 && len < 1024*1024 && len > 0)
6461 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006462 int ok = TRUE;
6463
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006464 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6465 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6466 * encoding conversion usually doesn't work, so keep the text as-is.
6467 */
6468 if (has_mbyte)
6469 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006470 vimconv_T vc;
6471
6472 vc.vc_type = CONV_NONE;
6473 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6474 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006475 int intlen = len;
6476 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006477
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006478 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006479 conv_str = string_convert(&vc, str, &intlen);
6480 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006481 if (conv_str != NULL)
6482 {
6483 vim_free(str);
6484 str = conv_str;
6485 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006486 else
6487 {
6488 ok = FALSE;
6489 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006490 convert_setup(&vc, NULL, NULL);
6491 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006492 else
6493 {
6494 ok = FALSE;
6495 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006496 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006497
6498 /* Do not store the string if conversion failed. Better to use any
6499 * other selection than garbled text. */
6500 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006501 {
6502 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6503 XFlush(dpy);
6504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 }
6506
6507 vim_free(str);
6508}
6509#endif
6510
6511 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006512clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006514 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516 if (cbd == &clip_plus)
6517 y_current = &y_regs[PLUS_REGISTER];
6518 else
6519 y_current = &y_regs[STAR_REGISTER];
6520 free_yank_all();
6521 y_current->y_size = 0;
6522 y_current = y_ptr;
6523}
6524
6525/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006526 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 */
6528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006529clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006531 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 pos_T old_visual;
6534 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 colnr_T old_curswant;
6536 int old_set_curswant;
6537 pos_T old_op_start, old_op_end;
6538 oparg_T oa;
6539 cmdarg_T ca;
6540
6541 if (cbd->owned)
6542 {
6543 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6544 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6545 return;
6546
6547 /* Get the text between clip_star.start & clip_star.end */
6548 old_y_previous = y_previous;
6549 old_y_current = y_current;
6550 old_cursor = curwin->w_cursor;
6551 old_curswant = curwin->w_curswant;
6552 old_set_curswant = curwin->w_set_curswant;
6553 old_op_start = curbuf->b_op_start;
6554 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 old_visual = VIsual;
6556 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 clear_oparg(&oa);
6558 oa.regname = (cbd == &clip_plus ? '+' : '*');
6559 oa.op_type = OP_YANK;
6560 vim_memset(&ca, 0, sizeof(ca));
6561 ca.oap = &oa;
6562 ca.cmdchar = 'y';
6563 ca.count1 = 1;
6564 ca.retval = CA_NO_ADJ_OP_END;
6565 do_pending_operator(&ca, 0, TRUE);
6566 y_previous = old_y_previous;
6567 y_current = old_y_current;
6568 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006569 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 curwin->w_curswant = old_curswant;
6571 curwin->w_set_curswant = old_set_curswant;
6572 curbuf->b_op_start = old_op_start;
6573 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 VIsual = old_visual;
6575 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006577 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
6579 clip_free_selection(cbd);
6580
6581 /* Try to get selected text from another window */
6582 clip_gen_request_selection(cbd);
6583 }
6584}
6585
Bram Moolenaard44347f2011-06-19 01:14:29 +02006586/*
6587 * Convert from the GUI selection string into the '*'/'+' register.
6588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006590clip_yank_selection(
6591 int type,
6592 char_u *str,
6593 long len,
6594 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006596 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598 if (cbd == &clip_plus)
6599 y_ptr = &y_regs[PLUS_REGISTER];
6600 else
6601 y_ptr = &y_regs[STAR_REGISTER];
6602
6603 clip_free_selection(cbd);
6604
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006605 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
6607
6608/*
6609 * Convert the '*'/'+' register into a GUI selection string returned in *str
6610 * with length *len.
6611 * Returns the motion type, or -1 for failure.
6612 */
6613 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 char_u *p;
6617 int lnum;
6618 int i, j;
6619 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006620 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
6622 if (cbd == &clip_plus)
6623 y_ptr = &y_regs[PLUS_REGISTER];
6624 else
6625 y_ptr = &y_regs[STAR_REGISTER];
6626
6627#ifdef USE_CRNL
6628 eolsize = 2;
6629#else
6630 eolsize = 1;
6631#endif
6632
6633 *str = NULL;
6634 *len = 0;
6635 if (y_ptr->y_array == NULL)
6636 return -1;
6637
6638 for (i = 0; i < y_ptr->y_size; i++)
6639 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6640
6641 /*
6642 * Don't want newline character at end of last line if we're in MCHAR mode.
6643 */
6644 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6645 *len -= eolsize;
6646
6647 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6648 if (p == NULL)
6649 return -1;
6650 lnum = 0;
6651 for (i = 0, j = 0; i < (int)*len; i++, j++)
6652 {
6653 if (y_ptr->y_array[lnum][j] == '\n')
6654 p[i] = NUL;
6655 else if (y_ptr->y_array[lnum][j] == NUL)
6656 {
6657#ifdef USE_CRNL
6658 p[i++] = '\r';
6659#endif
6660#ifdef USE_CR
6661 p[i] = '\r';
6662#else
6663 p[i] = '\n';
6664#endif
6665 lnum++;
6666 j = -1;
6667 }
6668 else
6669 p[i] = y_ptr->y_array[lnum][j];
6670 }
6671 return y_ptr->y_type;
6672}
6673
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675/*
6676 * If we have written to a clipboard register, send the text to the clipboard.
6677 */
6678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6682 {
6683 clip_own_selection(&clip_star);
6684 clip_gen_set_selection(&clip_star);
6685 }
6686 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6687 {
6688 clip_own_selection(&clip_plus);
6689 clip_gen_set_selection(&clip_plus);
6690 }
6691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693#endif /* FEAT_CLIPBOARD || PROTO */
6694
6695
6696#if defined(FEAT_DND) || defined(PROTO)
6697/*
6698 * Replace the contents of the '~' register with str.
6699 */
6700 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006703 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705 curr = y_current;
6706 y_current = &y_regs[TILDE_REGISTER];
6707 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006708 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 y_current = curr;
6710}
6711#endif
6712
6713
6714#if defined(FEAT_EVAL) || defined(PROTO)
6715/*
6716 * Return the type of a register.
6717 * Used for getregtype()
6718 * Returns MAUTO for error.
6719 */
6720 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 switch (regname)
6724 {
6725 case '%': /* file name */
6726 case '#': /* alternate file name */
6727 case '=': /* expression */
6728 case ':': /* last command line */
6729 case '/': /* last search-pattern */
6730 case '.': /* last inserted text */
6731#ifdef FEAT_SEARCHPATH
6732 case Ctrl_F: /* Filename under cursor */
6733 case Ctrl_P: /* Path under cursor, expand via "path" */
6734#endif
6735 case Ctrl_W: /* word under cursor */
6736 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6737 case '_': /* black hole: always empty */
6738 return MCHAR;
6739 }
6740
6741#ifdef FEAT_CLIPBOARD
6742 regname = may_get_selection(regname);
6743#endif
6744
Bram Moolenaar32b92012014-01-14 12:33:36 +01006745 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006746 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 get_yank_register(regname, FALSE);
6749
6750 if (y_current->y_array != NULL)
6751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (reglen != NULL && y_current->y_type == MBLOCK)
6753 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 return y_current->y_type;
6755 }
6756 return MAUTO;
6757}
6758
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006759/*
6760 * When "flags" has GREG_LIST return a list with text "s".
6761 * Otherwise just return "s".
6762 */
6763 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006764getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006765{
6766 if (flags & GREG_LIST)
6767 {
6768 list_T *list = list_alloc();
6769
6770 if (list != NULL)
6771 {
6772 if (list_append_string(list, NULL, -1) == FAIL)
6773 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006774 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006775 return NULL;
6776 }
6777 list->lv_first->li_tv.vval.v_string = s;
6778 }
6779 return (char_u *)list;
6780 }
6781 return s;
6782}
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784/*
6785 * Return the contents of a register as a single allocated string.
6786 * Used for "@r" in expressions and for getreg().
6787 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006788 * Flags:
6789 * GREG_NO_EXPR Do not allow expression register
6790 * GREG_EXPR_SRC For the expression register: return expression itself,
6791 * not the result of its evaluation.
6792 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 */
6794 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006795get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 long i;
6798 char_u *retval;
6799 int allocated;
6800 long len;
6801
6802 /* Don't allow using an expression register inside an expression */
6803 if (regname == '=')
6804 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006805 if (flags & GREG_NO_EXPR)
6806 return NULL;
6807 if (flags & GREG_EXPR_SRC)
6808 return getreg_wrap_one_line(get_expr_line_src(), flags);
6809 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811
6812 if (regname == '@') /* "@@" is used for unnamed register */
6813 regname = '"';
6814
6815 /* check for valid regname */
6816 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6817 return NULL;
6818
6819#ifdef FEAT_CLIPBOARD
6820 regname = may_get_selection(regname);
6821#endif
6822
6823 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6824 {
6825 if (retval == NULL)
6826 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006827 if (allocated)
6828 return getreg_wrap_one_line(retval, flags);
6829 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831
6832 get_yank_register(regname, FALSE);
6833 if (y_current->y_array == NULL)
6834 return NULL;
6835
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006836 if (flags & GREG_LIST)
6837 {
6838 list_T *list = list_alloc();
6839 int error = FALSE;
6840
6841 if (list == NULL)
6842 return NULL;
6843 for (i = 0; i < y_current->y_size; ++i)
6844 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6845 error = TRUE;
6846 if (error)
6847 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006848 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006849 return NULL;
6850 }
6851 return (char_u *)list;
6852 }
6853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 /*
6855 * Compute length of resulting string.
6856 */
6857 len = 0;
6858 for (i = 0; i < y_current->y_size; ++i)
6859 {
6860 len += (long)STRLEN(y_current->y_array[i]);
6861 /*
6862 * Insert a newline between lines and after last line if
6863 * y_type is MLINE.
6864 */
6865 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6866 ++len;
6867 }
6868
6869 retval = lalloc(len + 1, TRUE);
6870
6871 /*
6872 * Copy the lines of the yank register into the string.
6873 */
6874 if (retval != NULL)
6875 {
6876 len = 0;
6877 for (i = 0; i < y_current->y_size; ++i)
6878 {
6879 STRCPY(retval + len, y_current->y_array[i]);
6880 len += (long)STRLEN(retval + len);
6881
6882 /*
6883 * Insert a NL between lines and after the last line if y_type is
6884 * MLINE.
6885 */
6886 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6887 retval[len++] = '\n';
6888 }
6889 retval[len] = NUL;
6890 }
6891
6892 return retval;
6893}
6894
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006895 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006896init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006897 int name,
6898 yankreg_T **old_y_previous,
6899 yankreg_T **old_y_current,
6900 int must_append,
6901 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006902{
6903 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6904 {
6905 emsg_invreg(name);
6906 return FAIL;
6907 }
6908
6909 /* Don't want to change the current (unnamed) register */
6910 *old_y_previous = y_previous;
6911 *old_y_current = y_current;
6912
6913 get_yank_register(name, TRUE);
6914 if (!y_append && !must_append)
6915 free_yank_all();
6916 return OK;
6917}
6918
6919 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006920finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006921 int name,
6922 yankreg_T *old_y_previous,
6923 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006924{
6925# ifdef FEAT_CLIPBOARD
6926 /* Send text of clipboard register to the clipboard. */
6927 may_set_selection();
6928# endif
6929
6930 /* ':let @" = "val"' should change the meaning of the "" register */
6931 if (name != '"')
6932 y_previous = old_y_previous;
6933 y_current = old_y_current;
6934}
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936/*
6937 * Store string "str" in register "name".
6938 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6939 * If "must_append" is TRUE, always append to the register. Otherwise append
6940 * if "name" is an uppercase letter.
6941 * Note: "maxlen" and "must_append" don't work for the "/" register.
6942 * Careful: 'str' is modified, you may have to use a copy!
6943 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6944 */
6945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006946write_reg_contents(
6947 int name,
6948 char_u *str,
6949 int maxlen,
6950 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6953}
6954
6955 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006956write_reg_contents_lst(
6957 int name,
6958 char_u **strings,
6959 int maxlen UNUSED,
6960 int must_append,
6961 int yank_type,
6962 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006963{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006964 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006965
6966 if (name == '/'
6967#ifdef FEAT_EVAL
6968 || name == '='
6969#endif
6970 )
6971 {
6972 char_u *s;
6973
6974 if (strings[0] == NULL)
6975 s = (char_u *)"";
6976 else if (strings[1] != NULL)
6977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006978 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006979 "contain two or more lines"));
6980 return;
6981 }
6982 else
6983 s = strings[0];
6984 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6985 return;
6986 }
6987
6988 if (name == '_') /* black hole: nothing to do */
6989 return;
6990
6991 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6992 &yank_type) == FAIL)
6993 return;
6994
6995 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6996
6997 finish_write_reg(name, old_y_previous, old_y_current);
6998}
6999
7000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007001write_reg_contents_ex(
7002 int name,
7003 char_u *str,
7004 int maxlen,
7005 int must_append,
7006 int yank_type,
7007 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007009 yankreg_T *old_y_previous, *old_y_current;
7010 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
Bram Moolenaare7566042005-06-17 22:00:15 +00007012 if (maxlen >= 0)
7013 len = maxlen;
7014 else
7015 len = (long)STRLEN(str);
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 /* Special case: '/' search pattern */
7018 if (name == '/')
7019 {
7020 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7021 return;
7022 }
7023
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007024 if (name == '#')
7025 {
7026 buf_T *buf;
7027
7028 if (VIM_ISDIGIT(*str))
7029 {
7030 int num = atoi((char *)str);
7031
7032 buf = buflist_findnr(num);
7033 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007034 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007035 }
7036 else
7037 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7038 TRUE, FALSE, FALSE));
7039 if (buf == NULL)
7040 return;
7041 curwin->w_alt_fnum = buf->b_fnum;
7042 return;
7043 }
7044
Bram Moolenaare7566042005-06-17 22:00:15 +00007045#ifdef FEAT_EVAL
7046 if (name == '=')
7047 {
7048 char_u *p, *s;
7049
7050 p = vim_strnsave(str, (int)len);
7051 if (p == NULL)
7052 return;
7053 if (must_append)
7054 {
7055 s = concat_str(get_expr_line_src(), p);
7056 vim_free(p);
7057 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007058 }
7059 set_expr_line(p);
7060 return;
7061 }
7062#endif
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (name == '_') /* black hole: nothing to do */
7065 return;
7066
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007067 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7068 &yank_type) == FAIL)
7069 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007071 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007073 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074}
7075#endif /* FEAT_EVAL */
7076
7077#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7078/*
7079 * Put a string into a register. When the register is not empty, the string
7080 * is appended.
7081 */
7082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007084 yankreg_T *y_ptr, /* pointer to yank register */
7085 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7086 char_u *str, /* string to put in register */
7087 long len, /* length of string */
7088 long blocklen, /* width of Visual block */
7089 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007091 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 int lnum;
7093 long start;
7094 long i;
7095 int extra;
7096 int newlines; /* number of lines added */
7097 int extraline = 0; /* extra line at the end */
7098 int append = FALSE; /* append to last line in register */
7099 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007100 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007104 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 y_ptr->y_size = 0;
7106
Bram Moolenaard44347f2011-06-19 01:14:29 +02007107 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007108 type = ((str_list || (len > 0 && (str[len - 1] == NL
7109 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007110 ? MLINE : MCHAR);
7111 else
7112 type = yank_type;
7113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 /*
7115 * Count the number of lines within the string
7116 */
7117 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007118 if (str_list)
7119 {
7120 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007125 for (i = 0; i < len; i++)
7126 if (str[i] == '\n')
7127 ++newlines;
7128 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7129 {
7130 extraline = 1;
7131 ++newlines; /* count extra newline at the end */
7132 }
7133 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7134 {
7135 append = TRUE;
7136 --newlines; /* uncount newline when appending first line */
7137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 }
7139
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007140 /* Without any lines make the register empty. */
7141 if (y_ptr->y_size + newlines == 0)
7142 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007143 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007144 return;
7145 }
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 /*
7148 * Allocate an array to hold the pointers to the new register lines.
7149 * If the register was not empty, move the existing lines to the new array.
7150 */
7151 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7152 * sizeof(char_u *), TRUE);
7153 if (pp == NULL) /* out of memory */
7154 return;
7155 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7156 pp[lnum] = y_ptr->y_array[lnum];
7157 vim_free(y_ptr->y_array);
7158 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161 /*
7162 * Find the end of each line and save it into the array.
7163 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007164 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007166 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7167 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007168 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007169 pp[lnum] = vim_strnsave(*ss, i);
7170 if (i > maxlen)
7171 maxlen = i;
7172 }
7173 }
7174 else
7175 {
7176 for (start = 0; start < len + extraline; start += i + 1)
7177 {
7178 for (i = start; i < len; ++i) /* find the end of the line */
7179 if (str[i] == '\n')
7180 break;
7181 i -= start; /* i is now length of line */
7182 if (i > maxlen)
7183 maxlen = i;
7184 if (append)
7185 {
7186 --lnum;
7187 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7188 }
7189 else
7190 extra = 0;
7191 s = alloc((unsigned)(i + extra + 1));
7192 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007194 if (extra)
7195 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7196 if (append)
7197 vim_free(y_ptr->y_array[lnum]);
7198 if (i)
7199 mch_memmove(s + extra, str + start, (size_t)i);
7200 extra += i;
7201 s[extra] = NUL;
7202 y_ptr->y_array[lnum++] = s;
7203 while (--extra >= 0)
7204 {
7205 if (*s == NUL)
7206 *s = '\n'; /* replace NUL with newline */
7207 ++s;
7208 }
7209 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 y_ptr->y_type = type;
7213 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (type == MBLOCK)
7215 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7216 else
7217 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007218#ifdef FEAT_VIMINFO
7219 y_ptr->y_time_set = vim_time();
7220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221}
7222#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7223
7224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007225clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
7227 vim_memset(oap, 0, sizeof(oparg_T));
7228}
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007231 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 *
7233 * "Words" are counted by looking for boundaries between non-space and
7234 * space characters. (it seems to produce results that match 'wc'.)
7235 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007236 * Return value is byte count; word count for the line is added to "*wc".
7237 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 *
7239 * The function will only examine the first "limit" characters in the
7240 * line, stopping if it encounters an end-of-line (NUL byte). In that
7241 * case, eol_size will be added to the character count to account for
7242 * the size of the EOL character.
7243 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007244 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007245line_count_info(
7246 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007247 varnumber_T *wc,
7248 varnumber_T *cc,
7249 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007250 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007252 varnumber_T i;
7253 varnumber_T words = 0;
7254 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 int is_word = 0;
7256
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007257 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 if (is_word)
7260 {
7261 if (vim_isspace(line[i]))
7262 {
7263 words++;
7264 is_word = 0;
7265 }
7266 }
7267 else if (!vim_isspace(line[i]))
7268 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007269 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007270 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 }
7272
7273 if (is_word)
7274 words++;
7275 *wc += words;
7276
7277 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007278 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007281 chars += eol_size;
7282 }
7283 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 return i;
7285}
7286
7287/*
7288 * Give some info about the position of the cursor (for "g CTRL-G").
7289 * In Visual mode, give some info about the selected region. (In this case,
7290 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007291 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 */
7293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007294cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007297 char_u buf1[50];
7298 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007300 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007301 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007302 varnumber_T byte_count_cursor = 0;
7303 varnumber_T char_count = 0;
7304 varnumber_T char_count_cursor = 0;
7305 varnumber_T word_count = 0;
7306 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007307 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007308 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 long line_count_selected = 0;
7310 pos_T min_pos, max_pos;
7311 oparg_T oparg;
7312 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
7314 /*
7315 * Compute the length of the file in characters.
7316 */
7317 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7318 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007319 if (dict == NULL)
7320 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007321 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01007322 return;
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325 else
7326 {
7327 if (get_fileformat(curbuf) == EOL_DOS)
7328 eol_size = 2;
7329 else
7330 eol_size = 1;
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (VIsual_active)
7333 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007334 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 min_pos = VIsual;
7337 max_pos = curwin->w_cursor;
7338 }
7339 else
7340 {
7341 min_pos = curwin->w_cursor;
7342 max_pos = VIsual;
7343 }
7344 if (*p_sel == 'e' && max_pos.col > 0)
7345 --max_pos.col;
7346
7347 if (VIsual_mode == Ctrl_V)
7348 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007349#ifdef FEAT_LINEBREAK
7350 char_u * saved_sbr = p_sbr;
7351
7352 /* Make 'sbr' empty for a moment to get the correct size. */
7353 p_sbr = empty_option;
7354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 oparg.is_VIsual = 1;
7356 oparg.block_mode = TRUE;
7357 oparg.op_type = OP_NOP;
7358 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007359 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007360#ifdef FEAT_LINEBREAK
7361 p_sbr = saved_sbr;
7362#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007363 if (curwin->w_curswant == MAXCOL)
7364 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 /* Swap the start, end vcol if needed */
7366 if (oparg.end_vcol < oparg.start_vcol)
7367 {
7368 oparg.end_vcol += oparg.start_vcol;
7369 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7370 oparg.end_vcol -= oparg.start_vcol;
7371 }
7372 }
7373 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7377 {
7378 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007379 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 ui_breakcheck();
7382 if (got_int)
7383 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007384 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 /* Do extra processing for VIsual mode. */
7388 if (VIsual_active
7389 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7390 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007391 char_u *s = NULL;
7392 long len = 0L;
7393
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 switch (VIsual_mode)
7395 {
7396 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007397#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007401#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007403#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007404 s = bd.textstart;
7405 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 break;
7407 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007408 s = ml_get(lnum);
7409 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 break;
7411 case 'v':
7412 {
7413 colnr_T start_col = (lnum == min_pos.lnum)
7414 ? min_pos.col : 0;
7415 colnr_T end_col = (lnum == max_pos.lnum)
7416 ? max_pos.col - start_col + 1 : MAXCOL;
7417
Bram Moolenaardef9e822004-12-31 20:58:58 +00007418 s = ml_get(lnum) + start_col;
7419 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 break;
7422 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007423 if (s != NULL)
7424 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007425 byte_count_cursor += line_count_info(s, &word_count_cursor,
7426 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007427 if (lnum == curbuf->b_ml.ml_line_count
7428 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007429 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007430 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007431 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
7436 /* In non-visual mode, check for the line the cursor is on */
7437 if (lnum == curwin->w_cursor.lnum)
7438 {
7439 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007440 char_count_cursor += char_count;
7441 byte_count_cursor = byte_count +
7442 line_count_info(ml_get(lnum),
7443 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007444 (varnumber_T)(curwin->w_cursor.col + 1),
7445 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 }
7447 }
7448 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007449 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007450 &char_count, (varnumber_T)MAXCOL,
7451 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 }
7453
7454 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007455 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007456 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
Bram Moolenaared767a22016-01-03 22:49:16 +01007458 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007460 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007462 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7463 {
7464 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7465 &max_pos.col);
7466 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7467 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7468 }
7469 else
7470 buf1[0] = NUL;
7471
7472 if (char_count_cursor == byte_count_cursor
7473 && char_count == byte_count)
7474 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007475 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007476 buf1, line_count_selected,
7477 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007478 (long_long_T)word_count_cursor,
7479 (long_long_T)word_count,
7480 (long_long_T)byte_count_cursor,
7481 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007482 else
7483 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007484 _("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 +01007485 buf1, line_count_selected,
7486 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007487 (long_long_T)word_count_cursor,
7488 (long_long_T)word_count,
7489 (long_long_T)char_count_cursor,
7490 (long_long_T)char_count,
7491 (long_long_T)byte_count_cursor,
7492 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007495 {
7496 p = ml_get_curline();
7497 validate_virtcol();
7498 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7499 (int)curwin->w_virtcol + 1);
7500 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7501 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
Bram Moolenaared767a22016-01-03 22:49:16 +01007503 if (char_count_cursor == byte_count_cursor
7504 && char_count == byte_count)
7505 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007506 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007507 (char *)buf1, (char *)buf2,
7508 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007510 (long_long_T)word_count_cursor, (long_long_T)word_count,
7511 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007512 else
7513 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007514 _("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 +01007515 (char *)buf1, (char *)buf2,
7516 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007517 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007518 (long_long_T)word_count_cursor, (long_long_T)word_count,
7519 (long_long_T)char_count_cursor, (long_long_T)char_count,
7520 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007521 }
7522 }
7523
Bram Moolenaared767a22016-01-03 22:49:16 +01007524 bom_count = bomb_size();
7525 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007526 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007527 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007528 if (dict == NULL)
7529 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007530 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007531 p = p_shm;
7532 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007533 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007534 p_shm = p;
7535 }
7536 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007537#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007538 if (dict != NULL)
7539 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007540 dict_add_number(dict, "words", word_count);
7541 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007542 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007543 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7544 byte_count_cursor);
7545 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7546 char_count_cursor);
7547 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7548 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551}