blob: 6e327a2b27360e713774060b98594c2e815bd865 [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,
Bram Moolenaar032a2d02020-12-22 17:59:35 +010012 * op_change, op_yank, do_join
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
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 Moolenaar071d4272004-06-13 20:20:40 +000020
Bram Moolenaarf2732452018-06-03 14:47:35 +020021// Flags for third item in "opchars".
22#define OPF_LINES 1 // operator always works on lines
23#define OPF_CHANGE 2 // operator changes text
24
Bram Moolenaar071d4272004-06-13 20:20:40 +000025/*
26 * The names of operators.
27 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020028 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 */
30static char opchars[][3] =
31{
Bram Moolenaarf2732452018-06-03 14:47:35 +020032 {NUL, NUL, 0}, // OP_NOP
33 {'d', NUL, OPF_CHANGE}, // OP_DELETE
34 {'y', NUL, 0}, // OP_YANK
35 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
36 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
37 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
38 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
39 {'g', '~', OPF_CHANGE}, // OP_TILDE
40 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
41 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
42 {':', NUL, OPF_LINES}, // OP_COLON
43 {'g', 'U', OPF_CHANGE}, // OP_UPPER
44 {'g', 'u', OPF_CHANGE}, // OP_LOWER
45 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
46 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
47 {'g', '?', OPF_CHANGE}, // OP_ROT13
48 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
49 {'I', NUL, OPF_CHANGE}, // OP_INSERT
50 {'A', NUL, OPF_CHANGE}, // OP_APPEND
51 {'z', 'f', OPF_LINES}, // OP_FOLD
52 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
53 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
54 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
55 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
56 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
57 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
58 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
59 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
60 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
61 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +000062};
63
64/*
65 * Translate a command name into an operator type.
66 * Must only be called with a valid operator name!
67 */
68 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010069get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070{
71 int i;
72
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010073 if (char1 == 'r') // ignore second character
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 return OP_REPLACE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010075 if (char1 == '~') // when tilde is an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 return OP_TILDE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010077 if (char1 == 'g' && char2 == Ctrl_A) // add
Bram Moolenaard79e5502016-01-10 22:13:02 +010078 return OP_NR_ADD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +010079 if (char1 == 'g' && char2 == Ctrl_X) // subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +010080 return OP_NR_SUB;
Christian Brabandt544a38e2021-06-10 19:39:11 +020081 if (char1 == 'z' && char2 == 'y') // OP_YANK
82 return OP_YANK;
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;
K.Takataeeec2542021-06-02 13:28:16 +020087 if (i == (int)ARRAY_LENGTH(opchars) - 1)
Bram Moolenaar2efb3232017-12-19 12:27:23 +010088 {
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();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100155 if (first_char == NUL) // empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 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
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100160 // 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'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#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 Moolenaar6e0ce172019-12-05 20:12:41 +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;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100178 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 }
180 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100181 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100183#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100184 // The cursor line is not in a closed fold
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100185 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 Moolenaare4fc7462020-03-15 19:17:50 +0100206 msg_attr_keep((char *)IObuff, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 }
208
Bram Moolenaare1004402020-10-24 20:49:43 +0200209 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100210 {
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 Moolenaar6e0ce172019-12-05 20:12:41 +0100294 p_ri = 0; // don't want revins in indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100297 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100302 // 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100305 return; // multiplication overflow
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200306
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 */
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100317 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100336 // TODO: is passing bd.textstart for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200337 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 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100342 // 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100352 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 if (i)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100354 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100358 // if we're splitting a TAB, allow for it
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100368 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100371 else // left
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100373 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
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000382 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 Moolenaar6e0ce172019-12-05 20:12:41 +0100395 // 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.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000398 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100399 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100401 // The character's column is in "bd.start_vcol".
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000402 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;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100411 // We will shift by "total" or "block_space_width", whichever is less.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100415 // The column to which we will shift the text.
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000416 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Now let's find out how much of the beginning of the line we can
419 // reuse without modification.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // If "bd.startspaces" is set, "bd.textstart" points to the character
424 // preceding the block. We have to subtract its width to obtain its
425 // column number.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200430 char_u *line = verbatim_copy_end;
431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100432 // TODO: is passing verbatim_copy_end for start of the line OK?
Bram Moolenaar597a4222014-06-25 14:39:50 +0200433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100438 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000439 }
440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100441 // If "destination_col" is different from the width of the initial
442 // part of the line that will be copied, it means we encountered a tab
443 // character, which we will have to partly replace with spaces.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000444 fill = destination_col - verbatim_copy_width;
445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100446 // The replacement line will consist of:
447 // - the beginning of the original line up to "verbatim_copy_end",
448 // - "fill" number of spaces,
449 // - the rest of the line, pointed to by non_white.
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
452 + (unsigned)STRLEN(non_white) + 1;
453
Bram Moolenaar964b3742019-05-24 18:54:09 +0200454 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (newp == NULL)
456 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100461 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466#ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468#endif
469}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100476block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaardac13472019-09-16 21:06:21 +0200482 int ts_val;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100483 int count = 0; // extra spaces to replace a cut TAB
484 int spaces = 0; // non-zero if cutting a TAB
485 colnr_T offset; // pointer along new line
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200486 colnr_T startcol; // column where insert starts
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 unsigned s_len; // STRLEN(s)
488 char_u *newp, *oldp; // new, old lines
489 linenr_T lnum; // loop var
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int oldstate = State;
491
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100492 State = INSERT; // don't want REPLACE for State
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 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)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100499 continue; // OP_INSERT, line ends before block start
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
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 Moolenaar6e0ce172019-12-05 20:12:41 +0100508 count = ts_val - 1; // we're cutting a TAB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 offset = bdp->textcol;
510 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100511 else // append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 ts_val = bdp->end_char_vcols;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100514 if (!bdp->is_short) // spaces = padding after block
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
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 Moolenaar6e0ce172019-12-05 20:12:41 +0100518 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 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100521 else // spaces = padding to block edge
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 // if $ used, just append to EOL (ie spaces==0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 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)
Bram Moolenaar57df9e82022-01-20 12:10:48 +0000532 // avoid copying part of a multi-byte character
533 offset -= (*mb_head_off)(oldp, oldp + offset);
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100534
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200535 if (spaces < 0) // can happen when the cursor was moved
536 spaces = 0;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200537
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000538 // Make sure the allocated size matches what is actually copied below.
539 newp = alloc(STRLEN(oldp) + spaces + s_len
540 + (spaces > 0 && !bdp->is_short ? ts_val - spaces : 0)
541 + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (newp == NULL)
543 continue;
544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100545 // copy up to shifted part
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000546 mch_memmove(newp, oldp, (size_t)offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 oldp += offset;
548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100549 // insert pre-padding
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200550 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200551 startcol = offset + spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100553 // copy the new text
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200554 mch_memmove(newp + startcol, s, (size_t)s_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 offset += s_len;
556
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000557 if (spaces > 0 && !bdp->is_short)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
Bram Moolenaar9f8c3042022-01-17 17:30:21 +0000559 if (*oldp == TAB)
560 {
561 // insert post-padding
562 vim_memset(newp + offset + spaces, ' ',
563 (size_t)(ts_val - spaces));
564 // we're splitting a TAB, don't copy it
565 oldp++;
566 // We allowed for that TAB, remember this now
567 count++;
568 }
569 else
570 // Not a TAB, no extra spaces
571 count = spaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
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
Bram Moolenaar8b51b7f2020-09-15 21:34:18 +0200580 if (b_insert)
581 // correct any text properties
582 inserted_bytes(lnum, startcol, s_len);
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (lnum == oap->end.lnum)
585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100586 // Set "']" mark to the end of the block instead of the end of
587 // the insert in the first line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 curbuf->b_op_end.lnum = oap->end.lnum;
589 curbuf->b_op_end.col = offset;
590 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100591 } // for all lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
594
595 State = oldstate;
596}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598/*
Bram Moolenaard04b7502010-07-08 22:27:55 +0200599 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 *
Bram Moolenaard04b7502010-07-08 22:27:55 +0200601 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 */
603 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100604op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 int n;
607 linenr_T lnum;
608 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 char_u *newp, *oldp;
610 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
612 int did_yank = FALSE;
613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100614 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 return OK;
616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100617 // Nothing to delete, return here. Do prepare undo, for op_change().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 if (oap->empty)
619 return u_save_cursor();
620
621 if (!curbuf->b_p_ma)
622 {
Bram Moolenaar108010a2021-06-27 22:03:33 +0200623 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return FAIL;
625 }
626
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000627 if (VIsual_select && oap->is_VIsual)
628 // use register given with CTRL_R, defaults to zero
629 oap->regname = VIsual_select_reg;
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_CLIPBOARD
632 adjust_clip_reg(&oap->regname);
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (has_mbyte)
636 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaard04b7502010-07-08 22:27:55 +0200638 /*
639 * Imitate the strange Vi behaviour: If the delete spans more than one
640 * line and motion_type == MCHAR and the result is a blank line, make the
641 * delete linewise. Don't do this for the change command or Visual mode.
642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +0000645 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +0100647 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 && oap->op_type == OP_DELETE)
649 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200650 ptr = ml_get(oap->end.lnum) + oap->end.col;
651 if (*ptr != NUL)
652 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 ptr = skipwhite(ptr);
654 if (*ptr == NUL && inindent(0))
655 oap->motion_type = MLINE;
656 }
657
Bram Moolenaard04b7502010-07-08 22:27:55 +0200658 /*
659 * Check for trying to delete (e.g. "D") in an empty line.
660 * Note: For the change operator it is ok.
661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if ( oap->motion_type == MCHAR
663 && oap->line_count == 1
664 && oap->op_type == OP_DELETE
665 && *ml_get(oap->start.lnum) == NUL)
666 {
667 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000668 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 * 'cpoptions' (Vi compatible).
670 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000671 if (virtual_op)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
673 // marks as if it happened.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000674 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
676 beep_flush();
677 return OK;
678 }
679
Bram Moolenaard04b7502010-07-08 22:27:55 +0200680 /*
681 * Do a yank of whatever we're about to delete.
682 * If a yank register was specified, put the deleted text into that
683 * register. For the black hole register '_' don't yank anything.
684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (oap->regname != '_')
686 {
687 if (oap->regname != 0)
688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // check for read-only register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (!valid_yank_reg(oap->regname, TRUE))
691 {
692 beep_flush();
693 return OK;
694 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100695 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
696 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 did_yank = TRUE;
698 }
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200699 else
700 reset_y_append(); // not appending to unnamed register
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 /*
703 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100704 * delete contains a line break, or when using a specific operator (Vi
705 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +0200706 * Use the register name from before adjust_clip_reg() may have
707 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +0100709 if (oap->motion_type == MLINE || oap->line_count > 1
710 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaara1891842017-02-04 21:34:31 +0100712 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 if (op_yank(oap, TRUE, FALSE) == OK)
714 did_yank = TRUE;
715 }
716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 // Yank into small delete register when no named register specified
718 // and the delete is within one line.
Bram Moolenaar84298db2012-04-20 13:46:08 +0200719 if ((
720#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200721 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
722 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +0200723#endif
724 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 && oap->line_count == 1)
726 {
727 oap->regname = '-';
728 get_yank_register(oap->regname, TRUE);
729 if (op_yank(oap, TRUE, FALSE) == OK)
730 did_yank = TRUE;
731 oap->regname = 0;
732 }
733
734 /*
735 * If there's too much stuff to fit in the yank register, then get a
736 * confirmation before doing the delete. This is crude, but simple.
737 * And it avoids doing a delete of something we can't put back if we
738 * want.
739 */
740 if (!did_yank)
741 {
742 int msg_silent_save = msg_silent;
743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100744 msg_silent = 0; // must display the prompt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
746 msg_silent = msg_silent_save;
747 if (n != 'y')
748 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000749 emsg(_(e_command_aborted));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 return FAIL;
751 }
752 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100753
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100754#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100755 if (did_yank && has_textyankpost())
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200756 yank_do_autocmd(oap, get_y_current());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759
Bram Moolenaard04b7502010-07-08 22:27:55 +0200760 /*
761 * block mode delete
762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (oap->block_mode)
764 {
765 if (u_save((linenr_T)(oap->start.lnum - 1),
766 (linenr_T)(oap->end.lnum + 1)) == FAIL)
767 return FAIL;
768
769 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
770 {
771 block_prep(oap, &bd, lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100772 if (bd.textlen == 0) // nothing to delete
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 continue;
774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100775 // Adjust cursor position for tab replaced by spaces and 'lbr'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (lnum == curwin->w_cursor.lnum)
777 {
778 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781
Bram Moolenaar8055d172019-05-17 22:57:26 +0200782 // "n" == number of chars deleted
783 // If we delete a TAB, it may be replaced by several characters.
784 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 n = bd.textlen - bd.startspaces - bd.endspaces;
786 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +0200787 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (newp == NULL)
789 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100790 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 // insert spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200793 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 (size_t)(bd.startspaces + bd.endspaces));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100795 // copy the part after the deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000797 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100798 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200800
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100801#ifdef FEAT_PROP_POPUP
Bram Moolenaar8055d172019-05-17 22:57:26 +0200802 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +0200803 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +0200804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806
807 check_cursor_col();
808 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
809 oap->end.lnum + 1, 0L);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100810 oap->line_count = 0; // no lines deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100812 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 if (oap->op_type == OP_CHANGE)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Delete the lines except the first one. Temporarily move the
817 // cursor to the next line. Save the current line number, if the
818 // last line is deleted it may be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (oap->line_count > 1)
820 {
821 lnum = curwin->w_cursor.lnum;
822 ++curwin->w_cursor.lnum;
823 del_lines((long)(oap->line_count - 1), TRUE);
824 curwin->w_cursor.lnum = lnum;
825 }
826 if (u_save_cursor() == FAIL)
827 return FAIL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 if (curbuf->b_p_ai) // don't delete indent
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 beginline(BL_WHITE); // cursor on first non-white
831 did_ai = TRUE; // delete the indent when ESC hit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 ai_col = curwin->w_cursor.col;
833 }
834 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 beginline(0); // cursor in column 0
836 truncate_line(FALSE); // delete the rest of the line
837 // leave cursor past last char in line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (oap->line_count > 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100839 u_clearline(); // "U" command not possible after "2cc"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
841 else
842 {
843 del_lines(oap->line_count, TRUE);
844 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 u_clearline(); // "U" command not possible after "dd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 }
848 else
849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (virtual_op)
851 {
852 int endcol = 0;
853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // For virtualedit: break the tabs that are partly included.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (gchar_pos(&oap->start) == '\t')
856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 if (u_save_cursor() == FAIL) // save first line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 return FAIL;
859 if (oap->line_count == 1)
860 endcol = getviscol2(oap->end.col, oap->end.coladd);
861 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
862 oap->start = curwin->w_cursor;
863 if (oap->line_count == 1)
864 {
865 coladvance(endcol);
866 oap->end.col = curwin->w_cursor.col;
867 oap->end.coladd = curwin->w_cursor.coladd;
868 curwin->w_cursor = oap->start;
869 }
870 }
871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100872 // Break a tab only when it's included in the area.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (gchar_pos(&oap->end) == '\t'
874 && (int)oap->end.coladd < oap->inclusive)
875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100876 // save last line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (u_save((linenr_T)(oap->end.lnum - 1),
878 (linenr_T)(oap->end.lnum + 1)) == FAIL)
879 return FAIL;
880 curwin->w_cursor = oap->end;
881 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
882 oap->end = curwin->w_cursor;
883 curwin->w_cursor = oap->start;
884 }
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100885 if (has_mbyte)
886 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100889 if (oap->line_count == 1) // delete characters within one line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100891 if (u_save_cursor() == FAIL) // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 return FAIL;
893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100894 // if 'cpoptions' contains '$', display '$' at end of change
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100895 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 && oap->op_type == OP_CHANGE
897 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100898 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 display_dollar(oap->end.col - !oap->inclusive);
900
901 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 if (virtual_op)
904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100905 // fix up things for virtualedit-delete:
906 // break the tabs which are going to get in our way
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 char_u *curline = ml_get_curline();
908 int len = (int)STRLEN(curline);
909
910 if (oap->end.coladd != 0
911 && (int)oap->end.col >= len - 1
912 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
913 n++;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100914 // Delete at least one char (e.g, when on a control char).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (n == 0 && oap->start.coladd != oap->end.coladd)
916 n = 1;
917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When deleted a char in the line, reset coladd.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (gchar_cursor() != NUL)
920 curwin->w_cursor.coladd = 0;
921 }
Bram Moolenaard009e862015-06-09 20:20:03 +0200922 (void)del_bytes((long)n, !virtual_op,
923 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 else // delete characters between lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
927 pos_T curpos;
928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100929 // save deleted and changed lines for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
931 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
932 return FAIL;
933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100934 truncate_line(TRUE); // delete from cursor to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100936 curpos = curwin->w_cursor; // remember curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 ++curwin->w_cursor.lnum;
938 del_lines((long)(oap->line_count - 2), FALSE);
939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100940 // delete from start of line until op_end
Bram Moolenaar44286ca2011-07-15 17:51:34 +0200941 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +0200942 curwin->w_cursor.col = 0;
943 (void)del_bytes((long)n, !virtual_op,
944 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100945 curwin->w_cursor = curpos; // restore curwin->w_cursor
Bram Moolenaard009e862015-06-09 20:20:03 +0200946 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +0200948 if (oap->op_type == OP_DELETE)
949 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951
952 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
953
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000954setmarks:
Bram Moolenaare1004402020-10-24 20:49:43 +0200955 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +0100957 if (oap->block_mode)
958 {
959 curbuf->b_op_end.lnum = oap->end.lnum;
960 curbuf->b_op_end.col = oap->start.col;
961 }
962 else
963 curbuf->b_op_end = oap->start;
964 curbuf->b_op_start = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967 return OK;
968}
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970/*
971 * Adjust end of operating area for ending on a multi-byte character.
972 * Used for deletion.
973 */
974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100975mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 char_u *p;
978
979 if (oap->inclusive)
980 {
981 p = ml_get(oap->end.lnum);
982 oap->end.col += mb_tail_off(p, p + oap->end.col);
983 }
984}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
Bram Moolenaar630afe82018-06-28 19:26:28 +0200986/*
987 * Replace the character under the cursor with "c".
988 * This takes care of multi-byte characters.
989 */
990 static void
991replace_character(int c)
992{
993 int n = State;
994
995 State = REPLACE;
996 ins_char(c);
997 State = n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100998 // Backup to the replaced character.
Bram Moolenaar630afe82018-06-28 19:26:28 +0200999 dec_cursor();
1000}
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Replace a whole area with one character.
1004 */
1005 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001006op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007{
1008 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 char_u *newp, *oldp;
1011 size_t oldlen;
1012 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01001013 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001014 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
1016 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001017 return OK; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001019 if (c == REPLACE_CR_NCHAR)
1020 {
1021 had_ctrl_v_cr = TRUE;
1022 c = CAR;
1023 }
1024 else if (c == REPLACE_NL_NCHAR)
1025 {
1026 had_ctrl_v_cr = TRUE;
1027 c = NL;
1028 }
Bram Moolenaard9820532013-11-04 01:41:17 +01001029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (has_mbyte)
1031 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 if (u_save((linenr_T)(oap->start.lnum - 1),
1034 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1035 return FAIL;
1036
1037 /*
1038 * block mode replace
1039 */
1040 if (oap->block_mode)
1041 {
1042 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1043 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1044 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001045 curwin->w_cursor.col = 0; // make sure cursor position is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1047 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001048 continue; // nothing to replace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // n == number of extra chars required
1051 // If we split a TAB, it may be replaced by several characters.
1052 // Thus the number of characters may increase!
1053 // If the range starts in virtual space, count the initial
1054 // coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1056 {
1057 pos_T vpos;
1058
Bram Moolenaara1381de2009-11-03 15:44:21 +00001059 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 getvpos(&vpos, oap->start_vcol);
1061 bd.startspaces += vpos.coladd;
1062 n = bd.startspaces;
1063 }
1064 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 // allow for pre spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 // allow for post spp
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001072 // Figure out how many characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 numc = oap->end_vcol - oap->start_vcol + 1;
1074 if (bd.is_short && (!virtual_op || bd.is_MAX))
1075 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // A double-wide character can be replaced only up to half the
1078 // times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if ((*mb_char2cells)(c) > 1)
1080 {
1081 if ((numc & 1) && !bd.is_short)
1082 {
1083 ++bd.endspaces;
1084 ++n;
1085 }
1086 numc = numc / 2;
1087 }
1088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001089 // Compute bytes needed, move character count to num_chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 num_chars = numc;
1091 numc *= (*mb_char2len)(c);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001092 // oldlen includes textlen, so don't double count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 n += numc - bd.textlen;
1094
1095 oldp = ml_get_curline();
1096 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001097 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (newp == NULL)
1099 continue;
1100 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 // copy up to deleted part
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 mch_memmove(newp, oldp, (size_t)bd.textcol);
1103 oldp += bd.textcol + bd.textlen;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001104 // insert pre-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001105 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001106 // insert replacement chars CHECK FOR ALLOCATED SPACE
1107 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1108 // literally.
Bram Moolenaard9820532013-11-04 01:41:17 +01001109 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaard9820532013-11-04 01:41:17 +01001111 if (has_mbyte)
1112 {
1113 n = (int)STRLEN(newp);
1114 while (--num_chars >= 0)
1115 n += (*mb_char2bytes)(c, newp + n);
1116 }
1117 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001118 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01001119 if (!bd.is_short)
1120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // insert post-spaces
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001122 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001123 // copy the part after the changed part
Bram Moolenaard9820532013-11-04 01:41:17 +01001124 STRMOVE(newp + STRLEN(newp), oldp);
1125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001129 // Replacing with \r or \n means splitting the line.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001130 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01001131 if (after_p != NULL)
1132 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001134 // replace the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01001136 if (after_p != NULL)
1137 {
1138 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1139 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1140 oap->end.lnum++;
1141 vim_free(after_p);
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144 }
1145 else
1146 {
1147 /*
1148 * MCHAR and MLINE motion replace.
1149 */
1150 if (oap->motion_type == MLINE)
1151 {
1152 oap->start.col = 0;
1153 curwin->w_cursor.col = 0;
1154 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1155 if (oap->end.col)
1156 --oap->end.col;
1157 }
1158 else if (!oap->inclusive)
1159 dec(&(oap->end));
1160
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001161 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
1163 n = gchar_cursor();
1164 if (n != NUL)
1165 {
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001166 int new_byte_len = (*mb_char2len)(c);
1167 int old_byte_len = mb_ptr2len(ml_get_cursor());
1168
1169 if (new_byte_len > 1 || old_byte_len > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001171 // This is slow, but it handles replacing a single-byte
1172 // with a multi-byte and the other way around.
Bram Moolenaardb813952013-03-07 18:50:57 +01001173 if (curwin->w_cursor.lnum == oap->end.lnum)
Bram Moolenaar8ee60282021-12-15 21:08:50 +00001174 oap->end.col += new_byte_len - old_byte_len;
Bram Moolenaar630afe82018-06-28 19:26:28 +02001175 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (n == TAB)
1180 {
1181 int end_vcol = 0;
1182
1183 if (curwin->w_cursor.lnum == oap->end.lnum)
1184 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001185 // oap->end has to be recalculated when
1186 // the tab breaks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 end_vcol = getviscol2(oap->end.col,
1188 oap->end.coladd);
1189 }
1190 coladvance_force(getviscol());
1191 if (curwin->w_cursor.lnum == oap->end.lnum)
1192 getvpos(&oap->end, end_vcol);
1193 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02001194 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1198 {
1199 int virtcols = oap->end.coladd;
1200
1201 if (curwin->w_cursor.lnum == oap->start.lnum
1202 && oap->start.col == oap->end.col && oap->start.coladd)
1203 virtcols -= oap->start.coladd;
1204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001205 // oap->end has been trimmed so it's effectively inclusive;
1206 // as a result an extra +1 must be counted so we don't
1207 // trample the NUL byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1209 curwin->w_cursor.col -= (virtcols + 1);
1210 for (; virtcols >= 0; virtcols--)
1211 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02001212 if ((*mb_char2len)(c) > 1)
1213 replace_character(c);
1214 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001215 PBYTE(curwin->w_cursor, c);
1216 if (inc(&curwin->w_cursor) == -1)
1217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 // Advance to next character, stop at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (inc_cursor() == -1)
1223 break;
1224 }
1225 }
1226
1227 curwin->w_cursor = oap->start;
1228 check_cursor();
1229 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1230
Bram Moolenaare1004402020-10-24 20:49:43 +02001231 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001233 // Set "'[" and "']" marks.
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001234 curbuf->b_op_start = oap->start;
1235 curbuf->b_op_end = oap->end;
1236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238 return OK;
1239}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001241static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
1244 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1245 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001246 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001247op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001251 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253 if (u_save((linenr_T)(oap->start.lnum - 1),
1254 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1255 return;
1256
1257 pos = oap->start;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001258 if (oap->block_mode) // Visual block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
1260 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1261 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001262 int one_change;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 block_prep(oap, &bd, pos.lnum, FALSE);
1265 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00001266 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1267 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001268
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001269#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001270 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1273
Bram Moolenaar009b2592004-10-24 19:18:58 +00001274 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1275 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001277 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 if (did_change)
1282 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1283 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001284 else // not block mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
1286 if (oap->motion_type == MLINE)
1287 {
1288 oap->start.col = 0;
1289 pos.col = 0;
1290 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1291 if (oap->end.col)
1292 --oap->end.col;
1293 }
1294 else if (!oap->inclusive)
1295 dec(&(oap->end));
1296
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001297 if (pos.lnum == oap->end.lnum)
1298 did_change = swapchars(oap->op_type, &pos,
1299 oap->end.col - pos.col + 1);
1300 else
1301 for (;;)
1302 {
1303 did_change |= swapchars(oap->op_type, &pos,
1304 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1305 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001306 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00001307 break;
1308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (did_change)
1310 {
1311 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1312 0L);
1313#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001314 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
1316 char_u *ptr;
1317 int count;
1318
1319 pos = oap->start;
1320 while (pos.lnum < oap->end.lnum)
1321 {
1322 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001323 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001324 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001326 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 pos.col = 0;
1328 pos.lnum++;
1329 }
1330 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1331 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001332 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001334 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336#endif
1337 }
1338 }
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (!did_change && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001341 // No change: need to remove the Visual selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaare1004402020-10-24 20:49:43 +02001344 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001345 {
1346 // Set '[ and '] marks.
1347 curbuf->b_op_start = oap->start;
1348 curbuf->b_op_end = oap->end;
1349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001353 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
1356/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001357 * Invoke swapchar() on "length" bytes at position "pos".
1358 * "pos" is advanced to just after the changed characters.
1359 * "length" is rounded up to include the whole last multi-byte character.
1360 * Also works correctly when the number of bytes changes.
1361 * Returns TRUE if some character was changed.
1362 */
1363 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001365{
1366 int todo;
1367 int did_change = 0;
1368
1369 for (todo = length; todo > 0; --todo)
1370 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001371 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001372 {
1373 int len = (*mb_ptr2len)(ml_get_pos(pos));
1374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001375 // we're counting bytes, not characters
Bram Moolenaarf17968b2013-08-09 19:48:40 +02001376 if (len > 0)
1377 todo -= len - 1;
1378 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001379 did_change |= swapchar(op_type, pos);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001380 if (inc(pos) == -1) // at end of file
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001381 break;
1382 }
1383 return did_change;
1384}
1385
1386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 * If op_type == OP_UPPER: make uppercase,
1388 * if op_type == OP_LOWER: make lowercase,
1389 * if op_type == OP_ROT13: do rot13 encoding,
1390 * else swap case of character at 'pos'
1391 * returns TRUE when something actually changed.
1392 */
1393 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001394swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 int c;
1397 int nc;
1398
1399 c = gchar_pos(pos);
1400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 // Only do rot13 encoding for ASCII characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (c >= 0x80 && op_type == OP_ROT13)
1403 return FALSE;
1404
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00001405 if (op_type == OP_UPPER && c == 0xdf
1406 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001407 {
1408 pos_T sp = curwin->w_cursor;
1409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001410 // Special handling of German sharp s: change to "SS".
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00001411 curwin->w_cursor = *pos;
1412 del_char(FALSE);
1413 ins_char('S');
1414 ins_char('S');
1415 curwin->w_cursor = sp;
1416 inc(pos);
1417 }
1418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001419 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 nc = c;
1422 if (MB_ISLOWER(c))
1423 {
1424 if (op_type == OP_ROT13)
1425 nc = ROT13(c, 'a');
1426 else if (op_type != OP_LOWER)
1427 nc = MB_TOUPPER(c);
1428 }
1429 else if (MB_ISUPPER(c))
1430 {
1431 if (op_type == OP_ROT13)
1432 nc = ROT13(c, 'A');
1433 else if (op_type != OP_UPPER)
1434 nc = MB_TOLOWER(c);
1435 }
1436 if (nc != c)
1437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1439 {
1440 pos_T sp = curwin->w_cursor;
1441
1442 curwin->w_cursor = *pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001443 // don't use del_char(), it also removes composing chars
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02001444 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 ins_char(nc);
1446 curwin->w_cursor = sp;
1447 }
1448 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02001449 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return TRUE;
1451 }
1452 return FALSE;
1453}
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455/*
1456 * op_insert - Insert and append operators for Visual mode.
1457 */
1458 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001459op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 long ins_len, pre_textlen = 0;
1462 char_u *firstline, *ins_text;
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001463 colnr_T ind_pre_col = 0, ind_post_col;
1464 int ind_pre_vcol = 0, ind_post_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 struct block_def bd;
1466 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001467 pos_T t1;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001468 pos_T start_insert;
1469 // offset when cursor was moved in insert mode
1470 int offset = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001472 // edit() changes this - record it for OP_APPEND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 // vis block is still marked. Get rid of it now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 curwin->w_cursor.lnum = oap->start.lnum;
1477 update_screen(INVERTED);
1478
1479 if (oap->block_mode)
1480 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001481 // When 'virtualedit' is used, need to insert the extra spaces before
1482 // doing block_prep(). When only "block" is used, virtual edit is
1483 // already disabled, but still need it when calling
1484 // coladvance_force().
Gary Johnson53ba05b2021-07-26 22:19:10 +02001485 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
Gary Johnson51ad8502021-08-03 18:33:08 +02001486 // state for the current window. To override that state, we need to
1487 // set the window-local value of ve_flags rather than the global value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (curwin->w_cursor.coladd > 0)
1489 {
Gary Johnson51ad8502021-08-03 18:33:08 +02001490 int old_ve_flags = curwin->w_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (u_save_cursor() == FAIL)
1493 return;
Gary Johnson53ba05b2021-07-26 22:19:10 +02001494
Gary Johnson51ad8502021-08-03 18:33:08 +02001495 curwin->w_ve_flags = VE_ALL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 coladvance_force(oap->op_type == OP_APPEND
1497 ? oap->end_vcol + 1 : getviscol());
1498 if (oap->op_type == OP_APPEND)
1499 --curwin->w_cursor.col;
Gary Johnson51ad8502021-08-03 18:33:08 +02001500 curwin->w_ve_flags = old_ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001502 // Get the info about the block before entering the text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // Get indent information
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001505 ind_pre_col = (colnr_T)getwhitecols_curline();
1506 ind_pre_vcol = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001508
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (oap->op_type == OP_APPEND)
1510 firstline += bd.textlen;
1511 pre_textlen = (long)STRLEN(firstline);
1512 }
1513
1514 if (oap->op_type == OP_APPEND)
1515 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001516 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001518 // Move the cursor to the character right of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 curwin->w_set_curswant = TRUE;
1520 while (*ml_get_cursor() != NUL
1521 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1522 ++curwin->w_cursor.col;
1523 if (bd.is_short && !bd.is_MAX)
1524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001525 // First line was too short, make it longer and adjust the
1526 // values in "bd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (u_save_cursor() == FAIL)
1528 return;
1529 for (i = 0; i < bd.endspaces; ++i)
1530 ins_char(' ');
1531 bd.textlen += bd.endspaces;
1532 }
1533 }
1534 else
1535 {
1536 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00001537 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001539 // Works just like an 'i'nsert on the next character.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001540 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 && oap->start_vcol != oap->end_vcol)
1542 inc_cursor();
1543 }
1544 }
1545
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001546 t1 = oap->start;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001547 start_insert = curwin->w_cursor;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01001548 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001550 // When a tab was inserted, and the characters in front of the tab
1551 // have been converted to a tab as well, the column of the cursor
1552 // might have actually been reduced, so need to adjust here.
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001553 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001554 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001555 oap->start = curbuf->b_op_start_orig;
1556
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001557 // If user has moved off this line, we don't know what to do, so do
1558 // nothing.
1559 // Also don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001560 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 return;
1562
1563 if (oap->block_mode)
1564 {
1565 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001566 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001567 size_t len;
1568 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001570 // If indent kicked in, the firstline might have changed
1571 // but only do that, if the indent actually increased.
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001572 ind_post_col = (colnr_T)getwhitecols_curline();
1573 if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001574 {
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001575 bd.textcol += ind_post_col - ind_pre_col;
1576 ind_post_vcol = get_indent();
1577 bd.start_vcol += ind_post_vcol - ind_pre_vcol;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001578 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02001579 }
1580
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001581 // The user may have moved the cursor before inserting something, try
1582 // to adjust the block for that. But only do it, if the difference
1583 // does not come from indent kicking in.
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001584 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1585 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001586 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001587 int t = getviscol2(curbuf->b_op_start_orig.col,
1588 curbuf->b_op_start_orig.coladd);
1589
1590 if (!bd.is_MAX)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001591 {
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001592 if (oap->op_type == OP_INSERT
1593 && oap->start.col + oap->start.coladd
1594 != curbuf->b_op_start_orig.col
1595 + curbuf->b_op_start_orig.coladd)
1596 {
1597 oap->start.col = curbuf->b_op_start_orig.col;
1598 pre_textlen -= t - oap->start_vcol;
1599 oap->start_vcol = t;
1600 }
1601 else if (oap->op_type == OP_APPEND
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001602 && oap->start.col + oap->start.coladd
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001603 >= curbuf->b_op_start_orig.col
1604 + curbuf->b_op_start_orig.coladd)
1605 {
1606 oap->start.col = curbuf->b_op_start_orig.col;
1607 // reset pre_textlen to the value of OP_INSERT
1608 pre_textlen += bd.textlen;
1609 pre_textlen -= t - oap->start_vcol;
1610 oap->start_vcol = t;
1611 oap->op_type = OP_INSERT;
1612 }
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001613 }
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001614 else if (bd.is_MAX && oap->op_type == OP_APPEND)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001616 // reset pre_textlen to the value of OP_INSERT
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001617 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01001618 pre_textlen -= t - oap->start_vcol;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01001619 }
1620 }
1621
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001622 // Spaces and tabs in the indent may have changed to other spaces and
1623 // tabs. Get the starting column again and correct the length.
1624 // Don't do this when "$" used, end-of-line will have changed.
1625 //
1626 // if indent was added and the inserted text was after the indent,
1627 // correct the selection for the new indent.
1628 if (did_indent && bd.textcol - ind_post_col > 0)
1629 {
1630 oap->start.col += ind_post_col - ind_pre_col;
1631 oap->start_vcol += ind_post_vcol - ind_pre_vcol;
1632 oap->end.col += ind_post_col - ind_pre_col;
1633 oap->end_vcol += ind_post_vcol - ind_pre_vcol;
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 block_prep(oap, &bd2, oap->start.lnum, TRUE);
Bram Moolenaar59f4f952021-11-27 22:47:43 +00001636 if (did_indent && bd.textcol - ind_post_col > 0)
1637 {
1638 // undo for where "oap" is used below
1639 oap->start.col -= ind_post_col - ind_pre_col;
1640 oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
1641 oap->end.col -= ind_post_col - ind_pre_col;
1642 oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
1643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1645 {
1646 if (oap->op_type == OP_APPEND)
1647 {
1648 pre_textlen += bd2.textlen - bd.textlen;
1649 if (bd2.endspaces)
1650 --bd2.textlen;
1651 }
1652 bd.textcol = bd2.textcol;
1653 bd.textlen = bd2.textlen;
1654 }
1655
1656 /*
1657 * Subsequent calls to ml_get() flush the firstline data - take a
1658 * copy of the required string.
1659 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001660 firstline = ml_get(oap->start.lnum);
1661 len = STRLEN(firstline);
1662 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (oap->op_type == OP_APPEND)
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001664 {
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001665 add += bd.textlen;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001666 // account for pressing cursor in insert mode when '$' was used
1667 if (bd.is_MAX
1668 && (start_insert.lnum == Insstart.lnum
1669 && start_insert.col > Insstart.col))
1670 {
1671 offset = (start_insert.col - Insstart.col);
1672 add -= offset;
1673 if (oap->end_vcol > offset)
1674 oap->end_vcol -= (offset + 1);
1675 else
1676 // moved outside of the visual block, what to do?
1677 return;
1678 }
1679 }
Bram Moolenaar35e802e2018-04-30 17:21:03 +02001680 if ((size_t)add > len)
1681 firstline += len; // short line, point to the NUL
1682 else
1683 firstline += add;
Bram Moolenaar4067bd32021-06-29 18:54:35 +02001684 if (pre_textlen >= 0 && (ins_len =
1685 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001687 ins_text = vim_strnsave(firstline, ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (ins_text != NULL)
1689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001690 // block handled here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 if (u_save(oap->start.lnum,
1692 (linenr_T)(oap->end.lnum + 1)) == OK)
1693 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1694 &bd);
1695
1696 curwin->w_cursor.col = oap->start.col;
1697 check_cursor();
1698 vim_free(ins_text);
1699 }
1700 }
1701 }
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704/*
1705 * op_change - handle a change operation
1706 *
1707 * return TRUE if edit() returns because of a CTRL-O command
1708 */
1709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001710op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 colnr_T l;
1713 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 long offset;
1715 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00001716 long ins_len;
1717 long pre_textlen = 0;
1718 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 char_u *firstline;
1720 char_u *ins_text, *newp, *oldp;
1721 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 l = oap->start.col;
1724 if (oap->motion_type == MLINE)
1725 {
1726 l = 0;
1727#ifdef FEAT_SMARTINDENT
1728 if (!p_paste && curbuf->b_p_si
1729# ifdef FEAT_CINDENT
1730 && !curbuf->b_p_cin
1731# endif
1732 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001733 can_si = TRUE; // It's like opening a new line, do si
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
1735 }
1736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001737 // First delete the text in the region. In an empty buffer only need to
1738 // save for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1740 {
1741 if (u_save_cursor() == FAIL)
1742 return FALSE;
1743 }
1744 else if (op_delete(oap) == FAIL)
1745 return FALSE;
1746
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001747 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 && !virtual_op)
1749 inc_cursor();
1750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001751 // check for still on same line (<CR> in inserted text meaningless)
1752 // skip blank lines too
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if (oap->block_mode)
1754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001755 // Add spaces before getting the current line length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (virtual_op && (curwin->w_cursor.coladd > 0
1757 || gchar_cursor() == NUL))
1758 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00001759 firstline = ml_get(oap->start.lnum);
1760 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001761 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 bd.textcol = curwin->w_cursor.col;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1766 if (oap->motion_type == MLINE)
1767 fix_indent();
1768#endif
1769
1770 retval = edit(NUL, FALSE, (linenr_T)1);
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001773 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001775 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00001777 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001779 // Auto-indenting may have changed the indent. If the cursor was past
1780 // the indent, exclude that indent change from the inserted text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00001782 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001784 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00001785
1786 pre_textlen += new_indent - pre_indent;
1787 bd.textcol += new_indent - pre_indent;
1788 }
1789
1790 ins_len = (long)STRLEN(firstline) - pre_textlen;
1791 if (ins_len > 0)
1792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001793 // Subsequent calls to ml_get() flush the firstline data - take a
1794 // copy of the inserted text.
Bram Moolenaar964b3742019-05-24 18:54:09 +02001795 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001797 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1799 linenr++)
1800 {
1801 block_prep(oap, &bd, linenr, TRUE);
1802 if (!bd.is_short || virtual_op)
1803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 pos_T vpos;
1805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001806 // If the block starts in virtual space, count the
1807 // initial coladd offset as part of "startspaces"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (bd.is_short)
1809 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00001810 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 else
1814 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001816 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (newp == NULL)
1818 continue;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 // copy up to block start
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 mch_memmove(newp, oldp, (size_t)bd.textcol);
1821 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001822 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1825 offset += ins_len;
1826 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001827 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 ml_replace(linenr, newp, FALSE);
1829 }
1830 }
1831 check_cursor();
1832
1833 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1834 }
1835 vim_free(ins_text);
1836 }
1837 }
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001838 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840 return retval;
1841}
1842
1843/*
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001844 * When the cursor is on the NUL past the end of the line and it should not be
1845 * there move it left.
1846 */
1847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001848adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001849{
Gary Johnson53ba05b2021-07-26 22:19:10 +02001850 unsigned int cur_ve_flags = get_ve_flags();
1851
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001852 if (curwin->w_cursor.col > 0
1853 && gchar_cursor() == NUL
Gary Johnson53ba05b2021-07-26 22:19:10 +02001854 && (cur_ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 && !(restart_edit || (State & INSERT)))
1856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001857 // Put the cursor on the last character in the line.
Bram Moolenaara5fac542005-10-12 20:58:49 +00001858 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001859
Gary Johnson53ba05b2021-07-26 22:19:10 +02001860 if (cur_ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00001861 {
1862 colnr_T scol, ecol;
1863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // Coladd is set to the width of the last character.
Bram Moolenaara5792f52005-11-23 21:25:05 +00001865 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1866 curwin->w_cursor.coladd = ecol - scol + 1;
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869}
1870
Bram Moolenaar81340392012-06-06 16:12:59 +02001871/*
1872 * If "process" is TRUE and the line begins with a comment leader (possibly
1873 * after some white space), return a pointer to the text after it. Put a boolean
1874 * value indicating whether the line ends with an unclosed comment in
1875 * "is_comment".
1876 * line - line to be processed,
1877 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001878 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02001879 * include_space - whether to also skip space following the comment leader,
1880 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001881 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001882 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01001883 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01001884skip_comment(
1885 char_u *line,
1886 int process,
1887 int include_space,
1888 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02001889{
1890 char_u *comment_flags = NULL;
1891 int lead_len;
1892 int leader_offset = get_last_leader_offset(line, &comment_flags);
1893
1894 *is_comment = FALSE;
1895 if (leader_offset != -1)
1896 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 // Let's check whether the line ends with an unclosed comment.
1898 // If the last comment leader has COM_END in flags, there's no comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02001899 while (*comment_flags)
1900 {
1901 if (*comment_flags == COM_END
1902 || *comment_flags == ':')
1903 break;
1904 ++comment_flags;
1905 }
1906 if (*comment_flags != COM_END)
1907 *is_comment = TRUE;
1908 }
1909
1910 if (process == FALSE)
1911 return line;
1912
1913 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1914
1915 if (lead_len == 0)
1916 return line;
1917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 // Find:
1919 // - COM_END,
1920 // - colon,
1921 // whichever comes first.
Bram Moolenaar81340392012-06-06 16:12:59 +02001922 while (*comment_flags)
1923 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02001924 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02001926 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02001927 ++comment_flags;
1928 }
1929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001930 // If we found a colon, it means that we are not processing a line
1931 // starting with a closing part of a three-part comment. That's good,
1932 // because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02001933 if (*comment_flags == ':' || *comment_flags == NUL)
1934 line += lead_len;
1935
1936 return line;
1937}
Bram Moolenaar81340392012-06-06 16:12:59 +02001938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001940 * Join 'count' lines (minimal 2) at cursor position.
1941 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001942 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1943 * leaders should not be removed.
1944 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1945 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00001947 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 */
1949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001950do_join(
1951 long count,
1952 int insert_space,
1953 int save_undo,
1954 int use_formatoptions UNUSED,
1955 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001957 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02001958 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001959 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 char_u *newp;
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001961 size_t newp_len;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001962 char_u *spaces; // number of spaces inserted before a line
Bram Moolenaard43848c2010-07-14 14:28:26 +02001963 int endcurr1 = NUL;
1964 int endcurr2 = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001965 int currsize = 0; // size of the current line
1966 int sumsize = 0; // size of the long new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001968 colnr_T col = 0;
1969 int ret = OK;
Bram Moolenaar802053f2012-06-06 23:08:38 +02001970 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001971 int remove_comments = (use_formatoptions == TRUE)
1972 && has_format_option(FO_REMOVE_COMS);
1973 int prev_was_comment;
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001974#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02001975 int propcount = 0; // number of props over all joined lines
1976 int props_remaining;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001979 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1980 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1981 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001983 // Allocate an array to store the number of spaces inserted before each
1984 // line. We will use it to pre-compute the length of the new line and the
1985 // proper placement of each original line in the new one.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001986 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001987 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02001989 if (remove_comments)
1990 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001991 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02001992 if (comments == NULL)
1993 {
1994 vim_free(spaces);
1995 return FAIL;
1996 }
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
1999 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002000 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002001 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002002 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002004 for (t = 0; t < count; ++t)
2005 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002006 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002007#ifdef FEAT_PROP_POPUP
2008 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
2009#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02002010 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002012 // Set the '[ mark.
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002013 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
2014 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
2015 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002016 if (remove_comments)
2017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002018 // We don't want to remove the comment leader if the
2019 // previous line is not a comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02002020 if (t > 0 && prev_was_comment)
2021 {
2022
2023 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2024 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02002025 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02002026 curr = new_curr;
2027 }
2028 else
2029 curr = skip_comment(curr, FALSE, insert_space,
2030 &prev_was_comment);
2031 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002032
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002033 if (insert_space && t > 0)
2034 {
2035 curr = skipwhite(curr);
Bram Moolenaarcc184cf2019-11-12 20:31:20 +01002036 if (*curr != NUL && *curr != ')'
Bram Moolenaar91b65e42019-12-17 22:10:58 +01002037 && sumsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002038 && (!has_format_option(FO_MBYTE_JOIN)
2039 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2040 && (!has_format_option(FO_MBYTE_JOIN2)
Bram Moolenaare52702f2020-06-04 18:22:13 +02002041 || (mb_ptr2char(curr) < 0x100
2042 && !(enc_utf8 && utf_eat_space(endcurr1)))
2043 || (endcurr1 < 0x100
2044 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002045 )
2046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002047 // don't add a space if the line is ending in a space
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002048 if (endcurr1 == ' ')
2049 endcurr1 = endcurr2;
2050 else
2051 ++spaces[t];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002052 // extra space when 'joinspaces' set and line ends in '.'
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002053 if ( p_js
2054 && (endcurr1 == '.'
2055 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2056 && (endcurr1 == '?' || endcurr1 == '!'))))
2057 ++spaces[t];
2058 }
2059 }
2060 currsize = (int)STRLEN(curr);
2061 sumsize += currsize + spaces[t];
2062 endcurr1 = endcurr2 = NUL;
2063 if (insert_space && currsize > 0)
2064 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002065 if (has_mbyte)
2066 {
2067 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002068 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002069 endcurr1 = (*mb_ptr2char)(cend);
2070 if (cend > curr)
2071 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002072 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002073 endcurr2 = (*mb_ptr2char)(cend);
2074 }
2075 }
2076 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002077 {
2078 endcurr1 = *(curr + currsize - 1);
2079 if (currsize > 1)
2080 endcurr2 = *(curr + currsize - 2);
2081 }
2082 }
2083 line_breakcheck();
2084 if (got_int)
2085 {
2086 ret = FAIL;
2087 goto theend;
2088 }
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 // store the column position before last line
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002092 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002094 // allocate the space for the new line
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002095 newp_len = sumsize + 1;
2096#ifdef FEAT_PROP_POPUP
2097 newp_len += propcount * sizeof(textprop_T);
2098#endif
2099 newp = alloc(newp_len);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002100 if (newp == NULL)
2101 {
2102 ret = FAIL;
2103 goto theend;
2104 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002105 cend = newp + sumsize;
2106 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002108 /*
2109 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002110 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002111 *
2112 * Move marks from each deleted line to the joined line, adjusting the
2113 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2114 * should not really be a problem.
2115 */
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002116#ifdef FEAT_PROP_POPUP
2117 props_remaining = propcount;
2118#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002119 for (t = count - 1; ; --t)
2120 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002121 int spaces_removed;
2122
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002123 cend -= currsize;
2124 mch_memmove(cend, curr, (size_t)currsize);
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002125
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002126 if (spaces[t] > 0)
2127 {
2128 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002129 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002130 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002131
2132 // If deleting more spaces than adding, the cursor moves no more than
2133 // what is added if it is inside these spaces.
2134 spaces_removed = (curr - curr_start) - spaces[t];
2135
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002136 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01002137 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002138#ifdef FEAT_PROP_POPUP
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002139 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2140 curwin->w_cursor.lnum + t, t == count - 1,
2141 (long)(cend - newp), spaces_removed);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002142#endif
2143
Bram Moolenaar87be9be2020-05-30 15:32:02 +02002144 if (t == 0)
2145 break;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02002146 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar81340392012-06-06 16:12:59 +02002147 if (remove_comments)
2148 curr += comments[t - 1];
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002149 if (insert_space && t > 1)
2150 curr = skipwhite(curr);
2151 currsize = (int)STRLEN(curr);
2152 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02002153
Bram Moolenaard6a77f92020-06-01 19:14:12 +02002154 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaare1004402020-10-24 20:49:43 +02002156 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002157 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002158 // Set the '] mark.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002159 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02002160 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02002161 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01002162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002163 // Only report the change in the first line here, del_lines() will report
2164 // the deleted line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 changed_lines(curwin->w_cursor.lnum, currsize,
2166 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002168 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 * briefly, and then move it back. After del_lines() the cursor may
2170 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
2172 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002174 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 curwin->w_cursor.lnum = t;
2176
2177 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002178 * Set the cursor column:
2179 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002180 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002182 curwin->w_cursor.col =
2183 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 curwin->w_set_curswant = TRUE;
2188
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002189theend:
2190 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02002191 if (remove_comments)
2192 vim_free(comments);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02002193 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194}
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 * prepare a few things for block mode yank/delete/tilde
2198 *
2199 * for delete:
2200 * - textlen includes the first/last char to be (partly) deleted
2201 * - start/endspaces is the number of columns that are taken by the
2202 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00002203 * deleted.
2204 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * - textlen includes the first/last char to be wholly yanked
2206 * - start/endspaces is the number of columns of the first/last yanked char
2207 * that are to be yanked.
2208 */
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002210block_prep(
2211 oparg_T *oap,
2212 struct block_def *bdp,
2213 linenr_T lnum,
2214 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int incr = 0;
2217 char_u *pend;
2218 char_u *pstart;
2219 char_u *line;
2220 char_u *prev_pstart;
2221 char_u *prev_pend;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002222#ifdef FEAT_LINEBREAK
2223 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002225 // Avoid a problem with unwanted linebreaks in block mode.
2226 curwin->w_p_lbr = FALSE;
2227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 bdp->startspaces = 0;
2229 bdp->endspaces = 0;
2230 bdp->textlen = 0;
2231 bdp->start_vcol = 0;
2232 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 bdp->is_short = FALSE;
2234 bdp->is_oneChar = FALSE;
2235 bdp->pre_whitesp = 0;
2236 bdp->pre_whitesp_c = 0;
2237 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 bdp->start_char_vcols = 0;
2239
2240 line = ml_get(lnum);
2241 pstart = line;
2242 prev_pstart = line;
2243 while (bdp->start_vcol < oap->start_vcol && *pstart)
2244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002245 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar597a4222014-06-25 14:39:50 +02002246 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002248 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 bdp->pre_whitesp += incr;
2251 bdp->pre_whitesp_c++;
2252 }
2253 else
2254 {
2255 bdp->pre_whitesp = 0;
2256 bdp->pre_whitesp_c = 0;
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002259 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261 bdp->start_char_vcols = incr;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002262 if (bdp->start_vcol < oap->start_vcol) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (!is_del || oap->op_type == OP_APPEND)
2267 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2268 }
2269 else
2270 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // notice: this converts partly selected Multibyte characters to
2272 // spaces, too.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2274 if (is_del && bdp->startspaces)
2275 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2276 pend = pstart;
2277 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002278 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (oap->op_type == OP_INSERT)
2282 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2283 else if (oap->op_type == OP_APPEND)
2284 {
2285 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2286 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2287 }
2288 else
2289 {
2290 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2291 if (is_del && oap->op_type != OP_LSHIFT)
2292 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002293 // just putting the sum of those two into
2294 // bdp->startspaces doesn't work for Visual replace,
2295 // so we have to split the tab in two
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 bdp->startspaces = bdp->start_char_vcols
2297 - (bdp->start_vcol - oap->start_vcol);
2298 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2299 }
2300 }
2301 }
2302 else
2303 {
2304 prev_pend = pend;
2305 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2306 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002307 // Count a tab for what it's worth (if list mode not on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01002309 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 bdp->end_vcol += incr;
2311 }
2312 if (bdp->end_vcol <= oap->end_vcol
2313 && (!is_del
2314 || oap->op_type == OP_APPEND
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002315 || oap->op_type == OP_REPLACE)) // line too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 bdp->is_short = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002318 // Alternative: include spaces to fill up the block.
2319 // Disadvantage: can lead to trailing spaces when the line is
2320 // short where the text is put
2321 // if (!is_del || oap->op_type == OP_APPEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (oap->op_type == OP_APPEND || virtual_op)
2323 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002324 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002326 bdp->endspaces = 0; // replace doesn't add characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 else if (bdp->end_vcol > oap->end_vcol)
2329 {
2330 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2331 if (!is_del && bdp->endspaces)
2332 {
2333 bdp->endspaces = incr - bdp->endspaces;
2334 if (pend != pstart)
2335 pend = prev_pend;
2336 }
2337 }
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (is_del && bdp->startspaces)
2341 pstart = prev_pstart;
2342 bdp->textlen = (int)(pend - pstart);
2343 }
2344 bdp->textcol = (colnr_T) (pstart - line);
2345 bdp->textstart = pstart;
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01002346#ifdef FEAT_LINEBREAK
2347 curwin->w_p_lbr = lbr_saved;
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01002352 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002355op_addsub(
2356 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002357 linenr_T Prenum1, // Amount of add/subtract
2358 int g_cmd) // was g<c-a>/g<c-x>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaard79e5502016-01-10 22:13:02 +01002360 pos_T pos;
2361 struct block_def bd;
2362 int change_cnt = 0;
2363 linenr_T amount = Prenum1;
2364
Bram Moolenaar6b731882018-11-16 20:54:47 +01002365 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01002366 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01002367 // the call to changed_lines().
2368#ifdef FEAT_FOLDING
2369 disable_fold_update++;
2370#endif
2371
Bram Moolenaard79e5502016-01-10 22:13:02 +01002372 if (!VIsual_active)
2373 {
2374 pos = curwin->w_cursor;
2375 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002376 {
2377#ifdef FEAT_FOLDING
2378 disable_fold_update--;
2379#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002380 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002381 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002382 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01002383#ifdef FEAT_FOLDING
2384 disable_fold_update--;
2385#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002386 if (change_cnt)
2387 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2388 }
2389 else
2390 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002391 int one_change;
2392 int length;
2393 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002394
2395 if (u_save((linenr_T)(oap->start.lnum - 1),
2396 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01002397 {
2398#ifdef FEAT_FOLDING
2399 disable_fold_update--;
2400#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002401 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01002402 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002403
2404 pos = oap->start;
2405 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2406 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 if (oap->block_mode) // Visual block mode
Bram Moolenaard79e5502016-01-10 22:13:02 +01002408 {
2409 block_prep(oap, &bd, pos.lnum, FALSE);
2410 pos.col = bd.textcol;
2411 length = bd.textlen;
2412 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002413 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002414 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002415 curwin->w_cursor.col = 0;
2416 pos.col = 0;
2417 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2418 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 else // oap->motion_type == MCHAR
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002420 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01002421 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002422 dec(&(oap->end));
2423 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2424 pos.col = 0;
2425 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002426 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002427 pos.col += oap->start.col;
2428 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002429 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002430 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002431 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002432 length = (int)STRLEN(ml_get(oap->end.lnum));
2433 if (oap->end.col >= length)
2434 oap->end.col = length - 1;
2435 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002436 }
2437 }
2438 one_change = do_addsub(oap->op_type, &pos, length, amount);
2439 if (one_change)
2440 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002441 // Remember the start position of the first change.
Bram Moolenaard79e5502016-01-10 22:13:02 +01002442 if (change_cnt == 0)
2443 startpos = curbuf->b_op_start;
2444 ++change_cnt;
2445 }
2446
2447#ifdef FEAT_NETBEANS_INTG
2448 if (netbeans_active() && one_change)
2449 {
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002450 char_u *ptr;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002451
2452 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +02002453 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002454 netbeans_inserted(curbuf, pos.lnum, pos.col,
2455 &ptr[pos.col], length);
2456 }
2457#endif
2458 if (g_cmd && one_change)
2459 amount += Prenum1;
2460 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01002461
2462#ifdef FEAT_FOLDING
2463 disable_fold_update--;
2464#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002465 if (change_cnt)
2466 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2467
2468 if (!change_cnt && oap->is_VIsual)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002469 // No change: need to remove the Visual selection
Bram Moolenaard79e5502016-01-10 22:13:02 +01002470 redraw_curbuf_later(INVERTED);
2471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002472 // Set '[ mark if something changed. Keep the last end
2473 // position from do_addsub().
Bram Moolenaare1004402020-10-24 20:49:43 +02002474 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002475 curbuf->b_op_start = startpos;
2476
2477 if (change_cnt > p_report)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002478 smsg(NGETTEXT("%d line changed", "%d lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002479 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002480 }
2481}
2482
2483/*
2484 * Add or subtract 'Prenum1' from a number in a line
2485 * op_type is OP_NR_ADD or OP_NR_SUB
2486 *
2487 * Returns TRUE if some character was changed.
2488 */
2489 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002490do_addsub(
2491 int op_type,
2492 pos_T *pos,
2493 int length,
2494 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002495{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 int col;
2497 char_u *buf1;
2498 char_u buf2[NUMBUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002499 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2500 static int hexupper = FALSE; // 0xABC
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002501 uvarnumber_T n;
2502 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 char_u *ptr;
2504 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int todel;
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002506 int do_hex;
2507 int do_oct;
2508 int do_bin;
2509 int do_alpha;
2510 int do_unsigned;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002513 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002514 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002515 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02002516 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002517 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002518 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002519 pos_T startpos;
2520 pos_T endpos;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002521 colnr_T save_coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002523 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2524 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2525 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2526 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2527 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002529 if (virtual_active())
2530 {
2531 save_coladd = pos->coladd;
2532 pos->coladd = 0;
2533 }
2534
Bram Moolenaard79e5502016-01-10 22:13:02 +01002535 curwin->w_cursor = *pos;
2536 ptr = ml_get(pos->lnum);
2537 col = pos->col;
2538
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002539 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002540 goto theend;
2541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /*
2543 * First check if we are on a hexadecimal number, after the "0x".
2544 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002545 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002547 if (do_bin)
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002548 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002549 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002550 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002551 if (has_mbyte)
2552 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002553 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002554
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002555 if (do_hex)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002556 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002557 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002558 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002559 if (has_mbyte)
2560 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002561 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002562
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002563 if ( do_bin
2564 && do_hex
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002565 && ! ((col > 0
2566 && (ptr[col] == 'X'
2567 || ptr[col] == 'x')
2568 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002569 && (!has_mbyte ||
2570 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002571 && vim_isxdigit(ptr[col + 1]))))
2572 {
2573
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002574 // In case of binary/hexadecimal pattern overlap match, rescan
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002575
Bram Moolenaard79e5502016-01-10 22:13:02 +01002576 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002577
2578 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002579 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002580 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002581 if (has_mbyte)
2582 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002583 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002584 }
2585
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002586 if (( do_hex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002587 && col > 0
2588 && (ptr[col] == 'X'
2589 || ptr[col] == 'x')
2590 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002591 && (!has_mbyte ||
2592 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002593 && vim_isxdigit(ptr[col + 1])) ||
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002594 ( do_bin
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002595 && col > 0
2596 && (ptr[col] == 'B'
2597 || ptr[col] == 'b')
2598 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002599 && (!has_mbyte ||
2600 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01002601 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // Found hexadecimal or binary number, move to its start.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002604 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002605 if (has_mbyte)
2606 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002607 }
2608 else
2609 {
2610 /*
2611 * Search forward and then backward to find the start of number.
2612 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01002613 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002614
2615 while (ptr[col] != NUL
2616 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002617 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaar1614a142019-10-06 22:00:13 +02002618 col += mb_ptr2len(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002619
2620 while (col > 0
2621 && vim_isdigit(ptr[col - 1])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002622 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002623 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002624 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002625 if (has_mbyte)
2626 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002627 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002628 }
2629 }
2630
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02002631 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002632 {
2633 while (ptr[col] != NUL && length > 0
2634 && !vim_isdigit(ptr[col])
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002635 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002636 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02002637 int mb_len = mb_ptr2len(ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002638
2639 col += mb_len;
2640 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002641 }
2642
2643 if (length == 0)
2644 goto theend;
2645
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002646 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002647 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2648 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002649 {
2650 negative = TRUE;
2651 was_positive = FALSE;
2652 }
2653 }
2654
2655 /*
2656 * If a number was found, and saving for undo works, replace the number.
2657 */
2658 firstdigit = ptr[col];
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002659 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002660 {
2661 beep_flush();
2662 goto theend;
2663 }
2664
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002665 if (do_alpha && ASCII_ISALPHA(firstdigit))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002666 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // decrement or increment alphabetic character
Bram Moolenaard79e5502016-01-10 22:13:02 +01002668 if (op_type == OP_NR_SUB)
2669 {
2670 if (CharOrd(firstdigit) < Prenum1)
2671 {
2672 if (isupper(firstdigit))
2673 firstdigit = 'A';
2674 else
2675 firstdigit = 'a';
2676 }
2677 else
2678#ifdef EBCDIC
2679 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2680#else
2681 firstdigit -= Prenum1;
2682#endif
2683 }
2684 else
2685 {
2686 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2687 {
2688 if (isupper(firstdigit))
2689 firstdigit = 'Z';
2690 else
2691 firstdigit = 'z';
2692 }
2693 else
2694#ifdef EBCDIC
2695 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2696#else
2697 firstdigit += Prenum1;
2698#endif
2699 }
2700 curwin->w_cursor.col = col;
2701 if (!did_change)
2702 startpos = curwin->w_cursor;
2703 did_change = TRUE;
2704 (void)del_char(FALSE);
2705 ins_char(firstdigit);
2706 endpos = curwin->w_cursor;
2707 curwin->w_cursor.col = col;
2708 }
2709 else
2710 {
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002711 pos_T save_pos;
2712 int i;
2713
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002714 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02002715 && (!has_mbyte ||
2716 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002717 && !visual
2718 && !do_unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // negative number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002721 --col;
2722 negative = TRUE;
2723 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 // get the number value (unsigned)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002725 if (visual && VIsual_mode != 'V')
2726 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2727 ? (int)STRLEN(ptr) - col
2728 : length);
2729
2730 vim_str2nr(ptr + col, &pre, &length,
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002731 0 + (do_bin ? STR2NR_BIN : 0)
2732 + (do_oct ? STR2NR_OCT : 0)
2733 + (do_hex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002734 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // ignore leading '-' for hex and octal and bin numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002737 if (pre && negative)
2738 {
2739 ++col;
2740 --length;
2741 negative = FALSE;
2742 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // add or subtract
Bram Moolenaard79e5502016-01-10 22:13:02 +01002744 subtract = FALSE;
2745 if (op_type == OP_NR_SUB)
2746 subtract ^= TRUE;
2747 if (negative)
2748 subtract ^= TRUE;
2749
2750 oldn = n;
2751 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002752 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002753 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002754 n += (uvarnumber_T)Prenum1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // handle wraparound for decimal numbers
Bram Moolenaard79e5502016-01-10 22:13:02 +01002756 if (!pre)
2757 {
2758 if (subtract)
2759 {
2760 if (n > oldn)
2761 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002762 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002763 negative ^= TRUE;
2764 }
2765 }
2766 else
2767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // add
Bram Moolenaard79e5502016-01-10 22:13:02 +01002769 if (n < oldn)
2770 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002771 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002772 negative ^= TRUE;
2773 }
2774 }
2775 if (n == 0)
2776 negative = FALSE;
2777 }
2778
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002779 if (do_unsigned && negative)
2780 {
2781 if (subtract)
2782 // sticking at zero.
2783 n = (uvarnumber_T)0;
2784 else
2785 // sticking at 2^64 - 1.
2786 n = (uvarnumber_T)(-1);
2787 negative = FALSE;
2788 }
2789
Bram Moolenaard79e5502016-01-10 22:13:02 +01002790 if (visual && !was_positive && !negative && col > 0)
2791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002792 // need to remove the '-'
Bram Moolenaard79e5502016-01-10 22:13:02 +01002793 col--;
2794 length++;
2795 }
2796
2797 /*
2798 * Delete the old number.
2799 */
2800 curwin->w_cursor.col = col;
2801 if (!did_change)
2802 startpos = curwin->w_cursor;
2803 did_change = TRUE;
2804 todel = length;
2805 c = gchar_cursor();
2806 /*
2807 * Don't include the '-' in the length, only the length of the
2808 * part after it is kept the same.
2809 */
2810 if (c == '-')
2811 --length;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002812
2813 save_pos = curwin->w_cursor;
2814 for (i = 0; i < todel; ++i)
Bram Moolenaard79e5502016-01-10 22:13:02 +01002815 {
2816 if (c < 0x100 && isalpha(c))
2817 {
2818 if (isupper(c))
2819 hexupper = TRUE;
2820 else
2821 hexupper = FALSE;
2822 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002823 inc_cursor();
Bram Moolenaard79e5502016-01-10 22:13:02 +01002824 c = gchar_cursor();
2825 }
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002826 curwin->w_cursor = save_pos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002827
2828 /*
2829 * Prepare the leading characters in buf1[].
2830 * When there are many leading zeros it could be very long.
2831 * Allocate a bit too much.
2832 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002833 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002834 if (buf1 == NULL)
2835 goto theend;
2836 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02002837 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002838 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01002839 if (pre)
2840 {
2841 *ptr++ = '0';
2842 --length;
2843 }
2844 if (pre == 'b' || pre == 'B' ||
2845 pre == 'x' || pre == 'X')
2846 {
2847 *ptr++ = pre;
2848 --length;
2849 }
2850
2851 /*
2852 * Put the number characters in buf2[].
2853 */
2854 if (pre == 'b' || pre == 'B')
2855 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01002856 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002857 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01002858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // leading zeros
Bram Moolenaard79e5502016-01-10 22:13:02 +01002860 for (bit = bits; bit > 0; bit--)
2861 if ((n >> (bit - 1)) & 0x1) break;
2862
2863 for (i = 0; bit > 0; bit--)
2864 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2865
2866 buf2[i] = '\0';
2867 }
2868 else if (pre == 0)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002869 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002870 else if (pre == '0')
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002871 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002872 else if (pre && hexupper)
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002873 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002874 else
Bram Moolenaarf9706e92020-02-22 14:27:04 +01002875 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002876 length -= (int)STRLEN(buf2);
2877
2878 /*
2879 * Adjust number of zeros to the new number of digits, so the
2880 * total length of the number remains the same.
2881 * Don't do this when
2882 * the result may look like an octal number.
2883 */
Bram Moolenaaraaad9952020-05-31 15:08:59 +02002884 if (firstdigit == '0' && !(do_oct && pre == 0))
Bram Moolenaard79e5502016-01-10 22:13:02 +01002885 while (length-- > 0)
2886 *ptr++ = '0';
2887 *ptr = NUL;
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002888
Bram Moolenaard79e5502016-01-10 22:13:02 +01002889 STRCAT(buf1, buf2);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002890
2891 // Insert just after the first character to be removed, so that any
2892 // text properties will be adjusted. Then delete the old number
2893 // afterwards.
2894 save_pos = curwin->w_cursor;
2895 if (todel > 0)
2896 inc_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 ins_str(buf1); // insert the new number
Bram Moolenaard79e5502016-01-10 22:13:02 +01002898 vim_free(buf1);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002899
2900 // del_char() will also mark line needing displaying
2901 if (todel > 0)
2902 {
2903 int bytes_after = (int)STRLEN(ml_get_curline())
2904 - curwin->w_cursor.col;
2905
2906 // Delete the one character before the insert.
2907 curwin->w_cursor = save_pos;
2908 (void)del_char(FALSE);
Bram Moolenaar573545a2020-09-16 15:46:08 +02002909 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2910 - bytes_after);
Bram Moolenaarc8f12c92020-09-15 20:34:10 +02002911 --todel;
2912 }
2913 while (todel-- > 0)
2914 (void)del_char(FALSE);
2915
Bram Moolenaard79e5502016-01-10 22:13:02 +01002916 endpos = curwin->w_cursor;
2917 if (did_change && curwin->w_cursor.col)
2918 --curwin->w_cursor.col;
2919 }
2920
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // set the '[ and '] marks
Bram Moolenaara52dfae2016-01-10 20:21:57 +01002924 curbuf->b_op_start = startpos;
2925 curbuf->b_op_end = endpos;
2926 if (curbuf->b_op_end.col > 0)
2927 --curbuf->b_op_end.col;
2928 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002929
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002930theend:
2931 if (visual)
2932 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01002933 else if (did_change)
2934 curwin->w_set_curswant = TRUE;
Bram Moolenaar6c6be9e2020-06-12 20:19:44 +02002935 else if (virtual_active())
2936 curwin->w_cursor.coladd = save_coladd;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01002937
Bram Moolenaard79e5502016-01-10 22:13:02 +01002938 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939}
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002942clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002944 CLEAR_POINTER(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00002948 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 *
2950 * "Words" are counted by looking for boundaries between non-space and
2951 * space characters. (it seems to produce results that match 'wc'.)
2952 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00002953 * Return value is byte count; word count for the line is added to "*wc".
2954 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 *
2956 * The function will only examine the first "limit" characters in the
2957 * line, stopping if it encounters an end-of-line (NUL byte). In that
2958 * case, eol_size will be added to the character count to account for
2959 * the size of the EOL character.
2960 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002961 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01002962line_count_info(
2963 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002964 varnumber_T *wc,
2965 varnumber_T *cc,
2966 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01002967 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002969 varnumber_T i;
2970 varnumber_T words = 0;
2971 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 int is_word = 0;
2973
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02002974 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 if (is_word)
2977 {
2978 if (vim_isspace(line[i]))
2979 {
2980 words++;
2981 is_word = 0;
2982 }
2983 }
2984 else if (!vim_isspace(line[i]))
2985 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002986 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002987 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 }
2989
2990 if (is_word)
2991 words++;
2992 *wc += words;
2993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 // Add eol_size if the end of line was reached before hitting limit.
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02002995 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00002996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00002998 chars += eol_size;
2999 }
3000 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 return i;
3002}
3003
3004/*
3005 * Give some info about the position of the cursor (for "g CTRL-G").
3006 * In Visual mode, give some info about the selected region. (In this case,
3007 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01003008 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 */
3010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003011cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00003014 char_u buf1[50];
3015 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003017 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003018 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003019 varnumber_T byte_count_cursor = 0;
3020 varnumber_T char_count = 0;
3021 varnumber_T char_count_cursor = 0;
3022 varnumber_T word_count = 0;
3023 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003024 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003025 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 long line_count_selected = 0;
3027 pos_T min_pos, max_pos;
3028 oparg_T oparg;
3029 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 /*
3032 * Compute the length of the file in characters.
3033 */
3034 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3035 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003036 if (dict == NULL)
3037 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003038 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01003039 return;
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042 else
3043 {
3044 if (get_fileformat(curbuf) == EOL_DOS)
3045 eol_size = 2;
3046 else
3047 eol_size = 1;
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (VIsual_active)
3050 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003051 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
3053 min_pos = VIsual;
3054 max_pos = curwin->w_cursor;
3055 }
3056 else
3057 {
3058 min_pos = curwin->w_cursor;
3059 max_pos = VIsual;
3060 }
3061 if (*p_sel == 'e' && max_pos.col > 0)
3062 --max_pos.col;
3063
3064 if (VIsual_mode == Ctrl_V)
3065 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003066#ifdef FEAT_LINEBREAK
3067 char_u * saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003068 char_u * saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00003071 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01003072 curwin->w_p_sbr = empty_option;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 oparg.is_VIsual = 1;
3075 oparg.block_mode = TRUE;
3076 oparg.op_type = OP_NOP;
3077 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003078 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003079#ifdef FEAT_LINEBREAK
3080 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01003081 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00003082#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00003083 if (curwin->w_curswant == MAXCOL)
3084 oparg.end_vcol = MAXCOL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // Swap the start, end vcol if needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 if (oparg.end_vcol < oparg.start_vcol)
3087 {
3088 oparg.end_vcol += oparg.start_vcol;
3089 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3090 oparg.end_vcol -= oparg.start_vcol;
3091 }
3092 }
3093 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // Check for a CTRL-C every 100000 characters.
Bram Moolenaar7c626922005-02-07 22:01:03 +00003099 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
3101 ui_breakcheck();
3102 if (got_int)
3103 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003104 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // Do extra processing for VIsual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (VIsual_active
3109 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3110 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00003111 char_u *s = NULL;
3112 long len = 0L;
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 switch (VIsual_mode)
3115 {
3116 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003120 s = bd.textstart;
3121 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 break;
3123 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00003124 s = ml_get(lnum);
3125 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 break;
3127 case 'v':
3128 {
3129 colnr_T start_col = (lnum == min_pos.lnum)
3130 ? min_pos.col : 0;
3131 colnr_T end_col = (lnum == max_pos.lnum)
3132 ? max_pos.col - start_col + 1 : MAXCOL;
3133
Bram Moolenaardef9e822004-12-31 20:58:58 +00003134 s = ml_get(lnum) + start_col;
3135 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 }
3137 break;
3138 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003139 if (s != NULL)
3140 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00003141 byte_count_cursor += line_count_info(s, &word_count_cursor,
3142 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00003143 if (lnum == curbuf->b_ml.ml_line_count
3144 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003145 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003146 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00003147 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00003148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // In non-visual mode, check for the line the cursor is on
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 if (lnum == curwin->w_cursor.lnum)
3154 {
3155 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003156 char_count_cursor += char_count;
3157 byte_count_cursor = byte_count +
3158 line_count_info(ml_get(lnum),
3159 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003160 (varnumber_T)(curwin->w_cursor.col + 1),
3161 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // Add to the running totals
Bram Moolenaar7c626922005-02-07 22:01:03 +00003165 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003166 &char_count, (varnumber_T)MAXCOL,
3167 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
3169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003170 // Correction for when last line doesn't have an EOL.
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003171 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00003172 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaared767a22016-01-03 22:49:16 +01003174 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003176 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaared767a22016-01-03 22:49:16 +01003178 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3179 {
3180 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3181 &max_pos.col);
3182 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3183 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3184 }
3185 else
3186 buf1[0] = NUL;
3187
3188 if (char_count_cursor == byte_count_cursor
3189 && char_count == byte_count)
3190 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003191 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003192 buf1, line_count_selected,
3193 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003194 (varnumber_T)word_count_cursor,
3195 (varnumber_T)word_count,
3196 (varnumber_T)byte_count_cursor,
3197 (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003198 else
3199 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003200 _("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 +01003201 buf1, line_count_selected,
3202 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003203 (varnumber_T)word_count_cursor,
3204 (varnumber_T)word_count,
3205 (varnumber_T)char_count_cursor,
3206 (varnumber_T)char_count,
3207 (varnumber_T)byte_count_cursor,
3208 (varnumber_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 else
Bram Moolenaared767a22016-01-03 22:49:16 +01003211 {
3212 p = ml_get_curline();
3213 validate_virtcol();
3214 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3215 (int)curwin->w_virtcol + 1);
3216 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3217 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaared767a22016-01-03 22:49:16 +01003219 if (char_count_cursor == byte_count_cursor
3220 && char_count == byte_count)
3221 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003222 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01003223 (char *)buf1, (char *)buf2,
3224 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003226 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3227 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003228 else
3229 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003230 _("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 +01003231 (char *)buf1, (char *)buf2,
3232 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00003233 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003234 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3235 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3236 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01003237 }
3238 }
3239
Bram Moolenaared767a22016-01-03 22:49:16 +01003240 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02003241 if (dict == NULL && bom_count > 0)
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003242 {
3243 size_t len = STRLEN(IObuff);
3244
3245 vim_snprintf((char *)IObuff + len, IOSIZE - len,
Bram Moolenaarf9706e92020-02-22 14:27:04 +01003246 _("(+%lld for BOM)"), (varnumber_T)bom_count);
Bram Moolenaar08b28b72019-12-30 20:42:39 +01003247 }
Bram Moolenaared767a22016-01-03 22:49:16 +01003248 if (dict == NULL)
3249 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02003250 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01003251 p = p_shm;
3252 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01003253 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01003254 p_shm = p;
3255 }
3256 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003257#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01003258 if (dict != NULL)
3259 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02003260 dict_add_number(dict, "words", word_count);
3261 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003262 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003263 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3264 byte_count_cursor);
3265 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3266 char_count_cursor);
3267 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3268 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01003270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271}
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003272
3273/*
3274 * Handle indent and format operators and visual mode ":".
3275 */
3276 static void
3277op_colon(oparg_T *oap)
3278{
3279 stuffcharReadbuff(':');
3280 if (oap->is_VIsual)
3281 stuffReadbuff((char_u *)"'<,'>");
3282 else
3283 {
3284 // Make the range look nice, so it can be repeated.
3285 if (oap->start.lnum == curwin->w_cursor.lnum)
3286 stuffcharReadbuff('.');
3287 else
3288 stuffnumReadbuff((long)oap->start.lnum);
3289 if (oap->end.lnum != oap->start.lnum)
3290 {
3291 stuffcharReadbuff(',');
3292 if (oap->end.lnum == curwin->w_cursor.lnum)
3293 stuffcharReadbuff('.');
3294 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3295 stuffcharReadbuff('$');
3296 else if (oap->start.lnum == curwin->w_cursor.lnum)
3297 {
3298 stuffReadbuff((char_u *)".+");
3299 stuffnumReadbuff((long)oap->line_count - 1);
3300 }
3301 else
3302 stuffnumReadbuff((long)oap->end.lnum);
3303 }
3304 }
3305 if (oap->op_type != OP_COLON)
3306 stuffReadbuff((char_u *)"!");
3307 if (oap->op_type == OP_INDENT)
3308 {
3309#ifndef FEAT_CINDENT
3310 if (*get_equalprg() == NUL)
3311 stuffReadbuff((char_u *)"indent");
3312 else
3313#endif
3314 stuffReadbuff(get_equalprg());
3315 stuffReadbuff((char_u *)"\n");
3316 }
3317 else if (oap->op_type == OP_FORMAT)
3318 {
3319 if (*curbuf->b_p_fp != NUL)
3320 stuffReadbuff(curbuf->b_p_fp);
3321 else if (*p_fp != NUL)
3322 stuffReadbuff(p_fp);
3323 else
3324 stuffReadbuff((char_u *)"fmt");
3325 stuffReadbuff((char_u *)"\n']");
3326 }
3327
3328 // do_cmdline() does the rest
3329}
3330
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003331// callback function for 'operatorfunc'
3332static callback_T opfunc_cb;
3333
3334/*
3335 * Process the 'operatorfunc' option value.
3336 * Returns OK or FAIL.
3337 */
3338 int
3339set_operatorfunc_option(void)
3340{
3341 return option_set_callback_func(p_opfunc, &opfunc_cb);
3342}
3343
3344#if defined(EXITFREE) || defined(PROTO)
3345 void
3346free_operatorfunc_option(void)
3347{
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003348# ifdef FEAT_EVAL
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003349 free_callback(&opfunc_cb);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003350# endif
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003351}
3352#endif
3353
Dominique Pelle748b3082022-01-08 12:41:16 +00003354#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003355/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003356 * Mark the global 'operatorfunc' callback with 'copyID' so that it is not
3357 * garbage collected.
3358 */
3359 int
3360set_ref_in_opfunc(int copyID UNUSED)
3361{
3362 int abort = FALSE;
3363
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003364 abort = set_ref_in_callback(&opfunc_cb, copyID);
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003365
3366 return abort;
3367}
Dominique Pelle748b3082022-01-08 12:41:16 +00003368#endif
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00003369
3370/*
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003371 * Handle the "g@" operator: call 'operatorfunc'.
3372 */
3373 static void
3374op_function(oparg_T *oap UNUSED)
3375{
3376#ifdef FEAT_EVAL
3377 typval_T argv[2];
3378 int save_virtual_op = virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003379 int save_finish_op = finish_op;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003380 pos_T orig_start = curbuf->b_op_start;
3381 pos_T orig_end = curbuf->b_op_end;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003382 typval_T rettv;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003383
3384 if (*p_opfunc == NUL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00003385 emsg(_(e_operatorfunc_is_empty));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003386 else
3387 {
3388 // Set '[ and '] marks to text to be operated on.
3389 curbuf->b_op_start = oap->start;
3390 curbuf->b_op_end = oap->end;
3391 if (oap->motion_type != MLINE && !oap->inclusive)
3392 // Exclude the end position.
3393 decl(&curbuf->b_op_end);
3394
3395 argv[0].v_type = VAR_STRING;
3396 if (oap->block_mode)
3397 argv[0].vval.v_string = (char_u *)"block";
3398 else if (oap->motion_type == MLINE)
3399 argv[0].vval.v_string = (char_u *)"line";
3400 else
3401 argv[0].vval.v_string = (char_u *)"char";
3402 argv[1].v_type = VAR_UNKNOWN;
3403
3404 // Reset virtual_op so that 'virtualedit' can be changed in the
3405 // function.
3406 virtual_op = MAYBE;
3407
naohiro ono75c30e92021-10-19 11:15:41 +01003408 // Reset finish_op so that mode() returns the right value.
3409 finish_op = FALSE;
3410
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003411 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3412 clear_tv(&rettv);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003413
3414 virtual_op = save_virtual_op;
naohiro ono75c30e92021-10-19 11:15:41 +01003415 finish_op = save_finish_op;
Bram Moolenaare1004402020-10-24 20:49:43 +02003416 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003417 {
3418 curbuf->b_op_start = orig_start;
3419 curbuf->b_op_end = orig_end;
3420 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003421 }
3422#else
Bram Moolenaar677658a2022-01-05 16:09:06 +00003423 emsg(_(e_eval_feature_not_available));
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003424#endif
3425}
3426
3427/*
3428 * Calculate start/end virtual columns for operating in block mode.
3429 */
3430 static void
3431get_op_vcol(
3432 oparg_T *oap,
3433 colnr_T redo_VIsual_vcol,
3434 int initial) // when TRUE adjust position for 'selectmode'
3435{
3436 colnr_T start, end;
3437
3438 if (VIsual_mode != Ctrl_V
3439 || (!initial && oap->end.col < curwin->w_width))
3440 return;
3441
3442 oap->block_mode = TRUE;
3443
3444 // prevent from moving onto a trail byte
3445 if (has_mbyte)
3446 mb_adjustpos(curwin->w_buffer, &oap->end);
3447
3448 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3449
3450 if (!redo_VIsual_busy)
3451 {
3452 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3453
3454 if (start < oap->start_vcol)
3455 oap->start_vcol = start;
3456 if (end > oap->end_vcol)
3457 {
3458 if (initial && *p_sel == 'e' && start >= 1
3459 && start - 1 >= oap->end_vcol)
3460 oap->end_vcol = start - 1;
3461 else
3462 oap->end_vcol = end;
3463 }
3464 }
3465
3466 // if '$' was used, get oap->end_vcol from longest line
3467 if (curwin->w_curswant == MAXCOL)
3468 {
3469 curwin->w_cursor.col = MAXCOL;
3470 oap->end_vcol = 0;
3471 for (curwin->w_cursor.lnum = oap->start.lnum;
3472 curwin->w_cursor.lnum <= oap->end.lnum;
3473 ++curwin->w_cursor.lnum)
3474 {
3475 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3476 if (end > oap->end_vcol)
3477 oap->end_vcol = end;
3478 }
3479 }
3480 else if (redo_VIsual_busy)
3481 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3482 // Correct oap->end.col and oap->start.col to be the
3483 // upper-left and lower-right corner of the block area.
3484 //
3485 // (Actually, this does convert column positions into character
3486 // positions)
3487 curwin->w_cursor.lnum = oap->end.lnum;
3488 coladvance(oap->end_vcol);
3489 oap->end = curwin->w_cursor;
3490
3491 curwin->w_cursor = oap->start;
3492 coladvance(oap->start_vcol);
3493 oap->start = curwin->w_cursor;
3494}
3495
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003496// Information for redoing the previous Visual selection.
3497typedef struct {
3498 int rv_mode; // 'v', 'V', or Ctrl-V
3499 linenr_T rv_line_count; // number of lines
3500 colnr_T rv_vcol; // number of cols or end column
3501 long rv_count; // count for Visual operator
3502 int rv_arg; // extra argument
3503} redo_VIsual_T;
3504
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003505 static int
3506is_ex_cmdchar(cmdarg_T *cap)
3507{
3508 return cap->cmdchar == ':'
3509 || cap->cmdchar == K_COMMAND
3510 || cap->cmdchar == K_SCRIPT_COMMAND;
3511}
3512
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003513/*
3514 * Handle an operator after Visual mode or when the movement is finished.
3515 * "gui_yank" is true when yanking text for the clipboard.
3516 */
3517 void
3518do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3519{
3520 oparg_T *oap = cap->oap;
3521 pos_T old_cursor;
3522 int empty_region_error;
3523 int restart_edit_save;
3524#ifdef FEAT_LINEBREAK
3525 int lbr_saved = curwin->w_p_lbr;
3526#endif
3527
3528 // The visual area is remembered for redo
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003529 static redo_VIsual_T redo_VIsual = {NUL, 0, 0, 0,0};
3530
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003531 int include_line_break = FALSE;
3532
3533#if defined(FEAT_CLIPBOARD)
3534 // Yank the visual area into the GUI selection register before we operate
3535 // on it and lose it forever.
3536 // Don't do it if a specific register was specified, so that ""x"*P works.
3537 // This could call do_pending_operator() recursively, but that's OK
3538 // because gui_yank will be TRUE for the nested call.
3539 if ((clip_star.available || clip_plus.available)
3540 && oap->op_type != OP_NOP
3541 && !gui_yank
3542 && VIsual_active
3543 && !redo_VIsual_busy
3544 && oap->regname == 0)
3545 clip_auto_select();
3546#endif
3547 old_cursor = curwin->w_cursor;
3548
3549 // If an operation is pending, handle it...
3550 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3551 {
3552 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3553 // for the clipboard.
3554 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3555
3556#ifdef FEAT_LINEBREAK
3557 // Avoid a problem with unwanted linebreaks in block mode.
3558 if (curwin->w_p_lbr)
3559 curwin->w_valid &= ~VALID_VIRTCOL;
3560 curwin->w_p_lbr = FALSE;
3561#endif
3562 oap->is_VIsual = VIsual_active;
3563 if (oap->motion_force == 'V')
3564 oap->motion_type = MLINE;
3565 else if (oap->motion_force == 'v')
3566 {
3567 // If the motion was linewise, "inclusive" will not have been set.
3568 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3569 if (oap->motion_type == MLINE)
3570 oap->inclusive = FALSE;
3571 // If the motion already was characterwise, toggle "inclusive"
3572 else if (oap->motion_type == MCHAR)
3573 oap->inclusive = !oap->inclusive;
3574 oap->motion_type = MCHAR;
3575 }
3576 else if (oap->motion_force == Ctrl_V)
3577 {
3578 // Change line- or characterwise motion into Visual block mode.
3579 if (!VIsual_active)
3580 {
3581 VIsual_active = TRUE;
3582 VIsual = oap->start;
3583 }
3584 VIsual_mode = Ctrl_V;
3585 VIsual_select = FALSE;
3586 VIsual_reselect = FALSE;
3587 }
3588
3589 // Only redo yank when 'y' flag is in 'cpoptions'.
3590 // Never redo "zf" (define fold).
3591 if ((redo_yank || oap->op_type != OP_YANK)
3592 && ((!VIsual_active || oap->motion_force)
3593 // Also redo Operator-pending Visual mode mappings
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003594 || (VIsual_active
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003595 && is_ex_cmdchar(cap) && oap->op_type != OP_COLON))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003596 && cap->cmdchar != 'D'
3597#ifdef FEAT_FOLDING
3598 && oap->op_type != OP_FOLD
3599 && oap->op_type != OP_FOLDOPEN
3600 && oap->op_type != OP_FOLDOPENREC
3601 && oap->op_type != OP_FOLDCLOSE
3602 && oap->op_type != OP_FOLDCLOSEREC
3603 && oap->op_type != OP_FOLDDEL
3604 && oap->op_type != OP_FOLDDELREC
3605#endif
3606 )
3607 {
3608 prep_redo(oap->regname, cap->count0,
3609 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3610 oap->motion_force, cap->cmdchar, cap->nchar);
3611 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3612 {
3613 // If 'cpoptions' does not contain 'r', insert the search
3614 // pattern to really repeat the same command.
3615 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3616 AppendToRedobuffLit(cap->searchbuf, -1);
3617 AppendToRedobuff(NL_STR);
3618 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003619 else if (is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003620 {
3621 // do_cmdline() has stored the first typed line in
3622 // "repeat_cmdline". When several lines are typed repeating
3623 // won't be possible.
3624 if (repeat_cmdline == NULL)
3625 ResetRedobuff();
3626 else
3627 {
3628 AppendToRedobuffLit(repeat_cmdline, -1);
3629 AppendToRedobuff(NL_STR);
3630 VIM_CLEAR(repeat_cmdline);
3631 }
3632 }
3633 }
3634
3635 if (redo_VIsual_busy)
3636 {
3637 // Redo of an operation on a Visual area. Use the same size from
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003638 // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol.
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003639 oap->start = curwin->w_cursor;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003640 curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003641 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3642 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003643 VIsual_mode = redo_VIsual.rv_mode;
3644 if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003645 {
3646 if (VIsual_mode == 'v')
3647 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003648 if (redo_VIsual.rv_line_count <= 1)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003649 {
3650 validate_virtcol();
3651 curwin->w_curswant =
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003652 curwin->w_virtcol + redo_VIsual.rv_vcol - 1;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003653 }
3654 else
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003655 curwin->w_curswant = redo_VIsual.rv_vcol;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003656 }
3657 else
3658 {
3659 curwin->w_curswant = MAXCOL;
3660 }
3661 coladvance(curwin->w_curswant);
3662 }
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003663 cap->count0 = redo_VIsual.rv_count;
3664 if (redo_VIsual.rv_count != 0)
3665 cap->count1 = redo_VIsual.rv_count;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003666 else
3667 cap->count1 = 1;
3668 }
3669 else if (VIsual_active)
3670 {
3671 if (!gui_yank)
3672 {
3673 // Save the current VIsual area for '< and '> marks, and "gv"
3674 curbuf->b_visual.vi_start = VIsual;
3675 curbuf->b_visual.vi_end = curwin->w_cursor;
3676 curbuf->b_visual.vi_mode = VIsual_mode;
3677 restore_visual_mode();
3678 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3679# ifdef FEAT_EVAL
3680 curbuf->b_visual_mode_eval = VIsual_mode;
3681# endif
3682 }
3683
3684 // In Select mode, a linewise selection is operated upon like a
3685 // characterwise selection.
3686 // Special case: gH<Del> deletes the last line.
3687 if (VIsual_select && VIsual_mode == 'V'
3688 && cap->oap->op_type != OP_DELETE)
3689 {
3690 if (LT_POS(VIsual, curwin->w_cursor))
3691 {
3692 VIsual.col = 0;
3693 curwin->w_cursor.col =
3694 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3695 }
3696 else
3697 {
3698 curwin->w_cursor.col = 0;
3699 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3700 }
3701 VIsual_mode = 'v';
3702 }
3703 // If 'selection' is "exclusive", backup one character for
3704 // charwise selections.
3705 else if (VIsual_mode == 'v')
3706 include_line_break = unadjust_for_sel();
3707
3708 oap->start = VIsual;
3709 if (VIsual_mode == 'V')
3710 {
3711 oap->start.col = 0;
3712 oap->start.coladd = 0;
3713 }
3714 }
3715
3716 // Set oap->start to the first position of the operated text, oap->end
3717 // to the end of the operated text. w_cursor is equal to oap->start.
3718 if (LT_POS(oap->start, curwin->w_cursor))
3719 {
3720#ifdef FEAT_FOLDING
3721 // Include folded lines completely.
3722 if (!VIsual_active)
3723 {
3724 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3725 oap->start.col = 0;
Bram Moolenaar3b681232019-12-13 19:35:55 +01003726 if ((curwin->w_cursor.col > 0 || oap->inclusive
3727 || oap->motion_type == MLINE)
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003728 && hasFolding(curwin->w_cursor.lnum, NULL,
3729 &curwin->w_cursor.lnum))
3730 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3731 }
3732#endif
3733 oap->end = curwin->w_cursor;
3734 curwin->w_cursor = oap->start;
3735
3736 // w_virtcol may have been updated; if the cursor goes back to its
3737 // previous position w_virtcol becomes invalid and isn't updated
3738 // automatically.
3739 curwin->w_valid &= ~VALID_VIRTCOL;
3740 }
3741 else
3742 {
3743#ifdef FEAT_FOLDING
3744 // Include folded lines completely.
3745 if (!VIsual_active && oap->motion_type == MLINE)
3746 {
3747 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3748 NULL))
3749 curwin->w_cursor.col = 0;
3750 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3751 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3752 }
3753#endif
3754 oap->end = oap->start;
3755 oap->start = curwin->w_cursor;
3756 }
3757
3758 // Just in case lines were deleted that make the position invalid.
3759 check_pos(curwin->w_buffer, &oap->end);
3760 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3761
3762 // Set "virtual_op" before resetting VIsual_active.
3763 virtual_op = virtual_active();
3764
3765 if (VIsual_active || redo_VIsual_busy)
3766 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003767 get_op_vcol(oap, redo_VIsual.rv_vcol, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003768
3769 if (!redo_VIsual_busy && !gui_yank)
3770 {
3771 // Prepare to reselect and redo Visual: this is based on the
3772 // size of the Visual text
3773 resel_VIsual_mode = VIsual_mode;
3774 if (curwin->w_curswant == MAXCOL)
3775 resel_VIsual_vcol = MAXCOL;
3776 else
3777 {
3778 if (VIsual_mode != Ctrl_V)
3779 getvvcol(curwin, &(oap->end),
3780 NULL, NULL, &oap->end_vcol);
3781 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3782 {
3783 if (VIsual_mode != Ctrl_V)
3784 getvvcol(curwin, &(oap->start),
3785 &oap->start_vcol, NULL, NULL);
3786 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3787 }
3788 else
3789 resel_VIsual_vcol = oap->end_vcol;
3790 }
3791 resel_VIsual_line_count = oap->line_count;
3792 }
3793
3794 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3795 if ((redo_yank || oap->op_type != OP_YANK)
3796 && oap->op_type != OP_COLON
3797#ifdef FEAT_FOLDING
3798 && oap->op_type != OP_FOLD
3799 && oap->op_type != OP_FOLDOPEN
3800 && oap->op_type != OP_FOLDOPENREC
3801 && oap->op_type != OP_FOLDCLOSE
3802 && oap->op_type != OP_FOLDCLOSEREC
3803 && oap->op_type != OP_FOLDDEL
3804 && oap->op_type != OP_FOLDDELREC
3805#endif
3806 && oap->motion_force == NUL
3807 )
3808 {
3809 // Prepare for redoing. Only use the nchar field for "r",
3810 // otherwise it might be the second char of the operator.
3811 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3812 || cap->nchar == 'N'))
3813 prep_redo(oap->regname, cap->count0,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003814 get_op_char(oap->op_type),
3815 get_extra_op_char(oap->op_type),
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003816 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003817 else if (!is_ex_cmdchar(cap))
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003818 {
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003819 int opchar = get_op_char(oap->op_type);
3820 int extra_opchar = get_extra_op_char(oap->op_type);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003821 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3822
3823 // reverse what nv_replace() did
3824 if (nchar == REPLACE_CR_NCHAR)
3825 nchar = CAR;
3826 else if (nchar == REPLACE_NL_NCHAR)
3827 nchar = NL;
Bram Moolenaar2228cd72021-11-22 14:16:08 +00003828
3829 if (opchar == 'g' && extra_opchar == '@')
3830 // also repeat the count for 'operatorfunc'
3831 prep_redo_num2(oap->regname, 0L, NUL, 'v',
3832 cap->count0, opchar, extra_opchar, nchar);
3833 else
3834 prep_redo(oap->regname, 0L, NUL, 'v',
3835 opchar, extra_opchar, nchar);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003836 }
3837 if (!redo_VIsual_busy)
3838 {
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00003839 redo_VIsual.rv_mode = resel_VIsual_mode;
3840 redo_VIsual.rv_vcol = resel_VIsual_vcol;
3841 redo_VIsual.rv_line_count = resel_VIsual_line_count;
3842 redo_VIsual.rv_count = cap->count0;
3843 redo_VIsual.rv_arg = cap->arg;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003844 }
3845 }
3846
3847 // oap->inclusive defaults to TRUE.
3848 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3849 // FALSE. This makes "d}P" and "v}dP" work the same.
3850 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3851 oap->inclusive = TRUE;
3852 if (VIsual_mode == 'V')
3853 oap->motion_type = MLINE;
3854 else
3855 {
3856 oap->motion_type = MCHAR;
3857 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3858 && (include_line_break || !virtual_op))
3859 {
3860 oap->inclusive = FALSE;
3861 // Try to include the newline, unless it's an operator
3862 // that works on lines only.
3863 if (*p_sel != 'o'
3864 && !op_on_lines(oap->op_type)
3865 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3866 {
3867 ++oap->end.lnum;
3868 oap->end.col = 0;
3869 oap->end.coladd = 0;
3870 ++oap->line_count;
3871 }
3872 }
3873 }
3874
3875 redo_VIsual_busy = FALSE;
3876
3877 // Switch Visual off now, so screen updating does
3878 // not show inverted text when the screen is redrawn.
3879 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3880 // no screen redraw, so it is done here to remove the inverted
3881 // part.
3882 if (!gui_yank)
3883 {
3884 VIsual_active = FALSE;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003885 setmouse();
3886 mouse_dragging = 0;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02003887 may_clear_cmdline();
3888 if ((oap->op_type == OP_YANK
3889 || oap->op_type == OP_COLON
3890 || oap->op_type == OP_FUNCTION
3891 || oap->op_type == OP_FILTER)
3892 && oap->motion_force == NUL)
3893 {
3894#ifdef FEAT_LINEBREAK
3895 // make sure redrawing is correct
3896 curwin->w_p_lbr = lbr_saved;
3897#endif
3898 redraw_curbuf_later(INVERTED);
3899 }
3900 }
3901 }
3902
3903 // Include the trailing byte of a multi-byte char.
3904 if (has_mbyte && oap->inclusive)
3905 {
3906 int l;
3907
3908 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3909 if (l > 1)
3910 oap->end.col += l - 1;
3911 }
3912 curwin->w_set_curswant = TRUE;
3913
3914 // oap->empty is set when start and end are the same. The inclusive
3915 // flag affects this too, unless yanking and the end is on a NUL.
3916 oap->empty = (oap->motion_type == MCHAR
3917 && (!oap->inclusive
3918 || (oap->op_type == OP_YANK
3919 && gchar_pos(&oap->end) == NUL))
3920 && EQUAL_POS(oap->start, oap->end)
3921 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3922 // For delete, change and yank, it's an error to operate on an
3923 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3924 empty_region_error = (oap->empty
3925 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3926
3927 // Force a redraw when operating on an empty Visual region, when
3928 // 'modifiable is off or creating a fold.
3929 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3930#ifdef FEAT_FOLDING
3931 || oap->op_type == OP_FOLD
3932#endif
3933 ))
3934 {
3935#ifdef FEAT_LINEBREAK
3936 curwin->w_p_lbr = lbr_saved;
3937#endif
3938 redraw_curbuf_later(INVERTED);
3939 }
3940
3941 // If the end of an operator is in column one while oap->motion_type
3942 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3943 // character in the previous line. If op_start is on or before the
3944 // first non-blank in the line, the operator becomes linewise
3945 // (strange, but that's the way vi does it).
3946 if ( oap->motion_type == MCHAR
3947 && oap->inclusive == FALSE
3948 && !(cap->retval & CA_NO_ADJ_OP_END)
3949 && oap->end.col == 0
3950 && (!oap->is_VIsual || *p_sel == 'o')
3951 && !oap->block_mode
3952 && oap->line_count > 1)
3953 {
3954 oap->end_adjusted = TRUE; // remember that we did this
3955 --oap->line_count;
3956 --oap->end.lnum;
3957 if (inindent(0))
3958 oap->motion_type = MLINE;
3959 else
3960 {
3961 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3962 if (oap->end.col)
3963 {
3964 --oap->end.col;
3965 oap->inclusive = TRUE;
3966 }
3967 }
3968 }
3969 else
3970 oap->end_adjusted = FALSE;
3971
3972 switch (oap->op_type)
3973 {
3974 case OP_LSHIFT:
3975 case OP_RSHIFT:
3976 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3977 auto_format(FALSE, TRUE);
3978 break;
3979
3980 case OP_JOIN_NS:
3981 case OP_JOIN:
3982 if (oap->line_count < 2)
3983 oap->line_count = 2;
3984 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3985 curbuf->b_ml.ml_line_count)
3986 beep_flush();
3987 else
3988 {
3989 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3990 TRUE, TRUE, TRUE);
3991 auto_format(FALSE, TRUE);
3992 }
3993 break;
3994
3995 case OP_DELETE:
3996 VIsual_reselect = FALSE; // don't reselect now
3997 if (empty_region_error)
3998 {
3999 vim_beep(BO_OPER);
4000 CancelRedo();
4001 }
4002 else
4003 {
4004 (void)op_delete(oap);
Bram Moolenaar55466882020-11-21 14:16:22 +01004005 // save cursor line for undo if it wasn't saved yet
4006 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
4007 && u_save_cursor() == OK)
4008 auto_format(FALSE, TRUE);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004009 }
4010 break;
4011
4012 case OP_YANK:
4013 if (empty_region_error)
4014 {
4015 if (!gui_yank)
4016 {
4017 vim_beep(BO_OPER);
4018 CancelRedo();
4019 }
4020 }
4021 else
4022 {
4023#ifdef FEAT_LINEBREAK
4024 curwin->w_p_lbr = lbr_saved;
4025#endif
Christian Brabandt544a38e2021-06-10 19:39:11 +02004026 oap->excl_tr_ws = cap->cmdchar == 'z';
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004027 (void)op_yank(oap, FALSE, !gui_yank);
4028 }
4029 check_cursor_col();
4030 break;
4031
4032 case OP_CHANGE:
4033 VIsual_reselect = FALSE; // don't reselect now
4034 if (empty_region_error)
4035 {
4036 vim_beep(BO_OPER);
4037 CancelRedo();
4038 }
4039 else
4040 {
4041 // This is a new edit command, not a restart. Need to
4042 // remember it to make 'insertmode' work with mappings for
4043 // Visual mode. But do this only once and not when typed and
4044 // 'insertmode' isn't set.
4045 if (p_im || !KeyTyped)
4046 restart_edit_save = restart_edit;
4047 else
4048 restart_edit_save = 0;
4049 restart_edit = 0;
4050#ifdef FEAT_LINEBREAK
4051 // Restore linebreak, so that when the user edits it looks as
4052 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004053 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004054#endif
4055 // Reset finish_op now, don't want it set inside edit().
4056 finish_op = FALSE;
4057 if (op_change(oap)) // will call edit()
4058 cap->retval |= CA_COMMAND_BUSY;
4059 if (restart_edit == 0)
4060 restart_edit = restart_edit_save;
4061 }
4062 break;
4063
4064 case OP_FILTER:
4065 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4066 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4067 else
4068 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4069 // FALLTHROUGH
4070
4071 case OP_INDENT:
4072 case OP_COLON:
4073
4074#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4075 // If 'equalprg' is empty, do the indenting internally.
4076 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4077 {
4078# ifdef FEAT_LISP
4079 if (curbuf->b_p_lisp)
4080 {
4081 op_reindent(oap, get_lisp_indent);
4082 break;
4083 }
4084# endif
4085# ifdef FEAT_CINDENT
4086 op_reindent(oap,
4087# ifdef FEAT_EVAL
4088 *curbuf->b_p_inde != NUL ? get_expr_indent :
4089# endif
4090 get_c_indent);
4091 break;
4092# endif
4093 }
4094#endif
4095
4096 op_colon(oap);
4097 break;
4098
4099 case OP_TILDE:
4100 case OP_UPPER:
4101 case OP_LOWER:
4102 case OP_ROT13:
4103 if (empty_region_error)
4104 {
4105 vim_beep(BO_OPER);
4106 CancelRedo();
4107 }
4108 else
4109 op_tilde(oap);
4110 check_cursor_col();
4111 break;
4112
4113 case OP_FORMAT:
4114#if defined(FEAT_EVAL)
4115 if (*curbuf->b_p_fex != NUL)
4116 op_formatexpr(oap); // use expression
4117 else
4118#endif
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004119 {
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004120 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02004121 op_colon(oap); // use external command
4122 else
4123 op_format(oap, FALSE); // use internal function
4124 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004125 break;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004126 case OP_FORMAT2:
4127 op_format(oap, TRUE); // use internal function
4128 break;
4129
4130 case OP_FUNCTION:
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004131 {
4132 redo_VIsual_T save_redo_VIsual = redo_VIsual;
4133
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004134#ifdef FEAT_LINEBREAK
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004135 // Restore linebreak, so that when the user edits it looks as
4136 // before.
4137 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004138#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004139 // call 'operatorfunc'
4140 op_function(oap);
4141
4142 // Restore the info for redoing Visual mode, the function may
4143 // invoke another operator and unintentionally change it.
4144 redo_VIsual = save_redo_VIsual;
4145 break;
4146 }
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004147
4148 case OP_INSERT:
4149 case OP_APPEND:
4150 VIsual_reselect = FALSE; // don't reselect now
4151 if (empty_region_error)
4152 {
4153 vim_beep(BO_OPER);
4154 CancelRedo();
4155 }
4156 else
4157 {
4158 // This is a new edit command, not a restart. Need to
4159 // remember it to make 'insertmode' work with mappings for
4160 // Visual mode. But do this only once.
4161 restart_edit_save = restart_edit;
4162 restart_edit = 0;
4163#ifdef FEAT_LINEBREAK
4164 // Restore linebreak, so that when the user edits it looks as
4165 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004166 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004167#endif
4168 op_insert(oap, cap->count1);
4169#ifdef FEAT_LINEBREAK
4170 // Reset linebreak, so that formatting works correctly.
4171 curwin->w_p_lbr = FALSE;
4172#endif
4173
4174 // TODO: when inserting in several lines, should format all
4175 // the lines.
4176 auto_format(FALSE, TRUE);
4177
4178 if (restart_edit == 0)
4179 restart_edit = restart_edit_save;
4180 else
4181 cap->retval |= CA_COMMAND_BUSY;
4182 }
4183 break;
4184
4185 case OP_REPLACE:
4186 VIsual_reselect = FALSE; // don't reselect now
4187 if (empty_region_error)
4188 {
4189 vim_beep(BO_OPER);
4190 CancelRedo();
4191 }
4192 else
4193 {
4194#ifdef FEAT_LINEBREAK
4195 // Restore linebreak, so that when the user edits it looks as
4196 // before.
Bram Moolenaar03c3bd92020-01-23 20:58:09 +01004197 curwin->w_p_lbr = lbr_saved;
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004198#endif
4199 op_replace(oap, cap->nchar);
4200 }
4201 break;
4202
4203#ifdef FEAT_FOLDING
4204 case OP_FOLD:
4205 VIsual_reselect = FALSE; // don't reselect now
4206 foldCreate(oap->start.lnum, oap->end.lnum);
4207 break;
4208
4209 case OP_FOLDOPEN:
4210 case OP_FOLDOPENREC:
4211 case OP_FOLDCLOSE:
4212 case OP_FOLDCLOSEREC:
4213 VIsual_reselect = FALSE; // don't reselect now
4214 opFoldRange(oap->start.lnum, oap->end.lnum,
4215 oap->op_type == OP_FOLDOPEN
4216 || oap->op_type == OP_FOLDOPENREC,
4217 oap->op_type == OP_FOLDOPENREC
4218 || oap->op_type == OP_FOLDCLOSEREC,
4219 oap->is_VIsual);
4220 break;
4221
4222 case OP_FOLDDEL:
4223 case OP_FOLDDELREC:
4224 VIsual_reselect = FALSE; // don't reselect now
4225 deleteFold(oap->start.lnum, oap->end.lnum,
4226 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4227 break;
4228#endif
4229 case OP_NR_ADD:
4230 case OP_NR_SUB:
4231 if (empty_region_error)
4232 {
4233 vim_beep(BO_OPER);
4234 CancelRedo();
4235 }
4236 else
4237 {
4238 VIsual_active = TRUE;
4239#ifdef FEAT_LINEBREAK
4240 curwin->w_p_lbr = lbr_saved;
4241#endif
Bram Moolenaarb3bd1d32022-01-02 13:05:45 +00004242 op_addsub(oap, cap->count1, redo_VIsual.rv_arg);
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02004243 VIsual_active = FALSE;
4244 }
4245 check_cursor_col();
4246 break;
4247 default:
4248 clearopbeep(oap);
4249 }
4250 virtual_op = MAYBE;
4251 if (!gui_yank)
4252 {
4253 // if 'sol' not set, go back to old column for some commands
4254 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4255 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4256 || oap->op_type == OP_DELETE))
4257 {
4258#ifdef FEAT_LINEBREAK
4259 curwin->w_p_lbr = FALSE;
4260#endif
4261 coladvance(curwin->w_curswant = old_col);
4262 }
4263 }
4264 else
4265 {
4266 curwin->w_cursor = old_cursor;
4267 }
4268 oap->block_mode = FALSE;
4269 clearop(oap);
4270 motion_force = NUL;
4271 }
4272#ifdef FEAT_LINEBREAK
4273 curwin->w_p_lbr = lbr_saved;
4274#endif
4275}