blob: 8f6130cfefa6f9658465fc33e43bf2c22a136218 [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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020027static yankreg_T y_regs[NUM_REGISTERS];
28
29static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020031static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33/*
34 * structure used by block_prep, op_delete and op_yank for blockwise operators
35 * also op_change, op_shift, op_insert, op_replace - AKelly
36 */
37struct block_def
38{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000039 int startspaces; /* 'extra' cols before first char */
40 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000042 char_u *textstart; /* pointer to 1st char (partially) in block */
43 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 colnr_T start_vcol; /* start col of 1st char wholly inside block */
45 colnr_T end_vcol; /* start col of 1st char wholly after block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 int is_short; /* TRUE if line is too short to fit in block */
47 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
48 int is_oneChar; /* TRUE if block within one character */
49 int pre_whitesp; /* screen cols of ws before block */
50 int pre_whitesp_c; /* chars of ws before block */
51 colnr_T end_char_vcols; /* number of vcols of post-block char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 colnr_T start_char_vcols; /* number of vcols of pre-block char */
53};
54
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int stuff_yank(int, char_u *);
57static void put_reedit_in_typebuf(int silent);
58static int put_in_typebuf(char_u *s, int esc, int colon,
59 int silent);
60static void stuffescaped(char_u *arg, int literally);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void free_yank_all(void);
63static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020065static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010068static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
70static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020072static 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 +000073#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010074static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010076static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010078static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#endif
80
Bram Moolenaarf2732452018-06-03 14:47:35 +020081// Flags for third item in "opchars".
82#define OPF_LINES 1 // operator always works on lines
83#define OPF_CHANGE 2 // operator changes text
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085/*
86 * The names of operators.
87 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020088 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 */
90static char opchars[][3] =
91{
Bram Moolenaarf2732452018-06-03 14:47:35 +020092 {NUL, NUL, 0}, // OP_NOP
93 {'d', NUL, OPF_CHANGE}, // OP_DELETE
94 {'y', NUL, 0}, // OP_YANK
95 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
96 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
97 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
98 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
99 {'g', '~', OPF_CHANGE}, // OP_TILDE
100 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
101 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
102 {':', NUL, OPF_LINES}, // OP_COLON
103 {'g', 'U', OPF_CHANGE}, // OP_UPPER
104 {'g', 'u', OPF_CHANGE}, // OP_LOWER
105 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
106 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
107 {'g', '?', OPF_CHANGE}, // OP_ROT13
108 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
109 {'I', NUL, OPF_CHANGE}, // OP_INSERT
110 {'A', NUL, OPF_CHANGE}, // OP_APPEND
111 {'z', 'f', OPF_LINES}, // OP_FOLD
112 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
113 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
114 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
115 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
116 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
117 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
118 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
119 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
120 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
121 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122};
123
Bram Moolenaarc3328162019-07-23 22:15:25 +0200124 yankreg_T *
125get_y_regs(void)
126{
127 return y_regs;
128}
129
130 yankreg_T *
131get_y_current(void)
132{
133 return y_current;
134}
135
136 yankreg_T *
137get_y_previous(void)
138{
139 return y_previous;
140}
141
142 void
143set_y_previous(yankreg_T *yreg)
144{
145 y_previous = yreg;
146}
147
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
150 * Translate a command name into an operator type.
151 * Must only be called with a valid operator name!
152 */
153 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100154get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
156 int i;
157
158 if (char1 == 'r') /* ignore second character */
159 return OP_REPLACE;
160 if (char1 == '~') /* when tilde is an operator */
161 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100162 if (char1 == 'g' && char2 == Ctrl_A) /* add */
163 return OP_NR_ADD;
164 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
165 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (opchars[i][0] == char1 && opchars[i][1] == char2)
169 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100170 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
171 {
172 internal_error("get_op_type()");
173 break;
174 }
175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 return i;
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Return TRUE if operator "op" always works on whole lines.
181 */
182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100183op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200185 return opchars[op][2] & OPF_LINES;
186}
187
Bram Moolenaar113e1072019-01-20 15:30:40 +0100188#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200189/*
190 * Return TRUE if operator "op" changes text.
191 */
192 int
193op_is_change(int op)
194{
195 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
202 */
203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100204get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
206 return opchars[optype][0];
207}
208
209/*
210 * Get second operator command character.
211 */
212 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100213get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215 return opchars[optype][1];
216}
217
218/*
219 * op_shift - handle a shift operation
220 */
221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100222op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223{
224 long i;
225 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
228 if (u_save((linenr_T)(oap->start.lnum - 1),
229 (linenr_T)(oap->end.lnum + 1)) == FAIL)
230 return;
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 if (oap->block_mode)
233 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
235 for (i = oap->line_count; --i >= 0; )
236 {
237 first_char = *ml_get_curline();
238 if (first_char == NUL) /* empty line */
239 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 else if (oap->block_mode)
241 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 else
243 /* Move the line right if it doesn't start with '#', 'smartindent'
244 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
245#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
246 if (first_char != '#' || !preprocs_left())
247#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000248 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 ++curwin->w_cursor.lnum;
250 }
251
252 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 if (oap->block_mode)
254 {
255 curwin->w_cursor.lnum = oap->start.lnum;
256 curwin->w_cursor.col = block_col;
257 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100258 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 {
260 curwin->w_cursor.lnum = oap->start.lnum;
261 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
262 }
263 else
264 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
265
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100266#ifdef FEAT_FOLDING
267 /* The cursor line is not in a closed fold */
268 foldOpenCursor();
269#endif
270
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (oap->line_count > p_report)
273 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200274 char *op;
275 char *msg_line_single;
276 char *msg_line_plural;
277
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200279 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200281 op = "<";
282 msg_line_single = NGETTEXT("%ld line %sed %d time",
283 "%ld line %sed %d times", amount);
284 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
285 "%ld lines %sed %d times", amount);
286 vim_snprintf((char *)IObuff, IOSIZE,
287 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
288 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100289 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 }
291
292 /*
293 * Set "'[" and "']" marks.
294 */
295 curbuf->b_op_start = oap->start;
296 curbuf->b_op_end.lnum = oap->end.lnum;
297 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
298 if (curbuf->b_op_end.col > 0)
299 --curbuf->b_op_end.col;
300}
301
302/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100303 * Shift the current line one shiftwidth left (if left != 0) or right
304 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 */
306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100307shift_line(
308 int left,
309 int round,
310 int amount,
311 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
313 int count;
314 int i, j;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100315 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
317 count = get_indent(); /* get current indent */
318
319 if (round) /* round off indent */
320 {
321 i = count / p_sw; /* number of p_sw rounded down */
322 j = count % p_sw; /* extra spaces */
323 if (j && left) /* first remove extra spaces */
324 --amount;
325 if (left)
326 {
327 i -= amount;
328 if (i < 0)
329 i = 0;
330 }
331 else
332 i += amount;
333 count = i * p_sw;
334 }
335 else /* original vi indent */
336 {
337 if (left)
338 {
339 count -= p_sw * amount;
340 if (count < 0)
341 count = 0;
342 }
343 else
344 count += p_sw * amount;
345 }
346
347 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000349 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000351 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352}
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/*
355 * Shift one line of the current block one shiftwidth right or left.
356 * Leaves cursor on first character in block.
357 */
358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100359shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
361 int left = (oap->op_type == OP_LSHIFT);
362 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000363 int total;
364 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int oldcol = curwin->w_cursor.col;
Bram Moolenaarf9514162018-11-22 03:08:29 +0100366 int p_sw = (int)get_sw_value_indent(curbuf);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200367#ifdef FEAT_VARTABS
368 int *p_vts = curbuf->b_p_vts_array;
369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 int p_ts = (int)curbuf->b_p_ts;
371 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000373 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 int i = 0, j = 0;
375 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_RIGHTLEFT
377 int old_p_ri = p_ri;
378
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200379 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#endif
381
382 State = INSERT; /* don't want REPLACE for State */
383 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
384 if (bd.is_short)
385 return;
386
387 /* total is number of screen columns to be inserted/removed */
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200388 total = (int)((unsigned)amount * (unsigned)p_sw);
389 if ((total / p_sw) != amount)
390 return; /* multiplication overflow */
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 oldp = ml_get_curline();
393
394 if (!left)
395 {
396 /*
397 * 1. Get start vcol
398 * 2. Total ws vcols
399 * 3. Divvy into TABs & spp
400 * 4. Construct new string
401 */
402 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
403 ws_vcol = bd.start_vcol - bd.pre_whitesp;
404 if (bd.startspaces)
405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100407 {
408 if ((*mb_ptr2len)(bd.textstart) == 1)
409 ++bd.textstart;
410 else
411 {
412 ws_vcol = 0;
413 bd.startspaces = 0;
414 }
415 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000416 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100419 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200421 /* TODO: is passing bd.textstart for start of the line OK? */
422 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
423 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 total += incr;
425 bd.start_vcol += incr;
426 }
427 /* OK, now total=all the VWS reqd, and textstart points at the 1st
428 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200429#ifdef FEAT_VARTABS
430 if (!curbuf->b_p_et)
431 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j);
432 else
433 j = total;
434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 if (!curbuf->b_p_et)
436 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
437 if (i)
438 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
439 else
440 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 /* if we're splitting a TAB, allow for it */
443 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
444 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200445 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (newp == NULL)
447 return;
448 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
449 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200450 vim_memset(newp + bd.textcol, TAB, (size_t)i);
451 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 /* the end */
453 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
454 }
455 else /* left */
456 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 colnr_T destination_col; /* column to which text in block will
458 be shifted */
459 char_u *verbatim_copy_end; /* end of the part of the line which is
460 copied verbatim */
461 colnr_T verbatim_copy_width;/* the (displayed) width of this part
462 of line */
463 unsigned fill; /* nr of spaces that replace a TAB */
464 unsigned new_line_len; /* the length of the line after the
465 block shift */
466 size_t block_space_width;
467 size_t shift_amount;
468 char_u *non_white = bd.textstart;
469 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000471 /*
472 * Firstly, let's find the first non-whitespace character that is
473 * displayed after the block's start column and the character's column
474 * number. Also, let's calculate the width of all the whitespace
475 * characters that are displayed in the block and precede the searched
476 * non-whitespace character.
477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000479 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
480 * the part of which is displayed at the block's beginning. Let's start
481 * searching from the next character. */
482 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100483 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000484
485 /* The character's column is in "bd.start_vcol". */
486 non_white_col = bd.start_vcol;
487
Bram Moolenaar1c465442017-03-12 20:10:05 +0100488 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000489 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200490 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000491 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 }
493
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000494 block_space_width = non_white_col - oap->start_vcol;
495 /* We will shift by "total" or "block_space_width", whichever is less.
496 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000497 shift_amount = (block_space_width < (size_t)total
498 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000499
500 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000501 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000502
503 /* Now let's find out how much of the beginning of the line we can
504 * reuse without modification. */
505 verbatim_copy_end = bd.textstart;
506 verbatim_copy_width = bd.start_vcol;
507
508 /* If "bd.startspaces" is set, "bd.textstart" points to the character
509 * preceding the block. We have to subtract its width to obtain its
510 * column number. */
511 if (bd.startspaces)
512 verbatim_copy_width -= bd.start_char_vcols;
513 while (verbatim_copy_width < destination_col)
514 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200515 char_u *line = verbatim_copy_end;
516
517 /* TODO: is passing verbatim_copy_end for start of the line OK? */
518 incr = lbr_chartabsize(line, verbatim_copy_end,
519 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000520 if (verbatim_copy_width + incr > destination_col)
521 break;
522 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100523 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000524 }
525
526 /* If "destination_col" is different from the width of the initial
527 * part of the line that will be copied, it means we encountered a tab
528 * character, which we will have to partly replace with spaces. */
529 fill = destination_col - verbatim_copy_width;
530
531 /* The replacement line will consist of:
532 * - the beginning of the original line up to "verbatim_copy_end",
533 * - "fill" number of spaces,
534 * - the rest of the line, pointed to by non_white. */
535 new_line_len = (unsigned)(verbatim_copy_end - oldp)
536 + fill
537 + (unsigned)STRLEN(non_white) + 1;
538
Bram Moolenaar964b3742019-05-24 18:54:09 +0200539 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (newp == NULL)
541 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000542 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200543 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000544 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 /* replace the line */
547 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
548 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
549 State = oldstate;
550 curwin->w_cursor.col = oldcol;
551#ifdef FEAT_RIGHTLEFT
552 p_ri = old_p_ri;
553#endif
554}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556/*
557 * Insert string "s" (b_insert ? before : after) block :AKelly
558 * Caller must prepare for undo.
559 */
560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100561block_insert(
562 oparg_T *oap,
563 char_u *s,
564 int b_insert,
565 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 int p_ts;
568 int count = 0; /* extra spaces to replace a cut TAB */
569 int spaces = 0; /* non-zero if cutting a TAB */
570 colnr_T offset; /* pointer along new line */
571 unsigned s_len; /* STRLEN(s) */
572 char_u *newp, *oldp; /* new, old lines */
573 linenr_T lnum; /* loop var */
574 int oldstate = State;
575
576 State = INSERT; /* don't want REPLACE for State */
577 s_len = (unsigned)STRLEN(s);
578
579 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
580 {
581 block_prep(oap, bdp, lnum, TRUE);
582 if (bdp->is_short && b_insert)
583 continue; /* OP_INSERT, line ends before block start */
584
585 oldp = ml_get(lnum);
586
587 if (b_insert)
588 {
589 p_ts = bdp->start_char_vcols;
590 spaces = bdp->startspaces;
591 if (spaces != 0)
592 count = p_ts - 1; /* we're cutting a TAB */
593 offset = bdp->textcol;
594 }
595 else /* append */
596 {
597 p_ts = bdp->end_char_vcols;
598 if (!bdp->is_short) /* spaces = padding after block */
599 {
600 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
601 if (spaces != 0)
602 count = p_ts - 1; /* we're cutting a TAB */
603 offset = bdp->textcol + bdp->textlen - (spaces != 0);
604 }
605 else /* spaces = padding to block edge */
606 {
607 /* if $ used, just append to EOL (ie spaces==0) */
608 if (!bdp->is_MAX)
609 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
610 count = spaces;
611 offset = bdp->textcol + bdp->textlen;
612 }
613 }
614
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200615 if (has_mbyte && spaces > 0)
616 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100617 int off;
618
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200619 /* Avoid starting halfway a multi-byte character. */
620 if (b_insert)
621 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100622 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200623 }
624 else
625 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100626 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200627 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200628 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100629 spaces -= off;
630 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200631 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200632
Bram Moolenaar964b3742019-05-24 18:54:09 +0200633 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 if (newp == NULL)
635 continue;
636
637 /* copy up to shifted part */
638 mch_memmove(newp, oldp, (size_t)(offset));
639 oldp += offset;
640
641 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200642 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 /* copy the new text */
645 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
646 offset += s_len;
647
648 if (spaces && !bdp->is_short)
649 {
650 /* insert post-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200651 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 /* We're splitting a TAB, don't copy it. */
653 oldp++;
654 /* We allowed for that TAB, remember this now */
655 count++;
656 }
657
658 if (spaces > 0)
659 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000660 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 ml_replace(lnum, newp, FALSE);
663
664 if (lnum == oap->end.lnum)
665 {
666 /* Set "']" mark to the end of the block instead of the end of
667 * the insert in the first line. */
668 curbuf->b_op_end.lnum = oap->end.lnum;
669 curbuf->b_op_end.col = offset;
670 }
671 } /* for all lnum */
672
673 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
674
675 State = oldstate;
676}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
679/*
680 * op_reindent - handle reindenting a block of lines.
681 */
682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 long i;
686 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200687 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 linenr_T first_changed = 0;
689 linenr_T last_changed = 0;
690 linenr_T start_lnum = curwin->w_cursor.lnum;
691
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000692 /* Don't even try when 'modifiable' is off. */
693 if (!curbuf->b_p_ma)
694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000696 return;
697 }
698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 for (i = oap->line_count; --i >= 0 && !got_int; )
700 {
701 /* it's a slow thing to do, so give feedback so there's no worry that
702 * the computer's just hung. */
703
704 if (i > 1
705 && (i % 50 == 0 || i == oap->line_count - 1)
706 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100707 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709 /*
710 * Be vi-compatible: For lisp indenting the first line is not
711 * indented, unless there is only one line.
712 */
713#ifdef FEAT_LISP
714 if (i != oap->line_count - 1 || oap->line_count == 1
715 || how != get_lisp_indent)
716#endif
717 {
718 l = skipwhite(ml_get_curline());
719 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200720 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200722 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200724 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
726 /* did change the indent, call changed_lines() later */
727 if (first_changed == 0)
728 first_changed = curwin->w_cursor.lnum;
729 last_changed = curwin->w_cursor.lnum;
730 }
731 }
732 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000733 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735
736 /* put cursor on first non-blank of indented line */
737 curwin->w_cursor.lnum = start_lnum;
738 beginline(BL_SOL | BL_FIX);
739
740 /* Mark changed lines so that they will be redrawn. When Visual
741 * highlighting was present, need to continue until the last line. When
742 * there is no change still need to remove the Visual highlighting. */
743 if (last_changed != 0)
744 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 else if (oap->is_VIsual)
748 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750 if (oap->line_count > p_report)
751 {
752 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200754 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756 /* set '[ and '] marks */
757 curbuf->b_op_start = oap->start;
758 curbuf->b_op_end = oap->end;
759}
760#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
761
762#if defined(FEAT_EVAL) || defined(PROTO)
763/*
764 * Keep the last expression line here, for repeating.
765 */
766static char_u *expr_line = NULL;
767
768/*
769 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
770 * Returns '=' when OK, NUL otherwise.
771 */
772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100773get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774{
775 char_u *new_line;
776
Bram Moolenaare96a2492019-06-25 04:12:16 +0200777 new_line = getcmdline('=', 0L, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (new_line == NULL)
779 return NUL;
780 if (*new_line == NUL) /* use previous line */
781 vim_free(new_line);
782 else
783 set_expr_line(new_line);
784 return '=';
785}
786
787/*
788 * Set the expression for the '=' register.
789 * Argument must be an allocated string.
790 */
791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100792set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794 vim_free(expr_line);
795 expr_line = new_line;
796}
797
798/*
799 * Get the result of the '=' register expression.
800 * Returns a pointer to allocated memory, or NULL for failure.
801 */
802 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100803get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 char_u *expr_copy;
806 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000807 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 if (expr_line == NULL)
810 return NULL;
811
812 /* Make a copy of the expression, because evaluating it may cause it to be
813 * changed. */
814 expr_copy = vim_strsave(expr_line);
815 if (expr_copy == NULL)
816 return NULL;
817
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000818 /* When we are invoked recursively limit the evaluation to 10 levels.
819 * Then return the string as-is. */
820 if (nested >= 10)
821 return expr_copy;
822
823 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000825 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 vim_free(expr_copy);
827 return rv;
828}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000829
830/*
831 * Get the '=' register expression itself, without evaluating it.
832 */
833 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100834get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000835{
836 if (expr_line == NULL)
837 return NULL;
838 return vim_strsave(expr_line);
839}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif /* FEAT_EVAL */
841
842/*
843 * Check if 'regname' is a valid name of a yank register.
844 * Note: There is no check for 0 (default register), caller should do this
845 */
846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100847valid_yank_reg(
848 int regname,
849 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 if ( (regname > 0 && ASCII_ISALNUM(regname))
852 || (!writing && vim_strchr((char_u *)
853#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100854 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100856 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100859 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 || regname == '"'
861 || regname == '-'
862 || regname == '_'
863#ifdef FEAT_CLIPBOARD
864 || regname == '*'
865 || regname == '+'
866#endif
867#ifdef FEAT_DND
868 || (!writing && regname == '~')
869#endif
870 )
871 return TRUE;
872 return FALSE;
873}
874
875/*
876 * Set y_current and y_append, according to the value of "regname".
877 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000878 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 *
880 * If regname is 0 and writing, use register 0
881 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100882 *
883 * Return TRUE when the register should be inserted literally (selection or
884 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100886 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100887get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888{
889 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100890 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 y_append = FALSE;
893 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
894 {
895 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100896 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 i = regname;
899 if (VIM_ISDIGIT(i))
900 i -= '0';
901 else if (ASCII_ISLOWER(i))
902 i = CharOrdLow(i) + 10;
903 else if (ASCII_ISUPPER(i))
904 {
905 i = CharOrdUp(i) + 10;
906 y_append = TRUE;
907 }
908 else if (regname == '-')
909 i = DELETION_REGISTER;
910#ifdef FEAT_CLIPBOARD
911 /* When selection is not available, use register 0 instead of '*' */
912 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100915 ret = TRUE;
916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 /* When clipboard is not available, use register 0 instead of '+' */
918 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100921 ret = TRUE;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#endif
924#ifdef FEAT_DND
925 else if (!writing && regname == '~')
926 i = TILDE_REGISTER;
927#endif
928 else /* not 0-9, a-z, A-Z or '-': use register 0 */
929 i = 0;
930 y_current = &(y_regs[i]);
931 if (writing) /* remember the register we write into for do_put() */
932 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100933 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934}
935
Bram Moolenaar8299df92004-07-10 09:47:34 +0000936#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937/*
938 * When "regname" is a clipboard register, obtain the selection. If it's not
939 * available return zero, otherwise return "regname".
940 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000941 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100942may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 if (regname == '*')
945 {
946 if (!clip_star.available)
947 regname = 0;
948 else
949 clip_get_selection(&clip_star);
950 }
951 else if (regname == '+')
952 {
953 if (!clip_plus.available)
954 regname = 0;
955 else
956 clip_get_selection(&clip_plus);
957 }
958 return regname;
959}
960#endif
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
963 * Obtain the contents of a "normal" register. The register is made empty.
964 * The returned pointer has allocated memory, use put_register() later.
965 */
966 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100967get_register(
968 int name,
969 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200971 yankreg_T *reg;
972 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974#ifdef FEAT_CLIPBOARD
975 /* When Visual area changed, may have to update selection. Obtain the
976 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000977 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200979 if (clip_isautosel_star())
980 clip_update_selection(&clip_star);
981 may_get_selection(name);
982 }
983 if (name == '+' && clip_plus.available)
984 {
985 if (clip_isautosel_plus())
986 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 may_get_selection(name);
988 }
989#endif
990
991 get_yank_register(name, 0);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200992 reg = ALLOC_ONE(yankreg_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (reg != NULL)
994 {
995 *reg = *y_current;
996 if (copy)
997 {
998 /* If we run out of memory some or all of the lines are empty. */
999 if (reg->y_size == 0)
1000 reg->y_array = NULL;
1001 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001002 reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (reg->y_array != NULL)
1004 {
1005 for (i = 0; i < reg->y_size; ++i)
1006 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1007 }
1008 }
1009 else
1010 y_current->y_array = NULL;
1011 }
1012 return (void *)reg;
1013}
1014
1015/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001016 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
1018 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001019put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 get_yank_register(name, 0);
1022 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001023 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001024 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001026#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /* Send text written to clipboard register to the clipboard. */
1028 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001031
Bram Moolenaar113e1072019-01-20 15:30:40 +01001032#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1033 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001035free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001036{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001037 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001038
1039 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001040 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001041 free_yank_all();
1042 vim_free(reg);
1043 *y_current = tmp;
1044}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047#if defined(FEAT_MOUSE) || defined(PROTO)
1048/*
1049 * return TRUE if the current yank register has type MLINE
1050 */
1051 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001052yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1055 return FALSE;
1056 if (regname == '_') /* black hole is always empty */
1057 return FALSE;
1058 get_yank_register(regname, FALSE);
1059 return (y_current->y_type == MLINE);
1060}
1061#endif
1062
1063/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001064 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001066 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 */
1068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001069do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaard55de222007-05-06 13:38:48 +00001071 char_u *p;
1072 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001073 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001074 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001076 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001078 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1080 retval = FAIL;
1081 else
1082 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001083 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 showmode();
1085 regname = c;
1086 retval = OK;
1087 }
1088 }
1089 else /* stop recording */
1090 {
1091 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001092 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1093 * needs to be removed again to put it in a register. exec_reg then
1094 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001096 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 p = get_recorded();
1099 if (p == NULL)
1100 retval = FAIL;
1101 else
1102 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001103 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1104 vim_unescape_csi(p);
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * We don't want to change the default register here, so save and
1108 * restore the current register name.
1109 */
1110 old_y_previous = y_previous;
1111 old_y_current = y_current;
1112
1113 retval = stuff_yank(regname, p);
1114
1115 y_previous = old_y_previous;
1116 y_current = old_y_current;
1117 }
1118 }
1119 return retval;
1120}
1121
1122/*
1123 * Stuff string "p" into yank register "regname" as a single line (append if
1124 * uppercase). "p" must have been alloced.
1125 *
1126 * return FAIL for failure, OK otherwise
1127 */
1128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001129stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 char_u *lp;
1132 char_u **pp;
1133
1134 /* check for read-only register */
1135 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1136 {
1137 vim_free(p);
1138 return FAIL;
1139 }
1140 if (regname == '_') /* black hole: don't do anything */
1141 {
1142 vim_free(p);
1143 return OK;
1144 }
1145 get_yank_register(regname, TRUE);
1146 if (y_append && y_current->y_array != NULL)
1147 {
1148 pp = &(y_current->y_array[y_current->y_size - 1]);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001149 lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (lp == NULL)
1151 {
1152 vim_free(p);
1153 return FAIL;
1154 }
1155 STRCPY(lp, *pp);
1156 STRCAT(lp, p);
1157 vim_free(p);
1158 vim_free(*pp);
1159 *pp = lp;
1160 }
1161 else
1162 {
1163 free_yank_all();
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001164 if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 vim_free(p);
1167 return FAIL;
1168 }
1169 y_current->y_array[0] = p;
1170 y_current->y_size = 1;
1171 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001172#ifdef FEAT_VIMINFO
1173 y_current->y_time_set = vim_time();
1174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 return OK;
1177}
1178
Bram Moolenaar42b94362009-05-26 16:12:37 +00001179static int execreg_lastc = NUL;
1180
Bram Moolenaarc3328162019-07-23 22:15:25 +02001181 int
1182get_execreg_lastc(void)
1183{
1184 return execreg_lastc;
1185}
1186
1187 void
1188set_execreg_lastc(int lastc)
1189{
1190 execreg_lastc = lastc;
1191}
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001194 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001196 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
1198 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001199do_execreg(
1200 int regname,
1201 int colon, /* insert ':' before each line */
1202 int addcr, /* always add '\n' to end of line */
1203 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 long i;
1206 char_u *p;
1207 int retval = OK;
1208 int remap;
1209
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001210 // repeat previous one
1211 if (regname == '@')
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001212 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001213 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001216 return FAIL;
1217 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001218 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001219 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001220 // check for valid regname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001222 {
1223 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001225 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001226 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228#ifdef FEAT_CLIPBOARD
1229 regname = may_get_selection(regname);
1230#endif
1231
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001232 // black hole: don't stuff anything
1233 if (regname == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 return OK;
1235
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001236 // use last command line
1237 if (regname == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (last_cmdline == NULL)
1240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001241 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return FAIL;
1243 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001244 // don't keep the cmdline containing @:
1245 VIM_CLEAR(new_last_cmdline);
1246 // Escape all control characters with a CTRL-V
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001247 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001248 (char_u *)"\001\002\003\004\005\006\007"
1249 "\010\011\012\013\014\015\016\017"
1250 "\020\021\022\023\024\025\026\027"
1251 "\030\031\032\033\034\035\036\037",
1252 Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001253 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001254 {
1255 /* When in Visual mode "'<,'>" will be prepended to the command.
1256 * Remove it when it's already there. */
1257 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001258 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001259 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001260 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001261 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001262 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#ifdef FEAT_EVAL
1265 else if (regname == '=')
1266 {
1267 p = get_expr_line();
1268 if (p == NULL)
1269 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001270 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 vim_free(p);
1272 }
1273#endif
1274 else if (regname == '.') /* use last inserted text */
1275 {
1276 p = get_last_insert_save();
1277 if (p == NULL)
1278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001279 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 return FAIL;
1281 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 vim_free(p);
1284 }
1285 else
1286 {
1287 get_yank_register(regname, FALSE);
1288 if (y_current->y_array == NULL)
1289 return FAIL;
1290
1291 /* Disallow remaping for ":@r". */
1292 remap = colon ? REMAP_NONE : REMAP_YES;
1293
1294 /*
1295 * Insert lines into typeahead buffer, from last one to first one.
1296 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001297 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 for (i = y_current->y_size; --i >= 0; )
1299 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001300 char_u *escaped;
1301
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 /* insert NL between lines and after last line if type is MLINE */
1303 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1304 || addcr)
1305 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001306 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 return FAIL;
1308 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001309 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1310 if (escaped == NULL)
1311 return FAIL;
1312 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1313 vim_free(escaped);
1314 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001316 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 == FAIL)
1318 return FAIL;
1319 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001320 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 return retval;
1323}
1324
1325/*
1326 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1327 * used only after other typeahead has been processed.
1328 */
1329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001330put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 char_u buf[3];
1333
1334 if (restart_edit != NUL)
1335 {
1336 if (restart_edit == 'V')
1337 {
1338 buf[0] = 'g';
1339 buf[1] = 'R';
1340 buf[2] = NUL;
1341 }
1342 else
1343 {
1344 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1345 buf[1] = NUL;
1346 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001347 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 restart_edit = NUL;
1349 }
1350}
1351
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001352/*
1353 * Insert register contents "s" into the typeahead buffer, so that it will be
1354 * executed again.
1355 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1356 * no remapping.
1357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001359put_in_typebuf(
1360 char_u *s,
1361 int esc,
1362 int colon, /* add ':' before the line */
1363 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 int retval = OK;
1366
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001367 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001369 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001371 {
1372 char_u *p;
1373
1374 if (esc)
1375 p = vim_strsave_escape_csi(s);
1376 else
1377 p = s;
1378 if (p == NULL)
1379 retval = FAIL;
1380 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001381 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1382 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001383 if (esc)
1384 vim_free(p);
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001387 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 return retval;
1389}
1390
1391/*
1392 * Insert a yank register: copy it into the Read buffer.
1393 * Used by CTRL-R command and middle mouse button in insert mode.
1394 *
1395 * return FAIL for failure, OK otherwise
1396 */
1397 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001398insert_reg(
1399 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001400 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 long i;
1403 int retval = OK;
1404 char_u *arg;
1405 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001406 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 /*
1409 * It is possible to get into an endless loop by having CTRL-R a in
1410 * register a and then, in insert mode, doing CTRL-R a.
1411 * If you hit CTRL-C, the loop will be broken here.
1412 */
1413 ui_breakcheck();
1414 if (got_int)
1415 return FAIL;
1416
1417 /* check for valid regname */
1418 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1419 return FAIL;
1420
1421#ifdef FEAT_CLIPBOARD
1422 regname = may_get_selection(regname);
1423#endif
1424
1425 if (regname == '.') /* insert last inserted text */
1426 retval = stuff_inserted(NUL, 1L, TRUE);
1427 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1428 {
1429 if (arg == NULL)
1430 return FAIL;
1431 stuffescaped(arg, literally);
1432 if (allocated)
1433 vim_free(arg);
1434 }
1435 else /* name or number register */
1436 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001437 if (get_yank_register(regname, FALSE))
1438 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (y_current->y_array == NULL)
1440 retval = FAIL;
1441 else
1442 {
1443 for (i = 0; i < y_current->y_size; ++i)
1444 {
1445 stuffescaped(y_current->y_array[i], literally);
1446 /*
1447 * Insert a newline between lines and after last line if
1448 * y_type is MLINE.
1449 */
1450 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1451 stuffcharReadbuff('\n');
1452 }
1453 }
1454 }
1455
1456 return retval;
1457}
1458
1459/*
1460 * Stuff a string into the typeahead buffer, such that edit() will insert it
1461 * literally ("literally" TRUE) or interpret is as typed characters.
1462 */
1463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001464stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 int c;
1467 char_u *start;
1468
1469 while (*arg != NUL)
1470 {
1471 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1472 * stuff K_SPECIAL to get the effect of a special key when "literally"
1473 * is TRUE. */
1474 start = arg;
1475 while ((*arg >= ' '
1476#ifndef EBCDIC
1477 && *arg < DEL /* EBCDIC: chars above space are normal */
1478#endif
1479 )
1480 || (*arg == K_SPECIAL && !literally))
1481 ++arg;
1482 if (arg > start)
1483 stuffReadbuffLen(start, (long)(arg - start));
1484
1485 /* stuff a single special character */
1486 if (*arg != NUL)
1487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001489 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 c = *arg++;
1492 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1493 stuffcharReadbuff(Ctrl_V);
1494 stuffcharReadbuff(c);
1495 }
1496 }
1497}
1498
1499/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001500 * If "regname" is a special register, return TRUE and store a pointer to its
1501 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001503 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504get_spec_reg(
1505 int regname,
1506 char_u **argp,
1507 int *allocated, /* return: TRUE when value was allocated */
1508 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int cnt;
1511
1512 *argp = NULL;
1513 *allocated = FALSE;
1514 switch (regname)
1515 {
1516 case '%': /* file name */
1517 if (errmsg)
1518 check_fname(); /* will give emsg if not set */
1519 *argp = curbuf->b_fname;
1520 return TRUE;
1521
1522 case '#': /* alternate file name */
1523 *argp = getaltfname(errmsg); /* may give emsg if not set */
1524 return TRUE;
1525
1526#ifdef FEAT_EVAL
1527 case '=': /* result of expression */
1528 *argp = get_expr_line();
1529 *allocated = TRUE;
1530 return TRUE;
1531#endif
1532
1533 case ':': /* last command line */
1534 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001535 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 *argp = last_cmdline;
1537 return TRUE;
1538
1539 case '/': /* last search-pattern */
1540 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001541 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 *argp = last_search_pat();
1543 return TRUE;
1544
1545 case '.': /* last inserted text */
1546 *argp = get_last_insert_save();
1547 *allocated = TRUE;
1548 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 return TRUE;
1551
1552#ifdef FEAT_SEARCHPATH
1553 case Ctrl_F: /* Filename under cursor */
1554 case Ctrl_P: /* Path under cursor, expand via "path" */
1555 if (!errmsg)
1556 return FALSE;
1557 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001558 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 *allocated = TRUE;
1560 return TRUE;
1561#endif
1562
1563 case Ctrl_W: /* word under cursor */
1564 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1565 if (!errmsg)
1566 return FALSE;
1567 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1568 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1569 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1570 *allocated = TRUE;
1571 return TRUE;
1572
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001573 case Ctrl_L: /* Line under cursor */
1574 if (!errmsg)
1575 return FALSE;
1576
1577 *argp = ml_get_buf(curwin->w_buffer,
1578 curwin->w_cursor.lnum, FALSE);
1579 return TRUE;
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 case '_': /* black hole: always empty */
1582 *argp = (char_u *)"";
1583 return TRUE;
1584 }
1585
1586 return FALSE;
1587}
1588
1589/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001590 * Paste a yank register into the command line.
1591 * Only for non-special registers.
1592 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 * insert_reg() can't be used here, because special characters from the
1594 * register contents will be interpreted as commands.
1595 *
1596 * return FAIL for failure, OK otherwise
1597 */
1598 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001599cmdline_paste_reg(
1600 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001601 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001605 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001607 if (get_yank_register(regname, FALSE))
1608 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (y_current->y_array == NULL)
1610 return FAIL;
1611
1612 for (i = 0; i < y_current->y_size; ++i)
1613 {
1614 cmdline_paste_str(y_current->y_array[i], literally);
1615
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001616 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001617 * Don't do this when "remcr" is TRUE. */
1618 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 cmdline_paste_str((char_u *)"\r", literally);
1620
1621 /* Check for CTRL-C, in case someone tries to paste a few thousand
1622 * lines and gets bored. */
1623 ui_breakcheck();
1624 if (got_int)
1625 return FAIL;
1626 }
1627 return OK;
1628}
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1631/*
1632 * Adjust the register name pointed to with "rp" for the clipboard being
1633 * used always and the clipboard being available.
1634 */
1635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001636adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001638 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1639 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001640 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1641 {
1642 if (clip_unnamed != 0)
1643 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001644 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001645 else
1646 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1647 ? '+' : '*';
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (!clip_star.available && *rp == '*')
1650 *rp = 0;
1651 if (!clip_plus.available && *rp == '+')
1652 *rp = 0;
1653}
1654#endif
1655
1656/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001657 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1658 */
1659 void
1660shift_delete_registers()
1661{
1662 int n;
1663
1664 y_current = &y_regs[9];
1665 free_yank_all(); /* free register nine */
1666 for (n = 9; n > 1; --n)
1667 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001668 y_current = &y_regs[1];
1669 if (!y_append)
1670 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001671 y_regs[1].y_array = NULL; /* set register one to empty */
1672}
1673
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001675 static void
1676yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1677{
1678 static int recursive = FALSE;
1679 dict_T *v_event;
1680 list_T *list;
1681 int n;
1682 char_u buf[NUMBUFLEN + 2];
1683 long reglen = 0;
1684
1685 if (recursive)
1686 return;
1687
1688 v_event = get_vim_var_dict(VV_EVENT);
1689
1690 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001691 if (list == NULL)
1692 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001693 for (n = 0; n < reg->y_size; n++)
1694 list_append_string(list, reg->y_array[n], -1);
1695 list->lv_lock = VAR_FIXED;
1696 dict_add_list(v_event, "regcontents", list);
1697
1698 buf[0] = (char_u)oap->regname;
1699 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001700 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001701
1702 buf[0] = get_op_char(oap->op_type);
1703 buf[1] = get_extra_op_char(oap->op_type);
1704 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001705 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001706
1707 buf[0] = NUL;
1708 buf[1] = NUL;
1709 switch (get_reg_type(oap->regname, &reglen))
1710 {
1711 case MLINE: buf[0] = 'V'; break;
1712 case MCHAR: buf[0] = 'v'; break;
1713 case MBLOCK:
1714 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1715 reglen + 1);
1716 break;
1717 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001718 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001719
1720 /* Lock the dictionary and its keys */
1721 dict_set_items_ro(v_event);
1722
1723 recursive = TRUE;
1724 textlock++;
1725 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1726 textlock--;
1727 recursive = FALSE;
1728
1729 /* Empty the dictionary, v:event is still valid */
1730 dict_free_contents(v_event);
1731 hash_init(&v_event->dv_hashtab);
1732}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001733#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001734
Bram Moolenaara1891842017-02-04 21:34:31 +01001735/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001736 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001738 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 */
1740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001741op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 int n;
1744 linenr_T lnum;
1745 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 char_u *newp, *oldp;
1747 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1749 int did_yank = FALSE;
1750
1751 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1752 return OK;
1753
1754 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1755 if (oap->empty)
1756 return u_save_cursor();
1757
1758 if (!curbuf->b_p_ma)
1759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001760 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 return FAIL;
1762 }
1763
1764#ifdef FEAT_CLIPBOARD
1765 adjust_clip_reg(&oap->regname);
1766#endif
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (has_mbyte)
1769 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaard04b7502010-07-08 22:27:55 +02001771 /*
1772 * Imitate the strange Vi behaviour: If the delete spans more than one
1773 * line and motion_type == MCHAR and the result is a blank line, make the
1774 * delete linewise. Don't do this for the change command or Visual mode.
1775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001778 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001780 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 && oap->op_type == OP_DELETE)
1782 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001783 ptr = ml_get(oap->end.lnum) + oap->end.col;
1784 if (*ptr != NUL)
1785 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 ptr = skipwhite(ptr);
1787 if (*ptr == NUL && inindent(0))
1788 oap->motion_type = MLINE;
1789 }
1790
Bram Moolenaard04b7502010-07-08 22:27:55 +02001791 /*
1792 * Check for trying to delete (e.g. "D") in an empty line.
1793 * Note: For the change operator it is ok.
1794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if ( oap->motion_type == MCHAR
1796 && oap->line_count == 1
1797 && oap->op_type == OP_DELETE
1798 && *ml_get(oap->start.lnum) == NUL)
1799 {
1800 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001801 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 * 'cpoptions' (Vi compatible).
1803 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001804 if (virtual_op)
1805 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1806 * marks as if it happened. */
1807 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1809 beep_flush();
1810 return OK;
1811 }
1812
Bram Moolenaard04b7502010-07-08 22:27:55 +02001813 /*
1814 * Do a yank of whatever we're about to delete.
1815 * If a yank register was specified, put the deleted text into that
1816 * register. For the black hole register '_' don't yank anything.
1817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (oap->regname != '_')
1819 {
1820 if (oap->regname != 0)
1821 {
1822 /* check for read-only register */
1823 if (!valid_yank_reg(oap->regname, TRUE))
1824 {
1825 beep_flush();
1826 return OK;
1827 }
1828 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1829 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1830 did_yank = TRUE;
1831 }
1832
1833 /*
1834 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001835 * delete contains a line break, or when using a specific operator (Vi
1836 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +02001837 * Use the register name from before adjust_clip_reg() may have
1838 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001840 if (oap->motion_type == MLINE || oap->line_count > 1
1841 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001843 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (op_yank(oap, TRUE, FALSE) == OK)
1845 did_yank = TRUE;
1846 }
1847
Bram Moolenaar84298db2012-04-20 13:46:08 +02001848 /* Yank into small delete register when no named register specified
1849 * and the delete is within one line. */
1850 if ((
1851#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001852 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1853 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001854#endif
1855 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 && oap->line_count == 1)
1857 {
1858 oap->regname = '-';
1859 get_yank_register(oap->regname, TRUE);
1860 if (op_yank(oap, TRUE, FALSE) == OK)
1861 did_yank = TRUE;
1862 oap->regname = 0;
1863 }
1864
1865 /*
1866 * If there's too much stuff to fit in the yank register, then get a
1867 * confirmation before doing the delete. This is crude, but simple.
1868 * And it avoids doing a delete of something we can't put back if we
1869 * want.
1870 */
1871 if (!did_yank)
1872 {
1873 int msg_silent_save = msg_silent;
1874
1875 msg_silent = 0; /* must display the prompt */
1876 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1877 msg_silent = msg_silent_save;
1878 if (n != 'y')
1879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001880 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 return FAIL;
1882 }
1883 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001884
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001885#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001886 if (did_yank && has_textyankpost())
1887 yank_do_autocmd(oap, y_current);
1888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890
Bram Moolenaard04b7502010-07-08 22:27:55 +02001891 /*
1892 * block mode delete
1893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (oap->block_mode)
1895 {
1896 if (u_save((linenr_T)(oap->start.lnum - 1),
1897 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1898 return FAIL;
1899
1900 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1901 {
1902 block_prep(oap, &bd, lnum, TRUE);
1903 if (bd.textlen == 0) /* nothing to delete */
1904 continue;
1905
1906 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1907 if (lnum == curwin->w_cursor.lnum)
1908 {
1909 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912
Bram Moolenaar8055d172019-05-17 22:57:26 +02001913 // "n" == number of chars deleted
1914 // If we delete a TAB, it may be replaced by several characters.
1915 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 n = bd.textlen - bd.startspaces - bd.endspaces;
1917 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001918 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (newp == NULL)
1920 continue;
1921 /* copy up to deleted part */
1922 mch_memmove(newp, oldp, (size_t)bd.textcol);
1923 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001924 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 (size_t)(bd.startspaces + bd.endspaces));
1926 /* copy the part after the deleted part */
1927 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001928 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 /* replace the line */
1930 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001931
1932#ifdef FEAT_TEXT_PROP
1933 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001934 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937
1938 check_cursor_col();
1939 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1940 oap->end.lnum + 1, 0L);
1941 oap->line_count = 0; /* no lines deleted */
1942 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001943 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 if (oap->op_type == OP_CHANGE)
1946 {
1947 /* Delete the lines except the first one. Temporarily move the
1948 * cursor to the next line. Save the current line number, if the
1949 * last line is deleted it may be changed.
1950 */
1951 if (oap->line_count > 1)
1952 {
1953 lnum = curwin->w_cursor.lnum;
1954 ++curwin->w_cursor.lnum;
1955 del_lines((long)(oap->line_count - 1), TRUE);
1956 curwin->w_cursor.lnum = lnum;
1957 }
1958 if (u_save_cursor() == FAIL)
1959 return FAIL;
1960 if (curbuf->b_p_ai) /* don't delete indent */
1961 {
1962 beginline(BL_WHITE); /* cursor on first non-white */
1963 did_ai = TRUE; /* delete the indent when ESC hit */
1964 ai_col = curwin->w_cursor.col;
1965 }
1966 else
1967 beginline(0); /* cursor in column 0 */
1968 truncate_line(FALSE); /* delete the rest of the line */
1969 /* leave cursor past last char in line */
1970 if (oap->line_count > 1)
1971 u_clearline(); /* "U" command not possible after "2cc" */
1972 }
1973 else
1974 {
1975 del_lines(oap->line_count, TRUE);
1976 beginline(BL_WHITE | BL_FIX);
1977 u_clearline(); /* "U" command not possible after "dd" */
1978 }
1979 }
1980 else
1981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (virtual_op)
1983 {
1984 int endcol = 0;
1985
1986 /* For virtualedit: break the tabs that are partly included. */
1987 if (gchar_pos(&oap->start) == '\t')
1988 {
1989 if (u_save_cursor() == FAIL) /* save first line for undo */
1990 return FAIL;
1991 if (oap->line_count == 1)
1992 endcol = getviscol2(oap->end.col, oap->end.coladd);
1993 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1994 oap->start = curwin->w_cursor;
1995 if (oap->line_count == 1)
1996 {
1997 coladvance(endcol);
1998 oap->end.col = curwin->w_cursor.col;
1999 oap->end.coladd = curwin->w_cursor.coladd;
2000 curwin->w_cursor = oap->start;
2001 }
2002 }
2003
2004 /* Break a tab only when it's included in the area. */
2005 if (gchar_pos(&oap->end) == '\t'
2006 && (int)oap->end.coladd < oap->inclusive)
2007 {
2008 /* save last line for undo */
2009 if (u_save((linenr_T)(oap->end.lnum - 1),
2010 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2011 return FAIL;
2012 curwin->w_cursor = oap->end;
2013 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2014 oap->end = curwin->w_cursor;
2015 curwin->w_cursor = oap->start;
2016 }
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019 if (oap->line_count == 1) /* delete characters within one line */
2020 {
2021 if (u_save_cursor() == FAIL) /* save line for undo */
2022 return FAIL;
2023
2024 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002025 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 && oap->op_type == OP_CHANGE
2027 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002028 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 display_dollar(oap->end.col - !oap->inclusive);
2030
2031 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (virtual_op)
2034 {
2035 /* fix up things for virtualedit-delete:
2036 * break the tabs which are going to get in our way
2037 */
2038 char_u *curline = ml_get_curline();
2039 int len = (int)STRLEN(curline);
2040
2041 if (oap->end.coladd != 0
2042 && (int)oap->end.col >= len - 1
2043 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2044 n++;
2045 /* Delete at least one char (e.g, when on a control char). */
2046 if (n == 0 && oap->start.coladd != oap->end.coladd)
2047 n = 1;
2048
2049 /* When deleted a char in the line, reset coladd. */
2050 if (gchar_cursor() != NUL)
2051 curwin->w_cursor.coladd = 0;
2052 }
Bram Moolenaard009e862015-06-09 20:20:03 +02002053 (void)del_bytes((long)n, !virtual_op,
2054 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 else /* delete characters between lines */
2057 {
2058 pos_T curpos;
2059
2060 /* save deleted and changed lines for undo */
2061 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2062 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2063 return FAIL;
2064
2065 truncate_line(TRUE); /* delete from cursor to end of line */
2066
2067 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2068 ++curwin->w_cursor.lnum;
2069 del_lines((long)(oap->line_count - 2), FALSE);
2070
Bram Moolenaard009e862015-06-09 20:20:03 +02002071 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002072 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002073 curwin->w_cursor.col = 0;
2074 (void)del_bytes((long)n, !virtual_op,
2075 oap->op_type == OP_DELETE && !oap->is_VIsual);
2076 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2077 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 }
2080
2081 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2082
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002083setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (oap->block_mode)
2085 {
2086 curbuf->b_op_end.lnum = oap->end.lnum;
2087 curbuf->b_op_end.col = oap->start.col;
2088 }
2089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 curbuf->b_op_end = oap->start;
2091 curbuf->b_op_start = oap->start;
2092
2093 return OK;
2094}
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096/*
2097 * Adjust end of operating area for ending on a multi-byte character.
2098 * Used for deletion.
2099 */
2100 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002101mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 char_u *p;
2104
2105 if (oap->inclusive)
2106 {
2107 p = ml_get(oap->end.lnum);
2108 oap->end.col += mb_tail_off(p, p + oap->end.col);
2109 }
2110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaar630afe82018-06-28 19:26:28 +02002112/*
2113 * Replace the character under the cursor with "c".
2114 * This takes care of multi-byte characters.
2115 */
2116 static void
2117replace_character(int c)
2118{
2119 int n = State;
2120
2121 State = REPLACE;
2122 ins_char(c);
2123 State = n;
2124 /* Backup to the replaced character. */
2125 dec_cursor();
2126}
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128/*
2129 * Replace a whole area with one character.
2130 */
2131 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002132op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 char_u *newp, *oldp;
2137 size_t oldlen;
2138 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002139 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002140 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2143 return OK; /* nothing to do */
2144
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002145 if (c == REPLACE_CR_NCHAR)
2146 {
2147 had_ctrl_v_cr = TRUE;
2148 c = CAR;
2149 }
2150 else if (c == REPLACE_NL_NCHAR)
2151 {
2152 had_ctrl_v_cr = TRUE;
2153 c = NL;
2154 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (has_mbyte)
2157 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159 if (u_save((linenr_T)(oap->start.lnum - 1),
2160 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2161 return FAIL;
2162
2163 /*
2164 * block mode replace
2165 */
2166 if (oap->block_mode)
2167 {
2168 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2169 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2170 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002171 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2173 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2174 continue; /* nothing to replace */
2175
2176 /* n == number of extra chars required
2177 * If we split a TAB, it may be replaced by several characters.
2178 * Thus the number of characters may increase!
2179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 /* If the range starts in virtual space, count the initial
2181 * coladd offset as part of "startspaces" */
2182 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2183 {
2184 pos_T vpos;
2185
Bram Moolenaara1381de2009-11-03 15:44:21 +00002186 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 getvpos(&vpos, oap->start_vcol);
2188 bd.startspaces += vpos.coladd;
2189 n = bd.startspaces;
2190 }
2191 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 /* allow for pre spaces */
2193 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2194
2195 /* allow for post spp */
2196 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2199 /* Figure out how many characters to replace. */
2200 numc = oap->end_vcol - oap->start_vcol + 1;
2201 if (bd.is_short && (!virtual_op || bd.is_MAX))
2202 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 /* A double-wide character can be replaced only up to half the
2205 * times. */
2206 if ((*mb_char2cells)(c) > 1)
2207 {
2208 if ((numc & 1) && !bd.is_short)
2209 {
2210 ++bd.endspaces;
2211 ++n;
2212 }
2213 numc = numc / 2;
2214 }
2215
2216 /* Compute bytes needed, move character count to num_chars. */
2217 num_chars = numc;
2218 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 /* oldlen includes textlen, so don't double count */
2220 n += numc - bd.textlen;
2221
2222 oldp = ml_get_curline();
2223 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002224 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (newp == NULL)
2226 continue;
2227 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2228 /* copy up to deleted part */
2229 mch_memmove(newp, oldp, (size_t)bd.textcol);
2230 oldp += bd.textcol + bd.textlen;
2231 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002232 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002234 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2235 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002236 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002238 if (has_mbyte)
2239 {
2240 n = (int)STRLEN(newp);
2241 while (--num_chars >= 0)
2242 n += (*mb_char2bytes)(c, newp + n);
2243 }
2244 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002245 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002246 if (!bd.is_short)
2247 {
2248 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002249 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002250 /* copy the part after the changed part */
2251 STRMOVE(newp + STRLEN(newp), oldp);
2252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002256 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002257 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01002258 if (after_p != NULL)
2259 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 /* replace the line */
2262 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002263 if (after_p != NULL)
2264 {
2265 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2266 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2267 oap->end.lnum++;
2268 vim_free(after_p);
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 }
2272 else
2273 {
2274 /*
2275 * MCHAR and MLINE motion replace.
2276 */
2277 if (oap->motion_type == MLINE)
2278 {
2279 oap->start.col = 0;
2280 curwin->w_cursor.col = 0;
2281 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2282 if (oap->end.col)
2283 --oap->end.col;
2284 }
2285 else if (!oap->inclusive)
2286 dec(&(oap->end));
2287
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002288 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 n = gchar_cursor();
2291 if (n != NUL)
2292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2294 {
2295 /* This is slow, but it handles replacing a single-byte
2296 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002297 if (curwin->w_cursor.lnum == oap->end.lnum)
2298 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002299 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (n == TAB)
2304 {
2305 int end_vcol = 0;
2306
2307 if (curwin->w_cursor.lnum == oap->end.lnum)
2308 {
2309 /* oap->end has to be recalculated when
2310 * the tab breaks */
2311 end_vcol = getviscol2(oap->end.col,
2312 oap->end.coladd);
2313 }
2314 coladvance_force(getviscol());
2315 if (curwin->w_cursor.lnum == oap->end.lnum)
2316 getvpos(&oap->end, end_vcol);
2317 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02002318 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2322 {
2323 int virtcols = oap->end.coladd;
2324
2325 if (curwin->w_cursor.lnum == oap->start.lnum
2326 && oap->start.col == oap->end.col && oap->start.coladd)
2327 virtcols -= oap->start.coladd;
2328
2329 /* oap->end has been trimmed so it's effectively inclusive;
2330 * as a result an extra +1 must be counted so we don't
2331 * trample the NUL byte. */
2332 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2333 curwin->w_cursor.col -= (virtcols + 1);
2334 for (; virtcols >= 0; virtcols--)
2335 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002336 if ((*mb_char2len)(c) > 1)
2337 replace_character(c);
2338 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002339 PBYTE(curwin->w_cursor, c);
2340 if (inc(&curwin->w_cursor) == -1)
2341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 /* Advance to next character, stop at the end of the file. */
2346 if (inc_cursor() == -1)
2347 break;
2348 }
2349 }
2350
2351 curwin->w_cursor = oap->start;
2352 check_cursor();
2353 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2354
2355 /* Set "'[" and "']" marks. */
2356 curbuf->b_op_start = oap->start;
2357 curbuf->b_op_end = oap->end;
2358
2359 return OK;
2360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002362static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364/*
2365 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2366 */
2367 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002368op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369{
2370 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002372 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 if (u_save((linenr_T)(oap->start.lnum - 1),
2375 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2376 return;
2377
2378 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (oap->block_mode) /* Visual block mode */
2380 {
2381 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2382 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002383 int one_change;
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 block_prep(oap, &bd, pos.lnum, FALSE);
2386 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002387 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2388 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002389
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002390#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002391 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
2393 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2394
Bram Moolenaar009b2592004-10-24 19:18:58 +00002395 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2396 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002398 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 if (did_change)
2403 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2404 }
2405 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 if (oap->motion_type == MLINE)
2408 {
2409 oap->start.col = 0;
2410 pos.col = 0;
2411 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2412 if (oap->end.col)
2413 --oap->end.col;
2414 }
2415 else if (!oap->inclusive)
2416 dec(&(oap->end));
2417
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002418 if (pos.lnum == oap->end.lnum)
2419 did_change = swapchars(oap->op_type, &pos,
2420 oap->end.col - pos.col + 1);
2421 else
2422 for (;;)
2423 {
2424 did_change |= swapchars(oap->op_type, &pos,
2425 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2426 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002427 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002428 break;
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (did_change)
2431 {
2432 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2433 0L);
2434#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002435 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 char_u *ptr;
2438 int count;
2439
2440 pos = oap->start;
2441 while (pos.lnum < oap->end.lnum)
2442 {
2443 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002444 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002445 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002447 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 pos.col = 0;
2449 pos.lnum++;
2450 }
2451 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2452 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002453 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002455 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457#endif
2458 }
2459 }
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (!did_change && oap->is_VIsual)
2462 /* No change: need to remove the Visual selection */
2463 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 /*
2466 * Set '[ and '] marks.
2467 */
2468 curbuf->b_op_start = oap->start;
2469 curbuf->b_op_end = oap->end;
2470
2471 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002472 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002473 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474}
2475
2476/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002477 * Invoke swapchar() on "length" bytes at position "pos".
2478 * "pos" is advanced to just after the changed characters.
2479 * "length" is rounded up to include the whole last multi-byte character.
2480 * Also works correctly when the number of bytes changes.
2481 * Returns TRUE if some character was changed.
2482 */
2483 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002484swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002485{
2486 int todo;
2487 int did_change = 0;
2488
2489 for (todo = length; todo > 0; --todo)
2490 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002491 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002492 {
2493 int len = (*mb_ptr2len)(ml_get_pos(pos));
2494
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002495 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002496 if (len > 0)
2497 todo -= len - 1;
2498 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002499 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002500 if (inc(pos) == -1) /* at end of file */
2501 break;
2502 }
2503 return did_change;
2504}
2505
2506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 * If op_type == OP_UPPER: make uppercase,
2508 * if op_type == OP_LOWER: make lowercase,
2509 * if op_type == OP_ROT13: do rot13 encoding,
2510 * else swap case of character at 'pos'
2511 * returns TRUE when something actually changed.
2512 */
2513 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002514swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 int c;
2517 int nc;
2518
2519 c = gchar_pos(pos);
2520
2521 /* Only do rot13 encoding for ASCII characters. */
2522 if (c >= 0x80 && op_type == OP_ROT13)
2523 return FALSE;
2524
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002525 if (op_type == OP_UPPER && c == 0xdf
2526 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002527 {
2528 pos_T sp = curwin->w_cursor;
2529
2530 /* Special handling of German sharp s: change to "SS". */
2531 curwin->w_cursor = *pos;
2532 del_char(FALSE);
2533 ins_char('S');
2534 ins_char('S');
2535 curwin->w_cursor = sp;
2536 inc(pos);
2537 }
2538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2540 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 nc = c;
2542 if (MB_ISLOWER(c))
2543 {
2544 if (op_type == OP_ROT13)
2545 nc = ROT13(c, 'a');
2546 else if (op_type != OP_LOWER)
2547 nc = MB_TOUPPER(c);
2548 }
2549 else if (MB_ISUPPER(c))
2550 {
2551 if (op_type == OP_ROT13)
2552 nc = ROT13(c, 'A');
2553 else if (op_type != OP_UPPER)
2554 nc = MB_TOLOWER(c);
2555 }
2556 if (nc != c)
2557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2559 {
2560 pos_T sp = curwin->w_cursor;
2561
2562 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002563 /* don't use del_char(), it also removes composing chars */
2564 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 ins_char(nc);
2566 curwin->w_cursor = sp;
2567 }
2568 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002569 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return TRUE;
2571 }
2572 return FALSE;
2573}
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * op_insert - Insert and append operators for Visual mode.
2577 */
2578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002579op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 long ins_len, pre_textlen = 0;
2582 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002583 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 struct block_def bd;
2585 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002586 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588 /* edit() changes this - record it for OP_APPEND */
2589 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2590
2591 /* vis block is still marked. Get rid of it now. */
2592 curwin->w_cursor.lnum = oap->start.lnum;
2593 update_screen(INVERTED);
2594
2595 if (oap->block_mode)
2596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 /* When 'virtualedit' is used, need to insert the extra spaces before
2598 * doing block_prep(). When only "block" is used, virtual edit is
2599 * already disabled, but still need it when calling
2600 * coladvance_force(). */
2601 if (curwin->w_cursor.coladd > 0)
2602 {
2603 int old_ve_flags = ve_flags;
2604
2605 ve_flags = VE_ALL;
2606 if (u_save_cursor() == FAIL)
2607 return;
2608 coladvance_force(oap->op_type == OP_APPEND
2609 ? oap->end_vcol + 1 : getviscol());
2610 if (oap->op_type == OP_APPEND)
2611 --curwin->w_cursor.col;
2612 ve_flags = old_ve_flags;
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 /* Get the info about the block before entering the text */
2615 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002616 /* Get indent information */
2617 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (oap->op_type == OP_APPEND)
2621 firstline += bd.textlen;
2622 pre_textlen = (long)STRLEN(firstline);
2623 }
2624
2625 if (oap->op_type == OP_APPEND)
2626 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002627 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
2629 /* Move the cursor to the character right of the block. */
2630 curwin->w_set_curswant = TRUE;
2631 while (*ml_get_cursor() != NUL
2632 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2633 ++curwin->w_cursor.col;
2634 if (bd.is_short && !bd.is_MAX)
2635 {
2636 /* First line was too short, make it longer and adjust the
2637 * values in "bd". */
2638 if (u_save_cursor() == FAIL)
2639 return;
2640 for (i = 0; i < bd.endspaces; ++i)
2641 ins_char(' ');
2642 bd.textlen += bd.endspaces;
2643 }
2644 }
2645 else
2646 {
2647 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002648 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002651 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 && oap->start_vcol != oap->end_vcol)
2653 inc_cursor();
2654 }
2655 }
2656
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002657 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002658 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002660 /* When a tab was inserted, and the characters in front of the tab
2661 * have been converted to a tab as well, the column of the cursor
2662 * might have actually been reduced, so need to adjust here. */
2663 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002664 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002665 oap->start = curbuf->b_op_start_orig;
2666
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002667 /* If user has moved off this line, we don't know what to do, so do
2668 * nothing.
2669 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2670 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return;
2672
2673 if (oap->block_mode)
2674 {
2675 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002676 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002677 size_t len;
2678 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002680 /* If indent kicked in, the firstline might have changed
2681 * but only do that, if the indent actually increased. */
2682 ind_post = (colnr_T)getwhitecols_curline();
2683 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2684 {
2685 bd.textcol += ind_post - ind_pre;
2686 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002687 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002688 }
2689
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002690 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002691 * to adjust the block for that. But only do it, if the difference
2692 * does not come from indent kicking in. */
2693 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2694 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002695 {
2696 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002697 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002698 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002699 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002700 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002701 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002702 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002703 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002704 pre_textlen -= t - oap->start_vcol;
2705 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002706 }
2707 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002708 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002709 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002710 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002711 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002712 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002713 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002714 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002715 /* reset pre_textlen to the value of OP_INSERT */
2716 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002717 pre_textlen -= t - oap->start_vcol;
2718 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002719 oap->op_type = OP_INSERT;
2720 }
2721 }
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 /*
2724 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002725 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 * Don't do this when "$" used, end-of-line will have changed.
2727 */
2728 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2729 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2730 {
2731 if (oap->op_type == OP_APPEND)
2732 {
2733 pre_textlen += bd2.textlen - bd.textlen;
2734 if (bd2.endspaces)
2735 --bd2.textlen;
2736 }
2737 bd.textcol = bd2.textcol;
2738 bd.textlen = bd2.textlen;
2739 }
2740
2741 /*
2742 * Subsequent calls to ml_get() flush the firstline data - take a
2743 * copy of the required string.
2744 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002745 firstline = ml_get(oap->start.lnum);
2746 len = STRLEN(firstline);
2747 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002749 add += bd.textlen;
2750 if ((size_t)add > len)
2751 firstline += len; // short line, point to the NUL
2752 else
2753 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002754 if (pre_textlen >= 0
2755 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 ins_text = vim_strnsave(firstline, (int)ins_len);
2758 if (ins_text != NULL)
2759 {
2760 /* block handled here */
2761 if (u_save(oap->start.lnum,
2762 (linenr_T)(oap->end.lnum + 1)) == OK)
2763 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2764 &bd);
2765
2766 curwin->w_cursor.col = oap->start.col;
2767 check_cursor();
2768 vim_free(ins_text);
2769 }
2770 }
2771 }
2772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774/*
2775 * op_change - handle a change operation
2776 *
2777 * return TRUE if edit() returns because of a CTRL-O command
2778 */
2779 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002780op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 colnr_T l;
2783 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 long offset;
2785 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002786 long ins_len;
2787 long pre_textlen = 0;
2788 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 char_u *firstline;
2790 char_u *ins_text, *newp, *oldp;
2791 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 l = oap->start.col;
2794 if (oap->motion_type == MLINE)
2795 {
2796 l = 0;
2797#ifdef FEAT_SMARTINDENT
2798 if (!p_paste && curbuf->b_p_si
2799# ifdef FEAT_CINDENT
2800 && !curbuf->b_p_cin
2801# endif
2802 )
2803 can_si = TRUE; /* It's like opening a new line, do si */
2804#endif
2805 }
2806
2807 /* First delete the text in the region. In an empty buffer only need to
2808 * save for undo */
2809 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2810 {
2811 if (u_save_cursor() == FAIL)
2812 return FALSE;
2813 }
2814 else if (op_delete(oap) == FAIL)
2815 return FALSE;
2816
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002817 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 && !virtual_op)
2819 inc_cursor();
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 /* check for still on same line (<CR> in inserted text meaningless) */
2822 /* skip blank lines too */
2823 if (oap->block_mode)
2824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 /* Add spaces before getting the current line length. */
2826 if (virtual_op && (curwin->w_cursor.coladd > 0
2827 || gchar_cursor() == NUL))
2828 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00002829 firstline = ml_get(oap->start.lnum);
2830 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002831 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 bd.textcol = curwin->w_cursor.col;
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2836 if (oap->motion_type == MLINE)
2837 fix_indent();
2838#endif
2839
2840 retval = edit(NUL, FALSE, (linenr_T)1);
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002843 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002845 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002847 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002849 /* Auto-indenting may have changed the indent. If the cursor was past
2850 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002852 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002854 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002855
2856 pre_textlen += new_indent - pre_indent;
2857 bd.textcol += new_indent - pre_indent;
2858 }
2859
2860 ins_len = (long)STRLEN(firstline) - pre_textlen;
2861 if (ins_len > 0)
2862 {
2863 /* Subsequent calls to ml_get() flush the firstline data - take a
2864 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002865 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002867 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2869 linenr++)
2870 {
2871 block_prep(oap, &bd, linenr, TRUE);
2872 if (!bd.is_short || virtual_op)
2873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 pos_T vpos;
2875
2876 /* If the block starts in virtual space, count the
2877 * initial coladd offset as part of "startspaces" */
2878 if (bd.is_short)
2879 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002880 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 else
2884 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002886 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if (newp == NULL)
2888 continue;
2889 /* copy up to block start */
2890 mch_memmove(newp, oldp, (size_t)bd.textcol);
2891 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002892 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2895 offset += ins_len;
2896 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002897 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 ml_replace(linenr, newp, FALSE);
2899 }
2900 }
2901 check_cursor();
2902
2903 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2904 }
2905 vim_free(ins_text);
2906 }
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
2909 return retval;
2910}
2911
2912/*
2913 * set all the yank registers to empty (called from main())
2914 */
2915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002916init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 int i;
2919
2920 for (i = 0; i < NUM_REGISTERS; ++i)
2921 y_regs[i].y_array = NULL;
2922}
2923
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002924#if defined(EXITFREE) || defined(PROTO)
2925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002926clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002927{
2928 int i;
2929
2930 for (i = 0; i < NUM_REGISTERS; ++i)
2931 {
2932 y_current = &y_regs[i];
2933 if (y_current->y_array != NULL)
2934 free_yank_all();
2935 }
2936}
2937#endif
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939/*
2940 * Free "n" lines from the current yank register.
2941 * Called for normal freeing and in case of error.
2942 */
2943 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002944free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 if (y_current->y_array != NULL)
2947 {
2948 long i;
2949
2950 for (i = n; --i >= 0; )
2951 {
2952#ifdef AMIGA /* only for very slow machines */
2953 if ((i & 1023) == 1023) /* this may take a while */
2954 {
2955 /*
2956 * This message should never cause a hit-return message.
2957 * Overwrite this message with any next message.
2958 */
2959 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002960 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 --no_wait_return;
2962 msg_didout = FALSE;
2963 msg_col = 0;
2964 }
2965#endif
2966 vim_free(y_current->y_array[i]);
2967 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002968 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#ifdef AMIGA
2970 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002971 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
2973 }
2974}
2975
2976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002977free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 free_yank(y_current->y_size);
2980}
2981
2982/*
2983 * Yank the text between "oap->start" and "oap->end" into a yank register.
2984 * If we are to append (uppercase register), we first yank into a new yank
2985 * register and then concatenate the old and the new one (so we keep the old
2986 * one in case of out-of-memory).
2987 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002988 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 */
2990 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002991op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002994 yankreg_T *curr; /* copy of y_current */
2995 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 char_u **new_ptr;
2997 linenr_T lnum; /* current line number */
2998 long j;
2999 int yanktype = oap->motion_type;
3000 long yanklines = oap->line_count;
3001 linenr_T yankendlnum = oap->end.lnum;
3002 char_u *p;
3003 char_u *pnew;
3004 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003005#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003006 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
3009 /* check for read-only register */
3010 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3011 {
3012 beep_flush();
3013 return FAIL;
3014 }
3015 if (oap->regname == '_') /* black hole: nothing to do */
3016 return OK;
3017
3018#ifdef FEAT_CLIPBOARD
3019 if (!clip_star.available && oap->regname == '*')
3020 oap->regname = 0;
3021 else if (!clip_plus.available && oap->regname == '+')
3022 oap->regname = 0;
3023#endif
3024
3025 if (!deleting) /* op_delete() already set y_current */
3026 get_yank_register(oap->regname, TRUE);
3027
3028 curr = y_current;
3029 /* append to existing contents */
3030 if (y_append && y_current->y_array != NULL)
3031 y_current = &newreg;
3032 else
3033 free_yank_all(); /* free previously yanked lines */
3034
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003035 /*
3036 * If the cursor was in column 1 before and after the movement, and the
3037 * operator is not inclusive, the yank is always linewise.
3038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if ( oap->motion_type == MCHAR
3040 && oap->start.col == 0
3041 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003043 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 && oap->end.col == 0
3045 && yanklines > 1)
3046 {
3047 yanktype = MLINE;
3048 --yankendlnum;
3049 --yanklines;
3050 }
3051
3052 y_current->y_size = yanklines;
3053 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 y_current->y_width = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003055 y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 if (y_current->y_array == NULL)
3057 {
3058 y_current = curr;
3059 return FAIL;
3060 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003061#ifdef FEAT_VIMINFO
3062 y_current->y_time_set = vim_time();
3063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
3065 y_idx = 0;
3066 lnum = oap->start.lnum;
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (oap->block_mode)
3069 {
3070 /* Visual block mode */
3071 y_current->y_type = MBLOCK; /* set the yank register type */
3072 y_current->y_width = oap->end_vcol - oap->start_vcol;
3073
3074 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3075 y_current->y_width--;
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3079 {
3080 switch (y_current->y_type)
3081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 case MBLOCK:
3083 block_prep(oap, &bd, lnum, FALSE);
3084 if (yank_copy_line(&bd, y_idx) == FAIL)
3085 goto fail;
3086 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087
3088 case MLINE:
3089 if ((y_current->y_array[y_idx] =
3090 vim_strsave(ml_get(lnum))) == NULL)
3091 goto fail;
3092 break;
3093
3094 case MCHAR:
3095 {
3096 colnr_T startcol = 0, endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int is_oneChar = FALSE;
3098 colnr_T cs, ce;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 p = ml_get(lnum);
3101 bd.startspaces = 0;
3102 bd.endspaces = 0;
3103
3104 if (lnum == oap->start.lnum)
3105 {
3106 startcol = oap->start.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (virtual_op)
3108 {
3109 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3110 if (ce != cs && oap->start.coladd > 0)
3111 {
3112 /* Part of a tab selected -- but don't
3113 * double-count it. */
3114 bd.startspaces = (ce - cs + 1)
3115 - oap->start.coladd;
3116 startcol++;
3117 }
3118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
3120
3121 if (lnum == oap->end.lnum)
3122 {
3123 endcol = oap->end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (virtual_op)
3125 {
3126 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3127 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 /* Don't add space for double-wide
3129 * char; endcol will be on last byte
3130 * of multi-byte char. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003131 && (*mb_head_off)(p, p + endcol) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 if (oap->start.lnum == oap->end.lnum
3134 && oap->start.col == oap->end.col)
3135 {
3136 /* Special case: inside a single char */
3137 is_oneChar = TRUE;
3138 bd.startspaces = oap->end.coladd
3139 - oap->start.coladd + oap->inclusive;
3140 endcol = startcol;
3141 }
3142 else
3143 {
3144 bd.endspaces = oap->end.coladd
3145 + oap->inclusive;
3146 endcol -= oap->inclusive;
3147 }
3148 }
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003151 if (endcol == MAXCOL)
3152 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003153 if (startcol > endcol || is_oneChar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 bd.textlen = 0;
3155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 bd.textlen = endcol - startcol + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 bd.textstart = p + startcol;
3158 if (yank_copy_line(&bd, y_idx) == FAIL)
3159 goto fail;
3160 break;
3161 }
3162 /* NOTREACHED */
3163 }
3164 }
3165
3166 if (curr != y_current) /* append the new block to the old block */
3167 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003168 new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (new_ptr == NULL)
3170 goto fail;
3171 for (j = 0; j < curr->y_size; ++j)
3172 new_ptr[j] = curr->y_array[j];
3173 vim_free(curr->y_array);
3174 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003175#ifdef FEAT_VIMINFO
3176 curr->y_time_set = vim_time();
3177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3180 curr->y_type = MLINE;
3181
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003182 /* Concatenate the last line of the old block with the first line of
3183 * the new block, unless being Vi compatible. */
3184 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02003186 pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
3187 + STRLEN(y_current->y_array[0]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (pnew == NULL)
3189 {
3190 y_idx = y_current->y_size - 1;
3191 goto fail;
3192 }
3193 STRCPY(pnew, curr->y_array[--j]);
3194 STRCAT(pnew, y_current->y_array[0]);
3195 vim_free(curr->y_array[j]);
3196 vim_free(y_current->y_array[0]);
3197 curr->y_array[j++] = pnew;
3198 y_idx = 1;
3199 }
3200 else
3201 y_idx = 0;
3202 while (y_idx < y_current->y_size)
3203 curr->y_array[j++] = y_current->y_array[y_idx++];
3204 curr->y_size = j;
3205 vim_free(y_current->y_array);
3206 y_current = curr;
3207 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003208 if (curwin->w_p_rnu)
3209 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (mess) /* Display message about yank? */
3211 {
3212 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 && yanklines == 1)
3215 yanklines = 0;
3216 /* Some versions of Vi use ">=" here, some don't... */
3217 if (yanklines > p_report)
3218 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003219 char namebuf[100];
3220
3221 if (oap->regname == NUL)
3222 *namebuf = NUL;
3223 else
3224 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003225 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 /* redisplay now, so message is not deleted */
3228 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003229 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003231 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003232 "block of %ld lines yanked%s", yanklines),
3233 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003237 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003238 "%ld lines yanked%s", yanklines),
3239 yanklines, namebuf);
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242 }
3243
3244 /*
3245 * Set "'[" and "']" marks.
3246 */
3247 curbuf->b_op_start = oap->start;
3248 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003249 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003250 {
3251 curbuf->b_op_start.col = 0;
3252 curbuf->b_op_end.col = MAXCOL;
3253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255#ifdef FEAT_CLIPBOARD
3256 /*
3257 * If we were yanking to the '*' register, send result to clipboard.
3258 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3259 * to the '*' register.
3260 */
3261 if (clip_star.available
3262 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003263 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003264 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
3266 if (curr != &(y_regs[STAR_REGISTER]))
3267 /* Copy the text from register 0 to the clipboard register. */
3268 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3269
3270 clip_own_selection(&clip_star);
3271 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003272# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003273 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
3276
3277# ifdef FEAT_X11
3278 /*
3279 * If we were yanking to the '+' register, send result to selection.
3280 * Also copy to the '*' register, in case auto-select is off.
3281 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003282 if (clip_plus.available
3283 && (curr == &(y_regs[PLUS_REGISTER])
3284 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003285 && ((clip_unnamed | clip_unnamed_saved) &
3286 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003288 if (curr != &(y_regs[PLUS_REGISTER]))
3289 /* Copy the text from register 0 to the clipboard register. */
3290 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 clip_own_selection(&clip_plus);
3293 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003294 if (!clip_isautosel_star() && !clip_isautosel_plus()
3295 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3298 clip_own_selection(&clip_star);
3299 clip_gen_set_selection(&clip_star);
3300 }
3301 }
3302# endif
3303#endif
3304
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003305#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003306 if (!deleting && has_textyankpost())
3307 yank_do_autocmd(oap, y_current);
3308#endif
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return OK;
3311
3312fail: /* free the allocated lines */
3313 free_yank(y_idx + 1);
3314 y_current = curr;
3315 return FAIL;
3316}
3317
3318 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003319yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 char_u *pnew;
3322
3323 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3324 == NULL)
3325 return FAIL;
3326 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003327 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 pnew += bd->startspaces;
3329 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3330 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003331 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 pnew += bd->endspaces;
3333 *pnew = NUL;
3334 return OK;
3335}
3336
3337#ifdef FEAT_CLIPBOARD
3338/*
3339 * Make a copy of the y_current register to register "reg".
3340 */
3341 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003342copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003344 yankreg_T *curr = y_current;
3345 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347 y_current = reg;
3348 free_yank_all();
3349 *y_current = *curr;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003350 y_current->y_array = lalloc_clear(
Bram Moolenaar51e14382019-05-25 20:21:28 +02003351 sizeof(char_u *) * y_current->y_size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (y_current->y_array == NULL)
3353 y_current->y_size = 0;
3354 else
3355 for (j = 0; j < y_current->y_size; ++j)
3356 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3357 {
3358 free_yank(j);
3359 y_current->y_size = 0;
3360 break;
3361 }
3362 y_current = curr;
3363}
3364#endif
3365
3366/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003367 * Put contents of register "regname" into the text.
3368 * Caller must check "regname" to be valid!
3369 * "flags": PUT_FIXINDENT make indent look nice
3370 * PUT_CURSEND leave cursor after end of new text
3371 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 */
3373 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003374do_put(
3375 int regname,
3376 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3377 long count,
3378 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 char_u *ptr;
3381 char_u *newp, *oldp;
3382 int yanklen;
3383 int totlen = 0; /* init for gcc */
3384 linenr_T lnum;
3385 colnr_T col;
3386 long i; /* index in y_array[] */
3387 int y_type;
3388 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 int oldlen;
3390 long y_width = 0;
3391 colnr_T vcol;
3392 int delcount;
3393 int incr = 0;
3394 long j;
3395 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 char_u **y_array = NULL;
3397 long nr_lines = 0;
3398 pos_T new_cursor;
3399 int indent;
3400 int orig_indent = 0; /* init for gcc */
3401 int indent_diff = 0; /* init for gcc */
3402 int first_indent = TRUE;
3403 int lendiff = 0;
3404 pos_T old_pos;
3405 char_u *insert_string = NULL;
3406 int allocated = FALSE;
3407 long cnt;
3408
3409#ifdef FEAT_CLIPBOARD
3410 /* Adjust register name for "unnamed" in 'clipboard'. */
3411 adjust_clip_reg(&regname);
3412 (void)may_get_selection(regname);
3413#endif
3414
3415 if (flags & PUT_FIXINDENT)
3416 orig_indent = get_indent();
3417
3418 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3419 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3420
3421 /*
3422 * Using inserted text works differently, because the register includes
3423 * special characters (newlines, etc.).
3424 */
3425 if (regname == '.')
3426 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003427 if (VIsual_active)
3428 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3430 (count == -1 ? 'O' : 'i')), count, FALSE);
3431 /* Putting the text is done later, so can't really move the cursor to
3432 * the next character. Use "l" to simulate it. */
3433 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3434 stuffcharReadbuff('l');
3435 return;
3436 }
3437
3438 /*
3439 * For special registers '%' (file name), '#' (alternate file name) and
3440 * ':' (last command line), etc. we have to create a fake yank register.
3441 */
3442 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3443 {
3444 if (insert_string == NULL)
3445 return;
3446 }
3447
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003448 /* Autocommands may be executed when saving lines for undo. This might
3449 * make "y_array" invalid, so we start undo now to avoid that. */
3450 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3451 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (insert_string != NULL)
3454 {
3455 y_type = MCHAR;
3456#ifdef FEAT_EVAL
3457 if (regname == '=')
3458 {
3459 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003460 * characters.
3461 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 for (;;)
3463 {
3464 y_size = 0;
3465 ptr = insert_string;
3466 while (ptr != NULL)
3467 {
3468 if (y_array != NULL)
3469 y_array[y_size] = ptr;
3470 ++y_size;
3471 ptr = vim_strchr(ptr, '\n');
3472 if (ptr != NULL)
3473 {
3474 if (y_array != NULL)
3475 *ptr = NUL;
3476 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003477 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (*ptr == NUL)
3479 {
3480 y_type = MLINE;
3481 break;
3482 }
3483 }
3484 }
3485 if (y_array != NULL)
3486 break;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003487 y_array = ALLOC_MULT(char_u *, y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (y_array == NULL)
3489 goto end;
3490 }
3491 }
3492 else
3493#endif
3494 {
3495 y_size = 1; /* use fake one-line yank register */
3496 y_array = &insert_string;
3497 }
3498 }
3499 else
3500 {
3501 get_yank_register(regname, FALSE);
3502
3503 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 y_size = y_current->y_size;
3506 y_array = y_current->y_array;
3507 }
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (y_type == MLINE)
3510 {
3511 if (flags & PUT_LINE_SPLIT)
3512 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003513 char_u *p;
3514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 /* "p" or "P" in Visual mode: split the lines to put the text in
3516 * between. */
3517 if (u_save_cursor() == FAIL)
3518 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003519 p = ml_get_cursor();
3520 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003521 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003522 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (ptr == NULL)
3524 goto end;
3525 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3526 vim_free(ptr);
3527
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003528 oldp = ml_get_curline();
3529 p = oldp + curwin->w_cursor.col;
3530 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003531 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003532 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (ptr == NULL)
3534 goto end;
3535 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3536 ++nr_lines;
3537 dir = FORWARD;
3538 }
3539 if (flags & PUT_LINE_FORWARD)
3540 {
3541 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003542 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 dir = FORWARD;
3544 }
3545 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3546 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3550 y_type = MLINE;
3551
3552 if (y_size == 0 || y_array == NULL)
3553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003554 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 regname == 0 ? (char_u *)"\"" : transchar(regname));
3556 goto end;
3557 }
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (y_type == MBLOCK)
3560 {
3561 lnum = curwin->w_cursor.lnum + y_size + 1;
3562 if (lnum > curbuf->b_ml.ml_line_count)
3563 lnum = curbuf->b_ml.ml_line_count + 1;
3564 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3565 goto end;
3566 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003567 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 lnum = curwin->w_cursor.lnum;
3570#ifdef FEAT_FOLDING
3571 /* Correct line number for closed fold. Don't move the cursor yet,
3572 * u_save() uses it. */
3573 if (dir == BACKWARD)
3574 (void)hasFolding(lnum, &lnum, NULL);
3575 else
3576 (void)hasFolding(lnum, NULL, &lnum);
3577#endif
3578 if (dir == FORWARD)
3579 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003580 /* In an empty buffer the empty line is going to be replaced, include
3581 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003582 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 goto end;
3584#ifdef FEAT_FOLDING
3585 if (dir == FORWARD)
3586 curwin->w_cursor.lnum = lnum - 1;
3587 else
3588 curwin->w_cursor.lnum = lnum;
3589 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3590#endif
3591 }
3592 else if (u_save_cursor() == FAIL)
3593 goto end;
3594
3595 yanklen = (int)STRLEN(y_array[0]);
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (ve_flags == VE_ALL && y_type == MCHAR)
3598 {
3599 if (gchar_cursor() == TAB)
3600 {
3601 /* Don't need to insert spaces when "p" on the last position of a
3602 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003603#ifdef FEAT_VARTABS
3604 int viscol = getviscol();
3605 if (dir == FORWARD
3606 ? tabstop_padding(viscol, curbuf->b_p_ts,
3607 curbuf->b_p_vts_array) != 1
3608 : curwin->w_cursor.coladd > 0)
3609 coladvance_force(viscol);
3610#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (dir == FORWARD
3612 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3613 : curwin->w_cursor.coladd > 0)
3614 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 else
3617 curwin->w_cursor.coladd = 0;
3618 }
3619 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3620 coladvance_force(getviscol() + (dir == FORWARD));
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 lnum = curwin->w_cursor.lnum;
3624 col = curwin->w_cursor.col;
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 /*
3627 * Block mode
3628 */
3629 if (y_type == MBLOCK)
3630 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003631 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 colnr_T endcol2 = 0;
3633
3634 if (dir == FORWARD && c != NUL)
3635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (ve_flags == VE_ALL)
3637 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3638 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (has_mbyte)
3642 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003643 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (c != TAB || ve_flags != VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 ++curwin->w_cursor.col;
3647 ++col;
3648 }
3649 else
3650 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003653 if (ve_flags == VE_ALL
3654 && (curwin->w_cursor.coladd > 0
3655 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 if (dir == FORWARD && c == NUL)
3658 ++col;
3659 if (dir != FORWARD && c != NUL)
3660 ++curwin->w_cursor.col;
3661 if (c == TAB)
3662 {
3663 if (dir == BACKWARD && curwin->w_cursor.col)
3664 curwin->w_cursor.col--;
3665 if (dir == FORWARD && col - 1 == endcol2)
3666 curwin->w_cursor.col++;
3667 }
3668 }
3669 curwin->w_cursor.coladd = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003670 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 for (i = 0; i < y_size; ++i)
3672 {
3673 int spaces;
3674 char shortline;
3675
3676 bd.startspaces = 0;
3677 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 vcol = 0;
3679 delcount = 0;
3680
3681 /* add a new line */
3682 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3683 {
3684 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3685 (colnr_T)1, FALSE) == FAIL)
3686 break;
3687 ++nr_lines;
3688 }
3689 /* get the old line and advance to the position to insert at */
3690 oldp = ml_get_curline();
3691 oldlen = (int)STRLEN(oldp);
3692 for (ptr = oldp; vcol < col && *ptr; )
3693 {
3694 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003695 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 vcol += incr;
3697 }
3698 bd.textcol = (colnr_T)(ptr - oldp);
3699
3700 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3701
3702 if (vcol < col) /* line too short, padd with spaces */
3703 bd.startspaces = col - vcol;
3704 else if (vcol > col)
3705 {
3706 bd.endspaces = vcol - col;
3707 bd.startspaces = incr - bd.endspaces;
3708 --bd.textcol;
3709 delcount = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (has_mbyte)
3711 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (oldp[bd.textcol] != TAB)
3713 {
3714 /* Only a Tab can be split into spaces. Other
3715 * characters will have to be moved to after the
3716 * block, causing misalignment. */
3717 delcount = 0;
3718 bd.endspaces = 0;
3719 }
3720 }
3721
3722 yanklen = (int)STRLEN(y_array[i]);
3723
3724 /* calculate number of spaces required to fill right side of block*/
3725 spaces = y_width + 1;
3726 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003727 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (spaces < 0)
3729 spaces = 0;
3730
3731 /* insert the new text */
3732 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003733 newp = alloc(totlen + oldlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (newp == NULL)
3735 break;
3736 /* copy part up to cursor to new line */
3737 ptr = newp;
3738 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3739 ptr += bd.textcol;
3740 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003741 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 ptr += bd.startspaces;
3743 /* insert the new text */
3744 for (j = 0; j < count; ++j)
3745 {
3746 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3747 ptr += yanklen;
3748
3749 /* insert block's trailing spaces only if there's text behind */
3750 if ((j < count - 1 || !shortline) && spaces)
3751 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003752 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 ptr += spaces;
3754 }
3755 }
3756 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003757 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 ptr += bd.endspaces;
3759 /* move the text after the cursor to the end of the line. */
3760 mch_memmove(ptr, oldp + bd.textcol + delcount,
3761 (size_t)(oldlen - bd.textcol - delcount + 1));
3762 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3763
3764 ++curwin->w_cursor.lnum;
3765 if (i == 0)
3766 curwin->w_cursor.col += bd.startspaces;
3767 }
3768
3769 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3770
3771 /* Set '[ mark. */
3772 curbuf->b_op_start = curwin->w_cursor;
3773 curbuf->b_op_start.lnum = lnum;
3774
3775 /* adjust '] mark */
3776 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3777 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 curbuf->b_op_end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (flags & PUT_CURSEND)
3780 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003781 colnr_T len;
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 curwin->w_cursor = curbuf->b_op_end;
3784 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003785
3786 /* in Insert mode we might be after the NUL, correct for that */
3787 len = (colnr_T)STRLEN(ml_get_curline());
3788 if (curwin->w_cursor.col > len)
3789 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 else
3792 curwin->w_cursor.lnum = lnum;
3793 }
3794 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
3796 /*
3797 * Character or Line mode
3798 */
3799 if (y_type == MCHAR)
3800 {
3801 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3802 * char */
3803 if (dir == FORWARD && gchar_cursor() != NUL)
3804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (has_mbyte)
3806 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003807 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
3809 /* put it on the next of the multi-byte character. */
3810 col += bytelen;
3811 if (yanklen)
3812 {
3813 curwin->w_cursor.col += bytelen;
3814 curbuf->b_op_end.col += bytelen;
3815 }
3816 }
3817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 ++col;
3820 if (yanklen)
3821 {
3822 ++curwin->w_cursor.col;
3823 ++curbuf->b_op_end.col;
3824 }
3825 }
3826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 curbuf->b_op_start = curwin->w_cursor;
3828 }
3829 /*
3830 * Line mode: BACKWARD is the same as FORWARD on the previous line
3831 */
3832 else if (dir == BACKWARD)
3833 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003834 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 /*
3837 * simple case: insert into current line
3838 */
3839 if (y_type == MCHAR && y_size == 1)
3840 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003841 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003842
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003843 if (VIsual_active)
3844 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003845 end_lnum = curbuf->b_visual.vi_end.lnum;
3846 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3847 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003848 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003849
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003850 do {
3851 totlen = count * yanklen;
3852 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003854 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003855 if (VIsual_active && col > (int)STRLEN(oldp))
3856 {
3857 lnum++;
3858 continue;
3859 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02003860 newp = alloc(STRLEN(oldp) + totlen + 1);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003861 if (newp == NULL)
3862 goto end; /* alloc() gave an error message */
3863 mch_memmove(newp, oldp, (size_t)col);
3864 ptr = newp + col;
3865 for (i = 0; i < count; ++i)
3866 {
3867 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3868 ptr += yanklen;
3869 }
3870 STRMOVE(ptr, oldp + col);
3871 ml_replace(lnum, newp, FALSE);
3872 /* Place cursor on last putted char. */
3873 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003874 {
3875 /* make sure curwin->w_virtcol is updated */
3876 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003877 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003880 if (VIsual_active)
3881 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003882 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003883
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003884 if (VIsual_active) /* reset lnum to the last visual line */
3885 lnum--;
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 curbuf->b_op_end = curwin->w_cursor;
3888 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3889 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3890 ++curwin->w_cursor.col;
3891 changed_bytes(lnum, col);
3892 }
3893 else
3894 {
3895 /*
3896 * Insert at least one line. When y_type is MCHAR, break the first
3897 * line in two.
3898 */
3899 for (cnt = 1; cnt <= count; ++cnt)
3900 {
3901 i = 0;
3902 if (y_type == MCHAR)
3903 {
3904 /*
3905 * Split the current line in two at the insert position.
3906 * First insert y_array[size - 1] in front of second line.
3907 * Then append y_array[0] to first line.
3908 */
3909 lnum = new_cursor.lnum;
3910 ptr = ml_get(lnum) + col;
3911 totlen = (int)STRLEN(y_array[y_size - 1]);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003912 newp = alloc(STRLEN(ptr) + totlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (newp == NULL)
3914 goto error;
3915 STRCPY(newp, y_array[y_size - 1]);
3916 STRCAT(newp, ptr);
3917 /* insert second line */
3918 ml_append(lnum, newp, (colnr_T)0, FALSE);
3919 vim_free(newp);
3920
3921 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003922 newp = alloc(col + yanklen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (newp == NULL)
3924 goto error;
3925 /* copy first part of line */
3926 mch_memmove(newp, oldp, (size_t)col);
3927 /* append to first line */
3928 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3929 ml_replace(lnum, newp, FALSE);
3930
3931 curwin->w_cursor.lnum = lnum;
3932 i = 1;
3933 }
3934
3935 for (; i < y_size; ++i)
3936 {
3937 if ((y_type != MCHAR || i < y_size - 1)
3938 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3939 == FAIL)
3940 goto error;
3941 lnum++;
3942 ++nr_lines;
3943 if (flags & PUT_FIXINDENT)
3944 {
3945 old_pos = curwin->w_cursor;
3946 curwin->w_cursor.lnum = lnum;
3947 ptr = ml_get(lnum);
3948 if (cnt == count && i == y_size - 1)
3949 lendiff = (int)STRLEN(ptr);
3950#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3951 if (*ptr == '#' && preprocs_left())
3952 indent = 0; /* Leave # lines at start */
3953 else
3954#endif
3955 if (*ptr == NUL)
3956 indent = 0; /* Ignore empty lines */
3957 else if (first_indent)
3958 {
3959 indent_diff = orig_indent - get_indent();
3960 indent = orig_indent;
3961 first_indent = FALSE;
3962 }
3963 else if ((indent = get_indent() + indent_diff) < 0)
3964 indent = 0;
3965 (void)set_indent(indent, 0);
3966 curwin->w_cursor = old_pos;
3967 /* remember how many chars were removed */
3968 if (cnt == count && i == y_size - 1)
3969 lendiff -= (int)STRLEN(ml_get(lnum));
3970 }
3971 }
3972 }
3973
3974error:
3975 /* Adjust marks. */
3976 if (y_type == MLINE)
3977 {
3978 curbuf->b_op_start.col = 0;
3979 if (dir == FORWARD)
3980 curbuf->b_op_start.lnum++;
3981 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003982 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003983 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003984 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003985 < curbuf->b_ml.ml_line_count
3986#ifdef FEAT_DIFF
3987 || curwin->w_p_diff
3988#endif
3989 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003990 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 (linenr_T)MAXLNUM, nr_lines, 0L);
3992
3993 /* note changed text for displaying and folding */
3994 if (y_type == MCHAR)
3995 changed_lines(curwin->w_cursor.lnum, col,
3996 curwin->w_cursor.lnum + 1, nr_lines);
3997 else
3998 changed_lines(curbuf->b_op_start.lnum, 0,
3999 curbuf->b_op_start.lnum, nr_lines);
4000
4001 /* put '] mark at last inserted character */
4002 curbuf->b_op_end.lnum = lnum;
4003 /* correct length for change in indent */
4004 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4005 if (col > 1)
4006 curbuf->b_op_end.col = col - 1;
4007 else
4008 curbuf->b_op_end.col = 0;
4009
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004010 if (flags & PUT_CURSLINE)
4011 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004012 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004013 curwin->w_cursor.lnum = lnum;
4014 beginline(BL_WHITE | BL_FIX);
4015 }
4016 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 /* put cursor after inserted text */
4019 if (y_type == MLINE)
4020 {
4021 if (lnum >= curbuf->b_ml.ml_line_count)
4022 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4023 else
4024 curwin->w_cursor.lnum = lnum + 1;
4025 curwin->w_cursor.col = 0;
4026 }
4027 else
4028 {
4029 curwin->w_cursor.lnum = lnum;
4030 curwin->w_cursor.col = col;
4031 }
4032 }
4033 else if (y_type == MLINE)
4034 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004035 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 curwin->w_cursor.col = 0;
4037 if (dir == FORWARD)
4038 ++curwin->w_cursor.lnum;
4039 beginline(BL_WHITE | BL_FIX);
4040 }
4041 else /* put cursor on first inserted character */
4042 curwin->w_cursor = new_cursor;
4043 }
4044 }
4045
4046 msgmore(nr_lines);
4047 curwin->w_set_curswant = TRUE;
4048
4049end:
4050 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004052 if (regname == '=')
4053 vim_free(y_array);
4054
Bram Moolenaar033d8882013-09-25 23:24:57 +02004055 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004056
Bram Moolenaar677ee682005-01-27 14:41:15 +00004057 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004058 adjust_cursor_eol();
4059}
4060
4061/*
4062 * When the cursor is on the NUL past the end of the line and it should not be
4063 * there move it left.
4064 */
4065 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004066adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004067{
4068 if (curwin->w_cursor.col > 0
4069 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004070 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 && !(restart_edit || (State & INSERT)))
4072 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004073 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004074 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004077 {
4078 colnr_T scol, ecol;
4079
4080 /* Coladd is set to the width of the last character. */
4081 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4082 curwin->w_cursor.coladd = ecol - scol + 1;
4083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085}
4086
4087#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4088/*
4089 * Return TRUE if lines starting with '#' should be left aligned.
4090 */
4091 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004092preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
4094 return
4095# ifdef FEAT_SMARTINDENT
4096# ifdef FEAT_CINDENT
4097 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4098# else
4099 curbuf->b_p_si
4100# endif
4101# endif
4102# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004103 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4104 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105# endif
4106 ;
4107}
4108#endif
4109
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004110/*
4111 * Return the character name of the register with the given number.
4112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 if (num == -1)
4117 return '"';
4118 else if (num < 10)
4119 return num + '0';
4120 else if (num == DELETION_REGISTER)
4121 return '-';
4122#ifdef FEAT_CLIPBOARD
4123 else if (num == STAR_REGISTER)
4124 return '*';
4125 else if (num == PLUS_REGISTER)
4126 return '+';
4127#endif
4128 else
4129 {
4130#ifdef EBCDIC
4131 int i;
4132
4133 /* EBCDIC is really braindead ... */
4134 i = 'a' + (num - 10);
4135 if (i > 'i')
4136 i += 7;
4137 if (i > 'r')
4138 i += 8;
4139 return i;
4140#else
4141 return num + 'a' - 10;
4142#endif
4143 }
4144}
4145
4146/*
4147 * ":dis" and ":registers": Display the contents of the yank registers.
4148 */
4149 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004150ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004152 int i, n;
4153 long j;
4154 char_u *p;
4155 yankreg_T *yb;
4156 int name;
4157 int attr;
4158 char_u *arg = eap->arg;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004159 int clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 if (arg != NULL && *arg == NUL)
4162 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004163 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
4165 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004166 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4168 {
4169 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004170 if (arg != NULL && vim_strchr(arg, name) == NULL
4171#ifdef ONE_CLIPBOARD
4172 /* Star register and plus register contain the same thing. */
4173 && (name != '*' || vim_strchr(arg, '+') == NULL)
4174#endif
4175 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 continue; /* did not ask for this register */
4177
4178#ifdef FEAT_CLIPBOARD
4179 /* Adjust register name for "unnamed" in 'clipboard'.
4180 * When it's a clipboard register, fill it with the current contents
4181 * of the clipboard. */
4182 adjust_clip_reg(&name);
4183 (void)may_get_selection(name);
4184#endif
4185
4186 if (i == -1)
4187 {
4188 if (y_previous != NULL)
4189 yb = y_previous;
4190 else
4191 yb = &(y_regs[0]);
4192 }
4193 else
4194 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004195
4196#ifdef FEAT_EVAL
4197 if (name == MB_TOLOWER(redir_reg)
4198 || (redir_reg == '"' && yb == y_previous))
4199 continue; /* do not list register being written to, the
4200 * pointer can be freed */
4201#endif
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (yb->y_array != NULL)
4204 {
4205 msg_putchar('\n');
4206 msg_putchar('"');
4207 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004208 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 n = (int)Columns - 6;
4211 for (j = 0; j < yb->y_size && n > 1; ++j)
4212 {
4213 if (j)
4214 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004215 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 n -= 2;
4217 }
4218 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4219 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004220 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004221 msg_outtrans_len(p, clen);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004222 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 }
4225 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004226 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 out_flush(); /* show one line at a time */
4228 }
4229 ui_breakcheck();
4230 }
4231
4232 /*
4233 * display last inserted text
4234 */
4235 if ((p = get_last_insert()) != NULL
4236 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4237 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004238 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 dis_msg(p, TRUE);
4240 }
4241
4242 /*
4243 * display last command line
4244 */
4245 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4246 && !got_int)
4247 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004248 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 dis_msg(last_cmdline, FALSE);
4250 }
4251
4252 /*
4253 * display current file name
4254 */
4255 if (curbuf->b_fname != NULL
4256 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4257 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004258 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 dis_msg(curbuf->b_fname, FALSE);
4260 }
4261
4262 /*
4263 * display alternate file name
4264 */
4265 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4266 {
4267 char_u *fname;
4268 linenr_T dummy;
4269
4270 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4271 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004272 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 dis_msg(fname, FALSE);
4274 }
4275 }
4276
4277 /*
4278 * display last search pattern
4279 */
4280 if (last_search_pat() != NULL
4281 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4282 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004283 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 dis_msg(last_search_pat(), FALSE);
4285 }
4286
4287#ifdef FEAT_EVAL
4288 /*
4289 * display last used expression
4290 */
4291 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4292 && !got_int)
4293 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004294 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 dis_msg(expr_line, FALSE);
4296 }
4297#endif
4298}
4299
4300/*
4301 * display a string for do_dis()
4302 * truncate at end of screen line
4303 */
4304 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004305dis_msg(
4306 char_u *p,
4307 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 n = (int)Columns - 6;
4313 while (*p != NUL
4314 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4315 && (n -= ptr2cells(p)) >= 0)
4316 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004317 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 msg_outtrans_len(p, l);
4320 p += l;
4321 }
4322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 msg_outtrans_len(p++, 1);
4324 }
4325 ui_breakcheck();
4326}
4327
Bram Moolenaar81340392012-06-06 16:12:59 +02004328#if defined(FEAT_COMMENTS) || defined(PROTO)
4329/*
4330 * If "process" is TRUE and the line begins with a comment leader (possibly
4331 * after some white space), return a pointer to the text after it. Put a boolean
4332 * value indicating whether the line ends with an unclosed comment in
4333 * "is_comment".
4334 * line - line to be processed,
4335 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004336 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004337 * include_space - whether to also skip space following the comment leader,
4338 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004339 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004340 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004341 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004342skip_comment(
4343 char_u *line,
4344 int process,
4345 int include_space,
4346 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004347{
4348 char_u *comment_flags = NULL;
4349 int lead_len;
4350 int leader_offset = get_last_leader_offset(line, &comment_flags);
4351
4352 *is_comment = FALSE;
4353 if (leader_offset != -1)
4354 {
4355 /* Let's check whether the line ends with an unclosed comment.
4356 * If the last comment leader has COM_END in flags, there's no comment.
4357 */
4358 while (*comment_flags)
4359 {
4360 if (*comment_flags == COM_END
4361 || *comment_flags == ':')
4362 break;
4363 ++comment_flags;
4364 }
4365 if (*comment_flags != COM_END)
4366 *is_comment = TRUE;
4367 }
4368
4369 if (process == FALSE)
4370 return line;
4371
4372 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4373
4374 if (lead_len == 0)
4375 return line;
4376
4377 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004378 * - COM_END,
4379 * - colon,
4380 * whichever comes first.
4381 */
4382 while (*comment_flags)
4383 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004384 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004385 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02004386 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02004387 ++comment_flags;
4388 }
4389
4390 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004391 * starting with a closing part of a three-part comment. That's good,
4392 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004393 */
4394 if (*comment_flags == ':' || *comment_flags == NUL)
4395 line += lead_len;
4396
4397 return line;
4398}
4399#endif
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004402 * Join 'count' lines (minimal 2) at cursor position.
4403 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004404 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4405 * leaders should not be removed.
4406 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4407 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004409 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 */
4411 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004412do_join(
4413 long count,
4414 int insert_space,
4415 int save_undo,
4416 int use_formatoptions UNUSED,
4417 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004419 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004420 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004421 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004423 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004424 int endcurr1 = NUL;
4425 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004426 int currsize = 0; /* size of the current line */
4427 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004429 colnr_T col = 0;
4430 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004431#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004432 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004433 int remove_comments = (use_formatoptions == TRUE)
4434 && has_format_option(FO_REMOVE_COMS);
4435 int prev_was_comment;
4436#endif
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004437#ifdef FEAT_TEXT_PROP
4438 textprop_T **prop_lines = NULL;
4439 int *prop_lengths = NULL;
4440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004442 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4443 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4444 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004446 /* Allocate an array to store the number of spaces inserted before each
4447 * line. We will use it to pre-compute the length of the new line and the
4448 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004449 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004450 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004452#if defined(FEAT_COMMENTS) || defined(PROTO)
4453 if (remove_comments)
4454 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004455 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02004456 if (comments == NULL)
4457 {
4458 vim_free(spaces);
4459 return FAIL;
4460 }
4461 }
4462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004465 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004466 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004467 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004469 for (t = 0; t < count; ++t)
4470 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004471 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004472 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004473 {
4474 /* Set the '[ mark. */
4475 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4476 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4477 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004478#if defined(FEAT_COMMENTS) || defined(PROTO)
4479 if (remove_comments)
4480 {
4481 /* We don't want to remove the comment leader if the
4482 * previous line is not a comment. */
4483 if (t > 0 && prev_was_comment)
4484 {
4485
4486 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4487 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004488 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004489 curr = new_curr;
4490 }
4491 else
4492 curr = skip_comment(curr, FALSE, insert_space,
4493 &prev_was_comment);
4494 }
4495#endif
4496
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004497 if (insert_space && t > 0)
4498 {
4499 curr = skipwhite(curr);
4500 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004501 && (!has_format_option(FO_MBYTE_JOIN)
4502 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4503 && (!has_format_option(FO_MBYTE_JOIN2)
4504 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004505 )
4506 {
4507 /* don't add a space if the line is ending in a space */
4508 if (endcurr1 == ' ')
4509 endcurr1 = endcurr2;
4510 else
4511 ++spaces[t];
4512 /* extra space when 'joinspaces' set and line ends in '.' */
4513 if ( p_js
4514 && (endcurr1 == '.'
4515 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4516 && (endcurr1 == '?' || endcurr1 == '!'))))
4517 ++spaces[t];
4518 }
4519 }
4520 currsize = (int)STRLEN(curr);
4521 sumsize += currsize + spaces[t];
4522 endcurr1 = endcurr2 = NUL;
4523 if (insert_space && currsize > 0)
4524 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004525 if (has_mbyte)
4526 {
4527 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004528 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004529 endcurr1 = (*mb_ptr2char)(cend);
4530 if (cend > curr)
4531 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004532 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004533 endcurr2 = (*mb_ptr2char)(cend);
4534 }
4535 }
4536 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004537 {
4538 endcurr1 = *(curr + currsize - 1);
4539 if (currsize > 1)
4540 endcurr2 = *(curr + currsize - 2);
4541 }
4542 }
4543 line_breakcheck();
4544 if (got_int)
4545 {
4546 ret = FAIL;
4547 goto theend;
4548 }
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004551 /* store the column position before last line */
4552 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004554 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004555 newp = alloc(sumsize + 1);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004556 cend = newp + sumsize;
4557 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004559#ifdef FEAT_TEXT_PROP
4560 // We need to move properties of the lines that are going to be deleted to
4561 // the new long one.
4562 if (curbuf->b_has_textprop && !text_prop_frozen)
4563 {
4564 // Allocate an array to copy the text properties of joined lines into.
4565 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004566 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
4567 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004568 if (prop_lengths == NULL)
4569 VIM_CLEAR(prop_lines);
4570 }
4571#endif
4572
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004573 /*
4574 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004575 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004576 *
4577 * Move marks from each deleted line to the joined line, adjusting the
4578 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4579 * should not really be a problem.
4580 */
4581 for (t = count - 1; ; --t)
4582 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004583 int spaces_removed;
4584
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004585 cend -= currsize;
4586 mch_memmove(cend, curr, (size_t)currsize);
4587 if (spaces[t] > 0)
4588 {
4589 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004590 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004591 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004592
4593 // If deleting more spaces than adding, the cursor moves no more than
4594 // what is added if it is inside these spaces.
4595 spaces_removed = (curr - curr_start) - spaces[t];
4596
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004597 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004598 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004599 if (t == 0)
4600 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004601#ifdef FEAT_TEXT_PROP
4602 if (prop_lines != NULL)
4603 adjust_props_for_join(curwin->w_cursor.lnum + t,
4604 prop_lines + t - 1, prop_lengths + t - 1,
4605 (long)(cend - newp - spaces_removed), spaces_removed);
4606#endif
4607
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004608 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004609#if defined(FEAT_COMMENTS)
Bram Moolenaar81340392012-06-06 16:12:59 +02004610 if (remove_comments)
4611 curr += comments[t - 1];
4612#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004613 if (insert_space && t > 1)
4614 curr = skipwhite(curr);
4615 currsize = (int)STRLEN(curr);
4616 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004617
4618#ifdef FEAT_TEXT_PROP
4619 if (prop_lines != NULL)
4620 join_prop_lines(curwin->w_cursor.lnum, newp,
4621 prop_lines, prop_lengths, count);
4622 else
4623#endif
4624 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004626 if (setmark)
4627 {
4628 /* Set the '] mark. */
4629 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02004630 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004631 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 /* Only report the change in the first line here, del_lines() will report
4634 * the deleted line. */
4635 changed_lines(curwin->w_cursor.lnum, currsize,
4636 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004638 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 * briefly, and then move it back. After del_lines() the cursor may
4640 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 */
4642 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004644 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 curwin->w_cursor.lnum = t;
4646
4647 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004648 * Set the cursor column:
4649 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004650 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004652 curwin->w_cursor.col =
4653 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 curwin->w_set_curswant = TRUE;
4658
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004659theend:
4660 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004661#if defined(FEAT_COMMENTS) || defined(PROTO)
4662 if (remove_comments)
4663 vim_free(comments);
4664#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004665 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666}
4667
4668#ifdef FEAT_COMMENTS
4669/*
4670 * Return TRUE if the two comment leaders given are the same. "lnum" is
4671 * the first line. White-space is ignored. Note that the whole of
4672 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4673 */
4674 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004675same_leader(
4676 linenr_T lnum,
4677 int leader1_len,
4678 char_u *leader1_flags,
4679 int leader2_len,
4680 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 int idx1 = 0, idx2 = 0;
4683 char_u *p;
4684 char_u *line1;
4685 char_u *line2;
4686
4687 if (leader1_len == 0)
4688 return (leader2_len == 0);
4689
4690 /*
4691 * If first leader has 'f' flag, the lines can be joined only if the
4692 * second line does not have a leader.
4693 * If first leader has 'e' flag, the lines can never be joined.
4694 * If fist leader has 's' flag, the lines can only be joined if there is
4695 * some text after it and the second line has the 'm' flag.
4696 */
4697 if (leader1_flags != NULL)
4698 {
4699 for (p = leader1_flags; *p && *p != ':'; ++p)
4700 {
4701 if (*p == COM_FIRST)
4702 return (leader2_len == 0);
4703 if (*p == COM_END)
4704 return FALSE;
4705 if (*p == COM_START)
4706 {
4707 if (*(ml_get(lnum) + leader1_len) == NUL)
4708 return FALSE;
4709 if (leader2_flags == NULL || leader2_len == 0)
4710 return FALSE;
4711 for (p = leader2_flags; *p && *p != ':'; ++p)
4712 if (*p == COM_MIDDLE)
4713 return TRUE;
4714 return FALSE;
4715 }
4716 }
4717 }
4718
4719 /*
4720 * Get current line and next line, compare the leaders.
4721 * The first line has to be saved, only one line can be locked at a time.
4722 */
4723 line1 = vim_strsave(ml_get(lnum));
4724 if (line1 != NULL)
4725 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004726 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 ;
4728 line2 = ml_get(lnum + 1);
4729 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4730 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004731 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
4733 if (line1[idx1++] != line2[idx2])
4734 break;
4735 }
4736 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004737 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 ++idx1;
4739 }
4740 vim_free(line1);
4741 }
4742 return (idx2 == leader2_len && idx1 == leader1_len);
4743}
4744#endif
4745
4746/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004747 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
4749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004750op_format(
4751 oparg_T *oap,
4752 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 long old_line_count = curbuf->b_ml.ml_line_count;
4755
4756 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4757 * can put it back there. */
4758 curwin->w_cursor = oap->cursor_start;
4759
4760 if (u_save((linenr_T)(oap->start.lnum - 1),
4761 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4762 return;
4763 curwin->w_cursor = oap->start;
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (oap->is_VIsual)
4766 /* When there is no change: need to remove the Visual selection */
4767 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 /* Set '[ mark at the start of the formatted area */
4770 curbuf->b_op_start = oap->start;
4771
4772 /* For "gw" remember the cursor position and put it back below (adjusted
4773 * for joined and split lines). */
4774 if (keep_cursor)
4775 saved_cursor = oap->cursor_start;
4776
Bram Moolenaar81a82092008-03-12 16:27:00 +00004777 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 /*
4780 * Leave the cursor at the first non-blank of the last formatted line.
4781 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4782 * line, so "." will do the next lines.
4783 */
4784 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4785 ++curwin->w_cursor.lnum;
4786 beginline(BL_WHITE | BL_FIX);
4787 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4788 msgmore(old_line_count);
4789
4790 /* put '] mark on the end of the formatted area */
4791 curbuf->b_op_end = curwin->w_cursor;
4792
4793 if (keep_cursor)
4794 {
4795 curwin->w_cursor = saved_cursor;
4796 saved_cursor.lnum = 0;
4797 }
4798
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (oap->is_VIsual)
4800 {
4801 win_T *wp;
4802
4803 FOR_ALL_WINDOWS(wp)
4804 {
4805 if (wp->w_old_cursor_lnum != 0)
4806 {
4807 /* When lines have been inserted or deleted, adjust the end of
4808 * the Visual area to be redrawn. */
4809 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4810 wp->w_old_cursor_lnum += old_line_count;
4811 else
4812 wp->w_old_visual_lnum += old_line_count;
4813 }
4814 }
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816}
4817
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004818#if defined(FEAT_EVAL) || defined(PROTO)
4819/*
4820 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4821 */
4822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004823op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004824{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004825 if (oap->is_VIsual)
4826 /* When there is no change: need to remove the Visual selection */
4827 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004828
Bram Moolenaar700303e2010-07-11 17:35:50 +02004829 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4830 /* As documented: when 'formatexpr' returns non-zero fall back to
4831 * internal formatting. */
4832 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004833}
4834
4835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004836fex_format(
4837 linenr_T lnum,
4838 long count,
4839 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004840{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004841 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4842 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004843 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004844 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004845
4846 /*
4847 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004848 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004849 */
4850 set_vim_var_nr(VV_LNUM, lnum);
4851 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004852 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004853
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004854 /* Make a copy, the option could be changed while calling it. */
4855 fex = vim_strsave(curbuf->b_p_fex);
4856 if (fex == NULL)
4857 return 0;
4858
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004859 /*
4860 * Evaluate the function.
4861 */
4862 if (use_sandbox)
4863 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004864 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004865 if (use_sandbox)
4866 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004867
4868 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004869 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004870
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004871 return r;
4872}
4873#endif
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875/*
4876 * Format "line_count" lines, starting at the cursor position.
4877 * When "line_count" is negative, format until the end of the paragraph.
4878 * Lines after the cursor line are saved for undo, caller must have saved the
4879 * first line.
4880 */
4881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882format_lines(
4883 linenr_T line_count,
4884 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 int max_len;
4887 int is_not_par; /* current line not part of parag. */
4888 int next_is_not_par; /* next line not part of paragraph */
4889 int is_end_par; /* at end of paragraph */
4890 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4891 int next_is_start_par = FALSE;
4892#ifdef FEAT_COMMENTS
4893 int leader_len = 0; /* leader len of current line */
4894 int next_leader_len; /* leader len of next line */
4895 char_u *leader_flags = NULL; /* flags for leader of current line */
4896 char_u *next_leader_flags; /* flags for leader of next line */
4897 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004898 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004901 int second_indent = -1; /* indent for second line (comment
4902 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 int do_second_indent;
4904 int do_number_indent;
4905 int do_trail_white;
4906 int first_par_line = TRUE;
4907 int smd_save;
4908 long count;
4909 int need_set_indent = TRUE; /* set indent of next paragraph */
4910 int force_format = FALSE;
4911 int old_State = State;
4912
4913 /* length of a line to force formatting: 3 * 'tw' */
4914 max_len = comp_textwidth(TRUE) * 3;
4915
4916 /* check for 'q', '2' and '1' in 'formatoptions' */
4917#ifdef FEAT_COMMENTS
4918 do_comments = has_format_option(FO_Q_COMS);
4919#endif
4920 do_second_indent = has_format_option(FO_Q_SECOND);
4921 do_number_indent = has_format_option(FO_Q_NUMBER);
4922 do_trail_white = has_format_option(FO_WHITE_PAR);
4923
4924 /*
4925 * Get info about the previous and current line.
4926 */
4927 if (curwin->w_cursor.lnum > 1)
4928 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4929#ifdef FEAT_COMMENTS
4930 , &leader_len, &leader_flags, do_comments
4931#endif
4932 );
4933 else
4934 is_not_par = TRUE;
4935 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4936#ifdef FEAT_COMMENTS
4937 , &next_leader_len, &next_leader_flags, do_comments
4938#endif
4939 );
4940 is_end_par = (is_not_par || next_is_not_par);
4941 if (!is_end_par && do_trail_white)
4942 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4943
4944 curwin->w_cursor.lnum--;
4945 for (count = line_count; count != 0 && !got_int; --count)
4946 {
4947 /*
4948 * Advance to next paragraph.
4949 */
4950 if (advance)
4951 {
4952 curwin->w_cursor.lnum++;
4953 prev_is_end_par = is_end_par;
4954 is_not_par = next_is_not_par;
4955#ifdef FEAT_COMMENTS
4956 leader_len = next_leader_len;
4957 leader_flags = next_leader_flags;
4958#endif
4959 }
4960
4961 /*
4962 * The last line to be formatted.
4963 */
4964 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4965 {
4966 next_is_not_par = TRUE;
4967#ifdef FEAT_COMMENTS
4968 next_leader_len = 0;
4969 next_leader_flags = NULL;
4970#endif
4971 }
4972 else
4973 {
4974 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4975#ifdef FEAT_COMMENTS
4976 , &next_leader_len, &next_leader_flags, do_comments
4977#endif
4978 );
4979 if (do_number_indent)
4980 next_is_start_par =
4981 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4982 }
4983 advance = TRUE;
4984 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4985 if (!is_end_par && do_trail_white)
4986 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4987
4988 /*
4989 * Skip lines that are not in a paragraph.
4990 */
4991 if (is_not_par)
4992 {
4993 if (line_count < 0)
4994 break;
4995 }
4996 else
4997 {
4998 /*
4999 * For the first line of a paragraph, check indent of second line.
5000 * Don't do this for comments and empty lines.
5001 */
5002 if (first_par_line
5003 && (do_second_indent || do_number_indent)
5004 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005005 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005007 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005008 {
5009#ifdef FEAT_COMMENTS
5010 if (leader_len == 0 && next_leader_len == 0)
5011 {
5012 /* no comment found */
5013#endif
5014 second_indent =
5015 get_indent_lnum(curwin->w_cursor.lnum + 1);
5016#ifdef FEAT_COMMENTS
5017 }
5018 else
5019 {
5020 second_indent = next_leader_len;
5021 do_comments_list = 1;
5022 }
5023#endif
5024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005026 {
5027#ifdef FEAT_COMMENTS
5028 if (leader_len == 0 && next_leader_len == 0)
5029 {
5030 /* no comment found */
5031#endif
5032 second_indent =
5033 get_number_indent(curwin->w_cursor.lnum);
5034#ifdef FEAT_COMMENTS
5035 }
5036 else
5037 {
5038 /* get_number_indent() is now "comment aware"... */
5039 second_indent =
5040 get_number_indent(curwin->w_cursor.lnum);
5041 do_comments_list = 1;
5042 }
5043#endif
5044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046
5047 /*
5048 * When the comment leader changes, it's the end of the paragraph.
5049 */
5050 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5051#ifdef FEAT_COMMENTS
5052 || !same_leader(curwin->w_cursor.lnum,
5053 leader_len, leader_flags,
5054 next_leader_len, next_leader_flags)
5055#endif
5056 )
5057 is_end_par = TRUE;
5058
5059 /*
5060 * If we have got to the end of a paragraph, or the line is
5061 * getting long, format it.
5062 */
5063 if (is_end_par || force_format)
5064 {
5065 if (need_set_indent)
5066 /* replace indent in first line with minimal number of
5067 * tabs and spaces, according to current options */
5068 (void)set_indent(get_indent(), SIN_CHANGED);
5069
5070 /* put cursor on last non-space */
5071 State = NORMAL; /* don't go past end-of-line */
5072 coladvance((colnr_T)MAXCOL);
5073 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5074 dec_cursor();
5075
5076 /* do the formatting, without 'showmode' */
5077 State = INSERT; /* for open_line() */
5078 smd_save = p_smd;
5079 p_smd = FALSE;
5080 insertchar(NUL, INSCHAR_FORMAT
5081#ifdef FEAT_COMMENTS
5082 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005083 + (do_comments && do_comments_list
5084 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005086 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 State = old_State;
5088 p_smd = smd_save;
5089 second_indent = -1;
5090 /* at end of par.: need to set indent of next par. */
5091 need_set_indent = is_end_par;
5092 if (is_end_par)
5093 {
5094 /* When called with a negative line count, break at the
5095 * end of the paragraph. */
5096 if (line_count < 0)
5097 break;
5098 first_par_line = TRUE;
5099 }
5100 force_format = FALSE;
5101 }
5102
5103 /*
5104 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005105 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
5107 if (!is_end_par)
5108 {
5109 advance = FALSE;
5110 curwin->w_cursor.lnum++;
5111 curwin->w_cursor.col = 0;
5112 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005116 {
5117 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005119 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005120 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005122 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5123 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005124 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005125
5126 if (indent > 0)
5127 {
5128 (void)del_bytes(indent, FALSE, FALSE);
5129 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005130 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005131 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005134 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 beep_flush();
5137 break;
5138 }
5139 first_par_line = FALSE;
5140 /* If the line is getting long, format it next time */
5141 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5142 force_format = TRUE;
5143 else
5144 force_format = FALSE;
5145 }
5146 }
5147 line_breakcheck();
5148 }
5149}
5150
5151/*
5152 * Return TRUE if line "lnum" ends in a white character.
5153 */
5154 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
5157 char_u *s = ml_get(lnum);
5158 size_t l;
5159
5160 if (*s == NUL)
5161 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005162 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 * invocation may call function multiple times". */
5164 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005165 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166}
5167
5168/*
5169 * Blank lines, and lines containing only the comment leader, are left
5170 * untouched by the formatting. The function returns TRUE in this
5171 * case. It also returns TRUE when a line starts with the end of a comment
5172 * ('e' in comment flags), so that this line is skipped, and not joined to the
5173 * previous line. A new paragraph starts after a blank line, or when the
5174 * comment leader changes -- webb.
5175 */
5176#ifdef FEAT_COMMENTS
5177 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178fmt_check_par(
5179 linenr_T lnum,
5180 int *leader_len,
5181 char_u **leader_flags,
5182 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 char_u *flags = NULL; /* init for GCC */
5185 char_u *ptr;
5186
5187 ptr = ml_get(lnum);
5188 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005189 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 else
5191 *leader_len = 0;
5192
5193 if (*leader_len > 0)
5194 {
5195 /*
5196 * Search for 'e' flag in comment leader flags.
5197 */
5198 flags = *leader_flags;
5199 while (*flags && *flags != ':' && *flags != COM_END)
5200 ++flags;
5201 }
5202
5203 return (*skipwhite(ptr + *leader_len) == NUL
5204 || (*leader_len > 0 && *flags == COM_END)
5205 || startPS(lnum, NUL, FALSE));
5206}
5207#else
5208 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5212}
5213#endif
5214
5215/*
5216 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5217 * previous line is in the same paragraph. Used for auto-formatting.
5218 */
5219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005220paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 char_u *p;
5223#ifdef FEAT_COMMENTS
5224 int leader_len = 0; /* leader len of current line */
5225 char_u *leader_flags = NULL; /* flags for leader of current line */
5226 int next_leader_len; /* leader len of next line */
5227 char_u *next_leader_flags; /* flags for leader of next line */
5228 int do_comments; /* format comments */
5229#endif
5230
5231 if (lnum <= 1)
5232 return TRUE; /* start of the file */
5233
5234 p = ml_get(lnum - 1);
5235 if (*p == NUL)
5236 return TRUE; /* after empty line */
5237
5238#ifdef FEAT_COMMENTS
5239 do_comments = has_format_option(FO_Q_COMS);
5240#endif
5241 if (fmt_check_par(lnum - 1
5242#ifdef FEAT_COMMENTS
5243 , &leader_len, &leader_flags, do_comments
5244#endif
5245 ))
5246 return TRUE; /* after non-paragraph line */
5247
5248 if (fmt_check_par(lnum
5249#ifdef FEAT_COMMENTS
5250 , &next_leader_len, &next_leader_flags, do_comments
5251#endif
5252 ))
5253 return TRUE; /* "lnum" is not a paragraph line */
5254
5255 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5256 return TRUE; /* missing trailing space in previous line. */
5257
5258 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5259 return TRUE; /* numbered item starts in "lnum". */
5260
5261#ifdef FEAT_COMMENTS
5262 if (!same_leader(lnum - 1, leader_len, leader_flags,
5263 next_leader_len, next_leader_flags))
5264 return TRUE; /* change of comment leader. */
5265#endif
5266
5267 return FALSE;
5268}
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270/*
5271 * prepare a few things for block mode yank/delete/tilde
5272 *
5273 * for delete:
5274 * - textlen includes the first/last char to be (partly) deleted
5275 * - start/endspaces is the number of columns that are taken by the
5276 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005277 * deleted.
5278 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 * - textlen includes the first/last char to be wholly yanked
5280 * - start/endspaces is the number of columns of the first/last yanked char
5281 * that are to be yanked.
5282 */
5283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284block_prep(
5285 oparg_T *oap,
5286 struct block_def *bdp,
5287 linenr_T lnum,
5288 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290 int incr = 0;
5291 char_u *pend;
5292 char_u *pstart;
5293 char_u *line;
5294 char_u *prev_pstart;
5295 char_u *prev_pend;
5296
5297 bdp->startspaces = 0;
5298 bdp->endspaces = 0;
5299 bdp->textlen = 0;
5300 bdp->start_vcol = 0;
5301 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 bdp->is_short = FALSE;
5303 bdp->is_oneChar = FALSE;
5304 bdp->pre_whitesp = 0;
5305 bdp->pre_whitesp_c = 0;
5306 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 bdp->start_char_vcols = 0;
5308
5309 line = ml_get(lnum);
5310 pstart = line;
5311 prev_pstart = line;
5312 while (bdp->start_vcol < oap->start_vcol && *pstart)
5313 {
5314 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005315 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005317 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 bdp->pre_whitesp += incr;
5320 bdp->pre_whitesp_c++;
5321 }
5322 else
5323 {
5324 bdp->pre_whitesp = 0;
5325 bdp->pre_whitesp_c = 0;
5326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005328 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 bdp->start_char_vcols = incr;
5331 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5332 {
5333 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (!is_del || oap->op_type == OP_APPEND)
5336 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5337 }
5338 else
5339 {
5340 /* notice: this converts partly selected Multibyte characters to
5341 * spaces, too. */
5342 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5343 if (is_del && bdp->startspaces)
5344 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5345 pend = pstart;
5346 bdp->end_vcol = bdp->start_vcol;
5347 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (oap->op_type == OP_INSERT)
5351 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5352 else if (oap->op_type == OP_APPEND)
5353 {
5354 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5355 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5356 }
5357 else
5358 {
5359 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5360 if (is_del && oap->op_type != OP_LSHIFT)
5361 {
5362 /* just putting the sum of those two into
5363 * bdp->startspaces doesn't work for Visual replace,
5364 * so we have to split the tab in two */
5365 bdp->startspaces = bdp->start_char_vcols
5366 - (bdp->start_vcol - oap->start_vcol);
5367 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5368 }
5369 }
5370 }
5371 else
5372 {
5373 prev_pend = pend;
5374 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5375 {
5376 /* Count a tab for what it's worth (if list mode not on) */
5377 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005378 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 bdp->end_vcol += incr;
5380 }
5381 if (bdp->end_vcol <= oap->end_vcol
5382 && (!is_del
5383 || oap->op_type == OP_APPEND
5384 || oap->op_type == OP_REPLACE)) /* line too short */
5385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 /* Alternative: include spaces to fill up the block.
5388 * Disadvantage: can lead to trailing spaces when the line is
5389 * short where the text is put */
5390 /* if (!is_del || oap->op_type == OP_APPEND) */
5391 if (oap->op_type == OP_APPEND || virtual_op)
5392 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005393 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else
5395 bdp->endspaces = 0; /* replace doesn't add characters */
5396 }
5397 else if (bdp->end_vcol > oap->end_vcol)
5398 {
5399 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5400 if (!is_del && bdp->endspaces)
5401 {
5402 bdp->endspaces = incr - bdp->endspaces;
5403 if (pend != pstart)
5404 pend = prev_pend;
5405 }
5406 }
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 if (is_del && bdp->startspaces)
5410 pstart = prev_pstart;
5411 bdp->textlen = (int)(pend - pstart);
5412 }
5413 bdp->textcol = (colnr_T) (pstart - line);
5414 bdp->textstart = pstart;
5415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005418 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005420 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421op_addsub(
5422 oparg_T *oap,
5423 linenr_T Prenum1, /* Amount of add/subtract */
5424 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005426 pos_T pos;
5427 struct block_def bd;
5428 int change_cnt = 0;
5429 linenr_T amount = Prenum1;
5430
Bram Moolenaar6b731882018-11-16 20:54:47 +01005431 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01005432 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01005433 // the call to changed_lines().
5434#ifdef FEAT_FOLDING
5435 disable_fold_update++;
5436#endif
5437
Bram Moolenaard79e5502016-01-10 22:13:02 +01005438 if (!VIsual_active)
5439 {
5440 pos = curwin->w_cursor;
5441 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005442 {
5443#ifdef FEAT_FOLDING
5444 disable_fold_update--;
5445#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005446 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005447 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005448 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005449#ifdef FEAT_FOLDING
5450 disable_fold_update--;
5451#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005452 if (change_cnt)
5453 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5454 }
5455 else
5456 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005457 int one_change;
5458 int length;
5459 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005460
5461 if (u_save((linenr_T)(oap->start.lnum - 1),
5462 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005463 {
5464#ifdef FEAT_FOLDING
5465 disable_fold_update--;
5466#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005467 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005468 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005469
5470 pos = oap->start;
5471 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5472 {
5473 if (oap->block_mode) /* Visual block mode */
5474 {
5475 block_prep(oap, &bd, pos.lnum, FALSE);
5476 pos.col = bd.textcol;
5477 length = bd.textlen;
5478 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005479 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005480 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005481 curwin->w_cursor.col = 0;
5482 pos.col = 0;
5483 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5484 }
5485 else /* oap->motion_type == MCHAR */
5486 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005487 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005488 dec(&(oap->end));
5489 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5490 pos.col = 0;
5491 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005492 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005493 pos.col += oap->start.col;
5494 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005495 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005496 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005497 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005498 length = (int)STRLEN(ml_get(oap->end.lnum));
5499 if (oap->end.col >= length)
5500 oap->end.col = length - 1;
5501 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005502 }
5503 }
5504 one_change = do_addsub(oap->op_type, &pos, length, amount);
5505 if (one_change)
5506 {
5507 /* Remember the start position of the first change. */
5508 if (change_cnt == 0)
5509 startpos = curbuf->b_op_start;
5510 ++change_cnt;
5511 }
5512
5513#ifdef FEAT_NETBEANS_INTG
5514 if (netbeans_active() && one_change)
5515 {
5516 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5517
5518 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5519 netbeans_inserted(curbuf, pos.lnum, pos.col,
5520 &ptr[pos.col], length);
5521 }
5522#endif
5523 if (g_cmd && one_change)
5524 amount += Prenum1;
5525 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005526
5527#ifdef FEAT_FOLDING
5528 disable_fold_update--;
5529#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005530 if (change_cnt)
5531 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5532
5533 if (!change_cnt && oap->is_VIsual)
5534 /* No change: need to remove the Visual selection */
5535 redraw_curbuf_later(INVERTED);
5536
5537 /* Set '[ mark if something changed. Keep the last end
5538 * position from do_addsub(). */
5539 if (change_cnt > 0)
5540 curbuf->b_op_start = startpos;
5541
5542 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005543 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005544 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005545 }
5546}
5547
5548/*
5549 * Add or subtract 'Prenum1' from a number in a line
5550 * op_type is OP_NR_ADD or OP_NR_SUB
5551 *
5552 * Returns TRUE if some character was changed.
5553 */
5554 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005555do_addsub(
5556 int op_type,
5557 pos_T *pos,
5558 int length,
5559 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 int col;
5562 char_u *buf1;
5563 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005564 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005566 uvarnumber_T n;
5567 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *ptr;
5569 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 int todel;
5571 int dohex;
5572 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005573 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 int doalp;
5575 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005577 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005578 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005579 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005580 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005581 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005582 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005583 pos_T startpos;
5584 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5587 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005588 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5590
Bram Moolenaard79e5502016-01-10 22:13:02 +01005591 curwin->w_cursor = *pos;
5592 ptr = ml_get(pos->lnum);
5593 col = pos->col;
5594
5595 if (*ptr == NUL)
5596 goto theend;
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 /*
5599 * First check if we are on a hexadecimal number, after the "0x".
5600 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005601 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005603 if (dobin)
5604 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005605 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005606 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005607 if (has_mbyte)
5608 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005609 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005610
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005611 if (dohex)
5612 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005613 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005614 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005615 if (has_mbyte)
5616 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005617 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005618
5619 if ( dobin
5620 && dohex
5621 && ! ((col > 0
5622 && (ptr[col] == 'X'
5623 || ptr[col] == 'x')
5624 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005625 && (!has_mbyte ||
5626 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005627 && vim_isxdigit(ptr[col + 1]))))
5628 {
5629
5630 /* In case of binary/hexadecimal pattern overlap match, rescan */
5631
Bram Moolenaard79e5502016-01-10 22:13:02 +01005632 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005633
5634 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005635 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005636 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005637 if (has_mbyte)
5638 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005639 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005640 }
5641
5642 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005643 && col > 0
5644 && (ptr[col] == 'X'
5645 || ptr[col] == 'x')
5646 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005647 && (!has_mbyte ||
5648 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005649 && vim_isxdigit(ptr[col + 1])) ||
5650 ( dobin
5651 && col > 0
5652 && (ptr[col] == 'B'
5653 || ptr[col] == 'b')
5654 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005655 && (!has_mbyte ||
5656 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005657 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005658 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005659 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005660 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005661 if (has_mbyte)
5662 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005663 }
5664 else
5665 {
5666 /*
5667 * Search forward and then backward to find the start of number.
5668 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005669 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005670
5671 while (ptr[col] != NUL
5672 && !vim_isdigit(ptr[col])
5673 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005674 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005675
5676 while (col > 0
5677 && vim_isdigit(ptr[col - 1])
5678 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005679 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005680 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005681 if (has_mbyte)
5682 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005683 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005684 }
5685 }
5686
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005687 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005688 {
5689 while (ptr[col] != NUL && length > 0
5690 && !vim_isdigit(ptr[col])
5691 && !(doalp && ASCII_ISALPHA(ptr[col])))
5692 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005693 int mb_len = MB_PTR2LEN(ptr + col);
5694
5695 col += mb_len;
5696 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005697 }
5698
5699 if (length == 0)
5700 goto theend;
5701
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005702 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005703 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005704 {
5705 negative = TRUE;
5706 was_positive = FALSE;
5707 }
5708 }
5709
5710 /*
5711 * If a number was found, and saving for undo works, replace the number.
5712 */
5713 firstdigit = ptr[col];
5714 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5715 {
5716 beep_flush();
5717 goto theend;
5718 }
5719
5720 if (doalp && ASCII_ISALPHA(firstdigit))
5721 {
5722 /* decrement or increment alphabetic character */
5723 if (op_type == OP_NR_SUB)
5724 {
5725 if (CharOrd(firstdigit) < Prenum1)
5726 {
5727 if (isupper(firstdigit))
5728 firstdigit = 'A';
5729 else
5730 firstdigit = 'a';
5731 }
5732 else
5733#ifdef EBCDIC
5734 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5735#else
5736 firstdigit -= Prenum1;
5737#endif
5738 }
5739 else
5740 {
5741 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5742 {
5743 if (isupper(firstdigit))
5744 firstdigit = 'Z';
5745 else
5746 firstdigit = 'z';
5747 }
5748 else
5749#ifdef EBCDIC
5750 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5751#else
5752 firstdigit += Prenum1;
5753#endif
5754 }
5755 curwin->w_cursor.col = col;
5756 if (!did_change)
5757 startpos = curwin->w_cursor;
5758 did_change = TRUE;
5759 (void)del_char(FALSE);
5760 ins_char(firstdigit);
5761 endpos = curwin->w_cursor;
5762 curwin->w_cursor.col = col;
5763 }
5764 else
5765 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005766 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005767 && (!has_mbyte ||
5768 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005769 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005770 {
5771 /* negative number */
5772 --col;
5773 negative = TRUE;
5774 }
5775 /* get the number value (unsigned) */
5776 if (visual && VIsual_mode != 'V')
5777 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5778 ? (int)STRLEN(ptr) - col
5779 : length);
5780
5781 vim_str2nr(ptr + col, &pre, &length,
5782 0 + (dobin ? STR2NR_BIN : 0)
5783 + (dooct ? STR2NR_OCT : 0)
5784 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005785 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005786
5787 /* ignore leading '-' for hex and octal and bin numbers */
5788 if (pre && negative)
5789 {
5790 ++col;
5791 --length;
5792 negative = FALSE;
5793 }
5794 /* add or subtract */
5795 subtract = FALSE;
5796 if (op_type == OP_NR_SUB)
5797 subtract ^= TRUE;
5798 if (negative)
5799 subtract ^= TRUE;
5800
5801 oldn = n;
5802 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005803 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005804 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005805 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005806 /* handle wraparound for decimal numbers */
5807 if (!pre)
5808 {
5809 if (subtract)
5810 {
5811 if (n > oldn)
5812 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005813 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005814 negative ^= TRUE;
5815 }
5816 }
5817 else
5818 {
5819 /* add */
5820 if (n < oldn)
5821 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005822 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005823 negative ^= TRUE;
5824 }
5825 }
5826 if (n == 0)
5827 negative = FALSE;
5828 }
5829
5830 if (visual && !was_positive && !negative && col > 0)
5831 {
5832 /* need to remove the '-' */
5833 col--;
5834 length++;
5835 }
5836
5837 /*
5838 * Delete the old number.
5839 */
5840 curwin->w_cursor.col = col;
5841 if (!did_change)
5842 startpos = curwin->w_cursor;
5843 did_change = TRUE;
5844 todel = length;
5845 c = gchar_cursor();
5846 /*
5847 * Don't include the '-' in the length, only the length of the
5848 * part after it is kept the same.
5849 */
5850 if (c == '-')
5851 --length;
5852 while (todel-- > 0)
5853 {
5854 if (c < 0x100 && isalpha(c))
5855 {
5856 if (isupper(c))
5857 hexupper = TRUE;
5858 else
5859 hexupper = FALSE;
5860 }
5861 /* del_char() will mark line needing displaying */
5862 (void)del_char(FALSE);
5863 c = gchar_cursor();
5864 }
5865
5866 /*
5867 * Prepare the leading characters in buf1[].
5868 * When there are many leading zeros it could be very long.
5869 * Allocate a bit too much.
5870 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005871 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005872 if (buf1 == NULL)
5873 goto theend;
5874 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005875 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005876 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01005877 if (pre)
5878 {
5879 *ptr++ = '0';
5880 --length;
5881 }
5882 if (pre == 'b' || pre == 'B' ||
5883 pre == 'x' || pre == 'X')
5884 {
5885 *ptr++ = pre;
5886 --length;
5887 }
5888
5889 /*
5890 * Put the number characters in buf2[].
5891 */
5892 if (pre == 'b' || pre == 'B')
5893 {
5894 int i;
5895 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005896 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005897
5898 /* leading zeros */
5899 for (bit = bits; bit > 0; bit--)
5900 if ((n >> (bit - 1)) & 0x1) break;
5901
5902 for (i = 0; bit > 0; bit--)
5903 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5904
5905 buf2[i] = '\0';
5906 }
5907 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005908 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005909 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005910 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02005911 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005912 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005913 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02005914 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005915 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 else
Bram Moolenaarea391762018-04-08 13:07:22 +02005917 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005918 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005919 length -= (int)STRLEN(buf2);
5920
5921 /*
5922 * Adjust number of zeros to the new number of digits, so the
5923 * total length of the number remains the same.
5924 * Don't do this when
5925 * the result may look like an octal number.
5926 */
5927 if (firstdigit == '0' && !(dooct && pre == 0))
5928 while (length-- > 0)
5929 *ptr++ = '0';
5930 *ptr = NUL;
5931 STRCAT(buf1, buf2);
5932 ins_str(buf1); /* insert the new number */
5933 vim_free(buf1);
5934 endpos = curwin->w_cursor;
5935 if (did_change && curwin->w_cursor.col)
5936 --curwin->w_cursor.col;
5937 }
5938
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005939 if (did_change)
5940 {
5941 /* set the '[ and '] marks */
5942 curbuf->b_op_start = startpos;
5943 curbuf->b_op_end = endpos;
5944 if (curbuf->b_op_end.col > 0)
5945 --curbuf->b_op_end.col;
5946 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005947
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005948theend:
5949 if (visual)
5950 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005951 else if (did_change)
5952 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005953
Bram Moolenaard79e5502016-01-10 22:13:02 +01005954 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}
5956
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5958/*
5959 * SELECTION / PRIMARY ('*')
5960 *
5961 * Text selection stuff that uses the GUI selection register '*'. When using a
5962 * GUI this may be text from another window, otherwise it is the last text we
5963 * had highlighted with VIsual mode. With mouse support, clicking the middle
5964 * button performs the paste, otherwise you will need to do <"*p>. "
5965 * If not under X, it is synonymous with the clipboard register '+'.
5966 *
5967 * X CLIPBOARD ('+')
5968 *
5969 * Text selection stuff that uses the GUI clipboard register '+'.
5970 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5971 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5972 * otherwise you will need to do <"+p>. "
5973 * If not under X, it is synonymous with the selection register '*'.
5974 */
5975
5976/*
5977 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01005978 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 * only exist while the owning application exists, so we write to the
5980 * permanent (while X runs) store CUT_BUFFER0.
5981 * Dump the CLIPBOARD selection if we own it (it's logically the more
5982 * 'permanent' of the two), otherwise the PRIMARY one.
5983 * For now, use a hard-coded sanity limit of 1Mb of data.
5984 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02005985#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005987x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 Display *dpy;
5990 char_u *str = NULL;
5991 long_u len = 0;
5992 int motion_type = -1;
5993
5994# ifdef FEAT_GUI
5995 if (gui.in_use)
5996 dpy = X_DISPLAY;
5997 else
5998# endif
5999# ifdef FEAT_XCLIPBOARD
6000 dpy = xterm_dpy;
6001# else
6002 return;
6003# endif
6004
6005 /* Get selection to export */
6006 if (clip_plus.owned)
6007 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6008 else if (clip_star.owned)
6009 motion_type = clip_convert_selection(&str, &len, &clip_star);
6010
6011 /* Check it's OK */
6012 if (dpy != NULL && str != NULL && motion_type >= 0
6013 && len < 1024*1024 && len > 0)
6014 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006015 int ok = TRUE;
6016
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006017 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6018 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6019 * encoding conversion usually doesn't work, so keep the text as-is.
6020 */
6021 if (has_mbyte)
6022 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006023 vimconv_T vc;
6024
6025 vc.vc_type = CONV_NONE;
6026 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6027 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006028 int intlen = len;
6029 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006030
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006031 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006032 conv_str = string_convert(&vc, str, &intlen);
6033 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006034 if (conv_str != NULL)
6035 {
6036 vim_free(str);
6037 str = conv_str;
6038 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006039 else
6040 {
6041 ok = FALSE;
6042 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006043 convert_setup(&vc, NULL, NULL);
6044 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006045 else
6046 {
6047 ok = FALSE;
6048 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006049 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006050
6051 /* Do not store the string if conversion failed. Better to use any
6052 * other selection than garbled text. */
6053 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006054 {
6055 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6056 XFlush(dpy);
6057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059
6060 vim_free(str);
6061}
6062#endif
6063
6064 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006065clip_free_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006067 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
6069 if (cbd == &clip_plus)
6070 y_current = &y_regs[PLUS_REGISTER];
6071 else
6072 y_current = &y_regs[STAR_REGISTER];
6073 free_yank_all();
6074 y_current->y_size = 0;
6075 y_current = y_ptr;
6076}
6077
6078/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006079 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
6081 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006082clip_get_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006084 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 pos_T old_visual;
6087 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 colnr_T old_curswant;
6089 int old_set_curswant;
6090 pos_T old_op_start, old_op_end;
6091 oparg_T oa;
6092 cmdarg_T ca;
6093
6094 if (cbd->owned)
6095 {
6096 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6097 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6098 return;
6099
6100 /* Get the text between clip_star.start & clip_star.end */
6101 old_y_previous = y_previous;
6102 old_y_current = y_current;
6103 old_cursor = curwin->w_cursor;
6104 old_curswant = curwin->w_curswant;
6105 old_set_curswant = curwin->w_set_curswant;
6106 old_op_start = curbuf->b_op_start;
6107 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 old_visual = VIsual;
6109 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 clear_oparg(&oa);
6111 oa.regname = (cbd == &clip_plus ? '+' : '*');
6112 oa.op_type = OP_YANK;
6113 vim_memset(&ca, 0, sizeof(ca));
6114 ca.oap = &oa;
6115 ca.cmdchar = 'y';
6116 ca.count1 = 1;
6117 ca.retval = CA_NO_ADJ_OP_END;
6118 do_pending_operator(&ca, 0, TRUE);
6119 y_previous = old_y_previous;
6120 y_current = old_y_current;
6121 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006122 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 curwin->w_curswant = old_curswant;
6124 curwin->w_set_curswant = old_set_curswant;
6125 curbuf->b_op_start = old_op_start;
6126 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 VIsual = old_visual;
6128 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006130 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 clip_free_selection(cbd);
6133
6134 /* Try to get selected text from another window */
6135 clip_gen_request_selection(cbd);
6136 }
6137}
6138
Bram Moolenaard44347f2011-06-19 01:14:29 +02006139/*
6140 * Convert from the GUI selection string into the '*'/'+' register.
6141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143clip_yank_selection(
6144 int type,
6145 char_u *str,
6146 long len,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006147 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006149 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 if (cbd == &clip_plus)
6152 y_ptr = &y_regs[PLUS_REGISTER];
6153 else
6154 y_ptr = &y_regs[STAR_REGISTER];
6155
6156 clip_free_selection(cbd);
6157
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006158 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159}
6160
6161/*
6162 * Convert the '*'/'+' register into a GUI selection string returned in *str
6163 * with length *len.
6164 * Returns the motion type, or -1 for failure.
6165 */
6166 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006167clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 char_u *p;
6170 int lnum;
6171 int i, j;
6172 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006173 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175 if (cbd == &clip_plus)
6176 y_ptr = &y_regs[PLUS_REGISTER];
6177 else
6178 y_ptr = &y_regs[STAR_REGISTER];
6179
6180#ifdef USE_CRNL
6181 eolsize = 2;
6182#else
6183 eolsize = 1;
6184#endif
6185
6186 *str = NULL;
6187 *len = 0;
6188 if (y_ptr->y_array == NULL)
6189 return -1;
6190
6191 for (i = 0; i < y_ptr->y_size; i++)
6192 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6193
6194 /*
6195 * Don't want newline character at end of last line if we're in MCHAR mode.
6196 */
6197 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6198 *len -= eolsize;
6199
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006200 p = *str = alloc(*len + 1); // add one to avoid zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (p == NULL)
6202 return -1;
6203 lnum = 0;
6204 for (i = 0, j = 0; i < (int)*len; i++, j++)
6205 {
6206 if (y_ptr->y_array[lnum][j] == '\n')
6207 p[i] = NUL;
6208 else if (y_ptr->y_array[lnum][j] == NUL)
6209 {
6210#ifdef USE_CRNL
6211 p[i++] = '\r';
6212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 p[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 lnum++;
6215 j = -1;
6216 }
6217 else
6218 p[i] = y_ptr->y_array[lnum][j];
6219 }
6220 return y_ptr->y_type;
6221}
6222
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224/*
6225 * If we have written to a clipboard register, send the text to the clipboard.
6226 */
6227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006228may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6231 {
6232 clip_own_selection(&clip_star);
6233 clip_gen_set_selection(&clip_star);
6234 }
6235 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6236 {
6237 clip_own_selection(&clip_plus);
6238 clip_gen_set_selection(&clip_plus);
6239 }
6240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242#endif /* FEAT_CLIPBOARD || PROTO */
6243
6244
6245#if defined(FEAT_DND) || defined(PROTO)
6246/*
6247 * Replace the contents of the '~' register with str.
6248 */
6249 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006250dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006252 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
6254 curr = y_current;
6255 y_current = &y_regs[TILDE_REGISTER];
6256 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006257 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 y_current = curr;
6259}
6260#endif
6261
6262
6263#if defined(FEAT_EVAL) || defined(PROTO)
6264/*
6265 * Return the type of a register.
6266 * Used for getregtype()
6267 * Returns MAUTO for error.
6268 */
6269 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006270get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 switch (regname)
6273 {
6274 case '%': /* file name */
6275 case '#': /* alternate file name */
6276 case '=': /* expression */
6277 case ':': /* last command line */
6278 case '/': /* last search-pattern */
6279 case '.': /* last inserted text */
6280#ifdef FEAT_SEARCHPATH
6281 case Ctrl_F: /* Filename under cursor */
6282 case Ctrl_P: /* Path under cursor, expand via "path" */
6283#endif
6284 case Ctrl_W: /* word under cursor */
6285 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6286 case '_': /* black hole: always empty */
6287 return MCHAR;
6288 }
6289
6290#ifdef FEAT_CLIPBOARD
6291 regname = may_get_selection(regname);
6292#endif
6293
Bram Moolenaar32b92012014-01-14 12:33:36 +01006294 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006295 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 get_yank_register(regname, FALSE);
6298
6299 if (y_current->y_array != NULL)
6300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (reglen != NULL && y_current->y_type == MBLOCK)
6302 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 return y_current->y_type;
6304 }
6305 return MAUTO;
6306}
6307
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006308/*
6309 * When "flags" has GREG_LIST return a list with text "s".
6310 * Otherwise just return "s".
6311 */
6312 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006313getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006314{
6315 if (flags & GREG_LIST)
6316 {
6317 list_T *list = list_alloc();
6318
6319 if (list != NULL)
6320 {
6321 if (list_append_string(list, NULL, -1) == FAIL)
6322 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006323 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006324 return NULL;
6325 }
6326 list->lv_first->li_tv.vval.v_string = s;
6327 }
6328 return (char_u *)list;
6329 }
6330 return s;
6331}
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333/*
6334 * Return the contents of a register as a single allocated string.
6335 * Used for "@r" in expressions and for getreg().
6336 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006337 * Flags:
6338 * GREG_NO_EXPR Do not allow expression register
6339 * GREG_EXPR_SRC For the expression register: return expression itself,
6340 * not the result of its evaluation.
6341 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 */
6343 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 long i;
6347 char_u *retval;
6348 int allocated;
6349 long len;
6350
6351 /* Don't allow using an expression register inside an expression */
6352 if (regname == '=')
6353 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006354 if (flags & GREG_NO_EXPR)
6355 return NULL;
6356 if (flags & GREG_EXPR_SRC)
6357 return getreg_wrap_one_line(get_expr_line_src(), flags);
6358 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360
6361 if (regname == '@') /* "@@" is used for unnamed register */
6362 regname = '"';
6363
6364 /* check for valid regname */
6365 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6366 return NULL;
6367
6368#ifdef FEAT_CLIPBOARD
6369 regname = may_get_selection(regname);
6370#endif
6371
6372 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6373 {
6374 if (retval == NULL)
6375 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006376 if (allocated)
6377 return getreg_wrap_one_line(retval, flags);
6378 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380
6381 get_yank_register(regname, FALSE);
6382 if (y_current->y_array == NULL)
6383 return NULL;
6384
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006385 if (flags & GREG_LIST)
6386 {
6387 list_T *list = list_alloc();
6388 int error = FALSE;
6389
6390 if (list == NULL)
6391 return NULL;
6392 for (i = 0; i < y_current->y_size; ++i)
6393 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6394 error = TRUE;
6395 if (error)
6396 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006397 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006398 return NULL;
6399 }
6400 return (char_u *)list;
6401 }
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 /*
6404 * Compute length of resulting string.
6405 */
6406 len = 0;
6407 for (i = 0; i < y_current->y_size; ++i)
6408 {
6409 len += (long)STRLEN(y_current->y_array[i]);
6410 /*
6411 * Insert a newline between lines and after last line if
6412 * y_type is MLINE.
6413 */
6414 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6415 ++len;
6416 }
6417
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006418 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420 /*
6421 * Copy the lines of the yank register into the string.
6422 */
6423 if (retval != NULL)
6424 {
6425 len = 0;
6426 for (i = 0; i < y_current->y_size; ++i)
6427 {
6428 STRCPY(retval + len, y_current->y_array[i]);
6429 len += (long)STRLEN(retval + len);
6430
6431 /*
6432 * Insert a NL between lines and after the last line if y_type is
6433 * MLINE.
6434 */
6435 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6436 retval[len++] = '\n';
6437 }
6438 retval[len] = NUL;
6439 }
6440
6441 return retval;
6442}
6443
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006444 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006445init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006446 int name,
6447 yankreg_T **old_y_previous,
6448 yankreg_T **old_y_current,
6449 int must_append,
6450 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006451{
6452 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6453 {
6454 emsg_invreg(name);
6455 return FAIL;
6456 }
6457
6458 /* Don't want to change the current (unnamed) register */
6459 *old_y_previous = y_previous;
6460 *old_y_current = y_current;
6461
6462 get_yank_register(name, TRUE);
6463 if (!y_append && !must_append)
6464 free_yank_all();
6465 return OK;
6466}
6467
6468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006470 int name,
6471 yankreg_T *old_y_previous,
6472 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006473{
6474# ifdef FEAT_CLIPBOARD
6475 /* Send text of clipboard register to the clipboard. */
6476 may_set_selection();
6477# endif
6478
6479 /* ':let @" = "val"' should change the meaning of the "" register */
6480 if (name != '"')
6481 y_previous = old_y_previous;
6482 y_current = old_y_current;
6483}
6484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485/*
6486 * Store string "str" in register "name".
6487 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6488 * If "must_append" is TRUE, always append to the register. Otherwise append
6489 * if "name" is an uppercase letter.
6490 * Note: "maxlen" and "must_append" don't work for the "/" register.
6491 * Careful: 'str' is modified, you may have to use a copy!
6492 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6493 */
6494 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006495write_reg_contents(
6496 int name,
6497 char_u *str,
6498 int maxlen,
6499 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
6501 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6502}
6503
6504 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505write_reg_contents_lst(
6506 int name,
6507 char_u **strings,
6508 int maxlen UNUSED,
6509 int must_append,
6510 int yank_type,
6511 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006512{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006513 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006514
6515 if (name == '/'
6516#ifdef FEAT_EVAL
6517 || name == '='
6518#endif
6519 )
6520 {
6521 char_u *s;
6522
6523 if (strings[0] == NULL)
6524 s = (char_u *)"";
6525 else if (strings[1] != NULL)
6526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006527 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006528 "contain two or more lines"));
6529 return;
6530 }
6531 else
6532 s = strings[0];
6533 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6534 return;
6535 }
6536
6537 if (name == '_') /* black hole: nothing to do */
6538 return;
6539
6540 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6541 &yank_type) == FAIL)
6542 return;
6543
6544 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6545
6546 finish_write_reg(name, old_y_previous, old_y_current);
6547}
6548
6549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006550write_reg_contents_ex(
6551 int name,
6552 char_u *str,
6553 int maxlen,
6554 int must_append,
6555 int yank_type,
6556 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006558 yankreg_T *old_y_previous, *old_y_current;
6559 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaare7566042005-06-17 22:00:15 +00006561 if (maxlen >= 0)
6562 len = maxlen;
6563 else
6564 len = (long)STRLEN(str);
6565
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 /* Special case: '/' search pattern */
6567 if (name == '/')
6568 {
6569 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6570 return;
6571 }
6572
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006573 if (name == '#')
6574 {
6575 buf_T *buf;
6576
6577 if (VIM_ISDIGIT(*str))
6578 {
6579 int num = atoi((char *)str);
6580
6581 buf = buflist_findnr(num);
6582 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006583 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006584 }
6585 else
6586 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6587 TRUE, FALSE, FALSE));
6588 if (buf == NULL)
6589 return;
6590 curwin->w_alt_fnum = buf->b_fnum;
6591 return;
6592 }
6593
Bram Moolenaare7566042005-06-17 22:00:15 +00006594#ifdef FEAT_EVAL
6595 if (name == '=')
6596 {
6597 char_u *p, *s;
6598
6599 p = vim_strnsave(str, (int)len);
6600 if (p == NULL)
6601 return;
6602 if (must_append)
6603 {
6604 s = concat_str(get_expr_line_src(), p);
6605 vim_free(p);
6606 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006607 }
6608 set_expr_line(p);
6609 return;
6610 }
6611#endif
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 if (name == '_') /* black hole: nothing to do */
6614 return;
6615
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006616 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6617 &yank_type) == FAIL)
6618 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006620 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006622 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624#endif /* FEAT_EVAL */
6625
6626#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6627/*
6628 * Put a string into a register. When the register is not empty, the string
6629 * is appended.
6630 */
6631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006632str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006633 yankreg_T *y_ptr, /* pointer to yank register */
6634 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6635 char_u *str, /* string to put in register */
6636 long len, /* length of string */
6637 long blocklen, /* width of Visual block */
6638 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006640 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 int lnum;
6642 long start;
6643 long i;
6644 int extra;
6645 int newlines; /* number of lines added */
6646 int extraline = 0; /* extra line at the end */
6647 int append = FALSE; /* append to last line in register */
6648 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006649 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006653 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 y_ptr->y_size = 0;
6655
Bram Moolenaard44347f2011-06-19 01:14:29 +02006656 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006657 type = ((str_list || (len > 0 && (str[len - 1] == NL
6658 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006659 ? MLINE : MCHAR);
6660 else
6661 type = yank_type;
6662
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 /*
6664 * Count the number of lines within the string
6665 */
6666 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006667 if (str_list)
6668 {
6669 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006674 for (i = 0; i < len; i++)
6675 if (str[i] == '\n')
6676 ++newlines;
6677 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6678 {
6679 extraline = 1;
6680 ++newlines; /* count extra newline at the end */
6681 }
6682 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6683 {
6684 append = TRUE;
6685 --newlines; /* uncount newline when appending first line */
6686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006689 /* Without any lines make the register empty. */
6690 if (y_ptr->y_size + newlines == 0)
6691 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006692 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006693 return;
6694 }
6695
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 /*
6697 * Allocate an array to hold the pointers to the new register lines.
6698 * If the register was not empty, move the existing lines to the new array.
6699 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006700 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (pp == NULL) /* out of memory */
6702 return;
6703 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6704 pp[lnum] = y_ptr->y_array[lnum];
6705 vim_free(y_ptr->y_array);
6706 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709 /*
6710 * Find the end of each line and save it into the array.
6711 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006712 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006714 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6715 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006716 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006717 pp[lnum] = vim_strnsave(*ss, i);
6718 if (i > maxlen)
6719 maxlen = i;
6720 }
6721 }
6722 else
6723 {
6724 for (start = 0; start < len + extraline; start += i + 1)
6725 {
6726 for (i = start; i < len; ++i) /* find the end of the line */
6727 if (str[i] == '\n')
6728 break;
6729 i -= start; /* i is now length of line */
6730 if (i > maxlen)
6731 maxlen = i;
6732 if (append)
6733 {
6734 --lnum;
6735 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6736 }
6737 else
6738 extra = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006739 s = alloc(i + extra + 1);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006740 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006742 if (extra)
6743 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6744 if (append)
6745 vim_free(y_ptr->y_array[lnum]);
6746 if (i)
6747 mch_memmove(s + extra, str + start, (size_t)i);
6748 extra += i;
6749 s[extra] = NUL;
6750 y_ptr->y_array[lnum++] = s;
6751 while (--extra >= 0)
6752 {
6753 if (*s == NUL)
6754 *s = '\n'; /* replace NUL with newline */
6755 ++s;
6756 }
6757 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
6760 y_ptr->y_type = type;
6761 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (type == MBLOCK)
6763 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6764 else
6765 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006766#ifdef FEAT_VIMINFO
6767 y_ptr->y_time_set = vim_time();
6768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6771
6772 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 vim_memset(oap, 0, sizeof(oparg_T));
6776}
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006779 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 *
6781 * "Words" are counted by looking for boundaries between non-space and
6782 * space characters. (it seems to produce results that match 'wc'.)
6783 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006784 * Return value is byte count; word count for the line is added to "*wc".
6785 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 *
6787 * The function will only examine the first "limit" characters in the
6788 * line, stopping if it encounters an end-of-line (NUL byte). In that
6789 * case, eol_size will be added to the character count to account for
6790 * the size of the EOL character.
6791 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006792 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793line_count_info(
6794 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006795 varnumber_T *wc,
6796 varnumber_T *cc,
6797 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01006798 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006800 varnumber_T i;
6801 varnumber_T words = 0;
6802 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 int is_word = 0;
6804
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006805 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 if (is_word)
6808 {
6809 if (vim_isspace(line[i]))
6810 {
6811 words++;
6812 is_word = 0;
6813 }
6814 }
6815 else if (!vim_isspace(line[i]))
6816 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006817 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006818 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820
6821 if (is_word)
6822 words++;
6823 *wc += words;
6824
6825 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006826 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006829 chars += eol_size;
6830 }
6831 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 return i;
6833}
6834
6835/*
6836 * Give some info about the position of the cursor (for "g CTRL-G").
6837 * In Visual mode, give some info about the selected region. (In this case,
6838 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006839 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
6841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006842cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
6844 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006845 char_u buf1[50];
6846 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006848 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006849 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006850 varnumber_T byte_count_cursor = 0;
6851 varnumber_T char_count = 0;
6852 varnumber_T char_count_cursor = 0;
6853 varnumber_T word_count = 0;
6854 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006855 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006856 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 long line_count_selected = 0;
6858 pos_T min_pos, max_pos;
6859 oparg_T oparg;
6860 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 /*
6863 * Compute the length of the file in characters.
6864 */
6865 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6866 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006867 if (dict == NULL)
6868 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006869 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01006870 return;
6871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873 else
6874 {
6875 if (get_fileformat(curbuf) == EOL_DOS)
6876 eol_size = 2;
6877 else
6878 eol_size = 1;
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (VIsual_active)
6881 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006882 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
6884 min_pos = VIsual;
6885 max_pos = curwin->w_cursor;
6886 }
6887 else
6888 {
6889 min_pos = curwin->w_cursor;
6890 max_pos = VIsual;
6891 }
6892 if (*p_sel == 'e' && max_pos.col > 0)
6893 --max_pos.col;
6894
6895 if (VIsual_mode == Ctrl_V)
6896 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006897#ifdef FEAT_LINEBREAK
6898 char_u * saved_sbr = p_sbr;
6899
6900 /* Make 'sbr' empty for a moment to get the correct size. */
6901 p_sbr = empty_option;
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 oparg.is_VIsual = 1;
6904 oparg.block_mode = TRUE;
6905 oparg.op_type = OP_NOP;
6906 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006907 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006908#ifdef FEAT_LINEBREAK
6909 p_sbr = saved_sbr;
6910#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006911 if (curwin->w_curswant == MAXCOL)
6912 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 /* Swap the start, end vcol if needed */
6914 if (oparg.end_vcol < oparg.start_vcol)
6915 {
6916 oparg.end_vcol += oparg.start_vcol;
6917 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6918 oparg.end_vcol -= oparg.start_vcol;
6919 }
6920 }
6921 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6925 {
6926 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006927 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 ui_breakcheck();
6930 if (got_int)
6931 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006932 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 }
6934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 /* Do extra processing for VIsual mode. */
6936 if (VIsual_active
6937 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6938 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006939 char_u *s = NULL;
6940 long len = 0L;
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 switch (VIsual_mode)
6943 {
6944 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006948 s = bd.textstart;
6949 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 break;
6951 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006952 s = ml_get(lnum);
6953 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 break;
6955 case 'v':
6956 {
6957 colnr_T start_col = (lnum == min_pos.lnum)
6958 ? min_pos.col : 0;
6959 colnr_T end_col = (lnum == max_pos.lnum)
6960 ? max_pos.col - start_col + 1 : MAXCOL;
6961
Bram Moolenaardef9e822004-12-31 20:58:58 +00006962 s = ml_get(lnum) + start_col;
6963 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 }
6965 break;
6966 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006967 if (s != NULL)
6968 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006969 byte_count_cursor += line_count_info(s, &word_count_cursor,
6970 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006971 if (lnum == curbuf->b_ml.ml_line_count
6972 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006973 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006974 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006975 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 {
6980 /* In non-visual mode, check for the line the cursor is on */
6981 if (lnum == curwin->w_cursor.lnum)
6982 {
6983 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006984 char_count_cursor += char_count;
6985 byte_count_cursor = byte_count +
6986 line_count_info(ml_get(lnum),
6987 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006988 (varnumber_T)(curwin->w_cursor.col + 1),
6989 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991 }
6992 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006993 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006994 &char_count, (varnumber_T)MAXCOL,
6995 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997
6998 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006999 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007000 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001
Bram Moolenaared767a22016-01-03 22:49:16 +01007002 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007004 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007006 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7007 {
7008 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7009 &max_pos.col);
7010 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7011 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7012 }
7013 else
7014 buf1[0] = NUL;
7015
7016 if (char_count_cursor == byte_count_cursor
7017 && char_count == byte_count)
7018 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007019 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007020 buf1, line_count_selected,
7021 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007022 (long_long_T)word_count_cursor,
7023 (long_long_T)word_count,
7024 (long_long_T)byte_count_cursor,
7025 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007026 else
7027 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007028 _("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 +01007029 buf1, line_count_selected,
7030 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007031 (long_long_T)word_count_cursor,
7032 (long_long_T)word_count,
7033 (long_long_T)char_count_cursor,
7034 (long_long_T)char_count,
7035 (long_long_T)byte_count_cursor,
7036 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
7038 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007039 {
7040 p = ml_get_curline();
7041 validate_virtcol();
7042 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7043 (int)curwin->w_virtcol + 1);
7044 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7045 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaared767a22016-01-03 22:49:16 +01007047 if (char_count_cursor == byte_count_cursor
7048 && char_count == byte_count)
7049 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007050 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007051 (char *)buf1, (char *)buf2,
7052 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007054 (long_long_T)word_count_cursor, (long_long_T)word_count,
7055 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007056 else
7057 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007058 _("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 +01007059 (char *)buf1, (char *)buf2,
7060 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007061 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007062 (long_long_T)word_count_cursor, (long_long_T)word_count,
7063 (long_long_T)char_count_cursor, (long_long_T)char_count,
7064 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007065 }
7066 }
7067
Bram Moolenaared767a22016-01-03 22:49:16 +01007068 bom_count = bomb_size();
7069 if (bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007070 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007071 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007072 if (dict == NULL)
7073 {
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007074 /* Don't shorten this message, the user asked for it. */
Bram Moolenaared767a22016-01-03 22:49:16 +01007075 p = p_shm;
7076 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007077 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007078 p_shm = p;
7079 }
7080 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007081#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007082 if (dict != NULL)
7083 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007084 dict_add_number(dict, "words", word_count);
7085 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007086 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007087 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7088 byte_count_cursor);
7089 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7090 char_count_cursor);
7091 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7092 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}