blob: 4b1040f329f432a4ba3d94fc01624302a3aec863 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020053 * Each yank register has an array of pointers to lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020055typedef struct
Bram Moolenaar071d4272004-06-13 20:20:40 +000056{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 colnr_T y_width; /* only set if y_type == MBLOCK */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020061#ifdef FEAT_VIMINFO
62 time_t y_time_set;
63#endif
64} yankreg_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020066static yankreg_T y_regs[NUM_REGISTERS];
67
68static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020070static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/*
73 * structure used by block_prep, op_delete and op_yank for blockwise operators
74 * also op_change, op_shift, op_insert, op_replace - AKelly
75 */
76struct block_def
77{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000078 int startspaces; /* 'extra' cols before first char */
79 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000081 char_u *textstart; /* pointer to 1st char (partially) in block */
82 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 colnr_T start_vcol; /* start col of 1st char wholly inside block */
84 colnr_T end_vcol; /* start col of 1st char wholly after block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 int is_short; /* TRUE if line is too short to fit in block */
86 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
87 int is_oneChar; /* TRUE if block within one character */
88 int pre_whitesp; /* screen cols of ws before block */
89 int pre_whitesp_c; /* chars of ws before block */
90 colnr_T end_char_vcols; /* number of vcols of post-block char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 colnr_T start_char_vcols; /* number of vcols of pre-block char */
92};
93
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010094static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010095static int stuff_yank(int, char_u *);
96static void put_reedit_in_typebuf(int silent);
97static int put_in_typebuf(char_u *s, int esc, int colon,
98 int silent);
99static void stuffescaped(char_u *arg, int literally);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100101static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100103static void free_yank_all(void);
104static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200106static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100107static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100109static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100110static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
111static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200113static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100117static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100119static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121
Bram Moolenaarf2732452018-06-03 14:47:35 +0200122// Flags for third item in "opchars".
123#define OPF_LINES 1 // operator always works on lines
124#define OPF_CHANGE 2 // operator changes text
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
127 * The names of operators.
128 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +0200129 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 */
131static char opchars[][3] =
132{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200133 {NUL, NUL, 0}, // OP_NOP
134 {'d', NUL, OPF_CHANGE}, // OP_DELETE
135 {'y', NUL, 0}, // OP_YANK
136 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
137 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
138 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
139 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
140 {'g', '~', OPF_CHANGE}, // OP_TILDE
141 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
142 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
143 {':', NUL, OPF_LINES}, // OP_COLON
144 {'g', 'U', OPF_CHANGE}, // OP_UPPER
145 {'g', 'u', OPF_CHANGE}, // OP_LOWER
146 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
147 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
148 {'g', '?', OPF_CHANGE}, // OP_ROT13
149 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
150 {'I', NUL, OPF_CHANGE}, // OP_INSERT
151 {'A', NUL, OPF_CHANGE}, // OP_APPEND
152 {'z', 'f', OPF_LINES}, // OP_FOLD
153 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
154 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
155 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
156 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
157 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
158 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
159 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
160 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
161 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
162 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163};
164
165/*
166 * Translate a command name into an operator type.
167 * Must only be called with a valid operator name!
168 */
169 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100170get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 int i;
173
174 if (char1 == 'r') /* ignore second character */
175 return OP_REPLACE;
176 if (char1 == '~') /* when tilde is an operator */
177 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100178 if (char1 == 'g' && char2 == Ctrl_A) /* add */
179 return OP_NR_ADD;
180 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
181 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (opchars[i][0] == char1 && opchars[i][1] == char2)
185 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100186 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
187 {
188 internal_error("get_op_type()");
189 break;
190 }
191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 return i;
193}
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195/*
196 * Return TRUE if operator "op" always works on whole lines.
197 */
198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100199op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200201 return opchars[op][2] & OPF_LINES;
202}
203
Bram Moolenaar113e1072019-01-20 15:30:40 +0100204#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200205/*
206 * Return TRUE if operator "op" changes text.
207 */
208 int
209op_is_change(int op)
210{
211 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
215/*
216 * Get first operator command character.
217 * Returns 'g' or 'z' if there is another command character.
218 */
219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100220get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
222 return opchars[optype][0];
223}
224
225/*
226 * Get second operator command character.
227 */
228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100229get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 return opchars[optype][1];
232}
233
234/*
235 * op_shift - handle a shift operation
236 */
237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100238op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239{
240 long i;
241 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244 if (u_save((linenr_T)(oap->start.lnum - 1),
245 (linenr_T)(oap->end.lnum + 1)) == FAIL)
246 return;
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 if (oap->block_mode)
249 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251 for (i = oap->line_count; --i >= 0; )
252 {
253 first_char = *ml_get_curline();
254 if (first_char == NUL) /* empty line */
255 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 else if (oap->block_mode)
257 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 else
259 /* Move the line right if it doesn't start with '#', 'smartindent'
260 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
261#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
262 if (first_char != '#' || !preprocs_left())
263#endif
264 {
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000265 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 }
267 ++curwin->w_cursor.lnum;
268 }
269
270 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 if (oap->block_mode)
272 {
273 curwin->w_cursor.lnum = oap->start.lnum;
274 curwin->w_cursor.col = block_col;
275 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100276 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 {
278 curwin->w_cursor.lnum = oap->start.lnum;
279 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
280 }
281 else
282 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
283
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100284#ifdef FEAT_FOLDING
285 /* The cursor line is not in a closed fold */
286 foldOpenCursor();
287#endif
288
289
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 if (oap->line_count > p_report)
291 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200292 char *op;
293 char *msg_line_single;
294 char *msg_line_plural;
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200297 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200299 op = "<";
300 msg_line_single = NGETTEXT("%ld line %sed %d time",
301 "%ld line %sed %d times", amount);
302 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
303 "%ld lines %sed %d times", amount);
304 vim_snprintf((char *)IObuff, IOSIZE,
305 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
306 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100307 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 }
309
310 /*
311 * Set "'[" and "']" marks.
312 */
313 curbuf->b_op_start = oap->start;
314 curbuf->b_op_end.lnum = oap->end.lnum;
315 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
316 if (curbuf->b_op_end.col > 0)
317 --curbuf->b_op_end.col;
318}
319
320/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100321 * Shift the current line one shiftwidth left (if left != 0) or right
322 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 */
324 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100325shift_line(
326 int left,
327 int round,
328 int amount,
329 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int count;
332 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100333 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
335 count = get_indent(); /* get current indent */
336
337 if (round) /* round off indent */
338 {
339 i = count / p_sw; /* number of p_sw rounded down */
340 j = count % p_sw; /* extra spaces */
341 if (j && left) /* first remove extra spaces */
342 --amount;
343 if (left)
344 {
345 i -= amount;
346 if (i < 0)
347 i = 0;
348 }
349 else
350 i += amount;
351 count = i * p_sw;
352 }
353 else /* original vi indent */
354 {
355 if (left)
356 {
357 count -= p_sw * amount;
358 if (count < 0)
359 count = 0;
360 }
361 else
362 count += p_sw * amount;
363 }
364
365 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000367 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000369 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370}
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372/*
373 * Shift one line of the current block one shiftwidth right or left.
374 * Leaves cursor on first character in block.
375 */
376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100377shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
379 int left = (oap->op_type == OP_LSHIFT);
380 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000381 int total;
382 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100384 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200385#ifdef FEAT_VARTABS
386 int *p_vts = curbuf->b_p_vts_array;
387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000391 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 int i = 0, j = 0;
393 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#ifdef FEAT_RIGHTLEFT
395 int old_p_ri = p_ri;
396
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200397 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398#endif
399
400 State = INSERT; /* don't want REPLACE for State */
401 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
402 if (bd.is_short)
403 return;
404
405 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200406 total = (int)((unsigned)amount * (unsigned)p_sw);
407 if ((total / p_sw) != amount)
408 return; /* multiplication overflow */
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 oldp = ml_get_curline();
411
412 if (!left)
413 {
414 /*
415 * 1. Get start vcol
416 * 2. Total ws vcols
417 * 3. Divvy into TABs & spp
418 * 4. Construct new string
419 */
420 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
421 ws_vcol = bd.start_vcol - bd.pre_whitesp;
422 if (bd.startspaces)
423 {
424#ifdef FEAT_MBYTE
425 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100426 {
427 if ((*mb_ptr2len)(bd.textstart) == 1)
428 ++bd.textstart;
429 else
430 {
431 ws_vcol = 0;
432 bd.startspaces = 0;
433 }
434 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000437 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100439 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200441 /* TODO: is passing bd.textstart for start of the line OK? */
442 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
443 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 total += incr;
445 bd.start_vcol += incr;
446 }
447 /* OK, now total=all the VWS reqd, and textstart points at the 1st
448 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200449#ifdef FEAT_VARTABS
450 if (!curbuf->b_p_et)
451 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
452 else
453 j = total;
454#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (!curbuf->b_p_et)
456 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
457 if (i)
458 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
459 else
460 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 /* if we're splitting a TAB, allow for it */
463 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
464 len = (int)STRLEN(bd.textstart) + 1;
465 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
466 if (newp == NULL)
467 return;
468 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
469 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200470 vim_memset(newp + bd.textcol, TAB, (size_t)i);
471 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 /* the end */
473 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
474 }
475 else /* left */
476 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000477 colnr_T destination_col; /* column to which text in block will
478 be shifted */
479 char_u *verbatim_copy_end; /* end of the part of the line which is
480 copied verbatim */
481 colnr_T verbatim_copy_width;/* the (displayed) width of this part
482 of line */
483 unsigned fill; /* nr of spaces that replace a TAB */
484 unsigned new_line_len; /* the length of the line after the
485 block shift */
486 size_t block_space_width;
487 size_t shift_amount;
488 char_u *non_white = bd.textstart;
489 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000491 /*
492 * Firstly, let's find the first non-whitespace character that is
493 * displayed after the block's start column and the character's column
494 * number. Also, let's calculate the width of all the whitespace
495 * characters that are displayed in the block and precede the searched
496 * non-whitespace character.
497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000499 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
500 * the part of which is displayed at the block's beginning. Let's start
501 * searching from the next character. */
502 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100503 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000504
505 /* The character's column is in "bd.start_vcol". */
506 non_white_col = bd.start_vcol;
507
Bram Moolenaar1c465442017-03-12 20:10:05 +0100508 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000509 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000511 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
513
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000514 block_space_width = non_white_col - oap->start_vcol;
515 /* We will shift by "total" or "block_space_width", whichever is less.
516 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000517 shift_amount = (block_space_width < (size_t)total
518 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000519
520 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000521 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000522
523 /* Now let's find out how much of the beginning of the line we can
524 * reuse without modification. */
525 verbatim_copy_end = bd.textstart;
526 verbatim_copy_width = bd.start_vcol;
527
528 /* If "bd.startspaces" is set, "bd.textstart" points to the character
529 * preceding the block. We have to subtract its width to obtain its
530 * column number. */
531 if (bd.startspaces)
532 verbatim_copy_width -= bd.start_char_vcols;
533 while (verbatim_copy_width < destination_col)
534 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200535 char_u *line = verbatim_copy_end;
536
537 /* TODO: is passing verbatim_copy_end for start of the line OK? */
538 incr = lbr_chartabsize(line, verbatim_copy_end,
539 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000540 if (verbatim_copy_width + incr > destination_col)
541 break;
542 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100543 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000544 }
545
546 /* If "destination_col" is different from the width of the initial
547 * part of the line that will be copied, it means we encountered a tab
548 * character, which we will have to partly replace with spaces. */
549 fill = destination_col - verbatim_copy_width;
550
551 /* The replacement line will consist of:
552 * - the beginning of the original line up to "verbatim_copy_end",
553 * - "fill" number of spaces,
554 * - the rest of the line, pointed to by non_white. */
555 new_line_len = (unsigned)(verbatim_copy_end - oldp)
556 + fill
557 + (unsigned)STRLEN(non_white) + 1;
558
559 newp = alloc_check(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (newp == NULL)
561 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000562 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200563 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000564 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 /* replace the line */
567 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
568 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
569 State = oldstate;
570 curwin->w_cursor.col = oldcol;
571#ifdef FEAT_RIGHTLEFT
572 p_ri = old_p_ri;
573#endif
574}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Insert string "s" (b_insert ? before : after) block :AKelly
578 * Caller must prepare for undo.
579 */
580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100581block_insert(
582 oparg_T *oap,
583 char_u *s,
584 int b_insert,
585 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 int p_ts;
588 int count = 0; /* extra spaces to replace a cut TAB */
589 int spaces = 0; /* non-zero if cutting a TAB */
590 colnr_T offset; /* pointer along new line */
591 unsigned s_len; /* STRLEN(s) */
592 char_u *newp, *oldp; /* new, old lines */
593 linenr_T lnum; /* loop var */
594 int oldstate = State;
595
596 State = INSERT; /* don't want REPLACE for State */
597 s_len = (unsigned)STRLEN(s);
598
599 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
600 {
601 block_prep(oap, bdp, lnum, TRUE);
602 if (bdp->is_short && b_insert)
603 continue; /* OP_INSERT, line ends before block start */
604
605 oldp = ml_get(lnum);
606
607 if (b_insert)
608 {
609 p_ts = bdp->start_char_vcols;
610 spaces = bdp->startspaces;
611 if (spaces != 0)
612 count = p_ts - 1; /* we're cutting a TAB */
613 offset = bdp->textcol;
614 }
615 else /* append */
616 {
617 p_ts = bdp->end_char_vcols;
618 if (!bdp->is_short) /* spaces = padding after block */
619 {
620 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
621 if (spaces != 0)
622 count = p_ts - 1; /* we're cutting a TAB */
623 offset = bdp->textcol + bdp->textlen - (spaces != 0);
624 }
625 else /* spaces = padding to block edge */
626 {
627 /* if $ used, just append to EOL (ie spaces==0) */
628 if (!bdp->is_MAX)
629 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
630 count = spaces;
631 offset = bdp->textcol + bdp->textlen;
632 }
633 }
634
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200635#ifdef FEAT_MBYTE
636 if (has_mbyte && spaces > 0)
637 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100638 int off;
639
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200640 /* Avoid starting halfway a multi-byte character. */
641 if (b_insert)
642 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100643 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200644 }
645 else
646 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100647 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200648 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200649 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100650 spaces -= off;
651 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200652 }
653#endif
654
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
656 if (newp == NULL)
657 continue;
658
659 /* copy up to shifted part */
660 mch_memmove(newp, oldp, (size_t)(offset));
661 oldp += offset;
662
663 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200664 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666 /* copy the new text */
667 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
668 offset += s_len;
669
670 if (spaces && !bdp->is_short)
671 {
672 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200673 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 /* We're splitting a TAB, don't copy it. */
675 oldp++;
676 /* We allowed for that TAB, remember this now */
677 count++;
678 }
679
680 if (spaces > 0)
681 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000682 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 ml_replace(lnum, newp, FALSE);
685
686 if (lnum == oap->end.lnum)
687 {
688 /* Set "']" mark to the end of the block instead of the end of
689 * the insert in the first line. */
690 curbuf->b_op_end.lnum = oap->end.lnum;
691 curbuf->b_op_end.col = offset;
692 }
693 } /* for all lnum */
694
695 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
696
697 State = oldstate;
698}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
700#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
701/*
702 * op_reindent - handle reindenting a block of lines.
703 */
704 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100705op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 long i;
708 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200709 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 linenr_T first_changed = 0;
711 linenr_T last_changed = 0;
712 linenr_T start_lnum = curwin->w_cursor.lnum;
713
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000714 /* Don't even try when 'modifiable' is off. */
715 if (!curbuf->b_p_ma)
716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000718 return;
719 }
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 for (i = oap->line_count; --i >= 0 && !got_int; )
722 {
723 /* it's a slow thing to do, so give feedback so there's no worry that
724 * the computer's just hung. */
725
726 if (i > 1
727 && (i % 50 == 0 || i == oap->line_count - 1)
728 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100729 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731 /*
732 * Be vi-compatible: For lisp indenting the first line is not
733 * indented, unless there is only one line.
734 */
735#ifdef FEAT_LISP
736 if (i != oap->line_count - 1 || oap->line_count == 1
737 || how != get_lisp_indent)
738#endif
739 {
740 l = skipwhite(ml_get_curline());
741 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200742 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200744 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200746 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
748 /* did change the indent, call changed_lines() later */
749 if (first_changed == 0)
750 first_changed = curwin->w_cursor.lnum;
751 last_changed = curwin->w_cursor.lnum;
752 }
753 }
754 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000755 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757
758 /* put cursor on first non-blank of indented line */
759 curwin->w_cursor.lnum = start_lnum;
760 beginline(BL_SOL | BL_FIX);
761
762 /* Mark changed lines so that they will be redrawn. When Visual
763 * highlighting was present, need to continue until the last line. When
764 * there is no change still need to remove the Visual highlighting. */
765 if (last_changed != 0)
766 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 else if (oap->is_VIsual)
770 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
772 if (oap->line_count > p_report)
773 {
774 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200776 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 /* set '[ and '] marks */
779 curbuf->b_op_start = oap->start;
780 curbuf->b_op_end = oap->end;
781}
782#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
783
784#if defined(FEAT_EVAL) || defined(PROTO)
785/*
786 * Keep the last expression line here, for repeating.
787 */
788static char_u *expr_line = NULL;
789
790/*
791 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
792 * Returns '=' when OK, NUL otherwise.
793 */
794 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100795get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 char_u *new_line;
798
799 new_line = getcmdline('=', 0L, 0);
800 if (new_line == NULL)
801 return NUL;
802 if (*new_line == NUL) /* use previous line */
803 vim_free(new_line);
804 else
805 set_expr_line(new_line);
806 return '=';
807}
808
809/*
810 * Set the expression for the '=' register.
811 * Argument must be an allocated string.
812 */
813 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100814set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 vim_free(expr_line);
817 expr_line = new_line;
818}
819
820/*
821 * Get the result of the '=' register expression.
822 * Returns a pointer to allocated memory, or NULL for failure.
823 */
824 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100825get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
827 char_u *expr_copy;
828 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000829 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 if (expr_line == NULL)
832 return NULL;
833
834 /* Make a copy of the expression, because evaluating it may cause it to be
835 * changed. */
836 expr_copy = vim_strsave(expr_line);
837 if (expr_copy == NULL)
838 return NULL;
839
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000840 /* When we are invoked recursively limit the evaluation to 10 levels.
841 * Then return the string as-is. */
842 if (nested >= 10)
843 return expr_copy;
844
845 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000846 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000847 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 vim_free(expr_copy);
849 return rv;
850}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000851
852/*
853 * Get the '=' register expression itself, without evaluating it.
854 */
855 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100856get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000857{
858 if (expr_line == NULL)
859 return NULL;
860 return vim_strsave(expr_line);
861}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif /* FEAT_EVAL */
863
864/*
865 * Check if 'regname' is a valid name of a yank register.
866 * Note: There is no check for 0 (default register), caller should do this
867 */
868 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100869valid_yank_reg(
870 int regname,
871 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872{
873 if ( (regname > 0 && ASCII_ISALNUM(regname))
874 || (!writing && vim_strchr((char_u *)
875#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100876 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100878 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
880 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100881 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 || regname == '"'
883 || regname == '-'
884 || regname == '_'
885#ifdef FEAT_CLIPBOARD
886 || regname == '*'
887 || regname == '+'
888#endif
889#ifdef FEAT_DND
890 || (!writing && regname == '~')
891#endif
892 )
893 return TRUE;
894 return FALSE;
895}
896
897/*
898 * Set y_current and y_append, according to the value of "regname".
899 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000900 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 *
902 * If regname is 0 and writing, use register 0
903 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100904 *
905 * Return TRUE when the register should be inserted literally (selection or
906 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100909get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100912 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914 y_append = FALSE;
915 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
916 {
917 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100918 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 i = regname;
921 if (VIM_ISDIGIT(i))
922 i -= '0';
923 else if (ASCII_ISLOWER(i))
924 i = CharOrdLow(i) + 10;
925 else if (ASCII_ISUPPER(i))
926 {
927 i = CharOrdUp(i) + 10;
928 y_append = TRUE;
929 }
930 else if (regname == '-')
931 i = DELETION_REGISTER;
932#ifdef FEAT_CLIPBOARD
933 /* When selection is not available, use register 0 instead of '*' */
934 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100937 ret = TRUE;
938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 /* When clipboard is not available, use register 0 instead of '+' */
940 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100943 ret = TRUE;
944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#endif
946#ifdef FEAT_DND
947 else if (!writing && regname == '~')
948 i = TILDE_REGISTER;
949#endif
950 else /* not 0-9, a-z, A-Z or '-': use register 0 */
951 i = 0;
952 y_current = &(y_regs[i]);
953 if (writing) /* remember the register we write into for do_put() */
954 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100955 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
Bram Moolenaar8299df92004-07-10 09:47:34 +0000958#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959/*
960 * When "regname" is a clipboard register, obtain the selection. If it's not
961 * available return zero, otherwise return "regname".
962 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000963 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100964may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
966 if (regname == '*')
967 {
968 if (!clip_star.available)
969 regname = 0;
970 else
971 clip_get_selection(&clip_star);
972 }
973 else if (regname == '+')
974 {
975 if (!clip_plus.available)
976 regname = 0;
977 else
978 clip_get_selection(&clip_plus);
979 }
980 return regname;
981}
982#endif
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984/*
985 * Obtain the contents of a "normal" register. The register is made empty.
986 * The returned pointer has allocated memory, use put_register() later.
987 */
988 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100989get_register(
990 int name,
991 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200993 yankreg_T *reg;
994 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996#ifdef FEAT_CLIPBOARD
997 /* When Visual area changed, may have to update selection. Obtain the
998 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000999 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001001 if (clip_isautosel_star())
1002 clip_update_selection(&clip_star);
1003 may_get_selection(name);
1004 }
1005 if (name == '+' && clip_plus.available)
1006 {
1007 if (clip_isautosel_plus())
1008 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 may_get_selection(name);
1010 }
1011#endif
1012
1013 get_yank_register(name, 0);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001014 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (reg != NULL)
1016 {
1017 *reg = *y_current;
1018 if (copy)
1019 {
1020 /* If we run out of memory some or all of the lines are empty. */
1021 if (reg->y_size == 0)
1022 reg->y_array = NULL;
1023 else
1024 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
1025 * reg->y_size));
1026 if (reg->y_array != NULL)
1027 {
1028 for (i = 0; i < reg->y_size; ++i)
1029 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1030 }
1031 }
1032 else
1033 y_current->y_array = NULL;
1034 }
1035 return (void *)reg;
1036}
1037
1038/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001039 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
1041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001042put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 get_yank_register(name, 0);
1045 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001046 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001047 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001049#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 /* Send text written to clipboard register to the clipboard. */
1051 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001054
Bram Moolenaar113e1072019-01-20 15:30:40 +01001055#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1056 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001058free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001059{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001060 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001061
1062 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001063 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001064 free_yank_all();
1065 vim_free(reg);
1066 *y_current = tmp;
1067}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070#if defined(FEAT_MOUSE) || defined(PROTO)
1071/*
1072 * return TRUE if the current yank register has type MLINE
1073 */
1074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001075yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1078 return FALSE;
1079 if (regname == '_') /* black hole is always empty */
1080 return FALSE;
1081 get_yank_register(regname, FALSE);
1082 return (y_current->y_type == MLINE);
1083}
1084#endif
1085
1086/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001087 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001089 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
1091 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001092do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaard55de222007-05-06 13:38:48 +00001094 char_u *p;
1095 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001096 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001097 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001099 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001101 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1103 retval = FAIL;
1104 else
1105 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001106 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 showmode();
1108 regname = c;
1109 retval = OK;
1110 }
1111 }
1112 else /* stop recording */
1113 {
1114 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001115 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1116 * needs to be removed again to put it in a register. exec_reg then
1117 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001119 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001120 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 p = get_recorded();
1122 if (p == NULL)
1123 retval = FAIL;
1124 else
1125 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001126 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1127 vim_unescape_csi(p);
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 /*
1130 * We don't want to change the default register here, so save and
1131 * restore the current register name.
1132 */
1133 old_y_previous = y_previous;
1134 old_y_current = y_current;
1135
1136 retval = stuff_yank(regname, p);
1137
1138 y_previous = old_y_previous;
1139 y_current = old_y_current;
1140 }
1141 }
1142 return retval;
1143}
1144
1145/*
1146 * Stuff string "p" into yank register "regname" as a single line (append if
1147 * uppercase). "p" must have been alloced.
1148 *
1149 * return FAIL for failure, OK otherwise
1150 */
1151 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001152stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153{
1154 char_u *lp;
1155 char_u **pp;
1156
1157 /* check for read-only register */
1158 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1159 {
1160 vim_free(p);
1161 return FAIL;
1162 }
1163 if (regname == '_') /* black hole: don't do anything */
1164 {
1165 vim_free(p);
1166 return OK;
1167 }
1168 get_yank_register(regname, TRUE);
1169 if (y_append && y_current->y_array != NULL)
1170 {
1171 pp = &(y_current->y_array[y_current->y_size - 1]);
1172 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1173 if (lp == NULL)
1174 {
1175 vim_free(p);
1176 return FAIL;
1177 }
1178 STRCPY(lp, *pp);
1179 STRCAT(lp, p);
1180 vim_free(p);
1181 vim_free(*pp);
1182 *pp = lp;
1183 }
1184 else
1185 {
1186 free_yank_all();
1187 if ((y_current->y_array =
1188 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1189 {
1190 vim_free(p);
1191 return FAIL;
1192 }
1193 y_current->y_array[0] = p;
1194 y_current->y_size = 1;
1195 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001196#ifdef FEAT_VIMINFO
1197 y_current->y_time_set = vim_time();
1198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200 return OK;
1201}
1202
Bram Moolenaar42b94362009-05-26 16:12:37 +00001203static int execreg_lastc = NUL;
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001206 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001208 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001211do_execreg(
1212 int regname,
1213 int colon, /* insert ':' before each line */
1214 int addcr, /* always add '\n' to end of line */
1215 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 long i;
1218 char_u *p;
1219 int retval = OK;
1220 int remap;
1221
1222 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001223 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001224 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001227 return FAIL;
1228 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001229 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 /* check for valid regname */
1232 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001233 {
1234 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001236 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001237 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239#ifdef FEAT_CLIPBOARD
1240 regname = may_get_selection(regname);
1241#endif
1242
1243 if (regname == '_') /* black hole: don't stuff anything */
1244 return OK;
1245
1246#ifdef FEAT_CMDHIST
1247 if (regname == ':') /* use last command line */
1248 {
1249 if (last_cmdline == NULL)
1250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001251 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 return FAIL;
1253 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001254 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001255 /* Escape all control characters with a CTRL-V */
1256 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001257 (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 +00001258 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001259 {
1260 /* When in Visual mode "'<,'>" will be prepended to the command.
1261 * Remove it when it's already there. */
1262 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001263 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001264 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001265 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001266 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001267 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269#endif
1270#ifdef FEAT_EVAL
1271 else if (regname == '=')
1272 {
1273 p = get_expr_line();
1274 if (p == NULL)
1275 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001276 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 vim_free(p);
1278 }
1279#endif
1280 else if (regname == '.') /* use last inserted text */
1281 {
1282 p = get_last_insert_save();
1283 if (p == NULL)
1284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001285 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 return FAIL;
1287 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001288 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 vim_free(p);
1290 }
1291 else
1292 {
1293 get_yank_register(regname, FALSE);
1294 if (y_current->y_array == NULL)
1295 return FAIL;
1296
1297 /* Disallow remaping for ":@r". */
1298 remap = colon ? REMAP_NONE : REMAP_YES;
1299
1300 /*
1301 * Insert lines into typeahead buffer, from last one to first one.
1302 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001303 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 for (i = y_current->y_size; --i >= 0; )
1305 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001306 char_u *escaped;
1307
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 /* insert NL between lines and after last line if type is MLINE */
1309 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1310 || addcr)
1311 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001312 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 return FAIL;
1314 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001315 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1316 if (escaped == NULL)
1317 return FAIL;
1318 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1319 vim_free(escaped);
1320 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001322 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 == FAIL)
1324 return FAIL;
1325 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001326 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 return retval;
1329}
1330
1331/*
1332 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1333 * used only after other typeahead has been processed.
1334 */
1335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001336put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
1338 char_u buf[3];
1339
1340 if (restart_edit != NUL)
1341 {
1342 if (restart_edit == 'V')
1343 {
1344 buf[0] = 'g';
1345 buf[1] = 'R';
1346 buf[2] = NUL;
1347 }
1348 else
1349 {
1350 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1351 buf[1] = NUL;
1352 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001353 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 restart_edit = NUL;
1355 }
1356}
1357
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001358/*
1359 * Insert register contents "s" into the typeahead buffer, so that it will be
1360 * executed again.
1361 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1362 * no remapping.
1363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001365put_in_typebuf(
1366 char_u *s,
1367 int esc,
1368 int colon, /* add ':' before the line */
1369 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 int retval = OK;
1372
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001373 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001375 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001377 {
1378 char_u *p;
1379
1380 if (esc)
1381 p = vim_strsave_escape_csi(s);
1382 else
1383 p = s;
1384 if (p == NULL)
1385 retval = FAIL;
1386 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001387 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1388 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001389 if (esc)
1390 vim_free(p);
1391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001393 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 return retval;
1395}
1396
1397/*
1398 * Insert a yank register: copy it into the Read buffer.
1399 * Used by CTRL-R command and middle mouse button in insert mode.
1400 *
1401 * return FAIL for failure, OK otherwise
1402 */
1403 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001404insert_reg(
1405 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001406 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 long i;
1409 int retval = OK;
1410 char_u *arg;
1411 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001412 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414 /*
1415 * It is possible to get into an endless loop by having CTRL-R a in
1416 * register a and then, in insert mode, doing CTRL-R a.
1417 * If you hit CTRL-C, the loop will be broken here.
1418 */
1419 ui_breakcheck();
1420 if (got_int)
1421 return FAIL;
1422
1423 /* check for valid regname */
1424 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1425 return FAIL;
1426
1427#ifdef FEAT_CLIPBOARD
1428 regname = may_get_selection(regname);
1429#endif
1430
1431 if (regname == '.') /* insert last inserted text */
1432 retval = stuff_inserted(NUL, 1L, TRUE);
1433 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1434 {
1435 if (arg == NULL)
1436 return FAIL;
1437 stuffescaped(arg, literally);
1438 if (allocated)
1439 vim_free(arg);
1440 }
1441 else /* name or number register */
1442 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001443 if (get_yank_register(regname, FALSE))
1444 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (y_current->y_array == NULL)
1446 retval = FAIL;
1447 else
1448 {
1449 for (i = 0; i < y_current->y_size; ++i)
1450 {
1451 stuffescaped(y_current->y_array[i], literally);
1452 /*
1453 * Insert a newline between lines and after last line if
1454 * y_type is MLINE.
1455 */
1456 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1457 stuffcharReadbuff('\n');
1458 }
1459 }
1460 }
1461
1462 return retval;
1463}
1464
1465/*
1466 * Stuff a string into the typeahead buffer, such that edit() will insert it
1467 * literally ("literally" TRUE) or interpret is as typed characters.
1468 */
1469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001470stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 int c;
1473 char_u *start;
1474
1475 while (*arg != NUL)
1476 {
1477 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1478 * stuff K_SPECIAL to get the effect of a special key when "literally"
1479 * is TRUE. */
1480 start = arg;
1481 while ((*arg >= ' '
1482#ifndef EBCDIC
1483 && *arg < DEL /* EBCDIC: chars above space are normal */
1484#endif
1485 )
1486 || (*arg == K_SPECIAL && !literally))
1487 ++arg;
1488 if (arg > start)
1489 stuffReadbuffLen(start, (long)(arg - start));
1490
1491 /* stuff a single special character */
1492 if (*arg != NUL)
1493 {
1494#ifdef FEAT_MBYTE
1495 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001496 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 else
1498#endif
1499 c = *arg++;
1500 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1501 stuffcharReadbuff(Ctrl_V);
1502 stuffcharReadbuff(c);
1503 }
1504 }
1505}
1506
1507/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001508 * If "regname" is a special register, return TRUE and store a pointer to its
1509 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512get_spec_reg(
1513 int regname,
1514 char_u **argp,
1515 int *allocated, /* return: TRUE when value was allocated */
1516 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 int cnt;
1519
1520 *argp = NULL;
1521 *allocated = FALSE;
1522 switch (regname)
1523 {
1524 case '%': /* file name */
1525 if (errmsg)
1526 check_fname(); /* will give emsg if not set */
1527 *argp = curbuf->b_fname;
1528 return TRUE;
1529
1530 case '#': /* alternate file name */
1531 *argp = getaltfname(errmsg); /* may give emsg if not set */
1532 return TRUE;
1533
1534#ifdef FEAT_EVAL
1535 case '=': /* result of expression */
1536 *argp = get_expr_line();
1537 *allocated = TRUE;
1538 return TRUE;
1539#endif
1540
1541 case ':': /* last command line */
1542 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 *argp = last_cmdline;
1545 return TRUE;
1546
1547 case '/': /* last search-pattern */
1548 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 *argp = last_search_pat();
1551 return TRUE;
1552
1553 case '.': /* last inserted text */
1554 *argp = get_last_insert_save();
1555 *allocated = TRUE;
1556 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return TRUE;
1559
1560#ifdef FEAT_SEARCHPATH
1561 case Ctrl_F: /* Filename under cursor */
1562 case Ctrl_P: /* Path under cursor, expand via "path" */
1563 if (!errmsg)
1564 return FALSE;
1565 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001566 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 *allocated = TRUE;
1568 return TRUE;
1569#endif
1570
1571 case Ctrl_W: /* word under cursor */
1572 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1573 if (!errmsg)
1574 return FALSE;
1575 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1576 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1577 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1578 *allocated = TRUE;
1579 return TRUE;
1580
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001581 case Ctrl_L: /* Line under cursor */
1582 if (!errmsg)
1583 return FALSE;
1584
1585 *argp = ml_get_buf(curwin->w_buffer,
1586 curwin->w_cursor.lnum, FALSE);
1587 return TRUE;
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 case '_': /* black hole: always empty */
1590 *argp = (char_u *)"";
1591 return TRUE;
1592 }
1593
1594 return FALSE;
1595}
1596
1597/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001598 * Paste a yank register into the command line.
1599 * Only for non-special registers.
1600 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 * insert_reg() can't be used here, because special characters from the
1602 * register contents will be interpreted as commands.
1603 *
1604 * return FAIL for failure, OK otherwise
1605 */
1606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001607cmdline_paste_reg(
1608 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001609 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001610 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001613 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001615 if (get_yank_register(regname, FALSE))
1616 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (y_current->y_array == NULL)
1618 return FAIL;
1619
1620 for (i = 0; i < y_current->y_size; ++i)
1621 {
1622 cmdline_paste_str(y_current->y_array[i], literally);
1623
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001624 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001625 * Don't do this when "remcr" is TRUE. */
1626 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 cmdline_paste_str((char_u *)"\r", literally);
1628
1629 /* Check for CTRL-C, in case someone tries to paste a few thousand
1630 * lines and gets bored. */
1631 ui_breakcheck();
1632 if (got_int)
1633 return FAIL;
1634 }
1635 return OK;
1636}
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1639/*
1640 * Adjust the register name pointed to with "rp" for the clipboard being
1641 * used always and the clipboard being available.
1642 */
1643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001644adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001646 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1647 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001648 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1649 {
1650 if (clip_unnamed != 0)
1651 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001652 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001653 else
1654 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1655 ? '+' : '*';
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (!clip_star.available && *rp == '*')
1658 *rp = 0;
1659 if (!clip_plus.available && *rp == '+')
1660 *rp = 0;
1661}
1662#endif
1663
1664/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001665 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1666 */
1667 void
1668shift_delete_registers()
1669{
1670 int n;
1671
1672 y_current = &y_regs[9];
1673 free_yank_all(); /* free register nine */
1674 for (n = 9; n > 1; --n)
1675 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001676 y_current = &y_regs[1];
1677 if (!y_append)
1678 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001679 y_regs[1].y_array = NULL; /* set register one to empty */
1680}
1681
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001683 static void
1684yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1685{
1686 static int recursive = FALSE;
1687 dict_T *v_event;
1688 list_T *list;
1689 int n;
1690 char_u buf[NUMBUFLEN + 2];
1691 long reglen = 0;
1692
1693 if (recursive)
1694 return;
1695
1696 v_event = get_vim_var_dict(VV_EVENT);
1697
1698 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001699 if (list == NULL)
1700 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001701 for (n = 0; n < reg->y_size; n++)
1702 list_append_string(list, reg->y_array[n], -1);
1703 list->lv_lock = VAR_FIXED;
1704 dict_add_list(v_event, "regcontents", list);
1705
1706 buf[0] = (char_u)oap->regname;
1707 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001708 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001709
1710 buf[0] = get_op_char(oap->op_type);
1711 buf[1] = get_extra_op_char(oap->op_type);
1712 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001713 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001714
1715 buf[0] = NUL;
1716 buf[1] = NUL;
1717 switch (get_reg_type(oap->regname, &reglen))
1718 {
1719 case MLINE: buf[0] = 'V'; break;
1720 case MCHAR: buf[0] = 'v'; break;
1721 case MBLOCK:
1722 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1723 reglen + 1);
1724 break;
1725 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001726 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001727
1728 /* Lock the dictionary and its keys */
1729 dict_set_items_ro(v_event);
1730
1731 recursive = TRUE;
1732 textlock++;
1733 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1734 textlock--;
1735 recursive = FALSE;
1736
1737 /* Empty the dictionary, v:event is still valid */
1738 dict_free_contents(v_event);
1739 hash_init(&v_event->dv_hashtab);
1740}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001741#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001742
Bram Moolenaara1891842017-02-04 21:34:31 +01001743/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001744 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001746 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 */
1748 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 int n;
1752 linenr_T lnum;
1753 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 char_u *newp, *oldp;
1755 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1757 int did_yank = FALSE;
Bram Moolenaar7c821302012-09-05 14:18:45 +02001758 int orig_regname = oap->regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1761 return OK;
1762
1763 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1764 if (oap->empty)
1765 return u_save_cursor();
1766
1767 if (!curbuf->b_p_ma)
1768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001769 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 return FAIL;
1771 }
1772
1773#ifdef FEAT_CLIPBOARD
1774 adjust_clip_reg(&oap->regname);
1775#endif
1776
1777#ifdef FEAT_MBYTE
1778 if (has_mbyte)
1779 mb_adjust_opend(oap);
1780#endif
1781
Bram Moolenaard04b7502010-07-08 22:27:55 +02001782 /*
1783 * Imitate the strange Vi behaviour: If the delete spans more than one
1784 * line and motion_type == MCHAR and the result is a blank line, make the
1785 * delete linewise. Don't do this for the change command or Visual mode.
1786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001789 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001791 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 && oap->op_type == OP_DELETE)
1793 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001794 ptr = ml_get(oap->end.lnum) + oap->end.col;
1795 if (*ptr != NUL)
1796 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 ptr = skipwhite(ptr);
1798 if (*ptr == NUL && inindent(0))
1799 oap->motion_type = MLINE;
1800 }
1801
Bram Moolenaard04b7502010-07-08 22:27:55 +02001802 /*
1803 * Check for trying to delete (e.g. "D") in an empty line.
1804 * Note: For the change operator it is ok.
1805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if ( oap->motion_type == MCHAR
1807 && oap->line_count == 1
1808 && oap->op_type == OP_DELETE
1809 && *ml_get(oap->start.lnum) == NUL)
1810 {
1811 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001812 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 * 'cpoptions' (Vi compatible).
1814 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001815#ifdef FEAT_VIRTUALEDIT
1816 if (virtual_op)
1817 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1818 * marks as if it happened. */
1819 goto setmarks;
1820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1822 beep_flush();
1823 return OK;
1824 }
1825
Bram Moolenaard04b7502010-07-08 22:27:55 +02001826 /*
1827 * Do a yank of whatever we're about to delete.
1828 * If a yank register was specified, put the deleted text into that
1829 * register. For the black hole register '_' don't yank anything.
1830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if (oap->regname != '_')
1832 {
1833 if (oap->regname != 0)
1834 {
1835 /* check for read-only register */
1836 if (!valid_yank_reg(oap->regname, TRUE))
1837 {
1838 beep_flush();
1839 return OK;
1840 }
1841 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1842 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1843 did_yank = TRUE;
1844 }
1845
1846 /*
1847 * Put deleted text into register 1 and shift number registers if the
1848 * delete contains a line break, or when a regname has been specified.
Bram Moolenaar7c821302012-09-05 14:18:45 +02001849 * Use the register name from before adjust_clip_reg() may have
1850 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
Bram Moolenaar7c821302012-09-05 14:18:45 +02001852 if (orig_regname != 0 || oap->motion_type == MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 || oap->line_count > 1 || oap->use_reg_one)
1854 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001855 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (op_yank(oap, TRUE, FALSE) == OK)
1857 did_yank = TRUE;
1858 }
1859
Bram Moolenaar84298db2012-04-20 13:46:08 +02001860 /* Yank into small delete register when no named register specified
1861 * and the delete is within one line. */
1862 if ((
1863#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001864 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1865 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001866#endif
1867 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 && oap->line_count == 1)
1869 {
1870 oap->regname = '-';
1871 get_yank_register(oap->regname, TRUE);
1872 if (op_yank(oap, TRUE, FALSE) == OK)
1873 did_yank = TRUE;
1874 oap->regname = 0;
1875 }
1876
1877 /*
1878 * If there's too much stuff to fit in the yank register, then get a
1879 * confirmation before doing the delete. This is crude, but simple.
1880 * And it avoids doing a delete of something we can't put back if we
1881 * want.
1882 */
1883 if (!did_yank)
1884 {
1885 int msg_silent_save = msg_silent;
1886
1887 msg_silent = 0; /* must display the prompt */
1888 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1889 msg_silent = msg_silent_save;
1890 if (n != 'y')
1891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001892 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 return FAIL;
1894 }
1895 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001896
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001898 if (did_yank && has_textyankpost())
1899 yank_do_autocmd(oap, y_current);
1900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902
Bram Moolenaard04b7502010-07-08 22:27:55 +02001903 /*
1904 * block mode delete
1905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (oap->block_mode)
1907 {
1908 if (u_save((linenr_T)(oap->start.lnum - 1),
1909 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1910 return FAIL;
1911
1912 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1913 {
1914 block_prep(oap, &bd, lnum, TRUE);
1915 if (bd.textlen == 0) /* nothing to delete */
1916 continue;
1917
1918 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1919 if (lnum == curwin->w_cursor.lnum)
1920 {
1921 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1922# ifdef FEAT_VIRTUALEDIT
1923 curwin->w_cursor.coladd = 0;
1924# endif
1925 }
1926
1927 /* n == number of chars deleted
1928 * If we delete a TAB, it may be replaced by several characters.
1929 * Thus the number of characters may increase!
1930 */
1931 n = bd.textlen - bd.startspaces - bd.endspaces;
1932 oldp = ml_get(lnum);
1933 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1934 if (newp == NULL)
1935 continue;
1936 /* copy up to deleted part */
1937 mch_memmove(newp, oldp, (size_t)bd.textcol);
1938 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001939 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 (size_t)(bd.startspaces + bd.endspaces));
1941 /* copy the part after the deleted part */
1942 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001943 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /* replace the line */
1945 ml_replace(lnum, newp, FALSE);
1946 }
1947
1948 check_cursor_col();
1949 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1950 oap->end.lnum + 1, 0L);
1951 oap->line_count = 0; /* no lines deleted */
1952 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001953 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 if (oap->op_type == OP_CHANGE)
1956 {
1957 /* Delete the lines except the first one. Temporarily move the
1958 * cursor to the next line. Save the current line number, if the
1959 * last line is deleted it may be changed.
1960 */
1961 if (oap->line_count > 1)
1962 {
1963 lnum = curwin->w_cursor.lnum;
1964 ++curwin->w_cursor.lnum;
1965 del_lines((long)(oap->line_count - 1), TRUE);
1966 curwin->w_cursor.lnum = lnum;
1967 }
1968 if (u_save_cursor() == FAIL)
1969 return FAIL;
1970 if (curbuf->b_p_ai) /* don't delete indent */
1971 {
1972 beginline(BL_WHITE); /* cursor on first non-white */
1973 did_ai = TRUE; /* delete the indent when ESC hit */
1974 ai_col = curwin->w_cursor.col;
1975 }
1976 else
1977 beginline(0); /* cursor in column 0 */
1978 truncate_line(FALSE); /* delete the rest of the line */
1979 /* leave cursor past last char in line */
1980 if (oap->line_count > 1)
1981 u_clearline(); /* "U" command not possible after "2cc" */
1982 }
1983 else
1984 {
1985 del_lines(oap->line_count, TRUE);
1986 beginline(BL_WHITE | BL_FIX);
1987 u_clearline(); /* "U" command not possible after "dd" */
1988 }
1989 }
1990 else
1991 {
1992#ifdef FEAT_VIRTUALEDIT
1993 if (virtual_op)
1994 {
1995 int endcol = 0;
1996
1997 /* For virtualedit: break the tabs that are partly included. */
1998 if (gchar_pos(&oap->start) == '\t')
1999 {
2000 if (u_save_cursor() == FAIL) /* save first line for undo */
2001 return FAIL;
2002 if (oap->line_count == 1)
2003 endcol = getviscol2(oap->end.col, oap->end.coladd);
2004 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
2005 oap->start = curwin->w_cursor;
2006 if (oap->line_count == 1)
2007 {
2008 coladvance(endcol);
2009 oap->end.col = curwin->w_cursor.col;
2010 oap->end.coladd = curwin->w_cursor.coladd;
2011 curwin->w_cursor = oap->start;
2012 }
2013 }
2014
2015 /* Break a tab only when it's included in the area. */
2016 if (gchar_pos(&oap->end) == '\t'
2017 && (int)oap->end.coladd < oap->inclusive)
2018 {
2019 /* save last line for undo */
2020 if (u_save((linenr_T)(oap->end.lnum - 1),
2021 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2022 return FAIL;
2023 curwin->w_cursor = oap->end;
2024 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2025 oap->end = curwin->w_cursor;
2026 curwin->w_cursor = oap->start;
2027 }
2028 }
2029#endif
2030
2031 if (oap->line_count == 1) /* delete characters within one line */
2032 {
2033 if (u_save_cursor() == FAIL) /* save line for undo */
2034 return FAIL;
2035
2036 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002037 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 && oap->op_type == OP_CHANGE
2039 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002040 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 display_dollar(oap->end.col - !oap->inclusive);
2042
2043 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2044
2045#ifdef FEAT_VIRTUALEDIT
2046 if (virtual_op)
2047 {
2048 /* fix up things for virtualedit-delete:
2049 * break the tabs which are going to get in our way
2050 */
2051 char_u *curline = ml_get_curline();
2052 int len = (int)STRLEN(curline);
2053
2054 if (oap->end.coladd != 0
2055 && (int)oap->end.col >= len - 1
2056 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2057 n++;
2058 /* Delete at least one char (e.g, when on a control char). */
2059 if (n == 0 && oap->start.coladd != oap->end.coladd)
2060 n = 1;
2061
2062 /* When deleted a char in the line, reset coladd. */
2063 if (gchar_cursor() != NUL)
2064 curwin->w_cursor.coladd = 0;
2065 }
2066#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02002067 (void)del_bytes((long)n, !virtual_op,
2068 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 else /* delete characters between lines */
2071 {
2072 pos_T curpos;
2073
2074 /* save deleted and changed lines for undo */
2075 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2076 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2077 return FAIL;
2078
2079 truncate_line(TRUE); /* delete from cursor to end of line */
2080
2081 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2082 ++curwin->w_cursor.lnum;
2083 del_lines((long)(oap->line_count - 2), FALSE);
2084
Bram Moolenaard009e862015-06-09 20:20:03 +02002085 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002086 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002087 curwin->w_cursor.col = 0;
2088 (void)del_bytes((long)n, !virtual_op,
2089 oap->op_type == OP_DELETE && !oap->is_VIsual);
2090 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2091 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 }
2094
2095 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2096
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002097#ifdef FEAT_VIRTUALEDIT
2098setmarks:
2099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (oap->block_mode)
2101 {
2102 curbuf->b_op_end.lnum = oap->end.lnum;
2103 curbuf->b_op_end.col = oap->start.col;
2104 }
2105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 curbuf->b_op_end = oap->start;
2107 curbuf->b_op_start = oap->start;
2108
2109 return OK;
2110}
2111
2112#ifdef FEAT_MBYTE
2113/*
2114 * Adjust end of operating area for ending on a multi-byte character.
2115 * Used for deletion.
2116 */
2117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 char_u *p;
2121
2122 if (oap->inclusive)
2123 {
2124 p = ml_get(oap->end.lnum);
2125 oap->end.col += mb_tail_off(p, p + oap->end.col);
2126 }
2127}
2128#endif
2129
Bram Moolenaar630afe82018-06-28 19:26:28 +02002130
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002131#ifdef FEAT_MBYTE
Bram Moolenaar630afe82018-06-28 19:26:28 +02002132/*
2133 * Replace the character under the cursor with "c".
2134 * This takes care of multi-byte characters.
2135 */
2136 static void
2137replace_character(int c)
2138{
2139 int n = State;
2140
2141 State = REPLACE;
2142 ins_char(c);
2143 State = n;
2144 /* Backup to the replaced character. */
2145 dec_cursor();
2146}
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002147#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002148
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149/*
2150 * Replace a whole area with one character.
2151 */
2152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002153op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int n, numc;
2156#ifdef FEAT_MBYTE
2157 int num_chars;
2158#endif
2159 char_u *newp, *oldp;
2160 size_t oldlen;
2161 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002162 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002163 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2166 return OK; /* nothing to do */
2167
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002168 if (c == REPLACE_CR_NCHAR)
2169 {
2170 had_ctrl_v_cr = TRUE;
2171 c = CAR;
2172 }
2173 else if (c == REPLACE_NL_NCHAR)
2174 {
2175 had_ctrl_v_cr = TRUE;
2176 c = NL;
2177 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#ifdef FEAT_MBYTE
2180 if (has_mbyte)
2181 mb_adjust_opend(oap);
2182#endif
2183
2184 if (u_save((linenr_T)(oap->start.lnum - 1),
2185 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2186 return FAIL;
2187
2188 /*
2189 * block mode replace
2190 */
2191 if (oap->block_mode)
2192 {
2193 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2194 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2195 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002196 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2198 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2199 continue; /* nothing to replace */
2200
2201 /* n == number of extra chars required
2202 * If we split a TAB, it may be replaced by several characters.
2203 * Thus the number of characters may increase!
2204 */
2205#ifdef FEAT_VIRTUALEDIT
2206 /* If the range starts in virtual space, count the initial
2207 * coladd offset as part of "startspaces" */
2208 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2209 {
2210 pos_T vpos;
2211
Bram Moolenaara1381de2009-11-03 15:44:21 +00002212 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 getvpos(&vpos, oap->start_vcol);
2214 bd.startspaces += vpos.coladd;
2215 n = bd.startspaces;
2216 }
2217 else
2218#endif
2219 /* allow for pre spaces */
2220 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2221
2222 /* allow for post spp */
2223 n += (bd.endspaces
2224#ifdef FEAT_VIRTUALEDIT
2225 && !bd.is_oneChar
2226#endif
2227 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2228 /* Figure out how many characters to replace. */
2229 numc = oap->end_vcol - oap->start_vcol + 1;
2230 if (bd.is_short && (!virtual_op || bd.is_MAX))
2231 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2232
2233#ifdef FEAT_MBYTE
2234 /* A double-wide character can be replaced only up to half the
2235 * times. */
2236 if ((*mb_char2cells)(c) > 1)
2237 {
2238 if ((numc & 1) && !bd.is_short)
2239 {
2240 ++bd.endspaces;
2241 ++n;
2242 }
2243 numc = numc / 2;
2244 }
2245
2246 /* Compute bytes needed, move character count to num_chars. */
2247 num_chars = numc;
2248 numc *= (*mb_char2len)(c);
2249#endif
2250 /* oldlen includes textlen, so don't double count */
2251 n += numc - bd.textlen;
2252
2253 oldp = ml_get_curline();
2254 oldlen = STRLEN(oldp);
2255 newp = alloc_check((unsigned)oldlen + 1 + n);
2256 if (newp == NULL)
2257 continue;
2258 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2259 /* copy up to deleted part */
2260 mch_memmove(newp, oldp, (size_t)bd.textcol);
2261 oldp += bd.textcol + bd.textlen;
2262 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002263 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002265 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2266 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002267 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002269#ifdef FEAT_MBYTE
2270 if (has_mbyte)
2271 {
2272 n = (int)STRLEN(newp);
2273 while (--num_chars >= 0)
2274 n += (*mb_char2bytes)(c, newp + n);
2275 }
2276 else
2277#endif
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002278 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002279 if (!bd.is_short)
2280 {
2281 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002282 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002283 /* copy the part after the changed part */
2284 STRMOVE(newp + STRLEN(newp), oldp);
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002289 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaarefe06f42013-11-11 23:17:39 +01002290 after_p = alloc_check(
2291 (unsigned)(oldlen + 1 + n - STRLEN(newp)));
Bram Moolenaard9820532013-11-04 01:41:17 +01002292 if (after_p != NULL)
2293 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295 /* replace the line */
2296 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002297 if (after_p != NULL)
2298 {
2299 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2300 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2301 oap->end.lnum++;
2302 vim_free(after_p);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 }
2306 else
2307 {
2308 /*
2309 * MCHAR and MLINE motion replace.
2310 */
2311 if (oap->motion_type == MLINE)
2312 {
2313 oap->start.col = 0;
2314 curwin->w_cursor.col = 0;
2315 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2316 if (oap->end.col)
2317 --oap->end.col;
2318 }
2319 else if (!oap->inclusive)
2320 dec(&(oap->end));
2321
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002322 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 n = gchar_cursor();
2325 if (n != NUL)
2326 {
2327#ifdef FEAT_MBYTE
2328 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2329 {
2330 /* This is slow, but it handles replacing a single-byte
2331 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002332 if (curwin->w_cursor.lnum == oap->end.lnum)
2333 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002334 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336 else
2337#endif
2338 {
2339#ifdef FEAT_VIRTUALEDIT
2340 if (n == TAB)
2341 {
2342 int end_vcol = 0;
2343
2344 if (curwin->w_cursor.lnum == oap->end.lnum)
2345 {
2346 /* oap->end has to be recalculated when
2347 * the tab breaks */
2348 end_vcol = getviscol2(oap->end.col,
2349 oap->end.coladd);
2350 }
2351 coladvance_force(getviscol());
2352 if (curwin->w_cursor.lnum == oap->end.lnum)
2353 getvpos(&oap->end, end_vcol);
2354 }
2355#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002356 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358 }
2359#ifdef FEAT_VIRTUALEDIT
2360 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2361 {
2362 int virtcols = oap->end.coladd;
2363
2364 if (curwin->w_cursor.lnum == oap->start.lnum
2365 && oap->start.col == oap->end.col && oap->start.coladd)
2366 virtcols -= oap->start.coladd;
2367
2368 /* oap->end has been trimmed so it's effectively inclusive;
2369 * as a result an extra +1 must be counted so we don't
2370 * trample the NUL byte. */
2371 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2372 curwin->w_cursor.col -= (virtcols + 1);
2373 for (; virtcols >= 0; virtcols--)
2374 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002375# ifdef FEAT_MBYTE
Bram Moolenaar630afe82018-06-28 19:26:28 +02002376 if ((*mb_char2len)(c) > 1)
2377 replace_character(c);
2378 else
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002379# endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002380 PBYTE(curwin->w_cursor, c);
2381 if (inc(&curwin->w_cursor) == -1)
2382 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 }
2385#endif
2386
2387 /* Advance to next character, stop at the end of the file. */
2388 if (inc_cursor() == -1)
2389 break;
2390 }
2391 }
2392
2393 curwin->w_cursor = oap->start;
2394 check_cursor();
2395 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2396
2397 /* Set "'[" and "']" marks. */
2398 curbuf->b_op_start = oap->start;
2399 curbuf->b_op_end = oap->end;
2400
2401 return OK;
2402}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002404static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002405
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406/*
2407 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2408 */
2409 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002410op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
2412 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002414 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (u_save((linenr_T)(oap->start.lnum - 1),
2417 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2418 return;
2419
2420 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 if (oap->block_mode) /* Visual block mode */
2422 {
2423 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2424 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002425 int one_change;
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 block_prep(oap, &bd, pos.lnum, FALSE);
2428 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002429 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2430 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002431
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002432#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002433 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2436
Bram Moolenaar009b2592004-10-24 19:18:58 +00002437 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2438 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002440 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 if (did_change)
2445 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2446 }
2447 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 if (oap->motion_type == MLINE)
2450 {
2451 oap->start.col = 0;
2452 pos.col = 0;
2453 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2454 if (oap->end.col)
2455 --oap->end.col;
2456 }
2457 else if (!oap->inclusive)
2458 dec(&(oap->end));
2459
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002460 if (pos.lnum == oap->end.lnum)
2461 did_change = swapchars(oap->op_type, &pos,
2462 oap->end.col - pos.col + 1);
2463 else
2464 for (;;)
2465 {
2466 did_change |= swapchars(oap->op_type, &pos,
2467 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2468 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002469 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002470 break;
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (did_change)
2473 {
2474 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2475 0L);
2476#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002477 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 char_u *ptr;
2480 int count;
2481
2482 pos = oap->start;
2483 while (pos.lnum < oap->end.lnum)
2484 {
2485 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002486 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002487 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002489 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 pos.col = 0;
2491 pos.lnum++;
2492 }
2493 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2494 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002495 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002497 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
2499#endif
2500 }
2501 }
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 if (!did_change && oap->is_VIsual)
2504 /* No change: need to remove the Visual selection */
2505 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
2507 /*
2508 * Set '[ and '] marks.
2509 */
2510 curbuf->b_op_start = oap->start;
2511 curbuf->b_op_end = oap->end;
2512
2513 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002514 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002515 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002519 * Invoke swapchar() on "length" bytes at position "pos".
2520 * "pos" is advanced to just after the changed characters.
2521 * "length" is rounded up to include the whole last multi-byte character.
2522 * Also works correctly when the number of bytes changes.
2523 * Returns TRUE if some character was changed.
2524 */
2525 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002526swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002527{
2528 int todo;
2529 int did_change = 0;
2530
2531 for (todo = length; todo > 0; --todo)
2532 {
2533# ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002534 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002535 {
2536 int len = (*mb_ptr2len)(ml_get_pos(pos));
2537
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002538 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002539 if (len > 0)
2540 todo -= len - 1;
2541 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002542# endif
2543 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002544 if (inc(pos) == -1) /* at end of file */
2545 break;
2546 }
2547 return did_change;
2548}
2549
2550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 * If op_type == OP_UPPER: make uppercase,
2552 * if op_type == OP_LOWER: make lowercase,
2553 * if op_type == OP_ROT13: do rot13 encoding,
2554 * else swap case of character at 'pos'
2555 * returns TRUE when something actually changed.
2556 */
2557 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002558swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559{
2560 int c;
2561 int nc;
2562
2563 c = gchar_pos(pos);
2564
2565 /* Only do rot13 encoding for ASCII characters. */
2566 if (c >= 0x80 && op_type == OP_ROT13)
2567 return FALSE;
2568
2569#ifdef FEAT_MBYTE
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002570 if (op_type == OP_UPPER && c == 0xdf
2571 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002572 {
2573 pos_T sp = curwin->w_cursor;
2574
2575 /* Special handling of German sharp s: change to "SS". */
2576 curwin->w_cursor = *pos;
2577 del_char(FALSE);
2578 ins_char('S');
2579 ins_char('S');
2580 curwin->w_cursor = sp;
2581 inc(pos);
2582 }
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2585 return FALSE;
2586#endif
2587 nc = c;
2588 if (MB_ISLOWER(c))
2589 {
2590 if (op_type == OP_ROT13)
2591 nc = ROT13(c, 'a');
2592 else if (op_type != OP_LOWER)
2593 nc = MB_TOUPPER(c);
2594 }
2595 else if (MB_ISUPPER(c))
2596 {
2597 if (op_type == OP_ROT13)
2598 nc = ROT13(c, 'A');
2599 else if (op_type != OP_UPPER)
2600 nc = MB_TOLOWER(c);
2601 }
2602 if (nc != c)
2603 {
2604#ifdef FEAT_MBYTE
2605 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2606 {
2607 pos_T sp = curwin->w_cursor;
2608
2609 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002610 /* don't use del_char(), it also removes composing chars */
2611 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 ins_char(nc);
2613 curwin->w_cursor = sp;
2614 }
2615 else
2616#endif
Bram Moolenaar630afe82018-06-28 19:26:28 +02002617 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 return TRUE;
2619 }
2620 return FALSE;
2621}
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623/*
2624 * op_insert - Insert and append operators for Visual mode.
2625 */
2626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002627op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 long ins_len, pre_textlen = 0;
2630 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002631 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 struct block_def bd;
2633 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002634 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636 /* edit() changes this - record it for OP_APPEND */
2637 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2638
2639 /* vis block is still marked. Get rid of it now. */
2640 curwin->w_cursor.lnum = oap->start.lnum;
2641 update_screen(INVERTED);
2642
2643 if (oap->block_mode)
2644 {
2645#ifdef FEAT_VIRTUALEDIT
2646 /* When 'virtualedit' is used, need to insert the extra spaces before
2647 * doing block_prep(). When only "block" is used, virtual edit is
2648 * already disabled, but still need it when calling
2649 * coladvance_force(). */
2650 if (curwin->w_cursor.coladd > 0)
2651 {
2652 int old_ve_flags = ve_flags;
2653
2654 ve_flags = VE_ALL;
2655 if (u_save_cursor() == FAIL)
2656 return;
2657 coladvance_force(oap->op_type == OP_APPEND
2658 ? oap->end_vcol + 1 : getviscol());
2659 if (oap->op_type == OP_APPEND)
2660 --curwin->w_cursor.col;
2661 ve_flags = old_ve_flags;
2662 }
2663#endif
2664 /* Get the info about the block before entering the text */
2665 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002666 /* Get indent information */
2667 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (oap->op_type == OP_APPEND)
2671 firstline += bd.textlen;
2672 pre_textlen = (long)STRLEN(firstline);
2673 }
2674
2675 if (oap->op_type == OP_APPEND)
2676 {
2677 if (oap->block_mode
2678#ifdef FEAT_VIRTUALEDIT
2679 && curwin->w_cursor.coladd == 0
2680#endif
2681 )
2682 {
2683 /* Move the cursor to the character right of the block. */
2684 curwin->w_set_curswant = TRUE;
2685 while (*ml_get_cursor() != NUL
2686 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2687 ++curwin->w_cursor.col;
2688 if (bd.is_short && !bd.is_MAX)
2689 {
2690 /* First line was too short, make it longer and adjust the
2691 * values in "bd". */
2692 if (u_save_cursor() == FAIL)
2693 return;
2694 for (i = 0; i < bd.endspaces; ++i)
2695 ins_char(' ');
2696 bd.textlen += bd.endspaces;
2697 }
2698 }
2699 else
2700 {
2701 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002702 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002705 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 && oap->start_vcol != oap->end_vcol)
2707 inc_cursor();
2708 }
2709 }
2710
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002711 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002712 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002714 /* When a tab was inserted, and the characters in front of the tab
2715 * have been converted to a tab as well, the column of the cursor
2716 * might have actually been reduced, so need to adjust here. */
2717 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002718 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002719 oap->start = curbuf->b_op_start_orig;
2720
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002721 /* If user has moved off this line, we don't know what to do, so do
2722 * nothing.
2723 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2724 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 return;
2726
2727 if (oap->block_mode)
2728 {
2729 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002730 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002731 size_t len;
2732 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002734 /* If indent kicked in, the firstline might have changed
2735 * but only do that, if the indent actually increased. */
2736 ind_post = (colnr_T)getwhitecols_curline();
2737 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2738 {
2739 bd.textcol += ind_post - ind_pre;
2740 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002741 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002742 }
2743
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002744 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002745 * to adjust the block for that. But only do it, if the difference
2746 * does not come from indent kicking in. */
2747 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2748 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002749 {
2750 if (oap->op_type == OP_INSERT
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002751 && oap->start.col
2752#ifdef FEAT_VIRTUALEDIT
2753 + oap->start.coladd
2754#endif
2755 != curbuf->b_op_start_orig.col
2756#ifdef FEAT_VIRTUALEDIT
2757 + curbuf->b_op_start_orig.coladd
2758#endif
2759 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002760 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002761 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002762#ifdef FEAT_VIRTUALEDIT
2763 curbuf->b_op_start_orig.coladd
2764#else
2765 0
2766#endif
2767 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002768 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002769 pre_textlen -= t - oap->start_vcol;
2770 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002771 }
2772 else if (oap->op_type == OP_APPEND
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002773 && oap->end.col
2774#ifdef FEAT_VIRTUALEDIT
2775 + oap->end.coladd
2776#endif
2777 >= curbuf->b_op_start_orig.col
2778#ifdef FEAT_VIRTUALEDIT
2779 + curbuf->b_op_start_orig.coladd
2780#endif
2781 )
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002782 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002783 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar977239e2019-01-11 16:16:01 +01002784#ifdef FEAT_VIRTUALEDIT
2785 curbuf->b_op_start_orig.coladd
2786#else
2787 0
2788#endif
2789 );
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002790 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002791 /* reset pre_textlen to the value of OP_INSERT */
2792 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002793 pre_textlen -= t - oap->start_vcol;
2794 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002795 oap->op_type = OP_INSERT;
2796 }
2797 }
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 /*
2800 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002801 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * Don't do this when "$" used, end-of-line will have changed.
2803 */
2804 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2805 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2806 {
2807 if (oap->op_type == OP_APPEND)
2808 {
2809 pre_textlen += bd2.textlen - bd.textlen;
2810 if (bd2.endspaces)
2811 --bd2.textlen;
2812 }
2813 bd.textcol = bd2.textcol;
2814 bd.textlen = bd2.textlen;
2815 }
2816
2817 /*
2818 * Subsequent calls to ml_get() flush the firstline data - take a
2819 * copy of the required string.
2820 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002821 firstline = ml_get(oap->start.lnum);
2822 len = STRLEN(firstline);
2823 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002825 add += bd.textlen;
2826 if ((size_t)add > len)
2827 firstline += len; // short line, point to the NUL
2828 else
2829 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002830 if (pre_textlen >= 0
2831 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 ins_text = vim_strnsave(firstline, (int)ins_len);
2834 if (ins_text != NULL)
2835 {
2836 /* block handled here */
2837 if (u_save(oap->start.lnum,
2838 (linenr_T)(oap->end.lnum + 1)) == OK)
2839 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2840 &bd);
2841
2842 curwin->w_cursor.col = oap->start.col;
2843 check_cursor();
2844 vim_free(ins_text);
2845 }
2846 }
2847 }
2848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850/*
2851 * op_change - handle a change operation
2852 *
2853 * return TRUE if edit() returns because of a CTRL-O command
2854 */
2855 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 colnr_T l;
2859 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 long offset;
2861 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002862 long ins_len;
2863 long pre_textlen = 0;
2864 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 char_u *firstline;
2866 char_u *ins_text, *newp, *oldp;
2867 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 l = oap->start.col;
2870 if (oap->motion_type == MLINE)
2871 {
2872 l = 0;
2873#ifdef FEAT_SMARTINDENT
2874 if (!p_paste && curbuf->b_p_si
2875# ifdef FEAT_CINDENT
2876 && !curbuf->b_p_cin
2877# endif
2878 )
2879 can_si = TRUE; /* It's like opening a new line, do si */
2880#endif
2881 }
2882
2883 /* First delete the text in the region. In an empty buffer only need to
2884 * save for undo */
2885 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2886 {
2887 if (u_save_cursor() == FAIL)
2888 return FALSE;
2889 }
2890 else if (op_delete(oap) == FAIL)
2891 return FALSE;
2892
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002893 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 && !virtual_op)
2895 inc_cursor();
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /* check for still on same line (<CR> in inserted text meaningless) */
2898 /* skip blank lines too */
2899 if (oap->block_mode)
2900 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002901#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* Add spaces before getting the current line length. */
2903 if (virtual_op && (curwin->w_cursor.coladd > 0
2904 || gchar_cursor() == NUL))
2905 coladvance_force(getviscol());
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002906#endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002907 firstline = ml_get(oap->start.lnum);
2908 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002909 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 bd.textcol = curwin->w_cursor.col;
2911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
2913#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2914 if (oap->motion_type == MLINE)
2915 fix_indent();
2916#endif
2917
2918 retval = edit(NUL, FALSE, (linenr_T)1);
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002921 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002923 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002925 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002927 /* Auto-indenting may have changed the indent. If the cursor was past
2928 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002930 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002932 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002933
2934 pre_textlen += new_indent - pre_indent;
2935 bd.textcol += new_indent - pre_indent;
2936 }
2937
2938 ins_len = (long)STRLEN(firstline) - pre_textlen;
2939 if (ins_len > 0)
2940 {
2941 /* Subsequent calls to ml_get() flush the firstline data - take a
2942 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2944 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002945 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2947 linenr++)
2948 {
2949 block_prep(oap, &bd, linenr, TRUE);
2950 if (!bd.is_short || virtual_op)
2951 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002952#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 pos_T vpos;
2954
2955 /* If the block starts in virtual space, count the
2956 * initial coladd offset as part of "startspaces" */
2957 if (bd.is_short)
2958 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002959 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
2962 else
2963 vpos.coladd = 0;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 oldp = ml_get(linenr);
2966 newp = alloc_check((unsigned)(STRLEN(oldp)
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002967#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 + vpos.coladd
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 + ins_len + 1));
2971 if (newp == NULL)
2972 continue;
2973 /* copy up to block start */
2974 mch_memmove(newp, oldp, (size_t)bd.textcol);
2975 offset = bd.textcol;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002976#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002977 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 offset += vpos.coladd;
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2981 offset += ins_len;
2982 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002983 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 ml_replace(linenr, newp, FALSE);
2985 }
2986 }
2987 check_cursor();
2988
2989 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2990 }
2991 vim_free(ins_text);
2992 }
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 return retval;
2996}
2997
2998/*
2999 * set all the yank registers to empty (called from main())
3000 */
3001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003002init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 int i;
3005
3006 for (i = 0; i < NUM_REGISTERS; ++i)
3007 y_regs[i].y_array = NULL;
3008}
3009
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003010#if defined(EXITFREE) || defined(PROTO)
3011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003012clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003013{
3014 int i;
3015
3016 for (i = 0; i < NUM_REGISTERS; ++i)
3017 {
3018 y_current = &y_regs[i];
3019 if (y_current->y_array != NULL)
3020 free_yank_all();
3021 }
3022}
3023#endif
3024
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025/*
3026 * Free "n" lines from the current yank register.
3027 * Called for normal freeing and in case of error.
3028 */
3029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003030free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 if (y_current->y_array != NULL)
3033 {
3034 long i;
3035
3036 for (i = n; --i >= 0; )
3037 {
3038#ifdef AMIGA /* only for very slow machines */
3039 if ((i & 1023) == 1023) /* this may take a while */
3040 {
3041 /*
3042 * This message should never cause a hit-return message.
3043 * Overwrite this message with any next message.
3044 */
3045 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003046 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 --no_wait_return;
3048 msg_didout = FALSE;
3049 msg_col = 0;
3050 }
3051#endif
3052 vim_free(y_current->y_array[i]);
3053 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003054 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#ifdef AMIGA
3056 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003057 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#endif
3059 }
3060}
3061
3062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 free_yank(y_current->y_size);
3066}
3067
3068/*
3069 * Yank the text between "oap->start" and "oap->end" into a yank register.
3070 * If we are to append (uppercase register), we first yank into a new yank
3071 * register and then concatenate the old and the new one (so we keep the old
3072 * one in case of out-of-memory).
3073 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003074 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 */
3076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003077op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003080 yankreg_T *curr; /* copy of y_current */
3081 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 char_u **new_ptr;
3083 linenr_T lnum; /* current line number */
3084 long j;
3085 int yanktype = oap->motion_type;
3086 long yanklines = oap->line_count;
3087 linenr_T yankendlnum = oap->end.lnum;
3088 char_u *p;
3089 char_u *pnew;
3090 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003091#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003092 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 /* check for read-only register */
3096 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3097 {
3098 beep_flush();
3099 return FAIL;
3100 }
3101 if (oap->regname == '_') /* black hole: nothing to do */
3102 return OK;
3103
3104#ifdef FEAT_CLIPBOARD
3105 if (!clip_star.available && oap->regname == '*')
3106 oap->regname = 0;
3107 else if (!clip_plus.available && oap->regname == '+')
3108 oap->regname = 0;
3109#endif
3110
3111 if (!deleting) /* op_delete() already set y_current */
3112 get_yank_register(oap->regname, TRUE);
3113
3114 curr = y_current;
3115 /* append to existing contents */
3116 if (y_append && y_current->y_array != NULL)
3117 y_current = &newreg;
3118 else
3119 free_yank_all(); /* free previously yanked lines */
3120
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003121 /*
3122 * If the cursor was in column 1 before and after the movement, and the
3123 * operator is not inclusive, the yank is always linewise.
3124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if ( oap->motion_type == MCHAR
3126 && oap->start.col == 0
3127 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003129 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 && oap->end.col == 0
3131 && yanklines > 1)
3132 {
3133 yanktype = MLINE;
3134 --yankendlnum;
3135 --yanklines;
3136 }
3137
3138 y_current->y_size = yanklines;
3139 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 y_current->y_width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
3142 yanklines), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (y_current->y_array == NULL)
3144 {
3145 y_current = curr;
3146 return FAIL;
3147 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003148#ifdef FEAT_VIMINFO
3149 y_current->y_time_set = vim_time();
3150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 y_idx = 0;
3153 lnum = oap->start.lnum;
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (oap->block_mode)
3156 {
3157 /* Visual block mode */
3158 y_current->y_type = MBLOCK; /* set the yank register type */
3159 y_current->y_width = oap->end_vcol - oap->start_vcol;
3160
3161 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3162 y_current->y_width--;
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3166 {
3167 switch (y_current->y_type)
3168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 case MBLOCK:
3170 block_prep(oap, &bd, lnum, FALSE);
3171 if (yank_copy_line(&bd, y_idx) == FAIL)
3172 goto fail;
3173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 case MLINE:
3176 if ((y_current->y_array[y_idx] =
3177 vim_strsave(ml_get(lnum))) == NULL)
3178 goto fail;
3179 break;
3180
3181 case MCHAR:
3182 {
3183 colnr_T startcol = 0, endcol = MAXCOL;
3184#ifdef FEAT_VIRTUALEDIT
3185 int is_oneChar = FALSE;
3186 colnr_T cs, ce;
3187#endif
3188 p = ml_get(lnum);
3189 bd.startspaces = 0;
3190 bd.endspaces = 0;
3191
3192 if (lnum == oap->start.lnum)
3193 {
3194 startcol = oap->start.col;
3195#ifdef FEAT_VIRTUALEDIT
3196 if (virtual_op)
3197 {
3198 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3199 if (ce != cs && oap->start.coladd > 0)
3200 {
3201 /* Part of a tab selected -- but don't
3202 * double-count it. */
3203 bd.startspaces = (ce - cs + 1)
3204 - oap->start.coladd;
3205 startcol++;
3206 }
3207 }
3208#endif
3209 }
3210
3211 if (lnum == oap->end.lnum)
3212 {
3213 endcol = oap->end.col;
3214#ifdef FEAT_VIRTUALEDIT
3215 if (virtual_op)
3216 {
3217 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3218 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
3219# ifdef FEAT_MBYTE
3220 /* Don't add space for double-wide
3221 * char; endcol will be on last byte
3222 * of multi-byte char. */
3223 && (*mb_head_off)(p, p + endcol) == 0
3224# endif
3225 ))
3226 {
3227 if (oap->start.lnum == oap->end.lnum
3228 && oap->start.col == oap->end.col)
3229 {
3230 /* Special case: inside a single char */
3231 is_oneChar = TRUE;
3232 bd.startspaces = oap->end.coladd
3233 - oap->start.coladd + oap->inclusive;
3234 endcol = startcol;
3235 }
3236 else
3237 {
3238 bd.endspaces = oap->end.coladd
3239 + oap->inclusive;
3240 endcol -= oap->inclusive;
3241 }
3242 }
3243 }
3244#endif
3245 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003246 if (endcol == MAXCOL)
3247 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (startcol > endcol
3249#ifdef FEAT_VIRTUALEDIT
3250 || is_oneChar
3251#endif
3252 )
3253 bd.textlen = 0;
3254 else
3255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 bd.textlen = endcol - startcol + oap->inclusive;
3257 }
3258 bd.textstart = p + startcol;
3259 if (yank_copy_line(&bd, y_idx) == FAIL)
3260 goto fail;
3261 break;
3262 }
3263 /* NOTREACHED */
3264 }
3265 }
3266
3267 if (curr != y_current) /* append the new block to the old block */
3268 {
3269 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
3270 (curr->y_size + y_current->y_size)), TRUE);
3271 if (new_ptr == NULL)
3272 goto fail;
3273 for (j = 0; j < curr->y_size; ++j)
3274 new_ptr[j] = curr->y_array[j];
3275 vim_free(curr->y_array);
3276 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003277#ifdef FEAT_VIMINFO
3278 curr->y_time_set = vim_time();
3279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3282 curr->y_type = MLINE;
3283
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003284 /* Concatenate the last line of the old block with the first line of
3285 * the new block, unless being Vi compatible. */
3286 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
3289 + STRLEN(y_current->y_array[0]) + 1), TRUE);
3290 if (pnew == NULL)
3291 {
3292 y_idx = y_current->y_size - 1;
3293 goto fail;
3294 }
3295 STRCPY(pnew, curr->y_array[--j]);
3296 STRCAT(pnew, y_current->y_array[0]);
3297 vim_free(curr->y_array[j]);
3298 vim_free(y_current->y_array[0]);
3299 curr->y_array[j++] = pnew;
3300 y_idx = 1;
3301 }
3302 else
3303 y_idx = 0;
3304 while (y_idx < y_current->y_size)
3305 curr->y_array[j++] = y_current->y_array[y_idx++];
3306 curr->y_size = j;
3307 vim_free(y_current->y_array);
3308 y_current = curr;
3309 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003310 if (curwin->w_p_rnu)
3311 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (mess) /* Display message about yank? */
3313 {
3314 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 && yanklines == 1)
3317 yanklines = 0;
3318 /* Some versions of Vi use ">=" here, some don't... */
3319 if (yanklines > p_report)
3320 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003321 char namebuf[100];
3322
3323 if (oap->regname == NUL)
3324 *namebuf = NUL;
3325 else
3326 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003327 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 /* redisplay now, so message is not deleted */
3330 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003331 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003333 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003334 "block of %ld lines yanked%s", yanklines),
3335 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003339 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003340 "%ld lines yanked%s", yanklines),
3341 yanklines, namebuf);
3342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 }
3345
3346 /*
3347 * Set "'[" and "']" marks.
3348 */
3349 curbuf->b_op_start = oap->start;
3350 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003351 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003352 {
3353 curbuf->b_op_start.col = 0;
3354 curbuf->b_op_end.col = MAXCOL;
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357#ifdef FEAT_CLIPBOARD
3358 /*
3359 * If we were yanking to the '*' register, send result to clipboard.
3360 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3361 * to the '*' register.
3362 */
3363 if (clip_star.available
3364 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003365 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003366 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 if (curr != &(y_regs[STAR_REGISTER]))
3369 /* Copy the text from register 0 to the clipboard register. */
3370 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3371
3372 clip_own_selection(&clip_star);
3373 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003374# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003375 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378
3379# ifdef FEAT_X11
3380 /*
3381 * If we were yanking to the '+' register, send result to selection.
3382 * Also copy to the '*' register, in case auto-select is off.
3383 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003384 if (clip_plus.available
3385 && (curr == &(y_regs[PLUS_REGISTER])
3386 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003387 && ((clip_unnamed | clip_unnamed_saved) &
3388 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003390 if (curr != &(y_regs[PLUS_REGISTER]))
3391 /* Copy the text from register 0 to the clipboard register. */
3392 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 clip_own_selection(&clip_plus);
3395 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003396 if (!clip_isautosel_star() && !clip_isautosel_plus()
3397 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3400 clip_own_selection(&clip_star);
3401 clip_gen_set_selection(&clip_star);
3402 }
3403 }
3404# endif
3405#endif
3406
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003407#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003408 if (!deleting && has_textyankpost())
3409 yank_do_autocmd(oap, y_current);
3410#endif
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 return OK;
3413
3414fail: /* free the allocated lines */
3415 free_yank(y_idx + 1);
3416 y_current = curr;
3417 return FAIL;
3418}
3419
3420 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 char_u *pnew;
3424
3425 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3426 == NULL)
3427 return FAIL;
3428 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003429 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 pnew += bd->startspaces;
3431 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3432 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003433 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 pnew += bd->endspaces;
3435 *pnew = NUL;
3436 return OK;
3437}
3438
3439#ifdef FEAT_CLIPBOARD
3440/*
3441 * Make a copy of the y_current register to register "reg".
3442 */
3443 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003444copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003446 yankreg_T *curr = y_current;
3447 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 y_current = reg;
3450 free_yank_all();
3451 *y_current = *curr;
3452 y_current->y_array = (char_u **)lalloc_clear(
3453 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3454 if (y_current->y_array == NULL)
3455 y_current->y_size = 0;
3456 else
3457 for (j = 0; j < y_current->y_size; ++j)
3458 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3459 {
3460 free_yank(j);
3461 y_current->y_size = 0;
3462 break;
3463 }
3464 y_current = curr;
3465}
3466#endif
3467
3468/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003469 * Put contents of register "regname" into the text.
3470 * Caller must check "regname" to be valid!
3471 * "flags": PUT_FIXINDENT make indent look nice
3472 * PUT_CURSEND leave cursor after end of new text
3473 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
3475 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003476do_put(
3477 int regname,
3478 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3479 long count,
3480 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481{
3482 char_u *ptr;
3483 char_u *newp, *oldp;
3484 int yanklen;
3485 int totlen = 0; /* init for gcc */
3486 linenr_T lnum;
3487 colnr_T col;
3488 long i; /* index in y_array[] */
3489 int y_type;
3490 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 int oldlen;
3492 long y_width = 0;
3493 colnr_T vcol;
3494 int delcount;
3495 int incr = 0;
3496 long j;
3497 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 char_u **y_array = NULL;
3499 long nr_lines = 0;
3500 pos_T new_cursor;
3501 int indent;
3502 int orig_indent = 0; /* init for gcc */
3503 int indent_diff = 0; /* init for gcc */
3504 int first_indent = TRUE;
3505 int lendiff = 0;
3506 pos_T old_pos;
3507 char_u *insert_string = NULL;
3508 int allocated = FALSE;
3509 long cnt;
3510
3511#ifdef FEAT_CLIPBOARD
3512 /* Adjust register name for "unnamed" in 'clipboard'. */
3513 adjust_clip_reg(&regname);
3514 (void)may_get_selection(regname);
3515#endif
3516
3517 if (flags & PUT_FIXINDENT)
3518 orig_indent = get_indent();
3519
3520 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3521 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3522
3523 /*
3524 * Using inserted text works differently, because the register includes
3525 * special characters (newlines, etc.).
3526 */
3527 if (regname == '.')
3528 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003529 if (VIsual_active)
3530 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3532 (count == -1 ? 'O' : 'i')), count, FALSE);
3533 /* Putting the text is done later, so can't really move the cursor to
3534 * the next character. Use "l" to simulate it. */
3535 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3536 stuffcharReadbuff('l');
3537 return;
3538 }
3539
3540 /*
3541 * For special registers '%' (file name), '#' (alternate file name) and
3542 * ':' (last command line), etc. we have to create a fake yank register.
3543 */
3544 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3545 {
3546 if (insert_string == NULL)
3547 return;
3548 }
3549
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003550 /* Autocommands may be executed when saving lines for undo. This might
3551 * make "y_array" invalid, so we start undo now to avoid that. */
3552 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3553 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (insert_string != NULL)
3556 {
3557 y_type = MCHAR;
3558#ifdef FEAT_EVAL
3559 if (regname == '=')
3560 {
3561 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003562 * characters.
3563 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 for (;;)
3565 {
3566 y_size = 0;
3567 ptr = insert_string;
3568 while (ptr != NULL)
3569 {
3570 if (y_array != NULL)
3571 y_array[y_size] = ptr;
3572 ++y_size;
3573 ptr = vim_strchr(ptr, '\n');
3574 if (ptr != NULL)
3575 {
3576 if (y_array != NULL)
3577 *ptr = NUL;
3578 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003579 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 if (*ptr == NUL)
3581 {
3582 y_type = MLINE;
3583 break;
3584 }
3585 }
3586 }
3587 if (y_array != NULL)
3588 break;
3589 y_array = (char_u **)alloc((unsigned)
3590 (y_size * sizeof(char_u *)));
3591 if (y_array == NULL)
3592 goto end;
3593 }
3594 }
3595 else
3596#endif
3597 {
3598 y_size = 1; /* use fake one-line yank register */
3599 y_array = &insert_string;
3600 }
3601 }
3602 else
3603 {
3604 get_yank_register(regname, FALSE);
3605
3606 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 y_size = y_current->y_size;
3609 y_array = y_current->y_array;
3610 }
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (y_type == MLINE)
3613 {
3614 if (flags & PUT_LINE_SPLIT)
3615 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003616 char_u *p;
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /* "p" or "P" in Visual mode: split the lines to put the text in
3619 * between. */
3620 if (u_save_cursor() == FAIL)
3621 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003622 p = ml_get_cursor();
3623 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003624 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003625 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (ptr == NULL)
3627 goto end;
3628 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3629 vim_free(ptr);
3630
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003631 oldp = ml_get_curline();
3632 p = oldp + curwin->w_cursor.col;
3633 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003634 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003635 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (ptr == NULL)
3637 goto end;
3638 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3639 ++nr_lines;
3640 dir = FORWARD;
3641 }
3642 if (flags & PUT_LINE_FORWARD)
3643 {
3644 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003645 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 dir = FORWARD;
3647 }
3648 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3649 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3653 y_type = MLINE;
3654
3655 if (y_size == 0 || y_array == NULL)
3656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003657 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 regname == 0 ? (char_u *)"\"" : transchar(regname));
3659 goto end;
3660 }
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (y_type == MBLOCK)
3663 {
3664 lnum = curwin->w_cursor.lnum + y_size + 1;
3665 if (lnum > curbuf->b_ml.ml_line_count)
3666 lnum = curbuf->b_ml.ml_line_count + 1;
3667 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3668 goto end;
3669 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003670 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
3672 lnum = curwin->w_cursor.lnum;
3673#ifdef FEAT_FOLDING
3674 /* Correct line number for closed fold. Don't move the cursor yet,
3675 * u_save() uses it. */
3676 if (dir == BACKWARD)
3677 (void)hasFolding(lnum, &lnum, NULL);
3678 else
3679 (void)hasFolding(lnum, NULL, &lnum);
3680#endif
3681 if (dir == FORWARD)
3682 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003683 /* In an empty buffer the empty line is going to be replaced, include
3684 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003685 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 goto end;
3687#ifdef FEAT_FOLDING
3688 if (dir == FORWARD)
3689 curwin->w_cursor.lnum = lnum - 1;
3690 else
3691 curwin->w_cursor.lnum = lnum;
3692 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3693#endif
3694 }
3695 else if (u_save_cursor() == FAIL)
3696 goto end;
3697
3698 yanklen = (int)STRLEN(y_array[0]);
3699
3700#ifdef FEAT_VIRTUALEDIT
3701 if (ve_flags == VE_ALL && y_type == MCHAR)
3702 {
3703 if (gchar_cursor() == TAB)
3704 {
3705 /* Don't need to insert spaces when "p" on the last position of a
3706 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003707#ifdef FEAT_VARTABS
3708 int viscol = getviscol();
3709 if (dir == FORWARD
3710 ? tabstop_padding(viscol, curbuf->b_p_ts,
3711 curbuf->b_p_vts_array) != 1
3712 : curwin->w_cursor.coladd > 0)
3713 coladvance_force(viscol);
3714#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (dir == FORWARD
3716 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3717 : curwin->w_cursor.coladd > 0)
3718 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 else
3721 curwin->w_cursor.coladd = 0;
3722 }
3723 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3724 coladvance_force(getviscol() + (dir == FORWARD));
3725 }
3726#endif
3727
3728 lnum = curwin->w_cursor.lnum;
3729 col = curwin->w_cursor.col;
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 /*
3732 * Block mode
3733 */
3734 if (y_type == MBLOCK)
3735 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003736 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 colnr_T endcol2 = 0;
3738
3739 if (dir == FORWARD && c != NUL)
3740 {
3741#ifdef FEAT_VIRTUALEDIT
3742 if (ve_flags == VE_ALL)
3743 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3744 else
3745#endif
3746 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3747
3748#ifdef FEAT_MBYTE
3749 if (has_mbyte)
3750 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003751 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else
3753#endif
3754#ifdef FEAT_VIRTUALEDIT
3755 if (c != TAB || ve_flags != VE_ALL)
3756#endif
3757 ++curwin->w_cursor.col;
3758 ++col;
3759 }
3760 else
3761 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3762
3763#ifdef FEAT_VIRTUALEDIT
3764 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003765 if (ve_flags == VE_ALL
3766 && (curwin->w_cursor.coladd > 0
3767 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769 if (dir == FORWARD && c == NUL)
3770 ++col;
3771 if (dir != FORWARD && c != NUL)
3772 ++curwin->w_cursor.col;
3773 if (c == TAB)
3774 {
3775 if (dir == BACKWARD && curwin->w_cursor.col)
3776 curwin->w_cursor.col--;
3777 if (dir == FORWARD && col - 1 == endcol2)
3778 curwin->w_cursor.col++;
3779 }
3780 }
3781 curwin->w_cursor.coladd = 0;
3782#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003783 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 for (i = 0; i < y_size; ++i)
3785 {
3786 int spaces;
3787 char shortline;
3788
3789 bd.startspaces = 0;
3790 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 vcol = 0;
3792 delcount = 0;
3793
3794 /* add a new line */
3795 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3796 {
3797 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3798 (colnr_T)1, FALSE) == FAIL)
3799 break;
3800 ++nr_lines;
3801 }
3802 /* get the old line and advance to the position to insert at */
3803 oldp = ml_get_curline();
3804 oldlen = (int)STRLEN(oldp);
3805 for (ptr = oldp; vcol < col && *ptr; )
3806 {
3807 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003808 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 vcol += incr;
3810 }
3811 bd.textcol = (colnr_T)(ptr - oldp);
3812
3813 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3814
3815 if (vcol < col) /* line too short, padd with spaces */
3816 bd.startspaces = col - vcol;
3817 else if (vcol > col)
3818 {
3819 bd.endspaces = vcol - col;
3820 bd.startspaces = incr - bd.endspaces;
3821 --bd.textcol;
3822 delcount = 1;
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3826#endif
3827 if (oldp[bd.textcol] != TAB)
3828 {
3829 /* Only a Tab can be split into spaces. Other
3830 * characters will have to be moved to after the
3831 * block, causing misalignment. */
3832 delcount = 0;
3833 bd.endspaces = 0;
3834 }
3835 }
3836
3837 yanklen = (int)STRLEN(y_array[i]);
3838
3839 /* calculate number of spaces required to fill right side of block*/
3840 spaces = y_width + 1;
3841 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003842 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (spaces < 0)
3844 spaces = 0;
3845
3846 /* insert the new text */
3847 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3848 newp = alloc_check((unsigned)totlen + oldlen + 1);
3849 if (newp == NULL)
3850 break;
3851 /* copy part up to cursor to new line */
3852 ptr = newp;
3853 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3854 ptr += bd.textcol;
3855 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003856 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 ptr += bd.startspaces;
3858 /* insert the new text */
3859 for (j = 0; j < count; ++j)
3860 {
3861 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3862 ptr += yanklen;
3863
3864 /* insert block's trailing spaces only if there's text behind */
3865 if ((j < count - 1 || !shortline) && spaces)
3866 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003867 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 ptr += spaces;
3869 }
3870 }
3871 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003872 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 ptr += bd.endspaces;
3874 /* move the text after the cursor to the end of the line. */
3875 mch_memmove(ptr, oldp + bd.textcol + delcount,
3876 (size_t)(oldlen - bd.textcol - delcount + 1));
3877 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3878
3879 ++curwin->w_cursor.lnum;
3880 if (i == 0)
3881 curwin->w_cursor.col += bd.startspaces;
3882 }
3883
3884 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3885
3886 /* Set '[ mark. */
3887 curbuf->b_op_start = curwin->w_cursor;
3888 curbuf->b_op_start.lnum = lnum;
3889
3890 /* adjust '] mark */
3891 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3892 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003893# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (flags & PUT_CURSEND)
3897 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003898 colnr_T len;
3899
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 curwin->w_cursor = curbuf->b_op_end;
3901 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003902
3903 /* in Insert mode we might be after the NUL, correct for that */
3904 len = (colnr_T)STRLEN(ml_get_curline());
3905 if (curwin->w_cursor.col > len)
3906 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 else
3909 curwin->w_cursor.lnum = lnum;
3910 }
3911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
3913 /*
3914 * Character or Line mode
3915 */
3916 if (y_type == MCHAR)
3917 {
3918 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3919 * char */
3920 if (dir == FORWARD && gchar_cursor() != NUL)
3921 {
3922#ifdef FEAT_MBYTE
3923 if (has_mbyte)
3924 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003925 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /* put it on the next of the multi-byte character. */
3928 col += bytelen;
3929 if (yanklen)
3930 {
3931 curwin->w_cursor.col += bytelen;
3932 curbuf->b_op_end.col += bytelen;
3933 }
3934 }
3935 else
3936#endif
3937 {
3938 ++col;
3939 if (yanklen)
3940 {
3941 ++curwin->w_cursor.col;
3942 ++curbuf->b_op_end.col;
3943 }
3944 }
3945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 curbuf->b_op_start = curwin->w_cursor;
3947 }
3948 /*
3949 * Line mode: BACKWARD is the same as FORWARD on the previous line
3950 */
3951 else if (dir == BACKWARD)
3952 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003953 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 /*
3956 * simple case: insert into current line
3957 */
3958 if (y_type == MCHAR && y_size == 1)
3959 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003960 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003961
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003962 if (VIsual_active)
3963 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003964 end_lnum = curbuf->b_visual.vi_end.lnum;
3965 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3966 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003967 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003968
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003969 do {
3970 totlen = count * yanklen;
3971 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003973 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003974 if (VIsual_active && col > (int)STRLEN(oldp))
3975 {
3976 lnum++;
3977 continue;
3978 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003979 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3980 if (newp == NULL)
3981 goto end; /* alloc() gave an error message */
3982 mch_memmove(newp, oldp, (size_t)col);
3983 ptr = newp + col;
3984 for (i = 0; i < count; ++i)
3985 {
3986 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3987 ptr += yanklen;
3988 }
3989 STRMOVE(ptr, oldp + col);
3990 ml_replace(lnum, newp, FALSE);
3991 /* Place cursor on last putted char. */
3992 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003993 {
3994 /* make sure curwin->w_virtcol is updated */
3995 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003996 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003999 if (VIsual_active)
4000 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01004001 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02004002
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01004003 if (VIsual_active) /* reset lnum to the last visual line */
4004 lnum--;
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 curbuf->b_op_end = curwin->w_cursor;
4007 /* For "CTRL-O p" in Insert mode, put cursor after last char */
4008 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
4009 ++curwin->w_cursor.col;
4010 changed_bytes(lnum, col);
4011 }
4012 else
4013 {
4014 /*
4015 * Insert at least one line. When y_type is MCHAR, break the first
4016 * line in two.
4017 */
4018 for (cnt = 1; cnt <= count; ++cnt)
4019 {
4020 i = 0;
4021 if (y_type == MCHAR)
4022 {
4023 /*
4024 * Split the current line in two at the insert position.
4025 * First insert y_array[size - 1] in front of second line.
4026 * Then append y_array[0] to first line.
4027 */
4028 lnum = new_cursor.lnum;
4029 ptr = ml_get(lnum) + col;
4030 totlen = (int)STRLEN(y_array[y_size - 1]);
4031 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
4032 if (newp == NULL)
4033 goto error;
4034 STRCPY(newp, y_array[y_size - 1]);
4035 STRCAT(newp, ptr);
4036 /* insert second line */
4037 ml_append(lnum, newp, (colnr_T)0, FALSE);
4038 vim_free(newp);
4039
4040 oldp = ml_get(lnum);
4041 newp = alloc_check((unsigned)(col + yanklen + 1));
4042 if (newp == NULL)
4043 goto error;
4044 /* copy first part of line */
4045 mch_memmove(newp, oldp, (size_t)col);
4046 /* append to first line */
4047 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
4048 ml_replace(lnum, newp, FALSE);
4049
4050 curwin->w_cursor.lnum = lnum;
4051 i = 1;
4052 }
4053
4054 for (; i < y_size; ++i)
4055 {
4056 if ((y_type != MCHAR || i < y_size - 1)
4057 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
4058 == FAIL)
4059 goto error;
4060 lnum++;
4061 ++nr_lines;
4062 if (flags & PUT_FIXINDENT)
4063 {
4064 old_pos = curwin->w_cursor;
4065 curwin->w_cursor.lnum = lnum;
4066 ptr = ml_get(lnum);
4067 if (cnt == count && i == y_size - 1)
4068 lendiff = (int)STRLEN(ptr);
4069#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
4070 if (*ptr == '#' && preprocs_left())
4071 indent = 0; /* Leave # lines at start */
4072 else
4073#endif
4074 if (*ptr == NUL)
4075 indent = 0; /* Ignore empty lines */
4076 else if (first_indent)
4077 {
4078 indent_diff = orig_indent - get_indent();
4079 indent = orig_indent;
4080 first_indent = FALSE;
4081 }
4082 else if ((indent = get_indent() + indent_diff) < 0)
4083 indent = 0;
4084 (void)set_indent(indent, 0);
4085 curwin->w_cursor = old_pos;
4086 /* remember how many chars were removed */
4087 if (cnt == count && i == y_size - 1)
4088 lendiff -= (int)STRLEN(ml_get(lnum));
4089 }
4090 }
4091 }
4092
4093error:
4094 /* Adjust marks. */
4095 if (y_type == MLINE)
4096 {
4097 curbuf->b_op_start.col = 0;
4098 if (dir == FORWARD)
4099 curbuf->b_op_start.lnum++;
4100 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02004101 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004102 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02004103 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01004104 < curbuf->b_ml.ml_line_count
4105#ifdef FEAT_DIFF
4106 || curwin->w_p_diff
4107#endif
4108 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02004109 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 (linenr_T)MAXLNUM, nr_lines, 0L);
4111
4112 /* note changed text for displaying and folding */
4113 if (y_type == MCHAR)
4114 changed_lines(curwin->w_cursor.lnum, col,
4115 curwin->w_cursor.lnum + 1, nr_lines);
4116 else
4117 changed_lines(curbuf->b_op_start.lnum, 0,
4118 curbuf->b_op_start.lnum, nr_lines);
4119
4120 /* put '] mark at last inserted character */
4121 curbuf->b_op_end.lnum = lnum;
4122 /* correct length for change in indent */
4123 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4124 if (col > 1)
4125 curbuf->b_op_end.col = col - 1;
4126 else
4127 curbuf->b_op_end.col = 0;
4128
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004129 if (flags & PUT_CURSLINE)
4130 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004131 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004132 curwin->w_cursor.lnum = lnum;
4133 beginline(BL_WHITE | BL_FIX);
4134 }
4135 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
4137 /* put cursor after inserted text */
4138 if (y_type == MLINE)
4139 {
4140 if (lnum >= curbuf->b_ml.ml_line_count)
4141 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4142 else
4143 curwin->w_cursor.lnum = lnum + 1;
4144 curwin->w_cursor.col = 0;
4145 }
4146 else
4147 {
4148 curwin->w_cursor.lnum = lnum;
4149 curwin->w_cursor.col = col;
4150 }
4151 }
4152 else if (y_type == MLINE)
4153 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004154 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 curwin->w_cursor.col = 0;
4156 if (dir == FORWARD)
4157 ++curwin->w_cursor.lnum;
4158 beginline(BL_WHITE | BL_FIX);
4159 }
4160 else /* put cursor on first inserted character */
4161 curwin->w_cursor = new_cursor;
4162 }
4163 }
4164
4165 msgmore(nr_lines);
4166 curwin->w_set_curswant = TRUE;
4167
4168end:
4169 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004171 if (regname == '=')
4172 vim_free(y_array);
4173
Bram Moolenaar033d8882013-09-25 23:24:57 +02004174 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004175
Bram Moolenaar677ee682005-01-27 14:41:15 +00004176 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004177 adjust_cursor_eol();
4178}
4179
4180/*
4181 * When the cursor is on the NUL past the end of the line and it should not be
4182 * there move it left.
4183 */
4184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004186{
4187 if (curwin->w_cursor.col > 0
4188 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004189#ifdef FEAT_VIRTUALEDIT
4190 && (ve_flags & VE_ONEMORE) == 0
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 && !(restart_edit || (State & INSERT)))
4193 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004194 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004195 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#ifdef FEAT_VIRTUALEDIT
4198 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004199 {
4200 colnr_T scol, ecol;
4201
4202 /* Coladd is set to the width of the last character. */
4203 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4204 curwin->w_cursor.coladd = ecol - scol + 1;
4205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#endif
4207 }
4208}
4209
4210#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4211/*
4212 * Return TRUE if lines starting with '#' should be left aligned.
4213 */
4214 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004215preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 return
4218# ifdef FEAT_SMARTINDENT
4219# ifdef FEAT_CINDENT
4220 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4221# else
4222 curbuf->b_p_si
4223# endif
4224# endif
4225# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004226 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4227 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228# endif
4229 ;
4230}
4231#endif
4232
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004233/*
4234 * Return the character name of the register with the given number.
4235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004237get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 if (num == -1)
4240 return '"';
4241 else if (num < 10)
4242 return num + '0';
4243 else if (num == DELETION_REGISTER)
4244 return '-';
4245#ifdef FEAT_CLIPBOARD
4246 else if (num == STAR_REGISTER)
4247 return '*';
4248 else if (num == PLUS_REGISTER)
4249 return '+';
4250#endif
4251 else
4252 {
4253#ifdef EBCDIC
4254 int i;
4255
4256 /* EBCDIC is really braindead ... */
4257 i = 'a' + (num - 10);
4258 if (i > 'i')
4259 i += 7;
4260 if (i > 'r')
4261 i += 8;
4262 return i;
4263#else
4264 return num + 'a' - 10;
4265#endif
4266 }
4267}
4268
4269/*
4270 * ":dis" and ":registers": Display the contents of the yank registers.
4271 */
4272 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004273ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004275 int i, n;
4276 long j;
4277 char_u *p;
4278 yankreg_T *yb;
4279 int name;
4280 int attr;
4281 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004282#ifdef FEAT_MBYTE
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004283 int clen;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004284#else
4285# define clen 1
4286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
4288 if (arg != NULL && *arg == NUL)
4289 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004290 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004293 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4295 {
4296 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004297 if (arg != NULL && vim_strchr(arg, name) == NULL
4298#ifdef ONE_CLIPBOARD
4299 /* Star register and plus register contain the same thing. */
4300 && (name != '*' || vim_strchr(arg, '+') == NULL)
4301#endif
4302 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 continue; /* did not ask for this register */
4304
4305#ifdef FEAT_CLIPBOARD
4306 /* Adjust register name for "unnamed" in 'clipboard'.
4307 * When it's a clipboard register, fill it with the current contents
4308 * of the clipboard. */
4309 adjust_clip_reg(&name);
4310 (void)may_get_selection(name);
4311#endif
4312
4313 if (i == -1)
4314 {
4315 if (y_previous != NULL)
4316 yb = y_previous;
4317 else
4318 yb = &(y_regs[0]);
4319 }
4320 else
4321 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004322
4323#ifdef FEAT_EVAL
4324 if (name == MB_TOLOWER(redir_reg)
4325 || (redir_reg == '"' && yb == y_previous))
4326 continue; /* do not list register being written to, the
4327 * pointer can be freed */
4328#endif
4329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (yb->y_array != NULL)
4331 {
4332 msg_putchar('\n');
4333 msg_putchar('"');
4334 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004335 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 n = (int)Columns - 6;
4338 for (j = 0; j < yb->y_size && n > 1; ++j)
4339 {
4340 if (j)
4341 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004342 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 n -= 2;
4344 }
4345 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004348 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004349#endif
4350 msg_outtrans_len(p, clen);
4351#ifdef FEAT_MBYTE
4352 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
4354 }
4355 }
4356 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004357 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 out_flush(); /* show one line at a time */
4359 }
4360 ui_breakcheck();
4361 }
4362
4363 /*
4364 * display last inserted text
4365 */
4366 if ((p = get_last_insert()) != NULL
4367 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4368 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004369 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 dis_msg(p, TRUE);
4371 }
4372
4373 /*
4374 * display last command line
4375 */
4376 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4377 && !got_int)
4378 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004379 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 dis_msg(last_cmdline, FALSE);
4381 }
4382
4383 /*
4384 * display current file name
4385 */
4386 if (curbuf->b_fname != NULL
4387 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4388 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004389 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 dis_msg(curbuf->b_fname, FALSE);
4391 }
4392
4393 /*
4394 * display alternate file name
4395 */
4396 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4397 {
4398 char_u *fname;
4399 linenr_T dummy;
4400
4401 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4402 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004403 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 dis_msg(fname, FALSE);
4405 }
4406 }
4407
4408 /*
4409 * display last search pattern
4410 */
4411 if (last_search_pat() != NULL
4412 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4413 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004414 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 dis_msg(last_search_pat(), FALSE);
4416 }
4417
4418#ifdef FEAT_EVAL
4419 /*
4420 * display last used expression
4421 */
4422 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4423 && !got_int)
4424 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004425 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 dis_msg(expr_line, FALSE);
4427 }
4428#endif
4429}
4430
4431/*
4432 * display a string for do_dis()
4433 * truncate at end of screen line
4434 */
4435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004436dis_msg(
4437 char_u *p,
4438 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 int n;
4441#ifdef FEAT_MBYTE
4442 int l;
4443#endif
4444
4445 n = (int)Columns - 6;
4446 while (*p != NUL
4447 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4448 && (n -= ptr2cells(p)) >= 0)
4449 {
4450#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004451 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
4453 msg_outtrans_len(p, l);
4454 p += l;
4455 }
4456 else
4457#endif
4458 msg_outtrans_len(p++, 1);
4459 }
4460 ui_breakcheck();
4461}
4462
Bram Moolenaar81340392012-06-06 16:12:59 +02004463#if defined(FEAT_COMMENTS) || defined(PROTO)
4464/*
4465 * If "process" is TRUE and the line begins with a comment leader (possibly
4466 * after some white space), return a pointer to the text after it. Put a boolean
4467 * value indicating whether the line ends with an unclosed comment in
4468 * "is_comment".
4469 * line - line to be processed,
4470 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004471 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004472 * include_space - whether to also skip space following the comment leader,
4473 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004474 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004475 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004476 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004477skip_comment(
4478 char_u *line,
4479 int process,
4480 int include_space,
4481 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004482{
4483 char_u *comment_flags = NULL;
4484 int lead_len;
4485 int leader_offset = get_last_leader_offset(line, &comment_flags);
4486
4487 *is_comment = FALSE;
4488 if (leader_offset != -1)
4489 {
4490 /* Let's check whether the line ends with an unclosed comment.
4491 * If the last comment leader has COM_END in flags, there's no comment.
4492 */
4493 while (*comment_flags)
4494 {
4495 if (*comment_flags == COM_END
4496 || *comment_flags == ':')
4497 break;
4498 ++comment_flags;
4499 }
4500 if (*comment_flags != COM_END)
4501 *is_comment = TRUE;
4502 }
4503
4504 if (process == FALSE)
4505 return line;
4506
4507 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4508
4509 if (lead_len == 0)
4510 return line;
4511
4512 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004513 * - COM_END,
4514 * - colon,
4515 * whichever comes first.
4516 */
4517 while (*comment_flags)
4518 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004519 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004520 || *comment_flags == ':')
4521 {
4522 break;
4523 }
4524 ++comment_flags;
4525 }
4526
4527 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004528 * starting with a closing part of a three-part comment. That's good,
4529 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004530 */
4531 if (*comment_flags == ':' || *comment_flags == NUL)
4532 line += lead_len;
4533
4534 return line;
4535}
4536#endif
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004539 * Join 'count' lines (minimal 2) at cursor position.
4540 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004541 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4542 * leaders should not be removed.
4543 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4544 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004546 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
4548 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004549do_join(
4550 long count,
4551 int insert_space,
4552 int save_undo,
4553 int use_formatoptions UNUSED,
4554 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004557 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004558 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004560 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004561 int endcurr1 = NUL;
4562 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004563 int currsize = 0; /* size of the current line */
4564 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004566 colnr_T col = 0;
4567 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004568#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004569 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004570 int remove_comments = (use_formatoptions == TRUE)
4571 && has_format_option(FO_REMOVE_COMS);
4572 int prev_was_comment;
4573#endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004576 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4577 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004580 /* Allocate an array to store the number of spaces inserted before each
4581 * line. We will use it to pre-compute the length of the new line and the
4582 * proper placement of each original line in the new one. */
4583 spaces = lalloc_clear((long_u)count, TRUE);
4584 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004586#if defined(FEAT_COMMENTS) || defined(PROTO)
4587 if (remove_comments)
4588 {
4589 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
4590 if (comments == NULL)
4591 {
4592 vim_free(spaces);
4593 return FAIL;
4594 }
4595 }
4596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004599 * Don't move anything, just compute the final line length
4600 * and setup the array of space strings lengths
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004602 for (t = 0; t < count; ++t)
4603 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004604 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004605 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004606 {
4607 /* Set the '[ mark. */
4608 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4609 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4610 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004611#if defined(FEAT_COMMENTS) || defined(PROTO)
4612 if (remove_comments)
4613 {
4614 /* We don't want to remove the comment leader if the
4615 * previous line is not a comment. */
4616 if (t > 0 && prev_was_comment)
4617 {
4618
4619 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4620 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004621 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004622 curr = new_curr;
4623 }
4624 else
4625 curr = skip_comment(curr, FALSE, insert_space,
4626 &prev_was_comment);
4627 }
4628#endif
4629
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004630 if (insert_space && t > 0)
4631 {
4632 curr = skipwhite(curr);
4633 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
4634#ifdef FEAT_MBYTE
4635 && (!has_format_option(FO_MBYTE_JOIN)
4636 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4637 && (!has_format_option(FO_MBYTE_JOIN2)
4638 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
4639#endif
4640 )
4641 {
4642 /* don't add a space if the line is ending in a space */
4643 if (endcurr1 == ' ')
4644 endcurr1 = endcurr2;
4645 else
4646 ++spaces[t];
4647 /* extra space when 'joinspaces' set and line ends in '.' */
4648 if ( p_js
4649 && (endcurr1 == '.'
4650 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4651 && (endcurr1 == '?' || endcurr1 == '!'))))
4652 ++spaces[t];
4653 }
4654 }
4655 currsize = (int)STRLEN(curr);
4656 sumsize += currsize + spaces[t];
4657 endcurr1 = endcurr2 = NUL;
4658 if (insert_space && currsize > 0)
4659 {
4660#ifdef FEAT_MBYTE
4661 if (has_mbyte)
4662 {
4663 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004664 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004665 endcurr1 = (*mb_ptr2char)(cend);
4666 if (cend > curr)
4667 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004668 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004669 endcurr2 = (*mb_ptr2char)(cend);
4670 }
4671 }
4672 else
4673#endif
4674 {
4675 endcurr1 = *(curr + currsize - 1);
4676 if (currsize > 1)
4677 endcurr2 = *(curr + currsize - 2);
4678 }
4679 }
4680 line_breakcheck();
4681 if (got_int)
4682 {
4683 ret = FAIL;
4684 goto theend;
4685 }
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004688 /* store the column position before last line */
4689 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004691 /* allocate the space for the new line */
4692 newp = alloc_check((unsigned)(sumsize + 1));
4693 cend = newp + sumsize;
4694 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004696 /*
4697 * Move affected lines to the new long one.
4698 *
4699 * Move marks from each deleted line to the joined line, adjusting the
4700 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4701 * should not really be a problem.
4702 */
4703 for (t = count - 1; ; --t)
4704 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004705 int spaces_removed;
4706
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004707 cend -= currsize;
4708 mch_memmove(cend, curr, (size_t)currsize);
4709 if (spaces[t] > 0)
4710 {
4711 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004712 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004713 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004714
4715 // If deleting more spaces than adding, the cursor moves no more than
4716 // what is added if it is inside these spaces.
4717 spaces_removed = (curr - curr_start) - spaces[t];
4718
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004719 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004720 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004721 if (t == 0)
4722 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004723 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02004724#if defined(FEAT_COMMENTS) || defined(PROTO)
4725 if (remove_comments)
4726 curr += comments[t - 1];
4727#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004728 if (insert_space && t > 1)
4729 curr = skipwhite(curr);
4730 currsize = (int)STRLEN(curr);
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4733
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004734 if (setmark)
4735 {
4736 /* Set the '] mark. */
4737 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
4738 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
4739 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 /* Only report the change in the first line here, del_lines() will report
4742 * the deleted line. */
4743 changed_lines(curwin->w_cursor.lnum, currsize,
4744 curwin->w_cursor.lnum + 1, 0L);
4745
4746 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004747 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * briefly, and then move it back. After del_lines() the cursor may
4749 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
4751 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004753 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 curwin->w_cursor.lnum = t;
4755
4756 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004757 * Set the cursor column:
4758 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004759 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004761 curwin->w_cursor.col =
4762 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#ifdef FEAT_VIRTUALEDIT
4766 curwin->w_cursor.coladd = 0;
4767#endif
4768 curwin->w_set_curswant = TRUE;
4769
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004770theend:
4771 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004772#if defined(FEAT_COMMENTS) || defined(PROTO)
4773 if (remove_comments)
4774 vim_free(comments);
4775#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004776 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777}
4778
4779#ifdef FEAT_COMMENTS
4780/*
4781 * Return TRUE if the two comment leaders given are the same. "lnum" is
4782 * the first line. White-space is ignored. Note that the whole of
4783 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4784 */
4785 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004786same_leader(
4787 linenr_T lnum,
4788 int leader1_len,
4789 char_u *leader1_flags,
4790 int leader2_len,
4791 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 int idx1 = 0, idx2 = 0;
4794 char_u *p;
4795 char_u *line1;
4796 char_u *line2;
4797
4798 if (leader1_len == 0)
4799 return (leader2_len == 0);
4800
4801 /*
4802 * If first leader has 'f' flag, the lines can be joined only if the
4803 * second line does not have a leader.
4804 * If first leader has 'e' flag, the lines can never be joined.
4805 * If fist leader has 's' flag, the lines can only be joined if there is
4806 * some text after it and the second line has the 'm' flag.
4807 */
4808 if (leader1_flags != NULL)
4809 {
4810 for (p = leader1_flags; *p && *p != ':'; ++p)
4811 {
4812 if (*p == COM_FIRST)
4813 return (leader2_len == 0);
4814 if (*p == COM_END)
4815 return FALSE;
4816 if (*p == COM_START)
4817 {
4818 if (*(ml_get(lnum) + leader1_len) == NUL)
4819 return FALSE;
4820 if (leader2_flags == NULL || leader2_len == 0)
4821 return FALSE;
4822 for (p = leader2_flags; *p && *p != ':'; ++p)
4823 if (*p == COM_MIDDLE)
4824 return TRUE;
4825 return FALSE;
4826 }
4827 }
4828 }
4829
4830 /*
4831 * Get current line and next line, compare the leaders.
4832 * The first line has to be saved, only one line can be locked at a time.
4833 */
4834 line1 = vim_strsave(ml_get(lnum));
4835 if (line1 != NULL)
4836 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004837 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 ;
4839 line2 = ml_get(lnum + 1);
4840 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4841 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004842 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 if (line1[idx1++] != line2[idx2])
4845 break;
4846 }
4847 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004848 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 ++idx1;
4850 }
4851 vim_free(line1);
4852 }
4853 return (idx2 == leader2_len && idx1 == leader1_len);
4854}
4855#endif
4856
4857/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004858 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 */
4860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004861op_format(
4862 oparg_T *oap,
4863 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 long old_line_count = curbuf->b_ml.ml_line_count;
4866
4867 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4868 * can put it back there. */
4869 curwin->w_cursor = oap->cursor_start;
4870
4871 if (u_save((linenr_T)(oap->start.lnum - 1),
4872 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4873 return;
4874 curwin->w_cursor = oap->start;
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (oap->is_VIsual)
4877 /* When there is no change: need to remove the Visual selection */
4878 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 /* Set '[ mark at the start of the formatted area */
4881 curbuf->b_op_start = oap->start;
4882
4883 /* For "gw" remember the cursor position and put it back below (adjusted
4884 * for joined and split lines). */
4885 if (keep_cursor)
4886 saved_cursor = oap->cursor_start;
4887
Bram Moolenaar81a82092008-03-12 16:27:00 +00004888 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Leave the cursor at the first non-blank of the last formatted line.
4892 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4893 * line, so "." will do the next lines.
4894 */
4895 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4896 ++curwin->w_cursor.lnum;
4897 beginline(BL_WHITE | BL_FIX);
4898 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4899 msgmore(old_line_count);
4900
4901 /* put '] mark on the end of the formatted area */
4902 curbuf->b_op_end = curwin->w_cursor;
4903
4904 if (keep_cursor)
4905 {
4906 curwin->w_cursor = saved_cursor;
4907 saved_cursor.lnum = 0;
4908 }
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (oap->is_VIsual)
4911 {
4912 win_T *wp;
4913
4914 FOR_ALL_WINDOWS(wp)
4915 {
4916 if (wp->w_old_cursor_lnum != 0)
4917 {
4918 /* When lines have been inserted or deleted, adjust the end of
4919 * the Visual area to be redrawn. */
4920 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4921 wp->w_old_cursor_lnum += old_line_count;
4922 else
4923 wp->w_old_visual_lnum += old_line_count;
4924 }
4925 }
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927}
4928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004929#if defined(FEAT_EVAL) || defined(PROTO)
4930/*
4931 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4932 */
4933 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004935{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004936 if (oap->is_VIsual)
4937 /* When there is no change: need to remove the Visual selection */
4938 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004939
Bram Moolenaar700303e2010-07-11 17:35:50 +02004940 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4941 /* As documented: when 'formatexpr' returns non-zero fall back to
4942 * internal formatting. */
4943 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004944}
4945
4946 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004947fex_format(
4948 linenr_T lnum,
4949 long count,
4950 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004951{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004952 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4953 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004954 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004955 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004956
4957 /*
4958 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004959 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004960 */
4961 set_vim_var_nr(VV_LNUM, lnum);
4962 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004963 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004964
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004965 /* Make a copy, the option could be changed while calling it. */
4966 fex = vim_strsave(curbuf->b_p_fex);
4967 if (fex == NULL)
4968 return 0;
4969
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004970 /*
4971 * Evaluate the function.
4972 */
4973 if (use_sandbox)
4974 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004975 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004976 if (use_sandbox)
4977 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004978
4979 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004980 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004981
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004982 return r;
4983}
4984#endif
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986/*
4987 * Format "line_count" lines, starting at the cursor position.
4988 * When "line_count" is negative, format until the end of the paragraph.
4989 * Lines after the cursor line are saved for undo, caller must have saved the
4990 * first line.
4991 */
4992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993format_lines(
4994 linenr_T line_count,
4995 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
4997 int max_len;
4998 int is_not_par; /* current line not part of parag. */
4999 int next_is_not_par; /* next line not part of paragraph */
5000 int is_end_par; /* at end of paragraph */
5001 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
5002 int next_is_start_par = FALSE;
5003#ifdef FEAT_COMMENTS
5004 int leader_len = 0; /* leader len of current line */
5005 int next_leader_len; /* leader len of next line */
5006 char_u *leader_flags = NULL; /* flags for leader of current line */
5007 char_u *next_leader_flags; /* flags for leader of next line */
5008 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005009 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#endif
5011 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005012 int second_indent = -1; /* indent for second line (comment
5013 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 int do_second_indent;
5015 int do_number_indent;
5016 int do_trail_white;
5017 int first_par_line = TRUE;
5018 int smd_save;
5019 long count;
5020 int need_set_indent = TRUE; /* set indent of next paragraph */
5021 int force_format = FALSE;
5022 int old_State = State;
5023
5024 /* length of a line to force formatting: 3 * 'tw' */
5025 max_len = comp_textwidth(TRUE) * 3;
5026
5027 /* check for 'q', '2' and '1' in 'formatoptions' */
5028#ifdef FEAT_COMMENTS
5029 do_comments = has_format_option(FO_Q_COMS);
5030#endif
5031 do_second_indent = has_format_option(FO_Q_SECOND);
5032 do_number_indent = has_format_option(FO_Q_NUMBER);
5033 do_trail_white = has_format_option(FO_WHITE_PAR);
5034
5035 /*
5036 * Get info about the previous and current line.
5037 */
5038 if (curwin->w_cursor.lnum > 1)
5039 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
5040#ifdef FEAT_COMMENTS
5041 , &leader_len, &leader_flags, do_comments
5042#endif
5043 );
5044 else
5045 is_not_par = TRUE;
5046 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
5047#ifdef FEAT_COMMENTS
5048 , &next_leader_len, &next_leader_flags, do_comments
5049#endif
5050 );
5051 is_end_par = (is_not_par || next_is_not_par);
5052 if (!is_end_par && do_trail_white)
5053 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
5054
5055 curwin->w_cursor.lnum--;
5056 for (count = line_count; count != 0 && !got_int; --count)
5057 {
5058 /*
5059 * Advance to next paragraph.
5060 */
5061 if (advance)
5062 {
5063 curwin->w_cursor.lnum++;
5064 prev_is_end_par = is_end_par;
5065 is_not_par = next_is_not_par;
5066#ifdef FEAT_COMMENTS
5067 leader_len = next_leader_len;
5068 leader_flags = next_leader_flags;
5069#endif
5070 }
5071
5072 /*
5073 * The last line to be formatted.
5074 */
5075 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5076 {
5077 next_is_not_par = TRUE;
5078#ifdef FEAT_COMMENTS
5079 next_leader_len = 0;
5080 next_leader_flags = NULL;
5081#endif
5082 }
5083 else
5084 {
5085 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
5086#ifdef FEAT_COMMENTS
5087 , &next_leader_len, &next_leader_flags, do_comments
5088#endif
5089 );
5090 if (do_number_indent)
5091 next_is_start_par =
5092 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
5093 }
5094 advance = TRUE;
5095 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
5096 if (!is_end_par && do_trail_white)
5097 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
5098
5099 /*
5100 * Skip lines that are not in a paragraph.
5101 */
5102 if (is_not_par)
5103 {
5104 if (line_count < 0)
5105 break;
5106 }
5107 else
5108 {
5109 /*
5110 * For the first line of a paragraph, check indent of second line.
5111 * Don't do this for comments and empty lines.
5112 */
5113 if (first_par_line
5114 && (do_second_indent || do_number_indent)
5115 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005116 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005118 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005119 {
5120#ifdef FEAT_COMMENTS
5121 if (leader_len == 0 && next_leader_len == 0)
5122 {
5123 /* no comment found */
5124#endif
5125 second_indent =
5126 get_indent_lnum(curwin->w_cursor.lnum + 1);
5127#ifdef FEAT_COMMENTS
5128 }
5129 else
5130 {
5131 second_indent = next_leader_len;
5132 do_comments_list = 1;
5133 }
5134#endif
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005137 {
5138#ifdef FEAT_COMMENTS
5139 if (leader_len == 0 && next_leader_len == 0)
5140 {
5141 /* no comment found */
5142#endif
5143 second_indent =
5144 get_number_indent(curwin->w_cursor.lnum);
5145#ifdef FEAT_COMMENTS
5146 }
5147 else
5148 {
5149 /* get_number_indent() is now "comment aware"... */
5150 second_indent =
5151 get_number_indent(curwin->w_cursor.lnum);
5152 do_comments_list = 1;
5153 }
5154#endif
5155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
5157
5158 /*
5159 * When the comment leader changes, it's the end of the paragraph.
5160 */
5161 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5162#ifdef FEAT_COMMENTS
5163 || !same_leader(curwin->w_cursor.lnum,
5164 leader_len, leader_flags,
5165 next_leader_len, next_leader_flags)
5166#endif
5167 )
5168 is_end_par = TRUE;
5169
5170 /*
5171 * If we have got to the end of a paragraph, or the line is
5172 * getting long, format it.
5173 */
5174 if (is_end_par || force_format)
5175 {
5176 if (need_set_indent)
5177 /* replace indent in first line with minimal number of
5178 * tabs and spaces, according to current options */
5179 (void)set_indent(get_indent(), SIN_CHANGED);
5180
5181 /* put cursor on last non-space */
5182 State = NORMAL; /* don't go past end-of-line */
5183 coladvance((colnr_T)MAXCOL);
5184 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5185 dec_cursor();
5186
5187 /* do the formatting, without 'showmode' */
5188 State = INSERT; /* for open_line() */
5189 smd_save = p_smd;
5190 p_smd = FALSE;
5191 insertchar(NUL, INSCHAR_FORMAT
5192#ifdef FEAT_COMMENTS
5193 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005194 + (do_comments && do_comments_list
5195 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005197 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 State = old_State;
5199 p_smd = smd_save;
5200 second_indent = -1;
5201 /* at end of par.: need to set indent of next par. */
5202 need_set_indent = is_end_par;
5203 if (is_end_par)
5204 {
5205 /* When called with a negative line count, break at the
5206 * end of the paragraph. */
5207 if (line_count < 0)
5208 break;
5209 first_par_line = TRUE;
5210 }
5211 force_format = FALSE;
5212 }
5213
5214 /*
5215 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005216 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 */
5218 if (!is_end_par)
5219 {
5220 advance = FALSE;
5221 curwin->w_cursor.lnum++;
5222 curwin->w_cursor.col = 0;
5223 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005227 {
5228 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005230 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005231 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005233 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5234 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005235 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005236
5237 if (indent > 0)
5238 {
5239 (void)del_bytes(indent, FALSE, FALSE);
5240 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005241 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005242 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005245 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 beep_flush();
5248 break;
5249 }
5250 first_par_line = FALSE;
5251 /* If the line is getting long, format it next time */
5252 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5253 force_format = TRUE;
5254 else
5255 force_format = FALSE;
5256 }
5257 }
5258 line_breakcheck();
5259 }
5260}
5261
5262/*
5263 * Return TRUE if line "lnum" ends in a white character.
5264 */
5265 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005266ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *s = ml_get(lnum);
5269 size_t l;
5270
5271 if (*s == NUL)
5272 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005273 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 * invocation may call function multiple times". */
5275 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005276 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277}
5278
5279/*
5280 * Blank lines, and lines containing only the comment leader, are left
5281 * untouched by the formatting. The function returns TRUE in this
5282 * case. It also returns TRUE when a line starts with the end of a comment
5283 * ('e' in comment flags), so that this line is skipped, and not joined to the
5284 * previous line. A new paragraph starts after a blank line, or when the
5285 * comment leader changes -- webb.
5286 */
5287#ifdef FEAT_COMMENTS
5288 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005289fmt_check_par(
5290 linenr_T lnum,
5291 int *leader_len,
5292 char_u **leader_flags,
5293 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295 char_u *flags = NULL; /* init for GCC */
5296 char_u *ptr;
5297
5298 ptr = ml_get(lnum);
5299 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005300 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302 *leader_len = 0;
5303
5304 if (*leader_len > 0)
5305 {
5306 /*
5307 * Search for 'e' flag in comment leader flags.
5308 */
5309 flags = *leader_flags;
5310 while (*flags && *flags != ':' && *flags != COM_END)
5311 ++flags;
5312 }
5313
5314 return (*skipwhite(ptr + *leader_len) == NUL
5315 || (*leader_len > 0 && *flags == COM_END)
5316 || startPS(lnum, NUL, FALSE));
5317}
5318#else
5319 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005320fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5323}
5324#endif
5325
5326/*
5327 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5328 * previous line is in the same paragraph. Used for auto-formatting.
5329 */
5330 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005331paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 char_u *p;
5334#ifdef FEAT_COMMENTS
5335 int leader_len = 0; /* leader len of current line */
5336 char_u *leader_flags = NULL; /* flags for leader of current line */
5337 int next_leader_len; /* leader len of next line */
5338 char_u *next_leader_flags; /* flags for leader of next line */
5339 int do_comments; /* format comments */
5340#endif
5341
5342 if (lnum <= 1)
5343 return TRUE; /* start of the file */
5344
5345 p = ml_get(lnum - 1);
5346 if (*p == NUL)
5347 return TRUE; /* after empty line */
5348
5349#ifdef FEAT_COMMENTS
5350 do_comments = has_format_option(FO_Q_COMS);
5351#endif
5352 if (fmt_check_par(lnum - 1
5353#ifdef FEAT_COMMENTS
5354 , &leader_len, &leader_flags, do_comments
5355#endif
5356 ))
5357 return TRUE; /* after non-paragraph line */
5358
5359 if (fmt_check_par(lnum
5360#ifdef FEAT_COMMENTS
5361 , &next_leader_len, &next_leader_flags, do_comments
5362#endif
5363 ))
5364 return TRUE; /* "lnum" is not a paragraph line */
5365
5366 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5367 return TRUE; /* missing trailing space in previous line. */
5368
5369 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5370 return TRUE; /* numbered item starts in "lnum". */
5371
5372#ifdef FEAT_COMMENTS
5373 if (!same_leader(lnum - 1, leader_len, leader_flags,
5374 next_leader_len, next_leader_flags))
5375 return TRUE; /* change of comment leader. */
5376#endif
5377
5378 return FALSE;
5379}
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381/*
5382 * prepare a few things for block mode yank/delete/tilde
5383 *
5384 * for delete:
5385 * - textlen includes the first/last char to be (partly) deleted
5386 * - start/endspaces is the number of columns that are taken by the
5387 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005388 * deleted.
5389 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 * - textlen includes the first/last char to be wholly yanked
5391 * - start/endspaces is the number of columns of the first/last yanked char
5392 * that are to be yanked.
5393 */
5394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005395block_prep(
5396 oparg_T *oap,
5397 struct block_def *bdp,
5398 linenr_T lnum,
5399 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 int incr = 0;
5402 char_u *pend;
5403 char_u *pstart;
5404 char_u *line;
5405 char_u *prev_pstart;
5406 char_u *prev_pend;
5407
5408 bdp->startspaces = 0;
5409 bdp->endspaces = 0;
5410 bdp->textlen = 0;
5411 bdp->start_vcol = 0;
5412 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 bdp->is_short = FALSE;
5414 bdp->is_oneChar = FALSE;
5415 bdp->pre_whitesp = 0;
5416 bdp->pre_whitesp_c = 0;
5417 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 bdp->start_char_vcols = 0;
5419
5420 line = ml_get(lnum);
5421 pstart = line;
5422 prev_pstart = line;
5423 while (bdp->start_vcol < oap->start_vcol && *pstart)
5424 {
5425 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005426 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005428 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 bdp->pre_whitesp += incr;
5431 bdp->pre_whitesp_c++;
5432 }
5433 else
5434 {
5435 bdp->pre_whitesp = 0;
5436 bdp->pre_whitesp_c = 0;
5437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005439 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441 bdp->start_char_vcols = incr;
5442 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5443 {
5444 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (!is_del || oap->op_type == OP_APPEND)
5447 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5448 }
5449 else
5450 {
5451 /* notice: this converts partly selected Multibyte characters to
5452 * spaces, too. */
5453 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5454 if (is_del && bdp->startspaces)
5455 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5456 pend = pstart;
5457 bdp->end_vcol = bdp->start_vcol;
5458 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 if (oap->op_type == OP_INSERT)
5462 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5463 else if (oap->op_type == OP_APPEND)
5464 {
5465 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5466 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5467 }
5468 else
5469 {
5470 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5471 if (is_del && oap->op_type != OP_LSHIFT)
5472 {
5473 /* just putting the sum of those two into
5474 * bdp->startspaces doesn't work for Visual replace,
5475 * so we have to split the tab in two */
5476 bdp->startspaces = bdp->start_char_vcols
5477 - (bdp->start_vcol - oap->start_vcol);
5478 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5479 }
5480 }
5481 }
5482 else
5483 {
5484 prev_pend = pend;
5485 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5486 {
5487 /* Count a tab for what it's worth (if list mode not on) */
5488 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005489 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 bdp->end_vcol += incr;
5491 }
5492 if (bdp->end_vcol <= oap->end_vcol
5493 && (!is_del
5494 || oap->op_type == OP_APPEND
5495 || oap->op_type == OP_REPLACE)) /* line too short */
5496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 /* Alternative: include spaces to fill up the block.
5499 * Disadvantage: can lead to trailing spaces when the line is
5500 * short where the text is put */
5501 /* if (!is_del || oap->op_type == OP_APPEND) */
5502 if (oap->op_type == OP_APPEND || virtual_op)
5503 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005504 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else
5506 bdp->endspaces = 0; /* replace doesn't add characters */
5507 }
5508 else if (bdp->end_vcol > oap->end_vcol)
5509 {
5510 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5511 if (!is_del && bdp->endspaces)
5512 {
5513 bdp->endspaces = incr - bdp->endspaces;
5514 if (pend != pstart)
5515 pend = prev_pend;
5516 }
5517 }
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (is_del && bdp->startspaces)
5521 pstart = prev_pstart;
5522 bdp->textlen = (int)(pend - pstart);
5523 }
5524 bdp->textcol = (colnr_T) (pstart - line);
5525 bdp->textstart = pstart;
5526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005529 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005531 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532op_addsub(
5533 oparg_T *oap,
5534 linenr_T Prenum1, /* Amount of add/subtract */
5535 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005537 pos_T pos;
5538 struct block_def bd;
5539 int change_cnt = 0;
5540 linenr_T amount = Prenum1;
5541
Bram Moolenaar6b731882018-11-16 20:54:47 +01005542 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
5543 // buffer is not completly updated yet. Postpone updating folds until before
5544 // the call to changed_lines().
5545#ifdef FEAT_FOLDING
5546 disable_fold_update++;
5547#endif
5548
Bram Moolenaard79e5502016-01-10 22:13:02 +01005549 if (!VIsual_active)
5550 {
5551 pos = curwin->w_cursor;
5552 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005553 {
5554#ifdef FEAT_FOLDING
5555 disable_fold_update--;
5556#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005557 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005558 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005559 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005560#ifdef FEAT_FOLDING
5561 disable_fold_update--;
5562#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005563 if (change_cnt)
5564 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5565 }
5566 else
5567 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005568 int one_change;
5569 int length;
5570 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005571
5572 if (u_save((linenr_T)(oap->start.lnum - 1),
5573 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005574 {
5575#ifdef FEAT_FOLDING
5576 disable_fold_update--;
5577#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005578 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005579 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005580
5581 pos = oap->start;
5582 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5583 {
5584 if (oap->block_mode) /* Visual block mode */
5585 {
5586 block_prep(oap, &bd, pos.lnum, FALSE);
5587 pos.col = bd.textcol;
5588 length = bd.textlen;
5589 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005590 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005591 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005592 curwin->w_cursor.col = 0;
5593 pos.col = 0;
5594 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5595 }
5596 else /* oap->motion_type == MCHAR */
5597 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005598 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005599 dec(&(oap->end));
5600 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5601 pos.col = 0;
5602 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005603 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005604 pos.col += oap->start.col;
5605 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005606 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005607 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005608 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005609 length = (int)STRLEN(ml_get(oap->end.lnum));
5610 if (oap->end.col >= length)
5611 oap->end.col = length - 1;
5612 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005613 }
5614 }
5615 one_change = do_addsub(oap->op_type, &pos, length, amount);
5616 if (one_change)
5617 {
5618 /* Remember the start position of the first change. */
5619 if (change_cnt == 0)
5620 startpos = curbuf->b_op_start;
5621 ++change_cnt;
5622 }
5623
5624#ifdef FEAT_NETBEANS_INTG
5625 if (netbeans_active() && one_change)
5626 {
5627 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5628
5629 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5630 netbeans_inserted(curbuf, pos.lnum, pos.col,
5631 &ptr[pos.col], length);
5632 }
5633#endif
5634 if (g_cmd && one_change)
5635 amount += Prenum1;
5636 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005637
5638#ifdef FEAT_FOLDING
5639 disable_fold_update--;
5640#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005641 if (change_cnt)
5642 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5643
5644 if (!change_cnt && oap->is_VIsual)
5645 /* No change: need to remove the Visual selection */
5646 redraw_curbuf_later(INVERTED);
5647
5648 /* Set '[ mark if something changed. Keep the last end
5649 * position from do_addsub(). */
5650 if (change_cnt > 0)
5651 curbuf->b_op_start = startpos;
5652
5653 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005654 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005655 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005656 }
5657}
5658
5659/*
5660 * Add or subtract 'Prenum1' from a number in a line
5661 * op_type is OP_NR_ADD or OP_NR_SUB
5662 *
5663 * Returns TRUE if some character was changed.
5664 */
5665 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005666do_addsub(
5667 int op_type,
5668 pos_T *pos,
5669 int length,
5670 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005671{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 int col;
5673 char_u *buf1;
5674 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005675 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005677 uvarnumber_T n;
5678 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 char_u *ptr;
5680 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 int todel;
5682 int dohex;
5683 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005684 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 int doalp;
5686 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005688 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005689 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005690 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005691 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005692 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005693 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005694 pos_T startpos;
5695 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5698 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005699 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5701
Bram Moolenaard79e5502016-01-10 22:13:02 +01005702 curwin->w_cursor = *pos;
5703 ptr = ml_get(pos->lnum);
5704 col = pos->col;
5705
5706 if (*ptr == NUL)
5707 goto theend;
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 /*
5710 * First check if we are on a hexadecimal number, after the "0x".
5711 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005712 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005714 if (dobin)
5715 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005716 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005717 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005718#ifdef FEAT_MBYTE
5719 if (has_mbyte)
5720 col -= (*mb_head_off)(ptr, ptr + col);
5721#endif
5722 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005723
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005724 if (dohex)
5725 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005726 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005727 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005728#ifdef FEAT_MBYTE
5729 if (has_mbyte)
5730 col -= (*mb_head_off)(ptr, ptr + col);
5731#endif
5732 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005733
5734 if ( dobin
5735 && dohex
5736 && ! ((col > 0
5737 && (ptr[col] == 'X'
5738 || ptr[col] == 'x')
5739 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005740#ifdef FEAT_MBYTE
5741 && (!has_mbyte ||
5742 !(*mb_head_off)(ptr, ptr + col - 1))
5743#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005744 && vim_isxdigit(ptr[col + 1]))))
5745 {
5746
5747 /* In case of binary/hexadecimal pattern overlap match, rescan */
5748
Bram Moolenaard79e5502016-01-10 22:13:02 +01005749 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005750
5751 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005752 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005753 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005754#ifdef FEAT_MBYTE
5755 if (has_mbyte)
5756 col -= (*mb_head_off)(ptr, ptr + col);
5757#endif
5758 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005759 }
5760
5761 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005762 && col > 0
5763 && (ptr[col] == 'X'
5764 || ptr[col] == 'x')
5765 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005766#ifdef FEAT_MBYTE
5767 && (!has_mbyte ||
5768 !(*mb_head_off)(ptr, ptr + col - 1))
5769#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005770 && vim_isxdigit(ptr[col + 1])) ||
5771 ( dobin
5772 && col > 0
5773 && (ptr[col] == 'B'
5774 || ptr[col] == 'b')
5775 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005776#ifdef FEAT_MBYTE
5777 && (!has_mbyte ||
5778 !(*mb_head_off)(ptr, ptr + col - 1))
5779#endif
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005780 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005781 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005782 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005783 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005784#ifdef FEAT_MBYTE
5785 if (has_mbyte)
5786 col -= (*mb_head_off)(ptr, ptr + col);
5787#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005788 }
5789 else
5790 {
5791 /*
5792 * Search forward and then backward to find the start of number.
5793 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005794 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005795
5796 while (ptr[col] != NUL
5797 && !vim_isdigit(ptr[col])
5798 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005799 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005800
5801 while (col > 0
5802 && vim_isdigit(ptr[col - 1])
5803 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005804 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005805 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005806#ifdef FEAT_MBYTE
5807 if (has_mbyte)
5808 col -= (*mb_head_off)(ptr, ptr + col);
5809#endif
5810 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005811 }
5812 }
5813
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005814 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005815 {
5816 while (ptr[col] != NUL && length > 0
5817 && !vim_isdigit(ptr[col])
5818 && !(doalp && ASCII_ISALPHA(ptr[col])))
5819 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005820 int mb_len = MB_PTR2LEN(ptr + col);
5821
5822 col += mb_len;
5823 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005824 }
5825
5826 if (length == 0)
5827 goto theend;
5828
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005829 if (col > pos->col && ptr[col - 1] == '-'
5830#ifdef FEAT_MBYTE
5831 && (!has_mbyte ||
5832 !(*mb_head_off)(ptr, ptr + col - 1))
5833#endif
5834 )
Bram Moolenaard79e5502016-01-10 22:13:02 +01005835 {
5836 negative = TRUE;
5837 was_positive = FALSE;
5838 }
5839 }
5840
5841 /*
5842 * If a number was found, and saving for undo works, replace the number.
5843 */
5844 firstdigit = ptr[col];
5845 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5846 {
5847 beep_flush();
5848 goto theend;
5849 }
5850
5851 if (doalp && ASCII_ISALPHA(firstdigit))
5852 {
5853 /* decrement or increment alphabetic character */
5854 if (op_type == OP_NR_SUB)
5855 {
5856 if (CharOrd(firstdigit) < Prenum1)
5857 {
5858 if (isupper(firstdigit))
5859 firstdigit = 'A';
5860 else
5861 firstdigit = 'a';
5862 }
5863 else
5864#ifdef EBCDIC
5865 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5866#else
5867 firstdigit -= Prenum1;
5868#endif
5869 }
5870 else
5871 {
5872 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5873 {
5874 if (isupper(firstdigit))
5875 firstdigit = 'Z';
5876 else
5877 firstdigit = 'z';
5878 }
5879 else
5880#ifdef EBCDIC
5881 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5882#else
5883 firstdigit += Prenum1;
5884#endif
5885 }
5886 curwin->w_cursor.col = col;
5887 if (!did_change)
5888 startpos = curwin->w_cursor;
5889 did_change = TRUE;
5890 (void)del_char(FALSE);
5891 ins_char(firstdigit);
5892 endpos = curwin->w_cursor;
5893 curwin->w_cursor.col = col;
5894 }
5895 else
5896 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005897 if (col > 0 && ptr[col - 1] == '-'
5898#ifdef FEAT_MBYTE
5899 && (!has_mbyte ||
5900 !(*mb_head_off)(ptr, ptr + col - 1))
5901#endif
5902 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005903 {
5904 /* negative number */
5905 --col;
5906 negative = TRUE;
5907 }
5908 /* get the number value (unsigned) */
5909 if (visual && VIsual_mode != 'V')
5910 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5911 ? (int)STRLEN(ptr) - col
5912 : length);
5913
5914 vim_str2nr(ptr + col, &pre, &length,
5915 0 + (dobin ? STR2NR_BIN : 0)
5916 + (dooct ? STR2NR_OCT : 0)
5917 + (dohex ? STR2NR_HEX : 0),
5918 NULL, &n, maxlen);
5919
5920 /* ignore leading '-' for hex and octal and bin numbers */
5921 if (pre && negative)
5922 {
5923 ++col;
5924 --length;
5925 negative = FALSE;
5926 }
5927 /* add or subtract */
5928 subtract = FALSE;
5929 if (op_type == OP_NR_SUB)
5930 subtract ^= TRUE;
5931 if (negative)
5932 subtract ^= TRUE;
5933
5934 oldn = n;
5935 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005936 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005937 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005938 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005939 /* handle wraparound for decimal numbers */
5940 if (!pre)
5941 {
5942 if (subtract)
5943 {
5944 if (n > oldn)
5945 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005946 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005947 negative ^= TRUE;
5948 }
5949 }
5950 else
5951 {
5952 /* add */
5953 if (n < oldn)
5954 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005955 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005956 negative ^= TRUE;
5957 }
5958 }
5959 if (n == 0)
5960 negative = FALSE;
5961 }
5962
5963 if (visual && !was_positive && !negative && col > 0)
5964 {
5965 /* need to remove the '-' */
5966 col--;
5967 length++;
5968 }
5969
5970 /*
5971 * Delete the old number.
5972 */
5973 curwin->w_cursor.col = col;
5974 if (!did_change)
5975 startpos = curwin->w_cursor;
5976 did_change = TRUE;
5977 todel = length;
5978 c = gchar_cursor();
5979 /*
5980 * Don't include the '-' in the length, only the length of the
5981 * part after it is kept the same.
5982 */
5983 if (c == '-')
5984 --length;
5985 while (todel-- > 0)
5986 {
5987 if (c < 0x100 && isalpha(c))
5988 {
5989 if (isupper(c))
5990 hexupper = TRUE;
5991 else
5992 hexupper = FALSE;
5993 }
5994 /* del_char() will mark line needing displaying */
5995 (void)del_char(FALSE);
5996 c = gchar_cursor();
5997 }
5998
5999 /*
6000 * Prepare the leading characters in buf1[].
6001 * When there are many leading zeros it could be very long.
6002 * Allocate a bit too much.
6003 */
6004 buf1 = alloc((unsigned)length + NUMBUFLEN);
6005 if (buf1 == NULL)
6006 goto theend;
6007 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02006008 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01006009 {
6010 *ptr++ = '-';
6011 }
6012 if (pre)
6013 {
6014 *ptr++ = '0';
6015 --length;
6016 }
6017 if (pre == 'b' || pre == 'B' ||
6018 pre == 'x' || pre == 'X')
6019 {
6020 *ptr++ = pre;
6021 --length;
6022 }
6023
6024 /*
6025 * Put the number characters in buf2[].
6026 */
6027 if (pre == 'b' || pre == 'B')
6028 {
6029 int i;
6030 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006031 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006032
6033 /* leading zeros */
6034 for (bit = bits; bit > 0; bit--)
6035 if ((n >> (bit - 1)) & 0x1) break;
6036
6037 for (i = 0; bit > 0; bit--)
6038 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
6039
6040 buf2[i] = '\0';
6041 }
6042 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02006043 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01006044 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006045 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02006046 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01006047 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006048 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02006049 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01006050 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006051 else
Bram Moolenaarea391762018-04-08 13:07:22 +02006052 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01006053 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01006054 length -= (int)STRLEN(buf2);
6055
6056 /*
6057 * Adjust number of zeros to the new number of digits, so the
6058 * total length of the number remains the same.
6059 * Don't do this when
6060 * the result may look like an octal number.
6061 */
6062 if (firstdigit == '0' && !(dooct && pre == 0))
6063 while (length-- > 0)
6064 *ptr++ = '0';
6065 *ptr = NUL;
6066 STRCAT(buf1, buf2);
6067 ins_str(buf1); /* insert the new number */
6068 vim_free(buf1);
6069 endpos = curwin->w_cursor;
6070 if (did_change && curwin->w_cursor.col)
6071 --curwin->w_cursor.col;
6072 }
6073
Bram Moolenaara52dfae2016-01-10 20:21:57 +01006074 if (did_change)
6075 {
6076 /* set the '[ and '] marks */
6077 curbuf->b_op_start = startpos;
6078 curbuf->b_op_end = endpos;
6079 if (curbuf->b_op_end.col > 0)
6080 --curbuf->b_op_end.col;
6081 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01006082
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006083theend:
6084 if (visual)
6085 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01006086 else if (did_change)
6087 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01006088
Bram Moolenaard79e5502016-01-10 22:13:02 +01006089 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090}
6091
6092#ifdef FEAT_VIMINFO
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006093
6094static yankreg_T *y_read_regs = NULL;
6095
6096#define REG_PREVIOUS 1
6097#define REG_EXEC 2
6098
6099/*
6100 * Prepare for reading viminfo registers when writing viminfo later.
6101 */
6102 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006103prepare_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006104{
6105 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
6106 * (int)sizeof(yankreg_T));
6107}
6108
6109 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02006110finish_viminfo_registers(void)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006111{
6112 int i;
6113 int j;
6114
6115 if (y_read_regs != NULL)
6116 {
6117 for (i = 0; i < NUM_REGISTERS; ++i)
6118 if (y_read_regs[i].y_array != NULL)
6119 {
6120 for (j = 0; j < y_read_regs[i].y_size; j++)
6121 vim_free(y_read_regs[i].y_array[j]);
6122 vim_free(y_read_regs[i].y_array);
6123 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01006124 VIM_CLEAR(y_read_regs);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006125 }
6126}
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006129read_viminfo_register(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 int eof;
6132 int do_it = TRUE;
6133 int size;
6134 int limit;
6135 int i;
6136 int set_prev = FALSE;
6137 char_u *str;
6138 char_u **array = NULL;
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006139 int new_type = MCHAR; /* init to shut up compiler */
6140 colnr_T new_width = 0; /* init to shut up compiler */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 /* We only get here (hopefully) if line[0] == '"' */
6143 str = virp->vir_line + 1;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006144
6145 /* If the line starts with "" this is the y_previous register. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (*str == '"')
6147 {
6148 set_prev = TRUE;
6149 str++;
6150 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00006151
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (!ASCII_ISALNUM(*str) && *str != '-')
6153 {
6154 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
6155 return TRUE; /* too many errors, pretend end-of-file */
6156 do_it = FALSE;
6157 }
6158 get_yank_register(*str++, FALSE);
6159 if (!force && y_current->y_array != NULL)
6160 do_it = FALSE;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006161
6162 if (*str == '@')
6163 {
6164 /* "x@: register x used for @@ */
6165 if (force || execreg_lastc == NUL)
6166 execreg_lastc = str[-1];
6167 }
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 size = 0;
6170 limit = 100; /* Optimized for registers containing <= 100 lines */
6171 if (do_it)
6172 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006173 /*
6174 * Build the new register in array[].
6175 * y_array is kept as-is until done.
6176 * The "do_it" flag is reset when something is wrong, in which case
6177 * array[] needs to be freed.
6178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (set_prev)
6180 y_previous = y_current;
Bram Moolenaare88b0032014-12-17 21:00:49 +01006181 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
Bram Moolenaar42b94362009-05-26 16:12:37 +00006182 str = skipwhite(skiptowhite(str));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (STRNCMP(str, "CHAR", 4) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006184 new_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 else if (STRNCMP(str, "BLOCK", 5) == 0)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006186 new_type = MBLOCK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006188 new_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 /* get the block width; if it's missing we get a zero, which is OK */
6190 str = skipwhite(skiptowhite(str));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006191 new_width = getdigits(&str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 }
6193
6194 while (!(eof = viminfo_readline(virp))
6195 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
6196 {
6197 if (do_it)
6198 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006199 if (size == limit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006201 char_u **new_array = (char_u **)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
Bram Moolenaare88b0032014-12-17 21:00:49 +01006203
6204 if (new_array == NULL)
6205 {
6206 do_it = FALSE;
6207 break;
6208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 for (i = 0; i < limit; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006210 new_array[i] = array[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 vim_free(array);
Bram Moolenaare88b0032014-12-17 21:00:49 +01006212 array = new_array;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 limit *= 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215 str = viminfo_readstring(virp, 1, TRUE);
6216 if (str != NULL)
6217 array[size++] = str;
6218 else
Bram Moolenaare88b0032014-12-17 21:00:49 +01006219 /* error, don't store the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 do_it = FALSE;
6221 }
6222 }
Bram Moolenaar7cbc7032015-01-18 14:08:56 +01006223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (do_it)
6225 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006226 /* free y_array[] */
6227 for (i = 0; i < y_current->y_size; i++)
6228 vim_free(y_current->y_array[i]);
6229 vim_free(y_current->y_array);
6230
6231 y_current->y_type = new_type;
6232 y_current->y_width = new_width;
6233 y_current->y_size = size;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006234 y_current->y_time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (size == 0)
6236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 y_current->y_array = NULL;
6238 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
Bram Moolenaare88b0032014-12-17 21:00:49 +01006241 /* Move the lines from array[] to y_array[]. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 y_current->y_array =
6243 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
6244 for (i = 0; i < size; i++)
Bram Moolenaare88b0032014-12-17 21:00:49 +01006245 {
6246 if (y_current->y_array == NULL)
6247 vim_free(array[i]);
6248 else
6249 y_current->y_array[i] = array[i];
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
Bram Moolenaare88b0032014-12-17 21:00:49 +01006253 else
6254 {
6255 /* Free array[] if it was filled. */
6256 for (i = 0; i < size; i++)
6257 vim_free(array[i]);
6258 }
6259 vim_free(array);
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 return eof;
6262}
6263
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006264/*
6265 * Accept a new style register line from the viminfo, store it when it's new.
6266 */
6267 void
6268handle_viminfo_register(garray_T *values, int force)
6269{
6270 bval_T *vp = (bval_T *)values->ga_data;
6271 int flags;
6272 int name;
6273 int type;
6274 int linecount;
6275 int width;
6276 time_t timestamp;
6277 yankreg_T *y_ptr;
6278 int i;
6279
6280 /* Check the format:
6281 * |{bartype},{flags},{name},{type},
6282 * {linecount},{width},{timestamp},"line1","line2"
6283 */
6284 if (values->ga_len < 6
6285 || vp[0].bv_type != BVAL_NR
6286 || vp[1].bv_type != BVAL_NR
6287 || vp[2].bv_type != BVAL_NR
6288 || vp[3].bv_type != BVAL_NR
6289 || vp[4].bv_type != BVAL_NR
6290 || vp[5].bv_type != BVAL_NR)
6291 return;
6292 flags = vp[0].bv_nr;
6293 name = vp[1].bv_nr;
Bram Moolenaar67e37202016-06-14 21:32:28 +02006294 if (name < 0 || name >= NUM_REGISTERS)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006295 return;
6296 type = vp[2].bv_nr;
6297 if (type != MCHAR && type != MLINE && type != MBLOCK)
6298 return;
6299 linecount = vp[3].bv_nr;
6300 if (values->ga_len < 6 + linecount)
6301 return;
6302 width = vp[4].bv_nr;
6303 if (width < 0)
6304 return;
6305
6306 if (y_read_regs != NULL)
6307 /* Reading viminfo for merging and writing. Store the register
6308 * content, don't update the current registers. */
6309 y_ptr = &y_read_regs[name];
6310 else
6311 y_ptr = &y_regs[name];
6312
6313 /* Do not overwrite unless forced or the timestamp is newer. */
6314 timestamp = (time_t)vp[5].bv_nr;
6315 if (y_ptr->y_array != NULL && !force
6316 && (timestamp == 0 || y_ptr->y_time_set > timestamp))
6317 return;
6318
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02006319 if (y_ptr->y_array != NULL)
6320 for (i = 0; i < y_ptr->y_size; i++)
6321 vim_free(y_ptr->y_array[i]);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006322 vim_free(y_ptr->y_array);
6323
6324 if (y_read_regs == NULL)
6325 {
6326 if (flags & REG_PREVIOUS)
6327 y_previous = y_ptr;
6328 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
6329 execreg_lastc = get_register_name(name);
6330 }
6331 y_ptr->y_type = type;
6332 y_ptr->y_width = width;
6333 y_ptr->y_size = linecount;
6334 y_ptr->y_time_set = timestamp;
6335 if (linecount == 0)
6336 y_ptr->y_array = NULL;
6337 else
6338 {
6339 y_ptr->y_array =
6340 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
6341 for (i = 0; i < linecount; i++)
6342 {
6343 if (vp[i + 6].bv_allocated)
6344 {
6345 y_ptr->y_array[i] = vp[i + 6].bv_string;
6346 vp[i + 6].bv_string = NULL;
6347 }
6348 else
6349 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
6350 }
6351 }
6352}
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006355write_viminfo_registers(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006357 int i, j;
6358 char_u *type;
6359 char_u c;
6360 int num_lines;
6361 int max_num_lines;
6362 int max_kbyte;
6363 long len;
6364 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
Bram Moolenaar64404472010-06-26 06:24:45 +02006366 fputs(_("\n# Registers:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368 /* Get '<' value, use old '"' value if '<' is not found. */
6369 max_num_lines = get_viminfo_parameter('<');
6370 if (max_num_lines < 0)
6371 max_num_lines = get_viminfo_parameter('"');
6372 if (max_num_lines == 0)
6373 return;
6374 max_kbyte = get_viminfo_parameter('s');
6375 if (max_kbyte == 0)
6376 return;
Bram Moolenaar42b94362009-05-26 16:12:37 +00006377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 for (i = 0; i < NUM_REGISTERS; i++)
6379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380#ifdef FEAT_CLIPBOARD
6381 /* Skip '*'/'+' register, we don't want them back next time */
6382 if (i == STAR_REGISTER || i == PLUS_REGISTER)
6383 continue;
6384#endif
6385#ifdef FEAT_DND
6386 /* Neither do we want the '~' register */
6387 if (i == TILDE_REGISTER)
6388 continue;
6389#endif
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006390 /* When reading viminfo for merging and writing: Use the register from
6391 * viminfo if it's newer. */
6392 if (y_read_regs != NULL
6393 && y_read_regs[i].y_array != NULL
6394 && (y_regs[i].y_array == NULL ||
6395 y_read_regs[i].y_time_set > y_regs[i].y_time_set))
6396 y_ptr = &y_read_regs[i];
6397 else if (y_regs[i].y_array == NULL)
6398 continue;
6399 else
6400 y_ptr = &y_regs[i];
6401
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006402 /* Skip empty registers. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006403 num_lines = y_ptr->y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006404 if (num_lines == 0
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006405 || (num_lines == 1 && y_ptr->y_type == MCHAR
6406 && *y_ptr->y_array[0] == NUL))
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00006407 continue;
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (max_kbyte > 0)
6410 {
6411 /* Skip register if there is more text than the maximum size. */
6412 len = 0;
6413 for (j = 0; j < num_lines; j++)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006414 len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (len > (long)max_kbyte * 1024L)
6416 continue;
6417 }
6418
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006419 switch (y_ptr->y_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 {
6421 case MLINE:
6422 type = (char_u *)"LINE";
6423 break;
6424 case MCHAR:
6425 type = (char_u *)"CHAR";
6426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 case MBLOCK:
6428 type = (char_u *)"BLOCK";
6429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006431 semsg(_("E574: Unknown register type %d"), y_ptr->y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 type = (char_u *)"LINE";
6433 break;
6434 }
6435 if (y_previous == &y_regs[i])
6436 fprintf(fp, "\"");
6437 c = get_register_name(i);
Bram Moolenaar42b94362009-05-26 16:12:37 +00006438 fprintf(fp, "\"%c", c);
6439 if (c == execreg_lastc)
6440 fprintf(fp, "@");
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006441 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443 /* If max_num_lines < 0, then we save ALL the lines in the register */
6444 if (max_num_lines > 0 && num_lines > max_num_lines)
6445 num_lines = max_num_lines;
6446 for (j = 0; j < num_lines; j++)
6447 {
6448 putc('\t', fp);
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006449 viminfo_writestring(fp, y_ptr->y_array[j]);
6450 }
6451
6452 {
6453 int flags = 0;
6454 int remaining;
6455
6456 /* New style with a bar line. Format:
6457 * |{bartype},{flags},{name},{type},
6458 * {linecount},{width},{timestamp},"line1","line2"
6459 * flags: REG_PREVIOUS - register is y_previous
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006460 * REG_EXEC - used for @@
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006461 */
6462 if (y_previous == &y_regs[i])
6463 flags |= REG_PREVIOUS;
6464 if (c == execreg_lastc)
6465 flags |= REG_EXEC;
6466 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
6467 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
6468 (long)y_ptr->y_time_set);
6469 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */
6470 remaining = LSIZE - 71;
6471 for (j = 0; j < num_lines; j++)
6472 {
6473 putc(',', fp);
6474 --remaining;
6475 remaining = barline_writestring(fp, y_ptr->y_array[j],
6476 remaining);
6477 }
6478 putc('\n', fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
6480 }
6481}
6482#endif /* FEAT_VIMINFO */
6483
6484#if defined(FEAT_CLIPBOARD) || defined(PROTO)
6485/*
6486 * SELECTION / PRIMARY ('*')
6487 *
6488 * Text selection stuff that uses the GUI selection register '*'. When using a
6489 * GUI this may be text from another window, otherwise it is the last text we
6490 * had highlighted with VIsual mode. With mouse support, clicking the middle
6491 * button performs the paste, otherwise you will need to do <"*p>. "
6492 * If not under X, it is synonymous with the clipboard register '+'.
6493 *
6494 * X CLIPBOARD ('+')
6495 *
6496 * Text selection stuff that uses the GUI clipboard register '+'.
6497 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
6498 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
6499 * otherwise you will need to do <"+p>. "
6500 * If not under X, it is synonymous with the selection register '*'.
6501 */
6502
6503/*
6504 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01006505 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 * only exist while the owning application exists, so we write to the
6507 * permanent (while X runs) store CUT_BUFFER0.
6508 * Dump the CLIPBOARD selection if we own it (it's logically the more
6509 * 'permanent' of the two), otherwise the PRIMARY one.
6510 * For now, use a hard-coded sanity limit of 1Mb of data.
6511 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02006512#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006514x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 Display *dpy;
6517 char_u *str = NULL;
6518 long_u len = 0;
6519 int motion_type = -1;
6520
6521# ifdef FEAT_GUI
6522 if (gui.in_use)
6523 dpy = X_DISPLAY;
6524 else
6525# endif
6526# ifdef FEAT_XCLIPBOARD
6527 dpy = xterm_dpy;
6528# else
6529 return;
6530# endif
6531
6532 /* Get selection to export */
6533 if (clip_plus.owned)
6534 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6535 else if (clip_star.owned)
6536 motion_type = clip_convert_selection(&str, &len, &clip_star);
6537
6538 /* Check it's OK */
6539 if (dpy != NULL && str != NULL && motion_type >= 0
6540 && len < 1024*1024 && len > 0)
6541 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006542#ifdef FEAT_MBYTE
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006543 int ok = TRUE;
6544
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006545 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6546 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6547 * encoding conversion usually doesn't work, so keep the text as-is.
6548 */
6549 if (has_mbyte)
6550 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006551 vimconv_T vc;
6552
6553 vc.vc_type = CONV_NONE;
6554 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6555 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006556 int intlen = len;
6557 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006558
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006559 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006560 conv_str = string_convert(&vc, str, &intlen);
6561 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006562 if (conv_str != NULL)
6563 {
6564 vim_free(str);
6565 str = conv_str;
6566 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006567 else
6568 {
6569 ok = FALSE;
6570 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006571 convert_setup(&vc, NULL, NULL);
6572 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006573 else
6574 {
6575 ok = FALSE;
6576 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006577 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006578
6579 /* Do not store the string if conversion failed. Better to use any
6580 * other selection than garbled text. */
6581 if (ok)
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006582#endif
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006583 {
6584 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6585 XFlush(dpy);
6586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
6588
6589 vim_free(str);
6590}
6591#endif
6592
6593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006594clip_free_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006596 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598 if (cbd == &clip_plus)
6599 y_current = &y_regs[PLUS_REGISTER];
6600 else
6601 y_current = &y_regs[STAR_REGISTER];
6602 free_yank_all();
6603 y_current->y_size = 0;
6604 y_current = y_ptr;
6605}
6606
6607/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006608 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 */
6610 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611clip_get_selection(VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006613 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 pos_T old_visual;
6616 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 colnr_T old_curswant;
6618 int old_set_curswant;
6619 pos_T old_op_start, old_op_end;
6620 oparg_T oa;
6621 cmdarg_T ca;
6622
6623 if (cbd->owned)
6624 {
6625 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6626 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6627 return;
6628
6629 /* Get the text between clip_star.start & clip_star.end */
6630 old_y_previous = y_previous;
6631 old_y_current = y_current;
6632 old_cursor = curwin->w_cursor;
6633 old_curswant = curwin->w_curswant;
6634 old_set_curswant = curwin->w_set_curswant;
6635 old_op_start = curbuf->b_op_start;
6636 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 old_visual = VIsual;
6638 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 clear_oparg(&oa);
6640 oa.regname = (cbd == &clip_plus ? '+' : '*');
6641 oa.op_type = OP_YANK;
6642 vim_memset(&ca, 0, sizeof(ca));
6643 ca.oap = &oa;
6644 ca.cmdchar = 'y';
6645 ca.count1 = 1;
6646 ca.retval = CA_NO_ADJ_OP_END;
6647 do_pending_operator(&ca, 0, TRUE);
6648 y_previous = old_y_previous;
6649 y_current = old_y_current;
6650 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006651 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 curwin->w_curswant = old_curswant;
6653 curwin->w_set_curswant = old_set_curswant;
6654 curbuf->b_op_start = old_op_start;
6655 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 VIsual = old_visual;
6657 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006659 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 clip_free_selection(cbd);
6662
6663 /* Try to get selected text from another window */
6664 clip_gen_request_selection(cbd);
6665 }
6666}
6667
Bram Moolenaard44347f2011-06-19 01:14:29 +02006668/*
6669 * Convert from the GUI selection string into the '*'/'+' register.
6670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006672clip_yank_selection(
6673 int type,
6674 char_u *str,
6675 long len,
6676 VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006678 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 if (cbd == &clip_plus)
6681 y_ptr = &y_regs[PLUS_REGISTER];
6682 else
6683 y_ptr = &y_regs[STAR_REGISTER];
6684
6685 clip_free_selection(cbd);
6686
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006687 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690/*
6691 * Convert the '*'/'+' register into a GUI selection string returned in *str
6692 * with length *len.
6693 * Returns the motion type, or -1 for failure.
6694 */
6695 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006696clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 char_u *p;
6699 int lnum;
6700 int i, j;
6701 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006702 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 if (cbd == &clip_plus)
6705 y_ptr = &y_regs[PLUS_REGISTER];
6706 else
6707 y_ptr = &y_regs[STAR_REGISTER];
6708
6709#ifdef USE_CRNL
6710 eolsize = 2;
6711#else
6712 eolsize = 1;
6713#endif
6714
6715 *str = NULL;
6716 *len = 0;
6717 if (y_ptr->y_array == NULL)
6718 return -1;
6719
6720 for (i = 0; i < y_ptr->y_size; i++)
6721 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6722
6723 /*
6724 * Don't want newline character at end of last line if we're in MCHAR mode.
6725 */
6726 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6727 *len -= eolsize;
6728
6729 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
6730 if (p == NULL)
6731 return -1;
6732 lnum = 0;
6733 for (i = 0, j = 0; i < (int)*len; i++, j++)
6734 {
6735 if (y_ptr->y_array[lnum][j] == '\n')
6736 p[i] = NUL;
6737 else if (y_ptr->y_array[lnum][j] == NUL)
6738 {
6739#ifdef USE_CRNL
6740 p[i++] = '\r';
6741#endif
6742#ifdef USE_CR
6743 p[i] = '\r';
6744#else
6745 p[i] = '\n';
6746#endif
6747 lnum++;
6748 j = -1;
6749 }
6750 else
6751 p[i] = y_ptr->y_array[lnum][j];
6752 }
6753 return y_ptr->y_type;
6754}
6755
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757/*
6758 * If we have written to a clipboard register, send the text to the clipboard.
6759 */
6760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6764 {
6765 clip_own_selection(&clip_star);
6766 clip_gen_set_selection(&clip_star);
6767 }
6768 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6769 {
6770 clip_own_selection(&clip_plus);
6771 clip_gen_set_selection(&clip_plus);
6772 }
6773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
6775#endif /* FEAT_CLIPBOARD || PROTO */
6776
6777
6778#if defined(FEAT_DND) || defined(PROTO)
6779/*
6780 * Replace the contents of the '~' register with str.
6781 */
6782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006783dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006785 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 curr = y_current;
6788 y_current = &y_regs[TILDE_REGISTER];
6789 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006790 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 y_current = curr;
6792}
6793#endif
6794
6795
6796#if defined(FEAT_EVAL) || defined(PROTO)
6797/*
6798 * Return the type of a register.
6799 * Used for getregtype()
6800 * Returns MAUTO for error.
6801 */
6802 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006803get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 switch (regname)
6806 {
6807 case '%': /* file name */
6808 case '#': /* alternate file name */
6809 case '=': /* expression */
6810 case ':': /* last command line */
6811 case '/': /* last search-pattern */
6812 case '.': /* last inserted text */
6813#ifdef FEAT_SEARCHPATH
6814 case Ctrl_F: /* Filename under cursor */
6815 case Ctrl_P: /* Path under cursor, expand via "path" */
6816#endif
6817 case Ctrl_W: /* word under cursor */
6818 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6819 case '_': /* black hole: always empty */
6820 return MCHAR;
6821 }
6822
6823#ifdef FEAT_CLIPBOARD
6824 regname = may_get_selection(regname);
6825#endif
6826
Bram Moolenaar32b92012014-01-14 12:33:36 +01006827 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006828 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 get_yank_register(regname, FALSE);
6831
6832 if (y_current->y_array != NULL)
6833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (reglen != NULL && y_current->y_type == MBLOCK)
6835 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 return y_current->y_type;
6837 }
6838 return MAUTO;
6839}
6840
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006841/*
6842 * When "flags" has GREG_LIST return a list with text "s".
6843 * Otherwise just return "s".
6844 */
6845 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006846getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006847{
6848 if (flags & GREG_LIST)
6849 {
6850 list_T *list = list_alloc();
6851
6852 if (list != NULL)
6853 {
6854 if (list_append_string(list, NULL, -1) == FAIL)
6855 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006856 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006857 return NULL;
6858 }
6859 list->lv_first->li_tv.vval.v_string = s;
6860 }
6861 return (char_u *)list;
6862 }
6863 return s;
6864}
6865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * Return the contents of a register as a single allocated string.
6868 * Used for "@r" in expressions and for getreg().
6869 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006870 * Flags:
6871 * GREG_NO_EXPR Do not allow expression register
6872 * GREG_EXPR_SRC For the expression register: return expression itself,
6873 * not the result of its evaluation.
6874 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 */
6876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006877get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 long i;
6880 char_u *retval;
6881 int allocated;
6882 long len;
6883
6884 /* Don't allow using an expression register inside an expression */
6885 if (regname == '=')
6886 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006887 if (flags & GREG_NO_EXPR)
6888 return NULL;
6889 if (flags & GREG_EXPR_SRC)
6890 return getreg_wrap_one_line(get_expr_line_src(), flags);
6891 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893
6894 if (regname == '@') /* "@@" is used for unnamed register */
6895 regname = '"';
6896
6897 /* check for valid regname */
6898 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6899 return NULL;
6900
6901#ifdef FEAT_CLIPBOARD
6902 regname = may_get_selection(regname);
6903#endif
6904
6905 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6906 {
6907 if (retval == NULL)
6908 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006909 if (allocated)
6910 return getreg_wrap_one_line(retval, flags);
6911 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913
6914 get_yank_register(regname, FALSE);
6915 if (y_current->y_array == NULL)
6916 return NULL;
6917
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006918 if (flags & GREG_LIST)
6919 {
6920 list_T *list = list_alloc();
6921 int error = FALSE;
6922
6923 if (list == NULL)
6924 return NULL;
6925 for (i = 0; i < y_current->y_size; ++i)
6926 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6927 error = TRUE;
6928 if (error)
6929 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006930 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006931 return NULL;
6932 }
6933 return (char_u *)list;
6934 }
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 /*
6937 * Compute length of resulting string.
6938 */
6939 len = 0;
6940 for (i = 0; i < y_current->y_size; ++i)
6941 {
6942 len += (long)STRLEN(y_current->y_array[i]);
6943 /*
6944 * Insert a newline between lines and after last line if
6945 * y_type is MLINE.
6946 */
6947 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6948 ++len;
6949 }
6950
6951 retval = lalloc(len + 1, TRUE);
6952
6953 /*
6954 * Copy the lines of the yank register into the string.
6955 */
6956 if (retval != NULL)
6957 {
6958 len = 0;
6959 for (i = 0; i < y_current->y_size; ++i)
6960 {
6961 STRCPY(retval + len, y_current->y_array[i]);
6962 len += (long)STRLEN(retval + len);
6963
6964 /*
6965 * Insert a NL between lines and after the last line if y_type is
6966 * MLINE.
6967 */
6968 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6969 retval[len++] = '\n';
6970 }
6971 retval[len] = NUL;
6972 }
6973
6974 return retval;
6975}
6976
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006977 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006978init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006979 int name,
6980 yankreg_T **old_y_previous,
6981 yankreg_T **old_y_current,
6982 int must_append,
6983 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006984{
6985 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6986 {
6987 emsg_invreg(name);
6988 return FAIL;
6989 }
6990
6991 /* Don't want to change the current (unnamed) register */
6992 *old_y_previous = y_previous;
6993 *old_y_current = y_current;
6994
6995 get_yank_register(name, TRUE);
6996 if (!y_append && !must_append)
6997 free_yank_all();
6998 return OK;
6999}
7000
7001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007002finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007003 int name,
7004 yankreg_T *old_y_previous,
7005 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007006{
7007# ifdef FEAT_CLIPBOARD
7008 /* Send text of clipboard register to the clipboard. */
7009 may_set_selection();
7010# endif
7011
7012 /* ':let @" = "val"' should change the meaning of the "" register */
7013 if (name != '"')
7014 y_previous = old_y_previous;
7015 y_current = old_y_current;
7016}
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018/*
7019 * Store string "str" in register "name".
7020 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
7021 * If "must_append" is TRUE, always append to the register. Otherwise append
7022 * if "name" is an uppercase letter.
7023 * Note: "maxlen" and "must_append" don't work for the "/" register.
7024 * Careful: 'str' is modified, you may have to use a copy!
7025 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
7026 */
7027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007028write_reg_contents(
7029 int name,
7030 char_u *str,
7031 int maxlen,
7032 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
7035}
7036
7037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007038write_reg_contents_lst(
7039 int name,
7040 char_u **strings,
7041 int maxlen UNUSED,
7042 int must_append,
7043 int yank_type,
7044 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007045{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007046 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007047
7048 if (name == '/'
7049#ifdef FEAT_EVAL
7050 || name == '='
7051#endif
7052 )
7053 {
7054 char_u *s;
7055
7056 if (strings[0] == NULL)
7057 s = (char_u *)"";
7058 else if (strings[1] != NULL)
7059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007061 "contain two or more lines"));
7062 return;
7063 }
7064 else
7065 s = strings[0];
7066 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
7067 return;
7068 }
7069
7070 if (name == '_') /* black hole: nothing to do */
7071 return;
7072
7073 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7074 &yank_type) == FAIL)
7075 return;
7076
7077 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
7078
7079 finish_write_reg(name, old_y_previous, old_y_current);
7080}
7081
7082 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083write_reg_contents_ex(
7084 int name,
7085 char_u *str,
7086 int maxlen,
7087 int must_append,
7088 int yank_type,
7089 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007091 yankreg_T *old_y_previous, *old_y_current;
7092 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaare7566042005-06-17 22:00:15 +00007094 if (maxlen >= 0)
7095 len = maxlen;
7096 else
7097 len = (long)STRLEN(str);
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 /* Special case: '/' search pattern */
7100 if (name == '/')
7101 {
7102 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
7103 return;
7104 }
7105
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007106 if (name == '#')
7107 {
7108 buf_T *buf;
7109
7110 if (VIM_ISDIGIT(*str))
7111 {
7112 int num = atoi((char *)str);
7113
7114 buf = buflist_findnr(num);
7115 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01007117 }
7118 else
7119 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
7120 TRUE, FALSE, FALSE));
7121 if (buf == NULL)
7122 return;
7123 curwin->w_alt_fnum = buf->b_fnum;
7124 return;
7125 }
7126
Bram Moolenaare7566042005-06-17 22:00:15 +00007127#ifdef FEAT_EVAL
7128 if (name == '=')
7129 {
7130 char_u *p, *s;
7131
7132 p = vim_strnsave(str, (int)len);
7133 if (p == NULL)
7134 return;
7135 if (must_append)
7136 {
7137 s = concat_str(get_expr_line_src(), p);
7138 vim_free(p);
7139 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00007140 }
7141 set_expr_line(p);
7142 return;
7143 }
7144#endif
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (name == '_') /* black hole: nothing to do */
7147 return;
7148
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007149 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
7150 &yank_type) == FAIL)
7151 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007153 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007155 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156}
7157#endif /* FEAT_EVAL */
7158
7159#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
7160/*
7161 * Put a string into a register. When the register is not empty, the string
7162 * is appended.
7163 */
7164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007165str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007166 yankreg_T *y_ptr, /* pointer to yank register */
7167 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
7168 char_u *str, /* string to put in register */
7169 long len, /* length of string */
7170 long blocklen, /* width of Visual block */
7171 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
Bram Moolenaard44347f2011-06-19 01:14:29 +02007173 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 int lnum;
7175 long start;
7176 long i;
7177 int extra;
7178 int newlines; /* number of lines added */
7179 int extraline = 0; /* extra line at the end */
7180 int append = FALSE; /* append to last line in register */
7181 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007182 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaarcde547a2009-11-17 11:43:06 +00007186 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 y_ptr->y_size = 0;
7188
Bram Moolenaard44347f2011-06-19 01:14:29 +02007189 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007190 type = ((str_list || (len > 0 && (str[len - 1] == NL
7191 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02007192 ? MLINE : MCHAR);
7193 else
7194 type = yank_type;
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /*
7197 * Count the number of lines within the string
7198 */
7199 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007200 if (str_list)
7201 {
7202 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007207 for (i = 0; i < len; i++)
7208 if (str[i] == '\n')
7209 ++newlines;
7210 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
7211 {
7212 extraline = 1;
7213 ++newlines; /* count extra newline at the end */
7214 }
7215 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
7216 {
7217 append = TRUE;
7218 --newlines; /* uncount newline when appending first line */
7219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 }
7221
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007222 /* Without any lines make the register empty. */
7223 if (y_ptr->y_size + newlines == 0)
7224 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01007225 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02007226 return;
7227 }
7228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 /*
7230 * Allocate an array to hold the pointers to the new register lines.
7231 * If the register was not empty, move the existing lines to the new array.
7232 */
7233 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
7234 * sizeof(char_u *), TRUE);
7235 if (pp == NULL) /* out of memory */
7236 return;
7237 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
7238 pp[lnum] = y_ptr->y_array[lnum];
7239 vim_free(y_ptr->y_array);
7240 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243 /*
7244 * Find the end of each line and save it into the array.
7245 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007246 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007248 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
7249 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02007250 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007251 pp[lnum] = vim_strnsave(*ss, i);
7252 if (i > maxlen)
7253 maxlen = i;
7254 }
7255 }
7256 else
7257 {
7258 for (start = 0; start < len + extraline; start += i + 1)
7259 {
7260 for (i = start; i < len; ++i) /* find the end of the line */
7261 if (str[i] == '\n')
7262 break;
7263 i -= start; /* i is now length of line */
7264 if (i > maxlen)
7265 maxlen = i;
7266 if (append)
7267 {
7268 --lnum;
7269 extra = (int)STRLEN(y_ptr->y_array[lnum]);
7270 }
7271 else
7272 extra = 0;
7273 s = alloc((unsigned)(i + extra + 1));
7274 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007276 if (extra)
7277 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
7278 if (append)
7279 vim_free(y_ptr->y_array[lnum]);
7280 if (i)
7281 mch_memmove(s + extra, str + start, (size_t)i);
7282 extra += i;
7283 s[extra] = NUL;
7284 y_ptr->y_array[lnum++] = s;
7285 while (--extra >= 0)
7286 {
7287 if (*s == NUL)
7288 *s = '\n'; /* replace NUL with newline */
7289 ++s;
7290 }
7291 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
7294 y_ptr->y_type = type;
7295 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 if (type == MBLOCK)
7297 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
7298 else
7299 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02007300#ifdef FEAT_VIMINFO
7301 y_ptr->y_time_set = vim_time();
7302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303}
7304#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
7305
7306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007307clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 vim_memset(oap, 0, sizeof(oparg_T));
7310}
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 *
7315 * "Words" are counted by looking for boundaries between non-space and
7316 * space characters. (it seems to produce results that match 'wc'.)
7317 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00007318 * Return value is byte count; word count for the line is added to "*wc".
7319 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 *
7321 * The function will only examine the first "limit" characters in the
7322 * line, stopping if it encounters an end-of-line (NUL byte). In that
7323 * case, eol_size will be added to the character count to account for
7324 * the size of the EOL character.
7325 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007326 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01007327line_count_info(
7328 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007329 varnumber_T *wc,
7330 varnumber_T *cc,
7331 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01007332 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007334 varnumber_T i;
7335 varnumber_T words = 0;
7336 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 int is_word = 0;
7338
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02007339 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
7341 if (is_word)
7342 {
7343 if (vim_isspace(line[i]))
7344 {
7345 words++;
7346 is_word = 0;
7347 }
7348 }
7349 else if (!vim_isspace(line[i]))
7350 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007351 ++chars;
7352#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007353 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00007354#else
7355 ++i;
7356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358
7359 if (is_word)
7360 words++;
7361 *wc += words;
7362
7363 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02007364 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007367 chars += eol_size;
7368 }
7369 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 return i;
7371}
7372
7373/*
7374 * Give some info about the position of the cursor (for "g CTRL-G").
7375 * In Visual mode, give some info about the selected region. (In this case,
7376 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01007377 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 */
7379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007380cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
7382 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00007383 char_u buf1[50];
7384 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007386 varnumber_T byte_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007387#ifdef FEAT_MBYTE
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007388 varnumber_T bom_count = 0;
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007389#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007390 varnumber_T byte_count_cursor = 0;
7391 varnumber_T char_count = 0;
7392 varnumber_T char_count_cursor = 0;
7393 varnumber_T word_count = 0;
7394 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007395 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007396 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 long line_count_selected = 0;
7398 pos_T min_pos, max_pos;
7399 oparg_T oparg;
7400 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 /*
7403 * Compute the length of the file in characters.
7404 */
7405 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7406 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007407 if (dict == NULL)
7408 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007409 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01007410 return;
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413 else
7414 {
7415 if (get_fileformat(curbuf) == EOL_DOS)
7416 eol_size = 2;
7417 else
7418 eol_size = 1;
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (VIsual_active)
7421 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007422 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 {
7424 min_pos = VIsual;
7425 max_pos = curwin->w_cursor;
7426 }
7427 else
7428 {
7429 min_pos = curwin->w_cursor;
7430 max_pos = VIsual;
7431 }
7432 if (*p_sel == 'e' && max_pos.col > 0)
7433 --max_pos.col;
7434
7435 if (VIsual_mode == Ctrl_V)
7436 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00007437#ifdef FEAT_LINEBREAK
7438 char_u * saved_sbr = p_sbr;
7439
7440 /* Make 'sbr' empty for a moment to get the correct size. */
7441 p_sbr = empty_option;
7442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 oparg.is_VIsual = 1;
7444 oparg.block_mode = TRUE;
7445 oparg.op_type = OP_NOP;
7446 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007447 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00007448#ifdef FEAT_LINEBREAK
7449 p_sbr = saved_sbr;
7450#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007451 if (curwin->w_curswant == MAXCOL)
7452 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 /* Swap the start, end vcol if needed */
7454 if (oparg.end_vcol < oparg.start_vcol)
7455 {
7456 oparg.end_vcol += oparg.start_vcol;
7457 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
7458 oparg.end_vcol -= oparg.start_vcol;
7459 }
7460 }
7461 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
7462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
7465 {
7466 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007467 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
7469 ui_breakcheck();
7470 if (got_int)
7471 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007472 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 /* Do extra processing for VIsual mode. */
7476 if (VIsual_active
7477 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
7478 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00007479 char_u *s = NULL;
7480 long len = 0L;
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 switch (VIsual_mode)
7483 {
7484 case Ctrl_V:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007485#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 virtual_op = virtual_active();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007489#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 virtual_op = MAYBE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007491#endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00007492 s = bd.textstart;
7493 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 break;
7495 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00007496 s = ml_get(lnum);
7497 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 break;
7499 case 'v':
7500 {
7501 colnr_T start_col = (lnum == min_pos.lnum)
7502 ? min_pos.col : 0;
7503 colnr_T end_col = (lnum == max_pos.lnum)
7504 ? max_pos.col - start_col + 1 : MAXCOL;
7505
Bram Moolenaardef9e822004-12-31 20:58:58 +00007506 s = ml_get(lnum) + start_col;
7507 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 break;
7510 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00007511 if (s != NULL)
7512 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00007513 byte_count_cursor += line_count_info(s, &word_count_cursor,
7514 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00007515 if (lnum == curbuf->b_ml.ml_line_count
7516 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007517 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00007518 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00007519 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00007520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 {
7524 /* In non-visual mode, check for the line the cursor is on */
7525 if (lnum == curwin->w_cursor.lnum)
7526 {
7527 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007528 char_count_cursor += char_count;
7529 byte_count_cursor = byte_count +
7530 line_count_info(ml_get(lnum),
7531 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007532 (varnumber_T)(curwin->w_cursor.col + 1),
7533 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 }
7535 }
7536 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007537 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007538 &char_count, (varnumber_T)MAXCOL,
7539 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 }
7541
7542 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007543 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007544 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
Bram Moolenaared767a22016-01-03 22:49:16 +01007546 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007548 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007550 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7551 {
7552 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7553 &max_pos.col);
7554 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7555 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7556 }
7557 else
7558 buf1[0] = NUL;
7559
7560 if (char_count_cursor == byte_count_cursor
7561 && char_count == byte_count)
7562 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007563 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007564 buf1, line_count_selected,
7565 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007566 (long_long_T)word_count_cursor,
7567 (long_long_T)word_count,
7568 (long_long_T)byte_count_cursor,
7569 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007570 else
7571 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007572 _("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 +01007573 buf1, line_count_selected,
7574 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007575 (long_long_T)word_count_cursor,
7576 (long_long_T)word_count,
7577 (long_long_T)char_count_cursor,
7578 (long_long_T)char_count,
7579 (long_long_T)byte_count_cursor,
7580 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 }
7582 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007583 {
7584 p = ml_get_curline();
7585 validate_virtcol();
7586 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7587 (int)curwin->w_virtcol + 1);
7588 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7589 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590
Bram Moolenaared767a22016-01-03 22:49:16 +01007591 if (char_count_cursor == byte_count_cursor
7592 && char_count == byte_count)
7593 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007594 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007595 (char *)buf1, (char *)buf2,
7596 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007598 (long_long_T)word_count_cursor, (long_long_T)word_count,
7599 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007600 else
7601 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007602 _("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 +01007603 (char *)buf1, (char *)buf2,
7604 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007605 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007606 (long_long_T)word_count_cursor, (long_long_T)word_count,
7607 (long_long_T)char_count_cursor, (long_long_T)char_count,
7608 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007609 }
7610 }
7611
Bram Moolenaared767a22016-01-03 22:49:16 +01007612#ifdef FEAT_MBYTE
7613 bom_count = bomb_size();
7614 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007615 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007616 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007617#endif
7618 if (dict == NULL)
7619 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007620 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007621 p = p_shm;
7622 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007623 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007624 p_shm = p;
7625 }
7626 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007627#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007628 if (dict != NULL)
7629 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007630 dict_add_number(dict, "words", word_count);
7631 dict_add_number(dict, "chars", char_count);
7632 dict_add_number(dict, "bytes", byte_count
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007633# ifdef FEAT_MBYTE
7634 + bom_count
7635# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02007636 );
7637 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7638 byte_count_cursor);
7639 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7640 char_count_cursor);
7641 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7642 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645}