blob: b2827b263c121763973989cf242ba48ff5f5a765 [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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static int ends_in_white(linenr_T lnum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010021static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022
Bram Moolenaarf2732452018-06-03 14:47:35 +020023// Flags for third item in "opchars".
24#define OPF_LINES 1 // operator always works on lines
25#define OPF_CHANGE 2 // operator changes text
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027/*
28 * The names of operators.
29 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020030 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 */
32static char opchars[][3] =
33{
Bram Moolenaarf2732452018-06-03 14:47:35 +020034 {NUL, NUL, 0}, // OP_NOP
35 {'d', NUL, OPF_CHANGE}, // OP_DELETE
36 {'y', NUL, 0}, // OP_YANK
37 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
38 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
39 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
40 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
41 {'g', '~', OPF_CHANGE}, // OP_TILDE
42 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
43 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
44 {':', NUL, OPF_LINES}, // OP_COLON
45 {'g', 'U', OPF_CHANGE}, // OP_UPPER
46 {'g', 'u', OPF_CHANGE}, // OP_LOWER
47 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
48 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
49 {'g', '?', OPF_CHANGE}, // OP_ROT13
50 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
51 {'I', NUL, OPF_CHANGE}, // OP_INSERT
52 {'A', NUL, OPF_CHANGE}, // OP_APPEND
53 {'z', 'f', OPF_LINES}, // OP_FOLD
54 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
55 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
56 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
57 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
58 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
59 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
60 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
61 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
62 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
63 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000064};
65
66/*
67 * Translate a command name into an operator type.
68 * Must only be called with a valid operator name!
69 */
70 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010071get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 int i;
74
75 if (char1 == 'r') /* ignore second character */
76 return OP_REPLACE;
77 if (char1 == '~') /* when tilde is an operator */
78 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +010079 if (char1 == 'g' && char2 == Ctrl_A) /* add */
80 return OP_NR_ADD;
81 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
82 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +010087 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
88 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return i;
94}
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020099 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100100op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200102 return opchars[op][2] & OPF_LINES;
103}
104
Bram Moolenaar113e1072019-01-20 15:30:40 +0100105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200106/*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
110op_is_change(int op)
111{
112 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116/*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100121get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return opchars[optype][0];
124}
125
126/*
127 * Get second operator command character.
128 */
129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100130get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 return opchars[optype][1];
133}
134
135/*
136 * op_shift - handle a shift operation
137 */
138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100139op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 long i;
142 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
155 if (first_char == NUL) /* empty line */
156 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 else if (oap->block_mode)
158 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
160 /* Move the line right if it doesn't start with '#', 'smartindent'
161 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
162#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100175 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
178 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
179 }
180 else
181 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
184 /* The cursor line is not in a closed fold */
185 foldOpenCursor();
186#endif
187
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (oap->line_count > p_report)
190 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200196 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100206 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100209 if (!cmdmod.lockmarks)
210 {
211 // Set "'[" and "']" marks.
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100221 * Shift the current line one shiftwidth left (if left != 0) or right
222 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225shift_line(
226 int left,
227 int round,
228 int amount,
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200229 int call_changed_bytes) // call changed_bytes()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 int count;
232 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200233 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200235 count = get_indent(); // get current indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200237 if (round) // round off indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200239 i = count / sw_val; // number of 'shiftwidth' rounded down
240 j = count % sw_val; // extra spaces
241 if (j && left) // first remove extra spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 --amount;
243 if (left)
244 {
245 i -= amount;
246 if (i < 0)
247 i = 0;
248 }
249 else
250 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200251 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 }
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200253 else // original vi indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {
255 if (left)
256 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (count < 0)
259 count = 0;
260 }
261 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 }
264
Bram Moolenaar8c96af92019-09-28 19:05:57 +0200265 // Set new indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000267 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000269 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270}
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272/*
273 * Shift one line of the current block one shiftwidth right or left.
274 * Leaves cursor on first character in block.
275 */
276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100277shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
279 int left = (oap->op_type == OP_LSHIFT);
280 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000281 int total;
282 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 int sw_val = (int)get_sw_value_indent(curbuf);
285 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000288 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int i = 0, j = 0;
290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291#ifdef FEAT_RIGHTLEFT
292 int old_p_ri = p_ri;
293
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200294 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
297 State = INSERT; /* don't want REPLACE for State */
298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
302 /* total is number of screen columns to be inserted/removed */
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 total = (int)((unsigned)amount * (unsigned)sw_val);
304 if ((total / sw_val) != amount)
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200305 return; /* multiplication overflow */
306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 oldp = ml_get_curline();
308
309 if (!left)
310 {
311 /*
312 * 1. Get start vcol
313 * 2. Total ws vcols
314 * 3. Divvy into TABs & spp
315 * 4. Construct new string
316 */
317 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
318 ws_vcol = bd.start_vcol - bd.pre_whitesp;
319 if (bd.startspaces)
320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100322 {
323 if ((*mb_ptr2len)(bd.textstart) == 1)
324 ++bd.textstart;
325 else
326 {
327 ws_vcol = 0;
328 bd.startspaces = 0;
329 }
330 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000331 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000332 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100334 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200336 /* TODO: is passing bd.textstart for start of the line OK? */
337 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
338 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 total += incr;
340 bd.start_vcol += incr;
341 }
342 /* OK, now total=all the VWS reqd, and textstart points at the 1st
343 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200344#ifdef FEAT_VARTABS
345 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200346 tabstop_fromto(ws_vcol, ws_vcol + total,
347 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348 else
349 j = total;
350#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200352 i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (i)
Bram Moolenaardac13472019-09-16 21:06:21 +0200354 j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 else
356 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* if we're splitting a TAB, allow for it */
359 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
360 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200361 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (newp == NULL)
363 return;
364 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
365 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200366 vim_memset(newp + bd.textcol, TAB, (size_t)i);
367 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 /* the end */
369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
371 else /* left */
372 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000373 colnr_T destination_col; /* column to which text in block will
374 be shifted */
375 char_u *verbatim_copy_end; /* end of the part of the line which is
376 copied verbatim */
377 colnr_T verbatim_copy_width;/* the (displayed) width of this part
378 of line */
379 unsigned fill; /* nr of spaces that replace a TAB */
380 unsigned new_line_len; /* the length of the line after the
381 block shift */
382 size_t block_space_width;
383 size_t shift_amount;
384 char_u *non_white = bd.textstart;
385 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000387 /*
388 * Firstly, let's find the first non-whitespace character that is
389 * displayed after the block's start column and the character's column
390 * number. Also, let's calculate the width of all the whitespace
391 * characters that are displayed in the block and precede the searched
392 * non-whitespace character.
393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000395 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
396 * the part of which is displayed at the block's beginning. Let's start
397 * searching from the next character. */
398 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100399 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400
401 /* The character's column is in "bd.start_vcol". */
402 non_white_col = bd.start_vcol;
403
Bram Moolenaar1c465442017-03-12 20:10:05 +0100404 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000405 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200406 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000407 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000410 block_space_width = non_white_col - oap->start_vcol;
411 /* We will shift by "total" or "block_space_width", whichever is less.
412 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000413 shift_amount = (block_space_width < (size_t)total
414 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000415
416 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000417 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000418
419 /* Now let's find out how much of the beginning of the line we can
420 * reuse without modification. */
421 verbatim_copy_end = bd.textstart;
422 verbatim_copy_width = bd.start_vcol;
423
424 /* If "bd.startspaces" is set, "bd.textstart" points to the character
425 * preceding the block. We have to subtract its width to obtain its
426 * column number. */
427 if (bd.startspaces)
428 verbatim_copy_width -= bd.start_char_vcols;
429 while (verbatim_copy_width < destination_col)
430 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200431 char_u *line = verbatim_copy_end;
432
433 /* TODO: is passing verbatim_copy_end for start of the line OK? */
434 incr = lbr_chartabsize(line, verbatim_copy_end,
435 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000436 if (verbatim_copy_width + incr > destination_col)
437 break;
438 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100439 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000440 }
441
442 /* If "destination_col" is different from the width of the initial
443 * part of the line that will be copied, it means we encountered a tab
444 * character, which we will have to partly replace with spaces. */
445 fill = destination_col - verbatim_copy_width;
446
447 /* The replacement line will consist of:
448 * - the beginning of the original line up to "verbatim_copy_end",
449 * - "fill" number of spaces,
450 * - the rest of the line, pointed to by non_white. */
451 new_line_len = (unsigned)(verbatim_copy_end - oldp)
452 + fill
453 + (unsigned)STRLEN(non_white) + 1;
454
Bram Moolenaar964b3742019-05-24 18:54:09 +0200455 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (newp == NULL)
457 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000458 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200459 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000460 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
462 /* replace the line */
463 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
464 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
465 State = oldstate;
466 curwin->w_cursor.col = oldcol;
467#ifdef FEAT_RIGHTLEFT
468 p_ri = old_p_ri;
469#endif
470}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472/*
473 * Insert string "s" (b_insert ? before : after) block :AKelly
474 * Caller must prepare for undo.
475 */
476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100477block_insert(
478 oparg_T *oap,
479 char_u *s,
480 int b_insert,
481 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482{
Bram Moolenaardac13472019-09-16 21:06:21 +0200483 int ts_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 int count = 0; /* extra spaces to replace a cut TAB */
485 int spaces = 0; /* non-zero if cutting a TAB */
486 colnr_T offset; /* pointer along new line */
487 unsigned s_len; /* STRLEN(s) */
488 char_u *newp, *oldp; /* new, old lines */
489 linenr_T lnum; /* loop var */
490 int oldstate = State;
491
492 State = INSERT; /* don't want REPLACE for State */
493 s_len = (unsigned)STRLEN(s);
494
495 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
496 {
497 block_prep(oap, bdp, lnum, TRUE);
498 if (bdp->is_short && b_insert)
499 continue; /* OP_INSERT, line ends before block start */
500
501 oldp = ml_get(lnum);
502
503 if (b_insert)
504 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 spaces = bdp->startspaces;
507 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200508 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 offset = bdp->textcol;
510 }
511 else /* append */
512 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 ts_val = bdp->end_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (!bdp->is_short) /* spaces = padding after block */
515 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200516 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200518 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 offset = bdp->textcol + bdp->textlen - (spaces != 0);
520 }
521 else /* spaces = padding to block edge */
522 {
523 /* if $ used, just append to EOL (ie spaces==0) */
524 if (!bdp->is_MAX)
525 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
526 count = spaces;
527 offset = bdp->textcol + bdp->textlen;
528 }
529 }
530
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200531 if (has_mbyte && spaces > 0)
532 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100533 int off;
534
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200535 /* Avoid starting halfway a multi-byte character. */
536 if (b_insert)
537 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100538 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200539 }
540 else
541 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100542 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200543 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200544 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100545 spaces -= off;
546 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200547 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200548
Bram Moolenaar964b3742019-05-24 18:54:09 +0200549 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (newp == NULL)
551 continue;
552
553 /* copy up to shifted part */
554 mch_memmove(newp, oldp, (size_t)(offset));
555 oldp += offset;
556
557 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200558 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 /* copy the new text */
561 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
562 offset += s_len;
563
564 if (spaces && !bdp->is_short)
565 {
566 /* insert post-padding */
Bram Moolenaardac13472019-09-16 21:06:21 +0200567 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /* We're splitting a TAB, don't copy it. */
569 oldp++;
570 /* We allowed for that TAB, remember this now */
571 count++;
572 }
573
574 if (spaces > 0)
575 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000576 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 ml_replace(lnum, newp, FALSE);
579
580 if (lnum == oap->end.lnum)
581 {
582 /* Set "']" mark to the end of the block instead of the end of
583 * the insert in the first line. */
584 curbuf->b_op_end.lnum = oap->end.lnum;
585 curbuf->b_op_end.col = offset;
586 }
587 } /* for all lnum */
588
589 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
590
591 State = oldstate;
592}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594/*
595 * Stuff a string into the typeahead buffer, such that edit() will insert it
596 * literally ("literally" TRUE) or interpret is as typed characters.
597 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200598 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100599stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 int c;
602 char_u *start;
603
604 while (*arg != NUL)
605 {
606 /* Stuff a sequence of normal ASCII characters, that's fast. Also
607 * stuff K_SPECIAL to get the effect of a special key when "literally"
608 * is TRUE. */
609 start = arg;
610 while ((*arg >= ' '
611#ifndef EBCDIC
612 && *arg < DEL /* EBCDIC: chars above space are normal */
613#endif
614 )
615 || (*arg == K_SPECIAL && !literally))
616 ++arg;
617 if (arg > start)
618 stuffReadbuffLen(start, (long)(arg - start));
619
620 /* stuff a single special character */
621 if (*arg != NUL)
622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +0200624 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 c = *arg++;
627 if (literally && ((c < ' ' && c != TAB) || c == DEL))
628 stuffcharReadbuff(Ctrl_V);
629 stuffcharReadbuff(c);
630 }
631 }
632}
633
634/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200635 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200637 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 int n;
643 linenr_T lnum;
644 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 char_u *newp, *oldp;
646 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
648 int did_yank = FALSE;
649
650 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
651 return OK;
652
653 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
654 if (oap->empty)
655 return u_save_cursor();
656
657 if (!curbuf->b_p_ma)
658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100659 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 return FAIL;
661 }
662
663#ifdef FEAT_CLIPBOARD
664 adjust_clip_reg(&oap->regname);
665#endif
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (has_mbyte)
668 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaard04b7502010-07-08 22:27:55 +0200670 /*
671 * Imitate the strange Vi behaviour: If the delete spans more than one
672 * line and motion_type == MCHAR and the result is a blank line, make the
673 * delete linewise. Don't do this for the change command or Visual mode.
674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000677 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100679 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 && oap->op_type == OP_DELETE)
681 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200682 ptr = ml_get(oap->end.lnum) + oap->end.col;
683 if (*ptr != NUL)
684 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ptr = skipwhite(ptr);
686 if (*ptr == NUL && inindent(0))
687 oap->motion_type = MLINE;
688 }
689
Bram Moolenaard04b7502010-07-08 22:27:55 +0200690 /*
691 * Check for trying to delete (e.g. "D") in an empty line.
692 * Note: For the change operator it is ok.
693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if ( oap->motion_type == MCHAR
695 && oap->line_count == 1
696 && oap->op_type == OP_DELETE
697 && *ml_get(oap->start.lnum) == NUL)
698 {
699 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000700 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 * 'cpoptions' (Vi compatible).
702 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000703 if (virtual_op)
704 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
705 * marks as if it happened. */
706 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
708 beep_flush();
709 return OK;
710 }
711
Bram Moolenaard04b7502010-07-08 22:27:55 +0200712 /*
713 * Do a yank of whatever we're about to delete.
714 * If a yank register was specified, put the deleted text into that
715 * register. For the black hole register '_' don't yank anything.
716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (oap->regname != '_')
718 {
719 if (oap->regname != 0)
720 {
721 /* check for read-only register */
722 if (!valid_yank_reg(oap->regname, TRUE))
723 {
724 beep_flush();
725 return OK;
726 }
727 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
728 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
729 did_yank = TRUE;
730 }
731
732 /*
733 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100734 * delete contains a line break, or when using a specific operator (Vi
735 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200736 * Use the register name from before adjust_clip_reg() may have
737 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100739 if (oap->motion_type == MLINE || oap->line_count > 1
740 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100742 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 if (op_yank(oap, TRUE, FALSE) == OK)
744 did_yank = TRUE;
745 }
746
Bram Moolenaar84298db2012-04-20 13:46:08 +0200747 /* Yank into small delete register when no named register specified
748 * and the delete is within one line. */
749 if ((
750#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200751 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
752 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200753#endif
754 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 && oap->line_count == 1)
756 {
757 oap->regname = '-';
758 get_yank_register(oap->regname, TRUE);
759 if (op_yank(oap, TRUE, FALSE) == OK)
760 did_yank = TRUE;
761 oap->regname = 0;
762 }
763
764 /*
765 * If there's too much stuff to fit in the yank register, then get a
766 * confirmation before doing the delete. This is crude, but simple.
767 * And it avoids doing a delete of something we can't put back if we
768 * want.
769 */
770 if (!did_yank)
771 {
772 int msg_silent_save = msg_silent;
773
774 msg_silent = 0; /* must display the prompt */
775 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
776 msg_silent = msg_silent_save;
777 if (n != 'y')
778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return FAIL;
781 }
782 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100783
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100784#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100785 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200786 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789
Bram Moolenaard04b7502010-07-08 22:27:55 +0200790 /*
791 * block mode delete
792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 if (oap->block_mode)
794 {
795 if (u_save((linenr_T)(oap->start.lnum - 1),
796 (linenr_T)(oap->end.lnum + 1)) == FAIL)
797 return FAIL;
798
799 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
800 {
801 block_prep(oap, &bd, lnum, TRUE);
802 if (bd.textlen == 0) /* nothing to delete */
803 continue;
804
805 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
806 if (lnum == curwin->w_cursor.lnum)
807 {
808 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811
Bram Moolenaar8055d172019-05-17 22:57:26 +0200812 // "n" == number of chars deleted
813 // If we delete a TAB, it may be replaced by several characters.
814 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 n = bd.textlen - bd.startspaces - bd.endspaces;
816 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200817 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (newp == NULL)
819 continue;
820 /* copy up to deleted part */
821 mch_memmove(newp, oldp, (size_t)bd.textcol);
822 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200823 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 (size_t)(bd.startspaces + bd.endspaces));
825 /* copy the part after the deleted part */
826 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000827 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 /* replace the line */
829 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200830
831#ifdef FEAT_TEXT_PROP
832 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200833 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
837 check_cursor_col();
838 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
839 oap->end.lnum + 1, 0L);
840 oap->line_count = 0; /* no lines deleted */
841 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100842 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
844 if (oap->op_type == OP_CHANGE)
845 {
846 /* Delete the lines except the first one. Temporarily move the
847 * cursor to the next line. Save the current line number, if the
848 * last line is deleted it may be changed.
849 */
850 if (oap->line_count > 1)
851 {
852 lnum = curwin->w_cursor.lnum;
853 ++curwin->w_cursor.lnum;
854 del_lines((long)(oap->line_count - 1), TRUE);
855 curwin->w_cursor.lnum = lnum;
856 }
857 if (u_save_cursor() == FAIL)
858 return FAIL;
859 if (curbuf->b_p_ai) /* don't delete indent */
860 {
861 beginline(BL_WHITE); /* cursor on first non-white */
862 did_ai = TRUE; /* delete the indent when ESC hit */
863 ai_col = curwin->w_cursor.col;
864 }
865 else
866 beginline(0); /* cursor in column 0 */
867 truncate_line(FALSE); /* delete the rest of the line */
868 /* leave cursor past last char in line */
869 if (oap->line_count > 1)
870 u_clearline(); /* "U" command not possible after "2cc" */
871 }
872 else
873 {
874 del_lines(oap->line_count, TRUE);
875 beginline(BL_WHITE | BL_FIX);
876 u_clearline(); /* "U" command not possible after "dd" */
877 }
878 }
879 else
880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (virtual_op)
882 {
883 int endcol = 0;
884
885 /* For virtualedit: break the tabs that are partly included. */
886 if (gchar_pos(&oap->start) == '\t')
887 {
888 if (u_save_cursor() == FAIL) /* save first line for undo */
889 return FAIL;
890 if (oap->line_count == 1)
891 endcol = getviscol2(oap->end.col, oap->end.coladd);
892 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
893 oap->start = curwin->w_cursor;
894 if (oap->line_count == 1)
895 {
896 coladvance(endcol);
897 oap->end.col = curwin->w_cursor.col;
898 oap->end.coladd = curwin->w_cursor.coladd;
899 curwin->w_cursor = oap->start;
900 }
901 }
902
903 /* Break a tab only when it's included in the area. */
904 if (gchar_pos(&oap->end) == '\t'
905 && (int)oap->end.coladd < oap->inclusive)
906 {
907 /* save last line for undo */
908 if (u_save((linenr_T)(oap->end.lnum - 1),
909 (linenr_T)(oap->end.lnum + 1)) == FAIL)
910 return FAIL;
911 curwin->w_cursor = oap->end;
912 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
913 oap->end = curwin->w_cursor;
914 curwin->w_cursor = oap->start;
915 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100916 if (has_mbyte)
917 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
920 if (oap->line_count == 1) /* delete characters within one line */
921 {
922 if (u_save_cursor() == FAIL) /* save line for undo */
923 return FAIL;
924
925 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100926 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 && oap->op_type == OP_CHANGE
928 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100929 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 display_dollar(oap->end.col - !oap->inclusive);
931
932 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (virtual_op)
935 {
936 /* fix up things for virtualedit-delete:
937 * break the tabs which are going to get in our way
938 */
939 char_u *curline = ml_get_curline();
940 int len = (int)STRLEN(curline);
941
942 if (oap->end.coladd != 0
943 && (int)oap->end.col >= len - 1
944 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
945 n++;
946 /* Delete at least one char (e.g, when on a control char). */
947 if (n == 0 && oap->start.coladd != oap->end.coladd)
948 n = 1;
949
950 /* When deleted a char in the line, reset coladd. */
951 if (gchar_cursor() != NUL)
952 curwin->w_cursor.coladd = 0;
953 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200954 (void)del_bytes((long)n, !virtual_op,
955 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 }
957 else /* delete characters between lines */
958 {
959 pos_T curpos;
960
961 /* save deleted and changed lines for undo */
962 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
963 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
964 return FAIL;
965
966 truncate_line(TRUE); /* delete from cursor to end of line */
967
968 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
969 ++curwin->w_cursor.lnum;
970 del_lines((long)(oap->line_count - 2), FALSE);
971
Bram Moolenaard009e862015-06-09 20:20:03 +0200972 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200973 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200974 curwin->w_cursor.col = 0;
975 (void)del_bytes((long)n, !virtual_op,
976 oap->op_type == OP_DELETE && !oap->is_VIsual);
977 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
978 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 }
981
982 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
983
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000984setmarks:
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100985 if (!cmdmod.lockmarks)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100987 if (oap->block_mode)
988 {
989 curbuf->b_op_end.lnum = oap->end.lnum;
990 curbuf->b_op_end.col = oap->start.col;
991 }
992 else
993 curbuf->b_op_end = oap->start;
994 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 return OK;
998}
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Adjust end of operating area for ending on a multi-byte character.
1002 * Used for deletion.
1003 */
1004 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001005mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 char_u *p;
1008
1009 if (oap->inclusive)
1010 {
1011 p = ml_get(oap->end.lnum);
1012 oap->end.col += mb_tail_off(p, p + oap->end.col);
1013 }
1014}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar630afe82018-06-28 19:26:28 +02001016/*
1017 * Replace the character under the cursor with "c".
1018 * This takes care of multi-byte characters.
1019 */
1020 static void
1021replace_character(int c)
1022{
1023 int n = State;
1024
1025 State = REPLACE;
1026 ins_char(c);
1027 State = n;
1028 /* Backup to the replaced character. */
1029 dec_cursor();
1030}
1031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032/*
1033 * Replace a whole area with one character.
1034 */
1035 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001036op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
1038 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 char_u *newp, *oldp;
1041 size_t oldlen;
1042 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001043 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001044 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1047 return OK; /* nothing to do */
1048
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001049 if (c == REPLACE_CR_NCHAR)
1050 {
1051 had_ctrl_v_cr = TRUE;
1052 c = CAR;
1053 }
1054 else if (c == REPLACE_NL_NCHAR)
1055 {
1056 had_ctrl_v_cr = TRUE;
1057 c = NL;
1058 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (has_mbyte)
1061 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063 if (u_save((linenr_T)(oap->start.lnum - 1),
1064 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1065 return FAIL;
1066
1067 /*
1068 * block mode replace
1069 */
1070 if (oap->block_mode)
1071 {
1072 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1073 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1074 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001075 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1077 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1078 continue; /* nothing to replace */
1079
1080 /* n == number of extra chars required
1081 * If we split a TAB, it may be replaced by several characters.
1082 * Thus the number of characters may increase!
1083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 /* If the range starts in virtual space, count the initial
1085 * coladd offset as part of "startspaces" */
1086 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1087 {
1088 pos_T vpos;
1089
Bram Moolenaara1381de2009-11-03 15:44:21 +00001090 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 getvpos(&vpos, oap->start_vcol);
1092 bd.startspaces += vpos.coladd;
1093 n = bd.startspaces;
1094 }
1095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /* allow for pre spaces */
1097 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1098
1099 /* allow for post spp */
1100 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1103 /* Figure out how many characters to replace. */
1104 numc = oap->end_vcol - oap->start_vcol + 1;
1105 if (bd.is_short && (!virtual_op || bd.is_MAX))
1106 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /* A double-wide character can be replaced only up to half the
1109 * times. */
1110 if ((*mb_char2cells)(c) > 1)
1111 {
1112 if ((numc & 1) && !bd.is_short)
1113 {
1114 ++bd.endspaces;
1115 ++n;
1116 }
1117 numc = numc / 2;
1118 }
1119
1120 /* Compute bytes needed, move character count to num_chars. */
1121 num_chars = numc;
1122 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 /* oldlen includes textlen, so don't double count */
1124 n += numc - bd.textlen;
1125
1126 oldp = ml_get_curline();
1127 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001128 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (newp == NULL)
1130 continue;
1131 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
1132 /* copy up to deleted part */
1133 mch_memmove(newp, oldp, (size_t)bd.textcol);
1134 oldp += bd.textcol + bd.textlen;
1135 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001136 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001138 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1139 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01001140 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001142 if (has_mbyte)
1143 {
1144 n = (int)STRLEN(newp);
1145 while (--num_chars >= 0)
1146 n += (*mb_char2bytes)(c, newp + n);
1147 }
1148 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001149 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001150 if (!bd.is_short)
1151 {
1152 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001153 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01001154 /* copy the part after the changed part */
1155 STRMOVE(newp + STRLEN(newp), oldp);
1156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 }
1158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001160 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001161 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001162 if (after_p != NULL)
1163 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 /* replace the line */
1166 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001167 if (after_p != NULL)
1168 {
1169 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1170 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1171 oap->end.lnum++;
1172 vim_free(after_p);
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 }
1175 }
1176 else
1177 {
1178 /*
1179 * MCHAR and MLINE motion replace.
1180 */
1181 if (oap->motion_type == MLINE)
1182 {
1183 oap->start.col = 0;
1184 curwin->w_cursor.col = 0;
1185 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1186 if (oap->end.col)
1187 --oap->end.col;
1188 }
1189 else if (!oap->inclusive)
1190 dec(&(oap->end));
1191
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001192 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 n = gchar_cursor();
1195 if (n != NUL)
1196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1198 {
1199 /* This is slow, but it handles replacing a single-byte
1200 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01001201 if (curwin->w_cursor.lnum == oap->end.lnum)
1202 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02001203 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (n == TAB)
1208 {
1209 int end_vcol = 0;
1210
1211 if (curwin->w_cursor.lnum == oap->end.lnum)
1212 {
1213 /* oap->end has to be recalculated when
1214 * the tab breaks */
1215 end_vcol = getviscol2(oap->end.col,
1216 oap->end.coladd);
1217 }
1218 coladvance_force(getviscol());
1219 if (curwin->w_cursor.lnum == oap->end.lnum)
1220 getvpos(&oap->end, end_vcol);
1221 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001222 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 }
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1226 {
1227 int virtcols = oap->end.coladd;
1228
1229 if (curwin->w_cursor.lnum == oap->start.lnum
1230 && oap->start.col == oap->end.col && oap->start.coladd)
1231 virtcols -= oap->start.coladd;
1232
1233 /* oap->end has been trimmed so it's effectively inclusive;
1234 * as a result an extra +1 must be counted so we don't
1235 * trample the NUL byte. */
1236 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1237 curwin->w_cursor.col -= (virtcols + 1);
1238 for (; virtcols >= 0; virtcols--)
1239 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001240 if ((*mb_char2len)(c) > 1)
1241 replace_character(c);
1242 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001243 PBYTE(curwin->w_cursor, c);
1244 if (inc(&curwin->w_cursor) == -1)
1245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 }
1247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
1249 /* Advance to next character, stop at the end of the file. */
1250 if (inc_cursor() == -1)
1251 break;
1252 }
1253 }
1254
1255 curwin->w_cursor = oap->start;
1256 check_cursor();
1257 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1258
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001259 if (!cmdmod.lockmarks)
1260 {
1261 /* Set "'[" and "']" marks. */
1262 curbuf->b_op_start = oap->start;
1263 curbuf->b_op_end = oap->end;
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 return OK;
1267}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001269static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271/*
1272 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1273 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001275op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001279 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 if (u_save((linenr_T)(oap->start.lnum - 1),
1282 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1283 return;
1284
1285 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (oap->block_mode) /* Visual block mode */
1287 {
1288 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1289 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001290 int one_change;
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 block_prep(oap, &bd, pos.lnum, FALSE);
1293 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001294 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1295 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001296
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001297#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001298 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {
1300 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1301
Bram Moolenaar009b2592004-10-24 19:18:58 +00001302 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1303 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001305 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (did_change)
1310 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1311 }
1312 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
1314 if (oap->motion_type == MLINE)
1315 {
1316 oap->start.col = 0;
1317 pos.col = 0;
1318 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1319 if (oap->end.col)
1320 --oap->end.col;
1321 }
1322 else if (!oap->inclusive)
1323 dec(&(oap->end));
1324
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001325 if (pos.lnum == oap->end.lnum)
1326 did_change = swapchars(oap->op_type, &pos,
1327 oap->end.col - pos.col + 1);
1328 else
1329 for (;;)
1330 {
1331 did_change |= swapchars(oap->op_type, &pos,
1332 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1333 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001334 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001335 break;
1336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (did_change)
1338 {
1339 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1340 0L);
1341#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001342 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
1344 char_u *ptr;
1345 int count;
1346
1347 pos = oap->start;
1348 while (pos.lnum < oap->end.lnum)
1349 {
1350 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001351 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001352 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001354 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 pos.col = 0;
1356 pos.lnum++;
1357 }
1358 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1359 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001360 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001362 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364#endif
1365 }
1366 }
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (!did_change && oap->is_VIsual)
1369 /* No change: need to remove the Visual selection */
1370 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001372 if (!cmdmod.lockmarks)
1373 {
1374 // Set '[ and '] marks.
1375 curbuf->b_op_start = oap->start;
1376 curbuf->b_op_end = oap->end;
1377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001380 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001381 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382}
1383
1384/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001385 * Invoke swapchar() on "length" bytes at position "pos".
1386 * "pos" is advanced to just after the changed characters.
1387 * "length" is rounded up to include the whole last multi-byte character.
1388 * Also works correctly when the number of bytes changes.
1389 * Returns TRUE if some character was changed.
1390 */
1391 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001392swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001393{
1394 int todo;
1395 int did_change = 0;
1396
1397 for (todo = length; todo > 0; --todo)
1398 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001399 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001400 {
1401 int len = (*mb_ptr2len)(ml_get_pos(pos));
1402
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001403 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001404 if (len > 0)
1405 todo -= len - 1;
1406 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001407 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001408 if (inc(pos) == -1) /* at end of file */
1409 break;
1410 }
1411 return did_change;
1412}
1413
1414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 * If op_type == OP_UPPER: make uppercase,
1416 * if op_type == OP_LOWER: make lowercase,
1417 * if op_type == OP_ROT13: do rot13 encoding,
1418 * else swap case of character at 'pos'
1419 * returns TRUE when something actually changed.
1420 */
1421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001422swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 int c;
1425 int nc;
1426
1427 c = gchar_pos(pos);
1428
1429 /* Only do rot13 encoding for ASCII characters. */
1430 if (c >= 0x80 && op_type == OP_ROT13)
1431 return FALSE;
1432
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001433 if (op_type == OP_UPPER && c == 0xdf
1434 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001435 {
1436 pos_T sp = curwin->w_cursor;
1437
1438 /* Special handling of German sharp s: change to "SS". */
1439 curwin->w_cursor = *pos;
1440 del_char(FALSE);
1441 ins_char('S');
1442 ins_char('S');
1443 curwin->w_cursor = sp;
1444 inc(pos);
1445 }
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
1448 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 nc = c;
1450 if (MB_ISLOWER(c))
1451 {
1452 if (op_type == OP_ROT13)
1453 nc = ROT13(c, 'a');
1454 else if (op_type != OP_LOWER)
1455 nc = MB_TOUPPER(c);
1456 }
1457 else if (MB_ISUPPER(c))
1458 {
1459 if (op_type == OP_ROT13)
1460 nc = ROT13(c, 'A');
1461 else if (op_type != OP_UPPER)
1462 nc = MB_TOLOWER(c);
1463 }
1464 if (nc != c)
1465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1467 {
1468 pos_T sp = curwin->w_cursor;
1469
1470 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001471 /* don't use del_char(), it also removes composing chars */
1472 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ins_char(nc);
1474 curwin->w_cursor = sp;
1475 }
1476 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001477 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return TRUE;
1479 }
1480 return FALSE;
1481}
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * op_insert - Insert and append operators for Visual mode.
1485 */
1486 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001487op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 long ins_len, pre_textlen = 0;
1490 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02001491 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 struct block_def bd;
1493 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001494 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 /* edit() changes this - record it for OP_APPEND */
1497 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1498
1499 /* vis block is still marked. Get rid of it now. */
1500 curwin->w_cursor.lnum = oap->start.lnum;
1501 update_screen(INVERTED);
1502
1503 if (oap->block_mode)
1504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /* When 'virtualedit' is used, need to insert the extra spaces before
1506 * doing block_prep(). When only "block" is used, virtual edit is
1507 * already disabled, but still need it when calling
1508 * coladvance_force(). */
1509 if (curwin->w_cursor.coladd > 0)
1510 {
1511 int old_ve_flags = ve_flags;
1512
1513 ve_flags = VE_ALL;
1514 if (u_save_cursor() == FAIL)
1515 return;
1516 coladvance_force(oap->op_type == OP_APPEND
1517 ? oap->end_vcol + 1 : getviscol());
1518 if (oap->op_type == OP_APPEND)
1519 --curwin->w_cursor.col;
1520 ve_flags = old_ve_flags;
1521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 /* Get the info about the block before entering the text */
1523 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001524 /* Get indent information */
1525 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (oap->op_type == OP_APPEND)
1529 firstline += bd.textlen;
1530 pre_textlen = (long)STRLEN(firstline);
1531 }
1532
1533 if (oap->op_type == OP_APPEND)
1534 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001535 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 /* Move the cursor to the character right of the block. */
1538 curwin->w_set_curswant = TRUE;
1539 while (*ml_get_cursor() != NUL
1540 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1541 ++curwin->w_cursor.col;
1542 if (bd.is_short && !bd.is_MAX)
1543 {
1544 /* First line was too short, make it longer and adjust the
1545 * values in "bd". */
1546 if (u_save_cursor() == FAIL)
1547 return;
1548 for (i = 0; i < bd.endspaces; ++i)
1549 ins_char(' ');
1550 bd.textlen += bd.endspaces;
1551 }
1552 }
1553 else
1554 {
1555 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001556 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001559 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 && oap->start_vcol != oap->end_vcol)
1561 inc_cursor();
1562 }
1563 }
1564
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001565 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001566 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001568 /* When a tab was inserted, and the characters in front of the tab
1569 * have been converted to a tab as well, the column of the cursor
1570 * might have actually been reduced, so need to adjust here. */
1571 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001572 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001573 oap->start = curbuf->b_op_start_orig;
1574
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001575 /* If user has moved off this line, we don't know what to do, so do
1576 * nothing.
1577 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
1578 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return;
1580
1581 if (oap->block_mode)
1582 {
1583 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001584 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001585 size_t len;
1586 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001588 /* If indent kicked in, the firstline might have changed
1589 * but only do that, if the indent actually increased. */
1590 ind_post = (colnr_T)getwhitecols_curline();
1591 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1592 {
1593 bd.textcol += ind_post - ind_pre;
1594 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001595 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001596 }
1597
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001598 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001599 * to adjust the block for that. But only do it, if the difference
1600 * does not come from indent kicking in. */
1601 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1602 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001603 {
1604 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001605 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001606 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001607 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001608 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001609 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001610 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001611 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001612 pre_textlen -= t - oap->start_vcol;
1613 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001614 }
1615 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001616 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01001617 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001618 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001619 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001620 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001621 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01001622 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001623 /* reset pre_textlen to the value of OP_INSERT */
1624 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001625 pre_textlen -= t - oap->start_vcol;
1626 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001627 oap->op_type = OP_INSERT;
1628 }
1629 }
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 /*
1632 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00001633 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 * Don't do this when "$" used, end-of-line will have changed.
1635 */
1636 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1637 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1638 {
1639 if (oap->op_type == OP_APPEND)
1640 {
1641 pre_textlen += bd2.textlen - bd.textlen;
1642 if (bd2.endspaces)
1643 --bd2.textlen;
1644 }
1645 bd.textcol = bd2.textcol;
1646 bd.textlen = bd2.textlen;
1647 }
1648
1649 /*
1650 * Subsequent calls to ml_get() flush the firstline data - take a
1651 * copy of the required string.
1652 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001653 firstline = ml_get(oap->start.lnum);
1654 len = STRLEN(firstline);
1655 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001657 add += bd.textlen;
1658 if ((size_t)add > len)
1659 firstline += len; // short line, point to the NUL
1660 else
1661 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01001662 if (pre_textlen >= 0
1663 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
1665 ins_text = vim_strnsave(firstline, (int)ins_len);
1666 if (ins_text != NULL)
1667 {
1668 /* block handled here */
1669 if (u_save(oap->start.lnum,
1670 (linenr_T)(oap->end.lnum + 1)) == OK)
1671 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1672 &bd);
1673
1674 curwin->w_cursor.col = oap->start.col;
1675 check_cursor();
1676 vim_free(ins_text);
1677 }
1678 }
1679 }
1680}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682/*
1683 * op_change - handle a change operation
1684 *
1685 * return TRUE if edit() returns because of a CTRL-O command
1686 */
1687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 colnr_T l;
1691 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 long offset;
1693 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001694 long ins_len;
1695 long pre_textlen = 0;
1696 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 char_u *firstline;
1698 char_u *ins_text, *newp, *oldp;
1699 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 l = oap->start.col;
1702 if (oap->motion_type == MLINE)
1703 {
1704 l = 0;
1705#ifdef FEAT_SMARTINDENT
1706 if (!p_paste && curbuf->b_p_si
1707# ifdef FEAT_CINDENT
1708 && !curbuf->b_p_cin
1709# endif
1710 )
1711 can_si = TRUE; /* It's like opening a new line, do si */
1712#endif
1713 }
1714
1715 /* First delete the text in the region. In an empty buffer only need to
1716 * save for undo */
1717 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1718 {
1719 if (u_save_cursor() == FAIL)
1720 return FALSE;
1721 }
1722 else if (op_delete(oap) == FAIL)
1723 return FALSE;
1724
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001725 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 && !virtual_op)
1727 inc_cursor();
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /* check for still on same line (<CR> in inserted text meaningless) */
1730 /* skip blank lines too */
1731 if (oap->block_mode)
1732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 /* Add spaces before getting the current line length. */
1734 if (virtual_op && (curwin->w_cursor.coladd > 0
1735 || gchar_cursor() == NUL))
1736 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001737 firstline = ml_get(oap->start.lnum);
1738 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001739 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 bd.textcol = curwin->w_cursor.col;
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1744 if (oap->motion_type == MLINE)
1745 fix_indent();
1746#endif
1747
1748 retval = edit(NUL, FALSE, (linenr_T)1);
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001751 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001753 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001755 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00001757 /* Auto-indenting may have changed the indent. If the cursor was past
1758 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001760 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001762 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001763
1764 pre_textlen += new_indent - pre_indent;
1765 bd.textcol += new_indent - pre_indent;
1766 }
1767
1768 ins_len = (long)STRLEN(firstline) - pre_textlen;
1769 if (ins_len > 0)
1770 {
1771 /* Subsequent calls to ml_get() flush the firstline data - take a
1772 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001773 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001775 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1777 linenr++)
1778 {
1779 block_prep(oap, &bd, linenr, TRUE);
1780 if (!bd.is_short || virtual_op)
1781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 pos_T vpos;
1783
1784 /* If the block starts in virtual space, count the
1785 * initial coladd offset as part of "startspaces" */
1786 if (bd.is_short)
1787 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001788 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
1791 else
1792 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001794 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (newp == NULL)
1796 continue;
1797 /* copy up to block start */
1798 mch_memmove(newp, oldp, (size_t)bd.textcol);
1799 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001800 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1803 offset += ins_len;
1804 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001805 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 ml_replace(linenr, newp, FALSE);
1807 }
1808 }
1809 check_cursor();
1810
1811 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1812 }
1813 vim_free(ins_text);
1814 }
1815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 return retval;
1818}
1819
1820/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001821 * When the cursor is on the NUL past the end of the line and it should not be
1822 * there move it left.
1823 */
1824 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001825adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001826{
1827 if (curwin->w_cursor.col > 0
1828 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00001829 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 && !(restart_edit || (State & INSERT)))
1831 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00001832 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00001833 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001836 {
1837 colnr_T scol, ecol;
1838
1839 /* Coladd is set to the width of the last character. */
1840 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1841 curwin->w_cursor.coladd = ecol - scol + 1;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844}
1845
Bram Moolenaar81340392012-06-06 16:12:59 +02001846/*
1847 * If "process" is TRUE and the line begins with a comment leader (possibly
1848 * after some white space), return a pointer to the text after it. Put a boolean
1849 * value indicating whether the line ends with an unclosed comment in
1850 * "is_comment".
1851 * line - line to be processed,
1852 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001853 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001854 * include_space - whether to also skip space following the comment leader,
1855 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001856 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001857 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001858 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001859skip_comment(
1860 char_u *line,
1861 int process,
1862 int include_space,
1863 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001864{
1865 char_u *comment_flags = NULL;
1866 int lead_len;
1867 int leader_offset = get_last_leader_offset(line, &comment_flags);
1868
1869 *is_comment = FALSE;
1870 if (leader_offset != -1)
1871 {
1872 /* Let's check whether the line ends with an unclosed comment.
1873 * If the last comment leader has COM_END in flags, there's no comment.
1874 */
1875 while (*comment_flags)
1876 {
1877 if (*comment_flags == COM_END
1878 || *comment_flags == ':')
1879 break;
1880 ++comment_flags;
1881 }
1882 if (*comment_flags != COM_END)
1883 *is_comment = TRUE;
1884 }
1885
1886 if (process == FALSE)
1887 return line;
1888
1889 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1890
1891 if (lead_len == 0)
1892 return line;
1893
1894 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02001895 * - COM_END,
1896 * - colon,
1897 * whichever comes first.
1898 */
1899 while (*comment_flags)
1900 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001901 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001902 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001903 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001904 ++comment_flags;
1905 }
1906
1907 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001908 * starting with a closing part of a three-part comment. That's good,
1909 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001910 */
1911 if (*comment_flags == ':' || *comment_flags == NUL)
1912 line += lead_len;
1913
1914 return line;
1915}
Bram Moolenaar81340392012-06-06 16:12:59 +02001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001918 * Join 'count' lines (minimal 2) at cursor position.
1919 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001920 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1921 * leaders should not be removed.
1922 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1923 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001925 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001928do_join(
1929 long count,
1930 int insert_space,
1931 int save_undo,
1932 int use_formatoptions UNUSED,
1933 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001935 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001936 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001937 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001939 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02001940 int endcurr1 = NUL;
1941 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001942 int currsize = 0; /* size of the current line */
1943 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001945 colnr_T col = 0;
1946 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001947 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001948 int remove_comments = (use_formatoptions == TRUE)
1949 && has_format_option(FO_REMOVE_COMS);
1950 int prev_was_comment;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001951#ifdef FEAT_TEXT_PROP
1952 textprop_T **prop_lines = NULL;
1953 int *prop_lengths = NULL;
1954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001956 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1957 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1958 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001960 /* Allocate an array to store the number of spaces inserted before each
1961 * line. We will use it to pre-compute the length of the new line and the
1962 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001963 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001964 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001966 if (remove_comments)
1967 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001968 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001969 if (comments == NULL)
1970 {
1971 vim_free(spaces);
1972 return FAIL;
1973 }
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001977 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001978 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001979 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001981 for (t = 0; t < count; ++t)
1982 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001983 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001984 if (t == 0 && setmark && !cmdmod.lockmarks)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01001985 {
1986 /* Set the '[ mark. */
1987 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1988 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1989 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001990 if (remove_comments)
1991 {
1992 /* We don't want to remove the comment leader if the
1993 * previous line is not a comment. */
1994 if (t > 0 && prev_was_comment)
1995 {
1996
1997 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
1998 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02001999 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002000 curr = new_curr;
2001 }
2002 else
2003 curr = skip_comment(curr, FALSE, insert_space,
2004 &prev_was_comment);
2005 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002006
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002007 if (insert_space && t > 0)
2008 {
2009 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002010 if (*curr != NUL && *curr != ')'
2011 && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002012 && (!has_format_option(FO_MBYTE_JOIN)
2013 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2014 && (!has_format_option(FO_MBYTE_JOIN2)
2015 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002016 )
2017 {
2018 /* don't add a space if the line is ending in a space */
2019 if (endcurr1 == ' ')
2020 endcurr1 = endcurr2;
2021 else
2022 ++spaces[t];
2023 /* extra space when 'joinspaces' set and line ends in '.' */
2024 if ( p_js
2025 && (endcurr1 == '.'
2026 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2027 && (endcurr1 == '?' || endcurr1 == '!'))))
2028 ++spaces[t];
2029 }
2030 }
2031 currsize = (int)STRLEN(curr);
2032 sumsize += currsize + spaces[t];
2033 endcurr1 = endcurr2 = NUL;
2034 if (insert_space && currsize > 0)
2035 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002036 if (has_mbyte)
2037 {
2038 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002039 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002040 endcurr1 = (*mb_ptr2char)(cend);
2041 if (cend > curr)
2042 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002043 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002044 endcurr2 = (*mb_ptr2char)(cend);
2045 }
2046 }
2047 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 {
2049 endcurr1 = *(curr + currsize - 1);
2050 if (currsize > 1)
2051 endcurr2 = *(curr + currsize - 2);
2052 }
2053 }
2054 line_breakcheck();
2055 if (got_int)
2056 {
2057 ret = FAIL;
2058 goto theend;
2059 }
2060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002062 /* store the column position before last line */
2063 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002066 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002067 if (newp == NULL)
2068 {
2069 ret = FAIL;
2070 goto theend;
2071 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002072 cend = newp + sumsize;
2073 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002075#ifdef FEAT_TEXT_PROP
2076 // We need to move properties of the lines that are going to be deleted to
2077 // the new long one.
2078 if (curbuf->b_has_textprop && !text_prop_frozen)
2079 {
2080 // Allocate an array to copy the text properties of joined lines into.
2081 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002082 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
2083 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002084 if (prop_lengths == NULL)
2085 VIM_CLEAR(prop_lines);
2086 }
2087#endif
2088
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002089 /*
2090 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002091 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 *
2093 * Move marks from each deleted line to the joined line, adjusting the
2094 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2095 * should not really be a problem.
2096 */
2097 for (t = count - 1; ; --t)
2098 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002099 int spaces_removed;
2100
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002101 cend -= currsize;
2102 mch_memmove(cend, curr, (size_t)currsize);
2103 if (spaces[t] > 0)
2104 {
2105 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002106 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002107 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002108
2109 // If deleting more spaces than adding, the cursor moves no more than
2110 // what is added if it is inside these spaces.
2111 spaces_removed = (curr - curr_start) - spaces[t];
2112
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002113 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002114 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002115 if (t == 0)
2116 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002117#ifdef FEAT_TEXT_PROP
2118 if (prop_lines != NULL)
2119 adjust_props_for_join(curwin->w_cursor.lnum + t,
2120 prop_lines + t - 1, prop_lengths + t - 1,
2121 (long)(cend - newp - spaces_removed), spaces_removed);
2122#endif
2123
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002124 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002125 if (remove_comments)
2126 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002127 if (insert_space && t > 1)
2128 curr = skipwhite(curr);
2129 currsize = (int)STRLEN(curr);
2130 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002131
2132#ifdef FEAT_TEXT_PROP
2133 if (prop_lines != NULL)
2134 join_prop_lines(curwin->w_cursor.lnum, newp,
2135 prop_lines, prop_lengths, count);
2136 else
2137#endif
2138 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002140 if (setmark && !cmdmod.lockmarks)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002141 {
2142 /* Set the '] mark. */
2143 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002144 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002145 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 /* Only report the change in the first line here, del_lines() will report
2148 * the deleted line. */
2149 changed_lines(curwin->w_cursor.lnum, currsize,
2150 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002152 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 * briefly, and then move it back. After del_lines() the cursor may
2154 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
2156 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002158 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin->w_cursor.lnum = t;
2160
2161 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002162 * Set the cursor column:
2163 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002164 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002166 curwin->w_cursor.col =
2167 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002169
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 curwin->w_set_curswant = TRUE;
2172
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002173theend:
2174 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002175 if (remove_comments)
2176 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002177 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178}
2179
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180/*
2181 * Return TRUE if the two comment leaders given are the same. "lnum" is
2182 * the first line. White-space is ignored. Note that the whole of
2183 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
2184 */
2185 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002186same_leader(
2187 linenr_T lnum,
2188 int leader1_len,
2189 char_u *leader1_flags,
2190 int leader2_len,
2191 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 int idx1 = 0, idx2 = 0;
2194 char_u *p;
2195 char_u *line1;
2196 char_u *line2;
2197
2198 if (leader1_len == 0)
2199 return (leader2_len == 0);
2200
2201 /*
2202 * If first leader has 'f' flag, the lines can be joined only if the
2203 * second line does not have a leader.
2204 * If first leader has 'e' flag, the lines can never be joined.
2205 * If fist leader has 's' flag, the lines can only be joined if there is
2206 * some text after it and the second line has the 'm' flag.
2207 */
2208 if (leader1_flags != NULL)
2209 {
2210 for (p = leader1_flags; *p && *p != ':'; ++p)
2211 {
2212 if (*p == COM_FIRST)
2213 return (leader2_len == 0);
2214 if (*p == COM_END)
2215 return FALSE;
2216 if (*p == COM_START)
2217 {
2218 if (*(ml_get(lnum) + leader1_len) == NUL)
2219 return FALSE;
2220 if (leader2_flags == NULL || leader2_len == 0)
2221 return FALSE;
2222 for (p = leader2_flags; *p && *p != ':'; ++p)
2223 if (*p == COM_MIDDLE)
2224 return TRUE;
2225 return FALSE;
2226 }
2227 }
2228 }
2229
2230 /*
2231 * Get current line and next line, compare the leaders.
2232 * The first line has to be saved, only one line can be locked at a time.
2233 */
2234 line1 = vim_strsave(ml_get(lnum));
2235 if (line1 != NULL)
2236 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002237 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ;
2239 line2 = ml_get(lnum + 1);
2240 for (idx2 = 0; idx2 < leader2_len; ++idx2)
2241 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002242 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
2244 if (line1[idx1++] != line2[idx2])
2245 break;
2246 }
2247 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01002248 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 ++idx1;
2250 }
2251 vim_free(line1);
2252 }
2253 return (idx2 == leader2_len && idx1 == leader1_len);
2254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01002257 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002260op_format(
2261 oparg_T *oap,
2262 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 long old_line_count = curbuf->b_ml.ml_line_count;
2265
2266 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
2267 * can put it back there. */
2268 curwin->w_cursor = oap->cursor_start;
2269
2270 if (u_save((linenr_T)(oap->start.lnum - 1),
2271 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2272 return;
2273 curwin->w_cursor = oap->start;
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (oap->is_VIsual)
2276 /* When there is no change: need to remove the Visual selection */
2277 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002279 if (!cmdmod.lockmarks)
2280 /* Set '[ mark at the start of the formatted area */
2281 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 /* For "gw" remember the cursor position and put it back below (adjusted
2284 * for joined and split lines). */
2285 if (keep_cursor)
2286 saved_cursor = oap->cursor_start;
2287
Bram Moolenaar81a82092008-03-12 16:27:00 +00002288 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 /*
2291 * Leave the cursor at the first non-blank of the last formatted line.
2292 * If the cursor was moved one line back (e.g. with "Q}") go to the next
2293 * line, so "." will do the next lines.
2294 */
2295 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2296 ++curwin->w_cursor.lnum;
2297 beginline(BL_WHITE | BL_FIX);
2298 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
2299 msgmore(old_line_count);
2300
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002301 if (!cmdmod.lockmarks)
2302 /* put '] mark on the end of the formatted area */
2303 curbuf->b_op_end = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 if (keep_cursor)
2306 {
2307 curwin->w_cursor = saved_cursor;
2308 saved_cursor.lnum = 0;
2309 }
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (oap->is_VIsual)
2312 {
2313 win_T *wp;
2314
2315 FOR_ALL_WINDOWS(wp)
2316 {
2317 if (wp->w_old_cursor_lnum != 0)
2318 {
2319 /* When lines have been inserted or deleted, adjust the end of
2320 * the Visual area to be redrawn. */
2321 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
2322 wp->w_old_cursor_lnum += old_line_count;
2323 else
2324 wp->w_old_visual_lnum += old_line_count;
2325 }
2326 }
2327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328}
2329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002330#if defined(FEAT_EVAL) || defined(PROTO)
2331/*
2332 * Implementation of the format operator 'gq' for when using 'formatexpr'.
2333 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02002334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002335op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002336{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002337 if (oap->is_VIsual)
2338 /* When there is no change: need to remove the Visual selection */
2339 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002340
Bram Moolenaar700303e2010-07-11 17:35:50 +02002341 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
2342 /* As documented: when 'formatexpr' returns non-zero fall back to
2343 * internal formatting. */
2344 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002345}
2346
2347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002348fex_format(
2349 linenr_T lnum,
2350 long count,
2351 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002352{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002353 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
2354 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002355 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002356 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002357
2358 /*
2359 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002360 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 */
2362 set_vim_var_nr(VV_LNUM, lnum);
2363 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00002364 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002365
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002366 /* Make a copy, the option could be changed while calling it. */
2367 fex = vim_strsave(curbuf->b_p_fex);
2368 if (fex == NULL)
2369 return 0;
2370
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002371 /*
2372 * Evaluate the function.
2373 */
2374 if (use_sandbox)
2375 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002376 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002377 if (use_sandbox)
2378 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002379
2380 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02002381 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002383 return r;
2384}
2385#endif
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387/*
2388 * Format "line_count" lines, starting at the cursor position.
2389 * When "line_count" is negative, format until the end of the paragraph.
2390 * Lines after the cursor line are saved for undo, caller must have saved the
2391 * first line.
2392 */
2393 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002394format_lines(
2395 linenr_T line_count,
2396 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 int max_len;
2399 int is_not_par; /* current line not part of parag. */
2400 int next_is_not_par; /* next line not part of paragraph */
2401 int is_end_par; /* at end of paragraph */
2402 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
2403 int next_is_start_par = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 int leader_len = 0; /* leader len of current line */
2405 int next_leader_len; /* leader len of next line */
2406 char_u *leader_flags = NULL; /* flags for leader of current line */
2407 char_u *next_leader_flags; /* flags for leader of next line */
2408 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002409 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002411 int second_indent = -1; /* indent for second line (comment
2412 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 int do_second_indent;
2414 int do_number_indent;
2415 int do_trail_white;
2416 int first_par_line = TRUE;
2417 int smd_save;
2418 long count;
2419 int need_set_indent = TRUE; /* set indent of next paragraph */
2420 int force_format = FALSE;
2421 int old_State = State;
2422
2423 /* length of a line to force formatting: 3 * 'tw' */
2424 max_len = comp_textwidth(TRUE) * 3;
2425
2426 /* check for 'q', '2' and '1' in 'formatoptions' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 do_second_indent = has_format_option(FO_Q_SECOND);
2429 do_number_indent = has_format_option(FO_Q_NUMBER);
2430 do_trail_white = has_format_option(FO_WHITE_PAR);
2431
2432 /*
2433 * Get info about the previous and current line.
2434 */
2435 if (curwin->w_cursor.lnum > 1)
2436 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002437 , &leader_len, &leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 else
2439 is_not_par = TRUE;
2440 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002441 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 is_end_par = (is_not_par || next_is_not_par);
2443 if (!is_end_par && do_trail_white)
2444 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
2445
2446 curwin->w_cursor.lnum--;
2447 for (count = line_count; count != 0 && !got_int; --count)
2448 {
2449 /*
2450 * Advance to next paragraph.
2451 */
2452 if (advance)
2453 {
2454 curwin->w_cursor.lnum++;
2455 prev_is_end_par = is_end_par;
2456 is_not_par = next_is_not_par;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 leader_len = next_leader_len;
2458 leader_flags = next_leader_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
2461 /*
2462 * The last line to be formatted.
2463 */
2464 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2465 {
2466 next_is_not_par = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 next_leader_len = 0;
2468 next_leader_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470 else
2471 {
2472 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002473 , &next_leader_len, &next_leader_flags, do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 if (do_number_indent)
2475 next_is_start_par =
2476 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
2477 }
2478 advance = TRUE;
2479 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
2480 if (!is_end_par && do_trail_white)
2481 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
2482
2483 /*
2484 * Skip lines that are not in a paragraph.
2485 */
2486 if (is_not_par)
2487 {
2488 if (line_count < 0)
2489 break;
2490 }
2491 else
2492 {
2493 /*
2494 * For the first line of a paragraph, check indent of second line.
2495 * Don't do this for comments and empty lines.
2496 */
2497 if (first_par_line
2498 && (do_second_indent || do_number_indent)
2499 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002500 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002502 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002503 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002504 if (leader_len == 0 && next_leader_len == 0)
2505 {
2506 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002507 second_indent =
2508 get_indent_lnum(curwin->w_cursor.lnum + 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002509 }
2510 else
2511 {
2512 second_indent = next_leader_len;
2513 do_comments_list = 1;
2514 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002517 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002518 if (leader_len == 0 && next_leader_len == 0)
2519 {
2520 /* no comment found */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002521 second_indent =
2522 get_number_indent(curwin->w_cursor.lnum);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002523 }
2524 else
2525 {
2526 /* get_number_indent() is now "comment aware"... */
2527 second_indent =
2528 get_number_indent(curwin->w_cursor.lnum);
2529 do_comments_list = 1;
2530 }
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533
2534 /*
2535 * When the comment leader changes, it's the end of the paragraph.
2536 */
2537 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 || !same_leader(curwin->w_cursor.lnum,
2539 leader_len, leader_flags,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002540 next_leader_len, next_leader_flags))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 is_end_par = TRUE;
2542
2543 /*
2544 * If we have got to the end of a paragraph, or the line is
2545 * getting long, format it.
2546 */
2547 if (is_end_par || force_format)
2548 {
2549 if (need_set_indent)
2550 /* replace indent in first line with minimal number of
2551 * tabs and spaces, according to current options */
2552 (void)set_indent(get_indent(), SIN_CHANGED);
2553
2554 /* put cursor on last non-space */
2555 State = NORMAL; /* don't go past end-of-line */
2556 coladvance((colnr_T)MAXCOL);
2557 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
2558 dec_cursor();
2559
2560 /* do the formatting, without 'showmode' */
2561 State = INSERT; /* for open_line() */
2562 smd_save = p_smd;
2563 p_smd = FALSE;
2564 insertchar(NUL, INSCHAR_FORMAT
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02002566 + (do_comments && do_comments_list
2567 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar81a82092008-03-12 16:27:00 +00002568 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 State = old_State;
2570 p_smd = smd_save;
2571 second_indent = -1;
2572 /* at end of par.: need to set indent of next par. */
2573 need_set_indent = is_end_par;
2574 if (is_end_par)
2575 {
2576 /* When called with a negative line count, break at the
2577 * end of the paragraph. */
2578 if (line_count < 0)
2579 break;
2580 first_par_line = TRUE;
2581 }
2582 force_format = FALSE;
2583 }
2584
2585 /*
2586 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002587 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
2589 if (!is_end_par)
2590 {
2591 advance = FALSE;
2592 curwin->w_cursor.lnum++;
2593 curwin->w_cursor.col = 0;
2594 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002597 {
2598 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002600 (long)-next_leader_len, 0);
2601 }
2602 else if (second_indent > 0) // the "leader" for FO_Q_SECOND
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002603 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002604 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002605
2606 if (indent > 0)
2607 {
2608 (void)del_bytes(indent, FALSE, FALSE);
2609 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002610 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01002611 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02002612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002614 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 beep_flush();
2617 break;
2618 }
2619 first_par_line = FALSE;
2620 /* If the line is getting long, format it next time */
2621 if (STRLEN(ml_get_curline()) > (size_t)max_len)
2622 force_format = TRUE;
2623 else
2624 force_format = FALSE;
2625 }
2626 }
2627 line_breakcheck();
2628 }
2629}
2630
2631/*
2632 * Return TRUE if line "lnum" ends in a white character.
2633 */
2634 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002635ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 char_u *s = ml_get(lnum);
2638 size_t l;
2639
2640 if (*s == NUL)
2641 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002642 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 * invocation may call function multiple times". */
2644 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002645 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646}
2647
2648/*
2649 * Blank lines, and lines containing only the comment leader, are left
2650 * untouched by the formatting. The function returns TRUE in this
2651 * case. It also returns TRUE when a line starts with the end of a comment
2652 * ('e' in comment flags), so that this line is skipped, and not joined to the
2653 * previous line. A new paragraph starts after a blank line, or when the
2654 * comment leader changes -- webb.
2655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002657fmt_check_par(
2658 linenr_T lnum,
2659 int *leader_len,
2660 char_u **leader_flags,
2661 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 char_u *flags = NULL; /* init for GCC */
2664 char_u *ptr;
2665
2666 ptr = ml_get(lnum);
2667 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02002668 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 else
2670 *leader_len = 0;
2671
2672 if (*leader_len > 0)
2673 {
2674 /*
2675 * Search for 'e' flag in comment leader flags.
2676 */
2677 flags = *leader_flags;
2678 while (*flags && *flags != ':' && *flags != COM_END)
2679 ++flags;
2680 }
2681
2682 return (*skipwhite(ptr + *leader_len) == NUL
2683 || (*leader_len > 0 && *flags == COM_END)
2684 || startPS(lnum, NUL, FALSE));
2685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687/*
2688 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
2689 * previous line is in the same paragraph. Used for auto-formatting.
2690 */
2691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002692paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int leader_len = 0; /* leader len of current line */
2696 char_u *leader_flags = NULL; /* flags for leader of current line */
2697 int next_leader_len; /* leader len of next line */
2698 char_u *next_leader_flags; /* flags for leader of next line */
2699 int do_comments; /* format comments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 if (lnum <= 1)
2702 return TRUE; /* start of the file */
2703
2704 p = ml_get(lnum - 1);
2705 if (*p == NUL)
2706 return TRUE; /* after empty line */
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 do_comments = has_format_option(FO_Q_COMS);
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002709 if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 return TRUE; /* after non-paragraph line */
2711
Bram Moolenaar8c96af92019-09-28 19:05:57 +02002712 if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return TRUE; /* "lnum" is not a paragraph line */
2714
2715 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
2716 return TRUE; /* missing trailing space in previous line. */
2717
2718 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
2719 return TRUE; /* numbered item starts in "lnum". */
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (!same_leader(lnum - 1, leader_len, leader_flags,
2722 next_leader_len, next_leader_flags))
2723 return TRUE; /* change of comment leader. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 return FALSE;
2726}
2727
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728/*
2729 * prepare a few things for block mode yank/delete/tilde
2730 *
2731 * for delete:
2732 * - textlen includes the first/last char to be (partly) deleted
2733 * - start/endspaces is the number of columns that are taken by the
2734 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002735 * deleted.
2736 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * - textlen includes the first/last char to be wholly yanked
2738 * - start/endspaces is the number of columns of the first/last yanked char
2739 * that are to be yanked.
2740 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742block_prep(
2743 oparg_T *oap,
2744 struct block_def *bdp,
2745 linenr_T lnum,
2746 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 int incr = 0;
2749 char_u *pend;
2750 char_u *pstart;
2751 char_u *line;
2752 char_u *prev_pstart;
2753 char_u *prev_pend;
2754
2755 bdp->startspaces = 0;
2756 bdp->endspaces = 0;
2757 bdp->textlen = 0;
2758 bdp->start_vcol = 0;
2759 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 bdp->is_short = FALSE;
2761 bdp->is_oneChar = FALSE;
2762 bdp->pre_whitesp = 0;
2763 bdp->pre_whitesp_c = 0;
2764 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 bdp->start_char_vcols = 0;
2766
2767 line = ml_get(lnum);
2768 pstart = line;
2769 prev_pstart = line;
2770 while (bdp->start_vcol < oap->start_vcol && *pstart)
2771 {
2772 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002773 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002775 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
2777 bdp->pre_whitesp += incr;
2778 bdp->pre_whitesp_c++;
2779 }
2780 else
2781 {
2782 bdp->pre_whitesp = 0;
2783 bdp->pre_whitesp_c = 0;
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002786 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 bdp->start_char_vcols = incr;
2789 if (bdp->start_vcol < oap->start_vcol) /* line too short */
2790 {
2791 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (!is_del || oap->op_type == OP_APPEND)
2794 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2795 }
2796 else
2797 {
2798 /* notice: this converts partly selected Multibyte characters to
2799 * spaces, too. */
2800 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2801 if (is_del && bdp->startspaces)
2802 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2803 pend = pstart;
2804 bdp->end_vcol = bdp->start_vcol;
2805 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
2806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (oap->op_type == OP_INSERT)
2809 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2810 else if (oap->op_type == OP_APPEND)
2811 {
2812 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2813 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2814 }
2815 else
2816 {
2817 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2818 if (is_del && oap->op_type != OP_LSHIFT)
2819 {
2820 /* just putting the sum of those two into
2821 * bdp->startspaces doesn't work for Visual replace,
2822 * so we have to split the tab in two */
2823 bdp->startspaces = bdp->start_char_vcols
2824 - (bdp->start_vcol - oap->start_vcol);
2825 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2826 }
2827 }
2828 }
2829 else
2830 {
2831 prev_pend = pend;
2832 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2833 {
2834 /* Count a tab for what it's worth (if list mode not on) */
2835 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002836 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 bdp->end_vcol += incr;
2838 }
2839 if (bdp->end_vcol <= oap->end_vcol
2840 && (!is_del
2841 || oap->op_type == OP_APPEND
2842 || oap->op_type == OP_REPLACE)) /* line too short */
2843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 /* Alternative: include spaces to fill up the block.
2846 * Disadvantage: can lead to trailing spaces when the line is
2847 * short where the text is put */
2848 /* if (!is_del || oap->op_type == OP_APPEND) */
2849 if (oap->op_type == OP_APPEND || virtual_op)
2850 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002851 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else
2853 bdp->endspaces = 0; /* replace doesn't add characters */
2854 }
2855 else if (bdp->end_vcol > oap->end_vcol)
2856 {
2857 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2858 if (!is_del && bdp->endspaces)
2859 {
2860 bdp->endspaces = incr - bdp->endspaces;
2861 if (pend != pstart)
2862 pend = prev_pend;
2863 }
2864 }
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (is_del && bdp->startspaces)
2868 pstart = prev_pstart;
2869 bdp->textlen = (int)(pend - pstart);
2870 }
2871 bdp->textcol = (colnr_T) (pstart - line);
2872 bdp->textstart = pstart;
2873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002876 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879op_addsub(
2880 oparg_T *oap,
2881 linenr_T Prenum1, /* Amount of add/subtract */
2882 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002884 pos_T pos;
2885 struct block_def bd;
2886 int change_cnt = 0;
2887 linenr_T amount = Prenum1;
2888
Bram Moolenaar6b731882018-11-16 20:54:47 +01002889 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002890 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002891 // the call to changed_lines().
2892#ifdef FEAT_FOLDING
2893 disable_fold_update++;
2894#endif
2895
Bram Moolenaard79e5502016-01-10 22:13:02 +01002896 if (!VIsual_active)
2897 {
2898 pos = curwin->w_cursor;
2899 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002900 {
2901#ifdef FEAT_FOLDING
2902 disable_fold_update--;
2903#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002904 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002905 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002906 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002907#ifdef FEAT_FOLDING
2908 disable_fold_update--;
2909#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002910 if (change_cnt)
2911 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2912 }
2913 else
2914 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002915 int one_change;
2916 int length;
2917 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002918
2919 if (u_save((linenr_T)(oap->start.lnum - 1),
2920 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002921 {
2922#ifdef FEAT_FOLDING
2923 disable_fold_update--;
2924#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002925 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002926 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002927
2928 pos = oap->start;
2929 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2930 {
2931 if (oap->block_mode) /* Visual block mode */
2932 {
2933 block_prep(oap, &bd, pos.lnum, FALSE);
2934 pos.col = bd.textcol;
2935 length = bd.textlen;
2936 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002937 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002938 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002939 curwin->w_cursor.col = 0;
2940 pos.col = 0;
2941 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2942 }
2943 else /* oap->motion_type == MCHAR */
2944 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002945 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002946 dec(&(oap->end));
2947 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2948 pos.col = 0;
2949 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002950 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002951 pos.col += oap->start.col;
2952 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002953 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002954 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002955 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002956 length = (int)STRLEN(ml_get(oap->end.lnum));
2957 if (oap->end.col >= length)
2958 oap->end.col = length - 1;
2959 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002960 }
2961 }
2962 one_change = do_addsub(oap->op_type, &pos, length, amount);
2963 if (one_change)
2964 {
2965 /* Remember the start position of the first change. */
2966 if (change_cnt == 0)
2967 startpos = curbuf->b_op_start;
2968 ++change_cnt;
2969 }
2970
2971#ifdef FEAT_NETBEANS_INTG
2972 if (netbeans_active() && one_change)
2973 {
2974 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2975
2976 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2977 netbeans_inserted(curbuf, pos.lnum, pos.col,
2978 &ptr[pos.col], length);
2979 }
2980#endif
2981 if (g_cmd && one_change)
2982 amount += Prenum1;
2983 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002984
2985#ifdef FEAT_FOLDING
2986 disable_fold_update--;
2987#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002988 if (change_cnt)
2989 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2990
2991 if (!change_cnt && oap->is_VIsual)
2992 /* No change: need to remove the Visual selection */
2993 redraw_curbuf_later(INVERTED);
2994
2995 /* Set '[ mark if something changed. Keep the last end
2996 * position from do_addsub(). */
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002997 if (change_cnt > 0 && !cmdmod.lockmarks)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002998 curbuf->b_op_start = startpos;
2999
3000 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003002 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003003 }
3004}
3005
3006/*
3007 * Add or subtract 'Prenum1' from a number in a line
3008 * op_type is OP_NR_ADD or OP_NR_SUB
3009 *
3010 * Returns TRUE if some character was changed.
3011 */
3012 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003013do_addsub(
3014 int op_type,
3015 pos_T *pos,
3016 int length,
3017 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003018{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 int col;
3020 char_u *buf1;
3021 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003022 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003024 uvarnumber_T n;
3025 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 char_u *ptr;
3027 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 int todel;
3029 int dohex;
3030 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003031 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 int doalp;
3033 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003035 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003036 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003037 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02003038 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003039 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003040 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003041 pos_T startpos;
3042 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaardac13472019-09-16 21:06:21 +02003044 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
3045 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
3046 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
3047 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
Bram Moolenaard79e5502016-01-10 22:13:02 +01003049 curwin->w_cursor = *pos;
3050 ptr = ml_get(pos->lnum);
3051 col = pos->col;
3052
3053 if (*ptr == NUL)
3054 goto theend;
3055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 /*
3057 * First check if we are on a hexadecimal number, after the "0x".
3058 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003059 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003061 if (dobin)
3062 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003063 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003064 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003065 if (has_mbyte)
3066 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003067 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003068
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003069 if (dohex)
3070 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003071 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003072 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003073 if (has_mbyte)
3074 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003075 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003076
3077 if ( dobin
3078 && dohex
3079 && ! ((col > 0
3080 && (ptr[col] == 'X'
3081 || ptr[col] == 'x')
3082 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003083 && (!has_mbyte ||
3084 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003085 && vim_isxdigit(ptr[col + 1]))))
3086 {
3087
3088 /* In case of binary/hexadecimal pattern overlap match, rescan */
3089
Bram Moolenaard79e5502016-01-10 22:13:02 +01003090 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003091
3092 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003093 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003094 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003095 if (has_mbyte)
3096 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003097 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003098 }
3099
3100 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003101 && col > 0
3102 && (ptr[col] == 'X'
3103 || ptr[col] == 'x')
3104 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003105 && (!has_mbyte ||
3106 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003107 && vim_isxdigit(ptr[col + 1])) ||
3108 ( dobin
3109 && col > 0
3110 && (ptr[col] == 'B'
3111 || ptr[col] == 'b')
3112 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003113 && (!has_mbyte ||
3114 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003115 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003116 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01003117 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003118 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003119 if (has_mbyte)
3120 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003121 }
3122 else
3123 {
3124 /*
3125 * Search forward and then backward to find the start of number.
3126 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01003127 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003128
3129 while (ptr[col] != NUL
3130 && !vim_isdigit(ptr[col])
3131 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02003132 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003133
3134 while (col > 0
3135 && vim_isdigit(ptr[col - 1])
3136 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003137 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003138 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003139 if (has_mbyte)
3140 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003141 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003142 }
3143 }
3144
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003145 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003146 {
3147 while (ptr[col] != NUL && length > 0
3148 && !vim_isdigit(ptr[col])
3149 && !(doalp && ASCII_ISALPHA(ptr[col])))
3150 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02003151 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003152
3153 col += mb_len;
3154 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003155 }
3156
3157 if (length == 0)
3158 goto theend;
3159
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003160 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003161 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003162 {
3163 negative = TRUE;
3164 was_positive = FALSE;
3165 }
3166 }
3167
3168 /*
3169 * If a number was found, and saving for undo works, replace the number.
3170 */
3171 firstdigit = ptr[col];
3172 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
3173 {
3174 beep_flush();
3175 goto theend;
3176 }
3177
3178 if (doalp && ASCII_ISALPHA(firstdigit))
3179 {
3180 /* decrement or increment alphabetic character */
3181 if (op_type == OP_NR_SUB)
3182 {
3183 if (CharOrd(firstdigit) < Prenum1)
3184 {
3185 if (isupper(firstdigit))
3186 firstdigit = 'A';
3187 else
3188 firstdigit = 'a';
3189 }
3190 else
3191#ifdef EBCDIC
3192 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
3193#else
3194 firstdigit -= Prenum1;
3195#endif
3196 }
3197 else
3198 {
3199 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
3200 {
3201 if (isupper(firstdigit))
3202 firstdigit = 'Z';
3203 else
3204 firstdigit = 'z';
3205 }
3206 else
3207#ifdef EBCDIC
3208 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
3209#else
3210 firstdigit += Prenum1;
3211#endif
3212 }
3213 curwin->w_cursor.col = col;
3214 if (!did_change)
3215 startpos = curwin->w_cursor;
3216 did_change = TRUE;
3217 (void)del_char(FALSE);
3218 ins_char(firstdigit);
3219 endpos = curwin->w_cursor;
3220 curwin->w_cursor.col = col;
3221 }
3222 else
3223 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003224 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003225 && (!has_mbyte ||
3226 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02003227 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01003228 {
3229 /* negative number */
3230 --col;
3231 negative = TRUE;
3232 }
3233 /* get the number value (unsigned) */
3234 if (visual && VIsual_mode != 'V')
3235 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
3236 ? (int)STRLEN(ptr) - col
3237 : length);
3238
3239 vim_str2nr(ptr + col, &pre, &length,
3240 0 + (dobin ? STR2NR_BIN : 0)
3241 + (dooct ? STR2NR_OCT : 0)
3242 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02003243 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003244
3245 /* ignore leading '-' for hex and octal and bin numbers */
3246 if (pre && negative)
3247 {
3248 ++col;
3249 --length;
3250 negative = FALSE;
3251 }
3252 /* add or subtract */
3253 subtract = FALSE;
3254 if (op_type == OP_NR_SUB)
3255 subtract ^= TRUE;
3256 if (negative)
3257 subtract ^= TRUE;
3258
3259 oldn = n;
3260 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003261 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003262 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003263 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003264 /* handle wraparound for decimal numbers */
3265 if (!pre)
3266 {
3267 if (subtract)
3268 {
3269 if (n > oldn)
3270 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003271 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003272 negative ^= TRUE;
3273 }
3274 }
3275 else
3276 {
3277 /* add */
3278 if (n < oldn)
3279 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003280 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003281 negative ^= TRUE;
3282 }
3283 }
3284 if (n == 0)
3285 negative = FALSE;
3286 }
3287
3288 if (visual && !was_positive && !negative && col > 0)
3289 {
3290 /* need to remove the '-' */
3291 col--;
3292 length++;
3293 }
3294
3295 /*
3296 * Delete the old number.
3297 */
3298 curwin->w_cursor.col = col;
3299 if (!did_change)
3300 startpos = curwin->w_cursor;
3301 did_change = TRUE;
3302 todel = length;
3303 c = gchar_cursor();
3304 /*
3305 * Don't include the '-' in the length, only the length of the
3306 * part after it is kept the same.
3307 */
3308 if (c == '-')
3309 --length;
3310 while (todel-- > 0)
3311 {
3312 if (c < 0x100 && isalpha(c))
3313 {
3314 if (isupper(c))
3315 hexupper = TRUE;
3316 else
3317 hexupper = FALSE;
3318 }
3319 /* del_char() will mark line needing displaying */
3320 (void)del_char(FALSE);
3321 c = gchar_cursor();
3322 }
3323
3324 /*
3325 * Prepare the leading characters in buf1[].
3326 * When there are many leading zeros it could be very long.
3327 * Allocate a bit too much.
3328 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003329 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003330 if (buf1 == NULL)
3331 goto theend;
3332 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02003333 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01003334 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01003335 if (pre)
3336 {
3337 *ptr++ = '0';
3338 --length;
3339 }
3340 if (pre == 'b' || pre == 'B' ||
3341 pre == 'x' || pre == 'X')
3342 {
3343 *ptr++ = pre;
3344 --length;
3345 }
3346
3347 /*
3348 * Put the number characters in buf2[].
3349 */
3350 if (pre == 'b' || pre == 'B')
3351 {
3352 int i;
3353 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003354 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01003355
3356 /* leading zeros */
3357 for (bit = bits; bit > 0; bit--)
3358 if ((n >> (bit - 1)) & 0x1) break;
3359
3360 for (i = 0; bit > 0; bit--)
3361 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
3362
3363 buf2[i] = '\0';
3364 }
3365 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02003366 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003367 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003368 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02003369 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003370 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003371 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02003372 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003373 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003374 else
Bram Moolenaarea391762018-04-08 13:07:22 +02003375 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003376 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003377 length -= (int)STRLEN(buf2);
3378
3379 /*
3380 * Adjust number of zeros to the new number of digits, so the
3381 * total length of the number remains the same.
3382 * Don't do this when
3383 * the result may look like an octal number.
3384 */
3385 if (firstdigit == '0' && !(dooct && pre == 0))
3386 while (length-- > 0)
3387 *ptr++ = '0';
3388 *ptr = NUL;
3389 STRCAT(buf1, buf2);
3390 ins_str(buf1); /* insert the new number */
3391 vim_free(buf1);
3392 endpos = curwin->w_cursor;
3393 if (did_change && curwin->w_cursor.col)
3394 --curwin->w_cursor.col;
3395 }
3396
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003397 if (did_change && !cmdmod.lockmarks)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01003398 {
3399 /* set the '[ and '] marks */
3400 curbuf->b_op_start = startpos;
3401 curbuf->b_op_end = endpos;
3402 if (curbuf->b_op_end.col > 0)
3403 --curbuf->b_op_end.col;
3404 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003405
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003406theend:
3407 if (visual)
3408 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01003409 else if (did_change)
3410 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01003411
Bram Moolenaard79e5502016-01-10 22:13:02 +01003412 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413}
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#if defined(FEAT_CLIPBOARD) || defined(PROTO)
3416/*
3417 * SELECTION / PRIMARY ('*')
3418 *
3419 * Text selection stuff that uses the GUI selection register '*'. When using a
3420 * GUI this may be text from another window, otherwise it is the last text we
3421 * had highlighted with VIsual mode. With mouse support, clicking the middle
3422 * button performs the paste, otherwise you will need to do <"*p>. "
3423 * If not under X, it is synonymous with the clipboard register '+'.
3424 *
3425 * X CLIPBOARD ('+')
3426 *
3427 * Text selection stuff that uses the GUI clipboard register '+'.
3428 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
3429 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
3430 * otherwise you will need to do <"+p>. "
3431 * If not under X, it is synonymous with the selection register '*'.
3432 */
3433
3434/*
3435 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003436 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * only exist while the owning application exists, so we write to the
3438 * permanent (while X runs) store CUT_BUFFER0.
3439 * Dump the CLIPBOARD selection if we own it (it's logically the more
3440 * 'permanent' of the two), otherwise the PRIMARY one.
3441 * For now, use a hard-coded sanity limit of 1Mb of data.
3442 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003443#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003445x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 Display *dpy;
3448 char_u *str = NULL;
3449 long_u len = 0;
3450 int motion_type = -1;
3451
3452# ifdef FEAT_GUI
3453 if (gui.in_use)
3454 dpy = X_DISPLAY;
3455 else
3456# endif
3457# ifdef FEAT_XCLIPBOARD
3458 dpy = xterm_dpy;
3459# else
3460 return;
3461# endif
3462
3463 /* Get selection to export */
3464 if (clip_plus.owned)
3465 motion_type = clip_convert_selection(&str, &len, &clip_plus);
3466 else if (clip_star.owned)
3467 motion_type = clip_convert_selection(&str, &len, &clip_star);
3468
3469 /* Check it's OK */
3470 if (dpy != NULL && str != NULL && motion_type >= 0
3471 && len < 1024*1024 && len > 0)
3472 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003473 int ok = TRUE;
3474
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003475 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
3476 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
3477 * encoding conversion usually doesn't work, so keep the text as-is.
3478 */
3479 if (has_mbyte)
3480 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003481 vimconv_T vc;
3482
3483 vc.vc_type = CONV_NONE;
3484 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
3485 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003486 int intlen = len;
3487 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003488
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003489 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00003490 conv_str = string_convert(&vc, str, &intlen);
3491 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003492 if (conv_str != NULL)
3493 {
3494 vim_free(str);
3495 str = conv_str;
3496 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003497 else
3498 {
3499 ok = FALSE;
3500 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003501 convert_setup(&vc, NULL, NULL);
3502 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003503 else
3504 {
3505 ok = FALSE;
3506 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00003507 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003508
3509 /* Do not store the string if conversion failed. Better to use any
3510 * other selection than garbled text. */
3511 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01003512 {
3513 XStoreBuffer(dpy, (char *)str, (int)len, 0);
3514 XFlush(dpy);
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517
3518 vim_free(str);
3519}
3520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#endif /* FEAT_CLIPBOARD || PROTO */
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003524clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 vim_memset(oap, 0, sizeof(oparg_T));
3527}
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00003530 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 *
3532 * "Words" are counted by looking for boundaries between non-space and
3533 * space characters. (it seems to produce results that match 'wc'.)
3534 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00003535 * Return value is byte count; word count for the line is added to "*wc".
3536 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 *
3538 * The function will only examine the first "limit" characters in the
3539 * line, stopping if it encounters an end-of-line (NUL byte). In that
3540 * case, eol_size will be added to the character count to account for
3541 * the size of the EOL character.
3542 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003543 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01003544line_count_info(
3545 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003546 varnumber_T *wc,
3547 varnumber_T *cc,
3548 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01003549 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003551 varnumber_T i;
3552 varnumber_T words = 0;
3553 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 int is_word = 0;
3555
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02003556 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 if (is_word)
3559 {
3560 if (vim_isspace(line[i]))
3561 {
3562 words++;
3563 is_word = 0;
3564 }
3565 }
3566 else if (!vim_isspace(line[i]))
3567 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003568 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003569 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571
3572 if (is_word)
3573 words++;
3574 *wc += words;
3575
3576 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02003577 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003580 chars += eol_size;
3581 }
3582 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 return i;
3584}
3585
3586/*
3587 * Give some info about the position of the cursor (for "g CTRL-G").
3588 * In Visual mode, give some info about the selected region. (In this case,
3589 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003590 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 */
3592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003593cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003596 char_u buf1[50];
3597 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003599 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003600 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003601 varnumber_T byte_count_cursor = 0;
3602 varnumber_T char_count = 0;
3603 varnumber_T char_count_cursor = 0;
3604 varnumber_T word_count = 0;
3605 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003606 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003607 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 long line_count_selected = 0;
3609 pos_T min_pos, max_pos;
3610 oparg_T oparg;
3611 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 /*
3614 * Compute the length of the file in characters.
3615 */
3616 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3617 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003618 if (dict == NULL)
3619 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003620 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003621 return;
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else
3625 {
3626 if (get_fileformat(curbuf) == EOL_DOS)
3627 eol_size = 2;
3628 else
3629 eol_size = 1;
3630
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (VIsual_active)
3632 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003633 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 min_pos = VIsual;
3636 max_pos = curwin->w_cursor;
3637 }
3638 else
3639 {
3640 min_pos = curwin->w_cursor;
3641 max_pos = VIsual;
3642 }
3643 if (*p_sel == 'e' && max_pos.col > 0)
3644 --max_pos.col;
3645
3646 if (VIsual_mode == Ctrl_V)
3647 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003648#ifdef FEAT_LINEBREAK
3649 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003650 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003651
3652 /* Make 'sbr' empty for a moment to get the correct size. */
3653 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003654 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 oparg.is_VIsual = 1;
3657 oparg.block_mode = TRUE;
3658 oparg.op_type = OP_NOP;
3659 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003660 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003661#ifdef FEAT_LINEBREAK
3662 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003663 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003664#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003665 if (curwin->w_curswant == MAXCOL)
3666 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 /* Swap the start, end vcol if needed */
3668 if (oparg.end_vcol < oparg.start_vcol)
3669 {
3670 oparg.end_vcol += oparg.start_vcol;
3671 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3672 oparg.end_vcol -= oparg.start_vcol;
3673 }
3674 }
3675 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3679 {
3680 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003681 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
3683 ui_breakcheck();
3684 if (got_int)
3685 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003686 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 /* Do extra processing for VIsual mode. */
3690 if (VIsual_active
3691 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3692 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003693 char_u *s = NULL;
3694 long len = 0L;
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 switch (VIsual_mode)
3697 {
3698 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003702 s = bd.textstart;
3703 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 break;
3705 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003706 s = ml_get(lnum);
3707 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 break;
3709 case 'v':
3710 {
3711 colnr_T start_col = (lnum == min_pos.lnum)
3712 ? min_pos.col : 0;
3713 colnr_T end_col = (lnum == max_pos.lnum)
3714 ? max_pos.col - start_col + 1 : MAXCOL;
3715
Bram Moolenaardef9e822004-12-31 20:58:58 +00003716 s = ml_get(lnum) + start_col;
3717 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719 break;
3720 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003721 if (s != NULL)
3722 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003723 byte_count_cursor += line_count_info(s, &word_count_cursor,
3724 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003725 if (lnum == curbuf->b_ml.ml_line_count
3726 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003727 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003728 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003729 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 /* In non-visual mode, check for the line the cursor is on */
3735 if (lnum == curwin->w_cursor.lnum)
3736 {
3737 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003738 char_count_cursor += char_count;
3739 byte_count_cursor = byte_count +
3740 line_count_info(ml_get(lnum),
3741 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003742 (varnumber_T)(curwin->w_cursor.col + 1),
3743 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00003747 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003748 &char_count, (varnumber_T)MAXCOL,
3749 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751
3752 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003753 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003754 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
Bram Moolenaared767a22016-01-03 22:49:16 +01003756 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003758 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003760 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3761 {
3762 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3763 &max_pos.col);
3764 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3765 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3766 }
3767 else
3768 buf1[0] = NUL;
3769
3770 if (char_count_cursor == byte_count_cursor
3771 && char_count == byte_count)
3772 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003773 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003774 buf1, line_count_selected,
3775 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003776 (long_long_T)word_count_cursor,
3777 (long_long_T)word_count,
3778 (long_long_T)byte_count_cursor,
3779 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003780 else
3781 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003782 _("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 +01003783 buf1, line_count_selected,
3784 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003785 (long_long_T)word_count_cursor,
3786 (long_long_T)word_count,
3787 (long_long_T)char_count_cursor,
3788 (long_long_T)char_count,
3789 (long_long_T)byte_count_cursor,
3790 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003793 {
3794 p = ml_get_curline();
3795 validate_virtcol();
3796 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3797 (int)curwin->w_virtcol + 1);
3798 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3799 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
Bram Moolenaared767a22016-01-03 22:49:16 +01003801 if (char_count_cursor == byte_count_cursor
3802 && char_count == byte_count)
3803 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003804 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003805 (char *)buf1, (char *)buf2,
3806 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003808 (long_long_T)word_count_cursor, (long_long_T)word_count,
3809 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003810 else
3811 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003812 _("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 +01003813 (char *)buf1, (char *)buf2,
3814 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003815 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01003816 (long_long_T)word_count_cursor, (long_long_T)word_count,
3817 (long_long_T)char_count_cursor, (long_long_T)char_count,
3818 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003819 }
3820 }
3821
Bram Moolenaared767a22016-01-03 22:49:16 +01003822 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003823 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01003824 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003825 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003826 if (dict == NULL)
3827 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003828 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003829 p = p_shm;
3830 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003831 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003832 p_shm = p;
3833 }
3834 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003835#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003836 if (dict != NULL)
3837 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003838 dict_add_number(dict, "words", word_count);
3839 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003840 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003841 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3842 byte_count_cursor);
3843 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3844 char_count_cursor);
3845 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3846 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003850
3851/*
3852 * Handle indent and format operators and visual mode ":".
3853 */
3854 static void
3855op_colon(oparg_T *oap)
3856{
3857 stuffcharReadbuff(':');
3858 if (oap->is_VIsual)
3859 stuffReadbuff((char_u *)"'<,'>");
3860 else
3861 {
3862 // Make the range look nice, so it can be repeated.
3863 if (oap->start.lnum == curwin->w_cursor.lnum)
3864 stuffcharReadbuff('.');
3865 else
3866 stuffnumReadbuff((long)oap->start.lnum);
3867 if (oap->end.lnum != oap->start.lnum)
3868 {
3869 stuffcharReadbuff(',');
3870 if (oap->end.lnum == curwin->w_cursor.lnum)
3871 stuffcharReadbuff('.');
3872 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3873 stuffcharReadbuff('$');
3874 else if (oap->start.lnum == curwin->w_cursor.lnum)
3875 {
3876 stuffReadbuff((char_u *)".+");
3877 stuffnumReadbuff((long)oap->line_count - 1);
3878 }
3879 else
3880 stuffnumReadbuff((long)oap->end.lnum);
3881 }
3882 }
3883 if (oap->op_type != OP_COLON)
3884 stuffReadbuff((char_u *)"!");
3885 if (oap->op_type == OP_INDENT)
3886 {
3887#ifndef FEAT_CINDENT
3888 if (*get_equalprg() == NUL)
3889 stuffReadbuff((char_u *)"indent");
3890 else
3891#endif
3892 stuffReadbuff(get_equalprg());
3893 stuffReadbuff((char_u *)"\n");
3894 }
3895 else if (oap->op_type == OP_FORMAT)
3896 {
3897 if (*curbuf->b_p_fp != NUL)
3898 stuffReadbuff(curbuf->b_p_fp);
3899 else if (*p_fp != NUL)
3900 stuffReadbuff(p_fp);
3901 else
3902 stuffReadbuff((char_u *)"fmt");
3903 stuffReadbuff((char_u *)"\n']");
3904 }
3905
3906 // do_cmdline() does the rest
3907}
3908
3909/*
3910 * Handle the "g@" operator: call 'operatorfunc'.
3911 */
3912 static void
3913op_function(oparg_T *oap UNUSED)
3914{
3915#ifdef FEAT_EVAL
3916 typval_T argv[2];
3917 int save_virtual_op = virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003918 pos_T orig_start = curbuf->b_op_start;
3919 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003920
3921 if (*p_opfunc == NUL)
3922 emsg(_("E774: 'operatorfunc' is empty"));
3923 else
3924 {
3925 // Set '[ and '] marks to text to be operated on.
3926 curbuf->b_op_start = oap->start;
3927 curbuf->b_op_end = oap->end;
3928 if (oap->motion_type != MLINE && !oap->inclusive)
3929 // Exclude the end position.
3930 decl(&curbuf->b_op_end);
3931
3932 argv[0].v_type = VAR_STRING;
3933 if (oap->block_mode)
3934 argv[0].vval.v_string = (char_u *)"block";
3935 else if (oap->motion_type == MLINE)
3936 argv[0].vval.v_string = (char_u *)"line";
3937 else
3938 argv[0].vval.v_string = (char_u *)"char";
3939 argv[1].v_type = VAR_UNKNOWN;
3940
3941 // Reset virtual_op so that 'virtualedit' can be changed in the
3942 // function.
3943 virtual_op = MAYBE;
3944
3945 (void)call_func_retnr(p_opfunc, 1, argv);
3946
3947 virtual_op = save_virtual_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003948 if (cmdmod.lockmarks)
3949 {
3950 curbuf->b_op_start = orig_start;
3951 curbuf->b_op_end = orig_end;
3952 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003953 }
3954#else
3955 emsg(_("E775: Eval feature not available"));
3956#endif
3957}
3958
3959/*
3960 * Calculate start/end virtual columns for operating in block mode.
3961 */
3962 static void
3963get_op_vcol(
3964 oparg_T *oap,
3965 colnr_T redo_VIsual_vcol,
3966 int initial) // when TRUE adjust position for 'selectmode'
3967{
3968 colnr_T start, end;
3969
3970 if (VIsual_mode != Ctrl_V
3971 || (!initial && oap->end.col < curwin->w_width))
3972 return;
3973
3974 oap->block_mode = TRUE;
3975
3976 // prevent from moving onto a trail byte
3977 if (has_mbyte)
3978 mb_adjustpos(curwin->w_buffer, &oap->end);
3979
3980 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3981
3982 if (!redo_VIsual_busy)
3983 {
3984 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3985
3986 if (start < oap->start_vcol)
3987 oap->start_vcol = start;
3988 if (end > oap->end_vcol)
3989 {
3990 if (initial && *p_sel == 'e' && start >= 1
3991 && start - 1 >= oap->end_vcol)
3992 oap->end_vcol = start - 1;
3993 else
3994 oap->end_vcol = end;
3995 }
3996 }
3997
3998 // if '$' was used, get oap->end_vcol from longest line
3999 if (curwin->w_curswant == MAXCOL)
4000 {
4001 curwin->w_cursor.col = MAXCOL;
4002 oap->end_vcol = 0;
4003 for (curwin->w_cursor.lnum = oap->start.lnum;
4004 curwin->w_cursor.lnum <= oap->end.lnum;
4005 ++curwin->w_cursor.lnum)
4006 {
4007 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
4008 if (end > oap->end_vcol)
4009 oap->end_vcol = end;
4010 }
4011 }
4012 else if (redo_VIsual_busy)
4013 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
4014 // Correct oap->end.col and oap->start.col to be the
4015 // upper-left and lower-right corner of the block area.
4016 //
4017 // (Actually, this does convert column positions into character
4018 // positions)
4019 curwin->w_cursor.lnum = oap->end.lnum;
4020 coladvance(oap->end_vcol);
4021 oap->end = curwin->w_cursor;
4022
4023 curwin->w_cursor = oap->start;
4024 coladvance(oap->start_vcol);
4025 oap->start = curwin->w_cursor;
4026}
4027
4028/*
4029 * Handle an operator after Visual mode or when the movement is finished.
4030 * "gui_yank" is true when yanking text for the clipboard.
4031 */
4032 void
4033do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
4034{
4035 oparg_T *oap = cap->oap;
4036 pos_T old_cursor;
4037 int empty_region_error;
4038 int restart_edit_save;
4039#ifdef FEAT_LINEBREAK
4040 int lbr_saved = curwin->w_p_lbr;
4041#endif
4042
4043 // The visual area is remembered for redo
4044 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
4045 static linenr_T redo_VIsual_line_count; // number of lines
4046 static colnr_T redo_VIsual_vcol; // number of cols or end column
4047 static long redo_VIsual_count; // count for Visual operator
4048 static int redo_VIsual_arg; // extra argument
4049 int include_line_break = FALSE;
4050
4051#if defined(FEAT_CLIPBOARD)
4052 // Yank the visual area into the GUI selection register before we operate
4053 // on it and lose it forever.
4054 // Don't do it if a specific register was specified, so that ""x"*P works.
4055 // This could call do_pending_operator() recursively, but that's OK
4056 // because gui_yank will be TRUE for the nested call.
4057 if ((clip_star.available || clip_plus.available)
4058 && oap->op_type != OP_NOP
4059 && !gui_yank
4060 && VIsual_active
4061 && !redo_VIsual_busy
4062 && oap->regname == 0)
4063 clip_auto_select();
4064#endif
4065 old_cursor = curwin->w_cursor;
4066
4067 // If an operation is pending, handle it...
4068 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
4069 {
4070 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
4071 // for the clipboard.
4072 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
4073
4074#ifdef FEAT_LINEBREAK
4075 // Avoid a problem with unwanted linebreaks in block mode.
4076 if (curwin->w_p_lbr)
4077 curwin->w_valid &= ~VALID_VIRTCOL;
4078 curwin->w_p_lbr = FALSE;
4079#endif
4080 oap->is_VIsual = VIsual_active;
4081 if (oap->motion_force == 'V')
4082 oap->motion_type = MLINE;
4083 else if (oap->motion_force == 'v')
4084 {
4085 // If the motion was linewise, "inclusive" will not have been set.
4086 // Use "exclusive" to be consistent. Makes "dvj" work nice.
4087 if (oap->motion_type == MLINE)
4088 oap->inclusive = FALSE;
4089 // If the motion already was characterwise, toggle "inclusive"
4090 else if (oap->motion_type == MCHAR)
4091 oap->inclusive = !oap->inclusive;
4092 oap->motion_type = MCHAR;
4093 }
4094 else if (oap->motion_force == Ctrl_V)
4095 {
4096 // Change line- or characterwise motion into Visual block mode.
4097 if (!VIsual_active)
4098 {
4099 VIsual_active = TRUE;
4100 VIsual = oap->start;
4101 }
4102 VIsual_mode = Ctrl_V;
4103 VIsual_select = FALSE;
4104 VIsual_reselect = FALSE;
4105 }
4106
4107 // Only redo yank when 'y' flag is in 'cpoptions'.
4108 // Never redo "zf" (define fold).
4109 if ((redo_yank || oap->op_type != OP_YANK)
4110 && ((!VIsual_active || oap->motion_force)
4111 // Also redo Operator-pending Visual mode mappings
4112 || (VIsual_active && cap->cmdchar == ':'
4113 && oap->op_type != OP_COLON))
4114 && cap->cmdchar != 'D'
4115#ifdef FEAT_FOLDING
4116 && oap->op_type != OP_FOLD
4117 && oap->op_type != OP_FOLDOPEN
4118 && oap->op_type != OP_FOLDOPENREC
4119 && oap->op_type != OP_FOLDCLOSE
4120 && oap->op_type != OP_FOLDCLOSEREC
4121 && oap->op_type != OP_FOLDDEL
4122 && oap->op_type != OP_FOLDDELREC
4123#endif
4124 )
4125 {
4126 prep_redo(oap->regname, cap->count0,
4127 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4128 oap->motion_force, cap->cmdchar, cap->nchar);
4129 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
4130 {
4131 // If 'cpoptions' does not contain 'r', insert the search
4132 // pattern to really repeat the same command.
4133 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
4134 AppendToRedobuffLit(cap->searchbuf, -1);
4135 AppendToRedobuff(NL_STR);
4136 }
4137 else if (cap->cmdchar == ':')
4138 {
4139 // do_cmdline() has stored the first typed line in
4140 // "repeat_cmdline". When several lines are typed repeating
4141 // won't be possible.
4142 if (repeat_cmdline == NULL)
4143 ResetRedobuff();
4144 else
4145 {
4146 AppendToRedobuffLit(repeat_cmdline, -1);
4147 AppendToRedobuff(NL_STR);
4148 VIM_CLEAR(repeat_cmdline);
4149 }
4150 }
4151 }
4152
4153 if (redo_VIsual_busy)
4154 {
4155 // Redo of an operation on a Visual area. Use the same size from
4156 // redo_VIsual_line_count and redo_VIsual_vcol.
4157 oap->start = curwin->w_cursor;
4158 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
4159 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4160 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4161 VIsual_mode = redo_VIsual_mode;
4162 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
4163 {
4164 if (VIsual_mode == 'v')
4165 {
4166 if (redo_VIsual_line_count <= 1)
4167 {
4168 validate_virtcol();
4169 curwin->w_curswant =
4170 curwin->w_virtcol + redo_VIsual_vcol - 1;
4171 }
4172 else
4173 curwin->w_curswant = redo_VIsual_vcol;
4174 }
4175 else
4176 {
4177 curwin->w_curswant = MAXCOL;
4178 }
4179 coladvance(curwin->w_curswant);
4180 }
4181 cap->count0 = redo_VIsual_count;
4182 if (redo_VIsual_count != 0)
4183 cap->count1 = redo_VIsual_count;
4184 else
4185 cap->count1 = 1;
4186 }
4187 else if (VIsual_active)
4188 {
4189 if (!gui_yank)
4190 {
4191 // Save the current VIsual area for '< and '> marks, and "gv"
4192 curbuf->b_visual.vi_start = VIsual;
4193 curbuf->b_visual.vi_end = curwin->w_cursor;
4194 curbuf->b_visual.vi_mode = VIsual_mode;
4195 restore_visual_mode();
4196 curbuf->b_visual.vi_curswant = curwin->w_curswant;
4197# ifdef FEAT_EVAL
4198 curbuf->b_visual_mode_eval = VIsual_mode;
4199# endif
4200 }
4201
4202 // In Select mode, a linewise selection is operated upon like a
4203 // characterwise selection.
4204 // Special case: gH<Del> deletes the last line.
4205 if (VIsual_select && VIsual_mode == 'V'
4206 && cap->oap->op_type != OP_DELETE)
4207 {
4208 if (LT_POS(VIsual, curwin->w_cursor))
4209 {
4210 VIsual.col = 0;
4211 curwin->w_cursor.col =
4212 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
4213 }
4214 else
4215 {
4216 curwin->w_cursor.col = 0;
4217 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
4218 }
4219 VIsual_mode = 'v';
4220 }
4221 // If 'selection' is "exclusive", backup one character for
4222 // charwise selections.
4223 else if (VIsual_mode == 'v')
4224 include_line_break = unadjust_for_sel();
4225
4226 oap->start = VIsual;
4227 if (VIsual_mode == 'V')
4228 {
4229 oap->start.col = 0;
4230 oap->start.coladd = 0;
4231 }
4232 }
4233
4234 // Set oap->start to the first position of the operated text, oap->end
4235 // to the end of the operated text. w_cursor is equal to oap->start.
4236 if (LT_POS(oap->start, curwin->w_cursor))
4237 {
4238#ifdef FEAT_FOLDING
4239 // Include folded lines completely.
4240 if (!VIsual_active)
4241 {
4242 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
4243 oap->start.col = 0;
4244 if ((curwin->w_cursor.col > 0 || oap->inclusive)
4245 && hasFolding(curwin->w_cursor.lnum, NULL,
4246 &curwin->w_cursor.lnum))
4247 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
4248 }
4249#endif
4250 oap->end = curwin->w_cursor;
4251 curwin->w_cursor = oap->start;
4252
4253 // w_virtcol may have been updated; if the cursor goes back to its
4254 // previous position w_virtcol becomes invalid and isn't updated
4255 // automatically.
4256 curwin->w_valid &= ~VALID_VIRTCOL;
4257 }
4258 else
4259 {
4260#ifdef FEAT_FOLDING
4261 // Include folded lines completely.
4262 if (!VIsual_active && oap->motion_type == MLINE)
4263 {
4264 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
4265 NULL))
4266 curwin->w_cursor.col = 0;
4267 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
4268 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
4269 }
4270#endif
4271 oap->end = oap->start;
4272 oap->start = curwin->w_cursor;
4273 }
4274
4275 // Just in case lines were deleted that make the position invalid.
4276 check_pos(curwin->w_buffer, &oap->end);
4277 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
4278
4279 // Set "virtual_op" before resetting VIsual_active.
4280 virtual_op = virtual_active();
4281
4282 if (VIsual_active || redo_VIsual_busy)
4283 {
4284 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
4285
4286 if (!redo_VIsual_busy && !gui_yank)
4287 {
4288 // Prepare to reselect and redo Visual: this is based on the
4289 // size of the Visual text
4290 resel_VIsual_mode = VIsual_mode;
4291 if (curwin->w_curswant == MAXCOL)
4292 resel_VIsual_vcol = MAXCOL;
4293 else
4294 {
4295 if (VIsual_mode != Ctrl_V)
4296 getvvcol(curwin, &(oap->end),
4297 NULL, NULL, &oap->end_vcol);
4298 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
4299 {
4300 if (VIsual_mode != Ctrl_V)
4301 getvvcol(curwin, &(oap->start),
4302 &oap->start_vcol, NULL, NULL);
4303 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
4304 }
4305 else
4306 resel_VIsual_vcol = oap->end_vcol;
4307 }
4308 resel_VIsual_line_count = oap->line_count;
4309 }
4310
4311 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
4312 if ((redo_yank || oap->op_type != OP_YANK)
4313 && oap->op_type != OP_COLON
4314#ifdef FEAT_FOLDING
4315 && oap->op_type != OP_FOLD
4316 && oap->op_type != OP_FOLDOPEN
4317 && oap->op_type != OP_FOLDOPENREC
4318 && oap->op_type != OP_FOLDCLOSE
4319 && oap->op_type != OP_FOLDCLOSEREC
4320 && oap->op_type != OP_FOLDDEL
4321 && oap->op_type != OP_FOLDDELREC
4322#endif
4323 && oap->motion_force == NUL
4324 )
4325 {
4326 // Prepare for redoing. Only use the nchar field for "r",
4327 // otherwise it might be the second char of the operator.
4328 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
4329 || cap->nchar == 'N'))
4330 prep_redo(oap->regname, cap->count0,
4331 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
4332 oap->motion_force, cap->cmdchar, cap->nchar);
4333 else if (cap->cmdchar != ':')
4334 {
4335 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
4336
4337 // reverse what nv_replace() did
4338 if (nchar == REPLACE_CR_NCHAR)
4339 nchar = CAR;
4340 else if (nchar == REPLACE_NL_NCHAR)
4341 nchar = NL;
4342 prep_redo(oap->regname, 0L, NUL, 'v',
4343 get_op_char(oap->op_type),
4344 get_extra_op_char(oap->op_type),
4345 nchar);
4346 }
4347 if (!redo_VIsual_busy)
4348 {
4349 redo_VIsual_mode = resel_VIsual_mode;
4350 redo_VIsual_vcol = resel_VIsual_vcol;
4351 redo_VIsual_line_count = resel_VIsual_line_count;
4352 redo_VIsual_count = cap->count0;
4353 redo_VIsual_arg = cap->arg;
4354 }
4355 }
4356
4357 // oap->inclusive defaults to TRUE.
4358 // If oap->end is on a NUL (empty line) oap->inclusive becomes
4359 // FALSE. This makes "d}P" and "v}dP" work the same.
4360 if (oap->motion_force == NUL || oap->motion_type == MLINE)
4361 oap->inclusive = TRUE;
4362 if (VIsual_mode == 'V')
4363 oap->motion_type = MLINE;
4364 else
4365 {
4366 oap->motion_type = MCHAR;
4367 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
4368 && (include_line_break || !virtual_op))
4369 {
4370 oap->inclusive = FALSE;
4371 // Try to include the newline, unless it's an operator
4372 // that works on lines only.
4373 if (*p_sel != 'o'
4374 && !op_on_lines(oap->op_type)
4375 && oap->end.lnum < curbuf->b_ml.ml_line_count)
4376 {
4377 ++oap->end.lnum;
4378 oap->end.col = 0;
4379 oap->end.coladd = 0;
4380 ++oap->line_count;
4381 }
4382 }
4383 }
4384
4385 redo_VIsual_busy = FALSE;
4386
4387 // Switch Visual off now, so screen updating does
4388 // not show inverted text when the screen is redrawn.
4389 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
4390 // no screen redraw, so it is done here to remove the inverted
4391 // part.
4392 if (!gui_yank)
4393 {
4394 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004395 setmouse();
4396 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004397 may_clear_cmdline();
4398 if ((oap->op_type == OP_YANK
4399 || oap->op_type == OP_COLON
4400 || oap->op_type == OP_FUNCTION
4401 || oap->op_type == OP_FILTER)
4402 && oap->motion_force == NUL)
4403 {
4404#ifdef FEAT_LINEBREAK
4405 // make sure redrawing is correct
4406 curwin->w_p_lbr = lbr_saved;
4407#endif
4408 redraw_curbuf_later(INVERTED);
4409 }
4410 }
4411 }
4412
4413 // Include the trailing byte of a multi-byte char.
4414 if (has_mbyte && oap->inclusive)
4415 {
4416 int l;
4417
4418 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
4419 if (l > 1)
4420 oap->end.col += l - 1;
4421 }
4422 curwin->w_set_curswant = TRUE;
4423
4424 // oap->empty is set when start and end are the same. The inclusive
4425 // flag affects this too, unless yanking and the end is on a NUL.
4426 oap->empty = (oap->motion_type == MCHAR
4427 && (!oap->inclusive
4428 || (oap->op_type == OP_YANK
4429 && gchar_pos(&oap->end) == NUL))
4430 && EQUAL_POS(oap->start, oap->end)
4431 && !(virtual_op && oap->start.coladd != oap->end.coladd));
4432 // For delete, change and yank, it's an error to operate on an
4433 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
4434 empty_region_error = (oap->empty
4435 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
4436
4437 // Force a redraw when operating on an empty Visual region, when
4438 // 'modifiable is off or creating a fold.
4439 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
4440#ifdef FEAT_FOLDING
4441 || oap->op_type == OP_FOLD
4442#endif
4443 ))
4444 {
4445#ifdef FEAT_LINEBREAK
4446 curwin->w_p_lbr = lbr_saved;
4447#endif
4448 redraw_curbuf_later(INVERTED);
4449 }
4450
4451 // If the end of an operator is in column one while oap->motion_type
4452 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
4453 // character in the previous line. If op_start is on or before the
4454 // first non-blank in the line, the operator becomes linewise
4455 // (strange, but that's the way vi does it).
4456 if ( oap->motion_type == MCHAR
4457 && oap->inclusive == FALSE
4458 && !(cap->retval & CA_NO_ADJ_OP_END)
4459 && oap->end.col == 0
4460 && (!oap->is_VIsual || *p_sel == 'o')
4461 && !oap->block_mode
4462 && oap->line_count > 1)
4463 {
4464 oap->end_adjusted = TRUE; // remember that we did this
4465 --oap->line_count;
4466 --oap->end.lnum;
4467 if (inindent(0))
4468 oap->motion_type = MLINE;
4469 else
4470 {
4471 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
4472 if (oap->end.col)
4473 {
4474 --oap->end.col;
4475 oap->inclusive = TRUE;
4476 }
4477 }
4478 }
4479 else
4480 oap->end_adjusted = FALSE;
4481
4482 switch (oap->op_type)
4483 {
4484 case OP_LSHIFT:
4485 case OP_RSHIFT:
4486 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
4487 auto_format(FALSE, TRUE);
4488 break;
4489
4490 case OP_JOIN_NS:
4491 case OP_JOIN:
4492 if (oap->line_count < 2)
4493 oap->line_count = 2;
4494 if (curwin->w_cursor.lnum + oap->line_count - 1 >
4495 curbuf->b_ml.ml_line_count)
4496 beep_flush();
4497 else
4498 {
4499 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
4500 TRUE, TRUE, TRUE);
4501 auto_format(FALSE, TRUE);
4502 }
4503 break;
4504
4505 case OP_DELETE:
4506 VIsual_reselect = FALSE; // don't reselect now
4507 if (empty_region_error)
4508 {
4509 vim_beep(BO_OPER);
4510 CancelRedo();
4511 }
4512 else
4513 {
4514 (void)op_delete(oap);
4515 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
4516 u_save_cursor(); // cursor line wasn't saved yet
4517 auto_format(FALSE, TRUE);
4518 }
4519 break;
4520
4521 case OP_YANK:
4522 if (empty_region_error)
4523 {
4524 if (!gui_yank)
4525 {
4526 vim_beep(BO_OPER);
4527 CancelRedo();
4528 }
4529 }
4530 else
4531 {
4532#ifdef FEAT_LINEBREAK
4533 curwin->w_p_lbr = lbr_saved;
4534#endif
4535 (void)op_yank(oap, FALSE, !gui_yank);
4536 }
4537 check_cursor_col();
4538 break;
4539
4540 case OP_CHANGE:
4541 VIsual_reselect = FALSE; // don't reselect now
4542 if (empty_region_error)
4543 {
4544 vim_beep(BO_OPER);
4545 CancelRedo();
4546 }
4547 else
4548 {
4549 // This is a new edit command, not a restart. Need to
4550 // remember it to make 'insertmode' work with mappings for
4551 // Visual mode. But do this only once and not when typed and
4552 // 'insertmode' isn't set.
4553 if (p_im || !KeyTyped)
4554 restart_edit_save = restart_edit;
4555 else
4556 restart_edit_save = 0;
4557 restart_edit = 0;
4558#ifdef FEAT_LINEBREAK
4559 // Restore linebreak, so that when the user edits it looks as
4560 // before.
4561 if (curwin->w_p_lbr != lbr_saved)
4562 {
4563 curwin->w_p_lbr = lbr_saved;
4564 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4565 }
4566#endif
4567 // Reset finish_op now, don't want it set inside edit().
4568 finish_op = FALSE;
4569 if (op_change(oap)) // will call edit()
4570 cap->retval |= CA_COMMAND_BUSY;
4571 if (restart_edit == 0)
4572 restart_edit = restart_edit_save;
4573 }
4574 break;
4575
4576 case OP_FILTER:
4577 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4578 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4579 else
4580 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4581 // FALLTHROUGH
4582
4583 case OP_INDENT:
4584 case OP_COLON:
4585
4586#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4587 // If 'equalprg' is empty, do the indenting internally.
4588 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4589 {
4590# ifdef FEAT_LISP
4591 if (curbuf->b_p_lisp)
4592 {
4593 op_reindent(oap, get_lisp_indent);
4594 break;
4595 }
4596# endif
4597# ifdef FEAT_CINDENT
4598 op_reindent(oap,
4599# ifdef FEAT_EVAL
4600 *curbuf->b_p_inde != NUL ? get_expr_indent :
4601# endif
4602 get_c_indent);
4603 break;
4604# endif
4605 }
4606#endif
4607
4608 op_colon(oap);
4609 break;
4610
4611 case OP_TILDE:
4612 case OP_UPPER:
4613 case OP_LOWER:
4614 case OP_ROT13:
4615 if (empty_region_error)
4616 {
4617 vim_beep(BO_OPER);
4618 CancelRedo();
4619 }
4620 else
4621 op_tilde(oap);
4622 check_cursor_col();
4623 break;
4624
4625 case OP_FORMAT:
4626#if defined(FEAT_EVAL)
4627 if (*curbuf->b_p_fex != NUL)
4628 op_formatexpr(oap); // use expression
4629 else
4630#endif
4631 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4632 op_colon(oap); // use external command
4633 else
4634 op_format(oap, FALSE); // use internal function
4635 break;
4636
4637 case OP_FORMAT2:
4638 op_format(oap, TRUE); // use internal function
4639 break;
4640
4641 case OP_FUNCTION:
4642#ifdef FEAT_LINEBREAK
4643 // Restore linebreak, so that when the user edits it looks as
4644 // before.
4645 curwin->w_p_lbr = lbr_saved;
4646#endif
4647 op_function(oap); // call 'operatorfunc'
4648 break;
4649
4650 case OP_INSERT:
4651 case OP_APPEND:
4652 VIsual_reselect = FALSE; // don't reselect now
4653 if (empty_region_error)
4654 {
4655 vim_beep(BO_OPER);
4656 CancelRedo();
4657 }
4658 else
4659 {
4660 // This is a new edit command, not a restart. Need to
4661 // remember it to make 'insertmode' work with mappings for
4662 // Visual mode. But do this only once.
4663 restart_edit_save = restart_edit;
4664 restart_edit = 0;
4665#ifdef FEAT_LINEBREAK
4666 // Restore linebreak, so that when the user edits it looks as
4667 // before.
4668 if (curwin->w_p_lbr != lbr_saved)
4669 {
4670 curwin->w_p_lbr = lbr_saved;
4671 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4672 }
4673#endif
4674 op_insert(oap, cap->count1);
4675#ifdef FEAT_LINEBREAK
4676 // Reset linebreak, so that formatting works correctly.
4677 curwin->w_p_lbr = FALSE;
4678#endif
4679
4680 // TODO: when inserting in several lines, should format all
4681 // the lines.
4682 auto_format(FALSE, TRUE);
4683
4684 if (restart_edit == 0)
4685 restart_edit = restart_edit_save;
4686 else
4687 cap->retval |= CA_COMMAND_BUSY;
4688 }
4689 break;
4690
4691 case OP_REPLACE:
4692 VIsual_reselect = FALSE; // don't reselect now
4693 if (empty_region_error)
4694 {
4695 vim_beep(BO_OPER);
4696 CancelRedo();
4697 }
4698 else
4699 {
4700#ifdef FEAT_LINEBREAK
4701 // Restore linebreak, so that when the user edits it looks as
4702 // before.
4703 if (curwin->w_p_lbr != lbr_saved)
4704 {
4705 curwin->w_p_lbr = lbr_saved;
4706 get_op_vcol(oap, redo_VIsual_mode, FALSE);
4707 }
4708#endif
4709 op_replace(oap, cap->nchar);
4710 }
4711 break;
4712
4713#ifdef FEAT_FOLDING
4714 case OP_FOLD:
4715 VIsual_reselect = FALSE; // don't reselect now
4716 foldCreate(oap->start.lnum, oap->end.lnum);
4717 break;
4718
4719 case OP_FOLDOPEN:
4720 case OP_FOLDOPENREC:
4721 case OP_FOLDCLOSE:
4722 case OP_FOLDCLOSEREC:
4723 VIsual_reselect = FALSE; // don't reselect now
4724 opFoldRange(oap->start.lnum, oap->end.lnum,
4725 oap->op_type == OP_FOLDOPEN
4726 || oap->op_type == OP_FOLDOPENREC,
4727 oap->op_type == OP_FOLDOPENREC
4728 || oap->op_type == OP_FOLDCLOSEREC,
4729 oap->is_VIsual);
4730 break;
4731
4732 case OP_FOLDDEL:
4733 case OP_FOLDDELREC:
4734 VIsual_reselect = FALSE; // don't reselect now
4735 deleteFold(oap->start.lnum, oap->end.lnum,
4736 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4737 break;
4738#endif
4739 case OP_NR_ADD:
4740 case OP_NR_SUB:
4741 if (empty_region_error)
4742 {
4743 vim_beep(BO_OPER);
4744 CancelRedo();
4745 }
4746 else
4747 {
4748 VIsual_active = TRUE;
4749#ifdef FEAT_LINEBREAK
4750 curwin->w_p_lbr = lbr_saved;
4751#endif
4752 op_addsub(oap, cap->count1, redo_VIsual_arg);
4753 VIsual_active = FALSE;
4754 }
4755 check_cursor_col();
4756 break;
4757 default:
4758 clearopbeep(oap);
4759 }
4760 virtual_op = MAYBE;
4761 if (!gui_yank)
4762 {
4763 // if 'sol' not set, go back to old column for some commands
4764 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4765 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4766 || oap->op_type == OP_DELETE))
4767 {
4768#ifdef FEAT_LINEBREAK
4769 curwin->w_p_lbr = FALSE;
4770#endif
4771 coladvance(curwin->w_curswant = old_col);
4772 }
4773 }
4774 else
4775 {
4776 curwin->w_cursor = old_cursor;
4777 }
4778 oap->block_mode = FALSE;
4779 clearop(oap);
4780 motion_force = NUL;
4781 }
4782#ifdef FEAT_LINEBREAK
4783 curwin->w_p_lbr = lbr_saved;
4784#endif
4785}